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