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