Annotation of loncom/lond, revision 1.100
1.1 albertel 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
1.60 www 4: #
1.100 ! matthew 5: # $Id: lond,v 1.99 2002/09/20 02:04:07 foxr Exp $
1.60 www 6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
28: #
1.1 albertel 29: # 5/26/99,6/4,6/10,6/11,6/14,6/15,6/26,6/28,6/30,
1.2 www 30: # 7/8,7/9,7/10,7/12,7/17,7/19,9/21,
1.6 www 31: # 10/7,10/8,10/9,10/11,10/13,10/15,11/4,11/16,
1.11 www 32: # 12/7,12/15,01/06,01/11,01/12,01/14,2/8,
1.12 harris41 33: # 03/07,05/31 Gerd Kortemeyer
1.13 www 34: # 06/26 Scott Harrison
1.20 www 35: # 06/29,06/30,07/14,07/15,07/17,07/20,07/25,09/18 Gerd Kortemeyer
1.25 www 36: # 12/05 Scott Harrison
1.34 www 37: # 12/05,12/13,12/29 Gerd Kortemeyer
1.61 harris41 38: # YEAR=2001
1.36 www 39: # Jan 01 Scott Harrison
40: # 02/12 Gerd Kortemeyer
1.37 harris41 41: # 03/15 Scott Harrison
1.41 www 42: # 03/24 Gerd Kortemeyer
1.47 www 43: # 04/02 Scott Harrison
1.51 www 44: # 05/11,05/28,08/30 Gerd Kortemeyer
1.56 harris41 45: # 9/30,10/22,11/13,11/15,11/16 Scott Harrison
1.59 www 46: # 11/26,11/27 Gerd Kortemeyer
1.61 harris41 47: # 12/20 Scott Harrison
1.62 www 48: # 12/22 Gerd Kortemeyer
1.63 www 49: # YEAR=2002
1.65 www 50: # 01/20/02,02/05 Gerd Kortemeyer
1.71 www 51: # 02/05 Guy Albertelli
52: # 02/07 Scott Harrison
53: # 02/12 Gerd Kortemeyer
1.73 www 54: # 02/19 Matthew Hall
55: # 02/25 Gerd Kortemeyer
1.80 harris41 56: # 05/11 Scott Harrison
1.54 harris41 57: ###
58:
1.1 albertel 59: # based on "Perl Cookbook" ISBN 1-56592-243-3
60: # preforker - server who forks first
61: # runs as a daemon
62: # HUPs
63: # uses IDEA encryption
64:
1.80 harris41 65: use lib '/home/httpd/lib/perl/';
66: use LONCAPA::Configuration;
67:
1.1 albertel 68: use IO::Socket;
69: use IO::File;
70: use Apache::File;
71: use Symbol;
72: use POSIX;
73: use Crypt::IDEA;
74: use LWP::UserAgent();
1.3 www 75: use GDBM_File;
76: use Authen::Krb4;
1.91 albertel 77: use Authen::Krb5;
1.49 albertel 78: use lib '/home/httpd/lib/perl/';
79: use localauth;
1.1 albertel 80:
1.77 foxr 81: my $DEBUG = 0; # Non zero to enable debug log entries.
82:
1.57 www 83: my $status='';
84: my $lastlog='';
85:
1.96 foxr 86: #
87: # The array below are password error strings."
88: #
1.97 foxr 89: my $lastpwderror = 13; # Largest error number from lcpasswd.
1.96 foxr 90: my @passwderrors = ("ok",
91: "lcpasswd must be run as user 'www'",
92: "lcpasswd got incorrect number of arguments",
93: "lcpasswd did not get the right nubmer of input text lines",
94: "lcpasswd too many simultaneous pwd changes in progress",
95: "lcpasswd User does not exist.",
96: "lcpasswd Incorrect current passwd",
97: "lcpasswd Unable to su to root.",
98: "lcpasswd Cannot set new passwd.",
99: "lcpasswd Username has invalid characters",
1.97 foxr 100: "lcpasswd Invalid characters in password",
101: "11", "12",
102: "lcpasswd Password mismatch");
103:
104:
105: # The array below are lcuseradd error strings.:
106:
107: my $lastadderror = 13;
108: my @adderrors = ("ok",
109: "User ID mismatch, lcuseradd must run as user www",
110: "lcuseradd Incorrect number of command line parameters must be 3",
111: "lcuseradd Incorrect number of stdinput lines, must be 3",
112: "lcuseradd Too many other simultaneous pwd changes in progress",
113: "lcuseradd User does not exist",
114: "lcuseradd Unabel to mak ewww member of users's group",
115: "lcuseradd Unable to su to root",
116: "lcuseradd Unable to set password",
117: "lcuseradd Usrname has invbalid charcters",
118: "lcuseradd Password has an invalid character",
119: "lcuseradd User already exists",
120: "lcuseradd Could not add user.",
121: "lcuseradd Password mismatch");
122:
1.96 foxr 123:
124: #
125: # Convert an error return code from lcpasswd to a string value.
126: #
127: sub lcpasswdstrerror {
128: my $ErrorCode = shift;
1.97 foxr 129: if(($ErrorCode < 0) || ($ErrorCode > $lastpwderror)) {
1.96 foxr 130: return "lcpasswd Unrecognized error return value ".$ErrorCode;
131: } else {
1.98 foxr 132: return $passwderrors[$ErrorCode];
1.96 foxr 133: }
134: }
135:
1.97 foxr 136: #
137: # Convert an error return code from lcuseradd to a string value:
138: #
139: sub lcuseraddstrerror {
140: my $ErrorCode = shift;
141: if(($ErrorCode < 0) || ($ErrorCode > $lastadderror)) {
142: return "lcuseradd - Unrecognized error code: ".$ErrorCode;
143: } else {
1.98 foxr 144: return $adderrors[$ErrorCode];
1.97 foxr 145: }
146: }
147:
1.23 harris41 148: # grabs exception and records it to log before exiting
149: sub catchexception {
1.27 albertel 150: my ($error)=@_;
1.25 www 151: $SIG{'QUIT'}='DEFAULT';
152: $SIG{__DIE__}='DEFAULT';
1.23 harris41 153: &logthis("<font color=red>CRITICAL: "
154: ."ABNORMAL EXIT. Child $$ for server $wasserver died through "
1.27 albertel 155: ."a crash with this error msg->[$error]</font>");
1.57 www 156: &logthis('Famous last words: '.$status.' - '.$lastlog);
1.27 albertel 157: if ($client) { print $client "error: $error\n"; }
1.59 www 158: $server->close();
1.27 albertel 159: die($error);
1.23 harris41 160: }
161:
1.63 www 162: sub timeout {
163: &logthis("<font color=ref>CRITICAL: TIME OUT ".$$."</font>");
164: &catchexception('Timeout');
165: }
1.22 harris41 166: # -------------------------------- Set signal handlers to record abnormal exits
167:
168: $SIG{'QUIT'}=\&catchexception;
169: $SIG{__DIE__}=\&catchexception;
170:
1.81 matthew 171: # ---------------------------------- Read loncapa_apache.conf and loncapa.conf
1.95 harris41 172: &status("Read loncapa.conf and loncapa_apache.conf");
173: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
1.80 harris41 174: my %perlvar=%{$perlvarref};
175: undef $perlvarref;
1.19 www 176:
1.35 harris41 177: # ----------------------------- Make sure this process is running from user=www
178: my $wwwid=getpwnam('www');
179: if ($wwwid!=$<) {
180: $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
181: $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
1.37 harris41 182: system("echo 'User ID mismatch. lond must be run as user www.' |\
1.35 harris41 183: mailto $emailto -s '$subj' > /dev/null");
184: exit 1;
185: }
186:
1.19 www 187: # --------------------------------------------- Check if other instance running
188:
189: my $pidfile="$perlvar{'lonDaemons'}/logs/lond.pid";
190:
191: if (-e $pidfile) {
192: my $lfh=IO::File->new("$pidfile");
193: my $pide=<$lfh>;
194: chomp($pide);
1.29 harris41 195: if (kill 0 => $pide) { die "already running"; }
1.19 www 196: }
1.1 albertel 197:
198: $PREFORK=4; # number of children to maintain, at least four spare
199:
200: # ------------------------------------------------------------- Read hosts file
201:
1.29 harris41 202: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
1.1 albertel 203:
204: while ($configline=<CONFIG>) {
205: my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
1.70 harris41 206: chomp($ip); $ip=~s/\D+$//;
1.1 albertel 207: $hostid{$ip}=$id;
208: if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
209: $PREFORK++;
210: }
211: close(CONFIG);
212:
213: # establish SERVER socket, bind and listen.
214: $server = IO::Socket::INET->new(LocalPort => $perlvar{'londPort'},
215: Type => SOCK_STREAM,
216: Proto => 'tcp',
217: Reuse => 1,
218: Listen => 10 )
1.29 harris41 219: or die "making socket: $@\n";
1.1 albertel 220:
221: # --------------------------------------------------------- Do global variables
222:
223: # global variables
224:
1.75 foxr 225: $MAX_CLIENTS_PER_CHILD = 50; # number of clients each child should
1.1 albertel 226: # process
227: %children = (); # keys are current child process IDs
228: $children = 0; # current number of children
229:
230: sub REAPER { # takes care of dead children
231: $SIG{CHLD} = \&REAPER;
232: my $pid = wait;
1.67 albertel 233: if (defined($children{$pid})) {
234: &logthis("Child $pid died");
235: $children --;
236: delete $children{$pid};
237: } else {
238: &logthis("Unknown Child $pid died");
239: }
1.1 albertel 240: }
241:
242: sub HUNTSMAN { # signal handler for SIGINT
243: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
244: kill 'INT' => keys %children;
1.59 www 245: &logthis("Free socket: ".shutdown($server,2)); # free up socket
1.1 albertel 246: my $execdir=$perlvar{'lonDaemons'};
247: unlink("$execdir/logs/lond.pid");
1.9 www 248: &logthis("<font color=red>CRITICAL: Shutting down</font>");
1.1 albertel 249: exit; # clean up with dignity
250: }
251:
252: sub HUPSMAN { # signal handler for SIGHUP
253: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
254: kill 'INT' => keys %children;
1.59 www 255: &logthis("Free socket: ".shutdown($server,2)); # free up socket
1.9 www 256: &logthis("<font color=red>CRITICAL: Restarting</font>");
1.30 harris41 257: unlink("$execdir/logs/lond.pid");
1.1 albertel 258: my $execdir=$perlvar{'lonDaemons'};
259: exec("$execdir/lond"); # here we go again
260: }
261:
1.57 www 262: sub checkchildren {
263: &initnewstatus();
264: &logstatus();
265: &logthis('Going to check on the children');
1.63 www 266: $docdir=$perlvar{'lonDocRoot'};
1.61 harris41 267: foreach (sort keys %children) {
1.57 www 268: sleep 1;
269: unless (kill 'USR1' => $_) {
270: &logthis ('Child '.$_.' is dead');
271: &logstatus($$.' is dead');
272: }
1.61 harris41 273: }
1.63 www 274: sleep 5;
275: foreach (sort keys %children) {
276: unless (-e "$docdir/lon-status/londchld/$_.txt") {
277: &logthis('Child '.$_.' did not respond');
1.67 albertel 278: kill 9 => $_;
279: $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
280: $subj="LON: $perlvar{'lonHostID'} killed lond process $_";
1.68 albertel 281: my $result=`echo 'Killed lond process $_.' | mailto $emailto -s '$subj' > /dev/null`;
1.66 www 282: $execdir=$perlvar{'lonDaemons'};
1.67 albertel 283: $result=`/bin/cp $execdir/logs/lond.log $execdir/logs/lond.log.$_`
1.63 www 284: }
285: }
1.57 www 286: }
287:
1.1 albertel 288: # --------------------------------------------------------------------- Logging
289:
290: sub logthis {
291: my $message=shift;
292: my $execdir=$perlvar{'lonDaemons'};
293: my $fh=IO::File->new(">>$execdir/logs/lond.log");
294: my $now=time;
295: my $local=localtime($now);
1.58 www 296: $lastlog=$local.': '.$message;
1.1 albertel 297: print $fh "$local ($$): $message\n";
298: }
299:
1.77 foxr 300: # ------------------------- Conditional log if $DEBUG true.
301: sub Debug {
302: my $message = shift;
303: if($DEBUG) {
304: &logthis($message);
305: }
306: }
1.57 www 307: # ------------------------------------------------------------------ Log status
308:
309: sub logstatus {
310: my $docdir=$perlvar{'lonDocRoot'};
1.63 www 311: {
1.57 www 312: my $fh=IO::File->new(">>$docdir/lon-status/londstatus.txt");
313: print $fh $$."\t".$status."\t".$lastlog."\n";
1.63 www 314: $fh->close();
315: }
316: {
317: my $fh=IO::File->new(">$docdir/lon-status/londchld/$$.txt");
318: print $fh $status."\n".$lastlog."\n".time;
319: $fh->close();
320: }
1.57 www 321: }
322:
323: sub initnewstatus {
324: my $docdir=$perlvar{'lonDocRoot'};
325: my $fh=IO::File->new(">$docdir/lon-status/londstatus.txt");
326: my $now=time;
327: my $local=localtime($now);
328: print $fh "LOND status $local - parent $$\n\n";
1.64 www 329: opendir(DIR,"$docdir/lon-status/londchld");
330: while ($filename=readdir(DIR)) {
331: unlink("$docdir/lon-status/londchld/$filename");
332: }
333: closedir(DIR);
1.57 www 334: }
335:
336: # -------------------------------------------------------------- Status setting
337:
338: sub status {
339: my $what=shift;
340: my $now=time;
341: my $local=localtime($now);
342: $status=$local.': '.$what;
343: }
1.11 www 344:
345: # -------------------------------------------------------- Escape Special Chars
346:
347: sub escape {
348: my $str=shift;
349: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
350: return $str;
351: }
352:
353: # ----------------------------------------------------- Un-Escape Special Chars
354:
355: sub unescape {
356: my $str=shift;
357: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
358: return $str;
359: }
360:
1.1 albertel 361: # ----------------------------------------------------------- Send USR1 to lonc
362:
363: sub reconlonc {
364: my $peerfile=shift;
365: &logthis("Trying to reconnect for $peerfile");
366: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
367: if (my $fh=IO::File->new("$loncfile")) {
368: my $loncpid=<$fh>;
369: chomp($loncpid);
370: if (kill 0 => $loncpid) {
371: &logthis("lonc at pid $loncpid responding, sending USR1");
372: kill USR1 => $loncpid;
1.75 foxr 373: sleep 5;
1.1 albertel 374: if (-e "$peerfile") { return; }
375: &logthis("$peerfile still not there, give it another try");
1.75 foxr 376: sleep 10;
1.1 albertel 377: if (-e "$peerfile") { return; }
1.9 www 378: &logthis(
379: "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
1.1 albertel 380: } else {
1.9 www 381: &logthis(
382: "<font color=red>CRITICAL: "
383: ."lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 384: }
385: } else {
1.9 www 386: &logthis('<font color=red>CRITICAL: lonc not running, giving up</font>');
1.1 albertel 387: }
388: }
389:
390: # -------------------------------------------------- Non-critical communication
1.11 www 391:
1.1 albertel 392: sub subreply {
393: my ($cmd,$server)=@_;
394: my $peerfile="$perlvar{'lonSockDir'}/$server";
395: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
396: Type => SOCK_STREAM,
397: Timeout => 10)
398: or return "con_lost";
399: print $sclient "$cmd\n";
400: my $answer=<$sclient>;
401: chomp($answer);
402: if (!$answer) { $answer="con_lost"; }
403: return $answer;
404: }
405:
406: sub reply {
407: my ($cmd,$server)=@_;
408: my $answer;
409: if ($server ne $perlvar{'lonHostID'}) {
410: $answer=subreply($cmd,$server);
411: if ($answer eq 'con_lost') {
412: $answer=subreply("ping",$server);
413: if ($answer ne $server) {
1.75 foxr 414: &logthis("sub reply: answer != server");
1.1 albertel 415: &reconlonc("$perlvar{'lonSockDir'}/$server");
416: }
417: $answer=subreply($cmd,$server);
418: }
419: } else {
420: $answer='self_reply';
421: }
422: return $answer;
423: }
424:
1.13 www 425: # -------------------------------------------------------------- Talk to lonsql
426:
1.12 harris41 427: sub sqlreply {
428: my ($cmd)=@_;
429: my $answer=subsqlreply($cmd);
430: if ($answer eq 'con_lost') { $answer=subsqlreply($cmd); }
431: return $answer;
432: }
433:
434: sub subsqlreply {
435: my ($cmd)=@_;
436: my $unixsock="mysqlsock";
437: my $peerfile="$perlvar{'lonSockDir'}/$unixsock";
438: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
439: Type => SOCK_STREAM,
440: Timeout => 10)
441: or return "con_lost";
442: print $sclient "$cmd\n";
443: my $answer=<$sclient>;
444: chomp($answer);
445: if (!$answer) { $answer="con_lost"; }
446: return $answer;
447: }
448:
1.1 albertel 449: # -------------------------------------------- Return path to profile directory
1.11 www 450:
1.1 albertel 451: sub propath {
452: my ($udom,$uname)=@_;
453: $udom=~s/\W//g;
454: $uname=~s/\W//g;
1.16 www 455: my $subdir=$uname.'__';
1.1 albertel 456: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
457: my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
458: return $proname;
459: }
460:
461: # --------------------------------------- Is this the home server of an author?
1.11 www 462:
1.1 albertel 463: sub ishome {
464: my $author=shift;
465: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
466: my ($udom,$uname)=split(/\//,$author);
467: my $proname=propath($udom,$uname);
468: if (-e $proname) {
469: return 'owner';
470: } else {
471: return 'not_owner';
472: }
473: }
474:
475: # ======================================================= Continue main program
476: # ---------------------------------------------------- Fork once and dissociate
477:
478: $fpid=fork;
479: exit if $fpid;
1.29 harris41 480: die "Couldn't fork: $!" unless defined ($fpid);
1.1 albertel 481:
1.29 harris41 482: POSIX::setsid() or die "Can't start new session: $!";
1.1 albertel 483:
484: # ------------------------------------------------------- Write our PID on disk
485:
486: $execdir=$perlvar{'lonDaemons'};
487: open (PIDSAVE,">$execdir/logs/lond.pid");
488: print PIDSAVE "$$\n";
489: close(PIDSAVE);
1.9 www 490: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
1.57 www 491: &status('Starting');
1.1 albertel 492:
493: # ------------------------------------------------------- Now we are on our own
494:
495: # Fork off our children.
496: for (1 .. $PREFORK) {
497: make_new_child();
498: }
499:
500: # ----------------------------------------------------- Install signal handlers
501:
1.57 www 502: &status('Forked children');
503:
1.1 albertel 504: $SIG{CHLD} = \&REAPER;
505: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
506: $SIG{HUP} = \&HUPSMAN;
1.57 www 507: $SIG{USR1} = \&checkchildren;
1.1 albertel 508:
509: # And maintain the population.
510: while (1) {
1.57 www 511: &status('Sleeping');
1.1 albertel 512: sleep; # wait for a signal (i.e., child's death)
1.57 www 513: &logthis('Woke up');
514: &status('Woke up');
1.1 albertel 515: for ($i = $children; $i < $PREFORK; $i++) {
516: make_new_child(); # top up the child pool
517: }
518: }
519:
520: sub make_new_child {
521: my $pid;
522: my $cipher;
523: my $sigset;
524: &logthis("Attempting to start child");
525: # block signal for fork
526: $sigset = POSIX::SigSet->new(SIGINT);
527: sigprocmask(SIG_BLOCK, $sigset)
1.29 harris41 528: or die "Can't block SIGINT for fork: $!\n";
1.1 albertel 529:
1.29 harris41 530: die "fork: $!" unless defined ($pid = fork);
1.1 albertel 531:
532: if ($pid) {
533: # Parent records the child's birth and returns.
534: sigprocmask(SIG_UNBLOCK, $sigset)
1.29 harris41 535: or die "Can't unblock SIGINT for fork: $!\n";
1.1 albertel 536: $children{$pid} = 1;
537: $children++;
1.57 www 538: &status('Started child '.$pid);
1.1 albertel 539: return;
540: } else {
541: # Child can *not* return from this subroutine.
542: $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before
1.57 www 543: $SIG{USR1}= \&logstatus;
1.63 www 544: $SIG{ALRM}= \&timeout;
1.57 www 545: $lastlog='Forked ';
546: $status='Forked';
547:
1.1 albertel 548: # unblock signals
549: sigprocmask(SIG_UNBLOCK, $sigset)
1.29 harris41 550: or die "Can't unblock SIGINT for fork: $!\n";
1.13 www 551:
552: $tmpsnum=0;
1.91 albertel 553: #---------------------------------------------------- kerberos 5 initialization
554: &Authen::Krb5::init_context();
555: &Authen::Krb5::init_ets();
556:
1.1 albertel 557: # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
558: for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
1.57 www 559: &status('Idle, waiting for connection');
1.1 albertel 560: $client = $server->accept() or last;
1.57 www 561: &status('Accepted connection');
1.1 albertel 562: # =============================================================================
563: # do something with the connection
564: # -----------------------------------------------------------------------------
1.94 albertel 565: $client->sockopt(SO_KEEPALIVE, 1);# Enable monitoring of
566: # connection liveness.
1.2 www 567: # see if we know client and check for spoof IP by challenge
1.1 albertel 568: my $caller=getpeername($client);
569: my ($port,$iaddr)=unpack_sockaddr_in($caller);
570: my $clientip=inet_ntoa($iaddr);
571: my $clientrec=($hostid{$clientip} ne undef);
1.9 www 572: &logthis(
1.51 www 573: "<font color=yellow>INFO: Connection $i, $clientip ($hostid{$clientip})</font>"
574: );
1.57 www 575: &status("Connecting $clientip ($hostid{$clientip})");
1.2 www 576: my $clientok;
1.1 albertel 577: if ($clientrec) {
1.57 www 578: &status("Waiting for init from $clientip ($hostid{$clientip})");
1.2 www 579: my $remotereq=<$client>;
580: $remotereq=~s/\W//g;
581: if ($remotereq eq 'init') {
582: my $challenge="$$".time;
583: print $client "$challenge\n";
1.57 www 584: &status(
585: "Waiting for challenge reply from $clientip ($hostid{$clientip})");
1.2 www 586: $remotereq=<$client>;
587: $remotereq=~s/\W//g;
588: if ($challenge eq $remotereq) {
589: $clientok=1;
590: print $client "ok\n";
591: } else {
1.9 www 592: &logthis(
593: "<font color=blue>WARNING: $clientip did not reply challenge</font>");
1.57 www 594: &status('No challenge reply '.$clientip);
1.2 www 595: }
596: } else {
1.9 www 597: &logthis(
598: "<font color=blue>WARNING: "
599: ."$clientip failed to initialize: >$remotereq< </font>");
1.57 www 600: &status('No init '.$clientip);
1.2 www 601: }
602: } else {
1.9 www 603: &logthis(
604: "<font color=blue>WARNING: Unknown client $clientip</font>");
1.57 www 605: &status('Hung up on '.$clientip);
1.2 www 606: }
607: if ($clientok) {
1.1 albertel 608: # ---------------- New known client connecting, could mean machine online again
1.75 foxr 609:
1.76 foxr 610: &reconlonc("$perlvar{'lonSockDir'}/$hostid{$clientip}");
1.9 www 611: &logthis(
612: "<font color=green>Established connection: $hostid{$clientip}</font>");
1.58 www 613: &status('Will listen to '.$hostid{$clientip});
1.1 albertel 614: # ------------------------------------------------------------ Process requests
615: while (my $userinput=<$client>) {
616: chomp($userinput);
1.79 foxr 617: Debug("Request = $userinput\n");
1.57 www 618: &status('Processing '.$hostid{$clientip}.': '.$userinput);
1.1 albertel 619: my $wasenc=0;
1.63 www 620: alarm(120);
1.1 albertel 621: # ------------------------------------------------------------ See if encrypted
622: if ($userinput =~ /^enc/) {
623: if ($cipher) {
624: my ($cmd,$cmdlength,$encinput)=split(/:/,$userinput);
625: $userinput='';
626: for (my $encidx=0;$encidx<length($encinput);$encidx+=16) {
627: $userinput.=
628: $cipher->decrypt(
629: pack("H16",substr($encinput,$encidx,16))
630: );
631: }
632: $userinput=substr($userinput,0,$cmdlength);
633: $wasenc=1;
634: }
1.75 foxr 635: }
636:
1.1 albertel 637: # ------------------------------------------------------------- Normal commands
638: # ------------------------------------------------------------------------ ping
639: if ($userinput =~ /^ping/) {
640: print $client "$perlvar{'lonHostID'}\n";
641: # ------------------------------------------------------------------------ pong
642: } elsif ($userinput =~ /^pong/) {
643: $reply=reply("ping",$hostid{$clientip});
644: print $client "$perlvar{'lonHostID'}:$reply\n";
645: # ------------------------------------------------------------------------ ekey
646: } elsif ($userinput =~ /^ekey/) {
647: my $buildkey=time.$$.int(rand 100000);
648: $buildkey=~tr/1-6/A-F/;
649: $buildkey=int(rand 100000).$buildkey.int(rand 100000);
650: my $key=$perlvar{'lonHostID'}.$hostid{$clientip};
651: $key=~tr/a-z/A-Z/;
652: $key=~tr/G-P/0-9/;
653: $key=~tr/Q-Z/0-9/;
654: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
655: $key=substr($key,0,32);
656: my $cipherkey=pack("H32",$key);
657: $cipher=new IDEA $cipherkey;
658: print $client "$buildkey\n";
659: # ------------------------------------------------------------------------ load
660: } elsif ($userinput =~ /^load/) {
661: my $loadavg;
662: {
663: my $loadfile=IO::File->new('/proc/loadavg');
664: $loadavg=<$loadfile>;
665: }
666: $loadavg =~ s/\s.*//g;
667: my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
668: print $client "$loadpercent\n";
1.54 harris41 669: # ----------------------------------------------------------------- currentauth
670: } elsif ($userinput =~ /^currentauth/) {
671: if ($wasenc==1) {
672: my ($cmd,$udom,$uname)=split(/:/,$userinput);
1.79 foxr 673: my $result = GetAuthType($udom, $uname);
1.78 foxr 674: if($result eq "nouser") {
675: print $client "unknown_user\n";
676: }
677: else {
678: print $client "$result\n"
679: }
1.54 harris41 680: } else {
681: print $client "refused\n";
682: }
1.1 albertel 683: # ------------------------------------------------------------------------ auth
684: } elsif ($userinput =~ /^auth/) {
685: if ($wasenc==1) {
686: my ($cmd,$udom,$uname,$upass)=split(/:/,$userinput);
687: chomp($upass);
1.11 www 688: $upass=unescape($upass);
1.1 albertel 689: my $proname=propath($udom,$uname);
690: my $passfilename="$proname/passwd";
691: if (-e $passfilename) {
692: my $pf = IO::File->new($passfilename);
693: my $realpasswd=<$pf>;
694: chomp($realpasswd);
1.2 www 695: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
696: my $pwdcorrect=0;
697: if ($howpwd eq 'internal') {
1.99 foxr 698: &Debug("Internal auth");
1.2 www 699: $pwdcorrect=
700: (crypt($upass,$contentpwd) eq $contentpwd);
701: } elsif ($howpwd eq 'unix') {
1.99 foxr 702: &Debug("Unix auth");
703: if((getpwnam($uname))[1] eq "") { #no such user!
704: $pwdcorrect = 0;
705: } else {
706: $contentpwd=(getpwnam($uname))[1];
707: my $pwauth_path="/usr/local/sbin/pwauth";
708: unless ($contentpwd eq 'x') {
709: $pwdcorrect=
710: (crypt($upass,$contentpwd) eq
711: $contentpwd);
712: }
713:
1.52 harris41 714: elsif (-e $pwauth_path) {
715: open PWAUTH, "|$pwauth_path" or
716: die "Cannot invoke authentication";
717: print PWAUTH "$uname\n$upass\n";
718: close PWAUTH;
719: $pwdcorrect=!$?;
1.99 foxr 720: }
1.52 harris41 721: }
1.3 www 722: } elsif ($howpwd eq 'krb4') {
1.71 www 723: $null=pack("C",0);
724: unless ($upass=~/$null/) {
1.3 www 725: $pwdcorrect=(
726: Authen::Krb4::get_pw_in_tkt($uname,"",
727: $contentpwd,'krbtgt',$contentpwd,1,
728: $upass) == 0);
1.71 www 729: } else { $pwdcorrect=0; }
1.91 albertel 730: } elsif ($howpwd eq 'krb5') {
731: $null=pack("C",0);
732: unless ($upass=~/$null/) {
733: my $krbclient=&Authen::Krb5::parse_name($uname.'@'.$contentpwd);
734: my $krbservice="krbtgt/".$contentpwd."\@".$contentpwd;
735: my $krbserver=&Authen::Krb5::parse_name($krbservice);
736: my $credentials=&Authen::Krb5::cc_default();
737: $credentials->initialize($krbclient);
738: my $krbreturn =
739: &Authen::Krb5::get_in_tkt_with_password(
740: $krbclient,$krbserver,$upass,$credentials);
1.92 albertel 741: # unless ($krbreturn) {
742: # &logthis("Krb5 Error: ".
743: # &Authen::Krb5::error());
744: # }
1.91 albertel 745: $pwdcorrect = ($krbreturn == 1);
746: } else { $pwdcorrect=0; }
1.50 albertel 747: } elsif ($howpwd eq 'localauth') {
1.49 albertel 748: $pwdcorrect=&localauth::localauth($uname,$upass,
749: $contentpwd);
750: }
1.2 www 751: if ($pwdcorrect) {
1.1 albertel 752: print $client "authorized\n";
753: } else {
754: print $client "non_authorized\n";
755: }
756: } else {
757: print $client "unknown_user\n";
758: }
759: } else {
760: print $client "refused\n";
761: }
762: # ---------------------------------------------------------------------- passwd
763: } elsif ($userinput =~ /^passwd/) {
764: if ($wasenc==1) {
765: my
766: ($cmd,$udom,$uname,$upass,$npass)=split(/:/,$userinput);
767: chomp($npass);
1.32 www 768: $upass=&unescape($upass);
769: $npass=&unescape($npass);
1.98 foxr 770: &Debug("Trying to change password for $uname");
1.72 matthew 771: my $proname=propath($udom,$uname);
1.1 albertel 772: my $passfilename="$proname/passwd";
773: if (-e $passfilename) {
774: my $realpasswd;
775: { my $pf = IO::File->new($passfilename);
776: $realpasswd=<$pf>; }
777: chomp($realpasswd);
1.2 www 778: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
779: if ($howpwd eq 'internal') {
1.98 foxr 780: &Debug("internal auth");
1.2 www 781: if (crypt($upass,$contentpwd) eq $contentpwd) {
782: my $salt=time;
783: $salt=substr($salt,6,2);
784: my $ncpass=crypt($npass,$salt);
1.1 albertel 785: { my $pf = IO::File->new(">$passfilename");
1.31 www 786: print $pf "internal:$ncpass\n"; }
1.72 matthew 787: &logthis("Result of password change for $uname: pwchange_success");
1.1 albertel 788: print $client "ok\n";
1.2 www 789: } else {
790: print $client "non_authorized\n";
791: }
1.72 matthew 792: } elsif ($howpwd eq 'unix') {
793: # Unix means we have to access /etc/password
794: # one way or another.
795: # First: Make sure the current password is
796: # correct
1.98 foxr 797: &Debug("auth is unix");
1.72 matthew 798: $contentpwd=(getpwnam($uname))[1];
799: my $pwdcorrect = "0";
800: my $pwauth_path="/usr/local/sbin/pwauth";
801: unless ($contentpwd eq 'x') {
802: $pwdcorrect=
803: (crypt($upass,$contentpwd) eq $contentpwd);
804: } elsif (-e $pwauth_path) {
805: open PWAUTH, "|$pwauth_path" or
806: die "Cannot invoke authentication";
807: print PWAUTH "$uname\n$upass\n";
808: close PWAUTH;
1.98 foxr 809: &Debug("exited pwauth with $? ($uname,$upass) ");
810: $pwdcorrect=($? == 0);
1.72 matthew 811: }
812: if ($pwdcorrect) {
813: my $execdir=$perlvar{'lonDaemons'};
1.98 foxr 814: &Debug("Opening lcpasswd pipeline");
815: my $pf = IO::File->new("|$execdir/lcpasswd > /home/www/lcpasswd.log");
1.72 matthew 816: print $pf "$uname\n$npass\n$npass\n";
817: close $pf;
1.97 foxr 818: my $err = $?;
819: my $result = ($err>0 ? 'pwchange_failure'
1.72 matthew 820: : 'ok');
1.96 foxr 821: &logthis("Result of password change for $uname: ".
822: &lcpasswdstrerror($?));
1.72 matthew 823: print $client "$result\n";
824: } else {
825: print $client "non_authorized\n";
826: }
827: } else {
1.2 www 828: print $client "auth_mode_error\n";
1.1 albertel 829: }
830: } else {
831: print $client "unknown_user\n";
1.31 www 832: }
833: } else {
834: print $client "refused\n";
835: }
836: # -------------------------------------------------------------------- makeuser
837: } elsif ($userinput =~ /^makeuser/) {
1.91 albertel 838: &Debug("Make user received");
1.56 harris41 839: my $oldumask=umask(0077);
1.31 www 840: if ($wasenc==1) {
841: my
842: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
1.77 foxr 843: &Debug("cmd =".$cmd." $udom =".$udom.
844: " uname=".$uname);
1.31 www 845: chomp($npass);
1.32 www 846: $npass=&unescape($npass);
1.31 www 847: my $proname=propath($udom,$uname);
848: my $passfilename="$proname/passwd";
1.77 foxr 849: &Debug("Password file created will be:".
850: $passfilename);
1.31 www 851: if (-e $passfilename) {
852: print $client "already_exists\n";
853: } elsif ($udom ne $perlvar{'lonDefDomain'}) {
854: print $client "not_right_domain\n";
855: } else {
856: @fpparts=split(/\//,$proname);
857: $fpnow=$fpparts[0].'/'.$fpparts[1].'/'.$fpparts[2];
858: $fperror='';
859: for ($i=3;$i<=$#fpparts;$i++) {
860: $fpnow.='/'.$fpparts[$i];
861: unless (-e $fpnow) {
862: unless (mkdir($fpnow,0777)) {
1.65 www 863: $fperror="error:$!";
1.31 www 864: }
865: }
866: }
867: unless ($fperror) {
1.98 foxr 868: my $result=&make_passwd_file($uname, $umode,$npass,
1.91 albertel 869: $passfilename);
870: print $client $result;
1.31 www 871: } else {
872: print $client "$fperror\n";
873: }
1.55 harris41 874: }
875: } else {
876: print $client "refused\n";
877: }
1.56 harris41 878: umask($oldumask);
1.55 harris41 879: # -------------------------------------------------------------- changeuserauth
880: } elsif ($userinput =~ /^changeuserauth/) {
1.77 foxr 881: &Debug("Changing authorization");
882: if ($wasenc==1) {
1.55 harris41 883: my
1.91 albertel 884: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
1.55 harris41 885: chomp($npass);
1.77 foxr 886: &Debug("cmd = ".$cmd." domain= ".$udom.
887: "uname =".$uname." umode= ".$umode);
1.55 harris41 888: $npass=&unescape($npass);
1.91 albertel 889: my $proname=&propath($udom,$uname);
1.55 harris41 890: my $passfilename="$proname/passwd";
891: if ($udom ne $perlvar{'lonDefDomain'}) {
892: print $client "not_right_domain\n";
893: } else {
1.98 foxr 894: my $result=&make_passwd_file($uname, $umode,$npass,
1.93 albertel 895: $passfilename);
1.91 albertel 896: print $client $result;
1.1 albertel 897: }
898: } else {
899: print $client "refused\n";
900: }
901: # ------------------------------------------------------------------------ home
902: } elsif ($userinput =~ /^home/) {
903: my ($cmd,$udom,$uname)=split(/:/,$userinput);
904: chomp($uname);
905: my $proname=propath($udom,$uname);
906: if (-e $proname) {
907: print $client "found\n";
908: } else {
909: print $client "not_found\n";
910: }
911: # ---------------------------------------------------------------------- update
912: } elsif ($userinput =~ /^update/) {
913: my ($cmd,$fname)=split(/:/,$userinput);
914: my $ownership=ishome($fname);
915: if ($ownership eq 'not_owner') {
916: if (-e $fname) {
917: my ($dev,$ino,$mode,$nlink,
918: $uid,$gid,$rdev,$size,
919: $atime,$mtime,$ctime,
920: $blksize,$blocks)=stat($fname);
921: $now=time;
922: $since=$now-$atime;
923: if ($since>$perlvar{'lonExpire'}) {
924: $reply=
925: reply("unsub:$fname","$hostid{$clientip}");
926: unlink("$fname");
927: } else {
928: my $transname="$fname.in.transfer";
929: my $remoteurl=
930: reply("sub:$fname","$hostid{$clientip}");
931: my $response;
932: {
933: my $ua=new LWP::UserAgent;
934: my $request=new HTTP::Request('GET',"$remoteurl");
935: $response=$ua->request($request,$transname);
936: }
937: if ($response->is_error()) {
1.24 albertel 938: unlink($transname);
1.1 albertel 939: my $message=$response->status_line;
940: &logthis(
941: "LWP GET: $message for $fname ($remoteurl)");
942: } else {
1.14 www 943: if ($remoteurl!~/\.meta$/) {
1.28 www 944: my $ua=new LWP::UserAgent;
1.14 www 945: my $mrequest=
946: new HTTP::Request('GET',$remoteurl.'.meta');
947: my $mresponse=
948: $ua->request($mrequest,$fname.'.meta');
949: if ($mresponse->is_error()) {
950: unlink($fname.'.meta');
951: }
952: }
1.1 albertel 953: rename($transname,$fname);
954: }
955: }
956: print $client "ok\n";
957: } else {
958: print $client "not_found\n";
959: }
960: } else {
961: print $client "rejected\n";
962: }
1.85 www 963: # -------------------------------------- fetch a user file from a remote server
964: } elsif ($userinput =~ /^fetchuserfile/) {
1.86 www 965: my ($cmd,$fname)=split(/:/,$userinput);
966: my ($udom,$uname,$ufile)=split(/\//,$fname);
967: my $udir=propath($udom,$uname).'/userfiles';
1.88 albertel 968: unless (-e $udir) { mkdir($udir,0770); }
1.86 www 969: if (-e $udir) {
970: $ufile=~s/^[\.\~]+//;
971: $ufile=~s/\///g;
972: my $transname=$udir.'/'.$ufile;
973: my $remoteurl='http://'.$clientip.'/userfiles/'.$fname;
974: my $response;
975: {
976: my $ua=new LWP::UserAgent;
977: my $request=new HTTP::Request('GET',"$remoteurl");
978: $response=$ua->request($request,$transname);
979: }
980: if ($response->is_error()) {
981: unlink($transname);
982: my $message=$response->status_line;
983: &logthis(
984: "LWP GET: $message for $fname ($remoteurl)");
985: print $client "failed\n";
986: } else {
987: print $client "ok\n";
988: }
989: } else {
990: print $client "not_home\n";
991: }
1.85 www 992: # ------------------------------------------ authenticate access to a user file
1.86 www 993: } elsif ($userinput =~ /^tokenauthuserfile/) {
1.85 www 994: my ($cmd,$fname,$session)=split(/:/,$userinput);
1.86 www 995: chomp($session);
996: $reply='non_auth';
997: if (open(ENVIN,$perlvar{'lonIDsDir'}.'/'.
998: $session.'.id')) {
999: while ($line=<ENVIN>) {
1000: if ($line=~/userfile\.$fname\=/) { $reply='ok'; }
1001: }
1002: close(ENVIN);
1003: print $client $reply."\n";
1004: } else {
1005: print $client "invalid_token\n";
1006: }
1.1 albertel 1007: # ----------------------------------------------------------------- unsubscribe
1008: } elsif ($userinput =~ /^unsub/) {
1009: my ($cmd,$fname)=split(/:/,$userinput);
1010: if (-e $fname) {
1.84 albertel 1011: print $client &unsub($client,$fname,$clientip);
1.1 albertel 1012: } else {
1013: print $client "not_found\n";
1014: }
1015: # ------------------------------------------------------------------- subscribe
1016: } elsif ($userinput =~ /^sub/) {
1.84 albertel 1017: print $client &subscribe($userinput,$clientip);
1.12 harris41 1018: # ------------------------------------------------------------------------- log
1019: } elsif ($userinput =~ /^log/) {
1020: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
1021: chomp($what);
1022: my $proname=propath($udom,$uname);
1023: my $now=time;
1024: {
1025: my $hfh;
1026: if ($hfh=IO::File->new(">>$proname/activity.log")) {
1027: print $hfh "$now:$hostid{$clientip}:$what\n";
1028: print $client "ok\n";
1029: } else {
1030: print $client "error:$!\n";
1031: }
1032: }
1.1 albertel 1033: # ------------------------------------------------------------------------- put
1034: } elsif ($userinput =~ /^put/) {
1.6 www 1035: my ($cmd,$udom,$uname,$namespace,$what)
1.1 albertel 1036: =split(/:/,$userinput);
1.8 www 1037: $namespace=~s/\//\_/g;
1.6 www 1038: $namespace=~s/\W//g;
1039: if ($namespace ne 'roles') {
1.1 albertel 1040: chomp($what);
1041: my $proname=propath($udom,$uname);
1042: my $now=time;
1.48 www 1043: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1044: my $hfh;
1045: if (
1046: $hfh=IO::File->new(">>$proname/$namespace.hist")
1047: ) { print $hfh "P:$now:$what\n"; }
1048: }
1049: my @pairs=split(/\&/,$what);
1.4 www 1050: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1051: foreach $pair (@pairs) {
1052: ($key,$value)=split(/=/,$pair);
1053: $hash{$key}=$value;
1054: }
1.4 www 1055: if (untie(%hash)) {
1.1 albertel 1056: print $client "ok\n";
1057: } else {
1058: print $client "error:$!\n";
1059: }
1060: } else {
1061: print $client "error:$!\n";
1062: }
1.6 www 1063: } else {
1064: print $client "refused\n";
1065: }
1066: # -------------------------------------------------------------------- rolesput
1067: } elsif ($userinput =~ /^rolesput/) {
1.77 foxr 1068: &Debug("rolesput");
1.6 www 1069: if ($wasenc==1) {
1070: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
1071: =split(/:/,$userinput);
1.77 foxr 1072: &Debug("cmd = ".$cmd." exedom= ".$exedom.
1073: "user = ".$exeuser." udom=".$udom.
1074: "what = ".$what);
1.6 www 1075: my $namespace='roles';
1076: chomp($what);
1077: my $proname=propath($udom,$uname);
1078: my $now=time;
1079: {
1080: my $hfh;
1081: if (
1082: $hfh=IO::File->new(">>$proname/$namespace.hist")
1083: ) {
1084: print $hfh "P:$now:$exedom:$exeuser:$what\n";
1085: }
1086: }
1087: my @pairs=split(/\&/,$what);
1088: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1089: foreach $pair (@pairs) {
1090: ($key,$value)=split(/=/,$pair);
1.78 foxr 1091: &ManagePermissions($key, $udom, $uname,
1092: &GetAuthType( $udom,
1093: $uname));
1.6 www 1094: $hash{$key}=$value;
1.78 foxr 1095:
1.6 www 1096: }
1097: if (untie(%hash)) {
1098: print $client "ok\n";
1099: } else {
1100: print $client "error:$!\n";
1101: }
1102: } else {
1103: print $client "error:$!\n";
1104: }
1105: } else {
1106: print $client "refused\n";
1107: }
1.1 albertel 1108: # ------------------------------------------------------------------------- get
1109: } elsif ($userinput =~ /^get/) {
1110: my ($cmd,$udom,$uname,$namespace,$what)
1111: =split(/:/,$userinput);
1.8 www 1112: $namespace=~s/\//\_/g;
1.1 albertel 1113: $namespace=~s/\W//g;
1114: chomp($what);
1115: my @queries=split(/\&/,$what);
1116: my $proname=propath($udom,$uname);
1117: my $qresult='';
1.20 www 1118: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1119: for ($i=0;$i<=$#queries;$i++) {
1120: $qresult.="$hash{$queries[$i]}&";
1121: }
1.4 www 1122: if (untie(%hash)) {
1.1 albertel 1123: $qresult=~s/\&$//;
1124: print $client "$qresult\n";
1125: } else {
1126: print $client "error:$!\n";
1127: }
1128: } else {
1129: print $client "error:$!\n";
1130: }
1131: # ------------------------------------------------------------------------ eget
1132: } elsif ($userinput =~ /^eget/) {
1133: my ($cmd,$udom,$uname,$namespace,$what)
1134: =split(/:/,$userinput);
1.8 www 1135: $namespace=~s/\//\_/g;
1.1 albertel 1136: $namespace=~s/\W//g;
1137: chomp($what);
1138: my @queries=split(/\&/,$what);
1139: my $proname=propath($udom,$uname);
1140: my $qresult='';
1.20 www 1141: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1142: for ($i=0;$i<=$#queries;$i++) {
1143: $qresult.="$hash{$queries[$i]}&";
1144: }
1.4 www 1145: if (untie(%hash)) {
1.1 albertel 1146: $qresult=~s/\&$//;
1147: if ($cipher) {
1148: my $cmdlength=length($qresult);
1149: $qresult.=" ";
1150: my $encqresult='';
1151: for
1152: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
1153: $encqresult.=
1154: unpack("H16",
1155: $cipher->encrypt(substr($qresult,$encidx,8)));
1156: }
1157: print $client "enc:$cmdlength:$encqresult\n";
1158: } else {
1159: print $client "error:no_key\n";
1160: }
1161: } else {
1162: print $client "error:$!\n";
1163: }
1164: } else {
1165: print $client "error:$!\n";
1166: }
1167: # ------------------------------------------------------------------------- del
1168: } elsif ($userinput =~ /^del/) {
1169: my ($cmd,$udom,$uname,$namespace,$what)
1170: =split(/:/,$userinput);
1.8 www 1171: $namespace=~s/\//\_/g;
1.1 albertel 1172: $namespace=~s/\W//g;
1173: chomp($what);
1174: my $proname=propath($udom,$uname);
1175: my $now=time;
1.48 www 1176: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1177: my $hfh;
1178: if (
1179: $hfh=IO::File->new(">>$proname/$namespace.hist")
1180: ) { print $hfh "D:$now:$what\n"; }
1181: }
1182: my @keys=split(/\&/,$what);
1.4 www 1183: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1184: foreach $key (@keys) {
1185: delete($hash{$key});
1186: }
1.4 www 1187: if (untie(%hash)) {
1.1 albertel 1188: print $client "ok\n";
1189: } else {
1190: print $client "error:$!\n";
1191: }
1192: } else {
1193: print $client "error:$!\n";
1194: }
1195: # ------------------------------------------------------------------------ keys
1196: } elsif ($userinput =~ /^keys/) {
1197: my ($cmd,$udom,$uname,$namespace)
1198: =split(/:/,$userinput);
1.8 www 1199: $namespace=~s/\//\_/g;
1.1 albertel 1200: $namespace=~s/\W//g;
1201: my $proname=propath($udom,$uname);
1202: my $qresult='';
1.20 www 1203: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1204: foreach $key (keys %hash) {
1205: $qresult.="$key&";
1206: }
1.4 www 1207: if (untie(%hash)) {
1.1 albertel 1208: $qresult=~s/\&$//;
1209: print $client "$qresult\n";
1210: } else {
1211: print $client "error:$!\n";
1212: }
1213: } else {
1214: print $client "error:$!\n";
1215: }
1216: # ------------------------------------------------------------------------ dump
1217: } elsif ($userinput =~ /^dump/) {
1.62 www 1218: my ($cmd,$udom,$uname,$namespace,$regexp)
1.1 albertel 1219: =split(/:/,$userinput);
1.8 www 1220: $namespace=~s/\//\_/g;
1.1 albertel 1221: $namespace=~s/\W//g;
1.62 www 1222: if (defined($regexp)) {
1223: $regexp=&unescape($regexp);
1224: } else {
1225: $regexp='.';
1226: }
1.100 ! matthew 1227: my $qresult='';
1.1 albertel 1228: my $proname=propath($udom,$uname);
1.100 ! matthew 1229: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER(),0640)) {
1.90 stredwic 1230: study($regexp);
1.100 ! matthew 1231: while (($key,$value) = each(%hash)) {
! 1232: if ($regexp eq '.') {
! 1233: $qresult.=$key.'='.$value.'&';
! 1234: } else {
! 1235: my $unescapeKey = &unescape($key);
! 1236: if (eval('$unescapeKey=~/$regexp/')) {
! 1237: $qresult.="$key=$value&";
! 1238: }
! 1239: }
1.7 www 1240: }
1.100 ! matthew 1241: if (untie(%hash)) {
! 1242: chop($qresult);
! 1243: print $client "$qresult\n";
1.7 www 1244: } else {
1.100 ! matthew 1245: print $client "error:$!\n";
1.7 www 1246: }
1247: } else {
1248: print $client "error:$!\n";
1249: }
1250: # ----------------------------------------------------------------------- store
1251: } elsif ($userinput =~ /^store/) {
1252: my ($cmd,$udom,$uname,$namespace,$rid,$what)
1253: =split(/:/,$userinput);
1.8 www 1254: $namespace=~s/\//\_/g;
1.7 www 1255: $namespace=~s/\W//g;
1256: if ($namespace ne 'roles') {
1257: chomp($what);
1258: my $proname=propath($udom,$uname);
1259: my $now=time;
1.48 www 1260: unless ($namespace=~/^nohist\_/) {
1.7 www 1261: my $hfh;
1262: if (
1263: $hfh=IO::File->new(">>$proname/$namespace.hist")
1264: ) { print $hfh "P:$now:$rid:$what\n"; }
1265: }
1266: my @pairs=split(/\&/,$what);
1267:
1268: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1269: my @previouskeys=split(/&/,$hash{"keys:$rid"});
1270: my $key;
1271: $hash{"version:$rid"}++;
1272: my $version=$hash{"version:$rid"};
1273: my $allkeys='';
1274: foreach $pair (@pairs) {
1275: ($key,$value)=split(/=/,$pair);
1276: $allkeys.=$key.':';
1277: $hash{"$version:$rid:$key"}=$value;
1278: }
1.36 www 1279: $hash{"$version:$rid:timestamp"}=$now;
1280: $allkeys.='timestamp';
1.7 www 1281: $hash{"$version:keys:$rid"}=$allkeys;
1282: if (untie(%hash)) {
1283: print $client "ok\n";
1284: } else {
1285: print $client "error:$!\n";
1286: }
1287: } else {
1288: print $client "error:$!\n";
1289: }
1290: } else {
1291: print $client "refused\n";
1292: }
1293: # --------------------------------------------------------------------- restore
1294: } elsif ($userinput =~ /^restore/) {
1295: my ($cmd,$udom,$uname,$namespace,$rid)
1296: =split(/:/,$userinput);
1.8 www 1297: $namespace=~s/\//\_/g;
1.7 www 1298: $namespace=~s/\W//g;
1299: chomp($rid);
1300: my $proname=propath($udom,$uname);
1301: my $qresult='';
1.20 www 1302: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.7 www 1303: my $version=$hash{"version:$rid"};
1304: $qresult.="version=$version&";
1305: my $scope;
1306: for ($scope=1;$scope<=$version;$scope++) {
1307: my $vkeys=$hash{"$scope:keys:$rid"};
1308: my @keys=split(/:/,$vkeys);
1309: my $key;
1310: $qresult.="$scope:keys=$vkeys&";
1311: foreach $key (@keys) {
1.21 www 1312: $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
1.7 www 1313: }
1.1 albertel 1314: }
1.4 www 1315: if (untie(%hash)) {
1.1 albertel 1316: $qresult=~s/\&$//;
1317: print $client "$qresult\n";
1318: } else {
1319: print $client "error:$!\n";
1320: }
1321: } else {
1322: print $client "error:$!\n";
1323: }
1.86 www 1324: # -------------------------------------------------------------------- chatsend
1325: } elsif ($userinput =~ /^chatsend/) {
1326: my ($cmd,$cdom,$cnum,$newpost)=split(/\:/,$userinput);
1327: &chatadd($cdom,$cnum,$newpost);
1328: print $client "ok\n";
1329: # -------------------------------------------------------------------- chatretr
1330: } elsif ($userinput =~ /^chatretr/) {
1331: my ($cmd,$cdom,$cnum)=split(/\:/,$userinput);
1332: my $reply='';
1333: foreach (&getchat($cdom,$cnum)) {
1334: $reply.=&escape($_).':';
1335: }
1336: $reply=~s/\:$//;
1337: print $client $reply."\n";
1.12 harris41 1338: # ------------------------------------------------------------------- querysend
1339: } elsif ($userinput =~ /^querysend/) {
1.44 harris41 1340: my ($cmd,$query,
1.82 www 1341: $arg1,$arg2,$arg3)=split(/\:/,$userinput);
1.12 harris41 1342: $query=~s/\n*$//g;
1.82 www 1343: print $client "".
1.40 harris41 1344: sqlreply("$hostid{$clientip}\&$query".
1.82 www 1345: "\&$arg1"."\&$arg2"."\&$arg3")."\n";
1.12 harris41 1346: # ------------------------------------------------------------------ queryreply
1347: } elsif ($userinput =~ /^queryreply/) {
1348: my ($cmd,$id,$reply)=split(/:/,$userinput);
1349: my $store;
1.13 www 1350: my $execdir=$perlvar{'lonDaemons'};
1351: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.43 harris41 1352: $reply=~s/\&/\n/g;
1.12 harris41 1353: print $store $reply;
1354: close $store;
1.46 harris41 1355: my $store2=IO::File->new(">$execdir/tmp/$id.end");
1356: print $store2 "done\n";
1357: close $store2;
1.12 harris41 1358: print $client "ok\n";
1359: }
1360: else {
1361: print $client "error:$!\n";
1362: }
1.1 albertel 1363: # ----------------------------------------------------------------------- idput
1364: } elsif ($userinput =~ /^idput/) {
1365: my ($cmd,$udom,$what)=split(/:/,$userinput);
1366: chomp($what);
1367: $udom=~s/\W//g;
1368: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1369: my $now=time;
1370: {
1371: my $hfh;
1372: if (
1373: $hfh=IO::File->new(">>$proname.hist")
1374: ) { print $hfh "P:$now:$what\n"; }
1375: }
1376: my @pairs=split(/\&/,$what);
1.4 www 1377: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1378: foreach $pair (@pairs) {
1379: ($key,$value)=split(/=/,$pair);
1380: $hash{$key}=$value;
1381: }
1.4 www 1382: if (untie(%hash)) {
1.1 albertel 1383: print $client "ok\n";
1384: } else {
1385: print $client "error:$!\n";
1386: }
1387: } else {
1388: print $client "error:$!\n";
1389: }
1390: # ----------------------------------------------------------------------- idget
1391: } elsif ($userinput =~ /^idget/) {
1392: my ($cmd,$udom,$what)=split(/:/,$userinput);
1393: chomp($what);
1394: $udom=~s/\W//g;
1395: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1396: my @queries=split(/\&/,$what);
1397: my $qresult='';
1.20 www 1398: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER,0640)) {
1.1 albertel 1399: for ($i=0;$i<=$#queries;$i++) {
1400: $qresult.="$hash{$queries[$i]}&";
1401: }
1.4 www 1402: if (untie(%hash)) {
1.1 albertel 1403: $qresult=~s/\&$//;
1404: print $client "$qresult\n";
1405: } else {
1406: print $client "error:$!\n";
1407: }
1408: } else {
1409: print $client "error:$!\n";
1410: }
1.13 www 1411: # ---------------------------------------------------------------------- tmpput
1412: } elsif ($userinput =~ /^tmpput/) {
1413: my ($cmd,$what)=split(/:/,$userinput);
1414: my $store;
1415: $tmpsnum++;
1416: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
1417: $id=~s/\W/\_/g;
1418: $what=~s/\n//g;
1419: my $execdir=$perlvar{'lonDaemons'};
1420: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1421: print $store $what;
1422: close $store;
1423: print $client "$id\n";
1424: }
1425: else {
1426: print $client "error:$!\n";
1427: }
1428:
1429: # ---------------------------------------------------------------------- tmpget
1430: } elsif ($userinput =~ /^tmpget/) {
1431: my ($cmd,$id)=split(/:/,$userinput);
1432: chomp($id);
1433: $id=~s/\W/\_/g;
1434: my $store;
1435: my $execdir=$perlvar{'lonDaemons'};
1436: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
1437: my $reply=<$store>;
1438: print $client "$reply\n";
1439: close $store;
1440: }
1441: else {
1442: print $client "error:$!\n";
1443: }
1444:
1.5 www 1445: # -------------------------------------------------------------------------- ls
1446: } elsif ($userinput =~ /^ls/) {
1447: my ($cmd,$ulsdir)=split(/:/,$userinput);
1448: my $ulsout='';
1449: my $ulsfn;
1450: if (-e $ulsdir) {
1.83 stredwic 1451: if(-d $ulsdir) {
1452: if (opendir(LSDIR,$ulsdir)) {
1453: while ($ulsfn=readdir(LSDIR)) {
1454: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1455: $ulsout.=$ulsfn.'&'.
1456: join('&',@ulsstats).':';
1457: }
1458: closedir(LSDIR);
1459: }
1460: } else {
1461: my @ulsstats=stat($ulsdir);
1462: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1463: }
1464: } else {
1.5 www 1465: $ulsout='no_such_dir';
1466: }
1.17 www 1467: if ($ulsout eq '') { $ulsout='empty'; }
1.5 www 1468: print $client "$ulsout\n";
1.51 www 1469: # ------------------------------------------------------------------ Hanging up
1470: } elsif (($userinput =~ /^exit/) ||
1471: ($userinput =~ /^init/)) {
1472: &logthis(
1473: "Client $clientip ($hostid{$clientip}) hanging up: $userinput");
1474: print $client "bye\n";
1.59 www 1475: $client->close();
1.51 www 1476: last;
1.1 albertel 1477: # ------------------------------------------------------------- unknown command
1478: } else {
1479: # unknown command
1480: print $client "unknown_cmd\n";
1481: }
1.58 www 1482: # -------------------------------------------------------------------- complete
1.63 www 1483: alarm(0);
1.58 www 1484: &status('Listening to '.$hostid{$clientip});
1485: }
1.59 www 1486: # --------------------------------------------- client unknown or fishy, refuse
1.1 albertel 1487: } else {
1488: print $client "refused\n";
1.59 www 1489: $client->close();
1.9 www 1490: &logthis("<font color=blue>WARNING: "
1491: ."Rejected client $clientip, closing connection</font>");
1.75 foxr 1492: }
1493: }
1494:
1.1 albertel 1495: # =============================================================================
1.75 foxr 1496:
1497: &logthis("<font color=red>CRITICAL: "
1498: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.1 albertel 1499: # tidy up gracefully and finish
1500:
1.59 www 1501: $server->close();
1502:
1.1 albertel 1503: # this exit is VERY important, otherwise the child will become
1504: # a producer of more and more children, forking yourself into
1505: # process death.
1506: exit;
1507: }
1.78 foxr 1508: }
1509:
1510:
1511: #
1512: # Checks to see if the input roleput request was to set
1513: # an author role. If so, invokes the lchtmldir script to set
1514: # up a correct public_html
1515: # Parameters:
1516: # request - The request sent to the rolesput subchunk.
1517: # We're looking for /domain/_au
1518: # domain - The domain in which the user is having roles doctored.
1519: # user - Name of the user for which the role is being put.
1520: # authtype - The authentication type associated with the user.
1521: #
1522: sub ManagePermissions
1523: {
1524: my $request = shift;
1525: my $domain = shift;
1526: my $user = shift;
1527: my $authtype= shift;
1528:
1529: # See if the request is of the form /$domain/_au
1530:
1531: if($request =~ /^(\/$domain\/_au)$/) { # It's an author rolesput...
1532: my $execdir = $perlvar{'lonDaemons'};
1533: my $userhome= "/home/$user" ;
1534: Debug("system $execdir/lchtmldir $userhome $system $authtype");
1535: system("$execdir/lchtmldir $userhome $user $authtype");
1536: }
1537: }
1538: #
1539: # GetAuthType - Determines the authorization type of a user in a domain.
1540:
1541: # Returns the authorization type or nouser if there is no such user.
1542: #
1543: sub GetAuthType
1544: {
1545: my $domain = shift;
1546: my $user = shift;
1547:
1.79 foxr 1548: Debug("GetAuthType( $domain, $user ) \n");
1.78 foxr 1549: my $proname = &propath($domain, $user);
1550: my $passwdfile = "$proname/passwd";
1551: if( -e $passwdfile ) {
1552: my $pf = IO::File->new($passwdfile);
1553: my $realpassword = <$pf>;
1554: chomp($realpassword);
1.79 foxr 1555: Debug("Password info = $realpassword\n");
1.78 foxr 1556: my ($authtype, $contentpwd) = split(/:/, $realpassword);
1.79 foxr 1557: Debug("Authtype = $authtype, content = $contentpwd\n");
1.78 foxr 1558: my $availinfo = '';
1.91 albertel 1559: if($authtype eq 'krb4' or $authtype eq 'krb5') {
1.78 foxr 1560: $availinfo = $contentpwd;
1561: }
1.79 foxr 1562:
1.78 foxr 1563: return "$authtype:$availinfo";
1564: }
1565: else {
1.79 foxr 1566: Debug("Returning nouser");
1.78 foxr 1567: return "nouser";
1568: }
1.1 albertel 1569: }
1570:
1.84 albertel 1571: sub addline {
1572: my ($fname,$hostid,$ip,$newline)=@_;
1573: my $contents;
1574: my $found=0;
1575: my $expr='^'.$hostid.':'.$ip.':';
1576: $expr =~ s/\./\\\./g;
1577: if ($sh=IO::File->new("$fname.subscription")) {
1578: while (my $subline=<$sh>) {
1579: if ($subline !~ /$expr/) {$contents.= $subline;} else {$found=1;}
1580: }
1581: $sh->close();
1582: }
1583: $sh=IO::File->new(">$fname.subscription");
1584: if ($contents) { print $sh $contents; }
1585: if ($newline) { print $sh $newline; }
1586: $sh->close();
1587: return $found;
1.86 www 1588: }
1589:
1590: sub getchat {
1591: my ($cdom,$cname)=@_;
1.87 www 1592: my %hash;
1593: my $proname=&propath($cdom,$cname);
1594: my @entries=();
1.88 albertel 1595: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
1596: &GDBM_READER(),0640)) {
1597: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
1598: untie %hash;
1.86 www 1599: }
1.87 www 1600: return @entries;
1.86 www 1601: }
1602:
1603: sub chatadd {
1.88 albertel 1604: my ($cdom,$cname,$newchat)=@_;
1605: my %hash;
1606: my $proname=&propath($cdom,$cname);
1607: my @entries=();
1608: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
1609: &GDBM_WRCREAT(),0640)) {
1610: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
1611: my $time=time;
1612: my ($lastid)=($entries[$#entries]=~/^(\w+)\:/);
1613: my ($thentime,$idnum)=split(/\_/,$lastid);
1614: my $newid=$time.'_000000';
1615: if ($thentime==$time) {
1616: $idnum=~s/^0+//;
1617: $idnum++;
1618: $idnum=substr('000000'.$idnum,-6,6);
1619: $newid=$time.'_'.$idnum;
1620: }
1621: $hash{$newid}=$newchat;
1622: my $expired=$time-3600;
1623: foreach (keys %hash) {
1624: my ($thistime)=($_=~/(\d+)\_/);
1625: if ($thistime<$expired) {
1.89 www 1626: delete $hash{$_};
1.88 albertel 1627: }
1628: }
1629: untie %hash;
1.86 www 1630: }
1.84 albertel 1631: }
1632:
1633: sub unsub {
1634: my ($fname,$clientip)=@_;
1635: my $result;
1636: if (unlink("$fname.$hostid{$clientip}")) {
1637: $result="ok\n";
1638: } else {
1639: $result="not_subscribed\n";
1640: }
1641: if (-e "$fname.subscription") {
1642: my $found=&addline($fname,$hostid{$clientip},$clientip,'');
1643: if ($found) { $result="ok\n"; }
1644: } else {
1645: if ($result != "ok\n") { $result="not_subscribed\n"; }
1646: }
1647: return $result;
1648: }
1649:
1650: sub subscribe {
1651: my ($userinput,$clientip)=@_;
1652: my $result;
1653: my ($cmd,$fname)=split(/:/,$userinput);
1654: my $ownership=&ishome($fname);
1655: if ($ownership eq 'owner') {
1656: if (-e $fname) {
1657: if (-d $fname) {
1658: $result="directory\n";
1659: } else {
1660: if (-e "$fname.$hostid{$clientip}") {&unsub($fname,$clientip);}
1661: $now=time;
1662: my $found=&addline($fname,$hostid{$clientip},$clientip,
1663: "$hostid{$clientip}:$clientip:$now\n");
1664: if ($found) { $result="$fname\n"; }
1665: # if they were subscribed to only meta data, delete that
1666: # subscription, when you subscribe to a file you also get
1667: # the metadata
1668: unless ($fname=~/\.meta$/) { &unsub("$fname.meta",$clientip); }
1669: $fname=~s/\/home\/httpd\/html\/res/raw/;
1670: $fname="http://$thisserver/".$fname;
1671: $result="$fname\n";
1672: }
1673: } else {
1674: $result="not_found\n";
1675: }
1676: } else {
1677: $result="rejected\n";
1678: }
1679: return $result;
1680: }
1.91 albertel 1681:
1682: sub make_passwd_file {
1.98 foxr 1683: my ($uname, $umode,$npass,$passfilename)=@_;
1.91 albertel 1684: my $result="ok\n";
1685: if ($umode eq 'krb4' or $umode eq 'krb5') {
1686: {
1687: my $pf = IO::File->new(">$passfilename");
1688: print $pf "$umode:$npass\n";
1689: }
1690: } elsif ($umode eq 'internal') {
1691: my $salt=time;
1692: $salt=substr($salt,6,2);
1693: my $ncpass=crypt($npass,$salt);
1694: {
1695: &Debug("Creating internal auth");
1696: my $pf = IO::File->new(">$passfilename");
1697: print $pf "internal:$ncpass\n";
1698: }
1699: } elsif ($umode eq 'localauth') {
1700: {
1701: my $pf = IO::File->new(">$passfilename");
1702: print $pf "localauth:$npass\n";
1703: }
1704: } elsif ($umode eq 'unix') {
1705: {
1706: my $execpath="$perlvar{'lonDaemons'}/"."lcuseradd";
1707: {
1708: &Debug("Executing external: ".$execpath);
1.98 foxr 1709: &Debug("user = ".$uname.", Password =". $npass);
1710: my $se = IO::File->new("|$execpath > /home/www/lcuseradd.log");
1.91 albertel 1711: print $se "$uname\n";
1712: print $se "$npass\n";
1713: print $se "$npass\n";
1.97 foxr 1714: }
1715: my $useraddok = $?;
1716: if($useraddok > 0) {
1717: &logthis("Failed lcuseradd: ".&lcuseraddstrerror($useraddok));
1.91 albertel 1718: }
1719: my $pf = IO::File->new(">$passfilename");
1720: print $pf "unix:\n";
1721: }
1722: } elsif ($umode eq 'none') {
1723: {
1724: my $pf = IO::File->new(">$passfilename");
1725: print $pf "none:\n";
1726: }
1727: } else {
1728: $result="auth_mode_error\n";
1729: }
1730: return $result;
1731: }
1732:
1.61 harris41 1733: # ----------------------------------- POD (plain old documentation, CPAN style)
1734:
1735: =head1 NAME
1736:
1737: lond - "LON Daemon" Server (port "LOND" 5663)
1738:
1739: =head1 SYNOPSIS
1740:
1.74 harris41 1741: Usage: B<lond>
1742:
1743: Should only be run as user=www. This is a command-line script which
1744: is invoked by B<loncron>. There is no expectation that a typical user
1745: will manually start B<lond> from the command-line. (In other words,
1746: DO NOT START B<lond> YOURSELF.)
1.61 harris41 1747:
1748: =head1 DESCRIPTION
1749:
1.74 harris41 1750: There are two characteristics associated with the running of B<lond>,
1751: PROCESS MANAGEMENT (starting, stopping, handling child processes)
1752: and SERVER-SIDE ACTIVITIES (password authentication, user creation,
1753: subscriptions, etc). These are described in two large
1754: sections below.
1755:
1756: B<PROCESS MANAGEMENT>
1757:
1.61 harris41 1758: Preforker - server who forks first. Runs as a daemon. HUPs.
1759: Uses IDEA encryption
1760:
1.74 harris41 1761: B<lond> forks off children processes that correspond to the other servers
1762: in the network. Management of these processes can be done at the
1763: parent process level or the child process level.
1764:
1765: B<logs/lond.log> is the location of log messages.
1766:
1767: The process management is now explained in terms of linux shell commands,
1768: subroutines internal to this code, and signal assignments:
1769:
1770: =over 4
1771:
1772: =item *
1773:
1774: PID is stored in B<logs/lond.pid>
1775:
1776: This is the process id number of the parent B<lond> process.
1777:
1778: =item *
1779:
1780: SIGTERM and SIGINT
1781:
1782: Parent signal assignment:
1783: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
1784:
1785: Child signal assignment:
1786: $SIG{INT} = 'DEFAULT'; (and SIGTERM is DEFAULT also)
1787: (The child dies and a SIGALRM is sent to parent, awaking parent from slumber
1788: to restart a new child.)
1789:
1790: Command-line invocations:
1791: B<kill> B<-s> SIGTERM I<PID>
1792: B<kill> B<-s> SIGINT I<PID>
1793:
1794: Subroutine B<HUNTSMAN>:
1795: This is only invoked for the B<lond> parent I<PID>.
1796: This kills all the children, and then the parent.
1797: The B<lonc.pid> file is cleared.
1798:
1799: =item *
1800:
1801: SIGHUP
1802:
1803: Current bug:
1804: This signal can only be processed the first time
1805: on the parent process. Subsequent SIGHUP signals
1806: have no effect.
1807:
1808: Parent signal assignment:
1809: $SIG{HUP} = \&HUPSMAN;
1810:
1811: Child signal assignment:
1812: none (nothing happens)
1813:
1814: Command-line invocations:
1815: B<kill> B<-s> SIGHUP I<PID>
1816:
1817: Subroutine B<HUPSMAN>:
1818: This is only invoked for the B<lond> parent I<PID>,
1819: This kills all the children, and then the parent.
1820: The B<lond.pid> file is cleared.
1821:
1822: =item *
1823:
1824: SIGUSR1
1825:
1826: Parent signal assignment:
1827: $SIG{USR1} = \&USRMAN;
1828:
1829: Child signal assignment:
1830: $SIG{USR1}= \&logstatus;
1831:
1832: Command-line invocations:
1833: B<kill> B<-s> SIGUSR1 I<PID>
1834:
1835: Subroutine B<USRMAN>:
1836: When invoked for the B<lond> parent I<PID>,
1837: SIGUSR1 is sent to all the children, and the status of
1838: each connection is logged.
1839:
1840: =item *
1841:
1842: SIGCHLD
1843:
1844: Parent signal assignment:
1845: $SIG{CHLD} = \&REAPER;
1846:
1847: Child signal assignment:
1848: none
1849:
1850: Command-line invocations:
1851: B<kill> B<-s> SIGCHLD I<PID>
1852:
1853: Subroutine B<REAPER>:
1854: This is only invoked for the B<lond> parent I<PID>.
1855: Information pertaining to the child is removed.
1856: The socket port is cleaned up.
1857:
1858: =back
1859:
1860: B<SERVER-SIDE ACTIVITIES>
1861:
1862: Server-side information can be accepted in an encrypted or non-encrypted
1863: method.
1864:
1865: =over 4
1866:
1867: =item ping
1868:
1869: Query a client in the hosts.tab table; "Are you there?"
1870:
1871: =item pong
1872:
1873: Respond to a ping query.
1874:
1875: =item ekey
1876:
1877: Read in encrypted key, make cipher. Respond with a buildkey.
1878:
1879: =item load
1880:
1881: Respond with CPU load based on a computation upon /proc/loadavg.
1882:
1883: =item currentauth
1884:
1885: Reply with current authentication information (only over an
1886: encrypted channel).
1887:
1888: =item auth
1889:
1890: Only over an encrypted channel, reply as to whether a user's
1891: authentication information can be validated.
1892:
1893: =item passwd
1894:
1895: Allow for a password to be set.
1896:
1897: =item makeuser
1898:
1899: Make a user.
1900:
1901: =item passwd
1902:
1903: Allow for authentication mechanism and password to be changed.
1904:
1905: =item home
1.61 harris41 1906:
1.74 harris41 1907: Respond to a question "are you the home for a given user?"
1908:
1909: =item update
1910:
1911: Update contents of a subscribed resource.
1912:
1913: =item unsubscribe
1914:
1915: The server is unsubscribing from a resource.
1916:
1917: =item subscribe
1918:
1919: The server is subscribing to a resource.
1920:
1921: =item log
1922:
1923: Place in B<logs/lond.log>
1924:
1925: =item put
1926:
1927: stores hash in namespace
1928:
1929: =item rolesput
1930:
1931: put a role into a user's environment
1932:
1933: =item get
1934:
1935: returns hash with keys from array
1936: reference filled in from namespace
1937:
1938: =item eget
1939:
1940: returns hash with keys from array
1941: reference filled in from namesp (encrypts the return communication)
1942:
1943: =item rolesget
1944:
1945: get a role from a user's environment
1946:
1947: =item del
1948:
1949: deletes keys out of array from namespace
1950:
1951: =item keys
1952:
1953: returns namespace keys
1954:
1955: =item dump
1956:
1957: dumps the complete (or key matching regexp) namespace into a hash
1958:
1959: =item store
1960:
1961: stores hash permanently
1962: for this url; hashref needs to be given and should be a \%hashname; the
1963: remaining args aren't required and if they aren't passed or are '' they will
1964: be derived from the ENV
1965:
1966: =item restore
1967:
1968: returns a hash for a given url
1969:
1970: =item querysend
1971:
1972: Tells client about the lonsql process that has been launched in response
1973: to a sent query.
1974:
1975: =item queryreply
1976:
1977: Accept information from lonsql and make appropriate storage in temporary
1978: file space.
1979:
1980: =item idput
1981:
1982: Defines usernames as corresponding to IDs. (These "IDs" are unique identifiers
1983: for each student, defined perhaps by the institutional Registrar.)
1984:
1985: =item idget
1986:
1987: Returns usernames corresponding to IDs. (These "IDs" are unique identifiers
1988: for each student, defined perhaps by the institutional Registrar.)
1989:
1990: =item tmpput
1991:
1992: Accept and store information in temporary space.
1993:
1994: =item tmpget
1995:
1996: Send along temporarily stored information.
1997:
1998: =item ls
1999:
2000: List part of a user's directory.
2001:
2002: =item Hanging up (exit or init)
2003:
2004: What to do when a client tells the server that they (the client)
2005: are leaving the network.
2006:
2007: =item unknown command
2008:
2009: If B<lond> is sent an unknown command (not in the list above),
2010: it replys to the client "unknown_cmd".
2011:
2012: =item UNKNOWN CLIENT
2013:
2014: If the anti-spoofing algorithm cannot verify the client,
2015: the client is rejected (with a "refused" message sent
2016: to the client, and the connection is closed.
2017:
2018: =back
1.61 harris41 2019:
2020: =head1 PREREQUISITES
2021:
2022: IO::Socket
2023: IO::File
2024: Apache::File
2025: Symbol
2026: POSIX
2027: Crypt::IDEA
2028: LWP::UserAgent()
2029: GDBM_File
2030: Authen::Krb4
1.91 albertel 2031: Authen::Krb5
1.61 harris41 2032:
2033: =head1 COREQUISITES
2034:
2035: =head1 OSNAMES
2036:
2037: linux
2038:
2039: =head1 SCRIPT CATEGORIES
2040:
2041: Server/Process
2042:
2043: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>