Annotation of loncom/lond, revision 1.41
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: }
672: $fname=~s/\/home\/httpd\/html\/res/raw/;
673: $fname="http://$thisserver/".$fname;
674: print $client "$fname\n";
1.18 www 675: }
1.1 albertel 676: } else {
677: print $client "not_found\n";
678: }
679: } else {
680: print $client "rejected\n";
681: }
1.12 harris41 682: # ------------------------------------------------------------------------- log
683: } elsif ($userinput =~ /^log/) {
684: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
685: chomp($what);
686: my $proname=propath($udom,$uname);
687: my $now=time;
688: {
689: my $hfh;
690: if ($hfh=IO::File->new(">>$proname/activity.log")) {
691: print $hfh "$now:$hostid{$clientip}:$what\n";
692: print $client "ok\n";
693: } else {
694: print $client "error:$!\n";
695: }
696: }
1.1 albertel 697: # ------------------------------------------------------------------------- put
698: } elsif ($userinput =~ /^put/) {
1.6 www 699: my ($cmd,$udom,$uname,$namespace,$what)
1.1 albertel 700: =split(/:/,$userinput);
1.8 www 701: $namespace=~s/\//\_/g;
1.6 www 702: $namespace=~s/\W//g;
703: if ($namespace ne 'roles') {
1.1 albertel 704: chomp($what);
705: my $proname=propath($udom,$uname);
706: my $now=time;
707: {
708: my $hfh;
709: if (
710: $hfh=IO::File->new(">>$proname/$namespace.hist")
711: ) { print $hfh "P:$now:$what\n"; }
712: }
713: my @pairs=split(/\&/,$what);
1.4 www 714: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 715: foreach $pair (@pairs) {
716: ($key,$value)=split(/=/,$pair);
717: $hash{$key}=$value;
718: }
1.4 www 719: if (untie(%hash)) {
1.1 albertel 720: print $client "ok\n";
721: } else {
722: print $client "error:$!\n";
723: }
724: } else {
725: print $client "error:$!\n";
726: }
1.6 www 727: } else {
728: print $client "refused\n";
729: }
730: # -------------------------------------------------------------------- rolesput
731: } elsif ($userinput =~ /^rolesput/) {
732: if ($wasenc==1) {
733: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
734: =split(/:/,$userinput);
735: my $namespace='roles';
736: chomp($what);
737: my $proname=propath($udom,$uname);
738: my $now=time;
739: {
740: my $hfh;
741: if (
742: $hfh=IO::File->new(">>$proname/$namespace.hist")
743: ) {
744: print $hfh "P:$now:$exedom:$exeuser:$what\n";
745: }
746: }
747: my @pairs=split(/\&/,$what);
748: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
749: foreach $pair (@pairs) {
750: ($key,$value)=split(/=/,$pair);
751: $hash{$key}=$value;
752: }
753: if (untie(%hash)) {
754: print $client "ok\n";
755: } else {
756: print $client "error:$!\n";
757: }
758: } else {
759: print $client "error:$!\n";
760: }
761: } else {
762: print $client "refused\n";
763: }
1.1 albertel 764: # ------------------------------------------------------------------------- get
765: } elsif ($userinput =~ /^get/) {
766: my ($cmd,$udom,$uname,$namespace,$what)
767: =split(/:/,$userinput);
1.8 www 768: $namespace=~s/\//\_/g;
1.1 albertel 769: $namespace=~s/\W//g;
770: chomp($what);
771: my @queries=split(/\&/,$what);
772: my $proname=propath($udom,$uname);
773: my $qresult='';
1.20 www 774: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 775: for ($i=0;$i<=$#queries;$i++) {
776: $qresult.="$hash{$queries[$i]}&";
777: }
1.4 www 778: if (untie(%hash)) {
1.1 albertel 779: $qresult=~s/\&$//;
780: print $client "$qresult\n";
781: } else {
782: print $client "error:$!\n";
783: }
784: } else {
785: print $client "error:$!\n";
786: }
787: # ------------------------------------------------------------------------ eget
788: } elsif ($userinput =~ /^eget/) {
789: my ($cmd,$udom,$uname,$namespace,$what)
790: =split(/:/,$userinput);
1.8 www 791: $namespace=~s/\//\_/g;
1.1 albertel 792: $namespace=~s/\W//g;
793: chomp($what);
794: my @queries=split(/\&/,$what);
795: my $proname=propath($udom,$uname);
796: my $qresult='';
1.20 www 797: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 798: for ($i=0;$i<=$#queries;$i++) {
799: $qresult.="$hash{$queries[$i]}&";
800: }
1.4 www 801: if (untie(%hash)) {
1.1 albertel 802: $qresult=~s/\&$//;
803: if ($cipher) {
804: my $cmdlength=length($qresult);
805: $qresult.=" ";
806: my $encqresult='';
807: for
808: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
809: $encqresult.=
810: unpack("H16",
811: $cipher->encrypt(substr($qresult,$encidx,8)));
812: }
813: print $client "enc:$cmdlength:$encqresult\n";
814: } else {
815: print $client "error:no_key\n";
816: }
817: } else {
818: print $client "error:$!\n";
819: }
820: } else {
821: print $client "error:$!\n";
822: }
823: # ------------------------------------------------------------------------- del
824: } elsif ($userinput =~ /^del/) {
825: my ($cmd,$udom,$uname,$namespace,$what)
826: =split(/:/,$userinput);
1.8 www 827: $namespace=~s/\//\_/g;
1.1 albertel 828: $namespace=~s/\W//g;
829: chomp($what);
830: my $proname=propath($udom,$uname);
831: my $now=time;
832: {
833: my $hfh;
834: if (
835: $hfh=IO::File->new(">>$proname/$namespace.hist")
836: ) { print $hfh "D:$now:$what\n"; }
837: }
838: my @keys=split(/\&/,$what);
1.4 www 839: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 840: foreach $key (@keys) {
841: delete($hash{$key});
842: }
1.4 www 843: if (untie(%hash)) {
1.1 albertel 844: print $client "ok\n";
845: } else {
846: print $client "error:$!\n";
847: }
848: } else {
849: print $client "error:$!\n";
850: }
851: # ------------------------------------------------------------------------ keys
852: } elsif ($userinput =~ /^keys/) {
853: my ($cmd,$udom,$uname,$namespace)
854: =split(/:/,$userinput);
1.8 www 855: $namespace=~s/\//\_/g;
1.1 albertel 856: $namespace=~s/\W//g;
857: my $proname=propath($udom,$uname);
858: my $qresult='';
1.20 www 859: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 860: foreach $key (keys %hash) {
861: $qresult.="$key&";
862: }
1.4 www 863: if (untie(%hash)) {
1.1 albertel 864: $qresult=~s/\&$//;
865: print $client "$qresult\n";
866: } else {
867: print $client "error:$!\n";
868: }
869: } else {
870: print $client "error:$!\n";
871: }
872: # ------------------------------------------------------------------------ dump
873: } elsif ($userinput =~ /^dump/) {
874: my ($cmd,$udom,$uname,$namespace)
875: =split(/:/,$userinput);
1.8 www 876: $namespace=~s/\//\_/g;
1.1 albertel 877: $namespace=~s/\W//g;
878: my $proname=propath($udom,$uname);
879: my $qresult='';
1.20 www 880: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 881: foreach $key (keys %hash) {
882: $qresult.="$key=$hash{$key}&";
1.7 www 883: }
884: if (untie(%hash)) {
885: $qresult=~s/\&$//;
886: print $client "$qresult\n";
887: } else {
888: print $client "error:$!\n";
889: }
890: } else {
891: print $client "error:$!\n";
892: }
893: # ----------------------------------------------------------------------- store
894: } elsif ($userinput =~ /^store/) {
895: my ($cmd,$udom,$uname,$namespace,$rid,$what)
896: =split(/:/,$userinput);
1.8 www 897: $namespace=~s/\//\_/g;
1.7 www 898: $namespace=~s/\W//g;
899: if ($namespace ne 'roles') {
900: chomp($what);
901: my $proname=propath($udom,$uname);
902: my $now=time;
903: {
904: my $hfh;
905: if (
906: $hfh=IO::File->new(">>$proname/$namespace.hist")
907: ) { print $hfh "P:$now:$rid:$what\n"; }
908: }
909: my @pairs=split(/\&/,$what);
910:
911: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
912: my @previouskeys=split(/&/,$hash{"keys:$rid"});
913: my $key;
914: $hash{"version:$rid"}++;
915: my $version=$hash{"version:$rid"};
916: my $allkeys='';
917: foreach $pair (@pairs) {
918: ($key,$value)=split(/=/,$pair);
919: $allkeys.=$key.':';
920: $hash{"$version:$rid:$key"}=$value;
921: }
1.36 www 922: $hash{"$version:$rid:timestamp"}=$now;
923: $allkeys.='timestamp';
1.7 www 924: $hash{"$version:keys:$rid"}=$allkeys;
925: if (untie(%hash)) {
926: print $client "ok\n";
927: } else {
928: print $client "error:$!\n";
929: }
930: } else {
931: print $client "error:$!\n";
932: }
933: } else {
934: print $client "refused\n";
935: }
936: # --------------------------------------------------------------------- restore
937: } elsif ($userinput =~ /^restore/) {
938: my ($cmd,$udom,$uname,$namespace,$rid)
939: =split(/:/,$userinput);
1.8 www 940: $namespace=~s/\//\_/g;
1.7 www 941: $namespace=~s/\W//g;
942: chomp($rid);
943: my $proname=propath($udom,$uname);
944: my $qresult='';
1.20 www 945: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.7 www 946: my $version=$hash{"version:$rid"};
947: $qresult.="version=$version&";
948: my $scope;
949: for ($scope=1;$scope<=$version;$scope++) {
950: my $vkeys=$hash{"$scope:keys:$rid"};
951: my @keys=split(/:/,$vkeys);
952: my $key;
953: $qresult.="$scope:keys=$vkeys&";
954: foreach $key (@keys) {
1.21 www 955: $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
1.7 www 956: }
1.1 albertel 957: }
1.4 www 958: if (untie(%hash)) {
1.1 albertel 959: $qresult=~s/\&$//;
960: print $client "$qresult\n";
961: } else {
962: print $client "error:$!\n";
963: }
964: } else {
965: print $client "error:$!\n";
966: }
1.12 harris41 967: # ------------------------------------------------------------------- querysend
968: } elsif ($userinput =~ /^querysend/) {
1.39 harris41 969: my ($cmd,$query,$custom)=split(/:/,$userinput);
1.12 harris41 970: $query=~s/\n*$//g;
1.40 harris41 971: unless ($custom) {
972: print $client "".
973: sqlreply("$hostid{$clientip}\&$query")."\n";
974: }
975: else {
976: print $client "".
977: sqlreply("$hostid{$clientip}\&$query".
978: "\&$custom")."\n";
979: }
1.12 harris41 980: # ------------------------------------------------------------------ queryreply
981: } elsif ($userinput =~ /^queryreply/) {
982: my ($cmd,$id,$reply)=split(/:/,$userinput);
983: my $store;
1.13 www 984: my $execdir=$perlvar{'lonDaemons'};
985: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.12 harris41 986: print $store $reply;
987: close $store;
988: print $client "ok\n";
989: }
990: else {
991: print $client "error:$!\n";
992: }
1.1 albertel 993: # ----------------------------------------------------------------------- idput
994: } elsif ($userinput =~ /^idput/) {
995: my ($cmd,$udom,$what)=split(/:/,$userinput);
996: chomp($what);
997: $udom=~s/\W//g;
998: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
999: my $now=time;
1000: {
1001: my $hfh;
1002: if (
1003: $hfh=IO::File->new(">>$proname.hist")
1004: ) { print $hfh "P:$now:$what\n"; }
1005: }
1006: my @pairs=split(/\&/,$what);
1.4 www 1007: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1008: foreach $pair (@pairs) {
1009: ($key,$value)=split(/=/,$pair);
1010: $hash{$key}=$value;
1011: }
1.4 www 1012: if (untie(%hash)) {
1.1 albertel 1013: print $client "ok\n";
1014: } else {
1015: print $client "error:$!\n";
1016: }
1017: } else {
1018: print $client "error:$!\n";
1019: }
1020: # ----------------------------------------------------------------------- idget
1021: } elsif ($userinput =~ /^idget/) {
1022: my ($cmd,$udom,$what)=split(/:/,$userinput);
1023: chomp($what);
1024: $udom=~s/\W//g;
1025: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1026: my @queries=split(/\&/,$what);
1027: my $qresult='';
1.20 www 1028: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER,0640)) {
1.1 albertel 1029: for ($i=0;$i<=$#queries;$i++) {
1030: $qresult.="$hash{$queries[$i]}&";
1031: }
1.4 www 1032: if (untie(%hash)) {
1.1 albertel 1033: $qresult=~s/\&$//;
1034: print $client "$qresult\n";
1035: } else {
1036: print $client "error:$!\n";
1037: }
1038: } else {
1039: print $client "error:$!\n";
1040: }
1.13 www 1041: # ---------------------------------------------------------------------- tmpput
1042: } elsif ($userinput =~ /^tmpput/) {
1043: my ($cmd,$what)=split(/:/,$userinput);
1044: my $store;
1045: $tmpsnum++;
1046: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
1047: $id=~s/\W/\_/g;
1048: $what=~s/\n//g;
1049: my $execdir=$perlvar{'lonDaemons'};
1050: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1051: print $store $what;
1052: close $store;
1053: print $client "$id\n";
1054: }
1055: else {
1056: print $client "error:$!\n";
1057: }
1058:
1059: # ---------------------------------------------------------------------- tmpget
1060: } elsif ($userinput =~ /^tmpget/) {
1061: my ($cmd,$id)=split(/:/,$userinput);
1062: chomp($id);
1063: $id=~s/\W/\_/g;
1064: my $store;
1065: my $execdir=$perlvar{'lonDaemons'};
1066: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
1067: my $reply=<$store>;
1068: print $client "$reply\n";
1069: close $store;
1070: }
1071: else {
1072: print $client "error:$!\n";
1073: }
1074:
1.5 www 1075: # -------------------------------------------------------------------------- ls
1076: } elsif ($userinput =~ /^ls/) {
1077: my ($cmd,$ulsdir)=split(/:/,$userinput);
1078: my $ulsout='';
1079: my $ulsfn;
1080: if (-e $ulsdir) {
1.41 ! www 1081: if (opendir(LSDIR,$ulsdir)) {
! 1082: while ($ulsfn=readdir(LSDIR)) {
1.5 www 1083: my @ulsstats=stat($ulsfn);
1084: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1085: }
1.41 ! www 1086: closedir(LSDIR);
! 1087: }
1.5 www 1088: } else {
1089: $ulsout='no_such_dir';
1090: }
1.17 www 1091: if ($ulsout eq '') { $ulsout='empty'; }
1.5 www 1092: print $client "$ulsout\n";
1.1 albertel 1093: # ------------------------------------------------------------- unknown command
1094: } else {
1095: # unknown command
1096: print $client "unknown_cmd\n";
1097: }
1098: # ------------------------------------------------------ client unknown, refuse
1099: }
1100: } else {
1101: print $client "refused\n";
1.9 www 1102: &logthis("<font color=blue>WARNING: "
1103: ."Rejected client $clientip, closing connection</font>");
1.1 albertel 1104: }
1.9 www 1105: &logthis("<font color=red>CRITICAL: "
1.10 www 1106: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.1 albertel 1107: # =============================================================================
1108: }
1109:
1110: # tidy up gracefully and finish
1111:
1112: # this exit is VERY important, otherwise the child will become
1113: # a producer of more and more children, forking yourself into
1114: # process death.
1115: exit;
1116: }
1117: }
1118:
1119:
1120:
1121:
1122:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>