Annotation of loncom/lond, revision 1.66
1.1 albertel 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
1.60 www 4: #
1.63 www 5: # $Id: lond,v 1.62 2001/12/22 21:46:02 www 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.54 harris41 51: ###
52:
1.1 albertel 53: # based on "Perl Cookbook" ISBN 1-56592-243-3
54: # preforker - server who forks first
55: # runs as a daemon
56: # HUPs
57: # uses IDEA encryption
58:
59: use IO::Socket;
60: use IO::File;
61: use Apache::File;
62: use Symbol;
63: use POSIX;
64: use Crypt::IDEA;
65: use LWP::UserAgent();
1.3 www 66: use GDBM_File;
67: use Authen::Krb4;
1.49 albertel 68: use lib '/home/httpd/lib/perl/';
69: use localauth;
1.1 albertel 70:
1.57 www 71: my $status='';
72: my $lastlog='';
73:
1.23 harris41 74: # grabs exception and records it to log before exiting
75: sub catchexception {
1.27 albertel 76: my ($error)=@_;
1.25 www 77: $SIG{'QUIT'}='DEFAULT';
78: $SIG{__DIE__}='DEFAULT';
1.23 harris41 79: &logthis("<font color=red>CRITICAL: "
80: ."ABNORMAL EXIT. Child $$ for server $wasserver died through "
1.27 albertel 81: ."a crash with this error msg->[$error]</font>");
1.57 www 82: &logthis('Famous last words: '.$status.' - '.$lastlog);
1.27 albertel 83: if ($client) { print $client "error: $error\n"; }
1.59 www 84: $server->close();
1.27 albertel 85: die($error);
1.23 harris41 86: }
87:
1.63 www 88: sub timeout {
89: &logthis("<font color=ref>CRITICAL: TIME OUT ".$$."</font>");
90: &catchexception('Timeout');
91: }
1.22 harris41 92: # -------------------------------- Set signal handlers to record abnormal exits
93:
94: $SIG{'QUIT'}=\&catchexception;
95: $SIG{__DIE__}=\&catchexception;
96:
1.1 albertel 97: # ------------------------------------ Read httpd access.conf and get variables
98:
1.29 harris41 99: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
1.1 albertel 100:
101: while ($configline=<CONFIG>) {
102: if ($configline =~ /PerlSetVar/) {
103: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
1.7 www 104: chomp($varvalue);
1.1 albertel 105: $perlvar{$varname}=$varvalue;
106: }
107: }
108: close(CONFIG);
1.19 www 109:
1.35 harris41 110: # ----------------------------- Make sure this process is running from user=www
111: my $wwwid=getpwnam('www');
112: if ($wwwid!=$<) {
113: $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
114: $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
1.37 harris41 115: system("echo 'User ID mismatch. lond must be run as user www.' |\
1.35 harris41 116: mailto $emailto -s '$subj' > /dev/null");
117: exit 1;
118: }
119:
1.19 www 120: # --------------------------------------------- Check if other instance running
121:
122: my $pidfile="$perlvar{'lonDaemons'}/logs/lond.pid";
123:
124: if (-e $pidfile) {
125: my $lfh=IO::File->new("$pidfile");
126: my $pide=<$lfh>;
127: chomp($pide);
1.29 harris41 128: if (kill 0 => $pide) { die "already running"; }
1.19 www 129: }
1.1 albertel 130:
131: $PREFORK=4; # number of children to maintain, at least four spare
132:
133: # ------------------------------------------------------------- Read hosts file
134:
1.29 harris41 135: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
1.1 albertel 136:
137: while ($configline=<CONFIG>) {
138: my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
139: chomp($ip);
140: $hostid{$ip}=$id;
141: if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
142: $PREFORK++;
143: }
144: close(CONFIG);
145:
146: # establish SERVER socket, bind and listen.
147: $server = IO::Socket::INET->new(LocalPort => $perlvar{'londPort'},
148: Type => SOCK_STREAM,
149: Proto => 'tcp',
150: Reuse => 1,
151: Listen => 10 )
1.29 harris41 152: or die "making socket: $@\n";
1.1 albertel 153:
154: # --------------------------------------------------------- Do global variables
155:
156: # global variables
157:
158: $MAX_CLIENTS_PER_CHILD = 5; # number of clients each child should
159: # process
160: %children = (); # keys are current child process IDs
161: $children = 0; # current number of children
162:
163: sub REAPER { # takes care of dead children
164: $SIG{CHLD} = \&REAPER;
165: my $pid = wait;
166: $children --;
167: &logthis("Child $pid died");
168: delete $children{$pid};
169: }
170:
171: sub HUNTSMAN { # signal handler for SIGINT
172: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
173: kill 'INT' => keys %children;
1.59 www 174: &logthis("Free socket: ".shutdown($server,2)); # free up socket
1.1 albertel 175: my $execdir=$perlvar{'lonDaemons'};
176: unlink("$execdir/logs/lond.pid");
1.9 www 177: &logthis("<font color=red>CRITICAL: Shutting down</font>");
1.1 albertel 178: exit; # clean up with dignity
179: }
180:
181: sub HUPSMAN { # signal handler for SIGHUP
182: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
183: kill 'INT' => keys %children;
1.59 www 184: &logthis("Free socket: ".shutdown($server,2)); # free up socket
1.9 www 185: &logthis("<font color=red>CRITICAL: Restarting</font>");
1.30 harris41 186: unlink("$execdir/logs/lond.pid");
1.1 albertel 187: my $execdir=$perlvar{'lonDaemons'};
188: exec("$execdir/lond"); # here we go again
189: }
190:
1.57 www 191: sub checkchildren {
192: &initnewstatus();
193: &logstatus();
194: &logthis('Going to check on the children');
1.63 www 195: $docdir=$perlvar{'lonDocRoot'};
1.61 harris41 196: foreach (sort keys %children) {
1.57 www 197: sleep 1;
198: unless (kill 'USR1' => $_) {
199: &logthis ('Child '.$_.' is dead');
200: &logstatus($$.' is dead');
201: }
1.61 harris41 202: }
1.63 www 203: sleep 5;
204: foreach (sort keys %children) {
205: unless (-e "$docdir/lon-status/londchld/$_.txt") {
206: &logthis('Child '.$_.' did not respond');
1.64 www 207: kill 9 => $_;
1.66 ! www 208: $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
! 209: $subj="LON: $perlvar{'lonHostID'} killed lond process $_";
! 210: system("echo 'Killed lond process $_.' |\
! 211: mailto $emailto -s '$subj' > /dev/null");
! 212: $execdir=$perlvar{'lonDaemons'};
! 213: system("cp $execdir/logs/lond.log $execdir/logs/lond.log.".$_);
1.63 www 214: }
215: }
1.57 www 216: }
217:
1.1 albertel 218: # --------------------------------------------------------------------- Logging
219:
220: sub logthis {
221: my $message=shift;
222: my $execdir=$perlvar{'lonDaemons'};
223: my $fh=IO::File->new(">>$execdir/logs/lond.log");
224: my $now=time;
225: my $local=localtime($now);
1.58 www 226: $lastlog=$local.': '.$message;
1.1 albertel 227: print $fh "$local ($$): $message\n";
228: }
229:
1.57 www 230: # ------------------------------------------------------------------ Log status
231:
232: sub logstatus {
233: my $docdir=$perlvar{'lonDocRoot'};
1.63 www 234: {
1.57 www 235: my $fh=IO::File->new(">>$docdir/lon-status/londstatus.txt");
236: print $fh $$."\t".$status."\t".$lastlog."\n";
1.63 www 237: $fh->close();
238: }
239: {
240: my $fh=IO::File->new(">$docdir/lon-status/londchld/$$.txt");
241: print $fh $status."\n".$lastlog."\n".time;
242: $fh->close();
243: }
1.57 www 244: }
245:
246: sub initnewstatus {
247: my $docdir=$perlvar{'lonDocRoot'};
248: my $fh=IO::File->new(">$docdir/lon-status/londstatus.txt");
249: my $now=time;
250: my $local=localtime($now);
251: print $fh "LOND status $local - parent $$\n\n";
1.64 www 252: opendir(DIR,"$docdir/lon-status/londchld");
253: while ($filename=readdir(DIR)) {
254: unlink("$docdir/lon-status/londchld/$filename");
255: }
256: closedir(DIR);
1.57 www 257: }
258:
259: # -------------------------------------------------------------- Status setting
260:
261: sub status {
262: my $what=shift;
263: my $now=time;
264: my $local=localtime($now);
265: $status=$local.': '.$what;
266: }
1.11 www 267:
268: # -------------------------------------------------------- Escape Special Chars
269:
270: sub escape {
271: my $str=shift;
272: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
273: return $str;
274: }
275:
276: # ----------------------------------------------------- Un-Escape Special Chars
277:
278: sub unescape {
279: my $str=shift;
280: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
281: return $str;
282: }
283:
1.1 albertel 284: # ----------------------------------------------------------- Send USR1 to lonc
285:
286: sub reconlonc {
287: my $peerfile=shift;
288: &logthis("Trying to reconnect for $peerfile");
289: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
290: if (my $fh=IO::File->new("$loncfile")) {
291: my $loncpid=<$fh>;
292: chomp($loncpid);
293: if (kill 0 => $loncpid) {
294: &logthis("lonc at pid $loncpid responding, sending USR1");
295: kill USR1 => $loncpid;
296: sleep 1;
297: if (-e "$peerfile") { return; }
298: &logthis("$peerfile still not there, give it another try");
299: sleep 5;
300: if (-e "$peerfile") { return; }
1.9 www 301: &logthis(
302: "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
1.1 albertel 303: } else {
1.9 www 304: &logthis(
305: "<font color=red>CRITICAL: "
306: ."lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 307: }
308: } else {
1.9 www 309: &logthis('<font color=red>CRITICAL: lonc not running, giving up</font>');
1.1 albertel 310: }
311: }
312:
313: # -------------------------------------------------- Non-critical communication
1.11 www 314:
1.1 albertel 315: sub subreply {
316: my ($cmd,$server)=@_;
317: my $peerfile="$perlvar{'lonSockDir'}/$server";
318: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
319: Type => SOCK_STREAM,
320: Timeout => 10)
321: or return "con_lost";
322: print $sclient "$cmd\n";
323: my $answer=<$sclient>;
324: chomp($answer);
325: if (!$answer) { $answer="con_lost"; }
326: return $answer;
327: }
328:
329: sub reply {
330: my ($cmd,$server)=@_;
331: my $answer;
332: if ($server ne $perlvar{'lonHostID'}) {
333: $answer=subreply($cmd,$server);
334: if ($answer eq 'con_lost') {
335: $answer=subreply("ping",$server);
336: if ($answer ne $server) {
337: &reconlonc("$perlvar{'lonSockDir'}/$server");
338: }
339: $answer=subreply($cmd,$server);
340: }
341: } else {
342: $answer='self_reply';
343: }
344: return $answer;
345: }
346:
1.13 www 347: # -------------------------------------------------------------- Talk to lonsql
348:
1.12 harris41 349: sub sqlreply {
350: my ($cmd)=@_;
351: my $answer=subsqlreply($cmd);
352: if ($answer eq 'con_lost') { $answer=subsqlreply($cmd); }
353: return $answer;
354: }
355:
356: sub subsqlreply {
357: my ($cmd)=@_;
358: my $unixsock="mysqlsock";
359: my $peerfile="$perlvar{'lonSockDir'}/$unixsock";
360: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
361: Type => SOCK_STREAM,
362: Timeout => 10)
363: or return "con_lost";
364: print $sclient "$cmd\n";
365: my $answer=<$sclient>;
366: chomp($answer);
367: if (!$answer) { $answer="con_lost"; }
368: return $answer;
369: }
370:
1.1 albertel 371: # -------------------------------------------- Return path to profile directory
1.11 www 372:
1.1 albertel 373: sub propath {
374: my ($udom,$uname)=@_;
375: $udom=~s/\W//g;
376: $uname=~s/\W//g;
1.16 www 377: my $subdir=$uname.'__';
1.1 albertel 378: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
379: my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
380: return $proname;
381: }
382:
383: # --------------------------------------- Is this the home server of an author?
1.11 www 384:
1.1 albertel 385: sub ishome {
386: my $author=shift;
387: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
388: my ($udom,$uname)=split(/\//,$author);
389: my $proname=propath($udom,$uname);
390: if (-e $proname) {
391: return 'owner';
392: } else {
393: return 'not_owner';
394: }
395: }
396:
397: # ======================================================= Continue main program
398: # ---------------------------------------------------- Fork once and dissociate
399:
400: $fpid=fork;
401: exit if $fpid;
1.29 harris41 402: die "Couldn't fork: $!" unless defined ($fpid);
1.1 albertel 403:
1.29 harris41 404: POSIX::setsid() or die "Can't start new session: $!";
1.1 albertel 405:
406: # ------------------------------------------------------- Write our PID on disk
407:
408: $execdir=$perlvar{'lonDaemons'};
409: open (PIDSAVE,">$execdir/logs/lond.pid");
410: print PIDSAVE "$$\n";
411: close(PIDSAVE);
1.9 www 412: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
1.57 www 413: &status('Starting');
1.1 albertel 414:
415: # ------------------------------------------------------- Now we are on our own
416:
417: # Fork off our children.
418: for (1 .. $PREFORK) {
419: make_new_child();
420: }
421:
422: # ----------------------------------------------------- Install signal handlers
423:
1.57 www 424: &status('Forked children');
425:
1.1 albertel 426: $SIG{CHLD} = \&REAPER;
427: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
428: $SIG{HUP} = \&HUPSMAN;
1.57 www 429: $SIG{USR1} = \&checkchildren;
1.1 albertel 430:
431: # And maintain the population.
432: while (1) {
1.57 www 433: &status('Sleeping');
1.1 albertel 434: sleep; # wait for a signal (i.e., child's death)
1.57 www 435: &logthis('Woke up');
436: &status('Woke up');
1.1 albertel 437: for ($i = $children; $i < $PREFORK; $i++) {
438: make_new_child(); # top up the child pool
439: }
440: }
441:
442: sub make_new_child {
443: my $pid;
444: my $cipher;
445: my $sigset;
446: &logthis("Attempting to start child");
447: # block signal for fork
448: $sigset = POSIX::SigSet->new(SIGINT);
449: sigprocmask(SIG_BLOCK, $sigset)
1.29 harris41 450: or die "Can't block SIGINT for fork: $!\n";
1.1 albertel 451:
1.29 harris41 452: die "fork: $!" unless defined ($pid = fork);
1.1 albertel 453:
454: if ($pid) {
455: # Parent records the child's birth and returns.
456: sigprocmask(SIG_UNBLOCK, $sigset)
1.29 harris41 457: or die "Can't unblock SIGINT for fork: $!\n";
1.1 albertel 458: $children{$pid} = 1;
459: $children++;
1.57 www 460: &status('Started child '.$pid);
1.1 albertel 461: return;
462: } else {
463: # Child can *not* return from this subroutine.
464: $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before
1.57 www 465: $SIG{USR1}= \&logstatus;
1.63 www 466: $SIG{ALRM}= \&timeout;
1.57 www 467: $lastlog='Forked ';
468: $status='Forked';
469:
1.1 albertel 470: # unblock signals
471: sigprocmask(SIG_UNBLOCK, $sigset)
1.29 harris41 472: or die "Can't unblock SIGINT for fork: $!\n";
1.13 www 473:
474: $tmpsnum=0;
1.1 albertel 475:
476: # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
477: for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
1.57 www 478: &status('Idle, waiting for connection');
1.1 albertel 479: $client = $server->accept() or last;
1.57 www 480: &status('Accepted connection');
1.1 albertel 481: # =============================================================================
482: # do something with the connection
483: # -----------------------------------------------------------------------------
1.2 www 484: # see if we know client and check for spoof IP by challenge
1.1 albertel 485: my $caller=getpeername($client);
486: my ($port,$iaddr)=unpack_sockaddr_in($caller);
487: my $clientip=inet_ntoa($iaddr);
488: my $clientrec=($hostid{$clientip} ne undef);
1.9 www 489: &logthis(
1.51 www 490: "<font color=yellow>INFO: Connection $i, $clientip ($hostid{$clientip})</font>"
491: );
1.57 www 492: &status("Connecting $clientip ($hostid{$clientip})");
1.2 www 493: my $clientok;
1.1 albertel 494: if ($clientrec) {
1.57 www 495: &status("Waiting for init from $clientip ($hostid{$clientip})");
1.2 www 496: my $remotereq=<$client>;
497: $remotereq=~s/\W//g;
498: if ($remotereq eq 'init') {
499: my $challenge="$$".time;
500: print $client "$challenge\n";
1.57 www 501: &status(
502: "Waiting for challenge reply from $clientip ($hostid{$clientip})");
1.2 www 503: $remotereq=<$client>;
504: $remotereq=~s/\W//g;
505: if ($challenge eq $remotereq) {
506: $clientok=1;
507: print $client "ok\n";
508: } else {
1.9 www 509: &logthis(
510: "<font color=blue>WARNING: $clientip did not reply challenge</font>");
1.57 www 511: &status('No challenge reply '.$clientip);
1.2 www 512: }
513: } else {
1.9 www 514: &logthis(
515: "<font color=blue>WARNING: "
516: ."$clientip failed to initialize: >$remotereq< </font>");
1.57 www 517: &status('No init '.$clientip);
1.2 www 518: }
519: } else {
1.9 www 520: &logthis(
521: "<font color=blue>WARNING: Unknown client $clientip</font>");
1.57 www 522: &status('Hung up on '.$clientip);
1.2 www 523: }
524: if ($clientok) {
1.1 albertel 525: # ---------------- New known client connecting, could mean machine online again
526: &reconlonc("$perlvar{'lonSockDir'}/$hostid{$clientip}");
1.9 www 527: &logthis(
528: "<font color=green>Established connection: $hostid{$clientip}</font>");
1.58 www 529: &status('Will listen to '.$hostid{$clientip});
1.1 albertel 530: # ------------------------------------------------------------ Process requests
531: while (my $userinput=<$client>) {
532: chomp($userinput);
1.57 www 533: &status('Processing '.$hostid{$clientip}.': '.$userinput);
1.1 albertel 534: my $wasenc=0;
1.63 www 535: alarm(120);
1.1 albertel 536: # ------------------------------------------------------------ See if encrypted
537: if ($userinput =~ /^enc/) {
538: if ($cipher) {
539: my ($cmd,$cmdlength,$encinput)=split(/:/,$userinput);
540: $userinput='';
541: for (my $encidx=0;$encidx<length($encinput);$encidx+=16) {
542: $userinput.=
543: $cipher->decrypt(
544: pack("H16",substr($encinput,$encidx,16))
545: );
546: }
547: $userinput=substr($userinput,0,$cmdlength);
548: $wasenc=1;
549: }
550: }
551: # ------------------------------------------------------------- Normal commands
552: # ------------------------------------------------------------------------ ping
553: if ($userinput =~ /^ping/) {
554: print $client "$perlvar{'lonHostID'}\n";
555: # ------------------------------------------------------------------------ pong
556: } elsif ($userinput =~ /^pong/) {
557: $reply=reply("ping",$hostid{$clientip});
558: print $client "$perlvar{'lonHostID'}:$reply\n";
559: # ------------------------------------------------------------------------ ekey
560: } elsif ($userinput =~ /^ekey/) {
561: my $buildkey=time.$$.int(rand 100000);
562: $buildkey=~tr/1-6/A-F/;
563: $buildkey=int(rand 100000).$buildkey.int(rand 100000);
564: my $key=$perlvar{'lonHostID'}.$hostid{$clientip};
565: $key=~tr/a-z/A-Z/;
566: $key=~tr/G-P/0-9/;
567: $key=~tr/Q-Z/0-9/;
568: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
569: $key=substr($key,0,32);
570: my $cipherkey=pack("H32",$key);
571: $cipher=new IDEA $cipherkey;
572: print $client "$buildkey\n";
573: # ------------------------------------------------------------------------ load
574: } elsif ($userinput =~ /^load/) {
575: my $loadavg;
576: {
577: my $loadfile=IO::File->new('/proc/loadavg');
578: $loadavg=<$loadfile>;
579: }
580: $loadavg =~ s/\s.*//g;
581: my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
582: print $client "$loadpercent\n";
1.54 harris41 583: # ----------------------------------------------------------------- currentauth
584: } elsif ($userinput =~ /^currentauth/) {
585: if ($wasenc==1) {
586: my ($cmd,$udom,$uname)=split(/:/,$userinput);
587: my $proname=propath($udom,$uname);
588: my $passfilename="$proname/passwd";
589: if (-e $passfilename) {
590: my $pf = IO::File->new($passfilename);
591: my $realpasswd=<$pf>;
592: chomp($realpasswd);
593: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
594: my $availablecontent='';
595: if ($howpwd eq 'krb4') {
596: $availablecontent=$contentpwd;
597: }
598: print $client "$howpwd:$availablecontent\n";
599: } else {
600: print $client "unknown_user\n";
601: }
602: } else {
603: print $client "refused\n";
604: }
1.1 albertel 605: # ------------------------------------------------------------------------ auth
606: } elsif ($userinput =~ /^auth/) {
607: if ($wasenc==1) {
608: my ($cmd,$udom,$uname,$upass)=split(/:/,$userinput);
609: chomp($upass);
1.11 www 610: $upass=unescape($upass);
1.1 albertel 611: my $proname=propath($udom,$uname);
612: my $passfilename="$proname/passwd";
613: if (-e $passfilename) {
614: my $pf = IO::File->new($passfilename);
615: my $realpasswd=<$pf>;
616: chomp($realpasswd);
1.2 www 617: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
618: my $pwdcorrect=0;
619: if ($howpwd eq 'internal') {
620: $pwdcorrect=
621: (crypt($upass,$contentpwd) eq $contentpwd);
622: } elsif ($howpwd eq 'unix') {
623: $contentpwd=(getpwnam($uname))[1];
1.52 harris41 624: my $pwauth_path="/usr/local/sbin/pwauth";
625: unless ($contentpwd eq 'x') {
626: $pwdcorrect=
627: (crypt($upass,$contentpwd) eq $contentpwd);
628: }
629: elsif (-e $pwauth_path) {
630: open PWAUTH, "|$pwauth_path" or
631: die "Cannot invoke authentication";
632: print PWAUTH "$uname\n$upass\n";
633: close PWAUTH;
634: $pwdcorrect=!$?;
635: }
1.3 www 636: } elsif ($howpwd eq 'krb4') {
637: $pwdcorrect=(
638: Authen::Krb4::get_pw_in_tkt($uname,"",
639: $contentpwd,'krbtgt',$contentpwd,1,
640: $upass) == 0);
1.50 albertel 641: } elsif ($howpwd eq 'localauth') {
1.49 albertel 642: $pwdcorrect=&localauth::localauth($uname,$upass,
643: $contentpwd);
644: }
1.2 www 645: if ($pwdcorrect) {
1.1 albertel 646: print $client "authorized\n";
647: } else {
648: print $client "non_authorized\n";
649: }
650: } else {
651: print $client "unknown_user\n";
652: }
653: } else {
654: print $client "refused\n";
655: }
656: # ---------------------------------------------------------------------- passwd
657: } elsif ($userinput =~ /^passwd/) {
658: if ($wasenc==1) {
659: my
660: ($cmd,$udom,$uname,$upass,$npass)=split(/:/,$userinput);
661: chomp($npass);
1.32 www 662: $upass=&unescape($upass);
663: $npass=&unescape($npass);
1.1 albertel 664: my $proname=propath($udom,$uname);
665: my $passfilename="$proname/passwd";
666: if (-e $passfilename) {
667: my $realpasswd;
668: { my $pf = IO::File->new($passfilename);
669: $realpasswd=<$pf>; }
670: chomp($realpasswd);
1.2 www 671: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
672: if ($howpwd eq 'internal') {
673: if (crypt($upass,$contentpwd) eq $contentpwd) {
674: my $salt=time;
675: $salt=substr($salt,6,2);
676: my $ncpass=crypt($npass,$salt);
1.1 albertel 677: { my $pf = IO::File->new(">$passfilename");
1.31 www 678: print $pf "internal:$ncpass\n"; }
1.1 albertel 679: print $client "ok\n";
1.2 www 680: } else {
681: print $client "non_authorized\n";
682: }
1.1 albertel 683: } else {
1.2 www 684: print $client "auth_mode_error\n";
1.1 albertel 685: }
686: } else {
687: print $client "unknown_user\n";
1.31 www 688: }
689: } else {
690: print $client "refused\n";
691: }
692: # -------------------------------------------------------------------- makeuser
693: } elsif ($userinput =~ /^makeuser/) {
1.56 harris41 694: my $oldumask=umask(0077);
1.31 www 695: if ($wasenc==1) {
696: my
697: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
698: chomp($npass);
1.32 www 699: $npass=&unescape($npass);
1.31 www 700: my $proname=propath($udom,$uname);
701: my $passfilename="$proname/passwd";
702: if (-e $passfilename) {
703: print $client "already_exists\n";
704: } elsif ($udom ne $perlvar{'lonDefDomain'}) {
705: print $client "not_right_domain\n";
706: } else {
707: @fpparts=split(/\//,$proname);
708: $fpnow=$fpparts[0].'/'.$fpparts[1].'/'.$fpparts[2];
709: $fperror='';
710: for ($i=3;$i<=$#fpparts;$i++) {
711: $fpnow.='/'.$fpparts[$i];
712: unless (-e $fpnow) {
713: unless (mkdir($fpnow,0777)) {
1.65 www 714: $fperror="error:$!";
1.31 www 715: }
716: }
717: }
718: unless ($fperror) {
1.34 www 719: if ($umode eq 'krb4') {
1.31 www 720: {
721: my $pf = IO::File->new(">$passfilename");
1.33 www 722: print $pf "krb4:$npass\n";
1.31 www 723: }
724: print $client "ok\n";
725: } elsif ($umode eq 'internal') {
726: my $salt=time;
727: $salt=substr($salt,6,2);
728: my $ncpass=crypt($npass,$salt);
729: {
730: my $pf = IO::File->new(">$passfilename");
731: print $pf "internal:$ncpass\n";
1.50 albertel 732: }
1.31 www 733: print $client "ok\n";
1.50 albertel 734: } elsif ($umode eq 'localauth') {
735: {
736: my $pf = IO::File->new(">$passfilename");
737: print $pf "localauth:$npass\n";
738: }
739: print $client "ok\n";
1.53 harris41 740: } elsif ($umode eq 'unix') {
741: {
742: my $execpath="$perlvar{'lonDaemons'}/".
743: "lcuseradd";
1.54 harris41 744: {
745: my $se = IO::File->new("|$execpath");
746: print $se "$uname\n";
747: print $se "$npass\n";
748: print $se "$npass\n";
749: }
1.53 harris41 750: my $pf = IO::File->new(">$passfilename");
751: print $pf "unix:\n";
752: }
1.54 harris41 753: print $client "ok\n";
1.53 harris41 754: } elsif ($umode eq 'none') {
1.31 www 755: {
756: my $pf = IO::File->new(">$passfilename");
757: print $pf "none:\n";
758: }
759: print $client "ok\n";
760: } else {
761: print $client "auth_mode_error\n";
762: }
763: } else {
764: print $client "$fperror\n";
765: }
1.55 harris41 766: }
767: } else {
768: print $client "refused\n";
769: }
1.56 harris41 770: umask($oldumask);
1.55 harris41 771: # -------------------------------------------------------------- changeuserauth
772: } elsif ($userinput =~ /^changeuserauth/) {
773: if ($wasenc==1) {
774: my
775: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
776: chomp($npass);
777: $npass=&unescape($npass);
778: my $proname=propath($udom,$uname);
779: my $passfilename="$proname/passwd";
780: if ($udom ne $perlvar{'lonDefDomain'}) {
781: print $client "not_right_domain\n";
782: } else {
783: if ($umode eq 'krb4') {
784: {
785: my $pf = IO::File->new(">$passfilename");
786: print $pf "krb4:$npass\n";
787: }
788: print $client "ok\n";
789: } elsif ($umode eq 'internal') {
790: my $salt=time;
791: $salt=substr($salt,6,2);
792: my $ncpass=crypt($npass,$salt);
793: {
794: my $pf = IO::File->new(">$passfilename");
795: print $pf "internal:$ncpass\n";
796: }
797: print $client "ok\n";
798: } elsif ($umode eq 'localauth') {
799: {
800: my $pf = IO::File->new(">$passfilename");
801: print $pf "localauth:$npass\n";
802: }
803: print $client "ok\n";
804: } elsif ($umode eq 'unix') {
805: {
806: my $execpath="$perlvar{'lonDaemons'}/".
807: "lcuseradd";
808: {
809: my $se = IO::File->new("|$execpath");
810: print $se "$uname\n";
811: print $se "$npass\n";
812: print $se "$npass\n";
813: }
814: my $pf = IO::File->new(">$passfilename");
815: print $pf "unix:\n";
816: }
817: print $client "ok\n";
818: } elsif ($umode eq 'none') {
819: {
820: my $pf = IO::File->new(">$passfilename");
821: print $pf "none:\n";
822: }
823: print $client "ok\n";
824: } else {
825: print $client "auth_mode_error\n";
826: }
1.1 albertel 827: }
828: } else {
829: print $client "refused\n";
830: }
831: # ------------------------------------------------------------------------ home
832: } elsif ($userinput =~ /^home/) {
833: my ($cmd,$udom,$uname)=split(/:/,$userinput);
834: chomp($uname);
835: my $proname=propath($udom,$uname);
836: if (-e $proname) {
837: print $client "found\n";
838: } else {
839: print $client "not_found\n";
840: }
841: # ---------------------------------------------------------------------- update
842: } elsif ($userinput =~ /^update/) {
843: my ($cmd,$fname)=split(/:/,$userinput);
844: my $ownership=ishome($fname);
845: if ($ownership eq 'not_owner') {
846: if (-e $fname) {
847: my ($dev,$ino,$mode,$nlink,
848: $uid,$gid,$rdev,$size,
849: $atime,$mtime,$ctime,
850: $blksize,$blocks)=stat($fname);
851: $now=time;
852: $since=$now-$atime;
853: if ($since>$perlvar{'lonExpire'}) {
854: $reply=
855: reply("unsub:$fname","$hostid{$clientip}");
856: unlink("$fname");
857: } else {
858: my $transname="$fname.in.transfer";
859: my $remoteurl=
860: reply("sub:$fname","$hostid{$clientip}");
861: my $response;
862: {
863: my $ua=new LWP::UserAgent;
864: my $request=new HTTP::Request('GET',"$remoteurl");
865: $response=$ua->request($request,$transname);
866: }
867: if ($response->is_error()) {
1.24 albertel 868: unlink($transname);
1.1 albertel 869: my $message=$response->status_line;
870: &logthis(
871: "LWP GET: $message for $fname ($remoteurl)");
872: } else {
1.14 www 873: if ($remoteurl!~/\.meta$/) {
1.28 www 874: my $ua=new LWP::UserAgent;
1.14 www 875: my $mrequest=
876: new HTTP::Request('GET',$remoteurl.'.meta');
877: my $mresponse=
878: $ua->request($mrequest,$fname.'.meta');
879: if ($mresponse->is_error()) {
880: unlink($fname.'.meta');
881: }
882: }
1.1 albertel 883: rename($transname,$fname);
884: }
885: }
886: print $client "ok\n";
887: } else {
888: print $client "not_found\n";
889: }
890: } else {
891: print $client "rejected\n";
892: }
893: # ----------------------------------------------------------------- unsubscribe
894: } elsif ($userinput =~ /^unsub/) {
895: my ($cmd,$fname)=split(/:/,$userinput);
896: if (-e $fname) {
897: if (unlink("$fname.$hostid{$clientip}")) {
898: print $client "ok\n";
899: } else {
900: print $client "not_subscribed\n";
901: }
902: } else {
903: print $client "not_found\n";
904: }
905: # ------------------------------------------------------------------- subscribe
906: } elsif ($userinput =~ /^sub/) {
907: my ($cmd,$fname)=split(/:/,$userinput);
908: my $ownership=ishome($fname);
909: if ($ownership eq 'owner') {
910: if (-e $fname) {
1.18 www 911: if (-d $fname) {
912: print $client "directory\n";
913: } else {
1.1 albertel 914: $now=time;
915: {
1.26 www 916: my $sh;
1.25 www 917: if ($sh=
918: IO::File->new(">$fname.$hostid{$clientip}")) {
919: print $sh "$clientip:$now\n";
920: }
1.1 albertel 921: }
1.42 www 922: unless ($fname=~/\.meta$/) {
923: unlink("$fname.meta.$hostid{$clientip}");
924: }
1.1 albertel 925: $fname=~s/\/home\/httpd\/html\/res/raw/;
926: $fname="http://$thisserver/".$fname;
927: print $client "$fname\n";
1.18 www 928: }
1.1 albertel 929: } else {
930: print $client "not_found\n";
931: }
932: } else {
933: print $client "rejected\n";
934: }
1.12 harris41 935: # ------------------------------------------------------------------------- log
936: } elsif ($userinput =~ /^log/) {
937: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
938: chomp($what);
939: my $proname=propath($udom,$uname);
940: my $now=time;
941: {
942: my $hfh;
943: if ($hfh=IO::File->new(">>$proname/activity.log")) {
944: print $hfh "$now:$hostid{$clientip}:$what\n";
945: print $client "ok\n";
946: } else {
947: print $client "error:$!\n";
948: }
949: }
1.1 albertel 950: # ------------------------------------------------------------------------- put
951: } elsif ($userinput =~ /^put/) {
1.6 www 952: my ($cmd,$udom,$uname,$namespace,$what)
1.1 albertel 953: =split(/:/,$userinput);
1.8 www 954: $namespace=~s/\//\_/g;
1.6 www 955: $namespace=~s/\W//g;
956: if ($namespace ne 'roles') {
1.1 albertel 957: chomp($what);
958: my $proname=propath($udom,$uname);
959: my $now=time;
1.48 www 960: unless ($namespace=~/^nohist\_/) {
1.1 albertel 961: my $hfh;
962: if (
963: $hfh=IO::File->new(">>$proname/$namespace.hist")
964: ) { print $hfh "P:$now:$what\n"; }
965: }
966: my @pairs=split(/\&/,$what);
1.4 www 967: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 968: foreach $pair (@pairs) {
969: ($key,$value)=split(/=/,$pair);
970: $hash{$key}=$value;
971: }
1.4 www 972: if (untie(%hash)) {
1.1 albertel 973: print $client "ok\n";
974: } else {
975: print $client "error:$!\n";
976: }
977: } else {
978: print $client "error:$!\n";
979: }
1.6 www 980: } else {
981: print $client "refused\n";
982: }
983: # -------------------------------------------------------------------- rolesput
984: } elsif ($userinput =~ /^rolesput/) {
985: if ($wasenc==1) {
986: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
987: =split(/:/,$userinput);
988: my $namespace='roles';
989: chomp($what);
990: my $proname=propath($udom,$uname);
991: my $now=time;
992: {
993: my $hfh;
994: if (
995: $hfh=IO::File->new(">>$proname/$namespace.hist")
996: ) {
997: print $hfh "P:$now:$exedom:$exeuser:$what\n";
998: }
999: }
1000: my @pairs=split(/\&/,$what);
1001: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1002: foreach $pair (@pairs) {
1003: ($key,$value)=split(/=/,$pair);
1004: $hash{$key}=$value;
1005: }
1006: if (untie(%hash)) {
1007: print $client "ok\n";
1008: } else {
1009: print $client "error:$!\n";
1010: }
1011: } else {
1012: print $client "error:$!\n";
1013: }
1014: } else {
1015: print $client "refused\n";
1016: }
1.1 albertel 1017: # ------------------------------------------------------------------------- get
1018: } elsif ($userinput =~ /^get/) {
1019: my ($cmd,$udom,$uname,$namespace,$what)
1020: =split(/:/,$userinput);
1.8 www 1021: $namespace=~s/\//\_/g;
1.1 albertel 1022: $namespace=~s/\W//g;
1023: chomp($what);
1024: my @queries=split(/\&/,$what);
1025: my $proname=propath($udom,$uname);
1026: my $qresult='';
1.20 www 1027: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1028: for ($i=0;$i<=$#queries;$i++) {
1029: $qresult.="$hash{$queries[$i]}&";
1030: }
1.4 www 1031: if (untie(%hash)) {
1.1 albertel 1032: $qresult=~s/\&$//;
1033: print $client "$qresult\n";
1034: } else {
1035: print $client "error:$!\n";
1036: }
1037: } else {
1038: print $client "error:$!\n";
1039: }
1040: # ------------------------------------------------------------------------ eget
1041: } elsif ($userinput =~ /^eget/) {
1042: my ($cmd,$udom,$uname,$namespace,$what)
1043: =split(/:/,$userinput);
1.8 www 1044: $namespace=~s/\//\_/g;
1.1 albertel 1045: $namespace=~s/\W//g;
1046: chomp($what);
1047: my @queries=split(/\&/,$what);
1048: my $proname=propath($udom,$uname);
1049: my $qresult='';
1.20 www 1050: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1051: for ($i=0;$i<=$#queries;$i++) {
1052: $qresult.="$hash{$queries[$i]}&";
1053: }
1.4 www 1054: if (untie(%hash)) {
1.1 albertel 1055: $qresult=~s/\&$//;
1056: if ($cipher) {
1057: my $cmdlength=length($qresult);
1058: $qresult.=" ";
1059: my $encqresult='';
1060: for
1061: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
1062: $encqresult.=
1063: unpack("H16",
1064: $cipher->encrypt(substr($qresult,$encidx,8)));
1065: }
1066: print $client "enc:$cmdlength:$encqresult\n";
1067: } else {
1068: print $client "error:no_key\n";
1069: }
1070: } else {
1071: print $client "error:$!\n";
1072: }
1073: } else {
1074: print $client "error:$!\n";
1075: }
1076: # ------------------------------------------------------------------------- del
1077: } elsif ($userinput =~ /^del/) {
1078: my ($cmd,$udom,$uname,$namespace,$what)
1079: =split(/:/,$userinput);
1.8 www 1080: $namespace=~s/\//\_/g;
1.1 albertel 1081: $namespace=~s/\W//g;
1082: chomp($what);
1083: my $proname=propath($udom,$uname);
1084: my $now=time;
1.48 www 1085: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1086: my $hfh;
1087: if (
1088: $hfh=IO::File->new(">>$proname/$namespace.hist")
1089: ) { print $hfh "D:$now:$what\n"; }
1090: }
1091: my @keys=split(/\&/,$what);
1.4 www 1092: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1093: foreach $key (@keys) {
1094: delete($hash{$key});
1095: }
1.4 www 1096: if (untie(%hash)) {
1.1 albertel 1097: print $client "ok\n";
1098: } else {
1099: print $client "error:$!\n";
1100: }
1101: } else {
1102: print $client "error:$!\n";
1103: }
1104: # ------------------------------------------------------------------------ keys
1105: } elsif ($userinput =~ /^keys/) {
1106: my ($cmd,$udom,$uname,$namespace)
1107: =split(/:/,$userinput);
1.8 www 1108: $namespace=~s/\//\_/g;
1.1 albertel 1109: $namespace=~s/\W//g;
1110: my $proname=propath($udom,$uname);
1111: my $qresult='';
1.20 www 1112: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1113: foreach $key (keys %hash) {
1114: $qresult.="$key&";
1115: }
1.4 www 1116: if (untie(%hash)) {
1.1 albertel 1117: $qresult=~s/\&$//;
1118: print $client "$qresult\n";
1119: } else {
1120: print $client "error:$!\n";
1121: }
1122: } else {
1123: print $client "error:$!\n";
1124: }
1125: # ------------------------------------------------------------------------ dump
1126: } elsif ($userinput =~ /^dump/) {
1.62 www 1127: my ($cmd,$udom,$uname,$namespace,$regexp)
1.1 albertel 1128: =split(/:/,$userinput);
1.8 www 1129: $namespace=~s/\//\_/g;
1.1 albertel 1130: $namespace=~s/\W//g;
1.62 www 1131: if (defined($regexp)) {
1132: $regexp=&unescape($regexp);
1133: } else {
1134: $regexp='.';
1135: }
1.1 albertel 1136: my $proname=propath($udom,$uname);
1137: my $qresult='';
1.20 www 1138: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1139: foreach $key (keys %hash) {
1.62 www 1140: if (eval('$key=~/$regexp/')) {
1141: $qresult.="$key=$hash{$key}&";
1142: }
1.7 www 1143: }
1144: if (untie(%hash)) {
1145: $qresult=~s/\&$//;
1146: print $client "$qresult\n";
1147: } else {
1148: print $client "error:$!\n";
1149: }
1150: } else {
1151: print $client "error:$!\n";
1152: }
1153: # ----------------------------------------------------------------------- store
1154: } elsif ($userinput =~ /^store/) {
1155: my ($cmd,$udom,$uname,$namespace,$rid,$what)
1156: =split(/:/,$userinput);
1.8 www 1157: $namespace=~s/\//\_/g;
1.7 www 1158: $namespace=~s/\W//g;
1159: if ($namespace ne 'roles') {
1160: chomp($what);
1161: my $proname=propath($udom,$uname);
1162: my $now=time;
1.48 www 1163: unless ($namespace=~/^nohist\_/) {
1.7 www 1164: my $hfh;
1165: if (
1166: $hfh=IO::File->new(">>$proname/$namespace.hist")
1167: ) { print $hfh "P:$now:$rid:$what\n"; }
1168: }
1169: my @pairs=split(/\&/,$what);
1170:
1171: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1172: my @previouskeys=split(/&/,$hash{"keys:$rid"});
1173: my $key;
1174: $hash{"version:$rid"}++;
1175: my $version=$hash{"version:$rid"};
1176: my $allkeys='';
1177: foreach $pair (@pairs) {
1178: ($key,$value)=split(/=/,$pair);
1179: $allkeys.=$key.':';
1180: $hash{"$version:$rid:$key"}=$value;
1181: }
1.36 www 1182: $hash{"$version:$rid:timestamp"}=$now;
1183: $allkeys.='timestamp';
1.7 www 1184: $hash{"$version:keys:$rid"}=$allkeys;
1185: if (untie(%hash)) {
1186: print $client "ok\n";
1187: } else {
1188: print $client "error:$!\n";
1189: }
1190: } else {
1191: print $client "error:$!\n";
1192: }
1193: } else {
1194: print $client "refused\n";
1195: }
1196: # --------------------------------------------------------------------- restore
1197: } elsif ($userinput =~ /^restore/) {
1198: my ($cmd,$udom,$uname,$namespace,$rid)
1199: =split(/:/,$userinput);
1.8 www 1200: $namespace=~s/\//\_/g;
1.7 www 1201: $namespace=~s/\W//g;
1202: chomp($rid);
1203: my $proname=propath($udom,$uname);
1204: my $qresult='';
1.20 www 1205: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.7 www 1206: my $version=$hash{"version:$rid"};
1207: $qresult.="version=$version&";
1208: my $scope;
1209: for ($scope=1;$scope<=$version;$scope++) {
1210: my $vkeys=$hash{"$scope:keys:$rid"};
1211: my @keys=split(/:/,$vkeys);
1212: my $key;
1213: $qresult.="$scope:keys=$vkeys&";
1214: foreach $key (@keys) {
1.21 www 1215: $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
1.7 www 1216: }
1.1 albertel 1217: }
1.4 www 1218: if (untie(%hash)) {
1.1 albertel 1219: $qresult=~s/\&$//;
1220: print $client "$qresult\n";
1221: } else {
1222: print $client "error:$!\n";
1223: }
1224: } else {
1225: print $client "error:$!\n";
1226: }
1.12 harris41 1227: # ------------------------------------------------------------------- querysend
1228: } elsif ($userinput =~ /^querysend/) {
1.44 harris41 1229: my ($cmd,$query,
1230: $custom,$customshow)=split(/:/,$userinput);
1.12 harris41 1231: $query=~s/\n*$//g;
1.45 harris41 1232: unless ($custom or $customshow) {
1.40 harris41 1233: print $client "".
1234: sqlreply("$hostid{$clientip}\&$query")."\n";
1235: }
1236: else {
1237: print $client "".
1238: sqlreply("$hostid{$clientip}\&$query".
1.44 harris41 1239: "\&$custom"."\&$customshow")."\n";
1.40 harris41 1240: }
1.12 harris41 1241: # ------------------------------------------------------------------ queryreply
1242: } elsif ($userinput =~ /^queryreply/) {
1243: my ($cmd,$id,$reply)=split(/:/,$userinput);
1244: my $store;
1.13 www 1245: my $execdir=$perlvar{'lonDaemons'};
1246: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.43 harris41 1247: $reply=~s/\&/\n/g;
1.12 harris41 1248: print $store $reply;
1249: close $store;
1.46 harris41 1250: my $store2=IO::File->new(">$execdir/tmp/$id.end");
1251: print $store2 "done\n";
1252: close $store2;
1.12 harris41 1253: print $client "ok\n";
1254: }
1255: else {
1256: print $client "error:$!\n";
1257: }
1.1 albertel 1258: # ----------------------------------------------------------------------- idput
1259: } elsif ($userinput =~ /^idput/) {
1260: my ($cmd,$udom,$what)=split(/:/,$userinput);
1261: chomp($what);
1262: $udom=~s/\W//g;
1263: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1264: my $now=time;
1265: {
1266: my $hfh;
1267: if (
1268: $hfh=IO::File->new(">>$proname.hist")
1269: ) { print $hfh "P:$now:$what\n"; }
1270: }
1271: my @pairs=split(/\&/,$what);
1.4 www 1272: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1273: foreach $pair (@pairs) {
1274: ($key,$value)=split(/=/,$pair);
1275: $hash{$key}=$value;
1276: }
1.4 www 1277: if (untie(%hash)) {
1.1 albertel 1278: print $client "ok\n";
1279: } else {
1280: print $client "error:$!\n";
1281: }
1282: } else {
1283: print $client "error:$!\n";
1284: }
1285: # ----------------------------------------------------------------------- idget
1286: } elsif ($userinput =~ /^idget/) {
1287: my ($cmd,$udom,$what)=split(/:/,$userinput);
1288: chomp($what);
1289: $udom=~s/\W//g;
1290: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1291: my @queries=split(/\&/,$what);
1292: my $qresult='';
1.20 www 1293: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER,0640)) {
1.1 albertel 1294: for ($i=0;$i<=$#queries;$i++) {
1295: $qresult.="$hash{$queries[$i]}&";
1296: }
1.4 www 1297: if (untie(%hash)) {
1.1 albertel 1298: $qresult=~s/\&$//;
1299: print $client "$qresult\n";
1300: } else {
1301: print $client "error:$!\n";
1302: }
1303: } else {
1304: print $client "error:$!\n";
1305: }
1.13 www 1306: # ---------------------------------------------------------------------- tmpput
1307: } elsif ($userinput =~ /^tmpput/) {
1308: my ($cmd,$what)=split(/:/,$userinput);
1309: my $store;
1310: $tmpsnum++;
1311: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
1312: $id=~s/\W/\_/g;
1313: $what=~s/\n//g;
1314: my $execdir=$perlvar{'lonDaemons'};
1315: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1316: print $store $what;
1317: close $store;
1318: print $client "$id\n";
1319: }
1320: else {
1321: print $client "error:$!\n";
1322: }
1323:
1324: # ---------------------------------------------------------------------- tmpget
1325: } elsif ($userinput =~ /^tmpget/) {
1326: my ($cmd,$id)=split(/:/,$userinput);
1327: chomp($id);
1328: $id=~s/\W/\_/g;
1329: my $store;
1330: my $execdir=$perlvar{'lonDaemons'};
1331: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
1332: my $reply=<$store>;
1333: print $client "$reply\n";
1334: close $store;
1335: }
1336: else {
1337: print $client "error:$!\n";
1338: }
1339:
1.5 www 1340: # -------------------------------------------------------------------------- ls
1341: } elsif ($userinput =~ /^ls/) {
1342: my ($cmd,$ulsdir)=split(/:/,$userinput);
1343: my $ulsout='';
1344: my $ulsfn;
1345: if (-e $ulsdir) {
1.41 www 1346: if (opendir(LSDIR,$ulsdir)) {
1347: while ($ulsfn=readdir(LSDIR)) {
1.47 www 1348: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1.5 www 1349: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1350: }
1.41 www 1351: closedir(LSDIR);
1352: }
1.5 www 1353: } else {
1354: $ulsout='no_such_dir';
1355: }
1.17 www 1356: if ($ulsout eq '') { $ulsout='empty'; }
1.5 www 1357: print $client "$ulsout\n";
1.51 www 1358: # ------------------------------------------------------------------ Hanging up
1359: } elsif (($userinput =~ /^exit/) ||
1360: ($userinput =~ /^init/)) {
1361: &logthis(
1362: "Client $clientip ($hostid{$clientip}) hanging up: $userinput");
1363: print $client "bye\n";
1.59 www 1364: $client->close();
1.51 www 1365: last;
1.1 albertel 1366: # ------------------------------------------------------------- unknown command
1367: } else {
1368: # unknown command
1369: print $client "unknown_cmd\n";
1370: }
1.58 www 1371: # -------------------------------------------------------------------- complete
1.63 www 1372: alarm(0);
1.58 www 1373: &status('Listening to '.$hostid{$clientip});
1374: }
1.59 www 1375: # --------------------------------------------- client unknown or fishy, refuse
1.1 albertel 1376: } else {
1377: print $client "refused\n";
1.59 www 1378: $client->close();
1.9 www 1379: &logthis("<font color=blue>WARNING: "
1380: ."Rejected client $clientip, closing connection</font>");
1.1 albertel 1381: }
1.9 www 1382: &logthis("<font color=red>CRITICAL: "
1.10 www 1383: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.1 albertel 1384: # =============================================================================
1385: }
1386:
1387: # tidy up gracefully and finish
1388:
1.59 www 1389: $client->close();
1390: $server->close();
1391:
1.1 albertel 1392: # this exit is VERY important, otherwise the child will become
1393: # a producer of more and more children, forking yourself into
1394: # process death.
1395: exit;
1396: }
1397: }
1398:
1.61 harris41 1399: # ----------------------------------- POD (plain old documentation, CPAN style)
1400:
1401: =head1 NAME
1402:
1403: lond - "LON Daemon" Server (port "LOND" 5663)
1404:
1405: =head1 SYNOPSIS
1406:
1407: Should only be run as user=www. Invoked by loncron.
1408:
1409: =head1 DESCRIPTION
1410:
1411: Preforker - server who forks first. Runs as a daemon. HUPs.
1412: Uses IDEA encryption
1413:
1414: =head1 README
1415:
1416: Not yet written.
1417:
1418: =head1 PREREQUISITES
1419:
1420: IO::Socket
1421: IO::File
1422: Apache::File
1423: Symbol
1424: POSIX
1425: Crypt::IDEA
1426: LWP::UserAgent()
1427: GDBM_File
1428: Authen::Krb4
1429:
1430: =head1 COREQUISITES
1431:
1432: =head1 OSNAMES
1433:
1434: linux
1435:
1436: =head1 SCRIPT CATEGORIES
1437:
1438: Server/Process
1439:
1440: =cut
1.1 albertel 1441:
1442:
1443:
1444:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>