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