Annotation of loncom/lond, revision 1.108
1.1 albertel 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
1.60 www 4: #
1.108 ! harris41 5: # $Id: lond,v 1.107 2003/01/15 19:34:02 matthew 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.65 www 858: $fperror="error:$!";
1.31 www 859: }
860: }
861: }
862: unless ($fperror) {
1.98 foxr 863: my $result=&make_passwd_file($uname, $umode,$npass,
1.91 albertel 864: $passfilename);
865: print $client $result;
1.31 www 866: } else {
867: print $client "$fperror\n";
868: }
1.55 harris41 869: }
870: } else {
871: print $client "refused\n";
872: }
1.56 harris41 873: umask($oldumask);
1.55 harris41 874: # -------------------------------------------------------------- changeuserauth
875: } elsif ($userinput =~ /^changeuserauth/) {
1.77 foxr 876: &Debug("Changing authorization");
877: if ($wasenc==1) {
1.55 harris41 878: my
1.91 albertel 879: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
1.55 harris41 880: chomp($npass);
1.77 foxr 881: &Debug("cmd = ".$cmd." domain= ".$udom.
882: "uname =".$uname." umode= ".$umode);
1.55 harris41 883: $npass=&unescape($npass);
1.91 albertel 884: my $proname=&propath($udom,$uname);
1.55 harris41 885: my $passfilename="$proname/passwd";
886: if ($udom ne $perlvar{'lonDefDomain'}) {
887: print $client "not_right_domain\n";
888: } else {
1.98 foxr 889: my $result=&make_passwd_file($uname, $umode,$npass,
1.93 albertel 890: $passfilename);
1.91 albertel 891: print $client $result;
1.1 albertel 892: }
893: } else {
894: print $client "refused\n";
895: }
896: # ------------------------------------------------------------------------ home
897: } elsif ($userinput =~ /^home/) {
898: my ($cmd,$udom,$uname)=split(/:/,$userinput);
899: chomp($uname);
900: my $proname=propath($udom,$uname);
901: if (-e $proname) {
902: print $client "found\n";
903: } else {
904: print $client "not_found\n";
905: }
906: # ---------------------------------------------------------------------- update
907: } elsif ($userinput =~ /^update/) {
908: my ($cmd,$fname)=split(/:/,$userinput);
909: my $ownership=ishome($fname);
910: if ($ownership eq 'not_owner') {
911: if (-e $fname) {
912: my ($dev,$ino,$mode,$nlink,
913: $uid,$gid,$rdev,$size,
914: $atime,$mtime,$ctime,
915: $blksize,$blocks)=stat($fname);
916: $now=time;
917: $since=$now-$atime;
918: if ($since>$perlvar{'lonExpire'}) {
919: $reply=
920: reply("unsub:$fname","$hostid{$clientip}");
921: unlink("$fname");
922: } else {
923: my $transname="$fname.in.transfer";
924: my $remoteurl=
925: reply("sub:$fname","$hostid{$clientip}");
926: my $response;
927: {
928: my $ua=new LWP::UserAgent;
929: my $request=new HTTP::Request('GET',"$remoteurl");
930: $response=$ua->request($request,$transname);
931: }
932: if ($response->is_error()) {
1.24 albertel 933: unlink($transname);
1.1 albertel 934: my $message=$response->status_line;
935: &logthis(
936: "LWP GET: $message for $fname ($remoteurl)");
937: } else {
1.14 www 938: if ($remoteurl!~/\.meta$/) {
1.28 www 939: my $ua=new LWP::UserAgent;
1.14 www 940: my $mrequest=
941: new HTTP::Request('GET',$remoteurl.'.meta');
942: my $mresponse=
943: $ua->request($mrequest,$fname.'.meta');
944: if ($mresponse->is_error()) {
945: unlink($fname.'.meta');
946: }
947: }
1.1 albertel 948: rename($transname,$fname);
949: }
950: }
951: print $client "ok\n";
952: } else {
953: print $client "not_found\n";
954: }
955: } else {
956: print $client "rejected\n";
957: }
1.85 www 958: # -------------------------------------- fetch a user file from a remote server
959: } elsif ($userinput =~ /^fetchuserfile/) {
1.86 www 960: my ($cmd,$fname)=split(/:/,$userinput);
961: my ($udom,$uname,$ufile)=split(/\//,$fname);
962: my $udir=propath($udom,$uname).'/userfiles';
1.88 albertel 963: unless (-e $udir) { mkdir($udir,0770); }
1.86 www 964: if (-e $udir) {
965: $ufile=~s/^[\.\~]+//;
966: $ufile=~s/\///g;
967: my $transname=$udir.'/'.$ufile;
968: my $remoteurl='http://'.$clientip.'/userfiles/'.$fname;
969: my $response;
970: {
971: my $ua=new LWP::UserAgent;
972: my $request=new HTTP::Request('GET',"$remoteurl");
973: $response=$ua->request($request,$transname);
974: }
975: if ($response->is_error()) {
976: unlink($transname);
977: my $message=$response->status_line;
978: &logthis(
979: "LWP GET: $message for $fname ($remoteurl)");
980: print $client "failed\n";
981: } else {
982: print $client "ok\n";
983: }
984: } else {
985: print $client "not_home\n";
986: }
1.85 www 987: # ------------------------------------------ authenticate access to a user file
1.86 www 988: } elsif ($userinput =~ /^tokenauthuserfile/) {
1.85 www 989: my ($cmd,$fname,$session)=split(/:/,$userinput);
1.86 www 990: chomp($session);
991: $reply='non_auth';
992: if (open(ENVIN,$perlvar{'lonIDsDir'}.'/'.
993: $session.'.id')) {
994: while ($line=<ENVIN>) {
995: if ($line=~/userfile\.$fname\=/) { $reply='ok'; }
996: }
997: close(ENVIN);
998: print $client $reply."\n";
999: } else {
1000: print $client "invalid_token\n";
1001: }
1.1 albertel 1002: # ----------------------------------------------------------------- unsubscribe
1003: } elsif ($userinput =~ /^unsub/) {
1004: my ($cmd,$fname)=split(/:/,$userinput);
1005: if (-e $fname) {
1.84 albertel 1006: print $client &unsub($client,$fname,$clientip);
1.1 albertel 1007: } else {
1008: print $client "not_found\n";
1009: }
1010: # ------------------------------------------------------------------- subscribe
1011: } elsif ($userinput =~ /^sub/) {
1.84 albertel 1012: print $client &subscribe($userinput,$clientip);
1.102 www 1013: # ------------------------------------------------------------- current version
1014: } elsif ($userinput =~ /^currentversion/) {
1015: my ($cmd,$fname)=split(/:/,$userinput);
1016: print $client ¤tversion($fname)."\n";
1.12 harris41 1017: # ------------------------------------------------------------------------- log
1018: } elsif ($userinput =~ /^log/) {
1019: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
1020: chomp($what);
1021: my $proname=propath($udom,$uname);
1022: my $now=time;
1023: {
1024: my $hfh;
1025: if ($hfh=IO::File->new(">>$proname/activity.log")) {
1026: print $hfh "$now:$hostid{$clientip}:$what\n";
1027: print $client "ok\n";
1028: } else {
1029: print $client "error:$!\n";
1030: }
1031: }
1.1 albertel 1032: # ------------------------------------------------------------------------- put
1033: } elsif ($userinput =~ /^put/) {
1.6 www 1034: my ($cmd,$udom,$uname,$namespace,$what)
1.1 albertel 1035: =split(/:/,$userinput);
1.8 www 1036: $namespace=~s/\//\_/g;
1.6 www 1037: $namespace=~s/\W//g;
1038: if ($namespace ne 'roles') {
1.1 albertel 1039: chomp($what);
1040: my $proname=propath($udom,$uname);
1041: my $now=time;
1.48 www 1042: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1043: my $hfh;
1044: if (
1045: $hfh=IO::File->new(">>$proname/$namespace.hist")
1046: ) { print $hfh "P:$now:$what\n"; }
1047: }
1048: my @pairs=split(/\&/,$what);
1.4 www 1049: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1050: foreach $pair (@pairs) {
1051: ($key,$value)=split(/=/,$pair);
1052: $hash{$key}=$value;
1053: }
1.4 www 1054: if (untie(%hash)) {
1.1 albertel 1055: print $client "ok\n";
1056: } else {
1057: print $client "error:$!\n";
1058: }
1059: } else {
1060: print $client "error:$!\n";
1061: }
1.6 www 1062: } else {
1063: print $client "refused\n";
1064: }
1065: # -------------------------------------------------------------------- rolesput
1066: } elsif ($userinput =~ /^rolesput/) {
1.77 foxr 1067: &Debug("rolesput");
1.6 www 1068: if ($wasenc==1) {
1069: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
1070: =split(/:/,$userinput);
1.77 foxr 1071: &Debug("cmd = ".$cmd." exedom= ".$exedom.
1072: "user = ".$exeuser." udom=".$udom.
1073: "what = ".$what);
1.6 www 1074: my $namespace='roles';
1075: chomp($what);
1076: my $proname=propath($udom,$uname);
1077: my $now=time;
1078: {
1079: my $hfh;
1080: if (
1081: $hfh=IO::File->new(">>$proname/$namespace.hist")
1082: ) {
1083: print $hfh "P:$now:$exedom:$exeuser:$what\n";
1084: }
1085: }
1086: my @pairs=split(/\&/,$what);
1087: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1088: foreach $pair (@pairs) {
1089: ($key,$value)=split(/=/,$pair);
1.78 foxr 1090: &ManagePermissions($key, $udom, $uname,
1091: &GetAuthType( $udom,
1092: $uname));
1.6 www 1093: $hash{$key}=$value;
1.78 foxr 1094:
1.6 www 1095: }
1096: if (untie(%hash)) {
1097: print $client "ok\n";
1098: } else {
1099: print $client "error:$!\n";
1100: }
1101: } else {
1102: print $client "error:$!\n";
1103: }
1104: } else {
1105: print $client "refused\n";
1106: }
1.1 albertel 1107: # ------------------------------------------------------------------------- get
1108: } elsif ($userinput =~ /^get/) {
1109: my ($cmd,$udom,$uname,$namespace,$what)
1110: =split(/:/,$userinput);
1.8 www 1111: $namespace=~s/\//\_/g;
1.1 albertel 1112: $namespace=~s/\W//g;
1113: chomp($what);
1114: my @queries=split(/\&/,$what);
1115: my $proname=propath($udom,$uname);
1116: my $qresult='';
1.20 www 1117: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1118: for ($i=0;$i<=$#queries;$i++) {
1119: $qresult.="$hash{$queries[$i]}&";
1120: }
1.4 www 1121: if (untie(%hash)) {
1.1 albertel 1122: $qresult=~s/\&$//;
1123: print $client "$qresult\n";
1124: } else {
1125: print $client "error:$!\n";
1126: }
1127: } else {
1128: print $client "error:$!\n";
1129: }
1130: # ------------------------------------------------------------------------ eget
1131: } elsif ($userinput =~ /^eget/) {
1132: my ($cmd,$udom,$uname,$namespace,$what)
1133: =split(/:/,$userinput);
1.8 www 1134: $namespace=~s/\//\_/g;
1.1 albertel 1135: $namespace=~s/\W//g;
1136: chomp($what);
1137: my @queries=split(/\&/,$what);
1138: my $proname=propath($udom,$uname);
1139: my $qresult='';
1.20 www 1140: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1141: for ($i=0;$i<=$#queries;$i++) {
1142: $qresult.="$hash{$queries[$i]}&";
1143: }
1.4 www 1144: if (untie(%hash)) {
1.1 albertel 1145: $qresult=~s/\&$//;
1146: if ($cipher) {
1147: my $cmdlength=length($qresult);
1148: $qresult.=" ";
1149: my $encqresult='';
1150: for
1151: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
1152: $encqresult.=
1153: unpack("H16",
1154: $cipher->encrypt(substr($qresult,$encidx,8)));
1155: }
1156: print $client "enc:$cmdlength:$encqresult\n";
1157: } else {
1158: print $client "error:no_key\n";
1159: }
1160: } else {
1161: print $client "error:$!\n";
1162: }
1163: } else {
1164: print $client "error:$!\n";
1165: }
1166: # ------------------------------------------------------------------------- del
1167: } elsif ($userinput =~ /^del/) {
1168: my ($cmd,$udom,$uname,$namespace,$what)
1169: =split(/:/,$userinput);
1.8 www 1170: $namespace=~s/\//\_/g;
1.1 albertel 1171: $namespace=~s/\W//g;
1172: chomp($what);
1173: my $proname=propath($udom,$uname);
1174: my $now=time;
1.48 www 1175: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1176: my $hfh;
1177: if (
1178: $hfh=IO::File->new(">>$proname/$namespace.hist")
1179: ) { print $hfh "D:$now:$what\n"; }
1180: }
1181: my @keys=split(/\&/,$what);
1.4 www 1182: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1183: foreach $key (@keys) {
1184: delete($hash{$key});
1185: }
1.4 www 1186: if (untie(%hash)) {
1.1 albertel 1187: print $client "ok\n";
1188: } else {
1189: print $client "error:$!\n";
1190: }
1191: } else {
1192: print $client "error:$!\n";
1193: }
1194: # ------------------------------------------------------------------------ keys
1195: } elsif ($userinput =~ /^keys/) {
1196: my ($cmd,$udom,$uname,$namespace)
1197: =split(/:/,$userinput);
1.8 www 1198: $namespace=~s/\//\_/g;
1.1 albertel 1199: $namespace=~s/\W//g;
1200: my $proname=propath($udom,$uname);
1201: my $qresult='';
1.20 www 1202: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.1 albertel 1203: foreach $key (keys %hash) {
1204: $qresult.="$key&";
1205: }
1.4 www 1206: if (untie(%hash)) {
1.1 albertel 1207: $qresult=~s/\&$//;
1208: print $client "$qresult\n";
1209: } else {
1210: print $client "error:$!\n";
1.105 matthew 1211: }
1212: } else {
1213: print $client "error:$!\n";
1214: }
1215: # ----------------------------------------------------------------- dumpcurrent
1.107 matthew 1216: } elsif ($userinput =~ /^currentdump/) {
1.105 matthew 1217: my ($cmd,$udom,$uname,$namespace)
1218: =split(/:/,$userinput);
1219: $namespace=~s/\//\_/g;
1220: $namespace=~s/\W//g;
1221: my $qresult='';
1222: my $proname=propath($udom,$uname);
1223: if (tie(%hash,'GDBM_File',
1224: "$proname/$namespace.db",
1225: &GDBM_READER(),0640)) {
1226: # Structure of %data:
1227: # $data{$symb}->{$parameter}=$value;
1228: # $data{$symb}->{'v.'.$parameter}=$version;
1229: # since $parameter will be unescaped, we do not
1230: # have to worry about silly parameter names...
1231: my %data = ();
1232: while (my ($key,$value) = each(%hash)) {
1233: my ($v,$symb,$param) = split(/:/,$key);
1234: next if ($v eq 'version' || $symb eq 'keys');
1235: next if (exists($data{$symb}) &&
1236: exists($data{$symb}->{$param}) &&
1237: $data{$symb}->{'v.'.$param} > $v);
1238: $data{$symb}->{$param}=$value;
1.107 matthew 1239: $data{$symb}->{'v.'.$param}=$v;
1.105 matthew 1240: }
1241: if (untie(%hash)) {
1242: while (my ($symb,$param_hash) = each(%data)) {
1243: while(my ($param,$value) = each (%$param_hash)){
1244: next if ($param =~ /^v\./);
1245: $qresult.=$symb.':'.$param.'='.$value.'&';
1246: }
1247: }
1248: chop($qresult);
1249: print $client "$qresult\n";
1250: } else {
1251: print $client "error:$!\n";
1.1 albertel 1252: }
1253: } else {
1254: print $client "error:$!\n";
1255: }
1256: # ------------------------------------------------------------------------ dump
1257: } elsif ($userinput =~ /^dump/) {
1.62 www 1258: my ($cmd,$udom,$uname,$namespace,$regexp)
1.1 albertel 1259: =split(/:/,$userinput);
1.8 www 1260: $namespace=~s/\//\_/g;
1.1 albertel 1261: $namespace=~s/\W//g;
1.62 www 1262: if (defined($regexp)) {
1263: $regexp=&unescape($regexp);
1264: } else {
1265: $regexp='.';
1266: }
1.100 matthew 1267: my $qresult='';
1.1 albertel 1268: my $proname=propath($udom,$uname);
1.100 matthew 1269: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER(),0640)) {
1.90 stredwic 1270: study($regexp);
1.100 matthew 1271: while (($key,$value) = each(%hash)) {
1272: if ($regexp eq '.') {
1273: $qresult.=$key.'='.$value.'&';
1274: } else {
1275: my $unescapeKey = &unescape($key);
1276: if (eval('$unescapeKey=~/$regexp/')) {
1277: $qresult.="$key=$value&";
1278: }
1279: }
1.7 www 1280: }
1.100 matthew 1281: if (untie(%hash)) {
1282: chop($qresult);
1283: print $client "$qresult\n";
1.7 www 1284: } else {
1.100 matthew 1285: print $client "error:$!\n";
1.7 www 1286: }
1287: } else {
1288: print $client "error:$!\n";
1289: }
1290: # ----------------------------------------------------------------------- store
1291: } elsif ($userinput =~ /^store/) {
1292: my ($cmd,$udom,$uname,$namespace,$rid,$what)
1293: =split(/:/,$userinput);
1.8 www 1294: $namespace=~s/\//\_/g;
1.7 www 1295: $namespace=~s/\W//g;
1296: if ($namespace ne 'roles') {
1297: chomp($what);
1298: my $proname=propath($udom,$uname);
1299: my $now=time;
1.48 www 1300: unless ($namespace=~/^nohist\_/) {
1.7 www 1301: my $hfh;
1302: if (
1303: $hfh=IO::File->new(">>$proname/$namespace.hist")
1304: ) { print $hfh "P:$now:$rid:$what\n"; }
1305: }
1306: my @pairs=split(/\&/,$what);
1307:
1308: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
1309: my @previouskeys=split(/&/,$hash{"keys:$rid"});
1310: my $key;
1311: $hash{"version:$rid"}++;
1312: my $version=$hash{"version:$rid"};
1313: my $allkeys='';
1314: foreach $pair (@pairs) {
1315: ($key,$value)=split(/=/,$pair);
1316: $allkeys.=$key.':';
1317: $hash{"$version:$rid:$key"}=$value;
1318: }
1.36 www 1319: $hash{"$version:$rid:timestamp"}=$now;
1320: $allkeys.='timestamp';
1.7 www 1321: $hash{"$version:keys:$rid"}=$allkeys;
1322: if (untie(%hash)) {
1323: print $client "ok\n";
1324: } else {
1325: print $client "error:$!\n";
1326: }
1327: } else {
1328: print $client "error:$!\n";
1329: }
1330: } else {
1331: print $client "refused\n";
1332: }
1333: # --------------------------------------------------------------------- restore
1334: } elsif ($userinput =~ /^restore/) {
1335: my ($cmd,$udom,$uname,$namespace,$rid)
1336: =split(/:/,$userinput);
1.8 www 1337: $namespace=~s/\//\_/g;
1.7 www 1338: $namespace=~s/\W//g;
1339: chomp($rid);
1340: my $proname=propath($udom,$uname);
1341: my $qresult='';
1.20 www 1342: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
1.7 www 1343: my $version=$hash{"version:$rid"};
1344: $qresult.="version=$version&";
1345: my $scope;
1346: for ($scope=1;$scope<=$version;$scope++) {
1347: my $vkeys=$hash{"$scope:keys:$rid"};
1348: my @keys=split(/:/,$vkeys);
1349: my $key;
1350: $qresult.="$scope:keys=$vkeys&";
1351: foreach $key (@keys) {
1.21 www 1352: $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
1.7 www 1353: }
1.1 albertel 1354: }
1.4 www 1355: if (untie(%hash)) {
1.1 albertel 1356: $qresult=~s/\&$//;
1357: print $client "$qresult\n";
1358: } else {
1359: print $client "error:$!\n";
1360: }
1361: } else {
1362: print $client "error:$!\n";
1363: }
1.86 www 1364: # -------------------------------------------------------------------- chatsend
1365: } elsif ($userinput =~ /^chatsend/) {
1366: my ($cmd,$cdom,$cnum,$newpost)=split(/\:/,$userinput);
1367: &chatadd($cdom,$cnum,$newpost);
1368: print $client "ok\n";
1369: # -------------------------------------------------------------------- chatretr
1370: } elsif ($userinput =~ /^chatretr/) {
1371: my ($cmd,$cdom,$cnum)=split(/\:/,$userinput);
1372: my $reply='';
1373: foreach (&getchat($cdom,$cnum)) {
1374: $reply.=&escape($_).':';
1375: }
1376: $reply=~s/\:$//;
1377: print $client $reply."\n";
1.12 harris41 1378: # ------------------------------------------------------------------- querysend
1379: } elsif ($userinput =~ /^querysend/) {
1.44 harris41 1380: my ($cmd,$query,
1.82 www 1381: $arg1,$arg2,$arg3)=split(/\:/,$userinput);
1.12 harris41 1382: $query=~s/\n*$//g;
1.82 www 1383: print $client "".
1.40 harris41 1384: sqlreply("$hostid{$clientip}\&$query".
1.82 www 1385: "\&$arg1"."\&$arg2"."\&$arg3")."\n";
1.12 harris41 1386: # ------------------------------------------------------------------ queryreply
1387: } elsif ($userinput =~ /^queryreply/) {
1388: my ($cmd,$id,$reply)=split(/:/,$userinput);
1389: my $store;
1.13 www 1390: my $execdir=$perlvar{'lonDaemons'};
1391: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.43 harris41 1392: $reply=~s/\&/\n/g;
1.12 harris41 1393: print $store $reply;
1394: close $store;
1.46 harris41 1395: my $store2=IO::File->new(">$execdir/tmp/$id.end");
1396: print $store2 "done\n";
1397: close $store2;
1.12 harris41 1398: print $client "ok\n";
1399: }
1400: else {
1401: print $client "error:$!\n";
1402: }
1.1 albertel 1403: # ----------------------------------------------------------------------- idput
1404: } elsif ($userinput =~ /^idput/) {
1405: my ($cmd,$udom,$what)=split(/:/,$userinput);
1406: chomp($what);
1407: $udom=~s/\W//g;
1408: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1409: my $now=time;
1410: {
1411: my $hfh;
1412: if (
1413: $hfh=IO::File->new(">>$proname.hist")
1414: ) { print $hfh "P:$now:$what\n"; }
1415: }
1416: my @pairs=split(/\&/,$what);
1.4 www 1417: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
1.1 albertel 1418: foreach $pair (@pairs) {
1419: ($key,$value)=split(/=/,$pair);
1420: $hash{$key}=$value;
1421: }
1.4 www 1422: if (untie(%hash)) {
1.1 albertel 1423: print $client "ok\n";
1424: } else {
1425: print $client "error:$!\n";
1426: }
1427: } else {
1428: print $client "error:$!\n";
1429: }
1430: # ----------------------------------------------------------------------- idget
1431: } elsif ($userinput =~ /^idget/) {
1432: my ($cmd,$udom,$what)=split(/:/,$userinput);
1433: chomp($what);
1434: $udom=~s/\W//g;
1435: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1436: my @queries=split(/\&/,$what);
1437: my $qresult='';
1.20 www 1438: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER,0640)) {
1.1 albertel 1439: for ($i=0;$i<=$#queries;$i++) {
1440: $qresult.="$hash{$queries[$i]}&";
1441: }
1.4 www 1442: if (untie(%hash)) {
1.1 albertel 1443: $qresult=~s/\&$//;
1444: print $client "$qresult\n";
1445: } else {
1446: print $client "error:$!\n";
1447: }
1448: } else {
1449: print $client "error:$!\n";
1450: }
1.13 www 1451: # ---------------------------------------------------------------------- tmpput
1452: } elsif ($userinput =~ /^tmpput/) {
1453: my ($cmd,$what)=split(/:/,$userinput);
1454: my $store;
1455: $tmpsnum++;
1456: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
1457: $id=~s/\W/\_/g;
1458: $what=~s/\n//g;
1459: my $execdir=$perlvar{'lonDaemons'};
1460: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1461: print $store $what;
1462: close $store;
1463: print $client "$id\n";
1464: }
1465: else {
1466: print $client "error:$!\n";
1467: }
1468:
1469: # ---------------------------------------------------------------------- tmpget
1470: } elsif ($userinput =~ /^tmpget/) {
1471: my ($cmd,$id)=split(/:/,$userinput);
1472: chomp($id);
1473: $id=~s/\W/\_/g;
1474: my $store;
1475: my $execdir=$perlvar{'lonDaemons'};
1476: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
1477: my $reply=<$store>;
1478: print $client "$reply\n";
1479: close $store;
1480: }
1481: else {
1482: print $client "error:$!\n";
1483: }
1484:
1.5 www 1485: # -------------------------------------------------------------------------- ls
1486: } elsif ($userinput =~ /^ls/) {
1487: my ($cmd,$ulsdir)=split(/:/,$userinput);
1488: my $ulsout='';
1489: my $ulsfn;
1490: if (-e $ulsdir) {
1.83 stredwic 1491: if(-d $ulsdir) {
1492: if (opendir(LSDIR,$ulsdir)) {
1493: while ($ulsfn=readdir(LSDIR)) {
1494: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1495: $ulsout.=$ulsfn.'&'.
1496: join('&',@ulsstats).':';
1497: }
1498: closedir(LSDIR);
1499: }
1500: } else {
1501: my @ulsstats=stat($ulsdir);
1502: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1503: }
1504: } else {
1.5 www 1505: $ulsout='no_such_dir';
1506: }
1.17 www 1507: if ($ulsout eq '') { $ulsout='empty'; }
1.5 www 1508: print $client "$ulsout\n";
1.51 www 1509: # ------------------------------------------------------------------ Hanging up
1510: } elsif (($userinput =~ /^exit/) ||
1511: ($userinput =~ /^init/)) {
1512: &logthis(
1513: "Client $clientip ($hostid{$clientip}) hanging up: $userinput");
1514: print $client "bye\n";
1.59 www 1515: $client->close();
1.51 www 1516: last;
1.1 albertel 1517: # ------------------------------------------------------------- unknown command
1518: } else {
1519: # unknown command
1520: print $client "unknown_cmd\n";
1521: }
1.58 www 1522: # -------------------------------------------------------------------- complete
1.63 www 1523: alarm(0);
1.58 www 1524: &status('Listening to '.$hostid{$clientip});
1525: }
1.59 www 1526: # --------------------------------------------- client unknown or fishy, refuse
1.1 albertel 1527: } else {
1528: print $client "refused\n";
1.59 www 1529: $client->close();
1.9 www 1530: &logthis("<font color=blue>WARNING: "
1531: ."Rejected client $clientip, closing connection</font>");
1.75 foxr 1532: }
1.106 foxr 1533: }
1.75 foxr 1534:
1.1 albertel 1535: # =============================================================================
1.75 foxr 1536:
1537: &logthis("<font color=red>CRITICAL: "
1538: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.106 foxr 1539:
1.59 www 1540:
1.1 albertel 1541: # this exit is VERY important, otherwise the child will become
1542: # a producer of more and more children, forking yourself into
1543: # process death.
1544: exit;
1.106 foxr 1545:
1.78 foxr 1546: }
1547:
1548:
1549: #
1550: # Checks to see if the input roleput request was to set
1551: # an author role. If so, invokes the lchtmldir script to set
1552: # up a correct public_html
1553: # Parameters:
1554: # request - The request sent to the rolesput subchunk.
1555: # We're looking for /domain/_au
1556: # domain - The domain in which the user is having roles doctored.
1557: # user - Name of the user for which the role is being put.
1558: # authtype - The authentication type associated with the user.
1559: #
1560: sub ManagePermissions
1561: {
1562: my $request = shift;
1563: my $domain = shift;
1564: my $user = shift;
1565: my $authtype= shift;
1566:
1567: # See if the request is of the form /$domain/_au
1568:
1569: if($request =~ /^(\/$domain\/_au)$/) { # It's an author rolesput...
1570: my $execdir = $perlvar{'lonDaemons'};
1571: my $userhome= "/home/$user" ;
1572: Debug("system $execdir/lchtmldir $userhome $system $authtype");
1573: system("$execdir/lchtmldir $userhome $user $authtype");
1574: }
1575: }
1576: #
1577: # GetAuthType - Determines the authorization type of a user in a domain.
1578:
1579: # Returns the authorization type or nouser if there is no such user.
1580: #
1581: sub GetAuthType
1582: {
1583: my $domain = shift;
1584: my $user = shift;
1585:
1.79 foxr 1586: Debug("GetAuthType( $domain, $user ) \n");
1.78 foxr 1587: my $proname = &propath($domain, $user);
1588: my $passwdfile = "$proname/passwd";
1589: if( -e $passwdfile ) {
1590: my $pf = IO::File->new($passwdfile);
1591: my $realpassword = <$pf>;
1592: chomp($realpassword);
1.79 foxr 1593: Debug("Password info = $realpassword\n");
1.78 foxr 1594: my ($authtype, $contentpwd) = split(/:/, $realpassword);
1.79 foxr 1595: Debug("Authtype = $authtype, content = $contentpwd\n");
1.78 foxr 1596: my $availinfo = '';
1.91 albertel 1597: if($authtype eq 'krb4' or $authtype eq 'krb5') {
1.78 foxr 1598: $availinfo = $contentpwd;
1599: }
1.79 foxr 1600:
1.78 foxr 1601: return "$authtype:$availinfo";
1602: }
1603: else {
1.79 foxr 1604: Debug("Returning nouser");
1.78 foxr 1605: return "nouser";
1606: }
1.1 albertel 1607: }
1608:
1.84 albertel 1609: sub addline {
1610: my ($fname,$hostid,$ip,$newline)=@_;
1611: my $contents;
1612: my $found=0;
1613: my $expr='^'.$hostid.':'.$ip.':';
1614: $expr =~ s/\./\\\./g;
1615: if ($sh=IO::File->new("$fname.subscription")) {
1616: while (my $subline=<$sh>) {
1617: if ($subline !~ /$expr/) {$contents.= $subline;} else {$found=1;}
1618: }
1619: $sh->close();
1620: }
1621: $sh=IO::File->new(">$fname.subscription");
1622: if ($contents) { print $sh $contents; }
1623: if ($newline) { print $sh $newline; }
1624: $sh->close();
1625: return $found;
1.86 www 1626: }
1627:
1628: sub getchat {
1629: my ($cdom,$cname)=@_;
1.87 www 1630: my %hash;
1631: my $proname=&propath($cdom,$cname);
1632: my @entries=();
1.88 albertel 1633: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
1634: &GDBM_READER(),0640)) {
1635: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
1636: untie %hash;
1.86 www 1637: }
1.87 www 1638: return @entries;
1.86 www 1639: }
1640:
1641: sub chatadd {
1.88 albertel 1642: my ($cdom,$cname,$newchat)=@_;
1643: my %hash;
1644: my $proname=&propath($cdom,$cname);
1645: my @entries=();
1646: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
1647: &GDBM_WRCREAT(),0640)) {
1648: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
1649: my $time=time;
1650: my ($lastid)=($entries[$#entries]=~/^(\w+)\:/);
1651: my ($thentime,$idnum)=split(/\_/,$lastid);
1652: my $newid=$time.'_000000';
1653: if ($thentime==$time) {
1654: $idnum=~s/^0+//;
1655: $idnum++;
1656: $idnum=substr('000000'.$idnum,-6,6);
1657: $newid=$time.'_'.$idnum;
1658: }
1659: $hash{$newid}=$newchat;
1660: my $expired=$time-3600;
1661: foreach (keys %hash) {
1662: my ($thistime)=($_=~/(\d+)\_/);
1663: if ($thistime<$expired) {
1.89 www 1664: delete $hash{$_};
1.88 albertel 1665: }
1666: }
1667: untie %hash;
1.86 www 1668: }
1.84 albertel 1669: }
1670:
1671: sub unsub {
1672: my ($fname,$clientip)=@_;
1673: my $result;
1674: if (unlink("$fname.$hostid{$clientip}")) {
1675: $result="ok\n";
1676: } else {
1677: $result="not_subscribed\n";
1678: }
1679: if (-e "$fname.subscription") {
1680: my $found=&addline($fname,$hostid{$clientip},$clientip,'');
1681: if ($found) { $result="ok\n"; }
1682: } else {
1683: if ($result != "ok\n") { $result="not_subscribed\n"; }
1684: }
1685: return $result;
1686: }
1687:
1.101 www 1688: sub currentversion {
1689: my $fname=shift;
1690: my $version=-1;
1691: my $ulsdir='';
1692: if ($fname=~/^(.+)\/[^\/]+$/) {
1693: $ulsdir=$1;
1694: }
1695: $fname=~s/\.\d+\.(\w+(?:\.meta)*)$/\.$1/;
1.102 www 1696: $fname=~s/\.(\w+(?:\.meta)*)$/\.\(\\d\+\)\.$1\$/;
1697:
1.101 www 1698: if (-e $fname) { $version=1; }
1699: if (-e $ulsdir) {
1700: if(-d $ulsdir) {
1701: if (opendir(LSDIR,$ulsdir)) {
1702: while ($ulsfn=readdir(LSDIR)) {
1703: # see if this is a regular file (ignore links produced earlier)
1704: my $thisfile=$ulsdir.'/'.$ulsfn;
1705: unless (-l $thisfile) {
1706: if ($thisfile=~/$fname/) {
1707: if ($1>$version) { $version=$1; }
1708: }
1709: }
1710: }
1711: closedir(LSDIR);
1712: $version++;
1713: }
1714: }
1715: }
1716: return $version;
1717: }
1718:
1719: sub thisversion {
1720: my $fname=shift;
1721: my $version=-1;
1722: if ($fname=~/\.(\d+)\.\w+(?:\.meta)*$/) {
1723: $version=$1;
1724: }
1725: return $version;
1726: }
1727:
1.84 albertel 1728: sub subscribe {
1729: my ($userinput,$clientip)=@_;
1730: my $result;
1731: my ($cmd,$fname)=split(/:/,$userinput);
1732: my $ownership=&ishome($fname);
1733: if ($ownership eq 'owner') {
1.101 www 1734: # explitly asking for the current version?
1735: unless (-e $fname) {
1736: my $currentversion=¤tversion($fname);
1737: if (&thisversion($fname)==$currentversion) {
1738: if ($fname=~/^(.+)\.\d+\.(\w+(?:\.meta)*)$/) {
1739: my $root=$1;
1740: my $extension=$2;
1741: symlink($root.'.'.$extension,
1742: $root.'.'.$currentversion.'.'.$extension);
1.102 www 1743: unless ($extension=~/\.meta$/) {
1744: symlink($root.'.'.$extension.'.meta',
1745: $root.'.'.$currentversion.'.'.$extension.'.meta');
1746: }
1.101 www 1747: }
1748: }
1749: }
1.84 albertel 1750: if (-e $fname) {
1751: if (-d $fname) {
1752: $result="directory\n";
1753: } else {
1754: if (-e "$fname.$hostid{$clientip}") {&unsub($fname,$clientip);}
1755: $now=time;
1756: my $found=&addline($fname,$hostid{$clientip},$clientip,
1757: "$hostid{$clientip}:$clientip:$now\n");
1758: if ($found) { $result="$fname\n"; }
1759: # if they were subscribed to only meta data, delete that
1760: # subscription, when you subscribe to a file you also get
1761: # the metadata
1762: unless ($fname=~/\.meta$/) { &unsub("$fname.meta",$clientip); }
1763: $fname=~s/\/home\/httpd\/html\/res/raw/;
1764: $fname="http://$thisserver/".$fname;
1765: $result="$fname\n";
1766: }
1767: } else {
1768: $result="not_found\n";
1769: }
1770: } else {
1771: $result="rejected\n";
1772: }
1773: return $result;
1774: }
1.91 albertel 1775:
1776: sub make_passwd_file {
1.98 foxr 1777: my ($uname, $umode,$npass,$passfilename)=@_;
1.91 albertel 1778: my $result="ok\n";
1779: if ($umode eq 'krb4' or $umode eq 'krb5') {
1780: {
1781: my $pf = IO::File->new(">$passfilename");
1782: print $pf "$umode:$npass\n";
1783: }
1784: } elsif ($umode eq 'internal') {
1785: my $salt=time;
1786: $salt=substr($salt,6,2);
1787: my $ncpass=crypt($npass,$salt);
1788: {
1789: &Debug("Creating internal auth");
1790: my $pf = IO::File->new(">$passfilename");
1791: print $pf "internal:$ncpass\n";
1792: }
1793: } elsif ($umode eq 'localauth') {
1794: {
1795: my $pf = IO::File->new(">$passfilename");
1796: print $pf "localauth:$npass\n";
1797: }
1798: } elsif ($umode eq 'unix') {
1799: {
1800: my $execpath="$perlvar{'lonDaemons'}/"."lcuseradd";
1801: {
1802: &Debug("Executing external: ".$execpath);
1.98 foxr 1803: &Debug("user = ".$uname.", Password =". $npass);
1804: my $se = IO::File->new("|$execpath > /home/www/lcuseradd.log");
1.91 albertel 1805: print $se "$uname\n";
1806: print $se "$npass\n";
1807: print $se "$npass\n";
1.97 foxr 1808: }
1809: my $useraddok = $?;
1810: if($useraddok > 0) {
1811: &logthis("Failed lcuseradd: ".&lcuseraddstrerror($useraddok));
1.91 albertel 1812: }
1813: my $pf = IO::File->new(">$passfilename");
1814: print $pf "unix:\n";
1815: }
1816: } elsif ($umode eq 'none') {
1817: {
1818: my $pf = IO::File->new(">$passfilename");
1819: print $pf "none:\n";
1820: }
1821: } else {
1822: $result="auth_mode_error\n";
1823: }
1824: return $result;
1825: }
1826:
1.61 harris41 1827: # ----------------------------------- POD (plain old documentation, CPAN style)
1828:
1829: =head1 NAME
1830:
1831: lond - "LON Daemon" Server (port "LOND" 5663)
1832:
1833: =head1 SYNOPSIS
1834:
1.74 harris41 1835: Usage: B<lond>
1836:
1837: Should only be run as user=www. This is a command-line script which
1838: is invoked by B<loncron>. There is no expectation that a typical user
1839: will manually start B<lond> from the command-line. (In other words,
1840: DO NOT START B<lond> YOURSELF.)
1.61 harris41 1841:
1842: =head1 DESCRIPTION
1843:
1.74 harris41 1844: There are two characteristics associated with the running of B<lond>,
1845: PROCESS MANAGEMENT (starting, stopping, handling child processes)
1846: and SERVER-SIDE ACTIVITIES (password authentication, user creation,
1847: subscriptions, etc). These are described in two large
1848: sections below.
1849:
1850: B<PROCESS MANAGEMENT>
1851:
1.61 harris41 1852: Preforker - server who forks first. Runs as a daemon. HUPs.
1853: Uses IDEA encryption
1854:
1.74 harris41 1855: B<lond> forks off children processes that correspond to the other servers
1856: in the network. Management of these processes can be done at the
1857: parent process level or the child process level.
1858:
1859: B<logs/lond.log> is the location of log messages.
1860:
1861: The process management is now explained in terms of linux shell commands,
1862: subroutines internal to this code, and signal assignments:
1863:
1864: =over 4
1865:
1866: =item *
1867:
1868: PID is stored in B<logs/lond.pid>
1869:
1870: This is the process id number of the parent B<lond> process.
1871:
1872: =item *
1873:
1874: SIGTERM and SIGINT
1875:
1876: Parent signal assignment:
1877: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
1878:
1879: Child signal assignment:
1880: $SIG{INT} = 'DEFAULT'; (and SIGTERM is DEFAULT also)
1881: (The child dies and a SIGALRM is sent to parent, awaking parent from slumber
1882: to restart a new child.)
1883:
1884: Command-line invocations:
1885: B<kill> B<-s> SIGTERM I<PID>
1886: B<kill> B<-s> SIGINT I<PID>
1887:
1888: Subroutine B<HUNTSMAN>:
1889: This is only invoked for the B<lond> parent I<PID>.
1890: This kills all the children, and then the parent.
1891: The B<lonc.pid> file is cleared.
1892:
1893: =item *
1894:
1895: SIGHUP
1896:
1897: Current bug:
1898: This signal can only be processed the first time
1899: on the parent process. Subsequent SIGHUP signals
1900: have no effect.
1901:
1902: Parent signal assignment:
1903: $SIG{HUP} = \&HUPSMAN;
1904:
1905: Child signal assignment:
1906: none (nothing happens)
1907:
1908: Command-line invocations:
1909: B<kill> B<-s> SIGHUP I<PID>
1910:
1911: Subroutine B<HUPSMAN>:
1912: This is only invoked for the B<lond> parent I<PID>,
1913: This kills all the children, and then the parent.
1914: The B<lond.pid> file is cleared.
1915:
1916: =item *
1917:
1918: SIGUSR1
1919:
1920: Parent signal assignment:
1921: $SIG{USR1} = \&USRMAN;
1922:
1923: Child signal assignment:
1924: $SIG{USR1}= \&logstatus;
1925:
1926: Command-line invocations:
1927: B<kill> B<-s> SIGUSR1 I<PID>
1928:
1929: Subroutine B<USRMAN>:
1930: When invoked for the B<lond> parent I<PID>,
1931: SIGUSR1 is sent to all the children, and the status of
1932: each connection is logged.
1933:
1934: =item *
1935:
1936: SIGCHLD
1937:
1938: Parent signal assignment:
1939: $SIG{CHLD} = \&REAPER;
1940:
1941: Child signal assignment:
1942: none
1943:
1944: Command-line invocations:
1945: B<kill> B<-s> SIGCHLD I<PID>
1946:
1947: Subroutine B<REAPER>:
1948: This is only invoked for the B<lond> parent I<PID>.
1949: Information pertaining to the child is removed.
1950: The socket port is cleaned up.
1951:
1952: =back
1953:
1954: B<SERVER-SIDE ACTIVITIES>
1955:
1956: Server-side information can be accepted in an encrypted or non-encrypted
1957: method.
1958:
1959: =over 4
1960:
1961: =item ping
1962:
1963: Query a client in the hosts.tab table; "Are you there?"
1964:
1965: =item pong
1966:
1967: Respond to a ping query.
1968:
1969: =item ekey
1970:
1971: Read in encrypted key, make cipher. Respond with a buildkey.
1972:
1973: =item load
1974:
1975: Respond with CPU load based on a computation upon /proc/loadavg.
1976:
1977: =item currentauth
1978:
1979: Reply with current authentication information (only over an
1980: encrypted channel).
1981:
1982: =item auth
1983:
1984: Only over an encrypted channel, reply as to whether a user's
1985: authentication information can be validated.
1986:
1987: =item passwd
1988:
1989: Allow for a password to be set.
1990:
1991: =item makeuser
1992:
1993: Make a user.
1994:
1995: =item passwd
1996:
1997: Allow for authentication mechanism and password to be changed.
1998:
1999: =item home
1.61 harris41 2000:
1.74 harris41 2001: Respond to a question "are you the home for a given user?"
2002:
2003: =item update
2004:
2005: Update contents of a subscribed resource.
2006:
2007: =item unsubscribe
2008:
2009: The server is unsubscribing from a resource.
2010:
2011: =item subscribe
2012:
2013: The server is subscribing to a resource.
2014:
2015: =item log
2016:
2017: Place in B<logs/lond.log>
2018:
2019: =item put
2020:
2021: stores hash in namespace
2022:
2023: =item rolesput
2024:
2025: put a role into a user's environment
2026:
2027: =item get
2028:
2029: returns hash with keys from array
2030: reference filled in from namespace
2031:
2032: =item eget
2033:
2034: returns hash with keys from array
2035: reference filled in from namesp (encrypts the return communication)
2036:
2037: =item rolesget
2038:
2039: get a role from a user's environment
2040:
2041: =item del
2042:
2043: deletes keys out of array from namespace
2044:
2045: =item keys
2046:
2047: returns namespace keys
2048:
2049: =item dump
2050:
2051: dumps the complete (or key matching regexp) namespace into a hash
2052:
2053: =item store
2054:
2055: stores hash permanently
2056: for this url; hashref needs to be given and should be a \%hashname; the
2057: remaining args aren't required and if they aren't passed or are '' they will
2058: be derived from the ENV
2059:
2060: =item restore
2061:
2062: returns a hash for a given url
2063:
2064: =item querysend
2065:
2066: Tells client about the lonsql process that has been launched in response
2067: to a sent query.
2068:
2069: =item queryreply
2070:
2071: Accept information from lonsql and make appropriate storage in temporary
2072: file space.
2073:
2074: =item idput
2075:
2076: Defines usernames as corresponding to IDs. (These "IDs" are unique identifiers
2077: for each student, defined perhaps by the institutional Registrar.)
2078:
2079: =item idget
2080:
2081: Returns usernames corresponding to IDs. (These "IDs" are unique identifiers
2082: for each student, defined perhaps by the institutional Registrar.)
2083:
2084: =item tmpput
2085:
2086: Accept and store information in temporary space.
2087:
2088: =item tmpget
2089:
2090: Send along temporarily stored information.
2091:
2092: =item ls
2093:
2094: List part of a user's directory.
2095:
2096: =item Hanging up (exit or init)
2097:
2098: What to do when a client tells the server that they (the client)
2099: are leaving the network.
2100:
2101: =item unknown command
2102:
2103: If B<lond> is sent an unknown command (not in the list above),
2104: it replys to the client "unknown_cmd".
2105:
2106: =item UNKNOWN CLIENT
2107:
2108: If the anti-spoofing algorithm cannot verify the client,
2109: the client is rejected (with a "refused" message sent
2110: to the client, and the connection is closed.
2111:
2112: =back
1.61 harris41 2113:
2114: =head1 PREREQUISITES
2115:
2116: IO::Socket
2117: IO::File
2118: Apache::File
2119: Symbol
2120: POSIX
2121: Crypt::IDEA
2122: LWP::UserAgent()
2123: GDBM_File
2124: Authen::Krb4
1.91 albertel 2125: Authen::Krb5
1.61 harris41 2126:
2127: =head1 COREQUISITES
2128:
2129: =head1 OSNAMES
2130:
2131: linux
2132:
2133: =head1 SCRIPT CATEGORIES
2134:
2135: Server/Process
2136:
2137: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>