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