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