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