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