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