Annotation of loncom/lond, revision 1.78
1.1 albertel 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
1.60 www 4: #
1.78 ! foxr 5: # $Id: lond,v 1.77 2002/04/27 13:10:47 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.57 www 552: &status('Processing '.$hostid{$clientip}.': '.$userinput);
1.1 albertel 553: my $wasenc=0;
1.63 www 554: alarm(120);
1.1 albertel 555: # ------------------------------------------------------------ See if encrypted
556: if ($userinput =~ /^enc/) {
557: if ($cipher) {
558: my ($cmd,$cmdlength,$encinput)=split(/:/,$userinput);
559: $userinput='';
560: for (my $encidx=0;$encidx<length($encinput);$encidx+=16) {
561: $userinput.=
562: $cipher->decrypt(
563: pack("H16",substr($encinput,$encidx,16))
564: );
565: }
566: $userinput=substr($userinput,0,$cmdlength);
567: $wasenc=1;
568: }
1.75 foxr 569: }
570:
1.1 albertel 571: # ------------------------------------------------------------- Normal commands
572: # ------------------------------------------------------------------------ ping
573: if ($userinput =~ /^ping/) {
574: print $client "$perlvar{'lonHostID'}\n";
575: # ------------------------------------------------------------------------ pong
576: } elsif ($userinput =~ /^pong/) {
577: $reply=reply("ping",$hostid{$clientip});
578: print $client "$perlvar{'lonHostID'}:$reply\n";
579: # ------------------------------------------------------------------------ ekey
580: } elsif ($userinput =~ /^ekey/) {
581: my $buildkey=time.$$.int(rand 100000);
582: $buildkey=~tr/1-6/A-F/;
583: $buildkey=int(rand 100000).$buildkey.int(rand 100000);
584: my $key=$perlvar{'lonHostID'}.$hostid{$clientip};
585: $key=~tr/a-z/A-Z/;
586: $key=~tr/G-P/0-9/;
587: $key=~tr/Q-Z/0-9/;
588: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
589: $key=substr($key,0,32);
590: my $cipherkey=pack("H32",$key);
591: $cipher=new IDEA $cipherkey;
592: print $client "$buildkey\n";
593: # ------------------------------------------------------------------------ load
594: } elsif ($userinput =~ /^load/) {
595: my $loadavg;
596: {
597: my $loadfile=IO::File->new('/proc/loadavg');
598: $loadavg=<$loadfile>;
599: }
600: $loadavg =~ s/\s.*//g;
601: my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
602: print $client "$loadpercent\n";
1.54 harris41 603: # ----------------------------------------------------------------- currentauth
604: } elsif ($userinput =~ /^currentauth/) {
605: if ($wasenc==1) {
606: my ($cmd,$udom,$uname)=split(/:/,$userinput);
1.78 ! foxr 607: my $result = GetAuthType($udom, $user);
! 608: if($result eq "nouser") {
! 609: print $client "unknown_user\n";
! 610: }
! 611: else {
! 612: print $client "$result\n"
! 613: }
1.54 harris41 614: } else {
615: print $client "refused\n";
616: }
1.1 albertel 617: # ------------------------------------------------------------------------ auth
618: } elsif ($userinput =~ /^auth/) {
619: if ($wasenc==1) {
620: my ($cmd,$udom,$uname,$upass)=split(/:/,$userinput);
621: chomp($upass);
1.11 www 622: $upass=unescape($upass);
1.1 albertel 623: my $proname=propath($udom,$uname);
624: my $passfilename="$proname/passwd";
625: if (-e $passfilename) {
626: my $pf = IO::File->new($passfilename);
627: my $realpasswd=<$pf>;
628: chomp($realpasswd);
1.2 www 629: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
630: my $pwdcorrect=0;
631: if ($howpwd eq 'internal') {
632: $pwdcorrect=
633: (crypt($upass,$contentpwd) eq $contentpwd);
634: } elsif ($howpwd eq 'unix') {
635: $contentpwd=(getpwnam($uname))[1];
1.52 harris41 636: my $pwauth_path="/usr/local/sbin/pwauth";
637: unless ($contentpwd eq 'x') {
638: $pwdcorrect=
639: (crypt($upass,$contentpwd) eq $contentpwd);
640: }
641: elsif (-e $pwauth_path) {
642: open PWAUTH, "|$pwauth_path" or
643: die "Cannot invoke authentication";
644: print PWAUTH "$uname\n$upass\n";
645: close PWAUTH;
646: $pwdcorrect=!$?;
647: }
1.3 www 648: } elsif ($howpwd eq 'krb4') {
1.71 www 649: $null=pack("C",0);
650: unless ($upass=~/$null/) {
1.3 www 651: $pwdcorrect=(
652: Authen::Krb4::get_pw_in_tkt($uname,"",
653: $contentpwd,'krbtgt',$contentpwd,1,
654: $upass) == 0);
1.71 www 655: } else { $pwdcorrect=0; }
1.50 albertel 656: } elsif ($howpwd eq 'localauth') {
1.49 albertel 657: $pwdcorrect=&localauth::localauth($uname,$upass,
658: $contentpwd);
659: }
1.2 www 660: if ($pwdcorrect) {
1.1 albertel 661: print $client "authorized\n";
662: } else {
663: print $client "non_authorized\n";
664: }
665: } else {
666: print $client "unknown_user\n";
667: }
668: } else {
669: print $client "refused\n";
670: }
671: # ---------------------------------------------------------------------- passwd
672: } elsif ($userinput =~ /^passwd/) {
673: if ($wasenc==1) {
674: my
675: ($cmd,$udom,$uname,$upass,$npass)=split(/:/,$userinput);
676: chomp($npass);
1.32 www 677: $upass=&unescape($upass);
678: $npass=&unescape($npass);
1.72 matthew 679: &logthis("Trying to change password for $uname");
680: my $proname=propath($udom,$uname);
1.1 albertel 681: my $passfilename="$proname/passwd";
682: if (-e $passfilename) {
683: my $realpasswd;
684: { my $pf = IO::File->new($passfilename);
685: $realpasswd=<$pf>; }
686: chomp($realpasswd);
1.2 www 687: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
688: if ($howpwd eq 'internal') {
689: if (crypt($upass,$contentpwd) eq $contentpwd) {
690: my $salt=time;
691: $salt=substr($salt,6,2);
692: my $ncpass=crypt($npass,$salt);
1.1 albertel 693: { my $pf = IO::File->new(">$passfilename");
1.31 www 694: print $pf "internal:$ncpass\n"; }
1.72 matthew 695: &logthis("Result of password change for $uname: pwchange_success");
1.1 albertel 696: print $client "ok\n";
1.2 www 697: } else {
698: print $client "non_authorized\n";
699: }
1.72 matthew 700: } elsif ($howpwd eq 'unix') {
701: # Unix means we have to access /etc/password
702: # one way or another.
703: # First: Make sure the current password is
704: # correct
705: $contentpwd=(getpwnam($uname))[1];
706: my $pwdcorrect = "0";
707: my $pwauth_path="/usr/local/sbin/pwauth";
708: unless ($contentpwd eq 'x') {
709: $pwdcorrect=
710: (crypt($upass,$contentpwd) eq $contentpwd);
711: } elsif (-e $pwauth_path) {
712: open PWAUTH, "|$pwauth_path" or
713: die "Cannot invoke authentication";
714: print PWAUTH "$uname\n$upass\n";
715: close PWAUTH;
716: $pwdcorrect=!$?;
717: }
718: if ($pwdcorrect) {
719: my $execdir=$perlvar{'lonDaemons'};
720: my $pf = IO::File->new("|$execdir/lcpasswd");
721: print $pf "$uname\n$npass\n$npass\n";
722: close $pf;
723: my $result = ($?>0 ? 'pwchange_failure'
724: : 'ok');
725: &logthis("Result of password change for $uname: $result");
726: print $client "$result\n";
727: } else {
728: print $client "non_authorized\n";
729: }
730: } else {
1.2 www 731: print $client "auth_mode_error\n";
1.1 albertel 732: }
733: } else {
734: print $client "unknown_user\n";
1.31 www 735: }
736: } else {
737: print $client "refused\n";
738: }
739: # -------------------------------------------------------------------- makeuser
740: } elsif ($userinput =~ /^makeuser/) {
1.77 foxr 741: Debug("Make user received");
1.56 harris41 742: my $oldumask=umask(0077);
1.31 www 743: if ($wasenc==1) {
744: my
745: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
1.77 foxr 746: &Debug("cmd =".$cmd." $udom =".$udom.
747: " uname=".$uname);
1.31 www 748: chomp($npass);
1.32 www 749: $npass=&unescape($npass);
1.31 www 750: my $proname=propath($udom,$uname);
751: my $passfilename="$proname/passwd";
1.77 foxr 752: &Debug("Password file created will be:".
753: $passfilename);
1.31 www 754: if (-e $passfilename) {
755: print $client "already_exists\n";
756: } elsif ($udom ne $perlvar{'lonDefDomain'}) {
757: print $client "not_right_domain\n";
758: } else {
759: @fpparts=split(/\//,$proname);
760: $fpnow=$fpparts[0].'/'.$fpparts[1].'/'.$fpparts[2];
761: $fperror='';
762: for ($i=3;$i<=$#fpparts;$i++) {
763: $fpnow.='/'.$fpparts[$i];
764: unless (-e $fpnow) {
765: unless (mkdir($fpnow,0777)) {
1.65 www 766: $fperror="error:$!";
1.31 www 767: }
768: }
769: }
770: unless ($fperror) {
1.34 www 771: if ($umode eq 'krb4') {
1.31 www 772: {
773: my $pf = IO::File->new(">$passfilename");
1.33 www 774: print $pf "krb4:$npass\n";
1.31 www 775: }
776: print $client "ok\n";
777: } elsif ($umode eq 'internal') {
778: my $salt=time;
779: $salt=substr($salt,6,2);
780: my $ncpass=crypt($npass,$salt);
781: {
1.77 foxr 782: &Debug("Creating internal auth");
783: my $pf = IO::File->new(">$passfilename");
1.31 www 784: print $pf "internal:$ncpass\n";
1.50 albertel 785: }
1.31 www 786: print $client "ok\n";
1.50 albertel 787: } elsif ($umode eq 'localauth') {
788: {
789: my $pf = IO::File->new(">$passfilename");
790: print $pf "localauth:$npass\n";
791: }
792: print $client "ok\n";
1.53 harris41 793: } elsif ($umode eq 'unix') {
794: {
795: my $execpath="$perlvar{'lonDaemons'}/".
796: "lcuseradd";
1.54 harris41 797: {
1.77 foxr 798: &Debug("Executing external: ".
799: $execpath);
1.54 harris41 800: my $se = IO::File->new("|$execpath");
801: print $se "$uname\n";
802: print $se "$npass\n";
803: print $se "$npass\n";
804: }
1.53 harris41 805: my $pf = IO::File->new(">$passfilename");
806: print $pf "unix:\n";
807: }
1.54 harris41 808: print $client "ok\n";
1.53 harris41 809: } elsif ($umode eq 'none') {
1.31 www 810: {
811: my $pf = IO::File->new(">$passfilename");
812: print $pf "none:\n";
813: }
814: print $client "ok\n";
815: } else {
816: print $client "auth_mode_error\n";
817: }
818: } else {
819: print $client "$fperror\n";
820: }
1.55 harris41 821: }
822: } else {
823: print $client "refused\n";
824: }
1.56 harris41 825: umask($oldumask);
1.55 harris41 826: # -------------------------------------------------------------- changeuserauth
827: } elsif ($userinput =~ /^changeuserauth/) {
1.77 foxr 828: &Debug("Changing authorization");
829: if ($wasenc==1) {
1.55 harris41 830: my
831: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
832: chomp($npass);
1.77 foxr 833: &Debug("cmd = ".$cmd." domain= ".$udom.
834: "uname =".$uname." umode= ".$umode);
1.55 harris41 835: $npass=&unescape($npass);
836: my $proname=propath($udom,$uname);
837: my $passfilename="$proname/passwd";
838: if ($udom ne $perlvar{'lonDefDomain'}) {
839: print $client "not_right_domain\n";
840: } else {
841: if ($umode eq 'krb4') {
842: {
843: my $pf = IO::File->new(">$passfilename");
844: print $pf "krb4:$npass\n";
845: }
846: print $client "ok\n";
847: } elsif ($umode eq 'internal') {
848: my $salt=time;
849: $salt=substr($salt,6,2);
850: my $ncpass=crypt($npass,$salt);
851: {
852: my $pf = IO::File->new(">$passfilename");
853: print $pf "internal:$ncpass\n";
854: }
855: print $client "ok\n";
856: } elsif ($umode eq 'localauth') {
857: {
858: my $pf = IO::File->new(">$passfilename");
859: print $pf "localauth:$npass\n";
860: }
861: print $client "ok\n";
862: } elsif ($umode eq 'unix') {
863: {
864: my $execpath="$perlvar{'lonDaemons'}/".
865: "lcuseradd";
866: {
867: my $se = IO::File->new("|$execpath");
868: print $se "$uname\n";
869: print $se "$npass\n";
870: print $se "$npass\n";
871: }
872: my $pf = IO::File->new(">$passfilename");
873: print $pf "unix:\n";
874: }
875: print $client "ok\n";
876: } elsif ($umode eq 'none') {
877: {
878: my $pf = IO::File->new(">$passfilename");
879: print $pf "none:\n";
880: }
881: print $client "ok\n";
882: } else {
883: print $client "auth_mode_error\n";
884: }
1.1 albertel 885: }
886: } else {
887: print $client "refused\n";
888: }
889: # ------------------------------------------------------------------------ home
890: } elsif ($userinput =~ /^home/) {
891: my ($cmd,$udom,$uname)=split(/:/,$userinput);
892: chomp($uname);
893: my $proname=propath($udom,$uname);
894: if (-e $proname) {
895: print $client "found\n";
896: } else {
897: print $client "not_found\n";
898: }
899: # ---------------------------------------------------------------------- update
900: } elsif ($userinput =~ /^update/) {
901: my ($cmd,$fname)=split(/:/,$userinput);
902: my $ownership=ishome($fname);
903: if ($ownership eq 'not_owner') {
904: if (-e $fname) {
905: my ($dev,$ino,$mode,$nlink,
906: $uid,$gid,$rdev,$size,
907: $atime,$mtime,$ctime,
908: $blksize,$blocks)=stat($fname);
909: $now=time;
910: $since=$now-$atime;
911: if ($since>$perlvar{'lonExpire'}) {
912: $reply=
913: reply("unsub:$fname","$hostid{$clientip}");
914: unlink("$fname");
915: } else {
916: my $transname="$fname.in.transfer";
917: my $remoteurl=
918: reply("sub:$fname","$hostid{$clientip}");
919: my $response;
920: {
921: my $ua=new LWP::UserAgent;
922: my $request=new HTTP::Request('GET',"$remoteurl");
923: $response=$ua->request($request,$transname);
924: }
925: if ($response->is_error()) {
1.24 albertel 926: unlink($transname);
1.1 albertel 927: my $message=$response->status_line;
928: &logthis(
929: "LWP GET: $message for $fname ($remoteurl)");
930: } else {
1.14 www 931: if ($remoteurl!~/\.meta$/) {
1.28 www 932: my $ua=new LWP::UserAgent;
1.14 www 933: my $mrequest=
934: new HTTP::Request('GET',$remoteurl.'.meta');
935: my $mresponse=
936: $ua->request($mrequest,$fname.'.meta');
937: if ($mresponse->is_error()) {
938: unlink($fname.'.meta');
939: }
940: }
1.1 albertel 941: rename($transname,$fname);
942: }
943: }
944: print $client "ok\n";
945: } else {
946: print $client "not_found\n";
947: }
948: } else {
949: print $client "rejected\n";
950: }
951: # ----------------------------------------------------------------- unsubscribe
952: } elsif ($userinput =~ /^unsub/) {
953: my ($cmd,$fname)=split(/:/,$userinput);
954: if (-e $fname) {
955: if (unlink("$fname.$hostid{$clientip}")) {
956: print $client "ok\n";
957: } else {
958: print $client "not_subscribed\n";
959: }
960: } else {
961: print $client "not_found\n";
962: }
963: # ------------------------------------------------------------------- subscribe
964: } elsif ($userinput =~ /^sub/) {
965: my ($cmd,$fname)=split(/:/,$userinput);
966: my $ownership=ishome($fname);
967: if ($ownership eq 'owner') {
968: if (-e $fname) {
1.18 www 969: if (-d $fname) {
970: print $client "directory\n";
971: } else {
1.1 albertel 972: $now=time;
973: {
1.26 www 974: my $sh;
1.25 www 975: if ($sh=
976: IO::File->new(">$fname.$hostid{$clientip}")) {
977: print $sh "$clientip:$now\n";
978: }
1.1 albertel 979: }
1.42 www 980: unless ($fname=~/\.meta$/) {
981: unlink("$fname.meta.$hostid{$clientip}");
982: }
1.1 albertel 983: $fname=~s/\/home\/httpd\/html\/res/raw/;
984: $fname="http://$thisserver/".$fname;
985: print $client "$fname\n";
1.18 www 986: }
1.1 albertel 987: } else {
988: print $client "not_found\n";
989: }
990: } else {
991: print $client "rejected\n";
992: }
1.12 harris41 993: # ------------------------------------------------------------------------- log
994: } elsif ($userinput =~ /^log/) {
995: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
996: chomp($what);
997: my $proname=propath($udom,$uname);
998: my $now=time;
999: {
1000: my $hfh;
1001: if ($hfh=IO::File->new(">>$proname/activity.log")) {
1002: print $hfh "$now:$hostid{$clientip}:$what\n";
1003: print $client "ok\n";
1004: } else {
1005: print $client "error:$!\n";
1006: }
1007: }
1.1 albertel 1008: # ------------------------------------------------------------------------- put
1009: } elsif ($userinput =~ /^put/) {
1.6 www 1010: my ($cmd,$udom,$uname,$namespace,$what)
1.1 albertel 1011: =split(/:/,$userinput);
1.8 www 1012: $namespace=~s/\//\_/g;
1.6 www 1013: $namespace=~s/\W//g;
1014: if ($namespace ne 'roles') {
1.1 albertel 1015: chomp($what);
1016: my $proname=propath($udom,$uname);
1017: my $now=time;
1.48 www 1018: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1019: my $hfh;
1020: if (
1021: $hfh=IO::File->new(">>$proname/$namespace.hist")
1022: ) { print $hfh "P:$now:$what\n"; }
1023: }
1024: my @pairs=split(/\&/,$what);
1.4 www 1025: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1026: foreach $pair (@pairs) {
1027: ($key,$value)=split(/=/,$pair);
1028: $hash{$key}=$value;
1029: }
1.4 www 1030: if (untie(%hash)) {
1.1 albertel 1031: print $client "ok\n";
1032: } else {
1033: print $client "error:$!\n";
1034: }
1035: } else {
1036: print $client "error:$!\n";
1037: }
1.6 www 1038: } else {
1039: print $client "refused\n";
1040: }
1041: # -------------------------------------------------------------------- rolesput
1042: } elsif ($userinput =~ /^rolesput/) {
1.77 foxr 1043: &Debug("rolesput");
1.6 www 1044: if ($wasenc==1) {
1045: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
1046: =split(/:/,$userinput);
1.77 foxr 1047: &Debug("cmd = ".$cmd." exedom= ".$exedom.
1048: "user = ".$exeuser." udom=".$udom.
1049: "what = ".$what);
1.6 www 1050: my $namespace='roles';
1051: chomp($what);
1052: my $proname=propath($udom,$uname);
1053: my $now=time;
1054: {
1055: my $hfh;
1056: if (
1057: $hfh=IO::File->new(">>$proname/$namespace.hist")
1058: ) {
1059: print $hfh "P:$now:$exedom:$exeuser:$what\n";
1060: }
1061: }
1062: my @pairs=split(/\&/,$what);
1063: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1064: foreach $pair (@pairs) {
1065: ($key,$value)=split(/=/,$pair);
1.78 ! foxr 1066: &ManagePermissions($key, $udom, $uname,
! 1067: &GetAuthType( $udom,
! 1068: $uname));
1.6 www 1069: $hash{$key}=$value;
1.78 ! foxr 1070:
1.6 www 1071: }
1072: if (untie(%hash)) {
1073: print $client "ok\n";
1074: } else {
1075: print $client "error:$!\n";
1076: }
1077: } else {
1078: print $client "error:$!\n";
1079: }
1080: } else {
1081: print $client "refused\n";
1082: }
1.1 albertel 1083: # ------------------------------------------------------------------------- get
1084: } elsif ($userinput =~ /^get/) {
1085: my ($cmd,$udom,$uname,$namespace,$what)
1086: =split(/:/,$userinput);
1.8 www 1087: $namespace=~s/\//\_/g;
1.1 albertel 1088: $namespace=~s/\W//g;
1089: chomp($what);
1090: my @queries=split(/\&/,$what);
1091: my $proname=propath($udom,$uname);
1092: my $qresult='';
1.20 www 1093: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1094: for ($i=0;$i<=$#queries;$i++) {
1095: $qresult.="$hash{$queries[$i]}&";
1096: }
1.4 www 1097: if (untie(%hash)) {
1.1 albertel 1098: $qresult=~s/\&$//;
1099: print $client "$qresult\n";
1100: } else {
1101: print $client "error:$!\n";
1102: }
1103: } else {
1104: print $client "error:$!\n";
1105: }
1106: # ------------------------------------------------------------------------ eget
1107: } elsif ($userinput =~ /^eget/) {
1108: my ($cmd,$udom,$uname,$namespace,$what)
1109: =split(/:/,$userinput);
1.8 www 1110: $namespace=~s/\//\_/g;
1.1 albertel 1111: $namespace=~s/\W//g;
1112: chomp($what);
1113: my @queries=split(/\&/,$what);
1114: my $proname=propath($udom,$uname);
1115: my $qresult='';
1.20 www 1116: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1117: for ($i=0;$i<=$#queries;$i++) {
1118: $qresult.="$hash{$queries[$i]}&";
1119: }
1.4 www 1120: if (untie(%hash)) {
1.1 albertel 1121: $qresult=~s/\&$//;
1122: if ($cipher) {
1123: my $cmdlength=length($qresult);
1124: $qresult.=" ";
1125: my $encqresult='';
1126: for
1127: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
1128: $encqresult.=
1129: unpack("H16",
1130: $cipher->encrypt(substr($qresult,$encidx,8)));
1131: }
1132: print $client "enc:$cmdlength:$encqresult\n";
1133: } else {
1134: print $client "error:no_key\n";
1135: }
1136: } else {
1137: print $client "error:$!\n";
1138: }
1139: } else {
1140: print $client "error:$!\n";
1141: }
1142: # ------------------------------------------------------------------------- del
1143: } elsif ($userinput =~ /^del/) {
1144: my ($cmd,$udom,$uname,$namespace,$what)
1145: =split(/:/,$userinput);
1.8 www 1146: $namespace=~s/\//\_/g;
1.1 albertel 1147: $namespace=~s/\W//g;
1148: chomp($what);
1149: my $proname=propath($udom,$uname);
1150: my $now=time;
1.48 www 1151: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1152: my $hfh;
1153: if (
1154: $hfh=IO::File->new(">>$proname/$namespace.hist")
1155: ) { print $hfh "D:$now:$what\n"; }
1156: }
1157: my @keys=split(/\&/,$what);
1.4 www 1158: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1159: foreach $key (@keys) {
1160: delete($hash{$key});
1161: }
1.4 www 1162: if (untie(%hash)) {
1.1 albertel 1163: print $client "ok\n";
1164: } else {
1165: print $client "error:$!\n";
1166: }
1167: } else {
1168: print $client "error:$!\n";
1169: }
1170: # ------------------------------------------------------------------------ keys
1171: } elsif ($userinput =~ /^keys/) {
1172: my ($cmd,$udom,$uname,$namespace)
1173: =split(/:/,$userinput);
1.8 www 1174: $namespace=~s/\//\_/g;
1.1 albertel 1175: $namespace=~s/\W//g;
1176: my $proname=propath($udom,$uname);
1177: my $qresult='';
1.20 www 1178: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1179: foreach $key (keys %hash) {
1180: $qresult.="$key&";
1181: }
1.4 www 1182: if (untie(%hash)) {
1.1 albertel 1183: $qresult=~s/\&$//;
1184: print $client "$qresult\n";
1185: } else {
1186: print $client "error:$!\n";
1187: }
1188: } else {
1189: print $client "error:$!\n";
1190: }
1191: # ------------------------------------------------------------------------ dump
1192: } elsif ($userinput =~ /^dump/) {
1.62 www 1193: my ($cmd,$udom,$uname,$namespace,$regexp)
1.1 albertel 1194: =split(/:/,$userinput);
1.8 www 1195: $namespace=~s/\//\_/g;
1.1 albertel 1196: $namespace=~s/\W//g;
1.62 www 1197: if (defined($regexp)) {
1198: $regexp=&unescape($regexp);
1199: } else {
1200: $regexp='.';
1201: }
1.1 albertel 1202: my $proname=propath($udom,$uname);
1203: my $qresult='';
1.20 www 1204: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1205: foreach $key (keys %hash) {
1.62 www 1206: if (eval('$key=~/$regexp/')) {
1207: $qresult.="$key=$hash{$key}&";
1208: }
1.7 www 1209: }
1210: if (untie(%hash)) {
1211: $qresult=~s/\&$//;
1212: print $client "$qresult\n";
1213: } else {
1214: print $client "error:$!\n";
1215: }
1216: } else {
1217: print $client "error:$!\n";
1218: }
1219: # ----------------------------------------------------------------------- store
1220: } elsif ($userinput =~ /^store/) {
1221: my ($cmd,$udom,$uname,$namespace,$rid,$what)
1222: =split(/:/,$userinput);
1.8 www 1223: $namespace=~s/\//\_/g;
1.7 www 1224: $namespace=~s/\W//g;
1225: if ($namespace ne 'roles') {
1226: chomp($what);
1227: my $proname=propath($udom,$uname);
1228: my $now=time;
1.48 www 1229: unless ($namespace=~/^nohist\_/) {
1.7 www 1230: my $hfh;
1231: if (
1232: $hfh=IO::File->new(">>$proname/$namespace.hist")
1233: ) { print $hfh "P:$now:$rid:$what\n"; }
1234: }
1235: my @pairs=split(/\&/,$what);
1236:
1237: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1238: my @previouskeys=split(/&/,$hash{"keys:$rid"});
1239: my $key;
1240: $hash{"version:$rid"}++;
1241: my $version=$hash{"version:$rid"};
1242: my $allkeys='';
1243: foreach $pair (@pairs) {
1244: ($key,$value)=split(/=/,$pair);
1245: $allkeys.=$key.':';
1246: $hash{"$version:$rid:$key"}=$value;
1247: }
1.36 www 1248: $hash{"$version:$rid:timestamp"}=$now;
1249: $allkeys.='timestamp';
1.7 www 1250: $hash{"$version:keys:$rid"}=$allkeys;
1251: if (untie(%hash)) {
1252: print $client "ok\n";
1253: } else {
1254: print $client "error:$!\n";
1255: }
1256: } else {
1257: print $client "error:$!\n";
1258: }
1259: } else {
1260: print $client "refused\n";
1261: }
1262: # --------------------------------------------------------------------- restore
1263: } elsif ($userinput =~ /^restore/) {
1264: my ($cmd,$udom,$uname,$namespace,$rid)
1265: =split(/:/,$userinput);
1.8 www 1266: $namespace=~s/\//\_/g;
1.7 www 1267: $namespace=~s/\W//g;
1268: chomp($rid);
1269: my $proname=propath($udom,$uname);
1270: my $qresult='';
1.20 www 1271: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.7 www 1272: my $version=$hash{"version:$rid"};
1273: $qresult.="version=$version&";
1274: my $scope;
1275: for ($scope=1;$scope<=$version;$scope++) {
1276: my $vkeys=$hash{"$scope:keys:$rid"};
1277: my @keys=split(/:/,$vkeys);
1278: my $key;
1279: $qresult.="$scope:keys=$vkeys&";
1280: foreach $key (@keys) {
1.21 www 1281: $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
1.7 www 1282: }
1.1 albertel 1283: }
1.4 www 1284: if (untie(%hash)) {
1.1 albertel 1285: $qresult=~s/\&$//;
1286: print $client "$qresult\n";
1287: } else {
1288: print $client "error:$!\n";
1289: }
1290: } else {
1291: print $client "error:$!\n";
1292: }
1.12 harris41 1293: # ------------------------------------------------------------------- querysend
1294: } elsif ($userinput =~ /^querysend/) {
1.44 harris41 1295: my ($cmd,$query,
1296: $custom,$customshow)=split(/:/,$userinput);
1.12 harris41 1297: $query=~s/\n*$//g;
1.45 harris41 1298: unless ($custom or $customshow) {
1.40 harris41 1299: print $client "".
1300: sqlreply("$hostid{$clientip}\&$query")."\n";
1301: }
1302: else {
1303: print $client "".
1304: sqlreply("$hostid{$clientip}\&$query".
1.44 harris41 1305: "\&$custom"."\&$customshow")."\n";
1.40 harris41 1306: }
1.12 harris41 1307: # ------------------------------------------------------------------ queryreply
1308: } elsif ($userinput =~ /^queryreply/) {
1309: my ($cmd,$id,$reply)=split(/:/,$userinput);
1310: my $store;
1.13 www 1311: my $execdir=$perlvar{'lonDaemons'};
1312: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.43 harris41 1313: $reply=~s/\&/\n/g;
1.12 harris41 1314: print $store $reply;
1315: close $store;
1.46 harris41 1316: my $store2=IO::File->new(">$execdir/tmp/$id.end");
1317: print $store2 "done\n";
1318: close $store2;
1.12 harris41 1319: print $client "ok\n";
1320: }
1321: else {
1322: print $client "error:$!\n";
1323: }
1.1 albertel 1324: # ----------------------------------------------------------------------- idput
1325: } elsif ($userinput =~ /^idput/) {
1326: my ($cmd,$udom,$what)=split(/:/,$userinput);
1327: chomp($what);
1328: $udom=~s/\W//g;
1329: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1330: my $now=time;
1331: {
1332: my $hfh;
1333: if (
1334: $hfh=IO::File->new(">>$proname.hist")
1335: ) { print $hfh "P:$now:$what\n"; }
1336: }
1337: my @pairs=split(/\&/,$what);
1.4 www 1338: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1339: foreach $pair (@pairs) {
1340: ($key,$value)=split(/=/,$pair);
1341: $hash{$key}=$value;
1342: }
1.4 www 1343: if (untie(%hash)) {
1.1 albertel 1344: print $client "ok\n";
1345: } else {
1346: print $client "error:$!\n";
1347: }
1348: } else {
1349: print $client "error:$!\n";
1350: }
1351: # ----------------------------------------------------------------------- idget
1352: } elsif ($userinput =~ /^idget/) {
1353: my ($cmd,$udom,$what)=split(/:/,$userinput);
1354: chomp($what);
1355: $udom=~s/\W//g;
1356: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1357: my @queries=split(/\&/,$what);
1358: my $qresult='';
1.20 www 1359: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER,0640)) {
1.1 albertel 1360: for ($i=0;$i<=$#queries;$i++) {
1361: $qresult.="$hash{$queries[$i]}&";
1362: }
1.4 www 1363: if (untie(%hash)) {
1.1 albertel 1364: $qresult=~s/\&$//;
1365: print $client "$qresult\n";
1366: } else {
1367: print $client "error:$!\n";
1368: }
1369: } else {
1370: print $client "error:$!\n";
1371: }
1.13 www 1372: # ---------------------------------------------------------------------- tmpput
1373: } elsif ($userinput =~ /^tmpput/) {
1374: my ($cmd,$what)=split(/:/,$userinput);
1375: my $store;
1376: $tmpsnum++;
1377: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
1378: $id=~s/\W/\_/g;
1379: $what=~s/\n//g;
1380: my $execdir=$perlvar{'lonDaemons'};
1381: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1382: print $store $what;
1383: close $store;
1384: print $client "$id\n";
1385: }
1386: else {
1387: print $client "error:$!\n";
1388: }
1389:
1390: # ---------------------------------------------------------------------- tmpget
1391: } elsif ($userinput =~ /^tmpget/) {
1392: my ($cmd,$id)=split(/:/,$userinput);
1393: chomp($id);
1394: $id=~s/\W/\_/g;
1395: my $store;
1396: my $execdir=$perlvar{'lonDaemons'};
1397: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
1398: my $reply=<$store>;
1399: print $client "$reply\n";
1400: close $store;
1401: }
1402: else {
1403: print $client "error:$!\n";
1404: }
1405:
1.5 www 1406: # -------------------------------------------------------------------------- ls
1407: } elsif ($userinput =~ /^ls/) {
1408: my ($cmd,$ulsdir)=split(/:/,$userinput);
1409: my $ulsout='';
1410: my $ulsfn;
1411: if (-e $ulsdir) {
1.41 www 1412: if (opendir(LSDIR,$ulsdir)) {
1413: while ($ulsfn=readdir(LSDIR)) {
1.47 www 1414: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1.5 www 1415: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1416: }
1.41 www 1417: closedir(LSDIR);
1418: }
1.5 www 1419: } else {
1420: $ulsout='no_such_dir';
1421: }
1.17 www 1422: if ($ulsout eq '') { $ulsout='empty'; }
1.5 www 1423: print $client "$ulsout\n";
1.51 www 1424: # ------------------------------------------------------------------ Hanging up
1425: } elsif (($userinput =~ /^exit/) ||
1426: ($userinput =~ /^init/)) {
1427: &logthis(
1428: "Client $clientip ($hostid{$clientip}) hanging up: $userinput");
1429: print $client "bye\n";
1.59 www 1430: $client->close();
1.51 www 1431: last;
1.1 albertel 1432: # ------------------------------------------------------------- unknown command
1433: } else {
1434: # unknown command
1435: print $client "unknown_cmd\n";
1436: }
1.58 www 1437: # -------------------------------------------------------------------- complete
1.63 www 1438: alarm(0);
1.58 www 1439: &status('Listening to '.$hostid{$clientip});
1440: }
1.59 www 1441: # --------------------------------------------- client unknown or fishy, refuse
1.1 albertel 1442: } else {
1443: print $client "refused\n";
1.59 www 1444: $client->close();
1.9 www 1445: &logthis("<font color=blue>WARNING: "
1446: ."Rejected client $clientip, closing connection</font>");
1.75 foxr 1447: }
1448: }
1449:
1.1 albertel 1450: # =============================================================================
1.75 foxr 1451:
1452: &logthis("<font color=red>CRITICAL: "
1453: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.1 albertel 1454: # tidy up gracefully and finish
1455:
1.59 www 1456: $server->close();
1457:
1.1 albertel 1458: # this exit is VERY important, otherwise the child will become
1459: # a producer of more and more children, forking yourself into
1460: # process death.
1461: exit;
1462: }
1.78 ! foxr 1463: }
! 1464:
! 1465:
! 1466: #
! 1467: # Checks to see if the input roleput request was to set
! 1468: # an author role. If so, invokes the lchtmldir script to set
! 1469: # up a correct public_html
! 1470: # Parameters:
! 1471: # request - The request sent to the rolesput subchunk.
! 1472: # We're looking for /domain/_au
! 1473: # domain - The domain in which the user is having roles doctored.
! 1474: # user - Name of the user for which the role is being put.
! 1475: # authtype - The authentication type associated with the user.
! 1476: #
! 1477: sub ManagePermissions
! 1478: {
! 1479: my $request = shift;
! 1480: my $domain = shift;
! 1481: my $user = shift;
! 1482: my $authtype= shift;
! 1483:
! 1484: # See if the request is of the form /$domain/_au
! 1485:
! 1486: if($request =~ /^(\/$domain\/_au)$/) { # It's an author rolesput...
! 1487: my $execdir = $perlvar{'lonDaemons'};
! 1488: my $userhome= "/home/$user" ;
! 1489: Debug("system $execdir/lchtmldir $userhome $system $authtype");
! 1490: system("$execdir/lchtmldir $userhome $user $authtype");
! 1491: }
! 1492: }
! 1493: #
! 1494: # GetAuthType - Determines the authorization type of a user in a domain.
! 1495:
! 1496: # Returns the authorization type or nouser if there is no such user.
! 1497: #
! 1498: sub GetAuthType
! 1499: {
! 1500: my $domain = shift;
! 1501: my $user = shift;
! 1502:
! 1503: my $proname = &propath($domain, $user);
! 1504: my $passwdfile = "$proname/passwd";
! 1505: if( -e $passwdfile ) {
! 1506: my $pf = IO::File->new($passwdfile);
! 1507: my $realpassword = <$pf>;
! 1508: chomp($realpassword);
! 1509: my ($authtype, $contentpwd) = split(/:/, $realpassword);
! 1510: my $availinfo = '';
! 1511: if($authtype eq 'krb4') {
! 1512: $availinfo = $contentpwd;
! 1513: }
! 1514: return "$authtype:$availinfo";
! 1515: }
! 1516: else {
! 1517: return "nouser";
! 1518: }
! 1519:
1.1 albertel 1520: }
1521:
1.61 harris41 1522: # ----------------------------------- POD (plain old documentation, CPAN style)
1523:
1524: =head1 NAME
1525:
1526: lond - "LON Daemon" Server (port "LOND" 5663)
1527:
1528: =head1 SYNOPSIS
1529:
1.74 harris41 1530: Usage: B<lond>
1531:
1532: Should only be run as user=www. This is a command-line script which
1533: is invoked by B<loncron>. There is no expectation that a typical user
1534: will manually start B<lond> from the command-line. (In other words,
1535: DO NOT START B<lond> YOURSELF.)
1.61 harris41 1536:
1537: =head1 DESCRIPTION
1538:
1.74 harris41 1539: There are two characteristics associated with the running of B<lond>,
1540: PROCESS MANAGEMENT (starting, stopping, handling child processes)
1541: and SERVER-SIDE ACTIVITIES (password authentication, user creation,
1542: subscriptions, etc). These are described in two large
1543: sections below.
1544:
1545: B<PROCESS MANAGEMENT>
1546:
1.61 harris41 1547: Preforker - server who forks first. Runs as a daemon. HUPs.
1548: Uses IDEA encryption
1549:
1.74 harris41 1550: B<lond> forks off children processes that correspond to the other servers
1551: in the network. Management of these processes can be done at the
1552: parent process level or the child process level.
1553:
1554: B<logs/lond.log> is the location of log messages.
1555:
1556: The process management is now explained in terms of linux shell commands,
1557: subroutines internal to this code, and signal assignments:
1558:
1559: =over 4
1560:
1561: =item *
1562:
1563: PID is stored in B<logs/lond.pid>
1564:
1565: This is the process id number of the parent B<lond> process.
1566:
1567: =item *
1568:
1569: SIGTERM and SIGINT
1570:
1571: Parent signal assignment:
1572: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
1573:
1574: Child signal assignment:
1575: $SIG{INT} = 'DEFAULT'; (and SIGTERM is DEFAULT also)
1576: (The child dies and a SIGALRM is sent to parent, awaking parent from slumber
1577: to restart a new child.)
1578:
1579: Command-line invocations:
1580: B<kill> B<-s> SIGTERM I<PID>
1581: B<kill> B<-s> SIGINT I<PID>
1582:
1583: Subroutine B<HUNTSMAN>:
1584: This is only invoked for the B<lond> parent I<PID>.
1585: This kills all the children, and then the parent.
1586: The B<lonc.pid> file is cleared.
1587:
1588: =item *
1589:
1590: SIGHUP
1591:
1592: Current bug:
1593: This signal can only be processed the first time
1594: on the parent process. Subsequent SIGHUP signals
1595: have no effect.
1596:
1597: Parent signal assignment:
1598: $SIG{HUP} = \&HUPSMAN;
1599:
1600: Child signal assignment:
1601: none (nothing happens)
1602:
1603: Command-line invocations:
1604: B<kill> B<-s> SIGHUP I<PID>
1605:
1606: Subroutine B<HUPSMAN>:
1607: This is only invoked for the B<lond> parent I<PID>,
1608: This kills all the children, and then the parent.
1609: The B<lond.pid> file is cleared.
1610:
1611: =item *
1612:
1613: SIGUSR1
1614:
1615: Parent signal assignment:
1616: $SIG{USR1} = \&USRMAN;
1617:
1618: Child signal assignment:
1619: $SIG{USR1}= \&logstatus;
1620:
1621: Command-line invocations:
1622: B<kill> B<-s> SIGUSR1 I<PID>
1623:
1624: Subroutine B<USRMAN>:
1625: When invoked for the B<lond> parent I<PID>,
1626: SIGUSR1 is sent to all the children, and the status of
1627: each connection is logged.
1628:
1629: =item *
1630:
1631: SIGCHLD
1632:
1633: Parent signal assignment:
1634: $SIG{CHLD} = \&REAPER;
1635:
1636: Child signal assignment:
1637: none
1638:
1639: Command-line invocations:
1640: B<kill> B<-s> SIGCHLD I<PID>
1641:
1642: Subroutine B<REAPER>:
1643: This is only invoked for the B<lond> parent I<PID>.
1644: Information pertaining to the child is removed.
1645: The socket port is cleaned up.
1646:
1647: =back
1648:
1649: B<SERVER-SIDE ACTIVITIES>
1650:
1651: Server-side information can be accepted in an encrypted or non-encrypted
1652: method.
1653:
1654: =over 4
1655:
1656: =item ping
1657:
1658: Query a client in the hosts.tab table; "Are you there?"
1659:
1660: =item pong
1661:
1662: Respond to a ping query.
1663:
1664: =item ekey
1665:
1666: Read in encrypted key, make cipher. Respond with a buildkey.
1667:
1668: =item load
1669:
1670: Respond with CPU load based on a computation upon /proc/loadavg.
1671:
1672: =item currentauth
1673:
1674: Reply with current authentication information (only over an
1675: encrypted channel).
1676:
1677: =item auth
1678:
1679: Only over an encrypted channel, reply as to whether a user's
1680: authentication information can be validated.
1681:
1682: =item passwd
1683:
1684: Allow for a password to be set.
1685:
1686: =item makeuser
1687:
1688: Make a user.
1689:
1690: =item passwd
1691:
1692: Allow for authentication mechanism and password to be changed.
1693:
1694: =item home
1.61 harris41 1695:
1.74 harris41 1696: Respond to a question "are you the home for a given user?"
1697:
1698: =item update
1699:
1700: Update contents of a subscribed resource.
1701:
1702: =item unsubscribe
1703:
1704: The server is unsubscribing from a resource.
1705:
1706: =item subscribe
1707:
1708: The server is subscribing to a resource.
1709:
1710: =item log
1711:
1712: Place in B<logs/lond.log>
1713:
1714: =item put
1715:
1716: stores hash in namespace
1717:
1718: =item rolesput
1719:
1720: put a role into a user's environment
1721:
1722: =item get
1723:
1724: returns hash with keys from array
1725: reference filled in from namespace
1726:
1727: =item eget
1728:
1729: returns hash with keys from array
1730: reference filled in from namesp (encrypts the return communication)
1731:
1732: =item rolesget
1733:
1734: get a role from a user's environment
1735:
1736: =item del
1737:
1738: deletes keys out of array from namespace
1739:
1740: =item keys
1741:
1742: returns namespace keys
1743:
1744: =item dump
1745:
1746: dumps the complete (or key matching regexp) namespace into a hash
1747:
1748: =item store
1749:
1750: stores hash permanently
1751: for this url; hashref needs to be given and should be a \%hashname; the
1752: remaining args aren't required and if they aren't passed or are '' they will
1753: be derived from the ENV
1754:
1755: =item restore
1756:
1757: returns a hash for a given url
1758:
1759: =item querysend
1760:
1761: Tells client about the lonsql process that has been launched in response
1762: to a sent query.
1763:
1764: =item queryreply
1765:
1766: Accept information from lonsql and make appropriate storage in temporary
1767: file space.
1768:
1769: =item idput
1770:
1771: Defines usernames as corresponding to IDs. (These "IDs" are unique identifiers
1772: for each student, defined perhaps by the institutional Registrar.)
1773:
1774: =item idget
1775:
1776: Returns usernames corresponding to IDs. (These "IDs" are unique identifiers
1777: for each student, defined perhaps by the institutional Registrar.)
1778:
1779: =item tmpput
1780:
1781: Accept and store information in temporary space.
1782:
1783: =item tmpget
1784:
1785: Send along temporarily stored information.
1786:
1787: =item ls
1788:
1789: List part of a user's directory.
1790:
1791: =item Hanging up (exit or init)
1792:
1793: What to do when a client tells the server that they (the client)
1794: are leaving the network.
1795:
1796: =item unknown command
1797:
1798: If B<lond> is sent an unknown command (not in the list above),
1799: it replys to the client "unknown_cmd".
1800:
1801: =item UNKNOWN CLIENT
1802:
1803: If the anti-spoofing algorithm cannot verify the client,
1804: the client is rejected (with a "refused" message sent
1805: to the client, and the connection is closed.
1806:
1807: =back
1.61 harris41 1808:
1809: =head1 PREREQUISITES
1810:
1811: IO::Socket
1812: IO::File
1813: Apache::File
1814: Symbol
1815: POSIX
1816: Crypt::IDEA
1817: LWP::UserAgent()
1818: GDBM_File
1819: Authen::Krb4
1820:
1821: =head1 COREQUISITES
1822:
1823: =head1 OSNAMES
1824:
1825: linux
1826:
1827: =head1 SCRIPT CATEGORIES
1828:
1829: Server/Process
1830:
1831: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>