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