Annotation of loncom/lond, revision 1.98
1.1 albertel 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
1.60 www 4: #
1.98 ! foxr 5: # $Id: lond,v 1.97 2002/09/16 13:26:21 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') {
698: $pwdcorrect=
699: (crypt($upass,$contentpwd) eq $contentpwd);
700: } elsif ($howpwd eq 'unix') {
701: $contentpwd=(getpwnam($uname))[1];
1.52 harris41 702: my $pwauth_path="/usr/local/sbin/pwauth";
703: unless ($contentpwd eq 'x') {
704: $pwdcorrect=
705: (crypt($upass,$contentpwd) eq $contentpwd);
706: }
707: elsif (-e $pwauth_path) {
708: open PWAUTH, "|$pwauth_path" or
709: die "Cannot invoke authentication";
710: print PWAUTH "$uname\n$upass\n";
711: close PWAUTH;
712: $pwdcorrect=!$?;
713: }
1.3 www 714: } elsif ($howpwd eq 'krb4') {
1.71 www 715: $null=pack("C",0);
716: unless ($upass=~/$null/) {
1.3 www 717: $pwdcorrect=(
718: Authen::Krb4::get_pw_in_tkt($uname,"",
719: $contentpwd,'krbtgt',$contentpwd,1,
720: $upass) == 0);
1.71 www 721: } else { $pwdcorrect=0; }
1.91 albertel 722: } elsif ($howpwd eq 'krb5') {
723: $null=pack("C",0);
724: unless ($upass=~/$null/) {
725: my $krbclient=&Authen::Krb5::parse_name($uname.'@'.$contentpwd);
726: my $krbservice="krbtgt/".$contentpwd."\@".$contentpwd;
727: my $krbserver=&Authen::Krb5::parse_name($krbservice);
728: my $credentials=&Authen::Krb5::cc_default();
729: $credentials->initialize($krbclient);
730: my $krbreturn =
731: &Authen::Krb5::get_in_tkt_with_password(
732: $krbclient,$krbserver,$upass,$credentials);
1.92 albertel 733: # unless ($krbreturn) {
734: # &logthis("Krb5 Error: ".
735: # &Authen::Krb5::error());
736: # }
1.91 albertel 737: $pwdcorrect = ($krbreturn == 1);
738: } else { $pwdcorrect=0; }
1.50 albertel 739: } elsif ($howpwd eq 'localauth') {
1.49 albertel 740: $pwdcorrect=&localauth::localauth($uname,$upass,
741: $contentpwd);
742: }
1.2 www 743: if ($pwdcorrect) {
1.1 albertel 744: print $client "authorized\n";
745: } else {
746: print $client "non_authorized\n";
747: }
748: } else {
749: print $client "unknown_user\n";
750: }
751: } else {
752: print $client "refused\n";
753: }
754: # ---------------------------------------------------------------------- passwd
755: } elsif ($userinput =~ /^passwd/) {
756: if ($wasenc==1) {
757: my
758: ($cmd,$udom,$uname,$upass,$npass)=split(/:/,$userinput);
759: chomp($npass);
1.32 www 760: $upass=&unescape($upass);
761: $npass=&unescape($npass);
1.98 ! foxr 762: &Debug("Trying to change password for $uname");
1.72 matthew 763: my $proname=propath($udom,$uname);
1.1 albertel 764: my $passfilename="$proname/passwd";
765: if (-e $passfilename) {
766: my $realpasswd;
767: { my $pf = IO::File->new($passfilename);
768: $realpasswd=<$pf>; }
769: chomp($realpasswd);
1.2 www 770: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
771: if ($howpwd eq 'internal') {
1.98 ! foxr 772: &Debug("internal auth");
1.2 www 773: if (crypt($upass,$contentpwd) eq $contentpwd) {
774: my $salt=time;
775: $salt=substr($salt,6,2);
776: my $ncpass=crypt($npass,$salt);
1.1 albertel 777: { my $pf = IO::File->new(">$passfilename");
1.31 www 778: print $pf "internal:$ncpass\n"; }
1.72 matthew 779: &logthis("Result of password change for $uname: pwchange_success");
1.1 albertel 780: print $client "ok\n";
1.2 www 781: } else {
782: print $client "non_authorized\n";
783: }
1.72 matthew 784: } elsif ($howpwd eq 'unix') {
785: # Unix means we have to access /etc/password
786: # one way or another.
787: # First: Make sure the current password is
788: # correct
1.98 ! foxr 789: &Debug("auth is unix");
1.72 matthew 790: $contentpwd=(getpwnam($uname))[1];
791: my $pwdcorrect = "0";
792: my $pwauth_path="/usr/local/sbin/pwauth";
793: unless ($contentpwd eq 'x') {
794: $pwdcorrect=
795: (crypt($upass,$contentpwd) eq $contentpwd);
796: } elsif (-e $pwauth_path) {
797: open PWAUTH, "|$pwauth_path" or
798: die "Cannot invoke authentication";
799: print PWAUTH "$uname\n$upass\n";
800: close PWAUTH;
1.98 ! foxr 801: &Debug("exited pwauth with $? ($uname,$upass) ");
! 802: $pwdcorrect=($? == 0);
1.72 matthew 803: }
804: if ($pwdcorrect) {
805: my $execdir=$perlvar{'lonDaemons'};
1.98 ! foxr 806: &Debug("Opening lcpasswd pipeline");
! 807: my $pf = IO::File->new("|$execdir/lcpasswd > /home/www/lcpasswd.log");
1.72 matthew 808: print $pf "$uname\n$npass\n$npass\n";
809: close $pf;
1.97 foxr 810: my $err = $?;
811: my $result = ($err>0 ? 'pwchange_failure'
1.72 matthew 812: : 'ok');
1.96 foxr 813: &logthis("Result of password change for $uname: ".
814: &lcpasswdstrerror($?));
1.72 matthew 815: print $client "$result\n";
816: } else {
817: print $client "non_authorized\n";
818: }
819: } else {
1.2 www 820: print $client "auth_mode_error\n";
1.1 albertel 821: }
822: } else {
823: print $client "unknown_user\n";
1.31 www 824: }
825: } else {
826: print $client "refused\n";
827: }
828: # -------------------------------------------------------------------- makeuser
829: } elsif ($userinput =~ /^makeuser/) {
1.91 albertel 830: &Debug("Make user received");
1.56 harris41 831: my $oldumask=umask(0077);
1.31 www 832: if ($wasenc==1) {
833: my
834: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
1.77 foxr 835: &Debug("cmd =".$cmd." $udom =".$udom.
836: " uname=".$uname);
1.31 www 837: chomp($npass);
1.32 www 838: $npass=&unescape($npass);
1.31 www 839: my $proname=propath($udom,$uname);
840: my $passfilename="$proname/passwd";
1.77 foxr 841: &Debug("Password file created will be:".
842: $passfilename);
1.31 www 843: if (-e $passfilename) {
844: print $client "already_exists\n";
845: } elsif ($udom ne $perlvar{'lonDefDomain'}) {
846: print $client "not_right_domain\n";
847: } else {
848: @fpparts=split(/\//,$proname);
849: $fpnow=$fpparts[0].'/'.$fpparts[1].'/'.$fpparts[2];
850: $fperror='';
851: for ($i=3;$i<=$#fpparts;$i++) {
852: $fpnow.='/'.$fpparts[$i];
853: unless (-e $fpnow) {
854: unless (mkdir($fpnow,0777)) {
1.65 www 855: $fperror="error:$!";
1.31 www 856: }
857: }
858: }
859: unless ($fperror) {
1.98 ! foxr 860: my $result=&make_passwd_file($uname, $umode,$npass,
1.91 albertel 861: $passfilename);
862: print $client $result;
1.31 www 863: } else {
864: print $client "$fperror\n";
865: }
1.55 harris41 866: }
867: } else {
868: print $client "refused\n";
869: }
1.56 harris41 870: umask($oldumask);
1.55 harris41 871: # -------------------------------------------------------------- changeuserauth
872: } elsif ($userinput =~ /^changeuserauth/) {
1.77 foxr 873: &Debug("Changing authorization");
874: if ($wasenc==1) {
1.55 harris41 875: my
1.91 albertel 876: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
1.55 harris41 877: chomp($npass);
1.77 foxr 878: &Debug("cmd = ".$cmd." domain= ".$udom.
879: "uname =".$uname." umode= ".$umode);
1.55 harris41 880: $npass=&unescape($npass);
1.91 albertel 881: my $proname=&propath($udom,$uname);
1.55 harris41 882: my $passfilename="$proname/passwd";
883: if ($udom ne $perlvar{'lonDefDomain'}) {
884: print $client "not_right_domain\n";
885: } else {
1.98 ! foxr 886: my $result=&make_passwd_file($uname, $umode,$npass,
1.93 albertel 887: $passfilename);
1.91 albertel 888: print $client $result;
1.1 albertel 889: }
890: } else {
891: print $client "refused\n";
892: }
893: # ------------------------------------------------------------------------ home
894: } elsif ($userinput =~ /^home/) {
895: my ($cmd,$udom,$uname)=split(/:/,$userinput);
896: chomp($uname);
897: my $proname=propath($udom,$uname);
898: if (-e $proname) {
899: print $client "found\n";
900: } else {
901: print $client "not_found\n";
902: }
903: # ---------------------------------------------------------------------- update
904: } elsif ($userinput =~ /^update/) {
905: my ($cmd,$fname)=split(/:/,$userinput);
906: my $ownership=ishome($fname);
907: if ($ownership eq 'not_owner') {
908: if (-e $fname) {
909: my ($dev,$ino,$mode,$nlink,
910: $uid,$gid,$rdev,$size,
911: $atime,$mtime,$ctime,
912: $blksize,$blocks)=stat($fname);
913: $now=time;
914: $since=$now-$atime;
915: if ($since>$perlvar{'lonExpire'}) {
916: $reply=
917: reply("unsub:$fname","$hostid{$clientip}");
918: unlink("$fname");
919: } else {
920: my $transname="$fname.in.transfer";
921: my $remoteurl=
922: reply("sub:$fname","$hostid{$clientip}");
923: my $response;
924: {
925: my $ua=new LWP::UserAgent;
926: my $request=new HTTP::Request('GET',"$remoteurl");
927: $response=$ua->request($request,$transname);
928: }
929: if ($response->is_error()) {
1.24 albertel 930: unlink($transname);
1.1 albertel 931: my $message=$response->status_line;
932: &logthis(
933: "LWP GET: $message for $fname ($remoteurl)");
934: } else {
1.14 www 935: if ($remoteurl!~/\.meta$/) {
1.28 www 936: my $ua=new LWP::UserAgent;
1.14 www 937: my $mrequest=
938: new HTTP::Request('GET',$remoteurl.'.meta');
939: my $mresponse=
940: $ua->request($mrequest,$fname.'.meta');
941: if ($mresponse->is_error()) {
942: unlink($fname.'.meta');
943: }
944: }
1.1 albertel 945: rename($transname,$fname);
946: }
947: }
948: print $client "ok\n";
949: } else {
950: print $client "not_found\n";
951: }
952: } else {
953: print $client "rejected\n";
954: }
1.85 www 955: # -------------------------------------- fetch a user file from a remote server
956: } elsif ($userinput =~ /^fetchuserfile/) {
1.86 www 957: my ($cmd,$fname)=split(/:/,$userinput);
958: my ($udom,$uname,$ufile)=split(/\//,$fname);
959: my $udir=propath($udom,$uname).'/userfiles';
1.88 albertel 960: unless (-e $udir) { mkdir($udir,0770); }
1.86 www 961: if (-e $udir) {
962: $ufile=~s/^[\.\~]+//;
963: $ufile=~s/\///g;
964: my $transname=$udir.'/'.$ufile;
965: my $remoteurl='http://'.$clientip.'/userfiles/'.$fname;
966: my $response;
967: {
968: my $ua=new LWP::UserAgent;
969: my $request=new HTTP::Request('GET',"$remoteurl");
970: $response=$ua->request($request,$transname);
971: }
972: if ($response->is_error()) {
973: unlink($transname);
974: my $message=$response->status_line;
975: &logthis(
976: "LWP GET: $message for $fname ($remoteurl)");
977: print $client "failed\n";
978: } else {
979: print $client "ok\n";
980: }
981: } else {
982: print $client "not_home\n";
983: }
1.85 www 984: # ------------------------------------------ authenticate access to a user file
1.86 www 985: } elsif ($userinput =~ /^tokenauthuserfile/) {
1.85 www 986: my ($cmd,$fname,$session)=split(/:/,$userinput);
1.86 www 987: chomp($session);
988: $reply='non_auth';
989: if (open(ENVIN,$perlvar{'lonIDsDir'}.'/'.
990: $session.'.id')) {
991: while ($line=<ENVIN>) {
992: if ($line=~/userfile\.$fname\=/) { $reply='ok'; }
993: }
994: close(ENVIN);
995: print $client $reply."\n";
996: } else {
997: print $client "invalid_token\n";
998: }
1.1 albertel 999: # ----------------------------------------------------------------- unsubscribe
1000: } elsif ($userinput =~ /^unsub/) {
1001: my ($cmd,$fname)=split(/:/,$userinput);
1002: if (-e $fname) {
1.84 albertel 1003: print $client &unsub($client,$fname,$clientip);
1.1 albertel 1004: } else {
1005: print $client "not_found\n";
1006: }
1007: # ------------------------------------------------------------------- subscribe
1008: } elsif ($userinput =~ /^sub/) {
1.84 albertel 1009: print $client &subscribe($userinput,$clientip);
1.12 harris41 1010: # ------------------------------------------------------------------------- log
1011: } elsif ($userinput =~ /^log/) {
1012: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
1013: chomp($what);
1014: my $proname=propath($udom,$uname);
1015: my $now=time;
1016: {
1017: my $hfh;
1018: if ($hfh=IO::File->new(">>$proname/activity.log")) {
1019: print $hfh "$now:$hostid{$clientip}:$what\n";
1020: print $client "ok\n";
1021: } else {
1022: print $client "error:$!\n";
1023: }
1024: }
1.1 albertel 1025: # ------------------------------------------------------------------------- put
1026: } elsif ($userinput =~ /^put/) {
1.6 www 1027: my ($cmd,$udom,$uname,$namespace,$what)
1.1 albertel 1028: =split(/:/,$userinput);
1.8 www 1029: $namespace=~s/\//\_/g;
1.6 www 1030: $namespace=~s/\W//g;
1031: if ($namespace ne 'roles') {
1.1 albertel 1032: chomp($what);
1033: my $proname=propath($udom,$uname);
1034: my $now=time;
1.48 www 1035: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1036: my $hfh;
1037: if (
1038: $hfh=IO::File->new(">>$proname/$namespace.hist")
1039: ) { print $hfh "P:$now:$what\n"; }
1040: }
1041: my @pairs=split(/\&/,$what);
1.4 www 1042: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1043: foreach $pair (@pairs) {
1044: ($key,$value)=split(/=/,$pair);
1045: $hash{$key}=$value;
1046: }
1.4 www 1047: if (untie(%hash)) {
1.1 albertel 1048: print $client "ok\n";
1049: } else {
1050: print $client "error:$!\n";
1051: }
1052: } else {
1053: print $client "error:$!\n";
1054: }
1.6 www 1055: } else {
1056: print $client "refused\n";
1057: }
1058: # -------------------------------------------------------------------- rolesput
1059: } elsif ($userinput =~ /^rolesput/) {
1.77 foxr 1060: &Debug("rolesput");
1.6 www 1061: if ($wasenc==1) {
1062: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
1063: =split(/:/,$userinput);
1.77 foxr 1064: &Debug("cmd = ".$cmd." exedom= ".$exedom.
1065: "user = ".$exeuser." udom=".$udom.
1066: "what = ".$what);
1.6 www 1067: my $namespace='roles';
1068: chomp($what);
1069: my $proname=propath($udom,$uname);
1070: my $now=time;
1071: {
1072: my $hfh;
1073: if (
1074: $hfh=IO::File->new(">>$proname/$namespace.hist")
1075: ) {
1076: print $hfh "P:$now:$exedom:$exeuser:$what\n";
1077: }
1078: }
1079: my @pairs=split(/\&/,$what);
1080: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1081: foreach $pair (@pairs) {
1082: ($key,$value)=split(/=/,$pair);
1.78 foxr 1083: &ManagePermissions($key, $udom, $uname,
1084: &GetAuthType( $udom,
1085: $uname));
1.6 www 1086: $hash{$key}=$value;
1.78 foxr 1087:
1.6 www 1088: }
1089: if (untie(%hash)) {
1090: print $client "ok\n";
1091: } else {
1092: print $client "error:$!\n";
1093: }
1094: } else {
1095: print $client "error:$!\n";
1096: }
1097: } else {
1098: print $client "refused\n";
1099: }
1.1 albertel 1100: # ------------------------------------------------------------------------- get
1101: } elsif ($userinput =~ /^get/) {
1102: my ($cmd,$udom,$uname,$namespace,$what)
1103: =split(/:/,$userinput);
1.8 www 1104: $namespace=~s/\//\_/g;
1.1 albertel 1105: $namespace=~s/\W//g;
1106: chomp($what);
1107: my @queries=split(/\&/,$what);
1108: my $proname=propath($udom,$uname);
1109: my $qresult='';
1.20 www 1110: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1111: for ($i=0;$i<=$#queries;$i++) {
1112: $qresult.="$hash{$queries[$i]}&";
1113: }
1.4 www 1114: if (untie(%hash)) {
1.1 albertel 1115: $qresult=~s/\&$//;
1116: print $client "$qresult\n";
1117: } else {
1118: print $client "error:$!\n";
1119: }
1120: } else {
1121: print $client "error:$!\n";
1122: }
1123: # ------------------------------------------------------------------------ eget
1124: } elsif ($userinput =~ /^eget/) {
1125: my ($cmd,$udom,$uname,$namespace,$what)
1126: =split(/:/,$userinput);
1.8 www 1127: $namespace=~s/\//\_/g;
1.1 albertel 1128: $namespace=~s/\W//g;
1129: chomp($what);
1130: my @queries=split(/\&/,$what);
1131: my $proname=propath($udom,$uname);
1132: my $qresult='';
1.20 www 1133: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1134: for ($i=0;$i<=$#queries;$i++) {
1135: $qresult.="$hash{$queries[$i]}&";
1136: }
1.4 www 1137: if (untie(%hash)) {
1.1 albertel 1138: $qresult=~s/\&$//;
1139: if ($cipher) {
1140: my $cmdlength=length($qresult);
1141: $qresult.=" ";
1142: my $encqresult='';
1143: for
1144: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
1145: $encqresult.=
1146: unpack("H16",
1147: $cipher->encrypt(substr($qresult,$encidx,8)));
1148: }
1149: print $client "enc:$cmdlength:$encqresult\n";
1150: } else {
1151: print $client "error:no_key\n";
1152: }
1153: } else {
1154: print $client "error:$!\n";
1155: }
1156: } else {
1157: print $client "error:$!\n";
1158: }
1159: # ------------------------------------------------------------------------- del
1160: } elsif ($userinput =~ /^del/) {
1161: my ($cmd,$udom,$uname,$namespace,$what)
1162: =split(/:/,$userinput);
1.8 www 1163: $namespace=~s/\//\_/g;
1.1 albertel 1164: $namespace=~s/\W//g;
1165: chomp($what);
1166: my $proname=propath($udom,$uname);
1167: my $now=time;
1.48 www 1168: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1169: my $hfh;
1170: if (
1171: $hfh=IO::File->new(">>$proname/$namespace.hist")
1172: ) { print $hfh "D:$now:$what\n"; }
1173: }
1174: my @keys=split(/\&/,$what);
1.4 www 1175: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1176: foreach $key (@keys) {
1177: delete($hash{$key});
1178: }
1.4 www 1179: if (untie(%hash)) {
1.1 albertel 1180: print $client "ok\n";
1181: } else {
1182: print $client "error:$!\n";
1183: }
1184: } else {
1185: print $client "error:$!\n";
1186: }
1187: # ------------------------------------------------------------------------ keys
1188: } elsif ($userinput =~ /^keys/) {
1189: my ($cmd,$udom,$uname,$namespace)
1190: =split(/:/,$userinput);
1.8 www 1191: $namespace=~s/\//\_/g;
1.1 albertel 1192: $namespace=~s/\W//g;
1193: my $proname=propath($udom,$uname);
1194: my $qresult='';
1.20 www 1195: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1196: foreach $key (keys %hash) {
1197: $qresult.="$key&";
1198: }
1.4 www 1199: if (untie(%hash)) {
1.1 albertel 1200: $qresult=~s/\&$//;
1201: print $client "$qresult\n";
1202: } else {
1203: print $client "error:$!\n";
1204: }
1205: } else {
1206: print $client "error:$!\n";
1207: }
1208: # ------------------------------------------------------------------------ dump
1209: } elsif ($userinput =~ /^dump/) {
1.62 www 1210: my ($cmd,$udom,$uname,$namespace,$regexp)
1.1 albertel 1211: =split(/:/,$userinput);
1.8 www 1212: $namespace=~s/\//\_/g;
1.1 albertel 1213: $namespace=~s/\W//g;
1.62 www 1214: if (defined($regexp)) {
1215: $regexp=&unescape($regexp);
1216: } else {
1217: $regexp='.';
1218: }
1.1 albertel 1219: my $proname=propath($udom,$uname);
1220: my $qresult='';
1.20 www 1221: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.90 stredwic 1222: study($regexp);
1.1 albertel 1223: foreach $key (keys %hash) {
1.90 stredwic 1224: my $unescapeKey = &unescape($key);
1225: if (eval('$unescapeKey=~/$regexp/')) {
1.62 www 1226: $qresult.="$key=$hash{$key}&";
1.90 stredwic 1227: }
1.7 www 1228: }
1229: if (untie(%hash)) {
1230: $qresult=~s/\&$//;
1231: print $client "$qresult\n";
1232: } else {
1233: print $client "error:$!\n";
1234: }
1235: } else {
1236: print $client "error:$!\n";
1237: }
1238: # ----------------------------------------------------------------------- store
1239: } elsif ($userinput =~ /^store/) {
1240: my ($cmd,$udom,$uname,$namespace,$rid,$what)
1241: =split(/:/,$userinput);
1.8 www 1242: $namespace=~s/\//\_/g;
1.7 www 1243: $namespace=~s/\W//g;
1244: if ($namespace ne 'roles') {
1245: chomp($what);
1246: my $proname=propath($udom,$uname);
1247: my $now=time;
1.48 www 1248: unless ($namespace=~/^nohist\_/) {
1.7 www 1249: my $hfh;
1250: if (
1251: $hfh=IO::File->new(">>$proname/$namespace.hist")
1252: ) { print $hfh "P:$now:$rid:$what\n"; }
1253: }
1254: my @pairs=split(/\&/,$what);
1255:
1256: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1257: my @previouskeys=split(/&/,$hash{"keys:$rid"});
1258: my $key;
1259: $hash{"version:$rid"}++;
1260: my $version=$hash{"version:$rid"};
1261: my $allkeys='';
1262: foreach $pair (@pairs) {
1263: ($key,$value)=split(/=/,$pair);
1264: $allkeys.=$key.':';
1265: $hash{"$version:$rid:$key"}=$value;
1266: }
1.36 www 1267: $hash{"$version:$rid:timestamp"}=$now;
1268: $allkeys.='timestamp';
1.7 www 1269: $hash{"$version:keys:$rid"}=$allkeys;
1270: if (untie(%hash)) {
1271: print $client "ok\n";
1272: } else {
1273: print $client "error:$!\n";
1274: }
1275: } else {
1276: print $client "error:$!\n";
1277: }
1278: } else {
1279: print $client "refused\n";
1280: }
1281: # --------------------------------------------------------------------- restore
1282: } elsif ($userinput =~ /^restore/) {
1283: my ($cmd,$udom,$uname,$namespace,$rid)
1284: =split(/:/,$userinput);
1.8 www 1285: $namespace=~s/\//\_/g;
1.7 www 1286: $namespace=~s/\W//g;
1287: chomp($rid);
1288: my $proname=propath($udom,$uname);
1289: my $qresult='';
1.20 www 1290: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.7 www 1291: my $version=$hash{"version:$rid"};
1292: $qresult.="version=$version&";
1293: my $scope;
1294: for ($scope=1;$scope<=$version;$scope++) {
1295: my $vkeys=$hash{"$scope:keys:$rid"};
1296: my @keys=split(/:/,$vkeys);
1297: my $key;
1298: $qresult.="$scope:keys=$vkeys&";
1299: foreach $key (@keys) {
1.21 www 1300: $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
1.7 www 1301: }
1.1 albertel 1302: }
1.4 www 1303: if (untie(%hash)) {
1.1 albertel 1304: $qresult=~s/\&$//;
1305: print $client "$qresult\n";
1306: } else {
1307: print $client "error:$!\n";
1308: }
1309: } else {
1310: print $client "error:$!\n";
1311: }
1.86 www 1312: # -------------------------------------------------------------------- chatsend
1313: } elsif ($userinput =~ /^chatsend/) {
1314: my ($cmd,$cdom,$cnum,$newpost)=split(/\:/,$userinput);
1315: &chatadd($cdom,$cnum,$newpost);
1316: print $client "ok\n";
1317: # -------------------------------------------------------------------- chatretr
1318: } elsif ($userinput =~ /^chatretr/) {
1319: my ($cmd,$cdom,$cnum)=split(/\:/,$userinput);
1320: my $reply='';
1321: foreach (&getchat($cdom,$cnum)) {
1322: $reply.=&escape($_).':';
1323: }
1324: $reply=~s/\:$//;
1325: print $client $reply."\n";
1.12 harris41 1326: # ------------------------------------------------------------------- querysend
1327: } elsif ($userinput =~ /^querysend/) {
1.44 harris41 1328: my ($cmd,$query,
1.82 www 1329: $arg1,$arg2,$arg3)=split(/\:/,$userinput);
1.12 harris41 1330: $query=~s/\n*$//g;
1.82 www 1331: print $client "".
1.40 harris41 1332: sqlreply("$hostid{$clientip}\&$query".
1.82 www 1333: "\&$arg1"."\&$arg2"."\&$arg3")."\n";
1.12 harris41 1334: # ------------------------------------------------------------------ queryreply
1335: } elsif ($userinput =~ /^queryreply/) {
1336: my ($cmd,$id,$reply)=split(/:/,$userinput);
1337: my $store;
1.13 www 1338: my $execdir=$perlvar{'lonDaemons'};
1339: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.43 harris41 1340: $reply=~s/\&/\n/g;
1.12 harris41 1341: print $store $reply;
1342: close $store;
1.46 harris41 1343: my $store2=IO::File->new(">$execdir/tmp/$id.end");
1344: print $store2 "done\n";
1345: close $store2;
1.12 harris41 1346: print $client "ok\n";
1347: }
1348: else {
1349: print $client "error:$!\n";
1350: }
1.1 albertel 1351: # ----------------------------------------------------------------------- idput
1352: } elsif ($userinput =~ /^idput/) {
1353: my ($cmd,$udom,$what)=split(/:/,$userinput);
1354: chomp($what);
1355: $udom=~s/\W//g;
1356: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1357: my $now=time;
1358: {
1359: my $hfh;
1360: if (
1361: $hfh=IO::File->new(">>$proname.hist")
1362: ) { print $hfh "P:$now:$what\n"; }
1363: }
1364: my @pairs=split(/\&/,$what);
1.4 www 1365: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1366: foreach $pair (@pairs) {
1367: ($key,$value)=split(/=/,$pair);
1368: $hash{$key}=$value;
1369: }
1.4 www 1370: if (untie(%hash)) {
1.1 albertel 1371: print $client "ok\n";
1372: } else {
1373: print $client "error:$!\n";
1374: }
1375: } else {
1376: print $client "error:$!\n";
1377: }
1378: # ----------------------------------------------------------------------- idget
1379: } elsif ($userinput =~ /^idget/) {
1380: my ($cmd,$udom,$what)=split(/:/,$userinput);
1381: chomp($what);
1382: $udom=~s/\W//g;
1383: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1384: my @queries=split(/\&/,$what);
1385: my $qresult='';
1.20 www 1386: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER,0640)) {
1.1 albertel 1387: for ($i=0;$i<=$#queries;$i++) {
1388: $qresult.="$hash{$queries[$i]}&";
1389: }
1.4 www 1390: if (untie(%hash)) {
1.1 albertel 1391: $qresult=~s/\&$//;
1392: print $client "$qresult\n";
1393: } else {
1394: print $client "error:$!\n";
1395: }
1396: } else {
1397: print $client "error:$!\n";
1398: }
1.13 www 1399: # ---------------------------------------------------------------------- tmpput
1400: } elsif ($userinput =~ /^tmpput/) {
1401: my ($cmd,$what)=split(/:/,$userinput);
1402: my $store;
1403: $tmpsnum++;
1404: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
1405: $id=~s/\W/\_/g;
1406: $what=~s/\n//g;
1407: my $execdir=$perlvar{'lonDaemons'};
1408: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1409: print $store $what;
1410: close $store;
1411: print $client "$id\n";
1412: }
1413: else {
1414: print $client "error:$!\n";
1415: }
1416:
1417: # ---------------------------------------------------------------------- tmpget
1418: } elsif ($userinput =~ /^tmpget/) {
1419: my ($cmd,$id)=split(/:/,$userinput);
1420: chomp($id);
1421: $id=~s/\W/\_/g;
1422: my $store;
1423: my $execdir=$perlvar{'lonDaemons'};
1424: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
1425: my $reply=<$store>;
1426: print $client "$reply\n";
1427: close $store;
1428: }
1429: else {
1430: print $client "error:$!\n";
1431: }
1432:
1.5 www 1433: # -------------------------------------------------------------------------- ls
1434: } elsif ($userinput =~ /^ls/) {
1435: my ($cmd,$ulsdir)=split(/:/,$userinput);
1436: my $ulsout='';
1437: my $ulsfn;
1438: if (-e $ulsdir) {
1.83 stredwic 1439: if(-d $ulsdir) {
1440: if (opendir(LSDIR,$ulsdir)) {
1441: while ($ulsfn=readdir(LSDIR)) {
1442: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1443: $ulsout.=$ulsfn.'&'.
1444: join('&',@ulsstats).':';
1445: }
1446: closedir(LSDIR);
1447: }
1448: } else {
1449: my @ulsstats=stat($ulsdir);
1450: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1451: }
1452: } else {
1.5 www 1453: $ulsout='no_such_dir';
1454: }
1.17 www 1455: if ($ulsout eq '') { $ulsout='empty'; }
1.5 www 1456: print $client "$ulsout\n";
1.51 www 1457: # ------------------------------------------------------------------ Hanging up
1458: } elsif (($userinput =~ /^exit/) ||
1459: ($userinput =~ /^init/)) {
1460: &logthis(
1461: "Client $clientip ($hostid{$clientip}) hanging up: $userinput");
1462: print $client "bye\n";
1.59 www 1463: $client->close();
1.51 www 1464: last;
1.1 albertel 1465: # ------------------------------------------------------------- unknown command
1466: } else {
1467: # unknown command
1468: print $client "unknown_cmd\n";
1469: }
1.58 www 1470: # -------------------------------------------------------------------- complete
1.63 www 1471: alarm(0);
1.58 www 1472: &status('Listening to '.$hostid{$clientip});
1473: }
1.59 www 1474: # --------------------------------------------- client unknown or fishy, refuse
1.1 albertel 1475: } else {
1476: print $client "refused\n";
1.59 www 1477: $client->close();
1.9 www 1478: &logthis("<font color=blue>WARNING: "
1479: ."Rejected client $clientip, closing connection</font>");
1.75 foxr 1480: }
1481: }
1482:
1.1 albertel 1483: # =============================================================================
1.75 foxr 1484:
1485: &logthis("<font color=red>CRITICAL: "
1486: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.1 albertel 1487: # tidy up gracefully and finish
1488:
1.59 www 1489: $server->close();
1490:
1.1 albertel 1491: # this exit is VERY important, otherwise the child will become
1492: # a producer of more and more children, forking yourself into
1493: # process death.
1494: exit;
1495: }
1.78 foxr 1496: }
1497:
1498:
1499: #
1500: # Checks to see if the input roleput request was to set
1501: # an author role. If so, invokes the lchtmldir script to set
1502: # up a correct public_html
1503: # Parameters:
1504: # request - The request sent to the rolesput subchunk.
1505: # We're looking for /domain/_au
1506: # domain - The domain in which the user is having roles doctored.
1507: # user - Name of the user for which the role is being put.
1508: # authtype - The authentication type associated with the user.
1509: #
1510: sub ManagePermissions
1511: {
1512: my $request = shift;
1513: my $domain = shift;
1514: my $user = shift;
1515: my $authtype= shift;
1516:
1517: # See if the request is of the form /$domain/_au
1518:
1519: if($request =~ /^(\/$domain\/_au)$/) { # It's an author rolesput...
1520: my $execdir = $perlvar{'lonDaemons'};
1521: my $userhome= "/home/$user" ;
1522: Debug("system $execdir/lchtmldir $userhome $system $authtype");
1523: system("$execdir/lchtmldir $userhome $user $authtype");
1524: }
1525: }
1526: #
1527: # GetAuthType - Determines the authorization type of a user in a domain.
1528:
1529: # Returns the authorization type or nouser if there is no such user.
1530: #
1531: sub GetAuthType
1532: {
1533: my $domain = shift;
1534: my $user = shift;
1535:
1.79 foxr 1536: Debug("GetAuthType( $domain, $user ) \n");
1.78 foxr 1537: my $proname = &propath($domain, $user);
1538: my $passwdfile = "$proname/passwd";
1539: if( -e $passwdfile ) {
1540: my $pf = IO::File->new($passwdfile);
1541: my $realpassword = <$pf>;
1542: chomp($realpassword);
1.79 foxr 1543: Debug("Password info = $realpassword\n");
1.78 foxr 1544: my ($authtype, $contentpwd) = split(/:/, $realpassword);
1.79 foxr 1545: Debug("Authtype = $authtype, content = $contentpwd\n");
1.78 foxr 1546: my $availinfo = '';
1.91 albertel 1547: if($authtype eq 'krb4' or $authtype eq 'krb5') {
1.78 foxr 1548: $availinfo = $contentpwd;
1549: }
1.79 foxr 1550:
1.78 foxr 1551: return "$authtype:$availinfo";
1552: }
1553: else {
1.79 foxr 1554: Debug("Returning nouser");
1.78 foxr 1555: return "nouser";
1556: }
1.1 albertel 1557: }
1558:
1.84 albertel 1559: sub addline {
1560: my ($fname,$hostid,$ip,$newline)=@_;
1561: my $contents;
1562: my $found=0;
1563: my $expr='^'.$hostid.':'.$ip.':';
1564: $expr =~ s/\./\\\./g;
1565: if ($sh=IO::File->new("$fname.subscription")) {
1566: while (my $subline=<$sh>) {
1567: if ($subline !~ /$expr/) {$contents.= $subline;} else {$found=1;}
1568: }
1569: $sh->close();
1570: }
1571: $sh=IO::File->new(">$fname.subscription");
1572: if ($contents) { print $sh $contents; }
1573: if ($newline) { print $sh $newline; }
1574: $sh->close();
1575: return $found;
1.86 www 1576: }
1577:
1578: sub getchat {
1579: my ($cdom,$cname)=@_;
1.87 www 1580: my %hash;
1581: my $proname=&propath($cdom,$cname);
1582: my @entries=();
1.88 albertel 1583: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
1584: &GDBM_READER(),0640)) {
1585: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
1586: untie %hash;
1.86 www 1587: }
1.87 www 1588: return @entries;
1.86 www 1589: }
1590:
1591: sub chatadd {
1.88 albertel 1592: my ($cdom,$cname,$newchat)=@_;
1593: my %hash;
1594: my $proname=&propath($cdom,$cname);
1595: my @entries=();
1596: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
1597: &GDBM_WRCREAT(),0640)) {
1598: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
1599: my $time=time;
1600: my ($lastid)=($entries[$#entries]=~/^(\w+)\:/);
1601: my ($thentime,$idnum)=split(/\_/,$lastid);
1602: my $newid=$time.'_000000';
1603: if ($thentime==$time) {
1604: $idnum=~s/^0+//;
1605: $idnum++;
1606: $idnum=substr('000000'.$idnum,-6,6);
1607: $newid=$time.'_'.$idnum;
1608: }
1609: $hash{$newid}=$newchat;
1610: my $expired=$time-3600;
1611: foreach (keys %hash) {
1612: my ($thistime)=($_=~/(\d+)\_/);
1613: if ($thistime<$expired) {
1.89 www 1614: delete $hash{$_};
1.88 albertel 1615: }
1616: }
1617: untie %hash;
1.86 www 1618: }
1.84 albertel 1619: }
1620:
1621: sub unsub {
1622: my ($fname,$clientip)=@_;
1623: my $result;
1624: if (unlink("$fname.$hostid{$clientip}")) {
1625: $result="ok\n";
1626: } else {
1627: $result="not_subscribed\n";
1628: }
1629: if (-e "$fname.subscription") {
1630: my $found=&addline($fname,$hostid{$clientip},$clientip,'');
1631: if ($found) { $result="ok\n"; }
1632: } else {
1633: if ($result != "ok\n") { $result="not_subscribed\n"; }
1634: }
1635: return $result;
1636: }
1637:
1638: sub subscribe {
1639: my ($userinput,$clientip)=@_;
1640: my $result;
1641: my ($cmd,$fname)=split(/:/,$userinput);
1642: my $ownership=&ishome($fname);
1643: if ($ownership eq 'owner') {
1644: if (-e $fname) {
1645: if (-d $fname) {
1646: $result="directory\n";
1647: } else {
1648: if (-e "$fname.$hostid{$clientip}") {&unsub($fname,$clientip);}
1649: $now=time;
1650: my $found=&addline($fname,$hostid{$clientip},$clientip,
1651: "$hostid{$clientip}:$clientip:$now\n");
1652: if ($found) { $result="$fname\n"; }
1653: # if they were subscribed to only meta data, delete that
1654: # subscription, when you subscribe to a file you also get
1655: # the metadata
1656: unless ($fname=~/\.meta$/) { &unsub("$fname.meta",$clientip); }
1657: $fname=~s/\/home\/httpd\/html\/res/raw/;
1658: $fname="http://$thisserver/".$fname;
1659: $result="$fname\n";
1660: }
1661: } else {
1662: $result="not_found\n";
1663: }
1664: } else {
1665: $result="rejected\n";
1666: }
1667: return $result;
1668: }
1.91 albertel 1669:
1670: sub make_passwd_file {
1.98 ! foxr 1671: my ($uname, $umode,$npass,$passfilename)=@_;
1.91 albertel 1672: my $result="ok\n";
1673: if ($umode eq 'krb4' or $umode eq 'krb5') {
1674: {
1675: my $pf = IO::File->new(">$passfilename");
1676: print $pf "$umode:$npass\n";
1677: }
1678: } elsif ($umode eq 'internal') {
1679: my $salt=time;
1680: $salt=substr($salt,6,2);
1681: my $ncpass=crypt($npass,$salt);
1682: {
1683: &Debug("Creating internal auth");
1684: my $pf = IO::File->new(">$passfilename");
1685: print $pf "internal:$ncpass\n";
1686: }
1687: } elsif ($umode eq 'localauth') {
1688: {
1689: my $pf = IO::File->new(">$passfilename");
1690: print $pf "localauth:$npass\n";
1691: }
1692: } elsif ($umode eq 'unix') {
1693: {
1694: my $execpath="$perlvar{'lonDaemons'}/"."lcuseradd";
1695: {
1696: &Debug("Executing external: ".$execpath);
1.98 ! foxr 1697: &Debug("user = ".$uname.", Password =". $npass);
! 1698: my $se = IO::File->new("|$execpath > /home/www/lcuseradd.log");
1.91 albertel 1699: print $se "$uname\n";
1700: print $se "$npass\n";
1701: print $se "$npass\n";
1.97 foxr 1702: }
1703: my $useraddok = $?;
1704: if($useraddok > 0) {
1705: &logthis("Failed lcuseradd: ".&lcuseraddstrerror($useraddok));
1.91 albertel 1706: }
1707: my $pf = IO::File->new(">$passfilename");
1708: print $pf "unix:\n";
1709: }
1710: } elsif ($umode eq 'none') {
1711: {
1712: my $pf = IO::File->new(">$passfilename");
1713: print $pf "none:\n";
1714: }
1715: } else {
1716: $result="auth_mode_error\n";
1717: }
1718: return $result;
1719: }
1720:
1.61 harris41 1721: # ----------------------------------- POD (plain old documentation, CPAN style)
1722:
1723: =head1 NAME
1724:
1725: lond - "LON Daemon" Server (port "LOND" 5663)
1726:
1727: =head1 SYNOPSIS
1728:
1.74 harris41 1729: Usage: B<lond>
1730:
1731: Should only be run as user=www. This is a command-line script which
1732: is invoked by B<loncron>. There is no expectation that a typical user
1733: will manually start B<lond> from the command-line. (In other words,
1734: DO NOT START B<lond> YOURSELF.)
1.61 harris41 1735:
1736: =head1 DESCRIPTION
1737:
1.74 harris41 1738: There are two characteristics associated with the running of B<lond>,
1739: PROCESS MANAGEMENT (starting, stopping, handling child processes)
1740: and SERVER-SIDE ACTIVITIES (password authentication, user creation,
1741: subscriptions, etc). These are described in two large
1742: sections below.
1743:
1744: B<PROCESS MANAGEMENT>
1745:
1.61 harris41 1746: Preforker - server who forks first. Runs as a daemon. HUPs.
1747: Uses IDEA encryption
1748:
1.74 harris41 1749: B<lond> forks off children processes that correspond to the other servers
1750: in the network. Management of these processes can be done at the
1751: parent process level or the child process level.
1752:
1753: B<logs/lond.log> is the location of log messages.
1754:
1755: The process management is now explained in terms of linux shell commands,
1756: subroutines internal to this code, and signal assignments:
1757:
1758: =over 4
1759:
1760: =item *
1761:
1762: PID is stored in B<logs/lond.pid>
1763:
1764: This is the process id number of the parent B<lond> process.
1765:
1766: =item *
1767:
1768: SIGTERM and SIGINT
1769:
1770: Parent signal assignment:
1771: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
1772:
1773: Child signal assignment:
1774: $SIG{INT} = 'DEFAULT'; (and SIGTERM is DEFAULT also)
1775: (The child dies and a SIGALRM is sent to parent, awaking parent from slumber
1776: to restart a new child.)
1777:
1778: Command-line invocations:
1779: B<kill> B<-s> SIGTERM I<PID>
1780: B<kill> B<-s> SIGINT I<PID>
1781:
1782: Subroutine B<HUNTSMAN>:
1783: This is only invoked for the B<lond> parent I<PID>.
1784: This kills all the children, and then the parent.
1785: The B<lonc.pid> file is cleared.
1786:
1787: =item *
1788:
1789: SIGHUP
1790:
1791: Current bug:
1792: This signal can only be processed the first time
1793: on the parent process. Subsequent SIGHUP signals
1794: have no effect.
1795:
1796: Parent signal assignment:
1797: $SIG{HUP} = \&HUPSMAN;
1798:
1799: Child signal assignment:
1800: none (nothing happens)
1801:
1802: Command-line invocations:
1803: B<kill> B<-s> SIGHUP I<PID>
1804:
1805: Subroutine B<HUPSMAN>:
1806: This is only invoked for the B<lond> parent I<PID>,
1807: This kills all the children, and then the parent.
1808: The B<lond.pid> file is cleared.
1809:
1810: =item *
1811:
1812: SIGUSR1
1813:
1814: Parent signal assignment:
1815: $SIG{USR1} = \&USRMAN;
1816:
1817: Child signal assignment:
1818: $SIG{USR1}= \&logstatus;
1819:
1820: Command-line invocations:
1821: B<kill> B<-s> SIGUSR1 I<PID>
1822:
1823: Subroutine B<USRMAN>:
1824: When invoked for the B<lond> parent I<PID>,
1825: SIGUSR1 is sent to all the children, and the status of
1826: each connection is logged.
1827:
1828: =item *
1829:
1830: SIGCHLD
1831:
1832: Parent signal assignment:
1833: $SIG{CHLD} = \&REAPER;
1834:
1835: Child signal assignment:
1836: none
1837:
1838: Command-line invocations:
1839: B<kill> B<-s> SIGCHLD I<PID>
1840:
1841: Subroutine B<REAPER>:
1842: This is only invoked for the B<lond> parent I<PID>.
1843: Information pertaining to the child is removed.
1844: The socket port is cleaned up.
1845:
1846: =back
1847:
1848: B<SERVER-SIDE ACTIVITIES>
1849:
1850: Server-side information can be accepted in an encrypted or non-encrypted
1851: method.
1852:
1853: =over 4
1854:
1855: =item ping
1856:
1857: Query a client in the hosts.tab table; "Are you there?"
1858:
1859: =item pong
1860:
1861: Respond to a ping query.
1862:
1863: =item ekey
1864:
1865: Read in encrypted key, make cipher. Respond with a buildkey.
1866:
1867: =item load
1868:
1869: Respond with CPU load based on a computation upon /proc/loadavg.
1870:
1871: =item currentauth
1872:
1873: Reply with current authentication information (only over an
1874: encrypted channel).
1875:
1876: =item auth
1877:
1878: Only over an encrypted channel, reply as to whether a user's
1879: authentication information can be validated.
1880:
1881: =item passwd
1882:
1883: Allow for a password to be set.
1884:
1885: =item makeuser
1886:
1887: Make a user.
1888:
1889: =item passwd
1890:
1891: Allow for authentication mechanism and password to be changed.
1892:
1893: =item home
1.61 harris41 1894:
1.74 harris41 1895: Respond to a question "are you the home for a given user?"
1896:
1897: =item update
1898:
1899: Update contents of a subscribed resource.
1900:
1901: =item unsubscribe
1902:
1903: The server is unsubscribing from a resource.
1904:
1905: =item subscribe
1906:
1907: The server is subscribing to a resource.
1908:
1909: =item log
1910:
1911: Place in B<logs/lond.log>
1912:
1913: =item put
1914:
1915: stores hash in namespace
1916:
1917: =item rolesput
1918:
1919: put a role into a user's environment
1920:
1921: =item get
1922:
1923: returns hash with keys from array
1924: reference filled in from namespace
1925:
1926: =item eget
1927:
1928: returns hash with keys from array
1929: reference filled in from namesp (encrypts the return communication)
1930:
1931: =item rolesget
1932:
1933: get a role from a user's environment
1934:
1935: =item del
1936:
1937: deletes keys out of array from namespace
1938:
1939: =item keys
1940:
1941: returns namespace keys
1942:
1943: =item dump
1944:
1945: dumps the complete (or key matching regexp) namespace into a hash
1946:
1947: =item store
1948:
1949: stores hash permanently
1950: for this url; hashref needs to be given and should be a \%hashname; the
1951: remaining args aren't required and if they aren't passed or are '' they will
1952: be derived from the ENV
1953:
1954: =item restore
1955:
1956: returns a hash for a given url
1957:
1958: =item querysend
1959:
1960: Tells client about the lonsql process that has been launched in response
1961: to a sent query.
1962:
1963: =item queryreply
1964:
1965: Accept information from lonsql and make appropriate storage in temporary
1966: file space.
1967:
1968: =item idput
1969:
1970: Defines usernames as corresponding to IDs. (These "IDs" are unique identifiers
1971: for each student, defined perhaps by the institutional Registrar.)
1972:
1973: =item idget
1974:
1975: Returns usernames corresponding to IDs. (These "IDs" are unique identifiers
1976: for each student, defined perhaps by the institutional Registrar.)
1977:
1978: =item tmpput
1979:
1980: Accept and store information in temporary space.
1981:
1982: =item tmpget
1983:
1984: Send along temporarily stored information.
1985:
1986: =item ls
1987:
1988: List part of a user's directory.
1989:
1990: =item Hanging up (exit or init)
1991:
1992: What to do when a client tells the server that they (the client)
1993: are leaving the network.
1994:
1995: =item unknown command
1996:
1997: If B<lond> is sent an unknown command (not in the list above),
1998: it replys to the client "unknown_cmd".
1999:
2000: =item UNKNOWN CLIENT
2001:
2002: If the anti-spoofing algorithm cannot verify the client,
2003: the client is rejected (with a "refused" message sent
2004: to the client, and the connection is closed.
2005:
2006: =back
1.61 harris41 2007:
2008: =head1 PREREQUISITES
2009:
2010: IO::Socket
2011: IO::File
2012: Apache::File
2013: Symbol
2014: POSIX
2015: Crypt::IDEA
2016: LWP::UserAgent()
2017: GDBM_File
2018: Authen::Krb4
1.91 albertel 2019: Authen::Krb5
1.61 harris41 2020:
2021: =head1 COREQUISITES
2022:
2023: =head1 OSNAMES
2024:
2025: linux
2026:
2027: =head1 SCRIPT CATEGORIES
2028:
2029: Server/Process
2030:
2031: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>