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