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