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