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