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