Annotation of loncom/lond, revision 1.13
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
! 10: # 06/29 Gerd Kortemeyer
! 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;
220: my $subdir=$uname;
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.2 www 336: }
337: } else {
1.9 www 338: &logthis(
339: "<font color=blue>WARNING: "
340: ."$clientip failed to initialize: >$remotereq< </font>");
1.2 www 341: }
342: } else {
1.9 www 343: &logthis(
344: "<font color=blue>WARNING: Unknown client $clientip</font>");
1.2 www 345: }
346: if ($clientok) {
1.1 albertel 347: # ---------------- New known client connecting, could mean machine online again
348: &reconlonc("$perlvar{'lonSockDir'}/$hostid{$clientip}");
1.9 www 349: &logthis(
350: "<font color=green>Established connection: $hostid{$clientip}</font>");
1.1 albertel 351: # ------------------------------------------------------------ Process requests
352: while (my $userinput=<$client>) {
353: chomp($userinput);
354: my $wasenc=0;
355: # ------------------------------------------------------------ See if encrypted
356: if ($userinput =~ /^enc/) {
357: if ($cipher) {
358: my ($cmd,$cmdlength,$encinput)=split(/:/,$userinput);
359: $userinput='';
360: for (my $encidx=0;$encidx<length($encinput);$encidx+=16) {
361: $userinput.=
362: $cipher->decrypt(
363: pack("H16",substr($encinput,$encidx,16))
364: );
365: }
366: $userinput=substr($userinput,0,$cmdlength);
367: $wasenc=1;
368: }
369: }
370: # ------------------------------------------------------------- Normal commands
371: # ------------------------------------------------------------------------ ping
372: if ($userinput =~ /^ping/) {
373: print $client "$perlvar{'lonHostID'}\n";
374: # ------------------------------------------------------------------------ pong
375: } elsif ($userinput =~ /^pong/) {
376: $reply=reply("ping",$hostid{$clientip});
377: print $client "$perlvar{'lonHostID'}:$reply\n";
378: # ------------------------------------------------------------------------ ekey
379: } elsif ($userinput =~ /^ekey/) {
380: my $buildkey=time.$$.int(rand 100000);
381: $buildkey=~tr/1-6/A-F/;
382: $buildkey=int(rand 100000).$buildkey.int(rand 100000);
383: my $key=$perlvar{'lonHostID'}.$hostid{$clientip};
384: $key=~tr/a-z/A-Z/;
385: $key=~tr/G-P/0-9/;
386: $key=~tr/Q-Z/0-9/;
387: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
388: $key=substr($key,0,32);
389: my $cipherkey=pack("H32",$key);
390: $cipher=new IDEA $cipherkey;
391: print $client "$buildkey\n";
392: # ------------------------------------------------------------------------ load
393: } elsif ($userinput =~ /^load/) {
394: my $loadavg;
395: {
396: my $loadfile=IO::File->new('/proc/loadavg');
397: $loadavg=<$loadfile>;
398: }
399: $loadavg =~ s/\s.*//g;
400: my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
401: print $client "$loadpercent\n";
402: # ------------------------------------------------------------------------ auth
403: } elsif ($userinput =~ /^auth/) {
404: if ($wasenc==1) {
405: my ($cmd,$udom,$uname,$upass)=split(/:/,$userinput);
406: chomp($upass);
1.11 www 407: $upass=unescape($upass);
1.1 albertel 408: my $proname=propath($udom,$uname);
409: my $passfilename="$proname/passwd";
410: if (-e $passfilename) {
411: my $pf = IO::File->new($passfilename);
412: my $realpasswd=<$pf>;
413: chomp($realpasswd);
1.2 www 414: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
415: my $pwdcorrect=0;
416: if ($howpwd eq 'internal') {
417: $pwdcorrect=
418: (crypt($upass,$contentpwd) eq $contentpwd);
419: } elsif ($howpwd eq 'unix') {
420: $contentpwd=(getpwnam($uname))[1];
421: $pwdcorrect=
422: (crypt($upass,$contentpwd) eq $contentpwd);
1.3 www 423: } elsif ($howpwd eq 'krb4') {
424: $pwdcorrect=(
425: Authen::Krb4::get_pw_in_tkt($uname,"",
426: $contentpwd,'krbtgt',$contentpwd,1,
427: $upass) == 0);
1.2 www 428: }
429: if ($pwdcorrect) {
1.1 albertel 430: print $client "authorized\n";
431: } else {
432: print $client "non_authorized\n";
433: }
434: } else {
435: print $client "unknown_user\n";
436: }
437: } else {
438: print $client "refused\n";
439: }
440: # ---------------------------------------------------------------------- passwd
441: } elsif ($userinput =~ /^passwd/) {
442: if ($wasenc==1) {
443: my
444: ($cmd,$udom,$uname,$upass,$npass)=split(/:/,$userinput);
445: chomp($npass);
446: my $proname=propath($udom,$uname);
447: my $passfilename="$proname/passwd";
448: if (-e $passfilename) {
449: my $realpasswd;
450: { my $pf = IO::File->new($passfilename);
451: $realpasswd=<$pf>; }
452: chomp($realpasswd);
1.2 www 453: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
454: if ($howpwd eq 'internal') {
455: if (crypt($upass,$contentpwd) eq $contentpwd) {
456: my $salt=time;
457: $salt=substr($salt,6,2);
458: my $ncpass=crypt($npass,$salt);
1.1 albertel 459: { my $pf = IO::File->new(">$passfilename");
1.2 www 460: print $pf "internal:$ncpass\n";; }
1.1 albertel 461: print $client "ok\n";
1.2 www 462: } else {
463: print $client "non_authorized\n";
464: }
1.1 albertel 465: } else {
1.2 www 466: print $client "auth_mode_error\n";
1.1 albertel 467: }
468: } else {
469: print $client "unknown_user\n";
470: }
471: } else {
472: print $client "refused\n";
473: }
474: # ------------------------------------------------------------------------ home
475: } elsif ($userinput =~ /^home/) {
476: my ($cmd,$udom,$uname)=split(/:/,$userinput);
477: chomp($uname);
478: my $proname=propath($udom,$uname);
479: if (-e $proname) {
480: print $client "found\n";
481: } else {
482: print $client "not_found\n";
483: }
484: # ---------------------------------------------------------------------- update
485: } elsif ($userinput =~ /^update/) {
486: my ($cmd,$fname)=split(/:/,$userinput);
487: my $ownership=ishome($fname);
488: if ($ownership eq 'not_owner') {
489: if (-e $fname) {
490: my ($dev,$ino,$mode,$nlink,
491: $uid,$gid,$rdev,$size,
492: $atime,$mtime,$ctime,
493: $blksize,$blocks)=stat($fname);
494: $now=time;
495: $since=$now-$atime;
496: if ($since>$perlvar{'lonExpire'}) {
497: $reply=
498: reply("unsub:$fname","$hostid{$clientip}");
499: unlink("$fname");
500: } else {
501: my $transname="$fname.in.transfer";
502: my $remoteurl=
503: reply("sub:$fname","$hostid{$clientip}");
504: my $response;
505: {
506: my $ua=new LWP::UserAgent;
507: my $request=new HTTP::Request('GET',"$remoteurl");
508: $response=$ua->request($request,$transname);
509: }
510: if ($response->is_error()) {
511: unline($transname);
512: my $message=$response->status_line;
513: &logthis(
514: "LWP GET: $message for $fname ($remoteurl)");
515: } else {
516: rename($transname,$fname);
517: }
518: }
519: print $client "ok\n";
520: } else {
521: print $client "not_found\n";
522: }
523: } else {
524: print $client "rejected\n";
525: }
526: # ----------------------------------------------------------------- unsubscribe
527: } elsif ($userinput =~ /^unsub/) {
528: my ($cmd,$fname)=split(/:/,$userinput);
529: if (-e $fname) {
530: if (unlink("$fname.$hostid{$clientip}")) {
531: print $client "ok\n";
532: } else {
533: print $client "not_subscribed\n";
534: }
535: } else {
536: print $client "not_found\n";
537: }
538: # ------------------------------------------------------------------- subscribe
539: } elsif ($userinput =~ /^sub/) {
540: my ($cmd,$fname)=split(/:/,$userinput);
541: my $ownership=ishome($fname);
542: if ($ownership eq 'owner') {
543: if (-e $fname) {
544: $now=time;
545: {
546: my $sh=IO::File->new(">$fname.$hostid{$clientip}");
547: print $sh "$clientip:$now\n";
548: }
549: $fname=~s/\/home\/httpd\/html\/res/raw/;
550: $fname="http://$thisserver/".$fname;
551: print $client "$fname\n";
552: } else {
553: print $client "not_found\n";
554: }
555: } else {
556: print $client "rejected\n";
557: }
1.12 harris41 558: # ------------------------------------------------------------------------- log
559: } elsif ($userinput =~ /^log/) {
560: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
561: chomp($what);
562: my $proname=propath($udom,$uname);
563: my $now=time;
564: {
565: my $hfh;
566: if ($hfh=IO::File->new(">>$proname/activity.log")) {
567: print $hfh "$now:$hostid{$clientip}:$what\n";
568: print $client "ok\n";
569: } else {
570: print $client "error:$!\n";
571: }
572: }
1.1 albertel 573: # ------------------------------------------------------------------------- put
574: } elsif ($userinput =~ /^put/) {
1.6 www 575: my ($cmd,$udom,$uname,$namespace,$what)
1.1 albertel 576: =split(/:/,$userinput);
1.8 www 577: $namespace=~s/\//\_/g;
1.6 www 578: $namespace=~s/\W//g;
579: if ($namespace ne 'roles') {
1.1 albertel 580: chomp($what);
581: my $proname=propath($udom,$uname);
582: my $now=time;
583: {
584: my $hfh;
585: if (
586: $hfh=IO::File->new(">>$proname/$namespace.hist")
587: ) { print $hfh "P:$now:$what\n"; }
588: }
589: my @pairs=split(/\&/,$what);
1.4 www 590: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 591: foreach $pair (@pairs) {
592: ($key,$value)=split(/=/,$pair);
593: $hash{$key}=$value;
594: }
1.4 www 595: if (untie(%hash)) {
1.1 albertel 596: print $client "ok\n";
597: } else {
598: print $client "error:$!\n";
599: }
600: } else {
601: print $client "error:$!\n";
602: }
1.6 www 603: } else {
604: print $client "refused\n";
605: }
606: # -------------------------------------------------------------------- rolesput
607: } elsif ($userinput =~ /^rolesput/) {
608: if ($wasenc==1) {
609: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
610: =split(/:/,$userinput);
611: my $namespace='roles';
612: chomp($what);
613: my $proname=propath($udom,$uname);
614: my $now=time;
615: {
616: my $hfh;
617: if (
618: $hfh=IO::File->new(">>$proname/$namespace.hist")
619: ) {
620: print $hfh "P:$now:$exedom:$exeuser:$what\n";
621: }
622: }
623: my @pairs=split(/\&/,$what);
624: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
625: foreach $pair (@pairs) {
626: ($key,$value)=split(/=/,$pair);
627: $hash{$key}=$value;
628: }
629: if (untie(%hash)) {
630: print $client "ok\n";
631: } else {
632: print $client "error:$!\n";
633: }
634: } else {
635: print $client "error:$!\n";
636: }
637: } else {
638: print $client "refused\n";
639: }
1.1 albertel 640: # ------------------------------------------------------------------------- get
641: } elsif ($userinput =~ /^get/) {
642: my ($cmd,$udom,$uname,$namespace,$what)
643: =split(/:/,$userinput);
1.8 www 644: $namespace=~s/\//\_/g;
1.1 albertel 645: $namespace=~s/\W//g;
646: chomp($what);
647: my @queries=split(/\&/,$what);
648: my $proname=propath($udom,$uname);
649: my $qresult='';
1.4 www 650: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 651: for ($i=0;$i<=$#queries;$i++) {
652: $qresult.="$hash{$queries[$i]}&";
653: }
1.4 www 654: if (untie(%hash)) {
1.1 albertel 655: $qresult=~s/\&$//;
656: print $client "$qresult\n";
657: } else {
658: print $client "error:$!\n";
659: }
660: } else {
661: print $client "error:$!\n";
662: }
663: # ------------------------------------------------------------------------ eget
664: } elsif ($userinput =~ /^eget/) {
665: my ($cmd,$udom,$uname,$namespace,$what)
666: =split(/:/,$userinput);
1.8 www 667: $namespace=~s/\//\_/g;
1.1 albertel 668: $namespace=~s/\W//g;
669: chomp($what);
670: my @queries=split(/\&/,$what);
671: my $proname=propath($udom,$uname);
672: my $qresult='';
1.4 www 673: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 674: for ($i=0;$i<=$#queries;$i++) {
675: $qresult.="$hash{$queries[$i]}&";
676: }
1.4 www 677: if (untie(%hash)) {
1.1 albertel 678: $qresult=~s/\&$//;
679: if ($cipher) {
680: my $cmdlength=length($qresult);
681: $qresult.=" ";
682: my $encqresult='';
683: for
684: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
685: $encqresult.=
686: unpack("H16",
687: $cipher->encrypt(substr($qresult,$encidx,8)));
688: }
689: print $client "enc:$cmdlength:$encqresult\n";
690: } else {
691: print $client "error:no_key\n";
692: }
693: } else {
694: print $client "error:$!\n";
695: }
696: } else {
697: print $client "error:$!\n";
698: }
699: # ------------------------------------------------------------------------- del
700: } elsif ($userinput =~ /^del/) {
701: my ($cmd,$udom,$uname,$namespace,$what)
702: =split(/:/,$userinput);
1.8 www 703: $namespace=~s/\//\_/g;
1.1 albertel 704: $namespace=~s/\W//g;
705: chomp($what);
706: my $proname=propath($udom,$uname);
707: my $now=time;
708: {
709: my $hfh;
710: if (
711: $hfh=IO::File->new(">>$proname/$namespace.hist")
712: ) { print $hfh "D:$now:$what\n"; }
713: }
714: my @keys=split(/\&/,$what);
1.4 www 715: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 716: foreach $key (@keys) {
717: delete($hash{$key});
718: }
1.4 www 719: if (untie(%hash)) {
1.1 albertel 720: print $client "ok\n";
721: } else {
722: print $client "error:$!\n";
723: }
724: } else {
725: print $client "error:$!\n";
726: }
727: # ------------------------------------------------------------------------ keys
728: } elsif ($userinput =~ /^keys/) {
729: my ($cmd,$udom,$uname,$namespace)
730: =split(/:/,$userinput);
1.8 www 731: $namespace=~s/\//\_/g;
1.1 albertel 732: $namespace=~s/\W//g;
733: my $proname=propath($udom,$uname);
734: my $qresult='';
1.4 www 735: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 736: foreach $key (keys %hash) {
737: $qresult.="$key&";
738: }
1.4 www 739: if (untie(%hash)) {
1.1 albertel 740: $qresult=~s/\&$//;
741: print $client "$qresult\n";
742: } else {
743: print $client "error:$!\n";
744: }
745: } else {
746: print $client "error:$!\n";
747: }
748: # ------------------------------------------------------------------------ dump
749: } elsif ($userinput =~ /^dump/) {
750: my ($cmd,$udom,$uname,$namespace)
751: =split(/:/,$userinput);
1.8 www 752: $namespace=~s/\//\_/g;
1.1 albertel 753: $namespace=~s/\W//g;
754: my $proname=propath($udom,$uname);
755: my $qresult='';
1.4 www 756: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 757: foreach $key (keys %hash) {
758: $qresult.="$key=$hash{$key}&";
1.7 www 759: }
760: if (untie(%hash)) {
761: $qresult=~s/\&$//;
762: print $client "$qresult\n";
763: } else {
764: print $client "error:$!\n";
765: }
766: } else {
767: print $client "error:$!\n";
768: }
769: # ----------------------------------------------------------------------- store
770: } elsif ($userinput =~ /^store/) {
771: my ($cmd,$udom,$uname,$namespace,$rid,$what)
772: =split(/:/,$userinput);
1.8 www 773: $namespace=~s/\//\_/g;
1.7 www 774: $namespace=~s/\W//g;
775: if ($namespace ne 'roles') {
776: chomp($what);
777: my $proname=propath($udom,$uname);
778: my $now=time;
779: {
780: my $hfh;
781: if (
782: $hfh=IO::File->new(">>$proname/$namespace.hist")
783: ) { print $hfh "P:$now:$rid:$what\n"; }
784: }
785: my @pairs=split(/\&/,$what);
786:
787: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
788: my @previouskeys=split(/&/,$hash{"keys:$rid"});
789: my $key;
790: $hash{"version:$rid"}++;
791: my $version=$hash{"version:$rid"};
792: my $allkeys='';
793: foreach $pair (@pairs) {
794: ($key,$value)=split(/=/,$pair);
795: $allkeys.=$key.':';
796: $hash{"$version:$rid:$key"}=$value;
797: }
798: $allkeys=~s/:$//;
799: $hash{"$version:keys:$rid"}=$allkeys;
800: if (untie(%hash)) {
801: print $client "ok\n";
802: } else {
803: print $client "error:$!\n";
804: }
805: } else {
806: print $client "error:$!\n";
807: }
808: } else {
809: print $client "refused\n";
810: }
811: # --------------------------------------------------------------------- restore
812: } elsif ($userinput =~ /^restore/) {
813: my ($cmd,$udom,$uname,$namespace,$rid)
814: =split(/:/,$userinput);
1.8 www 815: $namespace=~s/\//\_/g;
1.7 www 816: $namespace=~s/\W//g;
817: chomp($rid);
818: my $proname=propath($udom,$uname);
819: my $qresult='';
820: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
821: my $version=$hash{"version:$rid"};
822: $qresult.="version=$version&";
823: my $scope;
824: for ($scope=1;$scope<=$version;$scope++) {
825: my $vkeys=$hash{"$scope:keys:$rid"};
826: my @keys=split(/:/,$vkeys);
827: my $key;
828: $qresult.="$scope:keys=$vkeys&";
829: foreach $key (@keys) {
830: $qresult.="$version:$key=".$hash{"$scope:$rid:$key"}."&";
831: }
1.1 albertel 832: }
1.4 www 833: if (untie(%hash)) {
1.1 albertel 834: $qresult=~s/\&$//;
835: print $client "$qresult\n";
836: } else {
837: print $client "error:$!\n";
838: }
839: } else {
840: print $client "error:$!\n";
841: }
1.12 harris41 842: # ------------------------------------------------------------------- querysend
843: } elsif ($userinput =~ /^querysend/) {
844: my ($cmd,$query)=split(/:/,$userinput);
845: $query=~s/\n*$//g;
1.13 ! www 846: print $client sqlreply("$hostid{$clientip}\&$query")."\n";
1.12 harris41 847: # ------------------------------------------------------------------ queryreply
848: } elsif ($userinput =~ /^queryreply/) {
849: my ($cmd,$id,$reply)=split(/:/,$userinput);
850: my $store;
1.13 ! www 851: my $execdir=$perlvar{'lonDaemons'};
! 852: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.12 harris41 853: print $store $reply;
854: close $store;
855: print $client "ok\n";
856: }
857: else {
858: print $client "error:$!\n";
859: }
1.1 albertel 860: # ----------------------------------------------------------------------- idput
861: } elsif ($userinput =~ /^idput/) {
862: my ($cmd,$udom,$what)=split(/:/,$userinput);
863: chomp($what);
864: $udom=~s/\W//g;
865: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
866: my $now=time;
867: {
868: my $hfh;
869: if (
870: $hfh=IO::File->new(">>$proname.hist")
871: ) { print $hfh "P:$now:$what\n"; }
872: }
873: my @pairs=split(/\&/,$what);
1.4 www 874: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 875: foreach $pair (@pairs) {
876: ($key,$value)=split(/=/,$pair);
877: $hash{$key}=$value;
878: }
1.4 www 879: if (untie(%hash)) {
1.1 albertel 880: print $client "ok\n";
881: } else {
882: print $client "error:$!\n";
883: }
884: } else {
885: print $client "error:$!\n";
886: }
887: # ----------------------------------------------------------------------- idget
888: } elsif ($userinput =~ /^idget/) {
889: my ($cmd,$udom,$what)=split(/:/,$userinput);
890: chomp($what);
891: $udom=~s/\W//g;
892: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
893: my @queries=split(/\&/,$what);
894: my $qresult='';
1.4 www 895: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 896: for ($i=0;$i<=$#queries;$i++) {
897: $qresult.="$hash{$queries[$i]}&";
898: }
1.4 www 899: if (untie(%hash)) {
1.1 albertel 900: $qresult=~s/\&$//;
901: print $client "$qresult\n";
902: } else {
903: print $client "error:$!\n";
904: }
905: } else {
906: print $client "error:$!\n";
907: }
1.13 ! www 908: # ---------------------------------------------------------------------- tmpput
! 909: } elsif ($userinput =~ /^tmpput/) {
! 910: my ($cmd,$what)=split(/:/,$userinput);
! 911: my $store;
! 912: $tmpsnum++;
! 913: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
! 914: $id=~s/\W/\_/g;
! 915: $what=~s/\n//g;
! 916: my $execdir=$perlvar{'lonDaemons'};
! 917: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
! 918: print $store $what;
! 919: close $store;
! 920: print $client "$id\n";
! 921: }
! 922: else {
! 923: print $client "error:$!\n";
! 924: }
! 925:
! 926: # ---------------------------------------------------------------------- tmpget
! 927: } elsif ($userinput =~ /^tmpget/) {
! 928: my ($cmd,$id)=split(/:/,$userinput);
! 929: chomp($id);
! 930: $id=~s/\W/\_/g;
! 931: my $store;
! 932: my $execdir=$perlvar{'lonDaemons'};
! 933: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
! 934: my $reply=<$store>;
! 935: print $client "$reply\n";
! 936: close $store;
! 937: }
! 938: else {
! 939: print $client "error:$!\n";
! 940: }
! 941:
1.5 www 942: # -------------------------------------------------------------------------- ls
943: } elsif ($userinput =~ /^ls/) {
944: my ($cmd,$ulsdir)=split(/:/,$userinput);
945: my $ulsout='';
946: my $ulsfn;
947: if (-e $ulsdir) {
948: while ($ulsfn=<$ulsdir/*>) {
949: my @ulsstats=stat($ulsfn);
950: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
951: }
952: } else {
953: $ulsout='no_such_dir';
954: }
955: print $client "$ulsout\n";
1.1 albertel 956: # ------------------------------------------------------------- unknown command
957: } else {
958: # unknown command
959: print $client "unknown_cmd\n";
960: }
961: # ------------------------------------------------------ client unknown, refuse
962: }
963: } else {
964: print $client "refused\n";
1.9 www 965: &logthis("<font color=blue>WARNING: "
966: ."Rejected client $clientip, closing connection</font>");
1.1 albertel 967: }
1.9 www 968: &logthis("<font color=red>CRITICAL: "
1.10 www 969: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.1 albertel 970: # =============================================================================
971: }
972:
973: # tidy up gracefully and finish
974:
975: # this exit is VERY important, otherwise the child will become
976: # a producer of more and more children, forking yourself into
977: # process death.
978: exit;
979: }
980: }
981:
982:
983:
984:
985:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>