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