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