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