Annotation of loncom/lond, revision 1.50
1.1 albertel 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
4: # 5/26/99,6/4,6/10,6/11,6/14,6/15,6/26,6/28,6/30,
1.2 www 5: # 7/8,7/9,7/10,7/12,7/17,7/19,9/21,
1.6 www 6: # 10/7,10/8,10/9,10/11,10/13,10/15,11/4,11/16,
1.11 www 7: # 12/7,12/15,01/06,01/11,01/12,01/14,2/8,
1.12 harris41 8: # 03/07,05/31 Gerd Kortemeyer
1.13 www 9: # 06/26 Scott Harrison
1.20 www 10: # 06/29,06/30,07/14,07/15,07/17,07/20,07/25,09/18 Gerd Kortemeyer
1.25 www 11: # 12/05 Scott Harrison
1.34 www 12: # 12/05,12/13,12/29 Gerd Kortemeyer
1.36 www 13: # Jan 01 Scott Harrison
14: # 02/12 Gerd Kortemeyer
1.37 harris41 15: # 03/15 Scott Harrison
1.41 www 16: # 03/24 Gerd Kortemeyer
1.47 www 17: # 04/02 Scott Harrison
1.48 www 18: # 05/11,05/28 Gerd Kortemeyer
1.13 www 19: #
1.1 albertel 20: # based on "Perl Cookbook" ISBN 1-56592-243-3
21: # preforker - server who forks first
22: # runs as a daemon
23: # HUPs
24: # uses IDEA encryption
25:
26: use IO::Socket;
27: use IO::File;
28: use Apache::File;
29: use Symbol;
30: use POSIX;
31: use Crypt::IDEA;
32: use LWP::UserAgent();
1.3 www 33: use GDBM_File;
34: use Authen::Krb4;
1.49 albertel 35: use lib '/home/httpd/lib/perl/';
36: use localauth;
1.1 albertel 37:
1.23 harris41 38: # grabs exception and records it to log before exiting
39: sub catchexception {
1.27 albertel 40: my ($error)=@_;
1.25 www 41: $SIG{'QUIT'}='DEFAULT';
42: $SIG{__DIE__}='DEFAULT';
1.23 harris41 43: &logthis("<font color=red>CRITICAL: "
44: ."ABNORMAL EXIT. Child $$ for server $wasserver died through "
1.27 albertel 45: ."a crash with this error msg->[$error]</font>");
46: if ($client) { print $client "error: $error\n"; }
47: die($error);
1.23 harris41 48: }
49:
1.22 harris41 50: # -------------------------------- Set signal handlers to record abnormal exits
51:
52: $SIG{'QUIT'}=\&catchexception;
53: $SIG{__DIE__}=\&catchexception;
54:
1.1 albertel 55: # ------------------------------------ Read httpd access.conf and get variables
56:
1.29 harris41 57: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
1.1 albertel 58:
59: while ($configline=<CONFIG>) {
60: if ($configline =~ /PerlSetVar/) {
61: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
1.7 www 62: chomp($varvalue);
1.1 albertel 63: $perlvar{$varname}=$varvalue;
64: }
65: }
66: close(CONFIG);
1.19 www 67:
1.35 harris41 68: # ----------------------------- Make sure this process is running from user=www
69: my $wwwid=getpwnam('www');
70: if ($wwwid!=$<) {
71: $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
72: $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
1.37 harris41 73: system("echo 'User ID mismatch. lond must be run as user www.' |\
1.35 harris41 74: mailto $emailto -s '$subj' > /dev/null");
75: exit 1;
76: }
77:
1.19 www 78: # --------------------------------------------- Check if other instance running
79:
80: my $pidfile="$perlvar{'lonDaemons'}/logs/lond.pid";
81:
82: if (-e $pidfile) {
83: my $lfh=IO::File->new("$pidfile");
84: my $pide=<$lfh>;
85: chomp($pide);
1.29 harris41 86: if (kill 0 => $pide) { die "already running"; }
1.19 www 87: }
1.1 albertel 88:
89: $PREFORK=4; # number of children to maintain, at least four spare
90:
91: # ------------------------------------------------------------- Read hosts file
92:
1.29 harris41 93: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
1.1 albertel 94:
95: while ($configline=<CONFIG>) {
96: my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
97: chomp($ip);
98: $hostid{$ip}=$id;
99: if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
100: $PREFORK++;
101: }
102: close(CONFIG);
103:
104: # establish SERVER socket, bind and listen.
105: $server = IO::Socket::INET->new(LocalPort => $perlvar{'londPort'},
106: Type => SOCK_STREAM,
107: Proto => 'tcp',
108: Reuse => 1,
109: Listen => 10 )
1.29 harris41 110: or die "making socket: $@\n";
1.1 albertel 111:
112: # --------------------------------------------------------- Do global variables
113:
114: # global variables
115:
116: $MAX_CLIENTS_PER_CHILD = 5; # number of clients each child should
117: # process
118: %children = (); # keys are current child process IDs
119: $children = 0; # current number of children
120:
121: sub REAPER { # takes care of dead children
122: $SIG{CHLD} = \&REAPER;
123: my $pid = wait;
124: $children --;
125: &logthis("Child $pid died");
126: delete $children{$pid};
127: }
128:
129: sub HUNTSMAN { # signal handler for SIGINT
130: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
131: kill 'INT' => keys %children;
132: my $execdir=$perlvar{'lonDaemons'};
133: unlink("$execdir/logs/lond.pid");
1.9 www 134: &logthis("<font color=red>CRITICAL: Shutting down</font>");
1.1 albertel 135: exit; # clean up with dignity
136: }
137:
138: sub HUPSMAN { # signal handler for SIGHUP
139: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
140: kill 'INT' => keys %children;
141: close($server); # free up socket
1.9 www 142: &logthis("<font color=red>CRITICAL: Restarting</font>");
1.30 harris41 143: unlink("$execdir/logs/lond.pid");
1.1 albertel 144: my $execdir=$perlvar{'lonDaemons'};
145: exec("$execdir/lond"); # here we go again
146: }
147:
148: # --------------------------------------------------------------------- Logging
149:
150: sub logthis {
151: my $message=shift;
152: my $execdir=$perlvar{'lonDaemons'};
153: my $fh=IO::File->new(">>$execdir/logs/lond.log");
154: my $now=time;
155: my $local=localtime($now);
156: print $fh "$local ($$): $message\n";
157: }
158:
1.11 www 159:
160: # -------------------------------------------------------- Escape Special Chars
161:
162: sub escape {
163: my $str=shift;
164: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
165: return $str;
166: }
167:
168: # ----------------------------------------------------- Un-Escape Special Chars
169:
170: sub unescape {
171: my $str=shift;
172: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
173: return $str;
174: }
175:
1.1 albertel 176: # ----------------------------------------------------------- Send USR1 to lonc
177:
178: sub reconlonc {
179: my $peerfile=shift;
180: &logthis("Trying to reconnect for $peerfile");
181: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
182: if (my $fh=IO::File->new("$loncfile")) {
183: my $loncpid=<$fh>;
184: chomp($loncpid);
185: if (kill 0 => $loncpid) {
186: &logthis("lonc at pid $loncpid responding, sending USR1");
187: kill USR1 => $loncpid;
188: sleep 1;
189: if (-e "$peerfile") { return; }
190: &logthis("$peerfile still not there, give it another try");
191: sleep 5;
192: if (-e "$peerfile") { return; }
1.9 www 193: &logthis(
194: "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
1.1 albertel 195: } else {
1.9 www 196: &logthis(
197: "<font color=red>CRITICAL: "
198: ."lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 199: }
200: } else {
1.9 www 201: &logthis('<font color=red>CRITICAL: lonc not running, giving up</font>');
1.1 albertel 202: }
203: }
204:
205: # -------------------------------------------------- Non-critical communication
1.11 www 206:
1.1 albertel 207: sub subreply {
208: my ($cmd,$server)=@_;
209: my $peerfile="$perlvar{'lonSockDir'}/$server";
210: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
211: Type => SOCK_STREAM,
212: Timeout => 10)
213: or return "con_lost";
214: print $sclient "$cmd\n";
215: my $answer=<$sclient>;
216: chomp($answer);
217: if (!$answer) { $answer="con_lost"; }
218: return $answer;
219: }
220:
221: sub reply {
222: my ($cmd,$server)=@_;
223: my $answer;
224: if ($server ne $perlvar{'lonHostID'}) {
225: $answer=subreply($cmd,$server);
226: if ($answer eq 'con_lost') {
227: $answer=subreply("ping",$server);
228: if ($answer ne $server) {
229: &reconlonc("$perlvar{'lonSockDir'}/$server");
230: }
231: $answer=subreply($cmd,$server);
232: }
233: } else {
234: $answer='self_reply';
235: }
236: return $answer;
237: }
238:
1.13 www 239: # -------------------------------------------------------------- Talk to lonsql
240:
1.12 harris41 241: sub sqlreply {
242: my ($cmd)=@_;
243: my $answer=subsqlreply($cmd);
244: if ($answer eq 'con_lost') { $answer=subsqlreply($cmd); }
245: return $answer;
246: }
247:
248: sub subsqlreply {
249: my ($cmd)=@_;
250: my $unixsock="mysqlsock";
251: my $peerfile="$perlvar{'lonSockDir'}/$unixsock";
252: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
253: Type => SOCK_STREAM,
254: Timeout => 10)
255: or return "con_lost";
256: print $sclient "$cmd\n";
257: my $answer=<$sclient>;
258: chomp($answer);
259: if (!$answer) { $answer="con_lost"; }
260: return $answer;
261: }
262:
1.1 albertel 263: # -------------------------------------------- Return path to profile directory
1.11 www 264:
1.1 albertel 265: sub propath {
266: my ($udom,$uname)=@_;
267: $udom=~s/\W//g;
268: $uname=~s/\W//g;
1.16 www 269: my $subdir=$uname.'__';
1.1 albertel 270: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
271: my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
272: return $proname;
273: }
274:
275: # --------------------------------------- Is this the home server of an author?
1.11 www 276:
1.1 albertel 277: sub ishome {
278: my $author=shift;
279: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
280: my ($udom,$uname)=split(/\//,$author);
281: my $proname=propath($udom,$uname);
282: if (-e $proname) {
283: return 'owner';
284: } else {
285: return 'not_owner';
286: }
287: }
288:
289: # ======================================================= Continue main program
290: # ---------------------------------------------------- Fork once and dissociate
291:
292: $fpid=fork;
293: exit if $fpid;
1.29 harris41 294: die "Couldn't fork: $!" unless defined ($fpid);
1.1 albertel 295:
1.29 harris41 296: POSIX::setsid() or die "Can't start new session: $!";
1.1 albertel 297:
298: # ------------------------------------------------------- Write our PID on disk
299:
300: $execdir=$perlvar{'lonDaemons'};
301: open (PIDSAVE,">$execdir/logs/lond.pid");
302: print PIDSAVE "$$\n";
303: close(PIDSAVE);
1.9 www 304: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
1.1 albertel 305:
306: # ------------------------------------------------------- Now we are on our own
307:
308: # Fork off our children.
309: for (1 .. $PREFORK) {
310: make_new_child();
311: }
312:
313: # ----------------------------------------------------- Install signal handlers
314:
315: $SIG{CHLD} = \&REAPER;
316: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
317: $SIG{HUP} = \&HUPSMAN;
318:
319: # And maintain the population.
320: while (1) {
321: sleep; # wait for a signal (i.e., child's death)
322: for ($i = $children; $i < $PREFORK; $i++) {
323: make_new_child(); # top up the child pool
324: }
325: }
326:
327: sub make_new_child {
328: my $pid;
329: my $cipher;
330: my $sigset;
331: &logthis("Attempting to start child");
332: # block signal for fork
333: $sigset = POSIX::SigSet->new(SIGINT);
334: sigprocmask(SIG_BLOCK, $sigset)
1.29 harris41 335: or die "Can't block SIGINT for fork: $!\n";
1.1 albertel 336:
1.29 harris41 337: die "fork: $!" unless defined ($pid = fork);
1.1 albertel 338:
339: if ($pid) {
340: # Parent records the child's birth and returns.
341: sigprocmask(SIG_UNBLOCK, $sigset)
1.29 harris41 342: or die "Can't unblock SIGINT for fork: $!\n";
1.1 albertel 343: $children{$pid} = 1;
344: $children++;
345: return;
346: } else {
347: # Child can *not* return from this subroutine.
348: $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before
349:
350: # unblock signals
351: sigprocmask(SIG_UNBLOCK, $sigset)
1.29 harris41 352: or die "Can't unblock SIGINT for fork: $!\n";
1.13 www 353:
354: $tmpsnum=0;
1.1 albertel 355:
356: # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
357: for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
358: $client = $server->accept() or last;
359:
360: # =============================================================================
361: # do something with the connection
362: # -----------------------------------------------------------------------------
1.2 www 363: # see if we know client and check for spoof IP by challenge
1.1 albertel 364: my $caller=getpeername($client);
365: my ($port,$iaddr)=unpack_sockaddr_in($caller);
366: my $clientip=inet_ntoa($iaddr);
367: my $clientrec=($hostid{$clientip} ne undef);
1.9 www 368: &logthis(
369: "<font color=yellow>INFO: Connect from $clientip ($hostid{$clientip})</font>");
1.2 www 370: my $clientok;
1.1 albertel 371: if ($clientrec) {
1.2 www 372: my $remotereq=<$client>;
373: $remotereq=~s/\W//g;
374: if ($remotereq eq 'init') {
375: my $challenge="$$".time;
376: print $client "$challenge\n";
377: $remotereq=<$client>;
378: $remotereq=~s/\W//g;
379: if ($challenge eq $remotereq) {
380: $clientok=1;
381: print $client "ok\n";
382: } else {
1.9 www 383: &logthis(
384: "<font color=blue>WARNING: $clientip did not reply challenge</font>");
1.16 www 385: print $client "bye\n";
1.2 www 386: }
387: } else {
1.9 www 388: &logthis(
389: "<font color=blue>WARNING: "
390: ."$clientip failed to initialize: >$remotereq< </font>");
1.16 www 391: print $client "bye\n";
1.2 www 392: }
393: } else {
1.9 www 394: &logthis(
395: "<font color=blue>WARNING: Unknown client $clientip</font>");
1.16 www 396: print $client "bye\n";
1.2 www 397: }
398: if ($clientok) {
1.1 albertel 399: # ---------------- New known client connecting, could mean machine online again
400: &reconlonc("$perlvar{'lonSockDir'}/$hostid{$clientip}");
1.9 www 401: &logthis(
402: "<font color=green>Established connection: $hostid{$clientip}</font>");
1.1 albertel 403: # ------------------------------------------------------------ Process requests
404: while (my $userinput=<$client>) {
405: chomp($userinput);
406: my $wasenc=0;
407: # ------------------------------------------------------------ See if encrypted
408: if ($userinput =~ /^enc/) {
409: if ($cipher) {
410: my ($cmd,$cmdlength,$encinput)=split(/:/,$userinput);
411: $userinput='';
412: for (my $encidx=0;$encidx<length($encinput);$encidx+=16) {
413: $userinput.=
414: $cipher->decrypt(
415: pack("H16",substr($encinput,$encidx,16))
416: );
417: }
418: $userinput=substr($userinput,0,$cmdlength);
419: $wasenc=1;
420: }
421: }
422: # ------------------------------------------------------------- Normal commands
423: # ------------------------------------------------------------------------ ping
424: if ($userinput =~ /^ping/) {
425: print $client "$perlvar{'lonHostID'}\n";
426: # ------------------------------------------------------------------------ pong
427: } elsif ($userinput =~ /^pong/) {
428: $reply=reply("ping",$hostid{$clientip});
429: print $client "$perlvar{'lonHostID'}:$reply\n";
430: # ------------------------------------------------------------------------ ekey
431: } elsif ($userinput =~ /^ekey/) {
432: my $buildkey=time.$$.int(rand 100000);
433: $buildkey=~tr/1-6/A-F/;
434: $buildkey=int(rand 100000).$buildkey.int(rand 100000);
435: my $key=$perlvar{'lonHostID'}.$hostid{$clientip};
436: $key=~tr/a-z/A-Z/;
437: $key=~tr/G-P/0-9/;
438: $key=~tr/Q-Z/0-9/;
439: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
440: $key=substr($key,0,32);
441: my $cipherkey=pack("H32",$key);
442: $cipher=new IDEA $cipherkey;
443: print $client "$buildkey\n";
444: # ------------------------------------------------------------------------ load
445: } elsif ($userinput =~ /^load/) {
446: my $loadavg;
447: {
448: my $loadfile=IO::File->new('/proc/loadavg');
449: $loadavg=<$loadfile>;
450: }
451: $loadavg =~ s/\s.*//g;
452: my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
453: print $client "$loadpercent\n";
454: # ------------------------------------------------------------------------ auth
455: } elsif ($userinput =~ /^auth/) {
456: if ($wasenc==1) {
457: my ($cmd,$udom,$uname,$upass)=split(/:/,$userinput);
458: chomp($upass);
1.11 www 459: $upass=unescape($upass);
1.1 albertel 460: my $proname=propath($udom,$uname);
461: my $passfilename="$proname/passwd";
462: if (-e $passfilename) {
463: my $pf = IO::File->new($passfilename);
464: my $realpasswd=<$pf>;
465: chomp($realpasswd);
1.2 www 466: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
467: my $pwdcorrect=0;
468: if ($howpwd eq 'internal') {
469: $pwdcorrect=
470: (crypt($upass,$contentpwd) eq $contentpwd);
471: } elsif ($howpwd eq 'unix') {
472: $contentpwd=(getpwnam($uname))[1];
473: $pwdcorrect=
474: (crypt($upass,$contentpwd) eq $contentpwd);
1.3 www 475: } elsif ($howpwd eq 'krb4') {
476: $pwdcorrect=(
477: Authen::Krb4::get_pw_in_tkt($uname,"",
478: $contentpwd,'krbtgt',$contentpwd,1,
479: $upass) == 0);
1.50 ! albertel 480: } elsif ($howpwd eq 'localauth') {
1.49 albertel 481: $pwdcorrect=&localauth::localauth($uname,$upass,
482: $contentpwd);
483: }
1.2 www 484: if ($pwdcorrect) {
1.1 albertel 485: print $client "authorized\n";
486: } else {
487: print $client "non_authorized\n";
488: }
489: } else {
490: print $client "unknown_user\n";
491: }
492: } else {
493: print $client "refused\n";
494: }
495: # ---------------------------------------------------------------------- passwd
496: } elsif ($userinput =~ /^passwd/) {
497: if ($wasenc==1) {
498: my
499: ($cmd,$udom,$uname,$upass,$npass)=split(/:/,$userinput);
500: chomp($npass);
1.32 www 501: $upass=&unescape($upass);
502: $npass=&unescape($npass);
1.1 albertel 503: my $proname=propath($udom,$uname);
504: my $passfilename="$proname/passwd";
505: if (-e $passfilename) {
506: my $realpasswd;
507: { my $pf = IO::File->new($passfilename);
508: $realpasswd=<$pf>; }
509: chomp($realpasswd);
1.2 www 510: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
511: if ($howpwd eq 'internal') {
512: if (crypt($upass,$contentpwd) eq $contentpwd) {
513: my $salt=time;
514: $salt=substr($salt,6,2);
515: my $ncpass=crypt($npass,$salt);
1.1 albertel 516: { my $pf = IO::File->new(">$passfilename");
1.31 www 517: print $pf "internal:$ncpass\n"; }
1.1 albertel 518: print $client "ok\n";
1.2 www 519: } else {
520: print $client "non_authorized\n";
521: }
1.1 albertel 522: } else {
1.2 www 523: print $client "auth_mode_error\n";
1.1 albertel 524: }
525: } else {
526: print $client "unknown_user\n";
1.31 www 527: }
528: } else {
529: print $client "refused\n";
530: }
531: # -------------------------------------------------------------------- makeuser
532: } elsif ($userinput =~ /^makeuser/) {
533: if ($wasenc==1) {
534: my
535: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
536: chomp($npass);
1.32 www 537: $npass=&unescape($npass);
1.31 www 538: my $proname=propath($udom,$uname);
539: my $passfilename="$proname/passwd";
540: if (-e $passfilename) {
541: print $client "already_exists\n";
542: } elsif ($udom ne $perlvar{'lonDefDomain'}) {
543: print $client "not_right_domain\n";
544: } else {
545: @fpparts=split(/\//,$proname);
546: $fpnow=$fpparts[0].'/'.$fpparts[1].'/'.$fpparts[2];
547: $fperror='';
548: for ($i=3;$i<=$#fpparts;$i++) {
549: $fpnow.='/'.$fpparts[$i];
550: unless (-e $fpnow) {
551: unless (mkdir($fpnow,0777)) {
552: $fperror="error:$!\n";
553: }
554: }
555: }
556: unless ($fperror) {
1.34 www 557: if ($umode eq 'krb4') {
1.31 www 558: {
559: my $pf = IO::File->new(">$passfilename");
1.33 www 560: print $pf "krb4:$npass\n";
1.31 www 561: }
562: print $client "ok\n";
563: } elsif ($umode eq 'internal') {
564: my $salt=time;
565: $salt=substr($salt,6,2);
566: my $ncpass=crypt($npass,$salt);
567: {
568: my $pf = IO::File->new(">$passfilename");
569: print $pf "internal:$ncpass\n";
1.50 ! albertel 570: }
1.31 www 571: print $client "ok\n";
1.50 ! albertel 572: } elsif ($umode eq 'localauth') {
! 573: {
! 574: my $pf = IO::File->new(">$passfilename");
! 575: print $pf "localauth:$npass\n";
! 576: }
! 577: print $client "ok\n";
1.31 www 578: } elsif ($umode eq 'none') {
579: {
580: my $pf = IO::File->new(">$passfilename");
581: print $pf "none:\n";
582: }
583: print $client "ok\n";
584: } else {
585: print $client "auth_mode_error\n";
586: }
587: } else {
588: print $client "$fperror\n";
589: }
1.1 albertel 590: }
591: } else {
592: print $client "refused\n";
593: }
594: # ------------------------------------------------------------------------ home
595: } elsif ($userinput =~ /^home/) {
596: my ($cmd,$udom,$uname)=split(/:/,$userinput);
597: chomp($uname);
598: my $proname=propath($udom,$uname);
599: if (-e $proname) {
600: print $client "found\n";
601: } else {
602: print $client "not_found\n";
603: }
604: # ---------------------------------------------------------------------- update
605: } elsif ($userinput =~ /^update/) {
606: my ($cmd,$fname)=split(/:/,$userinput);
607: my $ownership=ishome($fname);
608: if ($ownership eq 'not_owner') {
609: if (-e $fname) {
610: my ($dev,$ino,$mode,$nlink,
611: $uid,$gid,$rdev,$size,
612: $atime,$mtime,$ctime,
613: $blksize,$blocks)=stat($fname);
614: $now=time;
615: $since=$now-$atime;
616: if ($since>$perlvar{'lonExpire'}) {
617: $reply=
618: reply("unsub:$fname","$hostid{$clientip}");
619: unlink("$fname");
620: } else {
621: my $transname="$fname.in.transfer";
622: my $remoteurl=
623: reply("sub:$fname","$hostid{$clientip}");
624: my $response;
625: {
626: my $ua=new LWP::UserAgent;
627: my $request=new HTTP::Request('GET',"$remoteurl");
628: $response=$ua->request($request,$transname);
629: }
630: if ($response->is_error()) {
1.24 albertel 631: unlink($transname);
1.1 albertel 632: my $message=$response->status_line;
633: &logthis(
634: "LWP GET: $message for $fname ($remoteurl)");
635: } else {
1.14 www 636: if ($remoteurl!~/\.meta$/) {
1.28 www 637: my $ua=new LWP::UserAgent;
1.14 www 638: my $mrequest=
639: new HTTP::Request('GET',$remoteurl.'.meta');
640: my $mresponse=
641: $ua->request($mrequest,$fname.'.meta');
642: if ($mresponse->is_error()) {
643: unlink($fname.'.meta');
644: }
645: }
1.1 albertel 646: rename($transname,$fname);
647: }
648: }
649: print $client "ok\n";
650: } else {
651: print $client "not_found\n";
652: }
653: } else {
654: print $client "rejected\n";
655: }
656: # ----------------------------------------------------------------- unsubscribe
657: } elsif ($userinput =~ /^unsub/) {
658: my ($cmd,$fname)=split(/:/,$userinput);
659: if (-e $fname) {
660: if (unlink("$fname.$hostid{$clientip}")) {
661: print $client "ok\n";
662: } else {
663: print $client "not_subscribed\n";
664: }
665: } else {
666: print $client "not_found\n";
667: }
668: # ------------------------------------------------------------------- subscribe
669: } elsif ($userinput =~ /^sub/) {
670: my ($cmd,$fname)=split(/:/,$userinput);
671: my $ownership=ishome($fname);
672: if ($ownership eq 'owner') {
673: if (-e $fname) {
1.18 www 674: if (-d $fname) {
675: print $client "directory\n";
676: } else {
1.1 albertel 677: $now=time;
678: {
1.26 www 679: my $sh;
1.25 www 680: if ($sh=
681: IO::File->new(">$fname.$hostid{$clientip}")) {
682: print $sh "$clientip:$now\n";
683: }
1.1 albertel 684: }
1.42 www 685: unless ($fname=~/\.meta$/) {
686: unlink("$fname.meta.$hostid{$clientip}");
687: }
1.1 albertel 688: $fname=~s/\/home\/httpd\/html\/res/raw/;
689: $fname="http://$thisserver/".$fname;
690: print $client "$fname\n";
1.18 www 691: }
1.1 albertel 692: } else {
693: print $client "not_found\n";
694: }
695: } else {
696: print $client "rejected\n";
697: }
1.12 harris41 698: # ------------------------------------------------------------------------- log
699: } elsif ($userinput =~ /^log/) {
700: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
701: chomp($what);
702: my $proname=propath($udom,$uname);
703: my $now=time;
704: {
705: my $hfh;
706: if ($hfh=IO::File->new(">>$proname/activity.log")) {
707: print $hfh "$now:$hostid{$clientip}:$what\n";
708: print $client "ok\n";
709: } else {
710: print $client "error:$!\n";
711: }
712: }
1.1 albertel 713: # ------------------------------------------------------------------------- put
714: } elsif ($userinput =~ /^put/) {
1.6 www 715: my ($cmd,$udom,$uname,$namespace,$what)
1.1 albertel 716: =split(/:/,$userinput);
1.8 www 717: $namespace=~s/\//\_/g;
1.6 www 718: $namespace=~s/\W//g;
719: if ($namespace ne 'roles') {
1.1 albertel 720: chomp($what);
721: my $proname=propath($udom,$uname);
722: my $now=time;
1.48 www 723: unless ($namespace=~/^nohist\_/) {
1.1 albertel 724: my $hfh;
725: if (
726: $hfh=IO::File->new(">>$proname/$namespace.hist")
727: ) { print $hfh "P:$now:$what\n"; }
728: }
729: my @pairs=split(/\&/,$what);
1.4 www 730: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 731: foreach $pair (@pairs) {
732: ($key,$value)=split(/=/,$pair);
733: $hash{$key}=$value;
734: }
1.4 www 735: if (untie(%hash)) {
1.1 albertel 736: print $client "ok\n";
737: } else {
738: print $client "error:$!\n";
739: }
740: } else {
741: print $client "error:$!\n";
742: }
1.6 www 743: } else {
744: print $client "refused\n";
745: }
746: # -------------------------------------------------------------------- rolesput
747: } elsif ($userinput =~ /^rolesput/) {
748: if ($wasenc==1) {
749: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
750: =split(/:/,$userinput);
751: my $namespace='roles';
752: chomp($what);
753: my $proname=propath($udom,$uname);
754: my $now=time;
755: {
756: my $hfh;
757: if (
758: $hfh=IO::File->new(">>$proname/$namespace.hist")
759: ) {
760: print $hfh "P:$now:$exedom:$exeuser:$what\n";
761: }
762: }
763: my @pairs=split(/\&/,$what);
764: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
765: foreach $pair (@pairs) {
766: ($key,$value)=split(/=/,$pair);
767: $hash{$key}=$value;
768: }
769: if (untie(%hash)) {
770: print $client "ok\n";
771: } else {
772: print $client "error:$!\n";
773: }
774: } else {
775: print $client "error:$!\n";
776: }
777: } else {
778: print $client "refused\n";
779: }
1.1 albertel 780: # ------------------------------------------------------------------------- get
781: } elsif ($userinput =~ /^get/) {
782: my ($cmd,$udom,$uname,$namespace,$what)
783: =split(/:/,$userinput);
1.8 www 784: $namespace=~s/\//\_/g;
1.1 albertel 785: $namespace=~s/\W//g;
786: chomp($what);
787: my @queries=split(/\&/,$what);
788: my $proname=propath($udom,$uname);
789: my $qresult='';
1.20 www 790: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 791: for ($i=0;$i<=$#queries;$i++) {
792: $qresult.="$hash{$queries[$i]}&";
793: }
1.4 www 794: if (untie(%hash)) {
1.1 albertel 795: $qresult=~s/\&$//;
796: print $client "$qresult\n";
797: } else {
798: print $client "error:$!\n";
799: }
800: } else {
801: print $client "error:$!\n";
802: }
803: # ------------------------------------------------------------------------ eget
804: } elsif ($userinput =~ /^eget/) {
805: my ($cmd,$udom,$uname,$namespace,$what)
806: =split(/:/,$userinput);
1.8 www 807: $namespace=~s/\//\_/g;
1.1 albertel 808: $namespace=~s/\W//g;
809: chomp($what);
810: my @queries=split(/\&/,$what);
811: my $proname=propath($udom,$uname);
812: my $qresult='';
1.20 www 813: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 814: for ($i=0;$i<=$#queries;$i++) {
815: $qresult.="$hash{$queries[$i]}&";
816: }
1.4 www 817: if (untie(%hash)) {
1.1 albertel 818: $qresult=~s/\&$//;
819: if ($cipher) {
820: my $cmdlength=length($qresult);
821: $qresult.=" ";
822: my $encqresult='';
823: for
824: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
825: $encqresult.=
826: unpack("H16",
827: $cipher->encrypt(substr($qresult,$encidx,8)));
828: }
829: print $client "enc:$cmdlength:$encqresult\n";
830: } else {
831: print $client "error:no_key\n";
832: }
833: } else {
834: print $client "error:$!\n";
835: }
836: } else {
837: print $client "error:$!\n";
838: }
839: # ------------------------------------------------------------------------- del
840: } elsif ($userinput =~ /^del/) {
841: my ($cmd,$udom,$uname,$namespace,$what)
842: =split(/:/,$userinput);
1.8 www 843: $namespace=~s/\//\_/g;
1.1 albertel 844: $namespace=~s/\W//g;
845: chomp($what);
846: my $proname=propath($udom,$uname);
847: my $now=time;
1.48 www 848: unless ($namespace=~/^nohist\_/) {
1.1 albertel 849: my $hfh;
850: if (
851: $hfh=IO::File->new(">>$proname/$namespace.hist")
852: ) { print $hfh "D:$now:$what\n"; }
853: }
854: my @keys=split(/\&/,$what);
1.4 www 855: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 856: foreach $key (@keys) {
857: delete($hash{$key});
858: }
1.4 www 859: if (untie(%hash)) {
1.1 albertel 860: print $client "ok\n";
861: } else {
862: print $client "error:$!\n";
863: }
864: } else {
865: print $client "error:$!\n";
866: }
867: # ------------------------------------------------------------------------ keys
868: } elsif ($userinput =~ /^keys/) {
869: my ($cmd,$udom,$uname,$namespace)
870: =split(/:/,$userinput);
1.8 www 871: $namespace=~s/\//\_/g;
1.1 albertel 872: $namespace=~s/\W//g;
873: my $proname=propath($udom,$uname);
874: my $qresult='';
1.20 www 875: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 876: foreach $key (keys %hash) {
877: $qresult.="$key&";
878: }
1.4 www 879: if (untie(%hash)) {
1.1 albertel 880: $qresult=~s/\&$//;
881: print $client "$qresult\n";
882: } else {
883: print $client "error:$!\n";
884: }
885: } else {
886: print $client "error:$!\n";
887: }
888: # ------------------------------------------------------------------------ dump
889: } elsif ($userinput =~ /^dump/) {
890: my ($cmd,$udom,$uname,$namespace)
891: =split(/:/,$userinput);
1.8 www 892: $namespace=~s/\//\_/g;
1.1 albertel 893: $namespace=~s/\W//g;
894: my $proname=propath($udom,$uname);
895: my $qresult='';
1.20 www 896: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 897: foreach $key (keys %hash) {
898: $qresult.="$key=$hash{$key}&";
1.7 www 899: }
900: if (untie(%hash)) {
901: $qresult=~s/\&$//;
902: print $client "$qresult\n";
903: } else {
904: print $client "error:$!\n";
905: }
906: } else {
907: print $client "error:$!\n";
908: }
909: # ----------------------------------------------------------------------- store
910: } elsif ($userinput =~ /^store/) {
911: my ($cmd,$udom,$uname,$namespace,$rid,$what)
912: =split(/:/,$userinput);
1.8 www 913: $namespace=~s/\//\_/g;
1.7 www 914: $namespace=~s/\W//g;
915: if ($namespace ne 'roles') {
916: chomp($what);
917: my $proname=propath($udom,$uname);
918: my $now=time;
1.48 www 919: unless ($namespace=~/^nohist\_/) {
1.7 www 920: my $hfh;
921: if (
922: $hfh=IO::File->new(">>$proname/$namespace.hist")
923: ) { print $hfh "P:$now:$rid:$what\n"; }
924: }
925: my @pairs=split(/\&/,$what);
926:
927: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
928: my @previouskeys=split(/&/,$hash{"keys:$rid"});
929: my $key;
930: $hash{"version:$rid"}++;
931: my $version=$hash{"version:$rid"};
932: my $allkeys='';
933: foreach $pair (@pairs) {
934: ($key,$value)=split(/=/,$pair);
935: $allkeys.=$key.':';
936: $hash{"$version:$rid:$key"}=$value;
937: }
1.36 www 938: $hash{"$version:$rid:timestamp"}=$now;
939: $allkeys.='timestamp';
1.7 www 940: $hash{"$version:keys:$rid"}=$allkeys;
941: if (untie(%hash)) {
942: print $client "ok\n";
943: } else {
944: print $client "error:$!\n";
945: }
946: } else {
947: print $client "error:$!\n";
948: }
949: } else {
950: print $client "refused\n";
951: }
952: # --------------------------------------------------------------------- restore
953: } elsif ($userinput =~ /^restore/) {
954: my ($cmd,$udom,$uname,$namespace,$rid)
955: =split(/:/,$userinput);
1.8 www 956: $namespace=~s/\//\_/g;
1.7 www 957: $namespace=~s/\W//g;
958: chomp($rid);
959: my $proname=propath($udom,$uname);
960: my $qresult='';
1.20 www 961: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.7 www 962: my $version=$hash{"version:$rid"};
963: $qresult.="version=$version&";
964: my $scope;
965: for ($scope=1;$scope<=$version;$scope++) {
966: my $vkeys=$hash{"$scope:keys:$rid"};
967: my @keys=split(/:/,$vkeys);
968: my $key;
969: $qresult.="$scope:keys=$vkeys&";
970: foreach $key (@keys) {
1.21 www 971: $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
1.7 www 972: }
1.1 albertel 973: }
1.4 www 974: if (untie(%hash)) {
1.1 albertel 975: $qresult=~s/\&$//;
976: print $client "$qresult\n";
977: } else {
978: print $client "error:$!\n";
979: }
980: } else {
981: print $client "error:$!\n";
982: }
1.12 harris41 983: # ------------------------------------------------------------------- querysend
984: } elsif ($userinput =~ /^querysend/) {
1.44 harris41 985: my ($cmd,$query,
986: $custom,$customshow)=split(/:/,$userinput);
1.12 harris41 987: $query=~s/\n*$//g;
1.45 harris41 988: unless ($custom or $customshow) {
1.40 harris41 989: print $client "".
990: sqlreply("$hostid{$clientip}\&$query")."\n";
991: }
992: else {
993: print $client "".
994: sqlreply("$hostid{$clientip}\&$query".
1.44 harris41 995: "\&$custom"."\&$customshow")."\n";
1.40 harris41 996: }
1.12 harris41 997: # ------------------------------------------------------------------ queryreply
998: } elsif ($userinput =~ /^queryreply/) {
999: my ($cmd,$id,$reply)=split(/:/,$userinput);
1000: my $store;
1.13 www 1001: my $execdir=$perlvar{'lonDaemons'};
1002: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.43 harris41 1003: $reply=~s/\&/\n/g;
1.12 harris41 1004: print $store $reply;
1005: close $store;
1.46 harris41 1006: my $store2=IO::File->new(">$execdir/tmp/$id.end");
1007: print $store2 "done\n";
1008: close $store2;
1.12 harris41 1009: print $client "ok\n";
1010: }
1011: else {
1012: print $client "error:$!\n";
1013: }
1.1 albertel 1014: # ----------------------------------------------------------------------- idput
1015: } elsif ($userinput =~ /^idput/) {
1016: my ($cmd,$udom,$what)=split(/:/,$userinput);
1017: chomp($what);
1018: $udom=~s/\W//g;
1019: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1020: my $now=time;
1021: {
1022: my $hfh;
1023: if (
1024: $hfh=IO::File->new(">>$proname.hist")
1025: ) { print $hfh "P:$now:$what\n"; }
1026: }
1027: my @pairs=split(/\&/,$what);
1.4 www 1028: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1029: foreach $pair (@pairs) {
1030: ($key,$value)=split(/=/,$pair);
1031: $hash{$key}=$value;
1032: }
1.4 www 1033: if (untie(%hash)) {
1.1 albertel 1034: print $client "ok\n";
1035: } else {
1036: print $client "error:$!\n";
1037: }
1038: } else {
1039: print $client "error:$!\n";
1040: }
1041: # ----------------------------------------------------------------------- idget
1042: } elsif ($userinput =~ /^idget/) {
1043: my ($cmd,$udom,$what)=split(/:/,$userinput);
1044: chomp($what);
1045: $udom=~s/\W//g;
1046: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1047: my @queries=split(/\&/,$what);
1048: my $qresult='';
1.20 www 1049: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER,0640)) {
1.1 albertel 1050: for ($i=0;$i<=$#queries;$i++) {
1051: $qresult.="$hash{$queries[$i]}&";
1052: }
1.4 www 1053: if (untie(%hash)) {
1.1 albertel 1054: $qresult=~s/\&$//;
1055: print $client "$qresult\n";
1056: } else {
1057: print $client "error:$!\n";
1058: }
1059: } else {
1060: print $client "error:$!\n";
1061: }
1.13 www 1062: # ---------------------------------------------------------------------- tmpput
1063: } elsif ($userinput =~ /^tmpput/) {
1064: my ($cmd,$what)=split(/:/,$userinput);
1065: my $store;
1066: $tmpsnum++;
1067: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
1068: $id=~s/\W/\_/g;
1069: $what=~s/\n//g;
1070: my $execdir=$perlvar{'lonDaemons'};
1071: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1072: print $store $what;
1073: close $store;
1074: print $client "$id\n";
1075: }
1076: else {
1077: print $client "error:$!\n";
1078: }
1079:
1080: # ---------------------------------------------------------------------- tmpget
1081: } elsif ($userinput =~ /^tmpget/) {
1082: my ($cmd,$id)=split(/:/,$userinput);
1083: chomp($id);
1084: $id=~s/\W/\_/g;
1085: my $store;
1086: my $execdir=$perlvar{'lonDaemons'};
1087: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
1088: my $reply=<$store>;
1089: print $client "$reply\n";
1090: close $store;
1091: }
1092: else {
1093: print $client "error:$!\n";
1094: }
1095:
1.5 www 1096: # -------------------------------------------------------------------------- ls
1097: } elsif ($userinput =~ /^ls/) {
1098: my ($cmd,$ulsdir)=split(/:/,$userinput);
1099: my $ulsout='';
1100: my $ulsfn;
1101: if (-e $ulsdir) {
1.41 www 1102: if (opendir(LSDIR,$ulsdir)) {
1103: while ($ulsfn=readdir(LSDIR)) {
1.47 www 1104: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1.5 www 1105: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1106: }
1.41 www 1107: closedir(LSDIR);
1108: }
1.5 www 1109: } else {
1110: $ulsout='no_such_dir';
1111: }
1.17 www 1112: if ($ulsout eq '') { $ulsout='empty'; }
1.5 www 1113: print $client "$ulsout\n";
1.1 albertel 1114: # ------------------------------------------------------------- unknown command
1115: } else {
1116: # unknown command
1117: print $client "unknown_cmd\n";
1118: }
1119: # ------------------------------------------------------ client unknown, refuse
1120: }
1121: } else {
1122: print $client "refused\n";
1.9 www 1123: &logthis("<font color=blue>WARNING: "
1124: ."Rejected client $clientip, closing connection</font>");
1.1 albertel 1125: }
1.9 www 1126: &logthis("<font color=red>CRITICAL: "
1.10 www 1127: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.1 albertel 1128: # =============================================================================
1129: }
1130:
1131: # tidy up gracefully and finish
1132:
1133: # this exit is VERY important, otherwise the child will become
1134: # a producer of more and more children, forking yourself into
1135: # process death.
1136: exit;
1137: }
1138: }
1139:
1140:
1141:
1142:
1143:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>