1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
4: #
5: # $Id: lond,v 1.79 2002/05/08 02:31:04 foxr Exp $
6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
28: #
29: # 5/26/99,6/4,6/10,6/11,6/14,6/15,6/26,6/28,6/30,
30: # 7/8,7/9,7/10,7/12,7/17,7/19,9/21,
31: # 10/7,10/8,10/9,10/11,10/13,10/15,11/4,11/16,
32: # 12/7,12/15,01/06,01/11,01/12,01/14,2/8,
33: # 03/07,05/31 Gerd Kortemeyer
34: # 06/26 Scott Harrison
35: # 06/29,06/30,07/14,07/15,07/17,07/20,07/25,09/18 Gerd Kortemeyer
36: # 12/05 Scott Harrison
37: # 12/05,12/13,12/29 Gerd Kortemeyer
38: # YEAR=2001
39: # Jan 01 Scott Harrison
40: # 02/12 Gerd Kortemeyer
41: # 03/15 Scott Harrison
42: # 03/24 Gerd Kortemeyer
43: # 04/02 Scott Harrison
44: # 05/11,05/28,08/30 Gerd Kortemeyer
45: # 9/30,10/22,11/13,11/15,11/16 Scott Harrison
46: # 11/26,11/27 Gerd Kortemeyer
47: # 12/20 Scott Harrison
48: # 12/22 Gerd Kortemeyer
49: # YEAR=2002
50: # 01/20/02,02/05 Gerd Kortemeyer
51: # 02/05 Guy Albertelli
52: # 02/07 Scott Harrison
53: # 02/12 Gerd Kortemeyer
54: # 02/19 Matthew Hall
55: # 02/25 Gerd Kortemeyer
56: ###
57:
58: # based on "Perl Cookbook" ISBN 1-56592-243-3
59: # preforker - server who forks first
60: # runs as a daemon
61: # HUPs
62: # uses IDEA encryption
63:
64: use IO::Socket;
65: use IO::File;
66: use Apache::File;
67: use Symbol;
68: use POSIX;
69: use Crypt::IDEA;
70: use LWP::UserAgent();
71: use GDBM_File;
72: use Authen::Krb4;
73: use lib '/home/httpd/lib/perl/';
74: use localauth;
75:
76: my $DEBUG = 0; # Non zero to enable debug log entries.
77:
78: my $status='';
79: my $lastlog='';
80:
81: # grabs exception and records it to log before exiting
82: sub catchexception {
83: my ($error)=@_;
84: $SIG{'QUIT'}='DEFAULT';
85: $SIG{__DIE__}='DEFAULT';
86: &logthis("<font color=red>CRITICAL: "
87: ."ABNORMAL EXIT. Child $$ for server $wasserver died through "
88: ."a crash with this error msg->[$error]</font>");
89: &logthis('Famous last words: '.$status.' - '.$lastlog);
90: if ($client) { print $client "error: $error\n"; }
91: $server->close();
92: die($error);
93: }
94:
95: sub timeout {
96: &logthis("<font color=ref>CRITICAL: TIME OUT ".$$."</font>");
97: &catchexception('Timeout');
98: }
99: # -------------------------------- Set signal handlers to record abnormal exits
100:
101: $SIG{'QUIT'}=\&catchexception;
102: $SIG{__DIE__}=\&catchexception;
103:
104: # ------------------------------------ Read httpd access.conf and get variables
105:
106: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
107:
108: while ($configline=<CONFIG>) {
109: if ($configline =~ /PerlSetVar/) {
110: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
111: chomp($varvalue);
112: $perlvar{$varname}=$varvalue;
113: }
114: }
115: close(CONFIG);
116:
117: # ----------------------------- Make sure this process is running from user=www
118: my $wwwid=getpwnam('www');
119: if ($wwwid!=$<) {
120: $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
121: $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
122: system("echo 'User ID mismatch. lond must be run as user www.' |\
123: mailto $emailto -s '$subj' > /dev/null");
124: exit 1;
125: }
126:
127: # --------------------------------------------- Check if other instance running
128:
129: my $pidfile="$perlvar{'lonDaemons'}/logs/lond.pid";
130:
131: if (-e $pidfile) {
132: my $lfh=IO::File->new("$pidfile");
133: my $pide=<$lfh>;
134: chomp($pide);
135: if (kill 0 => $pide) { die "already running"; }
136: }
137:
138: $PREFORK=4; # number of children to maintain, at least four spare
139:
140: # ------------------------------------------------------------- Read hosts file
141:
142: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
143:
144: while ($configline=<CONFIG>) {
145: my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
146: chomp($ip); $ip=~s/\D+$//;
147: $hostid{$ip}=$id;
148: if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
149: $PREFORK++;
150: }
151: close(CONFIG);
152:
153: # establish SERVER socket, bind and listen.
154: $server = IO::Socket::INET->new(LocalPort => $perlvar{'londPort'},
155: Type => SOCK_STREAM,
156: Proto => 'tcp',
157: Reuse => 1,
158: Listen => 10 )
159: or die "making socket: $@\n";
160:
161: # --------------------------------------------------------- Do global variables
162:
163: # global variables
164:
165: $MAX_CLIENTS_PER_CHILD = 50; # number of clients each child should
166: # process
167: %children = (); # keys are current child process IDs
168: $children = 0; # current number of children
169:
170: sub REAPER { # takes care of dead children
171: $SIG{CHLD} = \&REAPER;
172: my $pid = wait;
173: if (defined($children{$pid})) {
174: &logthis("Child $pid died");
175: $children --;
176: delete $children{$pid};
177: } else {
178: &logthis("Unknown Child $pid died");
179: }
180: }
181:
182: sub HUNTSMAN { # signal handler for SIGINT
183: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
184: kill 'INT' => keys %children;
185: &logthis("Free socket: ".shutdown($server,2)); # free up socket
186: my $execdir=$perlvar{'lonDaemons'};
187: unlink("$execdir/logs/lond.pid");
188: &logthis("<font color=red>CRITICAL: Shutting down</font>");
189: exit; # clean up with dignity
190: }
191:
192: sub HUPSMAN { # signal handler for SIGHUP
193: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
194: kill 'INT' => keys %children;
195: &logthis("Free socket: ".shutdown($server,2)); # free up socket
196: &logthis("<font color=red>CRITICAL: Restarting</font>");
197: unlink("$execdir/logs/lond.pid");
198: my $execdir=$perlvar{'lonDaemons'};
199: exec("$execdir/lond"); # here we go again
200: }
201:
202: sub checkchildren {
203: &initnewstatus();
204: &logstatus();
205: &logthis('Going to check on the children');
206: $docdir=$perlvar{'lonDocRoot'};
207: foreach (sort keys %children) {
208: sleep 1;
209: unless (kill 'USR1' => $_) {
210: &logthis ('Child '.$_.' is dead');
211: &logstatus($$.' is dead');
212: }
213: }
214: sleep 5;
215: foreach (sort keys %children) {
216: unless (-e "$docdir/lon-status/londchld/$_.txt") {
217: &logthis('Child '.$_.' did not respond');
218: kill 9 => $_;
219: $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
220: $subj="LON: $perlvar{'lonHostID'} killed lond process $_";
221: my $result=`echo 'Killed lond process $_.' | mailto $emailto -s '$subj' > /dev/null`;
222: $execdir=$perlvar{'lonDaemons'};
223: $result=`/bin/cp $execdir/logs/lond.log $execdir/logs/lond.log.$_`
224: }
225: }
226: }
227:
228: # --------------------------------------------------------------------- Logging
229:
230: sub logthis {
231: my $message=shift;
232: my $execdir=$perlvar{'lonDaemons'};
233: my $fh=IO::File->new(">>$execdir/logs/lond.log");
234: my $now=time;
235: my $local=localtime($now);
236: $lastlog=$local.': '.$message;
237: print $fh "$local ($$): $message\n";
238: }
239:
240: # ------------------------- Conditional log if $DEBUG true.
241: sub Debug {
242: my $message = shift;
243: if($DEBUG) {
244: &logthis($message);
245: }
246: }
247: # ------------------------------------------------------------------ Log status
248:
249: sub logstatus {
250: my $docdir=$perlvar{'lonDocRoot'};
251: {
252: my $fh=IO::File->new(">>$docdir/lon-status/londstatus.txt");
253: print $fh $$."\t".$status."\t".$lastlog."\n";
254: $fh->close();
255: }
256: {
257: my $fh=IO::File->new(">$docdir/lon-status/londchld/$$.txt");
258: print $fh $status."\n".$lastlog."\n".time;
259: $fh->close();
260: }
261: }
262:
263: sub initnewstatus {
264: my $docdir=$perlvar{'lonDocRoot'};
265: my $fh=IO::File->new(">$docdir/lon-status/londstatus.txt");
266: my $now=time;
267: my $local=localtime($now);
268: print $fh "LOND status $local - parent $$\n\n";
269: opendir(DIR,"$docdir/lon-status/londchld");
270: while ($filename=readdir(DIR)) {
271: unlink("$docdir/lon-status/londchld/$filename");
272: }
273: closedir(DIR);
274: }
275:
276: # -------------------------------------------------------------- Status setting
277:
278: sub status {
279: my $what=shift;
280: my $now=time;
281: my $local=localtime($now);
282: $status=$local.': '.$what;
283: }
284:
285: # -------------------------------------------------------- Escape Special Chars
286:
287: sub escape {
288: my $str=shift;
289: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
290: return $str;
291: }
292:
293: # ----------------------------------------------------- Un-Escape Special Chars
294:
295: sub unescape {
296: my $str=shift;
297: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
298: return $str;
299: }
300:
301: # ----------------------------------------------------------- Send USR1 to lonc
302:
303: sub reconlonc {
304: my $peerfile=shift;
305: &logthis("Trying to reconnect for $peerfile");
306: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
307: if (my $fh=IO::File->new("$loncfile")) {
308: my $loncpid=<$fh>;
309: chomp($loncpid);
310: if (kill 0 => $loncpid) {
311: &logthis("lonc at pid $loncpid responding, sending USR1");
312: kill USR1 => $loncpid;
313: sleep 5;
314: if (-e "$peerfile") { return; }
315: &logthis("$peerfile still not there, give it another try");
316: sleep 10;
317: if (-e "$peerfile") { return; }
318: &logthis(
319: "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
320: } else {
321: &logthis(
322: "<font color=red>CRITICAL: "
323: ."lonc at pid $loncpid not responding, giving up</font>");
324: }
325: } else {
326: &logthis('<font color=red>CRITICAL: lonc not running, giving up</font>');
327: }
328: }
329:
330: # -------------------------------------------------- Non-critical communication
331:
332: sub subreply {
333: my ($cmd,$server)=@_;
334: my $peerfile="$perlvar{'lonSockDir'}/$server";
335: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
336: Type => SOCK_STREAM,
337: Timeout => 10)
338: or return "con_lost";
339: print $sclient "$cmd\n";
340: my $answer=<$sclient>;
341: chomp($answer);
342: if (!$answer) { $answer="con_lost"; }
343: return $answer;
344: }
345:
346: sub reply {
347: my ($cmd,$server)=@_;
348: my $answer;
349: if ($server ne $perlvar{'lonHostID'}) {
350: $answer=subreply($cmd,$server);
351: if ($answer eq 'con_lost') {
352: $answer=subreply("ping",$server);
353: if ($answer ne $server) {
354: &logthis("sub reply: answer != server");
355: &reconlonc("$perlvar{'lonSockDir'}/$server");
356: }
357: $answer=subreply($cmd,$server);
358: }
359: } else {
360: $answer='self_reply';
361: }
362: return $answer;
363: }
364:
365: # -------------------------------------------------------------- Talk to lonsql
366:
367: sub sqlreply {
368: my ($cmd)=@_;
369: my $answer=subsqlreply($cmd);
370: if ($answer eq 'con_lost') { $answer=subsqlreply($cmd); }
371: return $answer;
372: }
373:
374: sub subsqlreply {
375: my ($cmd)=@_;
376: my $unixsock="mysqlsock";
377: my $peerfile="$perlvar{'lonSockDir'}/$unixsock";
378: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
379: Type => SOCK_STREAM,
380: Timeout => 10)
381: or return "con_lost";
382: print $sclient "$cmd\n";
383: my $answer=<$sclient>;
384: chomp($answer);
385: if (!$answer) { $answer="con_lost"; }
386: return $answer;
387: }
388:
389: # -------------------------------------------- Return path to profile directory
390:
391: sub propath {
392: my ($udom,$uname)=@_;
393: $udom=~s/\W//g;
394: $uname=~s/\W//g;
395: my $subdir=$uname.'__';
396: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
397: my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
398: return $proname;
399: }
400:
401: # --------------------------------------- Is this the home server of an author?
402:
403: sub ishome {
404: my $author=shift;
405: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
406: my ($udom,$uname)=split(/\//,$author);
407: my $proname=propath($udom,$uname);
408: if (-e $proname) {
409: return 'owner';
410: } else {
411: return 'not_owner';
412: }
413: }
414:
415: # ======================================================= Continue main program
416: # ---------------------------------------------------- Fork once and dissociate
417:
418: $fpid=fork;
419: exit if $fpid;
420: die "Couldn't fork: $!" unless defined ($fpid);
421:
422: POSIX::setsid() or die "Can't start new session: $!";
423:
424: # ------------------------------------------------------- Write our PID on disk
425:
426: $execdir=$perlvar{'lonDaemons'};
427: open (PIDSAVE,">$execdir/logs/lond.pid");
428: print PIDSAVE "$$\n";
429: close(PIDSAVE);
430: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
431: &status('Starting');
432:
433: # ------------------------------------------------------- Now we are on our own
434:
435: # Fork off our children.
436: for (1 .. $PREFORK) {
437: make_new_child();
438: }
439:
440: # ----------------------------------------------------- Install signal handlers
441:
442: &status('Forked children');
443:
444: $SIG{CHLD} = \&REAPER;
445: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
446: $SIG{HUP} = \&HUPSMAN;
447: $SIG{USR1} = \&checkchildren;
448:
449: # And maintain the population.
450: while (1) {
451: &status('Sleeping');
452: sleep; # wait for a signal (i.e., child's death)
453: &logthis('Woke up');
454: &status('Woke up');
455: for ($i = $children; $i < $PREFORK; $i++) {
456: make_new_child(); # top up the child pool
457: }
458: }
459:
460: sub make_new_child {
461: my $pid;
462: my $cipher;
463: my $sigset;
464: &logthis("Attempting to start child");
465: # block signal for fork
466: $sigset = POSIX::SigSet->new(SIGINT);
467: sigprocmask(SIG_BLOCK, $sigset)
468: or die "Can't block SIGINT for fork: $!\n";
469:
470: die "fork: $!" unless defined ($pid = fork);
471:
472: if ($pid) {
473: # Parent records the child's birth and returns.
474: sigprocmask(SIG_UNBLOCK, $sigset)
475: or die "Can't unblock SIGINT for fork: $!\n";
476: $children{$pid} = 1;
477: $children++;
478: &status('Started child '.$pid);
479: return;
480: } else {
481: # Child can *not* return from this subroutine.
482: $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before
483: $SIG{USR1}= \&logstatus;
484: $SIG{ALRM}= \&timeout;
485: $lastlog='Forked ';
486: $status='Forked';
487:
488: # unblock signals
489: sigprocmask(SIG_UNBLOCK, $sigset)
490: or die "Can't unblock SIGINT for fork: $!\n";
491:
492: $tmpsnum=0;
493:
494: # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
495: for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
496: &status('Idle, waiting for connection');
497: $client = $server->accept() or last;
498: &status('Accepted connection');
499: # =============================================================================
500: # do something with the connection
501: # -----------------------------------------------------------------------------
502: # see if we know client and check for spoof IP by challenge
503: my $caller=getpeername($client);
504: my ($port,$iaddr)=unpack_sockaddr_in($caller);
505: my $clientip=inet_ntoa($iaddr);
506: my $clientrec=($hostid{$clientip} ne undef);
507: &logthis(
508: "<font color=yellow>INFO: Connection $i, $clientip ($hostid{$clientip})</font>"
509: );
510: &status("Connecting $clientip ($hostid{$clientip})");
511: my $clientok;
512: if ($clientrec) {
513: &status("Waiting for init from $clientip ($hostid{$clientip})");
514: my $remotereq=<$client>;
515: $remotereq=~s/\W//g;
516: if ($remotereq eq 'init') {
517: my $challenge="$$".time;
518: print $client "$challenge\n";
519: &status(
520: "Waiting for challenge reply from $clientip ($hostid{$clientip})");
521: $remotereq=<$client>;
522: $remotereq=~s/\W//g;
523: if ($challenge eq $remotereq) {
524: $clientok=1;
525: print $client "ok\n";
526: } else {
527: &logthis(
528: "<font color=blue>WARNING: $clientip did not reply challenge</font>");
529: &status('No challenge reply '.$clientip);
530: }
531: } else {
532: &logthis(
533: "<font color=blue>WARNING: "
534: ."$clientip failed to initialize: >$remotereq< </font>");
535: &status('No init '.$clientip);
536: }
537: } else {
538: &logthis(
539: "<font color=blue>WARNING: Unknown client $clientip</font>");
540: &status('Hung up on '.$clientip);
541: }
542: if ($clientok) {
543: # ---------------- New known client connecting, could mean machine online again
544:
545: &reconlonc("$perlvar{'lonSockDir'}/$hostid{$clientip}");
546: &logthis(
547: "<font color=green>Established connection: $hostid{$clientip}</font>");
548: &status('Will listen to '.$hostid{$clientip});
549: # ------------------------------------------------------------ Process requests
550: while (my $userinput=<$client>) {
551: chomp($userinput);
552: Debug("Request = $userinput\n");
553: &status('Processing '.$hostid{$clientip}.': '.$userinput);
554: my $wasenc=0;
555: alarm(120);
556: # ------------------------------------------------------------ See if encrypted
557: if ($userinput =~ /^enc/) {
558: if ($cipher) {
559: my ($cmd,$cmdlength,$encinput)=split(/:/,$userinput);
560: $userinput='';
561: for (my $encidx=0;$encidx<length($encinput);$encidx+=16) {
562: $userinput.=
563: $cipher->decrypt(
564: pack("H16",substr($encinput,$encidx,16))
565: );
566: }
567: $userinput=substr($userinput,0,$cmdlength);
568: $wasenc=1;
569: }
570: }
571:
572: # ------------------------------------------------------------- Normal commands
573: # ------------------------------------------------------------------------ ping
574: if ($userinput =~ /^ping/) {
575: print $client "$perlvar{'lonHostID'}\n";
576: # ------------------------------------------------------------------------ pong
577: } elsif ($userinput =~ /^pong/) {
578: $reply=reply("ping",$hostid{$clientip});
579: print $client "$perlvar{'lonHostID'}:$reply\n";
580: # ------------------------------------------------------------------------ ekey
581: } elsif ($userinput =~ /^ekey/) {
582: my $buildkey=time.$$.int(rand 100000);
583: $buildkey=~tr/1-6/A-F/;
584: $buildkey=int(rand 100000).$buildkey.int(rand 100000);
585: my $key=$perlvar{'lonHostID'}.$hostid{$clientip};
586: $key=~tr/a-z/A-Z/;
587: $key=~tr/G-P/0-9/;
588: $key=~tr/Q-Z/0-9/;
589: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
590: $key=substr($key,0,32);
591: my $cipherkey=pack("H32",$key);
592: $cipher=new IDEA $cipherkey;
593: print $client "$buildkey\n";
594: # ------------------------------------------------------------------------ load
595: } elsif ($userinput =~ /^load/) {
596: my $loadavg;
597: {
598: my $loadfile=IO::File->new('/proc/loadavg');
599: $loadavg=<$loadfile>;
600: }
601: $loadavg =~ s/\s.*//g;
602: my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
603: print $client "$loadpercent\n";
604: # ----------------------------------------------------------------- currentauth
605: } elsif ($userinput =~ /^currentauth/) {
606: if ($wasenc==1) {
607: my ($cmd,$udom,$uname)=split(/:/,$userinput);
608: my $result = GetAuthType($udom, $uname);
609: if($result eq "nouser") {
610: print $client "unknown_user\n";
611: }
612: else {
613: print $client "$result\n"
614: }
615: } else {
616: print $client "refused\n";
617: }
618: # ------------------------------------------------------------------------ auth
619: } elsif ($userinput =~ /^auth/) {
620: if ($wasenc==1) {
621: my ($cmd,$udom,$uname,$upass)=split(/:/,$userinput);
622: chomp($upass);
623: $upass=unescape($upass);
624: my $proname=propath($udom,$uname);
625: my $passfilename="$proname/passwd";
626: if (-e $passfilename) {
627: my $pf = IO::File->new($passfilename);
628: my $realpasswd=<$pf>;
629: chomp($realpasswd);
630: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
631: my $pwdcorrect=0;
632: if ($howpwd eq 'internal') {
633: $pwdcorrect=
634: (crypt($upass,$contentpwd) eq $contentpwd);
635: } elsif ($howpwd eq 'unix') {
636: $contentpwd=(getpwnam($uname))[1];
637: my $pwauth_path="/usr/local/sbin/pwauth";
638: unless ($contentpwd eq 'x') {
639: $pwdcorrect=
640: (crypt($upass,$contentpwd) eq $contentpwd);
641: }
642: elsif (-e $pwauth_path) {
643: open PWAUTH, "|$pwauth_path" or
644: die "Cannot invoke authentication";
645: print PWAUTH "$uname\n$upass\n";
646: close PWAUTH;
647: $pwdcorrect=!$?;
648: }
649: } elsif ($howpwd eq 'krb4') {
650: $null=pack("C",0);
651: unless ($upass=~/$null/) {
652: $pwdcorrect=(
653: Authen::Krb4::get_pw_in_tkt($uname,"",
654: $contentpwd,'krbtgt',$contentpwd,1,
655: $upass) == 0);
656: } else { $pwdcorrect=0; }
657: } elsif ($howpwd eq 'localauth') {
658: $pwdcorrect=&localauth::localauth($uname,$upass,
659: $contentpwd);
660: }
661: if ($pwdcorrect) {
662: print $client "authorized\n";
663: } else {
664: print $client "non_authorized\n";
665: }
666: } else {
667: print $client "unknown_user\n";
668: }
669: } else {
670: print $client "refused\n";
671: }
672: # ---------------------------------------------------------------------- passwd
673: } elsif ($userinput =~ /^passwd/) {
674: if ($wasenc==1) {
675: my
676: ($cmd,$udom,$uname,$upass,$npass)=split(/:/,$userinput);
677: chomp($npass);
678: $upass=&unescape($upass);
679: $npass=&unescape($npass);
680: &logthis("Trying to change password for $uname");
681: my $proname=propath($udom,$uname);
682: my $passfilename="$proname/passwd";
683: if (-e $passfilename) {
684: my $realpasswd;
685: { my $pf = IO::File->new($passfilename);
686: $realpasswd=<$pf>; }
687: chomp($realpasswd);
688: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
689: if ($howpwd eq 'internal') {
690: if (crypt($upass,$contentpwd) eq $contentpwd) {
691: my $salt=time;
692: $salt=substr($salt,6,2);
693: my $ncpass=crypt($npass,$salt);
694: { my $pf = IO::File->new(">$passfilename");
695: print $pf "internal:$ncpass\n"; }
696: &logthis("Result of password change for $uname: pwchange_success");
697: print $client "ok\n";
698: } else {
699: print $client "non_authorized\n";
700: }
701: } elsif ($howpwd eq 'unix') {
702: # Unix means we have to access /etc/password
703: # one way or another.
704: # First: Make sure the current password is
705: # correct
706: $contentpwd=(getpwnam($uname))[1];
707: my $pwdcorrect = "0";
708: my $pwauth_path="/usr/local/sbin/pwauth";
709: unless ($contentpwd eq 'x') {
710: $pwdcorrect=
711: (crypt($upass,$contentpwd) eq $contentpwd);
712: } elsif (-e $pwauth_path) {
713: open PWAUTH, "|$pwauth_path" or
714: die "Cannot invoke authentication";
715: print PWAUTH "$uname\n$upass\n";
716: close PWAUTH;
717: $pwdcorrect=!$?;
718: }
719: if ($pwdcorrect) {
720: my $execdir=$perlvar{'lonDaemons'};
721: my $pf = IO::File->new("|$execdir/lcpasswd");
722: print $pf "$uname\n$npass\n$npass\n";
723: close $pf;
724: my $result = ($?>0 ? 'pwchange_failure'
725: : 'ok');
726: &logthis("Result of password change for $uname: $result");
727: print $client "$result\n";
728: } else {
729: print $client "non_authorized\n";
730: }
731: } else {
732: print $client "auth_mode_error\n";
733: }
734: } else {
735: print $client "unknown_user\n";
736: }
737: } else {
738: print $client "refused\n";
739: }
740: # -------------------------------------------------------------------- makeuser
741: } elsif ($userinput =~ /^makeuser/) {
742: Debug("Make user received");
743: my $oldumask=umask(0077);
744: if ($wasenc==1) {
745: my
746: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
747: &Debug("cmd =".$cmd." $udom =".$udom.
748: " uname=".$uname);
749: chomp($npass);
750: $npass=&unescape($npass);
751: my $proname=propath($udom,$uname);
752: my $passfilename="$proname/passwd";
753: &Debug("Password file created will be:".
754: $passfilename);
755: if (-e $passfilename) {
756: print $client "already_exists\n";
757: } elsif ($udom ne $perlvar{'lonDefDomain'}) {
758: print $client "not_right_domain\n";
759: } else {
760: @fpparts=split(/\//,$proname);
761: $fpnow=$fpparts[0].'/'.$fpparts[1].'/'.$fpparts[2];
762: $fperror='';
763: for ($i=3;$i<=$#fpparts;$i++) {
764: $fpnow.='/'.$fpparts[$i];
765: unless (-e $fpnow) {
766: unless (mkdir($fpnow,0777)) {
767: $fperror="error:$!";
768: }
769: }
770: }
771: unless ($fperror) {
772: if ($umode eq 'krb4') {
773: {
774: my $pf = IO::File->new(">$passfilename");
775: print $pf "krb4:$npass\n";
776: }
777: print $client "ok\n";
778: } elsif ($umode eq 'internal') {
779: my $salt=time;
780: $salt=substr($salt,6,2);
781: my $ncpass=crypt($npass,$salt);
782: {
783: &Debug("Creating internal auth");
784: my $pf = IO::File->new(">$passfilename");
785: print $pf "internal:$ncpass\n";
786: }
787: print $client "ok\n";
788: } elsif ($umode eq 'localauth') {
789: {
790: my $pf = IO::File->new(">$passfilename");
791: print $pf "localauth:$npass\n";
792: }
793: print $client "ok\n";
794: } elsif ($umode eq 'unix') {
795: {
796: my $execpath="$perlvar{'lonDaemons'}/".
797: "lcuseradd";
798: {
799: &Debug("Executing external: ".
800: $execpath);
801: my $se = IO::File->new("|$execpath");
802: print $se "$uname\n";
803: print $se "$npass\n";
804: print $se "$npass\n";
805: }
806: my $pf = IO::File->new(">$passfilename");
807: print $pf "unix:\n";
808: }
809: print $client "ok\n";
810: } elsif ($umode eq 'none') {
811: {
812: my $pf = IO::File->new(">$passfilename");
813: print $pf "none:\n";
814: }
815: print $client "ok\n";
816: } else {
817: print $client "auth_mode_error\n";
818: }
819: } else {
820: print $client "$fperror\n";
821: }
822: }
823: } else {
824: print $client "refused\n";
825: }
826: umask($oldumask);
827: # -------------------------------------------------------------- changeuserauth
828: } elsif ($userinput =~ /^changeuserauth/) {
829: &Debug("Changing authorization");
830: if ($wasenc==1) {
831: my
832: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
833: chomp($npass);
834: &Debug("cmd = ".$cmd." domain= ".$udom.
835: "uname =".$uname." umode= ".$umode);
836: $npass=&unescape($npass);
837: my $proname=propath($udom,$uname);
838: my $passfilename="$proname/passwd";
839: if ($udom ne $perlvar{'lonDefDomain'}) {
840: print $client "not_right_domain\n";
841: } else {
842: if ($umode eq 'krb4') {
843: {
844: my $pf = IO::File->new(">$passfilename");
845: print $pf "krb4:$npass\n";
846: }
847: print $client "ok\n";
848: } elsif ($umode eq 'internal') {
849: my $salt=time;
850: $salt=substr($salt,6,2);
851: my $ncpass=crypt($npass,$salt);
852: {
853: my $pf = IO::File->new(">$passfilename");
854: print $pf "internal:$ncpass\n";
855: }
856: print $client "ok\n";
857: } elsif ($umode eq 'localauth') {
858: {
859: my $pf = IO::File->new(">$passfilename");
860: print $pf "localauth:$npass\n";
861: }
862: print $client "ok\n";
863: } elsif ($umode eq 'unix') {
864: {
865: my $execpath="$perlvar{'lonDaemons'}/".
866: "lcuseradd";
867: {
868: my $se = IO::File->new("|$execpath");
869: print $se "$uname\n";
870: print $se "$npass\n";
871: print $se "$npass\n";
872: }
873: my $pf = IO::File->new(">$passfilename");
874: print $pf "unix:\n";
875: }
876: print $client "ok\n";
877: } elsif ($umode eq 'none') {
878: {
879: my $pf = IO::File->new(">$passfilename");
880: print $pf "none:\n";
881: }
882: print $client "ok\n";
883: } else {
884: print $client "auth_mode_error\n";
885: }
886: }
887: } else {
888: print $client "refused\n";
889: }
890: # ------------------------------------------------------------------------ home
891: } elsif ($userinput =~ /^home/) {
892: my ($cmd,$udom,$uname)=split(/:/,$userinput);
893: chomp($uname);
894: my $proname=propath($udom,$uname);
895: if (-e $proname) {
896: print $client "found\n";
897: } else {
898: print $client "not_found\n";
899: }
900: # ---------------------------------------------------------------------- update
901: } elsif ($userinput =~ /^update/) {
902: my ($cmd,$fname)=split(/:/,$userinput);
903: my $ownership=ishome($fname);
904: if ($ownership eq 'not_owner') {
905: if (-e $fname) {
906: my ($dev,$ino,$mode,$nlink,
907: $uid,$gid,$rdev,$size,
908: $atime,$mtime,$ctime,
909: $blksize,$blocks)=stat($fname);
910: $now=time;
911: $since=$now-$atime;
912: if ($since>$perlvar{'lonExpire'}) {
913: $reply=
914: reply("unsub:$fname","$hostid{$clientip}");
915: unlink("$fname");
916: } else {
917: my $transname="$fname.in.transfer";
918: my $remoteurl=
919: reply("sub:$fname","$hostid{$clientip}");
920: my $response;
921: {
922: my $ua=new LWP::UserAgent;
923: my $request=new HTTP::Request('GET',"$remoteurl");
924: $response=$ua->request($request,$transname);
925: }
926: if ($response->is_error()) {
927: unlink($transname);
928: my $message=$response->status_line;
929: &logthis(
930: "LWP GET: $message for $fname ($remoteurl)");
931: } else {
932: if ($remoteurl!~/\.meta$/) {
933: my $ua=new LWP::UserAgent;
934: my $mrequest=
935: new HTTP::Request('GET',$remoteurl.'.meta');
936: my $mresponse=
937: $ua->request($mrequest,$fname.'.meta');
938: if ($mresponse->is_error()) {
939: unlink($fname.'.meta');
940: }
941: }
942: rename($transname,$fname);
943: }
944: }
945: print $client "ok\n";
946: } else {
947: print $client "not_found\n";
948: }
949: } else {
950: print $client "rejected\n";
951: }
952: # ----------------------------------------------------------------- unsubscribe
953: } elsif ($userinput =~ /^unsub/) {
954: my ($cmd,$fname)=split(/:/,$userinput);
955: if (-e $fname) {
956: if (unlink("$fname.$hostid{$clientip}")) {
957: print $client "ok\n";
958: } else {
959: print $client "not_subscribed\n";
960: }
961: } else {
962: print $client "not_found\n";
963: }
964: # ------------------------------------------------------------------- subscribe
965: } elsif ($userinput =~ /^sub/) {
966: my ($cmd,$fname)=split(/:/,$userinput);
967: my $ownership=ishome($fname);
968: if ($ownership eq 'owner') {
969: if (-e $fname) {
970: if (-d $fname) {
971: print $client "directory\n";
972: } else {
973: $now=time;
974: {
975: my $sh;
976: if ($sh=
977: IO::File->new(">$fname.$hostid{$clientip}")) {
978: print $sh "$clientip:$now\n";
979: }
980: }
981: unless ($fname=~/\.meta$/) {
982: unlink("$fname.meta.$hostid{$clientip}");
983: }
984: $fname=~s/\/home\/httpd\/html\/res/raw/;
985: $fname="http://$thisserver/".$fname;
986: print $client "$fname\n";
987: }
988: } else {
989: print $client "not_found\n";
990: }
991: } else {
992: print $client "rejected\n";
993: }
994: # ------------------------------------------------------------------------- log
995: } elsif ($userinput =~ /^log/) {
996: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
997: chomp($what);
998: my $proname=propath($udom,$uname);
999: my $now=time;
1000: {
1001: my $hfh;
1002: if ($hfh=IO::File->new(">>$proname/activity.log")) {
1003: print $hfh "$now:$hostid{$clientip}:$what\n";
1004: print $client "ok\n";
1005: } else {
1006: print $client "error:$!\n";
1007: }
1008: }
1009: # ------------------------------------------------------------------------- put
1010: } elsif ($userinput =~ /^put/) {
1011: my ($cmd,$udom,$uname,$namespace,$what)
1012: =split(/:/,$userinput);
1013: $namespace=~s/\//\_/g;
1014: $namespace=~s/\W//g;
1015: if ($namespace ne 'roles') {
1016: chomp($what);
1017: my $proname=propath($udom,$uname);
1018: my $now=time;
1019: unless ($namespace=~/^nohist\_/) {
1020: my $hfh;
1021: if (
1022: $hfh=IO::File->new(">>$proname/$namespace.hist")
1023: ) { print $hfh "P:$now:$what\n"; }
1024: }
1025: my @pairs=split(/\&/,$what);
1026: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1027: foreach $pair (@pairs) {
1028: ($key,$value)=split(/=/,$pair);
1029: $hash{$key}=$value;
1030: }
1031: if (untie(%hash)) {
1032: print $client "ok\n";
1033: } else {
1034: print $client "error:$!\n";
1035: }
1036: } else {
1037: print $client "error:$!\n";
1038: }
1039: } else {
1040: print $client "refused\n";
1041: }
1042: # -------------------------------------------------------------------- rolesput
1043: } elsif ($userinput =~ /^rolesput/) {
1044: &Debug("rolesput");
1045: if ($wasenc==1) {
1046: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
1047: =split(/:/,$userinput);
1048: &Debug("cmd = ".$cmd." exedom= ".$exedom.
1049: "user = ".$exeuser." udom=".$udom.
1050: "what = ".$what);
1051: my $namespace='roles';
1052: chomp($what);
1053: my $proname=propath($udom,$uname);
1054: my $now=time;
1055: {
1056: my $hfh;
1057: if (
1058: $hfh=IO::File->new(">>$proname/$namespace.hist")
1059: ) {
1060: print $hfh "P:$now:$exedom:$exeuser:$what\n";
1061: }
1062: }
1063: my @pairs=split(/\&/,$what);
1064: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1065: foreach $pair (@pairs) {
1066: ($key,$value)=split(/=/,$pair);
1067: &ManagePermissions($key, $udom, $uname,
1068: &GetAuthType( $udom,
1069: $uname));
1070: $hash{$key}=$value;
1071:
1072: }
1073: if (untie(%hash)) {
1074: print $client "ok\n";
1075: } else {
1076: print $client "error:$!\n";
1077: }
1078: } else {
1079: print $client "error:$!\n";
1080: }
1081: } else {
1082: print $client "refused\n";
1083: }
1084: # ------------------------------------------------------------------------- get
1085: } elsif ($userinput =~ /^get/) {
1086: my ($cmd,$udom,$uname,$namespace,$what)
1087: =split(/:/,$userinput);
1088: $namespace=~s/\//\_/g;
1089: $namespace=~s/\W//g;
1090: chomp($what);
1091: my @queries=split(/\&/,$what);
1092: my $proname=propath($udom,$uname);
1093: my $qresult='';
1094: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1095: for ($i=0;$i<=$#queries;$i++) {
1096: $qresult.="$hash{$queries[$i]}&";
1097: }
1098: if (untie(%hash)) {
1099: $qresult=~s/\&$//;
1100: print $client "$qresult\n";
1101: } else {
1102: print $client "error:$!\n";
1103: }
1104: } else {
1105: print $client "error:$!\n";
1106: }
1107: # ------------------------------------------------------------------------ eget
1108: } elsif ($userinput =~ /^eget/) {
1109: my ($cmd,$udom,$uname,$namespace,$what)
1110: =split(/:/,$userinput);
1111: $namespace=~s/\//\_/g;
1112: $namespace=~s/\W//g;
1113: chomp($what);
1114: my @queries=split(/\&/,$what);
1115: my $proname=propath($udom,$uname);
1116: my $qresult='';
1117: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1118: for ($i=0;$i<=$#queries;$i++) {
1119: $qresult.="$hash{$queries[$i]}&";
1120: }
1121: if (untie(%hash)) {
1122: $qresult=~s/\&$//;
1123: if ($cipher) {
1124: my $cmdlength=length($qresult);
1125: $qresult.=" ";
1126: my $encqresult='';
1127: for
1128: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
1129: $encqresult.=
1130: unpack("H16",
1131: $cipher->encrypt(substr($qresult,$encidx,8)));
1132: }
1133: print $client "enc:$cmdlength:$encqresult\n";
1134: } else {
1135: print $client "error:no_key\n";
1136: }
1137: } else {
1138: print $client "error:$!\n";
1139: }
1140: } else {
1141: print $client "error:$!\n";
1142: }
1143: # ------------------------------------------------------------------------- del
1144: } elsif ($userinput =~ /^del/) {
1145: my ($cmd,$udom,$uname,$namespace,$what)
1146: =split(/:/,$userinput);
1147: $namespace=~s/\//\_/g;
1148: $namespace=~s/\W//g;
1149: chomp($what);
1150: my $proname=propath($udom,$uname);
1151: my $now=time;
1152: unless ($namespace=~/^nohist\_/) {
1153: my $hfh;
1154: if (
1155: $hfh=IO::File->new(">>$proname/$namespace.hist")
1156: ) { print $hfh "D:$now:$what\n"; }
1157: }
1158: my @keys=split(/\&/,$what);
1159: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1160: foreach $key (@keys) {
1161: delete($hash{$key});
1162: }
1163: if (untie(%hash)) {
1164: print $client "ok\n";
1165: } else {
1166: print $client "error:$!\n";
1167: }
1168: } else {
1169: print $client "error:$!\n";
1170: }
1171: # ------------------------------------------------------------------------ keys
1172: } elsif ($userinput =~ /^keys/) {
1173: my ($cmd,$udom,$uname,$namespace)
1174: =split(/:/,$userinput);
1175: $namespace=~s/\//\_/g;
1176: $namespace=~s/\W//g;
1177: my $proname=propath($udom,$uname);
1178: my $qresult='';
1179: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1180: foreach $key (keys %hash) {
1181: $qresult.="$key&";
1182: }
1183: if (untie(%hash)) {
1184: $qresult=~s/\&$//;
1185: print $client "$qresult\n";
1186: } else {
1187: print $client "error:$!\n";
1188: }
1189: } else {
1190: print $client "error:$!\n";
1191: }
1192: # ------------------------------------------------------------------------ dump
1193: } elsif ($userinput =~ /^dump/) {
1194: my ($cmd,$udom,$uname,$namespace,$regexp)
1195: =split(/:/,$userinput);
1196: $namespace=~s/\//\_/g;
1197: $namespace=~s/\W//g;
1198: if (defined($regexp)) {
1199: $regexp=&unescape($regexp);
1200: } else {
1201: $regexp='.';
1202: }
1203: my $proname=propath($udom,$uname);
1204: my $qresult='';
1205: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1206: foreach $key (keys %hash) {
1207: if (eval('$key=~/$regexp/')) {
1208: $qresult.="$key=$hash{$key}&";
1209: }
1210: }
1211: if (untie(%hash)) {
1212: $qresult=~s/\&$//;
1213: print $client "$qresult\n";
1214: } else {
1215: print $client "error:$!\n";
1216: }
1217: } else {
1218: print $client "error:$!\n";
1219: }
1220: # ----------------------------------------------------------------------- store
1221: } elsif ($userinput =~ /^store/) {
1222: my ($cmd,$udom,$uname,$namespace,$rid,$what)
1223: =split(/:/,$userinput);
1224: $namespace=~s/\//\_/g;
1225: $namespace=~s/\W//g;
1226: if ($namespace ne 'roles') {
1227: chomp($what);
1228: my $proname=propath($udom,$uname);
1229: my $now=time;
1230: unless ($namespace=~/^nohist\_/) {
1231: my $hfh;
1232: if (
1233: $hfh=IO::File->new(">>$proname/$namespace.hist")
1234: ) { print $hfh "P:$now:$rid:$what\n"; }
1235: }
1236: my @pairs=split(/\&/,$what);
1237:
1238: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1239: my @previouskeys=split(/&/,$hash{"keys:$rid"});
1240: my $key;
1241: $hash{"version:$rid"}++;
1242: my $version=$hash{"version:$rid"};
1243: my $allkeys='';
1244: foreach $pair (@pairs) {
1245: ($key,$value)=split(/=/,$pair);
1246: $allkeys.=$key.':';
1247: $hash{"$version:$rid:$key"}=$value;
1248: }
1249: $hash{"$version:$rid:timestamp"}=$now;
1250: $allkeys.='timestamp';
1251: $hash{"$version:keys:$rid"}=$allkeys;
1252: if (untie(%hash)) {
1253: print $client "ok\n";
1254: } else {
1255: print $client "error:$!\n";
1256: }
1257: } else {
1258: print $client "error:$!\n";
1259: }
1260: } else {
1261: print $client "refused\n";
1262: }
1263: # --------------------------------------------------------------------- restore
1264: } elsif ($userinput =~ /^restore/) {
1265: my ($cmd,$udom,$uname,$namespace,$rid)
1266: =split(/:/,$userinput);
1267: $namespace=~s/\//\_/g;
1268: $namespace=~s/\W//g;
1269: chomp($rid);
1270: my $proname=propath($udom,$uname);
1271: my $qresult='';
1272: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1273: my $version=$hash{"version:$rid"};
1274: $qresult.="version=$version&";
1275: my $scope;
1276: for ($scope=1;$scope<=$version;$scope++) {
1277: my $vkeys=$hash{"$scope:keys:$rid"};
1278: my @keys=split(/:/,$vkeys);
1279: my $key;
1280: $qresult.="$scope:keys=$vkeys&";
1281: foreach $key (@keys) {
1282: $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
1283: }
1284: }
1285: if (untie(%hash)) {
1286: $qresult=~s/\&$//;
1287: print $client "$qresult\n";
1288: } else {
1289: print $client "error:$!\n";
1290: }
1291: } else {
1292: print $client "error:$!\n";
1293: }
1294: # ------------------------------------------------------------------- querysend
1295: } elsif ($userinput =~ /^querysend/) {
1296: my ($cmd,$query,
1297: $custom,$customshow)=split(/:/,$userinput);
1298: $query=~s/\n*$//g;
1299: unless ($custom or $customshow) {
1300: print $client "".
1301: sqlreply("$hostid{$clientip}\&$query")."\n";
1302: }
1303: else {
1304: print $client "".
1305: sqlreply("$hostid{$clientip}\&$query".
1306: "\&$custom"."\&$customshow")."\n";
1307: }
1308: # ------------------------------------------------------------------ queryreply
1309: } elsif ($userinput =~ /^queryreply/) {
1310: my ($cmd,$id,$reply)=split(/:/,$userinput);
1311: my $store;
1312: my $execdir=$perlvar{'lonDaemons'};
1313: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1314: $reply=~s/\&/\n/g;
1315: print $store $reply;
1316: close $store;
1317: my $store2=IO::File->new(">$execdir/tmp/$id.end");
1318: print $store2 "done\n";
1319: close $store2;
1320: print $client "ok\n";
1321: }
1322: else {
1323: print $client "error:$!\n";
1324: }
1325: # ----------------------------------------------------------------------- idput
1326: } elsif ($userinput =~ /^idput/) {
1327: my ($cmd,$udom,$what)=split(/:/,$userinput);
1328: chomp($what);
1329: $udom=~s/\W//g;
1330: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1331: my $now=time;
1332: {
1333: my $hfh;
1334: if (
1335: $hfh=IO::File->new(">>$proname.hist")
1336: ) { print $hfh "P:$now:$what\n"; }
1337: }
1338: my @pairs=split(/\&/,$what);
1339: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1340: foreach $pair (@pairs) {
1341: ($key,$value)=split(/=/,$pair);
1342: $hash{$key}=$value;
1343: }
1344: if (untie(%hash)) {
1345: print $client "ok\n";
1346: } else {
1347: print $client "error:$!\n";
1348: }
1349: } else {
1350: print $client "error:$!\n";
1351: }
1352: # ----------------------------------------------------------------------- idget
1353: } elsif ($userinput =~ /^idget/) {
1354: my ($cmd,$udom,$what)=split(/:/,$userinput);
1355: chomp($what);
1356: $udom=~s/\W//g;
1357: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1358: my @queries=split(/\&/,$what);
1359: my $qresult='';
1360: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER,0640)) {
1361: for ($i=0;$i<=$#queries;$i++) {
1362: $qresult.="$hash{$queries[$i]}&";
1363: }
1364: if (untie(%hash)) {
1365: $qresult=~s/\&$//;
1366: print $client "$qresult\n";
1367: } else {
1368: print $client "error:$!\n";
1369: }
1370: } else {
1371: print $client "error:$!\n";
1372: }
1373: # ---------------------------------------------------------------------- tmpput
1374: } elsif ($userinput =~ /^tmpput/) {
1375: my ($cmd,$what)=split(/:/,$userinput);
1376: my $store;
1377: $tmpsnum++;
1378: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
1379: $id=~s/\W/\_/g;
1380: $what=~s/\n//g;
1381: my $execdir=$perlvar{'lonDaemons'};
1382: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1383: print $store $what;
1384: close $store;
1385: print $client "$id\n";
1386: }
1387: else {
1388: print $client "error:$!\n";
1389: }
1390:
1391: # ---------------------------------------------------------------------- tmpget
1392: } elsif ($userinput =~ /^tmpget/) {
1393: my ($cmd,$id)=split(/:/,$userinput);
1394: chomp($id);
1395: $id=~s/\W/\_/g;
1396: my $store;
1397: my $execdir=$perlvar{'lonDaemons'};
1398: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
1399: my $reply=<$store>;
1400: print $client "$reply\n";
1401: close $store;
1402: }
1403: else {
1404: print $client "error:$!\n";
1405: }
1406:
1407: # -------------------------------------------------------------------------- ls
1408: } elsif ($userinput =~ /^ls/) {
1409: my ($cmd,$ulsdir)=split(/:/,$userinput);
1410: my $ulsout='';
1411: my $ulsfn;
1412: if (-e $ulsdir) {
1413: if (opendir(LSDIR,$ulsdir)) {
1414: while ($ulsfn=readdir(LSDIR)) {
1415: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1416: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1417: }
1418: closedir(LSDIR);
1419: }
1420: } else {
1421: $ulsout='no_such_dir';
1422: }
1423: if ($ulsout eq '') { $ulsout='empty'; }
1424: print $client "$ulsout\n";
1425: # ------------------------------------------------------------------ Hanging up
1426: } elsif (($userinput =~ /^exit/) ||
1427: ($userinput =~ /^init/)) {
1428: &logthis(
1429: "Client $clientip ($hostid{$clientip}) hanging up: $userinput");
1430: print $client "bye\n";
1431: $client->close();
1432: last;
1433: # ------------------------------------------------------------- unknown command
1434: } else {
1435: # unknown command
1436: print $client "unknown_cmd\n";
1437: }
1438: # -------------------------------------------------------------------- complete
1439: alarm(0);
1440: &status('Listening to '.$hostid{$clientip});
1441: }
1442: # --------------------------------------------- client unknown or fishy, refuse
1443: } else {
1444: print $client "refused\n";
1445: $client->close();
1446: &logthis("<font color=blue>WARNING: "
1447: ."Rejected client $clientip, closing connection</font>");
1448: }
1449: }
1450:
1451: # =============================================================================
1452:
1453: &logthis("<font color=red>CRITICAL: "
1454: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1455: # tidy up gracefully and finish
1456:
1457: $server->close();
1458:
1459: # this exit is VERY important, otherwise the child will become
1460: # a producer of more and more children, forking yourself into
1461: # process death.
1462: exit;
1463: }
1464: }
1465:
1466:
1467: #
1468: # Checks to see if the input roleput request was to set
1469: # an author role. If so, invokes the lchtmldir script to set
1470: # up a correct public_html
1471: # Parameters:
1472: # request - The request sent to the rolesput subchunk.
1473: # We're looking for /domain/_au
1474: # domain - The domain in which the user is having roles doctored.
1475: # user - Name of the user for which the role is being put.
1476: # authtype - The authentication type associated with the user.
1477: #
1478: sub ManagePermissions
1479: {
1480: my $request = shift;
1481: my $domain = shift;
1482: my $user = shift;
1483: my $authtype= shift;
1484:
1485: # See if the request is of the form /$domain/_au
1486:
1487: if($request =~ /^(\/$domain\/_au)$/) { # It's an author rolesput...
1488: my $execdir = $perlvar{'lonDaemons'};
1489: my $userhome= "/home/$user" ;
1490: Debug("system $execdir/lchtmldir $userhome $system $authtype");
1491: system("$execdir/lchtmldir $userhome $user $authtype");
1492: }
1493: }
1494: #
1495: # GetAuthType - Determines the authorization type of a user in a domain.
1496:
1497: # Returns the authorization type or nouser if there is no such user.
1498: #
1499: sub GetAuthType
1500: {
1501: my $domain = shift;
1502: my $user = shift;
1503:
1504: Debug("GetAuthType( $domain, $user ) \n");
1505: my $proname = &propath($domain, $user);
1506: my $passwdfile = "$proname/passwd";
1507: if( -e $passwdfile ) {
1508: my $pf = IO::File->new($passwdfile);
1509: my $realpassword = <$pf>;
1510: chomp($realpassword);
1511: Debug("Password info = $realpassword\n");
1512: my ($authtype, $contentpwd) = split(/:/, $realpassword);
1513: Debug("Authtype = $authtype, content = $contentpwd\n");
1514: my $availinfo = '';
1515: if($authtype eq 'krb4') {
1516: $availinfo = $contentpwd;
1517: }
1518:
1519: return "$authtype:$availinfo";
1520: }
1521: else {
1522: Debug("Returning nouser");
1523: return "nouser";
1524: }
1525:
1526: }
1527:
1528: # ----------------------------------- POD (plain old documentation, CPAN style)
1529:
1530: =head1 NAME
1531:
1532: lond - "LON Daemon" Server (port "LOND" 5663)
1533:
1534: =head1 SYNOPSIS
1535:
1536: Usage: B<lond>
1537:
1538: Should only be run as user=www. This is a command-line script which
1539: is invoked by B<loncron>. There is no expectation that a typical user
1540: will manually start B<lond> from the command-line. (In other words,
1541: DO NOT START B<lond> YOURSELF.)
1542:
1543: =head1 DESCRIPTION
1544:
1545: There are two characteristics associated with the running of B<lond>,
1546: PROCESS MANAGEMENT (starting, stopping, handling child processes)
1547: and SERVER-SIDE ACTIVITIES (password authentication, user creation,
1548: subscriptions, etc). These are described in two large
1549: sections below.
1550:
1551: B<PROCESS MANAGEMENT>
1552:
1553: Preforker - server who forks first. Runs as a daemon. HUPs.
1554: Uses IDEA encryption
1555:
1556: B<lond> forks off children processes that correspond to the other servers
1557: in the network. Management of these processes can be done at the
1558: parent process level or the child process level.
1559:
1560: B<logs/lond.log> is the location of log messages.
1561:
1562: The process management is now explained in terms of linux shell commands,
1563: subroutines internal to this code, and signal assignments:
1564:
1565: =over 4
1566:
1567: =item *
1568:
1569: PID is stored in B<logs/lond.pid>
1570:
1571: This is the process id number of the parent B<lond> process.
1572:
1573: =item *
1574:
1575: SIGTERM and SIGINT
1576:
1577: Parent signal assignment:
1578: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
1579:
1580: Child signal assignment:
1581: $SIG{INT} = 'DEFAULT'; (and SIGTERM is DEFAULT also)
1582: (The child dies and a SIGALRM is sent to parent, awaking parent from slumber
1583: to restart a new child.)
1584:
1585: Command-line invocations:
1586: B<kill> B<-s> SIGTERM I<PID>
1587: B<kill> B<-s> SIGINT I<PID>
1588:
1589: Subroutine B<HUNTSMAN>:
1590: This is only invoked for the B<lond> parent I<PID>.
1591: This kills all the children, and then the parent.
1592: The B<lonc.pid> file is cleared.
1593:
1594: =item *
1595:
1596: SIGHUP
1597:
1598: Current bug:
1599: This signal can only be processed the first time
1600: on the parent process. Subsequent SIGHUP signals
1601: have no effect.
1602:
1603: Parent signal assignment:
1604: $SIG{HUP} = \&HUPSMAN;
1605:
1606: Child signal assignment:
1607: none (nothing happens)
1608:
1609: Command-line invocations:
1610: B<kill> B<-s> SIGHUP I<PID>
1611:
1612: Subroutine B<HUPSMAN>:
1613: This is only invoked for the B<lond> parent I<PID>,
1614: This kills all the children, and then the parent.
1615: The B<lond.pid> file is cleared.
1616:
1617: =item *
1618:
1619: SIGUSR1
1620:
1621: Parent signal assignment:
1622: $SIG{USR1} = \&USRMAN;
1623:
1624: Child signal assignment:
1625: $SIG{USR1}= \&logstatus;
1626:
1627: Command-line invocations:
1628: B<kill> B<-s> SIGUSR1 I<PID>
1629:
1630: Subroutine B<USRMAN>:
1631: When invoked for the B<lond> parent I<PID>,
1632: SIGUSR1 is sent to all the children, and the status of
1633: each connection is logged.
1634:
1635: =item *
1636:
1637: SIGCHLD
1638:
1639: Parent signal assignment:
1640: $SIG{CHLD} = \&REAPER;
1641:
1642: Child signal assignment:
1643: none
1644:
1645: Command-line invocations:
1646: B<kill> B<-s> SIGCHLD I<PID>
1647:
1648: Subroutine B<REAPER>:
1649: This is only invoked for the B<lond> parent I<PID>.
1650: Information pertaining to the child is removed.
1651: The socket port is cleaned up.
1652:
1653: =back
1654:
1655: B<SERVER-SIDE ACTIVITIES>
1656:
1657: Server-side information can be accepted in an encrypted or non-encrypted
1658: method.
1659:
1660: =over 4
1661:
1662: =item ping
1663:
1664: Query a client in the hosts.tab table; "Are you there?"
1665:
1666: =item pong
1667:
1668: Respond to a ping query.
1669:
1670: =item ekey
1671:
1672: Read in encrypted key, make cipher. Respond with a buildkey.
1673:
1674: =item load
1675:
1676: Respond with CPU load based on a computation upon /proc/loadavg.
1677:
1678: =item currentauth
1679:
1680: Reply with current authentication information (only over an
1681: encrypted channel).
1682:
1683: =item auth
1684:
1685: Only over an encrypted channel, reply as to whether a user's
1686: authentication information can be validated.
1687:
1688: =item passwd
1689:
1690: Allow for a password to be set.
1691:
1692: =item makeuser
1693:
1694: Make a user.
1695:
1696: =item passwd
1697:
1698: Allow for authentication mechanism and password to be changed.
1699:
1700: =item home
1701:
1702: Respond to a question "are you the home for a given user?"
1703:
1704: =item update
1705:
1706: Update contents of a subscribed resource.
1707:
1708: =item unsubscribe
1709:
1710: The server is unsubscribing from a resource.
1711:
1712: =item subscribe
1713:
1714: The server is subscribing to a resource.
1715:
1716: =item log
1717:
1718: Place in B<logs/lond.log>
1719:
1720: =item put
1721:
1722: stores hash in namespace
1723:
1724: =item rolesput
1725:
1726: put a role into a user's environment
1727:
1728: =item get
1729:
1730: returns hash with keys from array
1731: reference filled in from namespace
1732:
1733: =item eget
1734:
1735: returns hash with keys from array
1736: reference filled in from namesp (encrypts the return communication)
1737:
1738: =item rolesget
1739:
1740: get a role from a user's environment
1741:
1742: =item del
1743:
1744: deletes keys out of array from namespace
1745:
1746: =item keys
1747:
1748: returns namespace keys
1749:
1750: =item dump
1751:
1752: dumps the complete (or key matching regexp) namespace into a hash
1753:
1754: =item store
1755:
1756: stores hash permanently
1757: for this url; hashref needs to be given and should be a \%hashname; the
1758: remaining args aren't required and if they aren't passed or are '' they will
1759: be derived from the ENV
1760:
1761: =item restore
1762:
1763: returns a hash for a given url
1764:
1765: =item querysend
1766:
1767: Tells client about the lonsql process that has been launched in response
1768: to a sent query.
1769:
1770: =item queryreply
1771:
1772: Accept information from lonsql and make appropriate storage in temporary
1773: file space.
1774:
1775: =item idput
1776:
1777: Defines usernames as corresponding to IDs. (These "IDs" are unique identifiers
1778: for each student, defined perhaps by the institutional Registrar.)
1779:
1780: =item idget
1781:
1782: Returns usernames corresponding to IDs. (These "IDs" are unique identifiers
1783: for each student, defined perhaps by the institutional Registrar.)
1784:
1785: =item tmpput
1786:
1787: Accept and store information in temporary space.
1788:
1789: =item tmpget
1790:
1791: Send along temporarily stored information.
1792:
1793: =item ls
1794:
1795: List part of a user's directory.
1796:
1797: =item Hanging up (exit or init)
1798:
1799: What to do when a client tells the server that they (the client)
1800: are leaving the network.
1801:
1802: =item unknown command
1803:
1804: If B<lond> is sent an unknown command (not in the list above),
1805: it replys to the client "unknown_cmd".
1806:
1807: =item UNKNOWN CLIENT
1808:
1809: If the anti-spoofing algorithm cannot verify the client,
1810: the client is rejected (with a "refused" message sent
1811: to the client, and the connection is closed.
1812:
1813: =back
1814:
1815: =head1 PREREQUISITES
1816:
1817: IO::Socket
1818: IO::File
1819: Apache::File
1820: Symbol
1821: POSIX
1822: Crypt::IDEA
1823: LWP::UserAgent()
1824: GDBM_File
1825: Authen::Krb4
1826:
1827: =head1 COREQUISITES
1828:
1829: =head1 OSNAMES
1830:
1831: linux
1832:
1833: =head1 SCRIPT CATEGORIES
1834:
1835: Server/Process
1836:
1837: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>