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