Annotation of loncom/lond, revision 1.122
1.1 albertel 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
1.60 www 4: #
1.122 ! www 5: # $Id: lond,v 1.121 2003/03/27 23:00:28 albertel 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.122 ! www 76: my $VERSION='$Revision: 1.121 $'; #' 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;
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/) {
1.122 ! www 1482: my
! 1483: ($cmd,$cdom,$cnum,$udom,$uname)=split(/\:/,$userinput);
1.86 www 1484: my $reply='';
1.122 ! www 1485: foreach (&getchat($cdom,$cnum,$udom,$uname)) {
1.86 www 1486: $reply.=&escape($_).':';
1487: }
1488: $reply=~s/\:$//;
1489: print $client $reply."\n";
1.12 harris41 1490: # ------------------------------------------------------------------- querysend
1491: } elsif ($userinput =~ /^querysend/) {
1.44 harris41 1492: my ($cmd,$query,
1.82 www 1493: $arg1,$arg2,$arg3)=split(/\:/,$userinput);
1.12 harris41 1494: $query=~s/\n*$//g;
1.82 www 1495: print $client "".
1.40 harris41 1496: sqlreply("$hostid{$clientip}\&$query".
1.82 www 1497: "\&$arg1"."\&$arg2"."\&$arg3")."\n";
1.12 harris41 1498: # ------------------------------------------------------------------ queryreply
1499: } elsif ($userinput =~ /^queryreply/) {
1500: my ($cmd,$id,$reply)=split(/:/,$userinput);
1501: my $store;
1.13 www 1502: my $execdir=$perlvar{'lonDaemons'};
1503: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.43 harris41 1504: $reply=~s/\&/\n/g;
1.12 harris41 1505: print $store $reply;
1506: close $store;
1.46 harris41 1507: my $store2=IO::File->new(">$execdir/tmp/$id.end");
1508: print $store2 "done\n";
1509: close $store2;
1.12 harris41 1510: print $client "ok\n";
1511: }
1512: else {
1.109 foxr 1513: print $client "error: ".($!+0)
1.111 matthew 1514: ." IO::File->new Failed ".
1515: "while attempting queryreply\n";
1.12 harris41 1516: }
1.118 www 1517: # ----------------------------------------------------------------- courseidput
1518: } elsif ($userinput =~ /^courseidput/) {
1519: my ($cmd,$udom,$what)=split(/:/,$userinput);
1520: chomp($what);
1521: $udom=~s/\W//g;
1522: my $proname=
1523: "$perlvar{'lonUsersDir'}/$udom/nohist_courseids";
1524: my $now=time;
1525: my @pairs=split(/\&/,$what);
1.119 albertel 1526: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT(),0640)) {
1.118 www 1527: foreach $pair (@pairs) {
1528: ($key,$value)=split(/=/,$pair);
1529: $hash{$key}=$value.':'.$now;
1530: }
1531: if (untie(%hash)) {
1532: print $client "ok\n";
1533: } else {
1534: print $client "error: ".($!+0)
1535: ." untie(GDBM) Failed ".
1536: "while attempting courseidput\n";
1537: }
1538: } else {
1539: print $client "error: ".($!+0)
1540: ." tie(GDBM) Failed ".
1541: "while attempting courseidput\n";
1542: }
1543: # ---------------------------------------------------------------- courseiddump
1544: } elsif ($userinput =~ /^courseiddump/) {
1545: my ($cmd,$udom,$since,$description)
1546: =split(/:/,$userinput);
1547: if (defined($description)) {
1548: $description=&unescape($description);
1549: } else {
1550: $description='.';
1551: }
1552: unless (defined($since)) { $since=0; }
1553: my $qresult='';
1554: my $proname=
1555: "$perlvar{'lonUsersDir'}/$udom/nohist_courseids";
1556: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER(),0640)) {
1557: while (($key,$value) = each(%hash)) {
1558: my ($descr,$lasttime)=split(/\:/,$value);
1559: if ($lasttime<$since) { next; }
1560: if ($regexp eq '.') {
1561: $qresult.=$key.'='.$descr.'&';
1562: } else {
1563: my $unescapeVal = &unescape($descr);
1.120 www 1564: if (eval('$unescapeVal=~/$description/i')) {
1.118 www 1565: $qresult.="$key=$descr&";
1566: }
1567: }
1568: }
1569: if (untie(%hash)) {
1570: chop($qresult);
1571: print $client "$qresult\n";
1572: } else {
1573: print $client "error: ".($!+0)
1574: ." untie(GDBM) Failed ".
1575: "while attempting courseiddump\n";
1576: }
1577: } else {
1578: print $client "error: ".($!+0)
1579: ." tie(GDBM) Failed ".
1580: "while attempting courseiddump\n";
1581: }
1.1 albertel 1582: # ----------------------------------------------------------------------- idput
1583: } elsif ($userinput =~ /^idput/) {
1584: my ($cmd,$udom,$what)=split(/:/,$userinput);
1585: chomp($what);
1586: $udom=~s/\W//g;
1587: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1588: my $now=time;
1589: {
1590: my $hfh;
1591: if (
1592: $hfh=IO::File->new(">>$proname.hist")
1593: ) { print $hfh "P:$now:$what\n"; }
1594: }
1595: my @pairs=split(/\&/,$what);
1.119 albertel 1596: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT(),0640)) {
1.1 albertel 1597: foreach $pair (@pairs) {
1598: ($key,$value)=split(/=/,$pair);
1599: $hash{$key}=$value;
1600: }
1.4 www 1601: if (untie(%hash)) {
1.1 albertel 1602: print $client "ok\n";
1603: } else {
1.109 foxr 1604: print $client "error: ".($!+0)
1.111 matthew 1605: ." untie(GDBM) Failed ".
1606: "while attempting idput\n";
1.1 albertel 1607: }
1608: } else {
1.109 foxr 1609: print $client "error: ".($!+0)
1.111 matthew 1610: ." tie(GDBM) Failed ".
1611: "while attempting idput\n";
1.1 albertel 1612: }
1613: # ----------------------------------------------------------------------- idget
1614: } elsif ($userinput =~ /^idget/) {
1615: my ($cmd,$udom,$what)=split(/:/,$userinput);
1616: chomp($what);
1617: $udom=~s/\W//g;
1618: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1619: my @queries=split(/\&/,$what);
1620: my $qresult='';
1.119 albertel 1621: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER(),0640)) {
1.1 albertel 1622: for ($i=0;$i<=$#queries;$i++) {
1623: $qresult.="$hash{$queries[$i]}&";
1624: }
1.4 www 1625: if (untie(%hash)) {
1.1 albertel 1626: $qresult=~s/\&$//;
1627: print $client "$qresult\n";
1628: } else {
1.109 foxr 1629: print $client "error: ".($!+0)
1.111 matthew 1630: ." untie(GDBM) Failed ".
1631: "while attempting idget\n";
1.1 albertel 1632: }
1633: } else {
1.109 foxr 1634: print $client "error: ".($!+0)
1.111 matthew 1635: ." tie(GDBM) Failed ".
1636: "while attempting idget\n";
1.1 albertel 1637: }
1.13 www 1638: # ---------------------------------------------------------------------- tmpput
1639: } elsif ($userinput =~ /^tmpput/) {
1640: my ($cmd,$what)=split(/:/,$userinput);
1641: my $store;
1642: $tmpsnum++;
1643: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
1644: $id=~s/\W/\_/g;
1645: $what=~s/\n//g;
1646: my $execdir=$perlvar{'lonDaemons'};
1647: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1648: print $store $what;
1649: close $store;
1650: print $client "$id\n";
1651: }
1652: else {
1.109 foxr 1653: print $client "error: ".($!+0)
1.111 matthew 1654: ."IO::File->new Failed ".
1655: "while attempting tmpput\n";
1.13 www 1656: }
1657:
1658: # ---------------------------------------------------------------------- tmpget
1659: } elsif ($userinput =~ /^tmpget/) {
1660: my ($cmd,$id)=split(/:/,$userinput);
1661: chomp($id);
1662: $id=~s/\W/\_/g;
1663: my $store;
1664: my $execdir=$perlvar{'lonDaemons'};
1665: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
1666: my $reply=<$store>;
1667: print $client "$reply\n";
1668: close $store;
1669: }
1670: else {
1.109 foxr 1671: print $client "error: ".($!+0)
1.111 matthew 1672: ."IO::File->new Failed ".
1673: "while attempting tmpget\n";
1.13 www 1674: }
1675:
1.110 www 1676: # ---------------------------------------------------------------------- tmpdel
1677: } elsif ($userinput =~ /^tmpdel/) {
1678: my ($cmd,$id)=split(/:/,$userinput);
1679: chomp($id);
1680: $id=~s/\W/\_/g;
1681: my $execdir=$perlvar{'lonDaemons'};
1682: if (unlink("$execdir/tmp/$id.tmp")) {
1683: print $client "ok\n";
1684: } else {
1685: print $client "error: ".($!+0)
1.111 matthew 1686: ."Unlink tmp Failed ".
1687: "while attempting tmpdel\n";
1.110 www 1688: }
1.5 www 1689: # -------------------------------------------------------------------------- ls
1690: } elsif ($userinput =~ /^ls/) {
1691: my ($cmd,$ulsdir)=split(/:/,$userinput);
1692: my $ulsout='';
1693: my $ulsfn;
1694: if (-e $ulsdir) {
1.83 stredwic 1695: if(-d $ulsdir) {
1696: if (opendir(LSDIR,$ulsdir)) {
1697: while ($ulsfn=readdir(LSDIR)) {
1698: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1699: $ulsout.=$ulsfn.'&'.
1700: join('&',@ulsstats).':';
1701: }
1702: closedir(LSDIR);
1703: }
1704: } else {
1705: my @ulsstats=stat($ulsdir);
1706: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1707: }
1708: } else {
1.5 www 1709: $ulsout='no_such_dir';
1710: }
1.17 www 1711: if ($ulsout eq '') { $ulsout='empty'; }
1.5 www 1712: print $client "$ulsout\n";
1.51 www 1713: # ------------------------------------------------------------------ Hanging up
1714: } elsif (($userinput =~ /^exit/) ||
1715: ($userinput =~ /^init/)) {
1716: &logthis(
1717: "Client $clientip ($hostid{$clientip}) hanging up: $userinput");
1718: print $client "bye\n";
1.59 www 1719: $client->close();
1.51 www 1720: last;
1.1 albertel 1721: # ------------------------------------------------------------- unknown command
1.121 albertel 1722: } elsif ($userinput =~ /^sethost:/) {
1723: print $client &sethost($userinput)."\n";
1724: } elsif ($userinput =~/^version:/) {
1725: print $client &version($userinput)."\n";
1.1 albertel 1726: } else {
1727: # unknown command
1728: print $client "unknown_cmd\n";
1729: }
1.58 www 1730: # -------------------------------------------------------------------- complete
1.63 www 1731: alarm(0);
1.58 www 1732: &status('Listening to '.$hostid{$clientip});
1733: }
1.59 www 1734: # --------------------------------------------- client unknown or fishy, refuse
1.1 albertel 1735: } else {
1736: print $client "refused\n";
1.59 www 1737: $client->close();
1.9 www 1738: &logthis("<font color=blue>WARNING: "
1739: ."Rejected client $clientip, closing connection</font>");
1.75 foxr 1740: }
1.106 foxr 1741: }
1.75 foxr 1742:
1.1 albertel 1743: # =============================================================================
1.75 foxr 1744:
1745: &logthis("<font color=red>CRITICAL: "
1746: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.106 foxr 1747:
1.59 www 1748:
1.1 albertel 1749: # this exit is VERY important, otherwise the child will become
1750: # a producer of more and more children, forking yourself into
1751: # process death.
1752: exit;
1.106 foxr 1753:
1.78 foxr 1754: }
1755:
1756:
1757: #
1758: # Checks to see if the input roleput request was to set
1759: # an author role. If so, invokes the lchtmldir script to set
1760: # up a correct public_html
1761: # Parameters:
1762: # request - The request sent to the rolesput subchunk.
1763: # We're looking for /domain/_au
1764: # domain - The domain in which the user is having roles doctored.
1765: # user - Name of the user for which the role is being put.
1766: # authtype - The authentication type associated with the user.
1767: #
1768: sub ManagePermissions
1769: {
1770: my $request = shift;
1771: my $domain = shift;
1772: my $user = shift;
1773: my $authtype= shift;
1774:
1775: # See if the request is of the form /$domain/_au
1776:
1777: if($request =~ /^(\/$domain\/_au)$/) { # It's an author rolesput...
1778: my $execdir = $perlvar{'lonDaemons'};
1779: my $userhome= "/home/$user" ;
1780: Debug("system $execdir/lchtmldir $userhome $system $authtype");
1781: system("$execdir/lchtmldir $userhome $user $authtype");
1782: }
1783: }
1784: #
1785: # GetAuthType - Determines the authorization type of a user in a domain.
1786:
1787: # Returns the authorization type or nouser if there is no such user.
1788: #
1789: sub GetAuthType
1790: {
1791: my $domain = shift;
1792: my $user = shift;
1793:
1.79 foxr 1794: Debug("GetAuthType( $domain, $user ) \n");
1.78 foxr 1795: my $proname = &propath($domain, $user);
1796: my $passwdfile = "$proname/passwd";
1797: if( -e $passwdfile ) {
1798: my $pf = IO::File->new($passwdfile);
1799: my $realpassword = <$pf>;
1800: chomp($realpassword);
1.79 foxr 1801: Debug("Password info = $realpassword\n");
1.78 foxr 1802: my ($authtype, $contentpwd) = split(/:/, $realpassword);
1.79 foxr 1803: Debug("Authtype = $authtype, content = $contentpwd\n");
1.78 foxr 1804: my $availinfo = '';
1.91 albertel 1805: if($authtype eq 'krb4' or $authtype eq 'krb5') {
1.78 foxr 1806: $availinfo = $contentpwd;
1807: }
1.79 foxr 1808:
1.78 foxr 1809: return "$authtype:$availinfo";
1810: }
1811: else {
1.79 foxr 1812: Debug("Returning nouser");
1.78 foxr 1813: return "nouser";
1814: }
1.1 albertel 1815: }
1816:
1.84 albertel 1817: sub addline {
1818: my ($fname,$hostid,$ip,$newline)=@_;
1819: my $contents;
1820: my $found=0;
1821: my $expr='^'.$hostid.':'.$ip.':';
1822: $expr =~ s/\./\\\./g;
1823: if ($sh=IO::File->new("$fname.subscription")) {
1824: while (my $subline=<$sh>) {
1825: if ($subline !~ /$expr/) {$contents.= $subline;} else {$found=1;}
1826: }
1827: $sh->close();
1828: }
1829: $sh=IO::File->new(">$fname.subscription");
1830: if ($contents) { print $sh $contents; }
1831: if ($newline) { print $sh $newline; }
1832: $sh->close();
1833: return $found;
1.86 www 1834: }
1835:
1836: sub getchat {
1.122 ! www 1837: my ($cdom,$cname,$udom,$uname)=@_;
1.87 www 1838: my %hash;
1839: my $proname=&propath($cdom,$cname);
1840: my @entries=();
1.88 albertel 1841: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
1842: &GDBM_READER(),0640)) {
1843: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
1844: untie %hash;
1.86 www 1845: }
1.87 www 1846: return @entries;
1.86 www 1847: }
1848:
1849: sub chatadd {
1.88 albertel 1850: my ($cdom,$cname,$newchat)=@_;
1851: my %hash;
1852: my $proname=&propath($cdom,$cname);
1853: my @entries=();
1854: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
1855: &GDBM_WRCREAT(),0640)) {
1856: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
1857: my $time=time;
1858: my ($lastid)=($entries[$#entries]=~/^(\w+)\:/);
1859: my ($thentime,$idnum)=split(/\_/,$lastid);
1860: my $newid=$time.'_000000';
1861: if ($thentime==$time) {
1862: $idnum=~s/^0+//;
1863: $idnum++;
1864: $idnum=substr('000000'.$idnum,-6,6);
1865: $newid=$time.'_'.$idnum;
1866: }
1867: $hash{$newid}=$newchat;
1868: my $expired=$time-3600;
1869: foreach (keys %hash) {
1870: my ($thistime)=($_=~/(\d+)\_/);
1871: if ($thistime<$expired) {
1.89 www 1872: delete $hash{$_};
1.88 albertel 1873: }
1874: }
1875: untie %hash;
1.86 www 1876: }
1.84 albertel 1877: }
1878:
1879: sub unsub {
1880: my ($fname,$clientip)=@_;
1881: my $result;
1882: if (unlink("$fname.$hostid{$clientip}")) {
1883: $result="ok\n";
1884: } else {
1885: $result="not_subscribed\n";
1886: }
1887: if (-e "$fname.subscription") {
1888: my $found=&addline($fname,$hostid{$clientip},$clientip,'');
1889: if ($found) { $result="ok\n"; }
1890: } else {
1891: if ($result != "ok\n") { $result="not_subscribed\n"; }
1892: }
1893: return $result;
1894: }
1895:
1.101 www 1896: sub currentversion {
1897: my $fname=shift;
1898: my $version=-1;
1899: my $ulsdir='';
1900: if ($fname=~/^(.+)\/[^\/]+$/) {
1901: $ulsdir=$1;
1902: }
1.114 albertel 1903: my ($fnamere1,$fnamere2);
1904: # remove version if already specified
1.101 www 1905: $fname=~s/\.\d+\.(\w+(?:\.meta)*)$/\.$1/;
1.114 albertel 1906: # get the bits that go before and after the version number
1907: if ( $fname=~/^(.*\.)(\w+(?:\.meta)*)$/ ) {
1908: $fnamere1=$1;
1909: $fnamere2='.'.$2;
1910: }
1.101 www 1911: if (-e $fname) { $version=1; }
1912: if (-e $ulsdir) {
1913: if(-d $ulsdir) {
1914: if (opendir(LSDIR,$ulsdir)) {
1.114 albertel 1915:
1.101 www 1916: while ($ulsfn=readdir(LSDIR)) {
1917: # see if this is a regular file (ignore links produced earlier)
1918: my $thisfile=$ulsdir.'/'.$ulsfn;
1919: unless (-l $thisfile) {
1.114 albertel 1920: if ($thisfile=~/\Q$fnamere1\E(\d+)\Q$fnamere2\E/) {
1921: if ($1>$version) { $version=$1; }
1922: }
1.101 www 1923: }
1924: }
1925: closedir(LSDIR);
1926: $version++;
1927: }
1928: }
1929: }
1930: return $version;
1931: }
1932:
1933: sub thisversion {
1934: my $fname=shift;
1935: my $version=-1;
1936: if ($fname=~/\.(\d+)\.\w+(?:\.meta)*$/) {
1937: $version=$1;
1938: }
1939: return $version;
1940: }
1941:
1.84 albertel 1942: sub subscribe {
1943: my ($userinput,$clientip)=@_;
1944: my $result;
1945: my ($cmd,$fname)=split(/:/,$userinput);
1946: my $ownership=&ishome($fname);
1947: if ($ownership eq 'owner') {
1.101 www 1948: # explitly asking for the current version?
1949: unless (-e $fname) {
1950: my $currentversion=¤tversion($fname);
1951: if (&thisversion($fname)==$currentversion) {
1952: if ($fname=~/^(.+)\.\d+\.(\w+(?:\.meta)*)$/) {
1953: my $root=$1;
1954: my $extension=$2;
1955: symlink($root.'.'.$extension,
1956: $root.'.'.$currentversion.'.'.$extension);
1.102 www 1957: unless ($extension=~/\.meta$/) {
1958: symlink($root.'.'.$extension.'.meta',
1959: $root.'.'.$currentversion.'.'.$extension.'.meta');
1960: }
1.101 www 1961: }
1962: }
1963: }
1.84 albertel 1964: if (-e $fname) {
1965: if (-d $fname) {
1966: $result="directory\n";
1967: } else {
1968: if (-e "$fname.$hostid{$clientip}") {&unsub($fname,$clientip);}
1969: $now=time;
1970: my $found=&addline($fname,$hostid{$clientip},$clientip,
1971: "$hostid{$clientip}:$clientip:$now\n");
1972: if ($found) { $result="$fname\n"; }
1973: # if they were subscribed to only meta data, delete that
1974: # subscription, when you subscribe to a file you also get
1975: # the metadata
1976: unless ($fname=~/\.meta$/) { &unsub("$fname.meta",$clientip); }
1977: $fname=~s/\/home\/httpd\/html\/res/raw/;
1978: $fname="http://$thisserver/".$fname;
1979: $result="$fname\n";
1980: }
1981: } else {
1982: $result="not_found\n";
1983: }
1984: } else {
1985: $result="rejected\n";
1986: }
1987: return $result;
1988: }
1.91 albertel 1989:
1990: sub make_passwd_file {
1.98 foxr 1991: my ($uname, $umode,$npass,$passfilename)=@_;
1.91 albertel 1992: my $result="ok\n";
1993: if ($umode eq 'krb4' or $umode eq 'krb5') {
1994: {
1995: my $pf = IO::File->new(">$passfilename");
1996: print $pf "$umode:$npass\n";
1997: }
1998: } elsif ($umode eq 'internal') {
1999: my $salt=time;
2000: $salt=substr($salt,6,2);
2001: my $ncpass=crypt($npass,$salt);
2002: {
2003: &Debug("Creating internal auth");
2004: my $pf = IO::File->new(">$passfilename");
2005: print $pf "internal:$ncpass\n";
2006: }
2007: } elsif ($umode eq 'localauth') {
2008: {
2009: my $pf = IO::File->new(">$passfilename");
2010: print $pf "localauth:$npass\n";
2011: }
2012: } elsif ($umode eq 'unix') {
2013: {
2014: my $execpath="$perlvar{'lonDaemons'}/"."lcuseradd";
2015: {
2016: &Debug("Executing external: ".$execpath);
1.98 foxr 2017: &Debug("user = ".$uname.", Password =". $npass);
2018: my $se = IO::File->new("|$execpath > /home/www/lcuseradd.log");
1.91 albertel 2019: print $se "$uname\n";
2020: print $se "$npass\n";
2021: print $se "$npass\n";
1.97 foxr 2022: }
2023: my $useraddok = $?;
2024: if($useraddok > 0) {
2025: &logthis("Failed lcuseradd: ".&lcuseraddstrerror($useraddok));
1.91 albertel 2026: }
2027: my $pf = IO::File->new(">$passfilename");
2028: print $pf "unix:\n";
2029: }
2030: } elsif ($umode eq 'none') {
2031: {
2032: my $pf = IO::File->new(">$passfilename");
2033: print $pf "none:\n";
2034: }
2035: } else {
2036: $result="auth_mode_error\n";
2037: }
2038: return $result;
1.121 albertel 2039: }
2040:
2041: sub sethost {
2042: my ($remotereq) = @_;
2043: my (undef,$hostid)=split(/:/,$remotereq);
2044: if (!defined($hostid)) { $hostid=$perlvar{'lonHostID'}; }
2045: if ($hostip{$perlvar{'lonHostID'}} eq $hostip{$hostid}) {
2046: $currenthostid=$hostid;
2047: $currentdomainid=$hostdom{$hostid};
2048: &logthis("Setting hostid to $hostid, and domain to $currentdomainid");
2049: } else {
2050: &logthis("Requested host id $hostid not an alias of ".
2051: $perlvar{'lonHostID'}." refusing connection");
2052: return 'unable_to_set';
2053: }
2054: return 'ok';
2055: }
2056:
2057: sub version {
2058: my ($userinput)=@_;
2059: $remoteVERSION=(split(/:/,$userinput))[1];
2060: return "version:$VERSION";
1.91 albertel 2061: }
2062:
1.61 harris41 2063: # ----------------------------------- POD (plain old documentation, CPAN style)
2064:
2065: =head1 NAME
2066:
2067: lond - "LON Daemon" Server (port "LOND" 5663)
2068:
2069: =head1 SYNOPSIS
2070:
1.74 harris41 2071: Usage: B<lond>
2072:
2073: Should only be run as user=www. This is a command-line script which
2074: is invoked by B<loncron>. There is no expectation that a typical user
2075: will manually start B<lond> from the command-line. (In other words,
2076: DO NOT START B<lond> YOURSELF.)
1.61 harris41 2077:
2078: =head1 DESCRIPTION
2079:
1.74 harris41 2080: There are two characteristics associated with the running of B<lond>,
2081: PROCESS MANAGEMENT (starting, stopping, handling child processes)
2082: and SERVER-SIDE ACTIVITIES (password authentication, user creation,
2083: subscriptions, etc). These are described in two large
2084: sections below.
2085:
2086: B<PROCESS MANAGEMENT>
2087:
1.61 harris41 2088: Preforker - server who forks first. Runs as a daemon. HUPs.
2089: Uses IDEA encryption
2090:
1.74 harris41 2091: B<lond> forks off children processes that correspond to the other servers
2092: in the network. Management of these processes can be done at the
2093: parent process level or the child process level.
2094:
2095: B<logs/lond.log> is the location of log messages.
2096:
2097: The process management is now explained in terms of linux shell commands,
2098: subroutines internal to this code, and signal assignments:
2099:
2100: =over 4
2101:
2102: =item *
2103:
2104: PID is stored in B<logs/lond.pid>
2105:
2106: This is the process id number of the parent B<lond> process.
2107:
2108: =item *
2109:
2110: SIGTERM and SIGINT
2111:
2112: Parent signal assignment:
2113: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
2114:
2115: Child signal assignment:
2116: $SIG{INT} = 'DEFAULT'; (and SIGTERM is DEFAULT also)
2117: (The child dies and a SIGALRM is sent to parent, awaking parent from slumber
2118: to restart a new child.)
2119:
2120: Command-line invocations:
2121: B<kill> B<-s> SIGTERM I<PID>
2122: B<kill> B<-s> SIGINT I<PID>
2123:
2124: Subroutine B<HUNTSMAN>:
2125: This is only invoked for the B<lond> parent I<PID>.
2126: This kills all the children, and then the parent.
2127: The B<lonc.pid> file is cleared.
2128:
2129: =item *
2130:
2131: SIGHUP
2132:
2133: Current bug:
2134: This signal can only be processed the first time
2135: on the parent process. Subsequent SIGHUP signals
2136: have no effect.
2137:
2138: Parent signal assignment:
2139: $SIG{HUP} = \&HUPSMAN;
2140:
2141: Child signal assignment:
2142: none (nothing happens)
2143:
2144: Command-line invocations:
2145: B<kill> B<-s> SIGHUP I<PID>
2146:
2147: Subroutine B<HUPSMAN>:
2148: This is only invoked for the B<lond> parent I<PID>,
2149: This kills all the children, and then the parent.
2150: The B<lond.pid> file is cleared.
2151:
2152: =item *
2153:
2154: SIGUSR1
2155:
2156: Parent signal assignment:
2157: $SIG{USR1} = \&USRMAN;
2158:
2159: Child signal assignment:
2160: $SIG{USR1}= \&logstatus;
2161:
2162: Command-line invocations:
2163: B<kill> B<-s> SIGUSR1 I<PID>
2164:
2165: Subroutine B<USRMAN>:
2166: When invoked for the B<lond> parent I<PID>,
2167: SIGUSR1 is sent to all the children, and the status of
2168: each connection is logged.
2169:
2170: =item *
2171:
2172: SIGCHLD
2173:
2174: Parent signal assignment:
2175: $SIG{CHLD} = \&REAPER;
2176:
2177: Child signal assignment:
2178: none
2179:
2180: Command-line invocations:
2181: B<kill> B<-s> SIGCHLD I<PID>
2182:
2183: Subroutine B<REAPER>:
2184: This is only invoked for the B<lond> parent I<PID>.
2185: Information pertaining to the child is removed.
2186: The socket port is cleaned up.
2187:
2188: =back
2189:
2190: B<SERVER-SIDE ACTIVITIES>
2191:
2192: Server-side information can be accepted in an encrypted or non-encrypted
2193: method.
2194:
2195: =over 4
2196:
2197: =item ping
2198:
2199: Query a client in the hosts.tab table; "Are you there?"
2200:
2201: =item pong
2202:
2203: Respond to a ping query.
2204:
2205: =item ekey
2206:
2207: Read in encrypted key, make cipher. Respond with a buildkey.
2208:
2209: =item load
2210:
2211: Respond with CPU load based on a computation upon /proc/loadavg.
2212:
2213: =item currentauth
2214:
2215: Reply with current authentication information (only over an
2216: encrypted channel).
2217:
2218: =item auth
2219:
2220: Only over an encrypted channel, reply as to whether a user's
2221: authentication information can be validated.
2222:
2223: =item passwd
2224:
2225: Allow for a password to be set.
2226:
2227: =item makeuser
2228:
2229: Make a user.
2230:
2231: =item passwd
2232:
2233: Allow for authentication mechanism and password to be changed.
2234:
2235: =item home
1.61 harris41 2236:
1.74 harris41 2237: Respond to a question "are you the home for a given user?"
2238:
2239: =item update
2240:
2241: Update contents of a subscribed resource.
2242:
2243: =item unsubscribe
2244:
2245: The server is unsubscribing from a resource.
2246:
2247: =item subscribe
2248:
2249: The server is subscribing to a resource.
2250:
2251: =item log
2252:
2253: Place in B<logs/lond.log>
2254:
2255: =item put
2256:
2257: stores hash in namespace
2258:
2259: =item rolesput
2260:
2261: put a role into a user's environment
2262:
2263: =item get
2264:
2265: returns hash with keys from array
2266: reference filled in from namespace
2267:
2268: =item eget
2269:
2270: returns hash with keys from array
2271: reference filled in from namesp (encrypts the return communication)
2272:
2273: =item rolesget
2274:
2275: get a role from a user's environment
2276:
2277: =item del
2278:
2279: deletes keys out of array from namespace
2280:
2281: =item keys
2282:
2283: returns namespace keys
2284:
2285: =item dump
2286:
2287: dumps the complete (or key matching regexp) namespace into a hash
2288:
2289: =item store
2290:
2291: stores hash permanently
2292: for this url; hashref needs to be given and should be a \%hashname; the
2293: remaining args aren't required and if they aren't passed or are '' they will
2294: be derived from the ENV
2295:
2296: =item restore
2297:
2298: returns a hash for a given url
2299:
2300: =item querysend
2301:
2302: Tells client about the lonsql process that has been launched in response
2303: to a sent query.
2304:
2305: =item queryreply
2306:
2307: Accept information from lonsql and make appropriate storage in temporary
2308: file space.
2309:
2310: =item idput
2311:
2312: Defines usernames as corresponding to IDs. (These "IDs" are unique identifiers
2313: for each student, defined perhaps by the institutional Registrar.)
2314:
2315: =item idget
2316:
2317: Returns usernames corresponding to IDs. (These "IDs" are unique identifiers
2318: for each student, defined perhaps by the institutional Registrar.)
2319:
2320: =item tmpput
2321:
2322: Accept and store information in temporary space.
2323:
2324: =item tmpget
2325:
2326: Send along temporarily stored information.
2327:
2328: =item ls
2329:
2330: List part of a user's directory.
2331:
2332: =item Hanging up (exit or init)
2333:
2334: What to do when a client tells the server that they (the client)
2335: are leaving the network.
2336:
2337: =item unknown command
2338:
2339: If B<lond> is sent an unknown command (not in the list above),
2340: it replys to the client "unknown_cmd".
2341:
2342: =item UNKNOWN CLIENT
2343:
2344: If the anti-spoofing algorithm cannot verify the client,
2345: the client is rejected (with a "refused" message sent
2346: to the client, and the connection is closed.
2347:
2348: =back
1.61 harris41 2349:
2350: =head1 PREREQUISITES
2351:
2352: IO::Socket
2353: IO::File
2354: Apache::File
2355: Symbol
2356: POSIX
2357: Crypt::IDEA
2358: LWP::UserAgent()
2359: GDBM_File
2360: Authen::Krb4
1.91 albertel 2361: Authen::Krb5
1.61 harris41 2362:
2363: =head1 COREQUISITES
2364:
2365: =head1 OSNAMES
2366:
2367: linux
2368:
2369: =head1 SCRIPT CATEGORIES
2370:
2371: Server/Process
2372:
2373: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>