Annotation of loncom/lond, revision 1.18
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.18 ! www 10: # 06/29,06/30,07/14,07/15,07/17,07/20 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) {
1.18 ! www 556: if (-d $fname) {
! 557: print $client "directory\n";
! 558: } else {
1.1 albertel 559: $now=time;
560: {
561: my $sh=IO::File->new(">$fname.$hostid{$clientip}");
562: print $sh "$clientip:$now\n";
563: }
564: $fname=~s/\/home\/httpd\/html\/res/raw/;
565: $fname="http://$thisserver/".$fname;
566: print $client "$fname\n";
1.18 ! www 567: }
1.1 albertel 568: } else {
569: print $client "not_found\n";
570: }
571: } else {
572: print $client "rejected\n";
573: }
1.12 harris41 574: # ------------------------------------------------------------------------- log
575: } elsif ($userinput =~ /^log/) {
576: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
577: chomp($what);
578: my $proname=propath($udom,$uname);
579: my $now=time;
580: {
581: my $hfh;
582: if ($hfh=IO::File->new(">>$proname/activity.log")) {
583: print $hfh "$now:$hostid{$clientip}:$what\n";
584: print $client "ok\n";
585: } else {
586: print $client "error:$!\n";
587: }
588: }
1.1 albertel 589: # ------------------------------------------------------------------------- put
590: } elsif ($userinput =~ /^put/) {
1.6 www 591: my ($cmd,$udom,$uname,$namespace,$what)
1.1 albertel 592: =split(/:/,$userinput);
1.8 www 593: $namespace=~s/\//\_/g;
1.6 www 594: $namespace=~s/\W//g;
595: if ($namespace ne 'roles') {
1.1 albertel 596: chomp($what);
597: my $proname=propath($udom,$uname);
598: my $now=time;
599: {
600: my $hfh;
601: if (
602: $hfh=IO::File->new(">>$proname/$namespace.hist")
603: ) { print $hfh "P:$now:$what\n"; }
604: }
605: my @pairs=split(/\&/,$what);
1.4 www 606: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 607: foreach $pair (@pairs) {
608: ($key,$value)=split(/=/,$pair);
609: $hash{$key}=$value;
610: }
1.4 www 611: if (untie(%hash)) {
1.1 albertel 612: print $client "ok\n";
613: } else {
614: print $client "error:$!\n";
615: }
616: } else {
617: print $client "error:$!\n";
618: }
1.6 www 619: } else {
620: print $client "refused\n";
621: }
622: # -------------------------------------------------------------------- rolesput
623: } elsif ($userinput =~ /^rolesput/) {
624: if ($wasenc==1) {
625: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
626: =split(/:/,$userinput);
627: my $namespace='roles';
628: chomp($what);
629: my $proname=propath($udom,$uname);
630: my $now=time;
631: {
632: my $hfh;
633: if (
634: $hfh=IO::File->new(">>$proname/$namespace.hist")
635: ) {
636: print $hfh "P:$now:$exedom:$exeuser:$what\n";
637: }
638: }
639: my @pairs=split(/\&/,$what);
640: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
641: foreach $pair (@pairs) {
642: ($key,$value)=split(/=/,$pair);
643: $hash{$key}=$value;
644: }
645: if (untie(%hash)) {
646: print $client "ok\n";
647: } else {
648: print $client "error:$!\n";
649: }
650: } else {
651: print $client "error:$!\n";
652: }
653: } else {
654: print $client "refused\n";
655: }
1.1 albertel 656: # ------------------------------------------------------------------------- get
657: } elsif ($userinput =~ /^get/) {
658: my ($cmd,$udom,$uname,$namespace,$what)
659: =split(/:/,$userinput);
1.8 www 660: $namespace=~s/\//\_/g;
1.1 albertel 661: $namespace=~s/\W//g;
662: chomp($what);
663: my @queries=split(/\&/,$what);
664: my $proname=propath($udom,$uname);
665: my $qresult='';
1.4 www 666: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 667: for ($i=0;$i<=$#queries;$i++) {
668: $qresult.="$hash{$queries[$i]}&";
669: }
1.4 www 670: if (untie(%hash)) {
1.1 albertel 671: $qresult=~s/\&$//;
672: print $client "$qresult\n";
673: } else {
674: print $client "error:$!\n";
675: }
676: } else {
677: print $client "error:$!\n";
678: }
679: # ------------------------------------------------------------------------ eget
680: } elsif ($userinput =~ /^eget/) {
681: my ($cmd,$udom,$uname,$namespace,$what)
682: =split(/:/,$userinput);
1.8 www 683: $namespace=~s/\//\_/g;
1.1 albertel 684: $namespace=~s/\W//g;
685: chomp($what);
686: my @queries=split(/\&/,$what);
687: my $proname=propath($udom,$uname);
688: my $qresult='';
1.4 www 689: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 690: for ($i=0;$i<=$#queries;$i++) {
691: $qresult.="$hash{$queries[$i]}&";
692: }
1.4 www 693: if (untie(%hash)) {
1.1 albertel 694: $qresult=~s/\&$//;
695: if ($cipher) {
696: my $cmdlength=length($qresult);
697: $qresult.=" ";
698: my $encqresult='';
699: for
700: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
701: $encqresult.=
702: unpack("H16",
703: $cipher->encrypt(substr($qresult,$encidx,8)));
704: }
705: print $client "enc:$cmdlength:$encqresult\n";
706: } else {
707: print $client "error:no_key\n";
708: }
709: } else {
710: print $client "error:$!\n";
711: }
712: } else {
713: print $client "error:$!\n";
714: }
715: # ------------------------------------------------------------------------- del
716: } elsif ($userinput =~ /^del/) {
717: my ($cmd,$udom,$uname,$namespace,$what)
718: =split(/:/,$userinput);
1.8 www 719: $namespace=~s/\//\_/g;
1.1 albertel 720: $namespace=~s/\W//g;
721: chomp($what);
722: my $proname=propath($udom,$uname);
723: my $now=time;
724: {
725: my $hfh;
726: if (
727: $hfh=IO::File->new(">>$proname/$namespace.hist")
728: ) { print $hfh "D:$now:$what\n"; }
729: }
730: my @keys=split(/\&/,$what);
1.4 www 731: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 732: foreach $key (@keys) {
733: delete($hash{$key});
734: }
1.4 www 735: if (untie(%hash)) {
1.1 albertel 736: print $client "ok\n";
737: } else {
738: print $client "error:$!\n";
739: }
740: } else {
741: print $client "error:$!\n";
742: }
743: # ------------------------------------------------------------------------ keys
744: } elsif ($userinput =~ /^keys/) {
745: my ($cmd,$udom,$uname,$namespace)
746: =split(/:/,$userinput);
1.8 www 747: $namespace=~s/\//\_/g;
1.1 albertel 748: $namespace=~s/\W//g;
749: my $proname=propath($udom,$uname);
750: my $qresult='';
1.4 www 751: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 752: foreach $key (keys %hash) {
753: $qresult.="$key&";
754: }
1.4 www 755: if (untie(%hash)) {
1.1 albertel 756: $qresult=~s/\&$//;
757: print $client "$qresult\n";
758: } else {
759: print $client "error:$!\n";
760: }
761: } else {
762: print $client "error:$!\n";
763: }
764: # ------------------------------------------------------------------------ dump
765: } elsif ($userinput =~ /^dump/) {
766: my ($cmd,$udom,$uname,$namespace)
767: =split(/:/,$userinput);
1.8 www 768: $namespace=~s/\//\_/g;
1.1 albertel 769: $namespace=~s/\W//g;
770: my $proname=propath($udom,$uname);
771: my $qresult='';
1.4 www 772: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 773: foreach $key (keys %hash) {
774: $qresult.="$key=$hash{$key}&";
1.7 www 775: }
776: if (untie(%hash)) {
777: $qresult=~s/\&$//;
778: print $client "$qresult\n";
779: } else {
780: print $client "error:$!\n";
781: }
782: } else {
783: print $client "error:$!\n";
784: }
785: # ----------------------------------------------------------------------- store
786: } elsif ($userinput =~ /^store/) {
787: my ($cmd,$udom,$uname,$namespace,$rid,$what)
788: =split(/:/,$userinput);
1.8 www 789: $namespace=~s/\//\_/g;
1.7 www 790: $namespace=~s/\W//g;
791: if ($namespace ne 'roles') {
792: chomp($what);
793: my $proname=propath($udom,$uname);
794: my $now=time;
795: {
796: my $hfh;
797: if (
798: $hfh=IO::File->new(">>$proname/$namespace.hist")
799: ) { print $hfh "P:$now:$rid:$what\n"; }
800: }
801: my @pairs=split(/\&/,$what);
802:
803: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
804: my @previouskeys=split(/&/,$hash{"keys:$rid"});
805: my $key;
806: $hash{"version:$rid"}++;
807: my $version=$hash{"version:$rid"};
808: my $allkeys='';
809: foreach $pair (@pairs) {
810: ($key,$value)=split(/=/,$pair);
811: $allkeys.=$key.':';
812: $hash{"$version:$rid:$key"}=$value;
813: }
814: $allkeys=~s/:$//;
815: $hash{"$version:keys:$rid"}=$allkeys;
816: if (untie(%hash)) {
817: print $client "ok\n";
818: } else {
819: print $client "error:$!\n";
820: }
821: } else {
822: print $client "error:$!\n";
823: }
824: } else {
825: print $client "refused\n";
826: }
827: # --------------------------------------------------------------------- restore
828: } elsif ($userinput =~ /^restore/) {
829: my ($cmd,$udom,$uname,$namespace,$rid)
830: =split(/:/,$userinput);
1.8 www 831: $namespace=~s/\//\_/g;
1.7 www 832: $namespace=~s/\W//g;
833: chomp($rid);
834: my $proname=propath($udom,$uname);
835: my $qresult='';
836: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
837: my $version=$hash{"version:$rid"};
838: $qresult.="version=$version&";
839: my $scope;
840: for ($scope=1;$scope<=$version;$scope++) {
841: my $vkeys=$hash{"$scope:keys:$rid"};
842: my @keys=split(/:/,$vkeys);
843: my $key;
844: $qresult.="$scope:keys=$vkeys&";
845: foreach $key (@keys) {
846: $qresult.="$version:$key=".$hash{"$scope:$rid:$key"}."&";
847: }
1.1 albertel 848: }
1.4 www 849: if (untie(%hash)) {
1.1 albertel 850: $qresult=~s/\&$//;
851: print $client "$qresult\n";
852: } else {
853: print $client "error:$!\n";
854: }
855: } else {
856: print $client "error:$!\n";
857: }
1.12 harris41 858: # ------------------------------------------------------------------- querysend
859: } elsif ($userinput =~ /^querysend/) {
860: my ($cmd,$query)=split(/:/,$userinput);
861: $query=~s/\n*$//g;
1.13 www 862: print $client sqlreply("$hostid{$clientip}\&$query")."\n";
1.12 harris41 863: # ------------------------------------------------------------------ queryreply
864: } elsif ($userinput =~ /^queryreply/) {
865: my ($cmd,$id,$reply)=split(/:/,$userinput);
866: my $store;
1.13 www 867: my $execdir=$perlvar{'lonDaemons'};
868: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.12 harris41 869: print $store $reply;
870: close $store;
871: print $client "ok\n";
872: }
873: else {
874: print $client "error:$!\n";
875: }
1.1 albertel 876: # ----------------------------------------------------------------------- idput
877: } elsif ($userinput =~ /^idput/) {
878: my ($cmd,$udom,$what)=split(/:/,$userinput);
879: chomp($what);
880: $udom=~s/\W//g;
881: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
882: my $now=time;
883: {
884: my $hfh;
885: if (
886: $hfh=IO::File->new(">>$proname.hist")
887: ) { print $hfh "P:$now:$what\n"; }
888: }
889: my @pairs=split(/\&/,$what);
1.4 www 890: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 891: foreach $pair (@pairs) {
892: ($key,$value)=split(/=/,$pair);
893: $hash{$key}=$value;
894: }
1.4 www 895: if (untie(%hash)) {
1.1 albertel 896: print $client "ok\n";
897: } else {
898: print $client "error:$!\n";
899: }
900: } else {
901: print $client "error:$!\n";
902: }
903: # ----------------------------------------------------------------------- idget
904: } elsif ($userinput =~ /^idget/) {
905: my ($cmd,$udom,$what)=split(/:/,$userinput);
906: chomp($what);
907: $udom=~s/\W//g;
908: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
909: my @queries=split(/\&/,$what);
910: my $qresult='';
1.4 www 911: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 912: for ($i=0;$i<=$#queries;$i++) {
913: $qresult.="$hash{$queries[$i]}&";
914: }
1.4 www 915: if (untie(%hash)) {
1.1 albertel 916: $qresult=~s/\&$//;
917: print $client "$qresult\n";
918: } else {
919: print $client "error:$!\n";
920: }
921: } else {
922: print $client "error:$!\n";
923: }
1.13 www 924: # ---------------------------------------------------------------------- tmpput
925: } elsif ($userinput =~ /^tmpput/) {
926: my ($cmd,$what)=split(/:/,$userinput);
927: my $store;
928: $tmpsnum++;
929: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
930: $id=~s/\W/\_/g;
931: $what=~s/\n//g;
932: my $execdir=$perlvar{'lonDaemons'};
933: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
934: print $store $what;
935: close $store;
936: print $client "$id\n";
937: }
938: else {
939: print $client "error:$!\n";
940: }
941:
942: # ---------------------------------------------------------------------- tmpget
943: } elsif ($userinput =~ /^tmpget/) {
944: my ($cmd,$id)=split(/:/,$userinput);
945: chomp($id);
946: $id=~s/\W/\_/g;
947: my $store;
948: my $execdir=$perlvar{'lonDaemons'};
949: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
950: my $reply=<$store>;
951: print $client "$reply\n";
952: close $store;
953: }
954: else {
955: print $client "error:$!\n";
956: }
957:
1.5 www 958: # -------------------------------------------------------------------------- ls
959: } elsif ($userinput =~ /^ls/) {
960: my ($cmd,$ulsdir)=split(/:/,$userinput);
961: my $ulsout='';
962: my $ulsfn;
963: if (-e $ulsdir) {
964: while ($ulsfn=<$ulsdir/*>) {
965: my @ulsstats=stat($ulsfn);
966: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
967: }
968: } else {
969: $ulsout='no_such_dir';
970: }
1.17 www 971: if ($ulsout eq '') { $ulsout='empty'; }
1.5 www 972: print $client "$ulsout\n";
1.1 albertel 973: # ------------------------------------------------------------- unknown command
974: } else {
975: # unknown command
976: print $client "unknown_cmd\n";
977: }
978: # ------------------------------------------------------ client unknown, refuse
979: }
980: } else {
981: print $client "refused\n";
1.9 www 982: &logthis("<font color=blue>WARNING: "
983: ."Rejected client $clientip, closing connection</font>");
1.1 albertel 984: }
1.9 www 985: &logthis("<font color=red>CRITICAL: "
1.10 www 986: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.1 albertel 987: # =============================================================================
988: }
989:
990: # tidy up gracefully and finish
991:
992: # this exit is VERY important, otherwise the child will become
993: # a producer of more and more children, forking yourself into
994: # process death.
995: exit;
996: }
997: }
998:
999:
1000:
1001:
1002:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>