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