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