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