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