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