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