Annotation of loncom/lond, revision 1.271
1.1 albertel 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
1.60 www 4: #
1.271 ! raeburn 5: # $Id: lond,v 1.270 2004/12/28 15:09:38 matthew 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
1.178 foxr 23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.60 www 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.193 raeburn 48: use localenroll;
1.265 albertel 49: use localstudentphoto;
1.143 foxr 50: use File::Copy;
1.169 foxr 51: use LONCAPA::ConfigFileEdit;
1.200 matthew 52: use LONCAPA::lonlocal;
53: use LONCAPA::lonssl;
1.221 albertel 54: use Fcntl qw(:flock);
1.1 albertel 55:
1.239 foxr 56: my $DEBUG = 0; # Non zero to enable debug log entries.
1.77 foxr 57:
1.57 www 58: my $status='';
59: my $lastlog='';
60:
1.271 ! raeburn 61: my $VERSION='$Revision: 1.270 $'; #' stupid emacs
1.121 albertel 62: my $remoteVERSION;
1.214 foxr 63: my $currenthostid="default";
1.115 albertel 64: my $currentdomainid;
1.134 albertel 65:
66: my $client;
1.200 matthew 67: my $clientip; # IP address of client.
68: my $clientdns; # DNS name of client.
69: my $clientname; # LonCAPA name of client.
1.140 foxr 70:
1.134 albertel 71: my $server;
1.200 matthew 72: my $thisserver; # DNS of us.
73:
74: my $keymode;
1.198 foxr 75:
1.207 foxr 76: my $cipher; # Cipher key negotiated with client
77: my $tmpsnum = 0; # Id of tmpputs.
78:
1.178 foxr 79: #
80: # Connection type is:
81: # client - All client actions are allowed
82: # manager - only management functions allowed.
83: # both - Both management and client actions are allowed
84: #
1.161 foxr 85:
1.178 foxr 86: my $ConnectionType;
1.161 foxr 87:
1.200 matthew 88: my %hostid; # ID's for hosts in cluster by ip.
89: my %hostdom; # LonCAPA domain for hosts in cluster.
90: my %hostip; # IPs for hosts in cluster.
91: my %hostdns; # ID's of hosts looked up by DNS name.
1.161 foxr 92:
1.178 foxr 93: my %managers; # Ip -> manager names
1.161 foxr 94:
1.178 foxr 95: my %perlvar; # Will have the apache conf defined perl vars.
1.134 albertel 96:
1.178 foxr 97: #
1.207 foxr 98: # The hash below is used for command dispatching, and is therefore keyed on the request keyword.
99: # Each element of the hash contains a reference to an array that contains:
100: # A reference to a sub that executes the request corresponding to the keyword.
101: # A flag that is true if the request must be encoded to be acceptable.
102: # A mask with bits as follows:
103: # CLIENT_OK - Set when the function is allowed by ordinary clients
104: # MANAGER_OK - Set when the function is allowed to manager clients.
105: #
106: my $CLIENT_OK = 1;
107: my $MANAGER_OK = 2;
108: my %Dispatcher;
109:
110:
111: #
1.178 foxr 112: # The array below are password error strings."
113: #
114: my $lastpwderror = 13; # Largest error number from lcpasswd.
115: my @passwderrors = ("ok",
116: "lcpasswd must be run as user 'www'",
117: "lcpasswd got incorrect number of arguments",
118: "lcpasswd did not get the right nubmer of input text lines",
119: "lcpasswd too many simultaneous pwd changes in progress",
120: "lcpasswd User does not exist.",
121: "lcpasswd Incorrect current passwd",
122: "lcpasswd Unable to su to root.",
123: "lcpasswd Cannot set new passwd.",
124: "lcpasswd Username has invalid characters",
125: "lcpasswd Invalid characters in password",
1.223 foxr 126: "lcpasswd User already exists",
127: "lcpasswd Something went wrong with user addition.",
128: "lcpasswd Password mismatch",
129: "lcpasswd Error filename is invalid");
1.97 foxr 130:
131:
1.178 foxr 132: # The array below are lcuseradd error strings.:
1.97 foxr 133:
1.178 foxr 134: my $lastadderror = 13;
135: my @adderrors = ("ok",
136: "User ID mismatch, lcuseradd must run as user www",
137: "lcuseradd Incorrect number of command line parameters must be 3",
138: "lcuseradd Incorrect number of stdinput lines, must be 3",
139: "lcuseradd Too many other simultaneous pwd changes in progress",
140: "lcuseradd User does not exist",
141: "lcuseradd Unable to make www member of users's group",
142: "lcuseradd Unable to su to root",
143: "lcuseradd Unable to set password",
144: "lcuseradd Usrname has invalid characters",
145: "lcuseradd Password has an invalid character",
146: "lcuseradd User already exists",
147: "lcuseradd Could not add user.",
148: "lcuseradd Password mismatch");
1.97 foxr 149:
1.96 foxr 150:
1.207 foxr 151:
152: #
153: # Statistics that are maintained and dislayed in the status line.
154: #
1.212 foxr 155: my $Transactions = 0; # Number of attempted transactions.
156: my $Failures = 0; # Number of transcations failed.
1.207 foxr 157:
158: # ResetStatistics:
159: # Resets the statistics counters:
160: #
161: sub ResetStatistics {
162: $Transactions = 0;
163: $Failures = 0;
164: }
165:
1.200 matthew 166: #------------------------------------------------------------------------
167: #
168: # LocalConnection
169: # Completes the formation of a locally authenticated connection.
170: # This function will ensure that the 'remote' client is really the
171: # local host. If not, the connection is closed, and the function fails.
172: # If so, initcmd is parsed for the name of a file containing the
173: # IDEA session key. The fie is opened, read, deleted and the session
174: # key returned to the caller.
175: #
176: # Parameters:
177: # $Socket - Socket open on client.
178: # $initcmd - The full text of the init command.
179: #
180: # Implicit inputs:
181: # $clientdns - The DNS name of the remote client.
182: # $thisserver - Our DNS name.
183: #
184: # Returns:
185: # IDEA session key on success.
186: # undef on failure.
187: #
188: sub LocalConnection {
189: my ($Socket, $initcmd) = @_;
190: Debug("Attempting local connection: $initcmd client: $clientdns me: $thisserver");
191: if($clientdns ne $thisserver) {
192: &logthis('<font color="red"> LocalConnection rejecting non local: '
193: ."$clientdns ne $thisserver </font>");
194: close $Socket;
195: return undef;
1.224 foxr 196: } else {
1.200 matthew 197: chomp($initcmd); # Get rid of \n in filename.
198: my ($init, $type, $name) = split(/:/, $initcmd);
199: Debug(" Init command: $init $type $name ");
200:
201: # Require that $init = init, and $type = local: Otherwise
202: # the caller is insane:
203:
204: if(($init ne "init") && ($type ne "local")) {
205: &logthis('<font color = "red"> LocalConnection: caller is insane! '
206: ."init = $init, and type = $type </font>");
207: close($Socket);;
208: return undef;
209:
210: }
211: # Now get the key filename:
212:
213: my $IDEAKey = lonlocal::ReadKeyFile($name);
214: return $IDEAKey;
215: }
216: }
217: #------------------------------------------------------------------------------
218: #
219: # SSLConnection
220: # Completes the formation of an ssh authenticated connection. The
221: # socket is promoted to an ssl socket. If this promotion and the associated
222: # certificate exchange are successful, the IDEA key is generated and sent
223: # to the remote peer via the SSL tunnel. The IDEA key is also returned to
224: # the caller after the SSL tunnel is torn down.
225: #
226: # Parameters:
227: # Name Type Purpose
228: # $Socket IO::Socket::INET Plaintext socket.
229: #
230: # Returns:
231: # IDEA key on success.
232: # undef on failure.
233: #
234: sub SSLConnection {
235: my $Socket = shift;
236:
237: Debug("SSLConnection: ");
238: my $KeyFile = lonssl::KeyFile();
239: if(!$KeyFile) {
240: my $err = lonssl::LastError();
241: &logthis("<font color=\"red\"> CRITICAL"
242: ."Can't get key file $err </font>");
243: return undef;
244: }
245: my ($CACertificate,
246: $Certificate) = lonssl::CertificateFile();
247:
248:
249: # If any of the key, certificate or certificate authority
250: # certificate filenames are not defined, this can't work.
251:
252: if((!$Certificate) || (!$CACertificate)) {
253: my $err = lonssl::LastError();
254: &logthis("<font color=\"red\"> CRITICAL"
255: ."Can't get certificates: $err </font>");
256:
257: return undef;
258: }
259: Debug("Key: $KeyFile CA: $CACertificate Cert: $Certificate");
260:
261: # Indicate to our peer that we can procede with
262: # a transition to ssl authentication:
263:
264: print $Socket "ok:ssl\n";
265:
266: Debug("Approving promotion -> ssl");
267: # And do so:
268:
269: my $SSLSocket = lonssl::PromoteServerSocket($Socket,
270: $CACertificate,
271: $Certificate,
272: $KeyFile);
273: if(! ($SSLSocket) ) { # SSL socket promotion failed.
274: my $err = lonssl::LastError();
275: &logthis("<font color=\"red\"> CRITICAL "
276: ."SSL Socket promotion failed: $err </font>");
277: return undef;
278: }
279: Debug("SSL Promotion successful");
280:
281: #
282: # The only thing we'll use the socket for is to send the IDEA key
283: # to the peer:
284:
285: my $Key = lonlocal::CreateCipherKey();
286: print $SSLSocket "$Key\n";
287:
288: lonssl::Close($SSLSocket);
289:
290: Debug("Key exchange complete: $Key");
291:
292: return $Key;
293: }
294: #
295: # InsecureConnection:
296: # If insecure connections are allowd,
297: # exchange a challenge with the client to 'validate' the
298: # client (not really, but that's the protocol):
299: # We produce a challenge string that's sent to the client.
300: # The client must then echo the challenge verbatim to us.
301: #
302: # Parameter:
303: # Socket - Socket open on the client.
304: # Returns:
305: # 1 - success.
306: # 0 - failure (e.g.mismatch or insecure not allowed).
307: #
308: sub InsecureConnection {
309: my $Socket = shift;
310:
311: # Don't even start if insecure connections are not allowed.
312:
313: if(! $perlvar{londAllowInsecure}) { # Insecure connections not allowed.
314: return 0;
315: }
316:
317: # Fabricate a challenge string and send it..
318:
319: my $challenge = "$$".time; # pid + time.
320: print $Socket "$challenge\n";
321: &status("Waiting for challenge reply");
322:
323: my $answer = <$Socket>;
324: $answer =~s/\W//g;
325: if($challenge eq $answer) {
326: return 1;
1.224 foxr 327: } else {
1.200 matthew 328: logthis("<font color='blue'>WARNING client did not respond to challenge</font>");
329: &status("No challenge reqply");
330: return 0;
331: }
332:
333:
334: }
1.251 foxr 335: #
336: # Safely execute a command (as long as it's not a shel command and doesn
337: # not require/rely on shell escapes. The function operates by doing a
338: # a pipe based fork and capturing stdout and stderr from the pipe.
339: #
340: # Formal Parameters:
341: # $line - A line of text to be executed as a command.
342: # Returns:
343: # The output from that command. If the output is multiline the caller
344: # must know how to split up the output.
345: #
346: #
347: sub execute_command {
348: my ($line) = @_;
349: my @words = split(/\s/, $line); # Bust the command up into words.
350: my $output = "";
351:
352: my $pid = open(CHILD, "-|");
353:
354: if($pid) { # Parent process
355: Debug("In parent process for execute_command");
356: my @data = <CHILD>; # Read the child's outupt...
357: close CHILD;
358: foreach my $output_line (@data) {
359: Debug("Adding $output_line");
360: $output .= $output_line; # Presumably has a \n on it.
361: }
362:
363: } else { # Child process
364: close (STDERR);
365: open (STDERR, ">&STDOUT");# Combine stderr, and stdout...
366: exec(@words); # won't return.
367: }
368: return $output;
369: }
370:
1.200 matthew 371:
1.140 foxr 372: # GetCertificate: Given a transaction that requires a certificate,
373: # this function will extract the certificate from the transaction
374: # request. Note that at this point, the only concept of a certificate
375: # is the hostname to which we are connected.
376: #
377: # Parameter:
378: # request - The request sent by our client (this parameterization may
379: # need to change when we really use a certificate granting
380: # authority.
381: #
382: sub GetCertificate {
383: my $request = shift;
384:
385: return $clientip;
386: }
1.161 foxr 387:
1.178 foxr 388: #
389: # Return true if client is a manager.
390: #
391: sub isManager {
392: return (($ConnectionType eq "manager") || ($ConnectionType eq "both"));
393: }
394: #
395: # Return tru if client can do client functions
396: #
397: sub isClient {
398: return (($ConnectionType eq "client") || ($ConnectionType eq "both"));
399: }
1.161 foxr 400:
401:
1.156 foxr 402: #
403: # ReadManagerTable: Reads in the current manager table. For now this is
404: # done on each manager authentication because:
405: # - These authentications are not frequent
406: # - This allows dynamic changes to the manager table
407: # without the need to signal to the lond.
408: #
409: sub ReadManagerTable {
410:
411: # Clean out the old table first..
412:
1.166 foxr 413: foreach my $key (keys %managers) {
414: delete $managers{$key};
415: }
416:
417: my $tablename = $perlvar{'lonTabDir'}."/managers.tab";
418: if (!open (MANAGERS, $tablename)) {
419: logthis('<font color="red">No manager table. Nobody can manage!!</font>');
420: return;
421: }
422: while(my $host = <MANAGERS>) {
423: chomp($host);
424: if ($host =~ "^#") { # Comment line.
425: next;
426: }
427: if (!defined $hostip{$host}) { # This is a non cluster member
1.161 foxr 428: # The entry is of the form:
429: # cluname:hostname
430: # cluname - A 'cluster hostname' is needed in order to negotiate
431: # the host key.
432: # hostname- The dns name of the host.
433: #
1.166 foxr 434: my($cluname, $dnsname) = split(/:/, $host);
435:
436: my $ip = gethostbyname($dnsname);
437: if(defined($ip)) { # bad names don't deserve entry.
438: my $hostip = inet_ntoa($ip);
439: $managers{$hostip} = $cluname;
440: logthis('<font color="green"> registering manager '.
441: "$dnsname as $cluname with $hostip </font>\n");
442: }
443: } else {
444: logthis('<font color="green"> existing host'." $host</font>\n");
445: $managers{$hostip{$host}} = $host; # Use info from cluster tab if clumemeber
446: }
447: }
1.156 foxr 448: }
1.140 foxr 449:
450: #
451: # ValidManager: Determines if a given certificate represents a valid manager.
452: # in this primitive implementation, the 'certificate' is
453: # just the connecting loncapa client name. This is checked
454: # against a valid client list in the configuration.
455: #
456: #
457: sub ValidManager {
458: my $certificate = shift;
459:
1.163 foxr 460: return isManager;
1.140 foxr 461: }
462: #
1.143 foxr 463: # CopyFile: Called as part of the process of installing a
464: # new configuration file. This function copies an existing
465: # file to a backup file.
466: # Parameters:
467: # oldfile - Name of the file to backup.
468: # newfile - Name of the backup file.
469: # Return:
470: # 0 - Failure (errno has failure reason).
471: # 1 - Success.
472: #
473: sub CopyFile {
1.192 foxr 474:
475: my ($oldfile, $newfile) = @_;
1.143 foxr 476:
477: # The file must exist:
478:
479: if(-e $oldfile) {
480:
481: # Read the old file.
482:
483: my $oldfh = IO::File->new("< $oldfile");
484: if(!$oldfh) {
485: return 0;
486: }
487: my @contents = <$oldfh>; # Suck in the entire file.
488:
489: # write the backup file:
490:
491: my $newfh = IO::File->new("> $newfile");
492: if(!(defined $newfh)){
493: return 0;
494: }
495: my $lines = scalar @contents;
496: for (my $i =0; $i < $lines; $i++) {
497: print $newfh ($contents[$i]);
498: }
499:
500: $oldfh->close;
501: $newfh->close;
502:
503: chmod(0660, $newfile);
504:
505: return 1;
506:
507: } else {
508: return 0;
509: }
510: }
1.157 foxr 511: #
512: # Host files are passed out with externally visible host IPs.
513: # If, for example, we are behind a fire-wall or NAT host, our
514: # internally visible IP may be different than the externally
515: # visible IP. Therefore, we always adjust the contents of the
516: # host file so that the entry for ME is the IP that we believe
517: # we have. At present, this is defined as the entry that
518: # DNS has for us. If by some chance we are not able to get a
519: # DNS translation for us, then we assume that the host.tab file
520: # is correct.
521: # BUGBUGBUG - in the future, we really should see if we can
522: # easily query the interface(s) instead.
523: # Parameter(s):
524: # contents - The contents of the host.tab to check.
525: # Returns:
526: # newcontents - The adjusted contents.
527: #
528: #
529: sub AdjustHostContents {
530: my $contents = shift;
531: my $adjusted;
532: my $me = $perlvar{'lonHostID'};
533:
1.166 foxr 534: foreach my $line (split(/\n/,$contents)) {
1.157 foxr 535: if(!(($line eq "") || ($line =~ /^ *\#/) || ($line =~ /^ *$/))) {
536: chomp($line);
537: my ($id,$domain,$role,$name,$ip,$maxcon,$idleto,$mincon)=split(/:/,$line);
538: if ($id eq $me) {
1.166 foxr 539: my $ip = gethostbyname($name);
540: my $ipnew = inet_ntoa($ip);
541: $ip = $ipnew;
1.157 foxr 542: # Reconstruct the host line and append to adjusted:
543:
1.166 foxr 544: my $newline = "$id:$domain:$role:$name:$ip";
545: if($maxcon ne "") { # Not all hosts have loncnew tuning params
546: $newline .= ":$maxcon:$idleto:$mincon";
547: }
548: $adjusted .= $newline."\n";
1.157 foxr 549:
1.166 foxr 550: } else { # Not me, pass unmodified.
551: $adjusted .= $line."\n";
552: }
1.157 foxr 553: } else { # Blank or comment never re-written.
554: $adjusted .= $line."\n"; # Pass blanks and comments as is.
555: }
1.166 foxr 556: }
557: return $adjusted;
1.157 foxr 558: }
1.143 foxr 559: #
560: # InstallFile: Called to install an administrative file:
561: # - The file is created with <name>.tmp
562: # - The <name>.tmp file is then mv'd to <name>
563: # This lugubrious procedure is done to ensure that we are never without
564: # a valid, even if dated, version of the file regardless of who crashes
565: # and when the crash occurs.
566: #
567: # Parameters:
568: # Name of the file
569: # File Contents.
570: # Return:
571: # nonzero - success.
572: # 0 - failure and $! has an errno.
573: #
574: sub InstallFile {
1.192 foxr 575:
576: my ($Filename, $Contents) = @_;
1.143 foxr 577: my $TempFile = $Filename.".tmp";
578:
579: # Open the file for write:
580:
581: my $fh = IO::File->new("> $TempFile"); # Write to temp.
582: if(!(defined $fh)) {
583: &logthis('<font color="red"> Unable to create '.$TempFile."</font>");
584: return 0;
585: }
586: # write the contents of the file:
587:
588: print $fh ($Contents);
589: $fh->close; # In case we ever have a filesystem w. locking
590:
591: chmod(0660, $TempFile);
592:
593: # Now we can move install the file in position.
594:
595: move($TempFile, $Filename);
596:
597: return 1;
598: }
1.200 matthew 599:
600:
1.169 foxr 601: #
602: # ConfigFileFromSelector: converts a configuration file selector
603: # (one of host or domain at this point) into a
604: # configuration file pathname.
605: #
606: # Parameters:
607: # selector - Configuration file selector.
608: # Returns:
609: # Full path to the file or undef if the selector is invalid.
610: #
611: sub ConfigFileFromSelector {
612: my $selector = shift;
613: my $tablefile;
614:
615: my $tabledir = $perlvar{'lonTabDir'}.'/';
616: if ($selector eq "hosts") {
617: $tablefile = $tabledir."hosts.tab";
618: } elsif ($selector eq "domain") {
619: $tablefile = $tabledir."domain.tab";
620: } else {
621: return undef;
622: }
623: return $tablefile;
1.143 foxr 624:
1.169 foxr 625: }
1.143 foxr 626: #
1.141 foxr 627: # PushFile: Called to do an administrative push of a file.
628: # - Ensure the file being pushed is one we support.
629: # - Backup the old file to <filename.saved>
630: # - Separate the contents of the new file out from the
631: # rest of the request.
632: # - Write the new file.
633: # Parameter:
634: # Request - The entire user request. This consists of a : separated
635: # string pushfile:tablename:contents.
636: # NOTE: The contents may have :'s in it as well making things a bit
637: # more interesting... but not much.
638: # Returns:
639: # String to send to client ("ok" or "refused" if bad file).
640: #
641: sub PushFile {
642: my $request = shift;
643: my ($command, $filename, $contents) = split(":", $request, 3);
644:
645: # At this point in time, pushes for only the following tables are
646: # supported:
647: # hosts.tab ($filename eq host).
648: # domain.tab ($filename eq domain).
649: # Construct the destination filename or reject the request.
650: #
651: # lonManage is supposed to ensure this, however this session could be
652: # part of some elaborate spoof that managed somehow to authenticate.
653: #
654:
1.169 foxr 655:
656: my $tablefile = ConfigFileFromSelector($filename);
657: if(! (defined $tablefile)) {
1.141 foxr 658: return "refused";
659: }
660: #
661: # >copy< the old table to the backup table
662: # don't rename in case system crashes/reboots etc. in the time
663: # window between a rename and write.
664: #
665: my $backupfile = $tablefile;
666: $backupfile =~ s/\.tab$/.old/;
1.143 foxr 667: if(!CopyFile($tablefile, $backupfile)) {
668: &logthis('<font color="green"> CopyFile from '.$tablefile." to ".$backupfile." failed </font>");
669: return "error:$!";
670: }
1.141 foxr 671: &logthis('<font color="green"> Pushfile: backed up '
672: .$tablefile." to $backupfile</font>");
673:
1.157 foxr 674: # If the file being pushed is the host file, we adjust the entry for ourself so that the
675: # IP will be our current IP as looked up in dns. Note this is only 99% good as it's possible
676: # to conceive of conditions where we don't have a DNS entry locally. This is possible in a
677: # network sense but it doesn't make much sense in a LonCAPA sense so we ignore (for now)
678: # that possibilty.
679:
680: if($filename eq "host") {
681: $contents = AdjustHostContents($contents);
682: }
683:
1.141 foxr 684: # Install the new file:
685:
1.143 foxr 686: if(!InstallFile($tablefile, $contents)) {
687: &logthis('<font color="red"> Pushfile: unable to install '
1.145 foxr 688: .$tablefile." $! </font>");
1.143 foxr 689: return "error:$!";
1.224 foxr 690: } else {
1.143 foxr 691: &logthis('<font color="green"> Installed new '.$tablefile
692: ."</font>");
693:
694: }
695:
1.141 foxr 696:
697: # Indicate success:
698:
699: return "ok";
700:
701: }
1.145 foxr 702:
703: #
704: # Called to re-init either lonc or lond.
705: #
706: # Parameters:
707: # request - The full request by the client. This is of the form
708: # reinit:<process>
709: # where <process> is allowed to be either of
710: # lonc or lond
711: #
712: # Returns:
713: # The string to be sent back to the client either:
714: # ok - Everything worked just fine.
715: # error:why - There was a failure and why describes the reason.
716: #
717: #
718: sub ReinitProcess {
719: my $request = shift;
720:
1.146 foxr 721:
722: # separate the request (reinit) from the process identifier and
723: # validate it producing the name of the .pid file for the process.
724: #
725: #
726: my ($junk, $process) = split(":", $request);
1.147 foxr 727: my $processpidfile = $perlvar{'lonDaemons'}.'/logs/';
1.146 foxr 728: if($process eq 'lonc') {
729: $processpidfile = $processpidfile."lonc.pid";
1.147 foxr 730: if (!open(PIDFILE, "< $processpidfile")) {
731: return "error:Open failed for $processpidfile";
732: }
733: my $loncpid = <PIDFILE>;
734: close(PIDFILE);
735: logthis('<font color="red"> Reinitializing lonc pid='.$loncpid
736: ."</font>");
737: kill("USR2", $loncpid);
1.146 foxr 738: } elsif ($process eq 'lond') {
1.147 foxr 739: logthis('<font color="red"> Reinitializing self (lond) </font>');
740: &UpdateHosts; # Lond is us!!
1.146 foxr 741: } else {
742: &logthis('<font color="yellow" Invalid reinit request for '.$process
743: ."</font>");
744: return "error:Invalid process identifier $process";
745: }
1.145 foxr 746: return 'ok';
747: }
1.168 foxr 748: # Validate a line in a configuration file edit script:
749: # Validation includes:
750: # - Ensuring the command is valid.
751: # - Ensuring the command has sufficient parameters
752: # Parameters:
753: # scriptline - A line to validate (\n has been stripped for what it's worth).
1.167 foxr 754: #
1.168 foxr 755: # Return:
756: # 0 - Invalid scriptline.
757: # 1 - Valid scriptline
758: # NOTE:
759: # Only the command syntax is checked, not the executability of the
760: # command.
761: #
762: sub isValidEditCommand {
763: my $scriptline = shift;
764:
765: # Line elements are pipe separated:
766:
767: my ($command, $key, $newline) = split(/\|/, $scriptline);
768: &logthis('<font color="green"> isValideditCommand checking: '.
769: "Command = '$command', Key = '$key', Newline = '$newline' </font>\n");
770:
771: if ($command eq "delete") {
772: #
773: # key with no newline.
774: #
775: if( ($key eq "") || ($newline ne "")) {
776: return 0; # Must have key but no newline.
777: } else {
778: return 1; # Valid syntax.
779: }
1.169 foxr 780: } elsif ($command eq "replace") {
1.168 foxr 781: #
782: # key and newline:
783: #
784: if (($key eq "") || ($newline eq "")) {
785: return 0;
786: } else {
787: return 1;
788: }
1.169 foxr 789: } elsif ($command eq "append") {
790: if (($key ne "") && ($newline eq "")) {
791: return 1;
792: } else {
793: return 0;
794: }
1.168 foxr 795: } else {
796: return 0; # Invalid command.
797: }
798: return 0; # Should not get here!!!
799: }
1.169 foxr 800: #
801: # ApplyEdit - Applies an edit command to a line in a configuration
802: # file. It is the caller's responsiblity to validate the
803: # edit line.
804: # Parameters:
805: # $directive - A single edit directive to apply.
806: # Edit directives are of the form:
807: # append|newline - Appends a new line to the file.
808: # replace|key|newline - Replaces the line with key value 'key'
809: # delete|key - Deletes the line with key value 'key'.
810: # $editor - A config file editor object that contains the
811: # file being edited.
812: #
813: sub ApplyEdit {
1.192 foxr 814:
815: my ($directive, $editor) = @_;
1.169 foxr 816:
817: # Break the directive down into its command and its parameters
818: # (at most two at this point. The meaning of the parameters, if in fact
819: # they exist depends on the command).
820:
821: my ($command, $p1, $p2) = split(/\|/, $directive);
822:
823: if($command eq "append") {
824: $editor->Append($p1); # p1 - key p2 null.
825: } elsif ($command eq "replace") {
826: $editor->ReplaceLine($p1, $p2); # p1 - key p2 = newline.
827: } elsif ($command eq "delete") {
828: $editor->DeleteLine($p1); # p1 - key p2 null.
829: } else { # Should not get here!!!
830: die "Invalid command given to ApplyEdit $command"
831: }
832: }
833: #
834: # AdjustOurHost:
835: # Adjusts a host file stored in a configuration file editor object
836: # for the true IP address of this host. This is necessary for hosts
837: # that live behind a firewall.
838: # Those hosts have a publicly distributed IP of the firewall, but
839: # internally must use their actual IP. We assume that a given
840: # host only has a single IP interface for now.
841: # Formal Parameters:
842: # editor - The configuration file editor to adjust. This
843: # editor is assumed to contain a hosts.tab file.
844: # Strategy:
845: # - Figure out our hostname.
846: # - Lookup the entry for this host.
847: # - Modify the line to contain our IP
848: # - Do a replace for this host.
849: sub AdjustOurHost {
850: my $editor = shift;
851:
852: # figure out who I am.
853:
854: my $myHostName = $perlvar{'lonHostID'}; # LonCAPA hostname.
855:
856: # Get my host file entry.
857:
858: my $ConfigLine = $editor->Find($myHostName);
859: if(! (defined $ConfigLine)) {
860: die "AdjustOurHost - no entry for me in hosts file $myHostName";
861: }
862: # figure out my IP:
863: # Use the config line to get my hostname.
864: # Use gethostbyname to translate that into an IP address.
865: #
866: my ($id,$domain,$role,$name,$ip,$maxcon,$idleto,$mincon) = split(/:/,$ConfigLine);
867: my $BinaryIp = gethostbyname($name);
868: my $ip = inet_ntoa($ip);
869: #
870: # Reassemble the config line from the elements in the list.
871: # Note that if the loncnew items were not present before, they will
872: # be now even if they would be empty
873: #
874: my $newConfigLine = $id;
875: foreach my $item ($domain, $role, $name, $ip, $maxcon, $idleto, $mincon) {
876: $newConfigLine .= ":".$item;
877: }
878: # Replace the line:
879:
880: $editor->ReplaceLine($id, $newConfigLine);
881:
882: }
883: #
884: # ReplaceConfigFile:
885: # Replaces a configuration file with the contents of a
886: # configuration file editor object.
887: # This is done by:
888: # - Copying the target file to <filename>.old
889: # - Writing the new file to <filename>.tmp
890: # - Moving <filename.tmp> -> <filename>
891: # This laborious process ensures that the system is never without
892: # a configuration file that's at least valid (even if the contents
893: # may be dated).
894: # Parameters:
895: # filename - Name of the file to modify... this is a full path.
896: # editor - Editor containing the file.
897: #
898: sub ReplaceConfigFile {
1.192 foxr 899:
900: my ($filename, $editor) = @_;
1.168 foxr 901:
1.169 foxr 902: CopyFile ($filename, $filename.".old");
903:
904: my $contents = $editor->Get(); # Get the contents of the file.
905:
906: InstallFile($filename, $contents);
907: }
1.168 foxr 908: #
909: #
910: # Called to edit a configuration table file
1.167 foxr 911: # Parameters:
912: # request - The entire command/request sent by lonc or lonManage
913: # Return:
914: # The reply to send to the client.
1.168 foxr 915: #
1.167 foxr 916: sub EditFile {
917: my $request = shift;
918:
919: # Split the command into it's pieces: edit:filetype:script
920:
1.168 foxr 921: my ($request, $filetype, $script) = split(/:/, $request,3); # : in script
1.167 foxr 922:
923: # Check the pre-coditions for success:
924:
925: if($request != "edit") { # Something is amiss afoot alack.
926: return "error:edit request detected, but request != 'edit'\n";
927: }
928: if( ($filetype ne "hosts") &&
929: ($filetype ne "domain")) {
930: return "error:edit requested with invalid file specifier: $filetype \n";
931: }
932:
933: # Split the edit script and check it's validity.
1.168 foxr 934:
935: my @scriptlines = split(/\n/, $script); # one line per element.
936: my $linecount = scalar(@scriptlines);
937: for(my $i = 0; $i < $linecount; $i++) {
938: chomp($scriptlines[$i]);
939: if(!isValidEditCommand($scriptlines[$i])) {
940: return "error:edit with bad script line: '$scriptlines[$i]' \n";
941: }
942: }
1.145 foxr 943:
1.167 foxr 944: # Execute the edit operation.
1.169 foxr 945: # - Create a config file editor for the appropriate file and
946: # - execute each command in the script:
947: #
948: my $configfile = ConfigFileFromSelector($filetype);
949: if (!(defined $configfile)) {
950: return "refused\n";
951: }
952: my $editor = ConfigFileEdit->new($configfile);
1.167 foxr 953:
1.169 foxr 954: for (my $i = 0; $i < $linecount; $i++) {
955: ApplyEdit($scriptlines[$i], $editor);
956: }
957: # If the file is the host file, ensure that our host is
958: # adjusted to have our ip:
959: #
960: if($filetype eq "host") {
961: AdjustOurHost($editor);
962: }
963: # Finally replace the current file with our file.
964: #
965: ReplaceConfigFile($configfile, $editor);
1.167 foxr 966:
967: return "ok\n";
968: }
1.207 foxr 969:
970: #---------------------------------------------------------------
971: #
972: # Manipulation of hash based databases (factoring out common code
973: # for later use as we refactor.
974: #
975: # Ties a domain level resource file to a hash.
976: # If requested a history entry is created in the associated hist file.
977: #
978: # Parameters:
979: # domain - Name of the domain in which the resource file lives.
980: # namespace - Name of the hash within that domain.
981: # how - How to tie the hash (e.g. GDBM_WRCREAT()).
982: # loghead - Optional parameter, if present a log entry is created
983: # in the associated history file and this is the first part
984: # of that entry.
985: # logtail - Goes along with loghead, The actual logentry is of the
986: # form $loghead:<timestamp>:logtail.
987: # Returns:
988: # Reference to a hash bound to the db file or alternatively undef
989: # if the tie failed.
990: #
1.209 albertel 991: sub tie_domain_hash {
1.210 albertel 992: my ($domain,$namespace,$how,$loghead,$logtail) = @_;
1.207 foxr 993:
994: # Filter out any whitespace in the domain name:
995:
996: $domain =~ s/\W//g;
997:
998: # We have enough to go on to tie the hash:
999:
1000: my $user_top_dir = $perlvar{'lonUsersDir'};
1001: my $domain_dir = $user_top_dir."/$domain";
1002: my $resource_file = $domain_dir."/$namespace.db";
1003: my %hash;
1004: if(tie(%hash, 'GDBM_File', $resource_file, $how, 0640)) {
1.211 albertel 1005: if (defined($loghead)) { # Need to log the operation.
1.210 albertel 1006: my $logFh = IO::File->new(">>$domain_dir/$namespace.hist");
1.207 foxr 1007: if($logFh) {
1008: my $timestamp = time;
1009: print $logFh "$loghead:$timestamp:$logtail\n";
1010: }
1.210 albertel 1011: $logFh->close;
1.207 foxr 1012: }
1013: return \%hash; # Return the tied hash.
1.210 albertel 1014: } else {
1.207 foxr 1015: return undef; # Tie failed.
1016: }
1017: }
1018:
1019: #
1020: # Ties a user's resource file to a hash.
1021: # If necessary, an appropriate history
1022: # log file entry is made as well.
1023: # This sub factors out common code from the subs that manipulate
1024: # the various gdbm files that keep keyword value pairs.
1025: # Parameters:
1026: # domain - Name of the domain the user is in.
1027: # user - Name of the 'current user'.
1028: # namespace - Namespace representing the file to tie.
1029: # how - What the tie is done to (e.g. GDBM_WRCREAT().
1030: # loghead - Optional first part of log entry if there may be a
1031: # history file.
1032: # what - Optional tail of log entry if there may be a history
1033: # file.
1034: # Returns:
1035: # hash to which the database is tied. It's up to the caller to untie.
1036: # undef if the has could not be tied.
1037: #
1.210 albertel 1038: sub tie_user_hash {
1039: my ($domain,$user,$namespace,$how,$loghead,$what) = @_;
1.207 foxr 1040:
1041: $namespace=~s/\//\_/g; # / -> _
1042: $namespace=~s/\W//g; # whitespace eliminated.
1043: my $proname = propath($domain, $user);
1044:
1045: # Tie the database.
1046:
1047: my %hash;
1048: if(tie(%hash, 'GDBM_File', "$proname/$namespace.db",
1049: $how, 0640)) {
1.209 albertel 1050: # If this is a namespace for which a history is kept,
1051: # make the history log entry:
1.252 albertel 1052: if (($namespace !~/^nohist\_/) && (defined($loghead))) {
1.209 albertel 1053: my $args = scalar @_;
1054: Debug(" Opening history: $namespace $args");
1055: my $hfh = IO::File->new(">>$proname/$namespace.hist");
1056: if($hfh) {
1057: my $now = time;
1058: print $hfh "$loghead:$now:$what\n";
1059: }
1.210 albertel 1060: $hfh->close;
1.209 albertel 1061: }
1.207 foxr 1062: return \%hash;
1.209 albertel 1063: } else {
1.207 foxr 1064: return undef;
1065: }
1066:
1067: }
1.214 foxr 1068:
1.255 foxr 1069: # read_profile
1070: #
1071: # Returns a set of specific entries from a user's profile file.
1072: # this is a utility function that is used by both get_profile_entry and
1073: # get_profile_entry_encrypted.
1074: #
1075: # Parameters:
1076: # udom - Domain in which the user exists.
1077: # uname - User's account name (loncapa account)
1078: # namespace - The profile namespace to open.
1079: # what - A set of & separated queries.
1080: # Returns:
1081: # If all ok: - The string that needs to be shipped back to the user.
1082: # If failure - A string that starts with error: followed by the failure
1083: # reason.. note that this probabyl gets shipped back to the
1084: # user as well.
1085: #
1086: sub read_profile {
1087: my ($udom, $uname, $namespace, $what) = @_;
1088:
1089: my $hashref = &tie_user_hash($udom, $uname, $namespace,
1090: &GDBM_READER());
1091: if ($hashref) {
1092: my @queries=split(/\&/,$what);
1093: my $qresult='';
1094:
1095: for (my $i=0;$i<=$#queries;$i++) {
1096: $qresult.="$hashref->{$queries[$i]}&"; # Presumably failure gives empty string.
1097: }
1098: $qresult=~s/\&$//; # Remove trailing & from last lookup.
1099: if (untie %$hashref) {
1100: return $qresult;
1101: } else {
1102: return "error: ".($!+0)." untie (GDBM) Failed";
1103: }
1104: } else {
1105: if ($!+0 == 2) {
1106: return "error:No such file or GDBM reported bad block error";
1107: } else {
1108: return "error: ".($!+0)." tie (GDBM) Failed";
1109: }
1110: }
1111:
1112: }
1.214 foxr 1113: #--------------------- Request Handlers --------------------------------------------
1114: #
1.215 foxr 1115: # By convention each request handler registers itself prior to the sub
1116: # declaration:
1.214 foxr 1117: #
1118:
1.216 foxr 1119: #++
1120: #
1.214 foxr 1121: # Handles ping requests.
1122: # Parameters:
1123: # $cmd - the actual keyword that invoked us.
1124: # $tail - the tail of the request that invoked us.
1125: # $replyfd- File descriptor connected to the client
1126: # Implicit Inputs:
1127: # $currenthostid - Global variable that carries the name of the host we are
1128: # known as.
1129: # Returns:
1130: # 1 - Ok to continue processing.
1131: # 0 - Program should exit.
1132: # Side effects:
1133: # Reply information is sent to the client.
1134: sub ping_handler {
1135: my ($cmd, $tail, $client) = @_;
1136: Debug("$cmd $tail $client .. $currenthostid:");
1137:
1138: Reply( $client,"$currenthostid\n","$cmd:$tail");
1139:
1140: return 1;
1141: }
1142: ®ister_handler("ping", \&ping_handler, 0, 1, 1); # Ping unencoded, client or manager.
1143:
1.216 foxr 1144: #++
1.215 foxr 1145: #
1146: # Handles pong requests. Pong replies with our current host id, and
1147: # the results of a ping sent to us via our lonc.
1148: #
1149: # Parameters:
1150: # $cmd - the actual keyword that invoked us.
1151: # $tail - the tail of the request that invoked us.
1152: # $replyfd- File descriptor connected to the client
1153: # Implicit Inputs:
1154: # $currenthostid - Global variable that carries the name of the host we are
1155: # connected to.
1156: # Returns:
1157: # 1 - Ok to continue processing.
1158: # 0 - Program should exit.
1159: # Side effects:
1160: # Reply information is sent to the client.
1161: sub pong_handler {
1162: my ($cmd, $tail, $replyfd) = @_;
1163:
1164: my $reply=&reply("ping",$clientname);
1165: &Reply( $replyfd, "$currenthostid:$reply\n", "$cmd:$tail");
1166: return 1;
1167: }
1168: ®ister_handler("pong", \&pong_handler, 0, 1, 1); # Pong unencoded, client or manager
1169:
1.216 foxr 1170: #++
1171: # Called to establish an encrypted session key with the remote client.
1172: # Note that with secure lond, in most cases this function is never
1173: # invoked. Instead, the secure session key is established either
1174: # via a local file that's locked down tight and only lives for a short
1175: # time, or via an ssl tunnel...and is generated from a bunch-o-random
1176: # bits from /dev/urandom, rather than the predictable pattern used by
1177: # by this sub. This sub is only used in the old-style insecure
1178: # key negotiation.
1179: # Parameters:
1180: # $cmd - the actual keyword that invoked us.
1181: # $tail - the tail of the request that invoked us.
1182: # $replyfd- File descriptor connected to the client
1183: # Implicit Inputs:
1184: # $currenthostid - Global variable that carries the name of the host
1185: # known as.
1186: # $clientname - Global variable that carries the name of the hsot we're connected to.
1187: # Returns:
1188: # 1 - Ok to continue processing.
1189: # 0 - Program should exit.
1190: # Implicit Outputs:
1191: # Reply information is sent to the client.
1192: # $cipher is set with a reference to a new IDEA encryption object.
1193: #
1194: sub establish_key_handler {
1195: my ($cmd, $tail, $replyfd) = @_;
1196:
1197: my $buildkey=time.$$.int(rand 100000);
1198: $buildkey=~tr/1-6/A-F/;
1199: $buildkey=int(rand 100000).$buildkey.int(rand 100000);
1200: my $key=$currenthostid.$clientname;
1201: $key=~tr/a-z/A-Z/;
1202: $key=~tr/G-P/0-9/;
1203: $key=~tr/Q-Z/0-9/;
1204: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
1205: $key=substr($key,0,32);
1206: my $cipherkey=pack("H32",$key);
1207: $cipher=new IDEA $cipherkey;
1208: &Reply($replyfd, "$buildkey\n", "$cmd:$tail");
1209:
1210: return 1;
1211:
1212: }
1213: ®ister_handler("ekey", \&establish_key_handler, 0, 1,1);
1214:
1.217 foxr 1215: # Handler for the load command. Returns the current system load average
1216: # to the requestor.
1217: #
1218: # Parameters:
1219: # $cmd - the actual keyword that invoked us.
1220: # $tail - the tail of the request that invoked us.
1221: # $replyfd- File descriptor connected to the client
1222: # Implicit Inputs:
1223: # $currenthostid - Global variable that carries the name of the host
1224: # known as.
1225: # $clientname - Global variable that carries the name of the hsot we're connected to.
1226: # Returns:
1227: # 1 - Ok to continue processing.
1228: # 0 - Program should exit.
1229: # Side effects:
1230: # Reply information is sent to the client.
1231: sub load_handler {
1232: my ($cmd, $tail, $replyfd) = @_;
1233:
1234: # Get the load average from /proc/loadavg and calculate it as a percentage of
1235: # the allowed load limit as set by the perl global variable lonLoadLim
1236:
1237: my $loadavg;
1238: my $loadfile=IO::File->new('/proc/loadavg');
1239:
1240: $loadavg=<$loadfile>;
1241: $loadavg =~ s/\s.*//g; # Extract the first field only.
1242:
1243: my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
1244:
1245: &Reply( $replyfd, "$loadpercent\n", "$cmd:$tail");
1246:
1247: return 1;
1248: }
1.263 albertel 1249: ®ister_handler("load", \&load_handler, 0, 1, 0);
1.217 foxr 1250:
1251: #
1252: # Process the userload request. This sub returns to the client the current
1253: # user load average. It can be invoked either by clients or managers.
1254: #
1255: # Parameters:
1256: # $cmd - the actual keyword that invoked us.
1257: # $tail - the tail of the request that invoked us.
1258: # $replyfd- File descriptor connected to the client
1259: # Implicit Inputs:
1260: # $currenthostid - Global variable that carries the name of the host
1261: # known as.
1262: # $clientname - Global variable that carries the name of the hsot we're connected to.
1263: # Returns:
1264: # 1 - Ok to continue processing.
1265: # 0 - Program should exit
1266: # Implicit inputs:
1267: # whatever the userload() function requires.
1268: # Implicit outputs:
1269: # the reply is written to the client.
1270: #
1271: sub user_load_handler {
1272: my ($cmd, $tail, $replyfd) = @_;
1273:
1274: my $userloadpercent=&userload();
1275: &Reply($replyfd, "$userloadpercent\n", "$cmd:$tail");
1276:
1277: return 1;
1278: }
1.263 albertel 1279: ®ister_handler("userload", \&user_load_handler, 0, 1, 0);
1.217 foxr 1280:
1.218 foxr 1281: # Process a request for the authorization type of a user:
1282: # (userauth).
1283: #
1284: # Parameters:
1285: # $cmd - the actual keyword that invoked us.
1286: # $tail - the tail of the request that invoked us.
1287: # $replyfd- File descriptor connected to the client
1288: # Returns:
1289: # 1 - Ok to continue processing.
1290: # 0 - Program should exit
1291: # Implicit outputs:
1292: # The user authorization type is written to the client.
1293: #
1294: sub user_authorization_type {
1295: my ($cmd, $tail, $replyfd) = @_;
1296:
1297: my $userinput = "$cmd:$tail";
1298:
1299: # Pull the domain and username out of the command tail.
1.222 foxr 1300: # and call get_auth_type to determine the authentication type.
1.218 foxr 1301:
1302: my ($udom,$uname)=split(/:/,$tail);
1.222 foxr 1303: my $result = &get_auth_type($udom, $uname);
1.218 foxr 1304: if($result eq "nouser") {
1305: &Failure( $replyfd, "unknown_user\n", $userinput);
1306: } else {
1307: #
1.222 foxr 1308: # We only want to pass the second field from get_auth_type
1.218 foxr 1309: # for ^krb.. otherwise we'll be handing out the encrypted
1310: # password for internals e.g.
1311: #
1312: my ($type,$otherinfo) = split(/:/,$result);
1313: if($type =~ /^krb/) {
1314: $type = $result;
1.269 raeburn 1315: } else {
1316: $type .= ':';
1317: }
1318: &Reply( $replyfd, "$type\n", $userinput);
1.218 foxr 1319: }
1320:
1321: return 1;
1322: }
1323: ®ister_handler("currentauth", \&user_authorization_type, 1, 1, 0);
1324:
1325: # Process a request by a manager to push a hosts or domain table
1326: # to us. We pick apart the command and pass it on to the subs
1327: # that already exist to do this.
1328: #
1329: # Parameters:
1330: # $cmd - the actual keyword that invoked us.
1331: # $tail - the tail of the request that invoked us.
1332: # $client - File descriptor connected to the client
1333: # Returns:
1334: # 1 - Ok to continue processing.
1335: # 0 - Program should exit
1336: # Implicit Output:
1337: # a reply is written to the client.
1338: sub push_file_handler {
1339: my ($cmd, $tail, $client) = @_;
1340:
1341: my $userinput = "$cmd:$tail";
1342:
1343: # At this time we only know that the IP of our partner is a valid manager
1344: # the code below is a hook to do further authentication (e.g. to resolve
1345: # spoofing).
1346:
1347: my $cert = &GetCertificate($userinput);
1348: if(&ValidManager($cert)) {
1349:
1350: # Now presumably we have the bona fides of both the peer host and the
1351: # process making the request.
1352:
1353: my $reply = &PushFile($userinput);
1354: &Reply($client, "$reply\n", $userinput);
1355:
1356: } else {
1357: &Failure( $client, "refused\n", $userinput);
1358: }
1.219 foxr 1359: return 1;
1.218 foxr 1360: }
1361: ®ister_handler("pushfile", \&push_file_handler, 1, 0, 1);
1362:
1.243 banghart 1363: #
1364: # du - list the disk usuage of a directory recursively.
1365: #
1366: # note: stolen code from the ls file handler
1367: # under construction by Rick Banghart
1368: # .
1369: # Parameters:
1370: # $cmd - The command that dispatched us (du).
1371: # $ududir - The directory path to list... I'm not sure what this
1372: # is relative as things like ls:. return e.g.
1373: # no_such_dir.
1374: # $client - Socket open on the client.
1375: # Returns:
1376: # 1 - indicating that the daemon should not disconnect.
1377: # Side Effects:
1378: # The reply is written to $client.
1379: #
1380: sub du_handler {
1381: my ($cmd, $ududir, $client) = @_;
1.251 foxr 1382: my ($ududir) = split(/:/,$ududir); # Make 'telnet' testing easier.
1383: my $userinput = "$cmd:$ududir";
1384:
1.245 albertel 1385: if ($ududir=~/\.\./ || $ududir!~m|^/home/httpd/|) {
1386: &Failure($client,"refused\n","$cmd:$ududir");
1387: return 1;
1388: }
1.249 foxr 1389: # Since $ududir could have some nasties in it,
1390: # we will require that ududir is a valid
1391: # directory. Just in case someone tries to
1392: # slip us a line like .;(cd /home/httpd rm -rf*)
1393: # etc.
1394: #
1395: if (-d $ududir) {
1396: # And as Shakespeare would say to make
1.251 foxr 1397: # assurance double sure,
1398: # use execute_command to ensure that the command is not executed in
1399: # a shell that can screw us up.
1400:
1401: my $duout = execute_command("du -ks $ududir");
1.249 foxr 1402: $duout=~s/[^\d]//g; #preserve only the numbers
1403: &Reply($client,"$duout\n","$cmd:$ududir");
1404: } else {
1.251 foxr 1405:
1406: &Failure($client, "bad_directory:$ududir\n","$cmd:$ududir");
1407:
1.249 foxr 1408: }
1.243 banghart 1409: return 1;
1410: }
1411: ®ister_handler("du", \&du_handler, 0, 1, 0);
1.218 foxr 1412:
1.239 foxr 1413: #
1414: # ls - list the contents of a directory. For each file in the
1415: # selected directory the filename followed by the full output of
1416: # the stat function is returned. The returned info for each
1417: # file are separated by ':'. The stat fields are separated by &'s.
1418: # Parameters:
1419: # $cmd - The command that dispatched us (ls).
1420: # $ulsdir - The directory path to list... I'm not sure what this
1421: # is relative as things like ls:. return e.g.
1422: # no_such_dir.
1423: # $client - Socket open on the client.
1424: # Returns:
1425: # 1 - indicating that the daemon should not disconnect.
1426: # Side Effects:
1427: # The reply is written to $client.
1428: #
1429: sub ls_handler {
1430: my ($cmd, $ulsdir, $client) = @_;
1431:
1432: my $userinput = "$cmd:$ulsdir";
1433:
1434: my $obs;
1435: my $rights;
1436: my $ulsout='';
1437: my $ulsfn;
1438: if (-e $ulsdir) {
1439: if(-d $ulsdir) {
1440: if (opendir(LSDIR,$ulsdir)) {
1441: while ($ulsfn=readdir(LSDIR)) {
1442: undef $obs, $rights;
1443: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1444: #We do some obsolete checking here
1445: if(-e $ulsdir.'/'.$ulsfn.".meta") {
1446: open(FILE, $ulsdir.'/'.$ulsfn.".meta");
1447: my @obsolete=<FILE>;
1448: foreach my $obsolete (@obsolete) {
1449: if($obsolete =~ m|(<obsolete>)(on)|) { $obs = 1; }
1450: if($obsolete =~ m|(<copyright>)(default)|) { $rights = 1; }
1451: }
1452: }
1453: $ulsout.=$ulsfn.'&'.join('&',@ulsstats);
1454: if($obs eq '1') { $ulsout.="&1"; }
1455: else { $ulsout.="&0"; }
1456: if($rights eq '1') { $ulsout.="&1:"; }
1457: else { $ulsout.="&0:"; }
1458: }
1459: closedir(LSDIR);
1460: }
1461: } else {
1462: my @ulsstats=stat($ulsdir);
1463: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1464: }
1465: } else {
1466: $ulsout='no_such_dir';
1467: }
1468: if ($ulsout eq '') { $ulsout='empty'; }
1.249 foxr 1469: &Reply($client, "$ulsout\n", $userinput); # This supports debug logging.
1.239 foxr 1470:
1471: return 1;
1472:
1473: }
1474: ®ister_handler("ls", \&ls_handler, 0, 1, 0);
1475:
1.218 foxr 1476: # Process a reinit request. Reinit requests that either
1477: # lonc or lond be reinitialized so that an updated
1478: # host.tab or domain.tab can be processed.
1479: #
1480: # Parameters:
1481: # $cmd - the actual keyword that invoked us.
1482: # $tail - the tail of the request that invoked us.
1483: # $client - File descriptor connected to the client
1484: # Returns:
1485: # 1 - Ok to continue processing.
1486: # 0 - Program should exit
1487: # Implicit output:
1488: # a reply is sent to the client.
1489: #
1490: sub reinit_process_handler {
1491: my ($cmd, $tail, $client) = @_;
1492:
1493: my $userinput = "$cmd:$tail";
1494:
1495: my $cert = &GetCertificate($userinput);
1496: if(&ValidManager($cert)) {
1497: chomp($userinput);
1498: my $reply = &ReinitProcess($userinput);
1499: &Reply( $client, "$reply\n", $userinput);
1500: } else {
1501: &Failure( $client, "refused\n", $userinput);
1502: }
1503: return 1;
1504: }
1505: ®ister_handler("reinit", \&reinit_process_handler, 1, 0, 1);
1506:
1507: # Process the editing script for a table edit operation.
1508: # the editing operation must be encrypted and requested by
1509: # a manager host.
1510: #
1511: # Parameters:
1512: # $cmd - the actual keyword that invoked us.
1513: # $tail - the tail of the request that invoked us.
1514: # $client - File descriptor connected to the client
1515: # Returns:
1516: # 1 - Ok to continue processing.
1517: # 0 - Program should exit
1518: # Implicit output:
1519: # a reply is sent to the client.
1520: #
1521: sub edit_table_handler {
1522: my ($command, $tail, $client) = @_;
1523:
1524: my $userinput = "$command:$tail";
1525:
1526: my $cert = &GetCertificate($userinput);
1527: if(&ValidManager($cert)) {
1528: my($filetype, $script) = split(/:/, $tail);
1529: if (($filetype eq "hosts") ||
1530: ($filetype eq "domain")) {
1531: if($script ne "") {
1532: &Reply($client, # BUGBUG - EditFile
1533: &EditFile($userinput), # could fail.
1534: $userinput);
1535: } else {
1536: &Failure($client,"refused\n",$userinput);
1537: }
1538: } else {
1539: &Failure($client,"refused\n",$userinput);
1540: }
1541: } else {
1542: &Failure($client,"refused\n",$userinput);
1543: }
1544: return 1;
1545: }
1.263 albertel 1546: ®ister_handler("edit", \&edit_table_handler, 1, 0, 1);
1.218 foxr 1547:
1.220 foxr 1548: #
1549: # Authenticate a user against the LonCAPA authentication
1550: # database. Note that there are several authentication
1551: # possibilities:
1552: # - unix - The user can be authenticated against the unix
1553: # password file.
1554: # - internal - The user can be authenticated against a purely
1555: # internal per user password file.
1556: # - kerberos - The user can be authenticated against either a kerb4 or kerb5
1557: # ticket granting authority.
1558: # - user - The person tailoring LonCAPA can supply a user authentication
1559: # mechanism that is per system.
1560: #
1561: # Parameters:
1562: # $cmd - The command that got us here.
1563: # $tail - Tail of the command (remaining parameters).
1564: # $client - File descriptor connected to client.
1565: # Returns
1566: # 0 - Requested to exit, caller should shut down.
1567: # 1 - Continue processing.
1568: # Implicit inputs:
1569: # The authentication systems describe above have their own forms of implicit
1570: # input into the authentication process that are described above.
1571: #
1572: sub authenticate_handler {
1573: my ($cmd, $tail, $client) = @_;
1574:
1575:
1576: # Regenerate the full input line
1577:
1578: my $userinput = $cmd.":".$tail;
1579:
1580: # udom - User's domain.
1581: # uname - Username.
1582: # upass - User's password.
1583:
1584: my ($udom,$uname,$upass)=split(/:/,$tail);
1585: &Debug(" Authenticate domain = $udom, user = $uname, password = $upass");
1586: chomp($upass);
1587: $upass=&unescape($upass);
1588:
1589: my $pwdcorrect = &validate_user($udom, $uname, $upass);
1590: if($pwdcorrect) {
1591: &Reply( $client, "authorized\n", $userinput);
1592: #
1593: # Bad credentials: Failed to authorize
1594: #
1595: } else {
1596: &Failure( $client, "non_authorized\n", $userinput);
1597: }
1598:
1599: return 1;
1600: }
1.263 albertel 1601: ®ister_handler("auth", \&authenticate_handler, 1, 1, 0);
1.214 foxr 1602:
1.222 foxr 1603: #
1604: # Change a user's password. Note that this function is complicated by
1605: # the fact that a user may be authenticated in more than one way:
1606: # At present, we are not able to change the password for all types of
1607: # authentication methods. Only for:
1608: # unix - unix password or shadow passoword style authentication.
1609: # local - Locally written authentication mechanism.
1610: # For now, kerb4 and kerb5 password changes are not supported and result
1611: # in an error.
1612: # FUTURE WORK:
1613: # Support kerberos passwd changes?
1614: # Parameters:
1615: # $cmd - The command that got us here.
1616: # $tail - Tail of the command (remaining parameters).
1617: # $client - File descriptor connected to client.
1618: # Returns
1619: # 0 - Requested to exit, caller should shut down.
1620: # 1 - Continue processing.
1621: # Implicit inputs:
1622: # The authentication systems describe above have their own forms of implicit
1623: # input into the authentication process that are described above.
1624: sub change_password_handler {
1625: my ($cmd, $tail, $client) = @_;
1626:
1627: my $userinput = $cmd.":".$tail; # Reconstruct client's string.
1628:
1629: #
1630: # udom - user's domain.
1631: # uname - Username.
1632: # upass - Current password.
1633: # npass - New password.
1634:
1635: my ($udom,$uname,$upass,$npass)=split(/:/,$tail);
1636:
1637: $upass=&unescape($upass);
1638: $npass=&unescape($npass);
1639: &Debug("Trying to change password for $uname");
1640:
1641: # First require that the user can be authenticated with their
1642: # old password:
1643:
1644: my $validated = &validate_user($udom, $uname, $upass);
1645: if($validated) {
1646: my $realpasswd = &get_auth_type($udom, $uname); # Defined since authd.
1647:
1648: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
1649: if ($howpwd eq 'internal') {
1650: &Debug("internal auth");
1651: my $salt=time;
1652: $salt=substr($salt,6,2);
1653: my $ncpass=crypt($npass,$salt);
1654: if(&rewrite_password_file($udom, $uname, "internal:$ncpass")) {
1655: &logthis("Result of password change for "
1656: ."$uname: pwchange_success");
1657: &Reply($client, "ok\n", $userinput);
1658: } else {
1659: &logthis("Unable to open $uname passwd "
1660: ."to change password");
1661: &Failure( $client, "non_authorized\n",$userinput);
1662: }
1663: } elsif ($howpwd eq 'unix') {
1664: # Unix means we have to access /etc/password
1665: &Debug("auth is unix");
1666: my $execdir=$perlvar{'lonDaemons'};
1667: &Debug("Opening lcpasswd pipeline");
1668: my $pf = IO::File->new("|$execdir/lcpasswd > "
1669: ."$perlvar{'lonDaemons'}"
1670: ."/logs/lcpasswd.log");
1671: print $pf "$uname\n$npass\n$npass\n";
1672: close $pf;
1673: my $err = $?;
1674: my $result = ($err>0 ? 'pwchange_failure' : 'ok');
1675: &logthis("Result of password change for $uname: ".
1676: &lcpasswdstrerror($?));
1677: &Reply($client, "$result\n", $userinput);
1678: } else {
1679: # this just means that the current password mode is not
1680: # one we know how to change (e.g the kerberos auth modes or
1681: # locally written auth handler).
1682: #
1683: &Failure( $client, "auth_mode_error\n", $userinput);
1684: }
1685:
1.224 foxr 1686: } else {
1.222 foxr 1687: &Failure( $client, "non_authorized\n", $userinput);
1688: }
1689:
1690: return 1;
1691: }
1.263 albertel 1692: ®ister_handler("passwd", \&change_password_handler, 1, 1, 0);
1.222 foxr 1693:
1.225 foxr 1694: #
1695: # Create a new user. User in this case means a lon-capa user.
1696: # The user must either already exist in some authentication realm
1697: # like kerberos or the /etc/passwd. If not, a user completely local to
1698: # this loncapa system is created.
1699: #
1700: # Parameters:
1701: # $cmd - The command that got us here.
1702: # $tail - Tail of the command (remaining parameters).
1703: # $client - File descriptor connected to client.
1704: # Returns
1705: # 0 - Requested to exit, caller should shut down.
1706: # 1 - Continue processing.
1707: # Implicit inputs:
1708: # The authentication systems describe above have their own forms of implicit
1709: # input into the authentication process that are described above.
1710: sub add_user_handler {
1711:
1712: my ($cmd, $tail, $client) = @_;
1713:
1714:
1715: my ($udom,$uname,$umode,$npass)=split(/:/,$tail);
1716: my $userinput = $cmd.":".$tail; # Reconstruct the full request line.
1717:
1718: &Debug("cmd =".$cmd." $udom =".$udom." uname=".$uname);
1719:
1720:
1721: if($udom eq $currentdomainid) { # Reject new users for other domains...
1722:
1723: my $oldumask=umask(0077);
1724: chomp($npass);
1725: $npass=&unescape($npass);
1726: my $passfilename = &password_path($udom, $uname);
1727: &Debug("Password file created will be:".$passfilename);
1728: if (-e $passfilename) {
1729: &Failure( $client, "already_exists\n", $userinput);
1730: } else {
1731: my $fperror='';
1.264 albertel 1732: if (!&mkpath($passfilename)) {
1733: $fperror="error: ".($!+0)." mkdir failed while attempting "
1734: ."makeuser";
1.225 foxr 1735: }
1736: unless ($fperror) {
1737: my $result=&make_passwd_file($uname, $umode,$npass, $passfilename);
1738: &Reply($client, $result, $userinput); #BUGBUG - could be fail
1739: } else {
1740: &Failure($client, "$fperror\n", $userinput);
1741: }
1742: }
1743: umask($oldumask);
1744: } else {
1745: &Failure($client, "not_right_domain\n",
1746: $userinput); # Even if we are multihomed.
1747:
1748: }
1749: return 1;
1750:
1751: }
1752: ®ister_handler("makeuser", \&add_user_handler, 1, 1, 0);
1753:
1754: #
1755: # Change the authentication method of a user. Note that this may
1756: # also implicitly change the user's password if, for example, the user is
1757: # joining an existing authentication realm. Known authentication realms at
1758: # this time are:
1759: # internal - Purely internal password file (only loncapa knows this user)
1760: # local - Institutionally written authentication module.
1761: # unix - Unix user (/etc/passwd with or without /etc/shadow).
1762: # kerb4 - kerberos version 4
1763: # kerb5 - kerberos version 5
1764: #
1765: # Parameters:
1766: # $cmd - The command that got us here.
1767: # $tail - Tail of the command (remaining parameters).
1768: # $client - File descriptor connected to client.
1769: # Returns
1770: # 0 - Requested to exit, caller should shut down.
1771: # 1 - Continue processing.
1772: # Implicit inputs:
1773: # The authentication systems describe above have their own forms of implicit
1774: # input into the authentication process that are described above.
1775: #
1776: sub change_authentication_handler {
1777:
1778: my ($cmd, $tail, $client) = @_;
1779:
1780: my $userinput = "$cmd:$tail"; # Reconstruct user input.
1781:
1782: my ($udom,$uname,$umode,$npass)=split(/:/,$tail);
1783: &Debug("cmd = ".$cmd." domain= ".$udom."uname =".$uname." umode= ".$umode);
1784: if ($udom ne $currentdomainid) {
1785: &Failure( $client, "not_right_domain\n", $client);
1786: } else {
1787:
1788: chomp($npass);
1789:
1790: $npass=&unescape($npass);
1.261 foxr 1791: my $oldauth = &get_auth_type($udom, $uname); # Get old auth info.
1.225 foxr 1792: my $passfilename = &password_path($udom, $uname);
1793: if ($passfilename) { # Not allowed to create a new user!!
1794: my $result=&make_passwd_file($uname, $umode,$npass,$passfilename);
1.261 foxr 1795: #
1796: # If the current auth mode is internal, and the old auth mode was
1797: # unix, or krb*, and the user is an author for this domain,
1798: # re-run manage_permissions for that role in order to be able
1799: # to take ownership of the construction space back to www:www
1800: #
1801:
1802: if( ($oldauth =~ /^unix/) && ($umode eq "internal")) { # unix -> internal
1803: if(&is_author($udom, $uname)) {
1804: &Debug(" Need to manage author permissions...");
1805: &manage_permissions("/$udom/_au", $udom, $uname, "internal:");
1806: }
1807: }
1808:
1809:
1.225 foxr 1810: &Reply($client, $result, $userinput);
1811: } else {
1.251 foxr 1812: &Failure($client, "non_authorized\n", $userinput); # Fail the user now.
1.225 foxr 1813: }
1814: }
1815: return 1;
1816: }
1817: ®ister_handler("changeuserauth", \&change_authentication_handler, 1,1, 0);
1818:
1819: #
1820: # Determines if this is the home server for a user. The home server
1821: # for a user will have his/her lon-capa passwd file. Therefore all we need
1822: # to do is determine if this file exists.
1823: #
1824: # Parameters:
1825: # $cmd - The command that got us here.
1826: # $tail - Tail of the command (remaining parameters).
1827: # $client - File descriptor connected to client.
1828: # Returns
1829: # 0 - Requested to exit, caller should shut down.
1830: # 1 - Continue processing.
1831: # Implicit inputs:
1832: # The authentication systems describe above have their own forms of implicit
1833: # input into the authentication process that are described above.
1834: #
1835: sub is_home_handler {
1836: my ($cmd, $tail, $client) = @_;
1837:
1838: my $userinput = "$cmd:$tail";
1839:
1840: my ($udom,$uname)=split(/:/,$tail);
1841: chomp($uname);
1842: my $passfile = &password_filename($udom, $uname);
1843: if($passfile) {
1844: &Reply( $client, "found\n", $userinput);
1845: } else {
1846: &Failure($client, "not_found\n", $userinput);
1847: }
1848: return 1;
1849: }
1850: ®ister_handler("home", \&is_home_handler, 0,1,0);
1851:
1852: #
1853: # Process an update request for a resource?? I think what's going on here is
1854: # that a resource has been modified that we hold a subscription to.
1855: # If the resource is not local, then we must update, or at least invalidate our
1856: # cached copy of the resource.
1857: # FUTURE WORK:
1858: # I need to look at this logic carefully. My druthers would be to follow
1859: # typical caching logic, and simple invalidate the cache, drop any subscription
1860: # an let the next fetch start the ball rolling again... however that may
1861: # actually be more difficult than it looks given the complex web of
1862: # proxy servers.
1863: # Parameters:
1864: # $cmd - The command that got us here.
1865: # $tail - Tail of the command (remaining parameters).
1866: # $client - File descriptor connected to client.
1867: # Returns
1868: # 0 - Requested to exit, caller should shut down.
1869: # 1 - Continue processing.
1870: # Implicit inputs:
1871: # The authentication systems describe above have their own forms of implicit
1872: # input into the authentication process that are described above.
1873: #
1874: sub update_resource_handler {
1875:
1876: my ($cmd, $tail, $client) = @_;
1877:
1878: my $userinput = "$cmd:$tail";
1879:
1880: my $fname= $tail; # This allows interactive testing
1881:
1882:
1883: my $ownership=ishome($fname);
1884: if ($ownership eq 'not_owner') {
1885: if (-e $fname) {
1886: my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
1887: $atime,$mtime,$ctime,$blksize,$blocks)=stat($fname);
1888: my $now=time;
1889: my $since=$now-$atime;
1890: if ($since>$perlvar{'lonExpire'}) {
1891: my $reply=&reply("unsub:$fname","$clientname");
1892: unlink("$fname");
1893: } else {
1894: my $transname="$fname.in.transfer";
1895: my $remoteurl=&reply("sub:$fname","$clientname");
1896: my $response;
1897: alarm(120);
1898: {
1899: my $ua=new LWP::UserAgent;
1900: my $request=new HTTP::Request('GET',"$remoteurl");
1901: $response=$ua->request($request,$transname);
1902: }
1903: alarm(0);
1904: if ($response->is_error()) {
1905: unlink($transname);
1906: my $message=$response->status_line;
1907: &logthis("LWP GET: $message for $fname ($remoteurl)");
1908: } else {
1909: if ($remoteurl!~/\.meta$/) {
1910: alarm(120);
1911: {
1912: my $ua=new LWP::UserAgent;
1913: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1914: my $mresponse=$ua->request($mrequest,$fname.'.meta');
1915: if ($mresponse->is_error()) {
1916: unlink($fname.'.meta');
1917: }
1918: }
1919: alarm(0);
1920: }
1921: rename($transname,$fname);
1922: }
1923: }
1924: &Reply( $client, "ok\n", $userinput);
1925: } else {
1926: &Failure($client, "not_found\n", $userinput);
1927: }
1928: } else {
1929: &Failure($client, "rejected\n", $userinput);
1930: }
1931: return 1;
1932: }
1933: ®ister_handler("update", \&update_resource_handler, 0 ,1, 0);
1934:
1935: #
1.226 foxr 1936: # Fetch a user file from a remote server to the user's home directory
1937: # userfiles subdir.
1.225 foxr 1938: # Parameters:
1939: # $cmd - The command that got us here.
1940: # $tail - Tail of the command (remaining parameters).
1941: # $client - File descriptor connected to client.
1942: # Returns
1943: # 0 - Requested to exit, caller should shut down.
1944: # 1 - Continue processing.
1945: #
1946: sub fetch_user_file_handler {
1947:
1948: my ($cmd, $tail, $client) = @_;
1949:
1950: my $userinput = "$cmd:$tail";
1951: my $fname = $tail;
1.232 foxr 1952: my ($udom,$uname,$ufile) = ($fname =~ m|^([^/]+)/([^/]+)/(.+)$|);
1.225 foxr 1953: my $udir=&propath($udom,$uname).'/userfiles';
1954: unless (-e $udir) {
1955: mkdir($udir,0770);
1956: }
1.232 foxr 1957: Debug("fetch user file for $fname");
1.225 foxr 1958: if (-e $udir) {
1959: $ufile=~s/^[\.\~]+//;
1.232 foxr 1960:
1961: # IF necessary, create the path right down to the file.
1962: # Note that any regular files in the way of this path are
1963: # wiped out to deal with some earlier folly of mine.
1964:
1.267 raeburn 1965: if (!&mkpath($udir.'/'.$ufile)) {
1.264 albertel 1966: &Failure($client, "unable_to_create\n", $userinput);
1.232 foxr 1967: }
1968:
1.225 foxr 1969: my $destname=$udir.'/'.$ufile;
1970: my $transname=$udir.'/'.$ufile.'.in.transit';
1971: my $remoteurl='http://'.$clientip.'/userfiles/'.$fname;
1972: my $response;
1.232 foxr 1973: Debug("Remote URL : $remoteurl Transfername $transname Destname: $destname");
1.225 foxr 1974: alarm(120);
1975: {
1976: my $ua=new LWP::UserAgent;
1977: my $request=new HTTP::Request('GET',"$remoteurl");
1978: $response=$ua->request($request,$transname);
1979: }
1980: alarm(0);
1981: if ($response->is_error()) {
1982: unlink($transname);
1983: my $message=$response->status_line;
1984: &logthis("LWP GET: $message for $fname ($remoteurl)");
1985: &Failure($client, "failed\n", $userinput);
1986: } else {
1.232 foxr 1987: Debug("Renaming $transname to $destname");
1.225 foxr 1988: if (!rename($transname,$destname)) {
1989: &logthis("Unable to move $transname to $destname");
1990: unlink($transname);
1991: &Failure($client, "failed\n", $userinput);
1992: } else {
1993: &Reply($client, "ok\n", $userinput);
1994: }
1995: }
1996: } else {
1997: &Failure($client, "not_home\n", $userinput);
1998: }
1999: return 1;
2000: }
2001: ®ister_handler("fetchuserfile", \&fetch_user_file_handler, 0, 1, 0);
2002:
1.226 foxr 2003: #
2004: # Remove a file from a user's home directory userfiles subdirectory.
2005: # Parameters:
2006: # cmd - the Lond request keyword that got us here.
2007: # tail - the part of the command past the keyword.
2008: # client- File descriptor connected with the client.
2009: #
2010: # Returns:
2011: # 1 - Continue processing.
2012: sub remove_user_file_handler {
2013: my ($cmd, $tail, $client) = @_;
2014:
2015: my ($fname) = split(/:/, $tail); # Get rid of any tailing :'s lonc may have sent.
2016:
2017: my ($udom,$uname,$ufile) = ($fname =~ m|^([^/]+)/([^/]+)/(.+)$|);
2018: if ($ufile =~m|/\.\./|) {
2019: # any files paths with /../ in them refuse
2020: # to deal with
2021: &Failure($client, "refused\n", "$cmd:$tail");
2022: } else {
2023: my $udir = &propath($udom,$uname);
2024: if (-e $udir) {
2025: my $file=$udir.'/userfiles/'.$ufile;
2026: if (-e $file) {
1.253 foxr 2027: #
2028: # If the file is a regular file unlink is fine...
2029: # However it's possible the client wants a dir.
2030: # removed, in which case rmdir is more approprate:
2031: #
1.240 banghart 2032: if (-f $file){
1.241 albertel 2033: unlink($file);
2034: } elsif(-d $file) {
2035: rmdir($file);
1.240 banghart 2036: }
1.226 foxr 2037: if (-e $file) {
1.253 foxr 2038: # File is still there after we deleted it ?!?
2039:
1.226 foxr 2040: &Failure($client, "failed\n", "$cmd:$tail");
2041: } else {
2042: &Reply($client, "ok\n", "$cmd:$tail");
2043: }
2044: } else {
2045: &Failure($client, "not_found\n", "$cmd:$tail");
2046: }
2047: } else {
2048: &Failure($client, "not_home\n", "$cmd:$tail");
2049: }
2050: }
2051: return 1;
2052: }
2053: ®ister_handler("removeuserfile", \&remove_user_file_handler, 0,1,0);
2054:
1.236 albertel 2055: #
2056: # make a directory in a user's home directory userfiles subdirectory.
2057: # Parameters:
2058: # cmd - the Lond request keyword that got us here.
2059: # tail - the part of the command past the keyword.
2060: # client- File descriptor connected with the client.
2061: #
2062: # Returns:
2063: # 1 - Continue processing.
2064: sub mkdir_user_file_handler {
2065: my ($cmd, $tail, $client) = @_;
2066:
2067: my ($dir) = split(/:/, $tail); # Get rid of any tailing :'s lonc may have sent.
2068: $dir=&unescape($dir);
2069: my ($udom,$uname,$ufile) = ($dir =~ m|^([^/]+)/([^/]+)/(.+)$|);
2070: if ($ufile =~m|/\.\./|) {
2071: # any files paths with /../ in them refuse
2072: # to deal with
2073: &Failure($client, "refused\n", "$cmd:$tail");
2074: } else {
2075: my $udir = &propath($udom,$uname);
2076: if (-e $udir) {
1.264 albertel 2077: my $newdir=$udir.'/userfiles/'.$ufile.'/';
2078: if (!&mkpath($newdir)) {
2079: &Failure($client, "failed\n", "$cmd:$tail");
1.236 albertel 2080: }
1.264 albertel 2081: &Reply($client, "ok\n", "$cmd:$tail");
1.236 albertel 2082: } else {
2083: &Failure($client, "not_home\n", "$cmd:$tail");
2084: }
2085: }
2086: return 1;
2087: }
2088: ®ister_handler("mkdiruserfile", \&mkdir_user_file_handler, 0,1,0);
2089:
1.237 albertel 2090: #
2091: # rename a file in a user's home directory userfiles subdirectory.
2092: # Parameters:
2093: # cmd - the Lond request keyword that got us here.
2094: # tail - the part of the command past the keyword.
2095: # client- File descriptor connected with the client.
2096: #
2097: # Returns:
2098: # 1 - Continue processing.
2099: sub rename_user_file_handler {
2100: my ($cmd, $tail, $client) = @_;
2101:
2102: my ($udom,$uname,$old,$new) = split(/:/, $tail);
2103: $old=&unescape($old);
2104: $new=&unescape($new);
2105: if ($new =~m|/\.\./| || $old =~m|/\.\./|) {
2106: # any files paths with /../ in them refuse to deal with
2107: &Failure($client, "refused\n", "$cmd:$tail");
2108: } else {
2109: my $udir = &propath($udom,$uname);
2110: if (-e $udir) {
2111: my $oldfile=$udir.'/userfiles/'.$old;
2112: my $newfile=$udir.'/userfiles/'.$new;
2113: if (-e $newfile) {
2114: &Failure($client, "exists\n", "$cmd:$tail");
2115: } elsif (! -e $oldfile) {
2116: &Failure($client, "not_found\n", "$cmd:$tail");
2117: } else {
2118: if (!rename($oldfile,$newfile)) {
2119: &Failure($client, "failed\n", "$cmd:$tail");
2120: } else {
2121: &Reply($client, "ok\n", "$cmd:$tail");
2122: }
2123: }
2124: } else {
2125: &Failure($client, "not_home\n", "$cmd:$tail");
2126: }
2127: }
2128: return 1;
2129: }
2130: ®ister_handler("renameuserfile", \&rename_user_file_handler, 0,1,0);
2131:
1.227 foxr 2132: #
1.263 albertel 2133: # Authenticate access to a user file by checking that the token the user's
2134: # passed also exists in their session file
1.227 foxr 2135: #
2136: # Parameters:
2137: # cmd - The request keyword that dispatched to tus.
2138: # tail - The tail of the request (colon separated parameters).
2139: # client - Filehandle open on the client.
2140: # Return:
2141: # 1.
2142: sub token_auth_user_file_handler {
2143: my ($cmd, $tail, $client) = @_;
2144:
2145: my ($fname, $session) = split(/:/, $tail);
2146:
2147: chomp($session);
1.251 foxr 2148: my $reply="non_auth\n";
1.227 foxr 2149: if (open(ENVIN,$perlvar{'lonIDsDir'}.'/'.
2150: $session.'.id')) {
2151: while (my $line=<ENVIN>) {
1.251 foxr 2152: if ($line=~ m|userfile\.\Q$fname\E\=|) { $reply="ok\n"; }
1.227 foxr 2153: }
2154: close(ENVIN);
1.251 foxr 2155: &Reply($client, $reply, "$cmd:$tail");
1.227 foxr 2156: } else {
2157: &Failure($client, "invalid_token\n", "$cmd:$tail");
2158: }
2159: return 1;
2160:
2161: }
2162: ®ister_handler("tokenauthuserfile", \&token_auth_user_file_handler, 0,1,0);
1.229 foxr 2163:
2164: #
2165: # Unsubscribe from a resource.
2166: #
2167: # Parameters:
2168: # $cmd - The command that got us here.
2169: # $tail - Tail of the command (remaining parameters).
2170: # $client - File descriptor connected to client.
2171: # Returns
2172: # 0 - Requested to exit, caller should shut down.
2173: # 1 - Continue processing.
2174: #
2175: sub unsubscribe_handler {
2176: my ($cmd, $tail, $client) = @_;
2177:
2178: my $userinput= "$cmd:$tail";
2179:
2180: my ($fname) = split(/:/,$tail); # Split in case there's extrs.
2181:
2182: &Debug("Unsubscribing $fname");
2183: if (-e $fname) {
2184: &Debug("Exists");
2185: &Reply($client, &unsub($fname,$clientip), $userinput);
2186: } else {
2187: &Failure($client, "not_found\n", $userinput);
2188: }
2189: return 1;
2190: }
2191: ®ister_handler("unsub", \&unsubscribe_handler, 0, 1, 0);
1.263 albertel 2192:
1.230 foxr 2193: # Subscribe to a resource
2194: #
2195: # Parameters:
2196: # $cmd - The command that got us here.
2197: # $tail - Tail of the command (remaining parameters).
2198: # $client - File descriptor connected to client.
2199: # Returns
2200: # 0 - Requested to exit, caller should shut down.
2201: # 1 - Continue processing.
2202: #
2203: sub subscribe_handler {
2204: my ($cmd, $tail, $client)= @_;
2205:
2206: my $userinput = "$cmd:$tail";
2207:
2208: &Reply( $client, &subscribe($userinput,$clientip), $userinput);
2209:
2210: return 1;
2211: }
2212: ®ister_handler("sub", \&subscribe_handler, 0, 1, 0);
2213:
2214: #
2215: # Determine the version of a resource (?) Or is it return
2216: # the top version of the resource? Not yet clear from the
2217: # code in currentversion.
2218: #
2219: # Parameters:
2220: # $cmd - The command that got us here.
2221: # $tail - Tail of the command (remaining parameters).
2222: # $client - File descriptor connected to client.
2223: # Returns
2224: # 0 - Requested to exit, caller should shut down.
2225: # 1 - Continue processing.
2226: #
2227: sub current_version_handler {
2228: my ($cmd, $tail, $client) = @_;
2229:
2230: my $userinput= "$cmd:$tail";
2231:
2232: my $fname = $tail;
2233: &Reply( $client, ¤tversion($fname)."\n", $userinput);
2234: return 1;
2235:
2236: }
2237: ®ister_handler("currentversion", \¤t_version_handler, 0, 1, 0);
2238:
2239: # Make an entry in a user's activity log.
2240: #
2241: # Parameters:
2242: # $cmd - The command that got us here.
2243: # $tail - Tail of the command (remaining parameters).
2244: # $client - File descriptor connected to client.
2245: # Returns
2246: # 0 - Requested to exit, caller should shut down.
2247: # 1 - Continue processing.
2248: #
2249: sub activity_log_handler {
2250: my ($cmd, $tail, $client) = @_;
2251:
2252:
2253: my $userinput= "$cmd:$tail";
2254:
2255: my ($udom,$uname,$what)=split(/:/,$tail);
2256: chomp($what);
2257: my $proname=&propath($udom,$uname);
2258: my $now=time;
2259: my $hfh;
2260: if ($hfh=IO::File->new(">>$proname/activity.log")) {
2261: print $hfh "$now:$clientname:$what\n";
2262: &Reply( $client, "ok\n", $userinput);
2263: } else {
2264: &Failure($client, "error: ".($!+0)." IO::File->new Failed "
2265: ."while attempting log\n",
2266: $userinput);
2267: }
2268:
2269: return 1;
2270: }
1.263 albertel 2271: ®ister_handler("log", \&activity_log_handler, 0, 1, 0);
1.230 foxr 2272:
2273: #
2274: # Put a namespace entry in a user profile hash.
2275: # My druthers would be for this to be an encrypted interaction too.
2276: # anything that might be an inadvertent covert channel about either
2277: # user authentication or user personal information....
2278: #
2279: # Parameters:
2280: # $cmd - The command that got us here.
2281: # $tail - Tail of the command (remaining parameters).
2282: # $client - File descriptor connected to client.
2283: # Returns
2284: # 0 - Requested to exit, caller should shut down.
2285: # 1 - Continue processing.
2286: #
2287: sub put_user_profile_entry {
2288: my ($cmd, $tail, $client) = @_;
1.229 foxr 2289:
1.230 foxr 2290: my $userinput = "$cmd:$tail";
2291:
1.242 raeburn 2292: my ($udom,$uname,$namespace,$what) =split(/:/,$tail,4);
1.230 foxr 2293: if ($namespace ne 'roles') {
2294: chomp($what);
2295: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2296: &GDBM_WRCREAT(),"P",$what);
2297: if($hashref) {
2298: my @pairs=split(/\&/,$what);
2299: foreach my $pair (@pairs) {
2300: my ($key,$value)=split(/=/,$pair);
2301: $hashref->{$key}=$value;
2302: }
2303: if (untie(%$hashref)) {
2304: &Reply( $client, "ok\n", $userinput);
2305: } else {
2306: &Failure($client, "error: ".($!+0)." untie(GDBM) failed ".
2307: "while attempting put\n",
2308: $userinput);
2309: }
2310: } else {
2311: &Failure( $client, "error: ".($!)." tie(GDBM) Failed ".
2312: "while attempting put\n", $userinput);
2313: }
2314: } else {
2315: &Failure( $client, "refused\n", $userinput);
2316: }
2317:
2318: return 1;
2319: }
2320: ®ister_handler("put", \&put_user_profile_entry, 0, 1, 0);
2321:
2322: #
2323: # Increment a profile entry in the user history file.
2324: # The history contains keyword value pairs. In this case,
2325: # The value itself is a pair of numbers. The first, the current value
2326: # the second an increment that this function applies to the current
2327: # value.
2328: #
2329: # Parameters:
2330: # $cmd - The command that got us here.
2331: # $tail - Tail of the command (remaining parameters).
2332: # $client - File descriptor connected to client.
2333: # Returns
2334: # 0 - Requested to exit, caller should shut down.
2335: # 1 - Continue processing.
2336: #
2337: sub increment_user_value_handler {
2338: my ($cmd, $tail, $client) = @_;
2339:
2340: my $userinput = "$cmd:$tail";
2341:
2342: my ($udom,$uname,$namespace,$what) =split(/:/,$tail);
2343: if ($namespace ne 'roles') {
2344: chomp($what);
2345: my $hashref = &tie_user_hash($udom, $uname,
2346: $namespace, &GDBM_WRCREAT(),
2347: "P",$what);
2348: if ($hashref) {
2349: my @pairs=split(/\&/,$what);
2350: foreach my $pair (@pairs) {
2351: my ($key,$value)=split(/=/,$pair);
2352: # We could check that we have a number...
2353: if (! defined($value) || $value eq '') {
2354: $value = 1;
2355: }
2356: $hashref->{$key}+=$value;
2357: }
2358: if (untie(%$hashref)) {
2359: &Reply( $client, "ok\n", $userinput);
2360: } else {
2361: &Failure($client, "error: ".($!+0)." untie(GDBM) failed ".
2362: "while attempting inc\n", $userinput);
2363: }
2364: } else {
2365: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
2366: "while attempting inc\n", $userinput);
2367: }
2368: } else {
2369: &Failure($client, "refused\n", $userinput);
2370: }
2371:
2372: return 1;
2373: }
2374: ®ister_handler("inc", \&increment_user_value_handler, 0, 1, 0);
2375:
2376: #
2377: # Put a new role for a user. Roles are LonCAPA's packaging of permissions.
2378: # Each 'role' a user has implies a set of permissions. Adding a new role
2379: # for a person grants the permissions packaged with that role
2380: # to that user when the role is selected.
2381: #
2382: # Parameters:
2383: # $cmd - The command string (rolesput).
2384: # $tail - The remainder of the request line. For rolesput this
2385: # consists of a colon separated list that contains:
2386: # The domain and user that is granting the role (logged).
2387: # The domain and user that is getting the role.
2388: # The roles being granted as a set of & separated pairs.
2389: # each pair a key value pair.
2390: # $client - File descriptor connected to the client.
2391: # Returns:
2392: # 0 - If the daemon should exit
2393: # 1 - To continue processing.
2394: #
2395: #
2396: sub roles_put_handler {
2397: my ($cmd, $tail, $client) = @_;
2398:
2399: my $userinput = "$cmd:$tail";
2400:
2401: my ( $exedom, $exeuser, $udom, $uname, $what) = split(/:/,$tail);
2402:
2403:
2404: my $namespace='roles';
2405: chomp($what);
2406: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2407: &GDBM_WRCREAT(), "P",
2408: "$exedom:$exeuser:$what");
2409: #
2410: # Log the attempt to set a role. The {}'s here ensure that the file
2411: # handle is open for the minimal amount of time. Since the flush
2412: # is done on close this improves the chances the log will be an un-
2413: # corrupted ordered thing.
2414: if ($hashref) {
1.261 foxr 2415: my $pass_entry = &get_auth_type($udom, $uname);
2416: my ($auth_type,$pwd) = split(/:/, $pass_entry);
2417: $auth_type = $auth_type.":";
1.230 foxr 2418: my @pairs=split(/\&/,$what);
2419: foreach my $pair (@pairs) {
2420: my ($key,$value)=split(/=/,$pair);
2421: &manage_permissions($key, $udom, $uname,
1.260 foxr 2422: $auth_type);
1.230 foxr 2423: $hashref->{$key}=$value;
2424: }
2425: if (untie($hashref)) {
2426: &Reply($client, "ok\n", $userinput);
2427: } else {
2428: &Failure( $client, "error: ".($!+0)." untie(GDBM) Failed ".
2429: "while attempting rolesput\n", $userinput);
2430: }
2431: } else {
2432: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
2433: "while attempting rolesput\n", $userinput);
2434: }
2435: return 1;
2436: }
2437: ®ister_handler("rolesput", \&roles_put_handler, 1,1,0); # Encoded client only.
2438:
2439: #
1.231 foxr 2440: # Deletes (removes) a role for a user. This is equivalent to removing
2441: # a permissions package associated with the role from the user's profile.
2442: #
2443: # Parameters:
2444: # $cmd - The command (rolesdel)
2445: # $tail - The remainder of the request line. This consists
2446: # of:
2447: # The domain and user requesting the change (logged)
2448: # The domain and user being changed.
2449: # The roles being revoked. These are shipped to us
2450: # as a bunch of & separated role name keywords.
2451: # $client - The file handle open on the client.
2452: # Returns:
2453: # 1 - Continue processing
2454: # 0 - Exit.
2455: #
2456: sub roles_delete_handler {
2457: my ($cmd, $tail, $client) = @_;
2458:
2459: my $userinput = "$cmd:$tail";
2460:
2461: my ($exedom,$exeuser,$udom,$uname,$what)=split(/:/,$tail);
2462: &Debug("cmd = ".$cmd." exedom= ".$exedom."user = ".$exeuser." udom=".$udom.
2463: "what = ".$what);
2464: my $namespace='roles';
2465: chomp($what);
2466: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2467: &GDBM_WRCREAT(), "D",
2468: "$exedom:$exeuser:$what");
2469:
2470: if ($hashref) {
2471: my @rolekeys=split(/\&/,$what);
2472:
2473: foreach my $key (@rolekeys) {
2474: delete $hashref->{$key};
2475: }
2476: if (untie(%$hashref)) {
2477: &Reply($client, "ok\n", $userinput);
2478: } else {
2479: &Failure( $client, "error: ".($!+0)." untie(GDBM) Failed ".
2480: "while attempting rolesdel\n", $userinput);
2481: }
2482: } else {
2483: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
2484: "while attempting rolesdel\n", $userinput);
2485: }
2486:
2487: return 1;
2488: }
2489: ®ister_handler("rolesdel", \&roles_delete_handler, 1,1, 0); # Encoded client only
2490:
2491: # Unencrypted get from a user's profile database. See
2492: # GetProfileEntryEncrypted for a version that does end-to-end encryption.
2493: # This function retrieves a keyed item from a specific named database in the
2494: # user's directory.
2495: #
2496: # Parameters:
2497: # $cmd - Command request keyword (get).
2498: # $tail - Tail of the command. This is a colon separated list
2499: # consisting of the domain and username that uniquely
2500: # identifies the profile,
2501: # The 'namespace' which selects the gdbm file to
2502: # do the lookup in,
2503: # & separated list of keys to lookup. Note that
2504: # the values are returned as an & separated list too.
2505: # $client - File descriptor open on the client.
2506: # Returns:
2507: # 1 - Continue processing.
2508: # 0 - Exit.
2509: #
2510: sub get_profile_entry {
2511: my ($cmd, $tail, $client) = @_;
2512:
2513: my $userinput= "$cmd:$tail";
2514:
2515: my ($udom,$uname,$namespace,$what) = split(/:/,$tail);
2516: chomp($what);
1.255 foxr 2517:
2518: my $replystring = read_profile($udom, $uname, $namespace, $what);
2519: my ($first) = split(/:/,$replystring);
2520: if($first ne "error") {
2521: &Reply($client, "$replystring\n", $userinput);
1.231 foxr 2522: } else {
1.255 foxr 2523: &Failure($client, $replystring." while attempting get\n", $userinput);
1.231 foxr 2524: }
2525: return 1;
1.255 foxr 2526:
2527:
1.231 foxr 2528: }
2529: ®ister_handler("get", \&get_profile_entry, 0,1,0);
2530:
2531: #
2532: # Process the encrypted get request. Note that the request is sent
2533: # in clear, but the reply is encrypted. This is a small covert channel:
2534: # information about the sensitive keys is given to the snooper. Just not
2535: # information about the values of the sensitive key. Hmm if I wanted to
2536: # know these I'd snoop for the egets. Get the profile item names from them
2537: # and then issue a get for them since there's no enforcement of the
2538: # requirement of an encrypted get for particular profile items. If I
2539: # were re-doing this, I'd force the request to be encrypted as well as the
2540: # reply. I'd also just enforce encrypted transactions for all gets since
2541: # that would prevent any covert channel snooping.
2542: #
2543: # Parameters:
2544: # $cmd - Command keyword of request (eget).
2545: # $tail - Tail of the command. See GetProfileEntry
# for more information about this.
2546: # $client - File open on the client.
2547: # Returns:
2548: # 1 - Continue processing
2549: # 0 - server should exit.
2550: sub get_profile_entry_encrypted {
2551: my ($cmd, $tail, $client) = @_;
2552:
2553: my $userinput = "$cmd:$tail";
2554:
2555: my ($cmd,$udom,$uname,$namespace,$what) = split(/:/,$userinput);
2556: chomp($what);
1.255 foxr 2557: my $qresult = read_profile($udom, $uname, $namespace, $what);
2558: my ($first) = split(/:/, $qresult);
2559: if($first ne "error") {
2560:
2561: if ($cipher) {
2562: my $cmdlength=length($qresult);
2563: $qresult.=" ";
2564: my $encqresult='';
2565: for(my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
2566: $encqresult.= unpack("H16",
2567: $cipher->encrypt(substr($qresult,
2568: $encidx,
2569: 8)));
2570: }
2571: &Reply( $client, "enc:$cmdlength:$encqresult\n", $userinput);
2572: } else {
1.231 foxr 2573: &Failure( $client, "error:no_key\n", $userinput);
2574: }
2575: } else {
1.255 foxr 2576: &Failure($client, "$qresult while attempting eget\n", $userinput);
2577:
1.231 foxr 2578: }
2579:
2580: return 1;
2581: }
1.255 foxr 2582: ®ister_handler("eget", \&get_profile_entry_encrypted, 0, 1, 0);
1.263 albertel 2583:
1.231 foxr 2584: #
2585: # Deletes a key in a user profile database.
2586: #
2587: # Parameters:
2588: # $cmd - Command keyword (del).
2589: # $tail - Command tail. IN this case a colon
2590: # separated list containing:
2591: # The domain and user that identifies uniquely
2592: # the identity of the user.
2593: # The profile namespace (name of the profile
2594: # database file).
2595: # & separated list of keywords to delete.
2596: # $client - File open on client socket.
2597: # Returns:
2598: # 1 - Continue processing
2599: # 0 - Exit server.
2600: #
2601: #
2602: sub delete_profile_entry {
2603: my ($cmd, $tail, $client) = @_;
2604:
2605: my $userinput = "cmd:$tail";
2606:
2607: my ($udom,$uname,$namespace,$what) = split(/:/,$tail);
2608: chomp($what);
2609: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2610: &GDBM_WRCREAT(),
2611: "D",$what);
2612: if ($hashref) {
2613: my @keys=split(/\&/,$what);
2614: foreach my $key (@keys) {
2615: delete($hashref->{$key});
2616: }
2617: if (untie(%$hashref)) {
2618: &Reply($client, "ok\n", $userinput);
2619: } else {
2620: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
2621: "while attempting del\n", $userinput);
2622: }
2623: } else {
2624: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
2625: "while attempting del\n", $userinput);
2626: }
2627: return 1;
2628: }
2629: ®ister_handler("del", \&delete_profile_entry, 0, 1, 0);
1.263 albertel 2630:
1.231 foxr 2631: #
2632: # List the set of keys that are defined in a profile database file.
2633: # A successful reply from this will contain an & separated list of
2634: # the keys.
2635: # Parameters:
2636: # $cmd - Command request (keys).
2637: # $tail - Remainder of the request, a colon separated
2638: # list containing domain/user that identifies the
2639: # user being queried, and the database namespace
2640: # (database filename essentially).
2641: # $client - File open on the client.
2642: # Returns:
2643: # 1 - Continue processing.
2644: # 0 - Exit the server.
2645: #
2646: sub get_profile_keys {
2647: my ($cmd, $tail, $client) = @_;
2648:
2649: my $userinput = "$cmd:$tail";
2650:
2651: my ($udom,$uname,$namespace)=split(/:/,$tail);
2652: my $qresult='';
2653: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2654: &GDBM_READER());
2655: if ($hashref) {
2656: foreach my $key (keys %$hashref) {
2657: $qresult.="$key&";
2658: }
2659: if (untie(%$hashref)) {
2660: $qresult=~s/\&$//;
2661: &Reply($client, "$qresult\n", $userinput);
2662: } else {
2663: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
2664: "while attempting keys\n", $userinput);
2665: }
2666: } else {
2667: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
2668: "while attempting keys\n", $userinput);
2669: }
2670:
2671: return 1;
2672: }
2673: ®ister_handler("keys", \&get_profile_keys, 0, 1, 0);
2674:
2675: #
2676: # Dump the contents of a user profile database.
2677: # Note that this constitutes a very large covert channel too since
2678: # the dump will return sensitive information that is not encrypted.
2679: # The naive security assumption is that the session negotiation ensures
2680: # our client is trusted and I don't believe that's assured at present.
2681: # Sure want badly to go to ssl or tls. Of course if my peer isn't really
2682: # a LonCAPA node they could have negotiated an encryption key too so >sigh<.
2683: #
2684: # Parameters:
2685: # $cmd - The command request keyword (currentdump).
2686: # $tail - Remainder of the request, consisting of a colon
2687: # separated list that has the domain/username and
2688: # the namespace to dump (database file).
2689: # $client - file open on the remote client.
2690: # Returns:
2691: # 1 - Continue processing.
2692: # 0 - Exit the server.
2693: #
2694: sub dump_profile_database {
2695: my ($cmd, $tail, $client) = @_;
2696:
2697: my $userinput = "$cmd:$tail";
2698:
2699: my ($udom,$uname,$namespace) = split(/:/,$tail);
2700: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2701: &GDBM_READER());
2702: if ($hashref) {
2703: # Structure of %data:
2704: # $data{$symb}->{$parameter}=$value;
2705: # $data{$symb}->{'v.'.$parameter}=$version;
2706: # since $parameter will be unescaped, we do not
2707: # have to worry about silly parameter names...
2708:
2709: my $qresult='';
2710: my %data = (); # A hash of anonymous hashes..
2711: while (my ($key,$value) = each(%$hashref)) {
2712: my ($v,$symb,$param) = split(/:/,$key);
2713: next if ($v eq 'version' || $symb eq 'keys');
2714: next if (exists($data{$symb}) &&
2715: exists($data{$symb}->{$param}) &&
2716: $data{$symb}->{'v.'.$param} > $v);
2717: $data{$symb}->{$param}=$value;
2718: $data{$symb}->{'v.'.$param}=$v;
2719: }
2720: if (untie(%$hashref)) {
2721: while (my ($symb,$param_hash) = each(%data)) {
2722: while(my ($param,$value) = each (%$param_hash)){
2723: next if ($param =~ /^v\./); # Ignore versions...
2724: #
2725: # Just dump the symb=value pairs separated by &
2726: #
2727: $qresult.=$symb.':'.$param.'='.$value.'&';
2728: }
2729: }
2730: chop($qresult);
2731: &Reply($client , "$qresult\n", $userinput);
2732: } else {
2733: &Failure( $client, "error: ".($!+0)." untie(GDBM) Failed ".
2734: "while attempting currentdump\n", $userinput);
2735: }
2736: } else {
2737: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
2738: "while attempting currentdump\n", $userinput);
2739: }
2740:
2741: return 1;
2742: }
2743: ®ister_handler("currentdump", \&dump_profile_database, 0, 1, 0);
2744:
2745: #
2746: # Dump a profile database with an optional regular expression
2747: # to match against the keys. In this dump, no effort is made
2748: # to separate symb from version information. Presumably the
2749: # databases that are dumped by this command are of a different
2750: # structure. Need to look at this and improve the documentation of
2751: # both this and the currentdump handler.
2752: # Parameters:
2753: # $cmd - The command keyword.
2754: # $tail - All of the characters after the $cmd:
2755: # These are expected to be a colon
2756: # separated list containing:
2757: # domain/user - identifying the user.
2758: # namespace - identifying the database.
2759: # regexp - optional regular expression
2760: # that is matched against
2761: # database keywords to do
2762: # selective dumps.
2763: # $client - Channel open on the client.
2764: # Returns:
2765: # 1 - Continue processing.
2766: # Side effects:
2767: # response is written to $client.
2768: #
2769: sub dump_with_regexp {
2770: my ($cmd, $tail, $client) = @_;
2771:
2772:
2773: my $userinput = "$cmd:$tail";
2774:
2775: my ($udom,$uname,$namespace,$regexp)=split(/:/,$tail);
2776: if (defined($regexp)) {
2777: $regexp=&unescape($regexp);
2778: } else {
2779: $regexp='.';
2780: }
2781: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2782: &GDBM_READER());
2783: if ($hashref) {
2784: my $qresult='';
2785: while (my ($key,$value) = each(%$hashref)) {
2786: if ($regexp eq '.') {
2787: $qresult.=$key.'='.$value.'&';
2788: } else {
2789: my $unescapeKey = &unescape($key);
2790: if (eval('$unescapeKey=~/$regexp/')) {
2791: $qresult.="$key=$value&";
2792: }
2793: }
2794: }
2795: if (untie(%$hashref)) {
2796: chop($qresult);
2797: &Reply($client, "$qresult\n", $userinput);
2798: } else {
2799: &Failure( $client, "error: ".($!+0)." untie(GDBM) Failed ".
2800: "while attempting dump\n", $userinput);
2801: }
2802: } else {
2803: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
2804: "while attempting dump\n", $userinput);
2805: }
2806:
2807: return 1;
2808: }
2809: ®ister_handler("dump", \&dump_with_regexp, 0, 1, 0);
2810:
2811: # Store a set of key=value pairs associated with a versioned name.
2812: #
2813: # Parameters:
2814: # $cmd - Request command keyword.
2815: # $tail - Tail of the request. This is a colon
2816: # separated list containing:
2817: # domain/user - User and authentication domain.
2818: # namespace - Name of the database being modified
2819: # rid - Resource keyword to modify.
2820: # what - new value associated with rid.
2821: #
2822: # $client - Socket open on the client.
2823: #
2824: #
2825: # Returns:
2826: # 1 (keep on processing).
2827: # Side-Effects:
2828: # Writes to the client
2829: sub store_handler {
2830: my ($cmd, $tail, $client) = @_;
2831:
2832: my $userinput = "$cmd:$tail";
2833:
2834: my ($udom,$uname,$namespace,$rid,$what) =split(/:/,$tail);
2835: if ($namespace ne 'roles') {
2836:
2837: chomp($what);
2838: my @pairs=split(/\&/,$what);
2839: my $hashref = &tie_user_hash($udom, $uname, $namespace,
1.268 albertel 2840: &GDBM_WRCREAT(), "S",
1.231 foxr 2841: "$rid:$what");
2842: if ($hashref) {
2843: my $now = time;
2844: my @previouskeys=split(/&/,$hashref->{"keys:$rid"});
2845: my $key;
2846: $hashref->{"version:$rid"}++;
2847: my $version=$hashref->{"version:$rid"};
2848: my $allkeys='';
2849: foreach my $pair (@pairs) {
2850: my ($key,$value)=split(/=/,$pair);
2851: $allkeys.=$key.':';
2852: $hashref->{"$version:$rid:$key"}=$value;
2853: }
2854: $hashref->{"$version:$rid:timestamp"}=$now;
2855: $allkeys.='timestamp';
2856: $hashref->{"$version:keys:$rid"}=$allkeys;
2857: if (untie($hashref)) {
2858: &Reply($client, "ok\n", $userinput);
2859: } else {
2860: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
2861: "while attempting store\n", $userinput);
2862: }
2863: } else {
2864: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
2865: "while attempting store\n", $userinput);
2866: }
2867: } else {
2868: &Failure($client, "refused\n", $userinput);
2869: }
2870:
2871: return 1;
2872: }
2873: ®ister_handler("store", \&store_handler, 0, 1, 0);
1.263 albertel 2874:
1.231 foxr 2875: #
2876: # Dump out all versions of a resource that has key=value pairs associated
2877: # with it for each version. These resources are built up via the store
2878: # command.
2879: #
2880: # Parameters:
2881: # $cmd - Command keyword.
2882: # $tail - Remainder of the request which consists of:
2883: # domain/user - User and auth. domain.
2884: # namespace - name of resource database.
2885: # rid - Resource id.
2886: # $client - socket open on the client.
2887: #
2888: # Returns:
2889: # 1 indicating the caller should not yet exit.
2890: # Side-effects:
2891: # Writes a reply to the client.
2892: # The reply is a string of the following shape:
2893: # version=current&version:keys=k1:k2...&1:k1=v1&1:k2=v2...
2894: # Where the 1 above represents version 1.
2895: # this continues for all pairs of keys in all versions.
2896: #
2897: #
2898: #
2899: #
2900: sub restore_handler {
2901: my ($cmd, $tail, $client) = @_;
2902:
2903: my $userinput = "$cmd:$tail"; # Only used for logging purposes.
2904:
2905: my ($cmd,$udom,$uname,$namespace,$rid) = split(/:/,$userinput);
2906: $namespace=~s/\//\_/g;
2907: $namespace=~s/\W//g;
2908: chomp($rid);
2909: my $proname=&propath($udom,$uname);
2910: my $qresult='';
2911: my %hash;
2912: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",
2913: &GDBM_READER(),0640)) {
2914: my $version=$hash{"version:$rid"};
2915: $qresult.="version=$version&";
2916: my $scope;
2917: for ($scope=1;$scope<=$version;$scope++) {
2918: my $vkeys=$hash{"$scope:keys:$rid"};
2919: my @keys=split(/:/,$vkeys);
2920: my $key;
2921: $qresult.="$scope:keys=$vkeys&";
2922: foreach $key (@keys) {
2923: $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
2924: }
2925: }
2926: if (untie(%hash)) {
2927: $qresult=~s/\&$//;
2928: &Reply( $client, "$qresult\n", $userinput);
2929: } else {
2930: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
2931: "while attempting restore\n", $userinput);
2932: }
2933: } else {
2934: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
2935: "while attempting restore\n", $userinput);
2936: }
2937:
2938: return 1;
2939:
2940:
2941: }
2942: ®ister_handler("restore", \&restore_handler, 0,1,0);
1.234 foxr 2943:
2944: #
2945: # Add a chat message to to a discussion board.
2946: #
2947: # Parameters:
2948: # $cmd - Request keyword.
2949: # $tail - Tail of the command. A colon separated list
2950: # containing:
2951: # cdom - Domain on which the chat board lives
2952: # cnum - Identifier of the discussion group.
2953: # post - Body of the posting.
2954: # $client - Socket open on the client.
2955: # Returns:
2956: # 1 - Indicating caller should keep on processing.
2957: #
2958: # Side-effects:
2959: # writes a reply to the client.
2960: #
2961: #
2962: sub send_chat_handler {
2963: my ($cmd, $tail, $client) = @_;
2964:
2965:
2966: my $userinput = "$cmd:$tail";
2967:
2968: my ($cdom,$cnum,$newpost)=split(/\:/,$tail);
2969: &chat_add($cdom,$cnum,$newpost);
2970: &Reply($client, "ok\n", $userinput);
2971:
2972: return 1;
2973: }
2974: ®ister_handler("chatsend", \&send_chat_handler, 0, 1, 0);
1.263 albertel 2975:
1.234 foxr 2976: #
2977: # Retrieve the set of chat messagss from a discussion board.
2978: #
2979: # Parameters:
2980: # $cmd - Command keyword that initiated the request.
2981: # $tail - Remainder of the request after the command
2982: # keyword. In this case a colon separated list of
2983: # chat domain - Which discussion board.
2984: # chat id - Discussion thread(?)
2985: # domain/user - Authentication domain and username
2986: # of the requesting person.
2987: # $client - Socket open on the client program.
2988: # Returns:
2989: # 1 - continue processing
2990: # Side effects:
2991: # Response is written to the client.
2992: #
2993: sub retrieve_chat_handler {
2994: my ($cmd, $tail, $client) = @_;
2995:
2996:
2997: my $userinput = "$cmd:$tail";
2998:
2999: my ($cdom,$cnum,$udom,$uname)=split(/\:/,$tail);
3000: my $reply='';
3001: foreach (&get_chat($cdom,$cnum,$udom,$uname)) {
3002: $reply.=&escape($_).':';
3003: }
3004: $reply=~s/\:$//;
3005: &Reply($client, $reply."\n", $userinput);
3006:
3007:
3008: return 1;
3009: }
3010: ®ister_handler("chatretr", \&retrieve_chat_handler, 0, 1, 0);
3011:
3012: #
3013: # Initiate a query of an sql database. SQL query repsonses get put in
3014: # a file for later retrieval. This prevents sql query results from
3015: # bottlenecking the system. Note that with loncnew, perhaps this is
3016: # less of an issue since multiple outstanding requests can be concurrently
3017: # serviced.
3018: #
3019: # Parameters:
3020: # $cmd - COmmand keyword that initiated the request.
3021: # $tail - Remainder of the command after the keyword.
3022: # For this function, this consists of a query and
3023: # 3 arguments that are self-documentingly labelled
3024: # in the original arg1, arg2, arg3.
3025: # $client - Socket open on the client.
3026: # Return:
3027: # 1 - Indicating processing should continue.
3028: # Side-effects:
3029: # a reply is written to $client.
3030: #
3031: sub send_query_handler {
3032: my ($cmd, $tail, $client) = @_;
3033:
3034:
3035: my $userinput = "$cmd:$tail";
3036:
3037: my ($query,$arg1,$arg2,$arg3)=split(/\:/,$tail);
3038: $query=~s/\n*$//g;
3039: &Reply($client, "". &sql_reply("$clientname\&$query".
3040: "\&$arg1"."\&$arg2"."\&$arg3")."\n",
3041: $userinput);
3042:
3043: return 1;
3044: }
3045: ®ister_handler("querysend", \&send_query_handler, 0, 1, 0);
3046:
3047: #
3048: # Add a reply to an sql query. SQL queries are done asyncrhonously.
3049: # The query is submitted via a "querysend" transaction.
3050: # There it is passed on to the lonsql daemon, queued and issued to
3051: # mysql.
3052: # This transaction is invoked when the sql transaction is complete
3053: # it stores the query results in flie and indicates query completion.
3054: # presumably local software then fetches this response... I'm guessing
3055: # the sequence is: lonc does a querysend, we ask lonsql to do it.
3056: # lonsql on completion of the query interacts with the lond of our
3057: # client to do a query reply storing two files:
3058: # - id - The results of the query.
3059: # - id.end - Indicating the transaction completed.
3060: # NOTE: id is a unique id assigned to the query and querysend time.
3061: # Parameters:
3062: # $cmd - Command keyword that initiated this request.
3063: # $tail - Remainder of the tail. In this case that's a colon
3064: # separated list containing the query Id and the
3065: # results of the query.
3066: # $client - Socket open on the client.
3067: # Return:
3068: # 1 - Indicating that we should continue processing.
3069: # Side effects:
3070: # ok written to the client.
3071: #
3072: sub reply_query_handler {
3073: my ($cmd, $tail, $client) = @_;
3074:
3075:
3076: my $userinput = "$cmd:$tail";
3077:
3078: my ($cmd,$id,$reply)=split(/:/,$userinput);
3079: my $store;
3080: my $execdir=$perlvar{'lonDaemons'};
3081: if ($store=IO::File->new(">$execdir/tmp/$id")) {
3082: $reply=~s/\&/\n/g;
3083: print $store $reply;
3084: close $store;
3085: my $store2=IO::File->new(">$execdir/tmp/$id.end");
3086: print $store2 "done\n";
3087: close $store2;
3088: &Reply($client, "ok\n", $userinput);
3089: } else {
3090: &Failure($client, "error: ".($!+0)
3091: ." IO::File->new Failed ".
3092: "while attempting queryreply\n", $userinput);
3093: }
3094:
3095:
3096: return 1;
3097: }
3098: ®ister_handler("queryreply", \&reply_query_handler, 0, 1, 0);
3099:
3100: #
3101: # Process the courseidput request. Not quite sure what this means
3102: # at the system level sense. It appears a gdbm file in the
3103: # /home/httpd/lonUsers/$domain/nohist_courseids is tied and
3104: # a set of entries made in that database.
3105: #
3106: # Parameters:
3107: # $cmd - The command keyword that initiated this request.
3108: # $tail - Tail of the command. In this case consists of a colon
3109: # separated list contaning the domain to apply this to and
3110: # an ampersand separated list of keyword=value pairs.
3111: # $client - Socket open on the client.
3112: # Returns:
3113: # 1 - indicating that processing should continue
3114: #
3115: # Side effects:
3116: # reply is written to the client.
3117: #
3118: sub put_course_id_handler {
3119: my ($cmd, $tail, $client) = @_;
3120:
3121:
3122: my $userinput = "$cmd:$tail";
3123:
1.266 raeburn 3124: my ($udom, $what) = split(/:/, $tail,2);
1.234 foxr 3125: chomp($what);
3126: my $now=time;
3127: my @pairs=split(/\&/,$what);
3128:
3129: my $hashref = &tie_domain_hash($udom, "nohist_courseids", &GDBM_WRCREAT());
3130: if ($hashref) {
3131: foreach my $pair (@pairs) {
1.271 ! raeburn 3132: my ($key,$courseinfo) = split(/=/,$pair,2);
! 3133: $courseinfo =~ s/=/:/g;
1.266 raeburn 3134: $hashref->{$key}=$courseinfo.':'.$now;
1.234 foxr 3135: }
3136: if (untie(%$hashref)) {
1.253 foxr 3137: &Reply( $client, "ok\n", $userinput);
1.234 foxr 3138: } else {
1.253 foxr 3139: &Failure($client, "error: ".($!+0)
1.234 foxr 3140: ." untie(GDBM) Failed ".
3141: "while attempting courseidput\n", $userinput);
3142: }
3143: } else {
1.253 foxr 3144: &Failure($client, "error: ".($!+0)
1.234 foxr 3145: ." tie(GDBM) Failed ".
3146: "while attempting courseidput\n", $userinput);
3147: }
1.253 foxr 3148:
1.234 foxr 3149:
3150: return 1;
3151: }
3152: ®ister_handler("courseidput", \&put_course_id_handler, 0, 1, 0);
3153:
3154: # Retrieves the value of a course id resource keyword pattern
3155: # defined since a starting date. Both the starting date and the
3156: # keyword pattern are optional. If the starting date is not supplied it
3157: # is treated as the beginning of time. If the pattern is not found,
3158: # it is treatred as "." matching everything.
3159: #
3160: # Parameters:
3161: # $cmd - Command keyword that resulted in us being dispatched.
3162: # $tail - The remainder of the command that, in this case, consists
3163: # of a colon separated list of:
3164: # domain - The domain in which the course database is
3165: # defined.
3166: # since - Optional parameter describing the minimum
3167: # time of definition(?) of the resources that
3168: # will match the dump.
3169: # description - regular expression that is used to filter
3170: # the dump. Only keywords matching this regexp
3171: # will be used.
3172: # $client - The socket open on the client.
3173: # Returns:
3174: # 1 - Continue processing.
3175: # Side Effects:
3176: # a reply is written to $client.
3177: sub dump_course_id_handler {
3178: my ($cmd, $tail, $client) = @_;
3179:
3180: my $userinput = "$cmd:$tail";
3181:
1.266 raeburn 3182: my ($udom,$since,$description,$instcodefilter,$ownerfilter) =split(/:/,$tail);
1.234 foxr 3183: if (defined($description)) {
3184: $description=&unescape($description);
3185: } else {
3186: $description='.';
3187: }
1.266 raeburn 3188: if (defined($instcodefilter)) {
3189: $instcodefilter=&unescape($instcodefilter);
3190: } else {
3191: $instcodefilter='.';
3192: }
3193: if (defined($ownerfilter)) {
3194: $ownerfilter=&unescape($ownerfilter);
3195: } else {
3196: $ownerfilter='.';
3197: }
3198:
1.234 foxr 3199: unless (defined($since)) { $since=0; }
3200: my $qresult='';
3201: my $hashref = &tie_domain_hash($udom, "nohist_courseids", &GDBM_WRCREAT());
3202: if ($hashref) {
3203: while (my ($key,$value) = each(%$hashref)) {
1.266 raeburn 3204: my ($descr,$lasttime,$inst_code,$owner);
3205: if ($value =~ m/^([^\:]*):([^\:]*):([^\:]*):(\d+)$/) {
3206: ($descr,$inst_code,$owner,$lasttime)=($1,$2,$3,$4);
3207: } elsif ($value =~ m/^([^\:]*):([^\:]*):(\d+)$/) {
1.234 foxr 3208: ($descr,$inst_code,$lasttime)=($1,$2,$3);
3209: } else {
3210: ($descr,$lasttime) = split(/\:/,$value);
3211: }
3212: if ($lasttime<$since) { next; }
1.266 raeburn 3213: my $match = 1;
3214: unless ($description eq '.') {
3215: my $unescapeDescr = &unescape($descr);
3216: unless (eval('$unescapeDescr=~/\Q$description\E/i')) {
3217: $match = 0;
1.234 foxr 3218: }
1.266 raeburn 3219: }
3220: unless ($instcodefilter eq '.' || !defined($instcodefilter)) {
3221: my $unescapeInstcode = &unescape($inst_code);
3222: unless (eval('$unescapeInstcode=~/\Q$instcodefilter\E/i')) {
3223: $match = 0;
3224: }
1.234 foxr 3225: }
1.266 raeburn 3226: unless ($ownerfilter eq '.' || !defined($ownerfilter)) {
3227: my $unescapeOwner = &unescape($owner);
3228: unless (eval('$unescapeOwner=~/\Q$ownerfilter\E/i')) {
3229: $match = 0;
3230: }
3231: }
3232: if ($match == 1) {
3233: $qresult.=$key.'='.$descr.':'.$inst_code.':'.$owner.'&';
3234: }
1.234 foxr 3235: }
3236: if (untie(%$hashref)) {
3237: chop($qresult);
3238: &Reply($client, "$qresult\n", $userinput);
3239: } else {
3240: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
3241: "while attempting courseiddump\n", $userinput);
3242: }
3243: } else {
3244: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
3245: "while attempting courseiddump\n", $userinput);
3246: }
3247:
3248:
3249: return 1;
3250: }
3251: ®ister_handler("courseiddump", \&dump_course_id_handler, 0, 1, 0);
1.238 foxr 3252:
3253: #
3254: # Puts an id to a domains id database.
3255: #
3256: # Parameters:
3257: # $cmd - The command that triggered us.
3258: # $tail - Remainder of the request other than the command. This is a
3259: # colon separated list containing:
3260: # $domain - The domain for which we are writing the id.
3261: # $pairs - The id info to write... this is and & separated list
3262: # of keyword=value.
3263: # $client - Socket open on the client.
3264: # Returns:
3265: # 1 - Continue processing.
3266: # Side effects:
3267: # reply is written to $client.
3268: #
3269: sub put_id_handler {
3270: my ($cmd,$tail,$client) = @_;
3271:
3272:
3273: my $userinput = "$cmd:$tail";
3274:
3275: my ($udom,$what)=split(/:/,$tail);
3276: chomp($what);
3277: my @pairs=split(/\&/,$what);
3278: my $hashref = &tie_domain_hash($udom, "ids", &GDBM_WRCREAT(),
3279: "P", $what);
3280: if ($hashref) {
3281: foreach my $pair (@pairs) {
3282: my ($key,$value)=split(/=/,$pair);
3283: $hashref->{$key}=$value;
3284: }
3285: if (untie(%$hashref)) {
3286: &Reply($client, "ok\n", $userinput);
3287: } else {
3288: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
3289: "while attempting idput\n", $userinput);
3290: }
3291: } else {
3292: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
3293: "while attempting idput\n", $userinput);
3294: }
3295:
3296: return 1;
3297: }
1.263 albertel 3298: ®ister_handler("idput", \&put_id_handler, 0, 1, 0);
1.238 foxr 3299:
3300: #
3301: # Retrieves a set of id values from the id database.
3302: # Returns an & separated list of results, one for each requested id to the
3303: # client.
3304: #
3305: # Parameters:
3306: # $cmd - Command keyword that caused us to be dispatched.
3307: # $tail - Tail of the command. Consists of a colon separated:
3308: # domain - the domain whose id table we dump
3309: # ids Consists of an & separated list of
3310: # id keywords whose values will be fetched.
3311: # nonexisting keywords will have an empty value.
3312: # $client - Socket open on the client.
3313: #
3314: # Returns:
3315: # 1 - indicating processing should continue.
3316: # Side effects:
3317: # An & separated list of results is written to $client.
3318: #
3319: sub get_id_handler {
3320: my ($cmd, $tail, $client) = @_;
3321:
3322:
3323: my $userinput = "$client:$tail";
3324:
3325: my ($udom,$what)=split(/:/,$tail);
3326: chomp($what);
3327: my @queries=split(/\&/,$what);
3328: my $qresult='';
3329: my $hashref = &tie_domain_hash($udom, "ids", &GDBM_READER());
3330: if ($hashref) {
3331: for (my $i=0;$i<=$#queries;$i++) {
3332: $qresult.="$hashref->{$queries[$i]}&";
3333: }
3334: if (untie(%$hashref)) {
3335: $qresult=~s/\&$//;
3336: &Reply($client, "$qresult\n", $userinput);
3337: } else {
3338: &Failure( $client, "error: ".($!+0)." untie(GDBM) Failed ".
3339: "while attempting idget\n",$userinput);
3340: }
3341: } else {
3342: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
3343: "while attempting idget\n",$userinput);
3344: }
3345:
3346: return 1;
3347: }
1.263 albertel 3348: ®ister_handler("idget", \&get_id_handler, 0, 1, 0);
1.238 foxr 3349:
3350: #
3351: # Process the tmpput command I'm not sure what this does.. Seems to
3352: # create a file in the lonDaemons/tmp directory of the form $id.tmp
3353: # where Id is the client's ip concatenated with a sequence number.
3354: # The file will contain some value that is passed in. Is this e.g.
3355: # a login token?
3356: #
3357: # Parameters:
3358: # $cmd - The command that got us dispatched.
3359: # $tail - The remainder of the request following $cmd:
3360: # In this case this will be the contents of the file.
3361: # $client - Socket connected to the client.
3362: # Returns:
3363: # 1 indicating processing can continue.
3364: # Side effects:
3365: # A file is created in the local filesystem.
3366: # A reply is sent to the client.
3367: sub tmp_put_handler {
3368: my ($cmd, $what, $client) = @_;
3369:
3370: my $userinput = "$cmd:$what"; # Reconstruct for logging.
3371:
3372:
3373: my $store;
3374: $tmpsnum++;
3375: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
3376: $id=~s/\W/\_/g;
3377: $what=~s/\n//g;
3378: my $execdir=$perlvar{'lonDaemons'};
3379: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
3380: print $store $what;
3381: close $store;
3382: &Reply($client, "$id\n", $userinput);
3383: } else {
3384: &Failure( $client, "error: ".($!+0)."IO::File->new Failed ".
3385: "while attempting tmpput\n", $userinput);
3386: }
3387: return 1;
3388:
3389: }
3390: ®ister_handler("tmpput", \&tmp_put_handler, 0, 1, 0);
1.263 albertel 3391:
1.238 foxr 3392: # Processes the tmpget command. This command returns the contents
3393: # of a temporary resource file(?) created via tmpput.
3394: #
3395: # Paramters:
3396: # $cmd - Command that got us dispatched.
3397: # $id - Tail of the command, contain the id of the resource
3398: # we want to fetch.
3399: # $client - socket open on the client.
3400: # Return:
3401: # 1 - Inidcating processing can continue.
3402: # Side effects:
3403: # A reply is sent to the client.
3404: #
3405: sub tmp_get_handler {
3406: my ($cmd, $id, $client) = @_;
3407:
3408: my $userinput = "$cmd:$id";
3409:
3410:
3411: $id=~s/\W/\_/g;
3412: my $store;
3413: my $execdir=$perlvar{'lonDaemons'};
3414: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
3415: my $reply=<$store>;
3416: &Reply( $client, "$reply\n", $userinput);
3417: close $store;
3418: } else {
3419: &Failure( $client, "error: ".($!+0)."IO::File->new Failed ".
3420: "while attempting tmpget\n", $userinput);
3421: }
3422:
3423: return 1;
3424: }
3425: ®ister_handler("tmpget", \&tmp_get_handler, 0, 1, 0);
1.263 albertel 3426:
1.238 foxr 3427: #
3428: # Process the tmpdel command. This command deletes a temp resource
3429: # created by the tmpput command.
3430: #
3431: # Parameters:
3432: # $cmd - Command that got us here.
3433: # $id - Id of the temporary resource created.
3434: # $client - socket open on the client process.
3435: #
3436: # Returns:
3437: # 1 - Indicating processing should continue.
3438: # Side Effects:
3439: # A file is deleted
3440: # A reply is sent to the client.
3441: sub tmp_del_handler {
3442: my ($cmd, $id, $client) = @_;
3443:
3444: my $userinput= "$cmd:$id";
3445:
3446: chomp($id);
3447: $id=~s/\W/\_/g;
3448: my $execdir=$perlvar{'lonDaemons'};
3449: if (unlink("$execdir/tmp/$id.tmp")) {
3450: &Reply($client, "ok\n", $userinput);
3451: } else {
3452: &Failure( $client, "error: ".($!+0)."Unlink tmp Failed ".
3453: "while attempting tmpdel\n", $userinput);
3454: }
3455:
3456: return 1;
3457:
3458: }
3459: ®ister_handler("tmpdel", \&tmp_del_handler, 0, 1, 0);
1.263 albertel 3460:
1.238 foxr 3461: #
1.246 foxr 3462: # Processes the setannounce command. This command
3463: # creates a file named announce.txt in the top directory of
3464: # the documentn root and sets its contents. The announce.txt file is
3465: # printed in its entirety at the LonCAPA login page. Note:
3466: # once the announcement.txt fileis created it cannot be deleted.
3467: # However, setting the contents of the file to empty removes the
3468: # announcement from the login page of loncapa so who cares.
3469: #
3470: # Parameters:
3471: # $cmd - The command that got us dispatched.
3472: # $announcement - The text of the announcement.
3473: # $client - Socket open on the client process.
3474: # Retunrns:
3475: # 1 - Indicating request processing should continue
3476: # Side Effects:
3477: # The file {DocRoot}/announcement.txt is created.
3478: # A reply is sent to $client.
3479: #
3480: sub set_announce_handler {
3481: my ($cmd, $announcement, $client) = @_;
3482:
3483: my $userinput = "$cmd:$announcement";
3484:
3485: chomp($announcement);
3486: $announcement=&unescape($announcement);
3487: if (my $store=IO::File->new('>'.$perlvar{'lonDocRoot'}.
3488: '/announcement.txt')) {
3489: print $store $announcement;
3490: close $store;
3491: &Reply($client, "ok\n", $userinput);
3492: } else {
3493: &Failure($client, "error: ".($!+0)."\n", $userinput);
3494: }
3495:
3496: return 1;
3497: }
3498: ®ister_handler("setannounce", \&set_announce_handler, 0, 1, 0);
1.263 albertel 3499:
1.246 foxr 3500: #
3501: # Return the version of the daemon. This can be used to determine
3502: # the compatibility of cross version installations or, alternatively to
3503: # simply know who's out of date and who isn't. Note that the version
3504: # is returned concatenated with the tail.
3505: # Parameters:
3506: # $cmd - the request that dispatched to us.
3507: # $tail - Tail of the request (client's version?).
3508: # $client - Socket open on the client.
3509: #Returns:
3510: # 1 - continue processing requests.
3511: # Side Effects:
3512: # Replies with version to $client.
3513: sub get_version_handler {
3514: my ($cmd, $tail, $client) = @_;
3515:
3516: my $userinput = $cmd.$tail;
3517:
3518: &Reply($client, &version($userinput)."\n", $userinput);
3519:
3520:
3521: return 1;
3522: }
3523: ®ister_handler("version", \&get_version_handler, 0, 1, 0);
1.263 albertel 3524:
1.246 foxr 3525: # Set the current host and domain. This is used to support
3526: # multihomed systems. Each IP of the system, or even separate daemons
3527: # on the same IP can be treated as handling a separate lonCAPA virtual
3528: # machine. This command selects the virtual lonCAPA. The client always
3529: # knows the right one since it is lonc and it is selecting the domain/system
3530: # from the hosts.tab file.
3531: # Parameters:
3532: # $cmd - Command that dispatched us.
3533: # $tail - Tail of the command (domain/host requested).
3534: # $socket - Socket open on the client.
3535: #
3536: # Returns:
3537: # 1 - Indicates the program should continue to process requests.
3538: # Side-effects:
3539: # The default domain/system context is modified for this daemon.
3540: # a reply is sent to the client.
3541: #
3542: sub set_virtual_host_handler {
3543: my ($cmd, $tail, $socket) = @_;
3544:
3545: my $userinput ="$cmd:$tail";
3546:
3547: &Reply($client, &sethost($userinput)."\n", $userinput);
3548:
3549:
3550: return 1;
3551: }
1.247 albertel 3552: ®ister_handler("sethost", \&set_virtual_host_handler, 0, 1, 0);
1.246 foxr 3553:
3554: # Process a request to exit:
3555: # - "bye" is sent to the client.
3556: # - The client socket is shutdown and closed.
3557: # - We indicate to the caller that we should exit.
3558: # Formal Parameters:
3559: # $cmd - The command that got us here.
3560: # $tail - Tail of the command (empty).
3561: # $client - Socket open on the tail.
3562: # Returns:
3563: # 0 - Indicating the program should exit!!
3564: #
3565: sub exit_handler {
3566: my ($cmd, $tail, $client) = @_;
3567:
3568: my $userinput = "$cmd:$tail";
3569:
3570: &logthis("Client $clientip ($clientname) hanging up: $userinput");
3571: &Reply($client, "bye\n", $userinput);
3572: $client->shutdown(2); # shutdown the socket forcibly.
3573: $client->close();
3574:
3575: return 0;
3576: }
1.248 foxr 3577: ®ister_handler("exit", \&exit_handler, 0,1,1);
3578: ®ister_handler("init", \&exit_handler, 0,1,1);
3579: ®ister_handler("quit", \&exit_handler, 0,1,1);
3580:
3581: # Determine if auto-enrollment is enabled.
3582: # Note that the original had what I believe to be a defect.
3583: # The original returned 0 if the requestor was not a registerd client.
3584: # It should return "refused".
3585: # Formal Parameters:
3586: # $cmd - The command that invoked us.
3587: # $tail - The tail of the command (Extra command parameters.
3588: # $client - The socket open on the client that issued the request.
3589: # Returns:
3590: # 1 - Indicating processing should continue.
3591: #
3592: sub enrollment_enabled_handler {
3593: my ($cmd, $tail, $client) = @_;
3594: my $userinput = $cmd.":".$tail; # For logging purposes.
3595:
3596:
3597: my $cdom = split(/:/, $tail); # Domain we're asking about.
3598: my $outcome = &localenroll::run($cdom);
3599: &Reply($client, "$outcome\n", $userinput);
3600:
3601: return 1;
3602: }
3603: ®ister_handler("autorun", \&enrollment_enabled_handler, 0, 1, 0);
3604:
3605: # Get the official sections for which auto-enrollment is possible.
3606: # Since the admin people won't know about 'unofficial sections'
3607: # we cannot auto-enroll on them.
3608: # Formal Parameters:
3609: # $cmd - The command request that got us dispatched here.
3610: # $tail - The remainder of the request. In our case this
3611: # will be split into:
3612: # $coursecode - The course name from the admin point of view.
3613: # $cdom - The course's domain(?).
3614: # $client - Socket open on the client.
3615: # Returns:
3616: # 1 - Indiciting processing should continue.
3617: #
3618: sub get_sections_handler {
3619: my ($cmd, $tail, $client) = @_;
3620: my $userinput = "$cmd:$tail";
3621:
3622: my ($coursecode, $cdom) = split(/:/, $tail);
3623: my @secs = &localenroll::get_sections($coursecode,$cdom);
3624: my $seclist = &escape(join(':',@secs));
3625:
3626: &Reply($client, "$seclist\n", $userinput);
3627:
3628:
3629: return 1;
3630: }
3631: ®ister_handler("autogetsections", \&get_sections_handler, 0, 1, 0);
3632:
3633: # Validate the owner of a new course section.
3634: #
3635: # Formal Parameters:
3636: # $cmd - Command that got us dispatched.
3637: # $tail - the remainder of the command. For us this consists of a
3638: # colon separated string containing:
3639: # $inst - Course Id from the institutions point of view.
3640: # $owner - Proposed owner of the course.
3641: # $cdom - Domain of the course (from the institutions
3642: # point of view?)..
3643: # $client - Socket open on the client.
3644: #
3645: # Returns:
3646: # 1 - Processing should continue.
3647: #
3648: sub validate_course_owner_handler {
3649: my ($cmd, $tail, $client) = @_;
3650: my $userinput = "$cmd:$tail";
3651: my ($inst_course_id, $owner, $cdom) = split(/:/, $tail);
3652:
3653: my $outcome = &localenroll::new_course($inst_course_id,$owner,$cdom);
3654: &Reply($client, "$outcome\n", $userinput);
3655:
3656:
3657:
3658: return 1;
3659: }
3660: ®ister_handler("autonewcourse", \&validate_course_owner_handler, 0, 1, 0);
1.263 albertel 3661:
1.248 foxr 3662: #
3663: # Validate a course section in the official schedule of classes
3664: # from the institutions point of view (part of autoenrollment).
3665: #
3666: # Formal Parameters:
3667: # $cmd - The command request that got us dispatched.
3668: # $tail - The tail of the command. In this case,
3669: # this is a colon separated set of words that will be split
3670: # into:
3671: # $inst_course_id - The course/section id from the
3672: # institutions point of view.
3673: # $cdom - The domain from the institutions
3674: # point of view.
3675: # $client - Socket open on the client.
3676: # Returns:
3677: # 1 - Indicating processing should continue.
3678: #
3679: sub validate_course_section_handler {
3680: my ($cmd, $tail, $client) = @_;
3681: my $userinput = "$cmd:$tail";
3682: my ($inst_course_id, $cdom) = split(/:/, $tail);
3683:
3684: my $outcome=&localenroll::validate_courseID($inst_course_id,$cdom);
3685: &Reply($client, "$outcome\n", $userinput);
3686:
3687:
3688: return 1;
3689: }
3690: ®ister_handler("autovalidatecourse", \&validate_course_section_handler, 0, 1, 0);
3691:
3692: #
3693: # Create a password for a new auto-enrollment user.
3694: # I think/guess, this password allows access to the institutions
3695: # AIS class list server/services. Stuart can correct this comment
3696: # when he finds out how wrong I am.
3697: #
3698: # Formal Parameters:
3699: # $cmd - The command request that got us dispatched.
3700: # $tail - The tail of the command. In this case this is a colon separated
3701: # set of words that will be split into:
3702: # $authparam - An authentication parameter (username??).
3703: # $cdom - The domain of the course from the institution's
3704: # point of view.
3705: # $client - The socket open on the client.
3706: # Returns:
3707: # 1 - continue processing.
3708: #
3709: sub create_auto_enroll_password_handler {
3710: my ($cmd, $tail, $client) = @_;
3711: my $userinput = "$cmd:$tail";
3712:
3713: my ($authparam, $cdom) = split(/:/, $userinput);
3714:
3715: my ($create_passwd,$authchk);
3716: ($authparam,
3717: $create_passwd,
3718: $authchk) = &localenroll::create_password($authparam,$cdom);
3719:
3720: &Reply($client, &escape($authparam.':'.$create_passwd.':'.$authchk)."\n",
3721: $userinput);
3722:
3723:
3724: return 1;
3725: }
3726: ®ister_handler("autocreatepassword", \&create_auto_enroll_password_handler,
3727: 0, 1, 0);
3728:
3729: # Retrieve and remove temporary files created by/during autoenrollment.
3730: #
3731: # Formal Parameters:
3732: # $cmd - The command that got us dispatched.
3733: # $tail - The tail of the command. In our case this is a colon
3734: # separated list that will be split into:
3735: # $filename - The name of the file to remove.
3736: # The filename is given as a path relative to
3737: # the LonCAPA temp file directory.
3738: # $client - Socket open on the client.
3739: #
3740: # Returns:
3741: # 1 - Continue processing.
3742: sub retrieve_auto_file_handler {
3743: my ($cmd, $tail, $client) = @_;
3744: my $userinput = "cmd:$tail";
3745:
3746: my ($filename) = split(/:/, $tail);
3747:
3748: my $source = $perlvar{'lonDaemons'}.'/tmp/'.$filename;
3749: if ( (-e $source) && ($filename ne '') ) {
3750: my $reply = '';
3751: if (open(my $fh,$source)) {
3752: while (<$fh>) {
3753: chomp($_);
3754: $_ =~ s/^\s+//g;
3755: $_ =~ s/\s+$//g;
3756: $reply .= $_;
3757: }
3758: close($fh);
3759: &Reply($client, &escape($reply)."\n", $userinput);
3760:
3761: # Does this have to be uncommented??!? (RF).
3762: #
3763: # unlink($source);
3764: } else {
3765: &Failure($client, "error\n", $userinput);
3766: }
3767: } else {
3768: &Failure($client, "error\n", $userinput);
3769: }
3770:
3771:
3772: return 1;
3773: }
3774: ®ister_handler("autoretrieve", \&retrieve_auto_file_handler, 0,1,0);
3775:
3776: #
3777: # Read and retrieve institutional code format (for support form).
3778: # Formal Parameters:
3779: # $cmd - Command that dispatched us.
3780: # $tail - Tail of the command. In this case it conatins
3781: # the course domain and the coursename.
3782: # $client - Socket open on the client.
3783: # Returns:
3784: # 1 - Continue processing.
3785: #
3786: sub get_institutional_code_format_handler {
3787: my ($cmd, $tail, $client) = @_;
3788: my $userinput = "$cmd:$tail";
3789:
3790: my $reply;
3791: my($cdom,$course) = split(/:/,$tail);
3792: my @pairs = split/\&/,$course;
3793: my %instcodes = ();
3794: my %codes = ();
3795: my @codetitles = ();
3796: my %cat_titles = ();
3797: my %cat_order = ();
3798: foreach (@pairs) {
3799: my ($key,$value) = split/=/,$_;
3800: $instcodes{&unescape($key)} = &unescape($value);
3801: }
3802: my $formatreply = &localenroll::instcode_format($cdom,
3803: \%instcodes,
3804: \%codes,
3805: \@codetitles,
3806: \%cat_titles,
3807: \%cat_order);
3808: if ($formatreply eq 'ok') {
3809: my $codes_str = &hash2str(%codes);
3810: my $codetitles_str = &array2str(@codetitles);
3811: my $cat_titles_str = &hash2str(%cat_titles);
3812: my $cat_order_str = &hash2str(%cat_order);
3813: &Reply($client,
3814: $codes_str.':'.$codetitles_str.':'.$cat_titles_str.':'
3815: .$cat_order_str."\n",
3816: $userinput);
3817: } else {
3818: # this else branch added by RF since if not ok, lonc will
3819: # hang waiting on reply until timeout.
3820: #
3821: &Reply($client, "format_error\n", $userinput);
3822: }
3823:
3824: return 1;
3825: }
1.265 albertel 3826: ®ister_handler("autoinstcodeformat",
3827: \&get_institutional_code_format_handler,0,1,0);
1.246 foxr 3828:
1.265 albertel 3829: #
3830: # Gets a student's photo to exist (in the correct image type) in the user's
3831: # directory.
3832: # Formal Parameters:
3833: # $cmd - The command request that got us dispatched.
3834: # $tail - A colon separated set of words that will be split into:
3835: # $domain - student's domain
3836: # $uname - student username
3837: # $type - image type desired
3838: # $client - The socket open on the client.
3839: # Returns:
3840: # 1 - continue processing.
3841: sub student_photo_handler {
3842: my ($cmd, $tail, $client) = @_;
3843: my ($domain,$uname,$type) = split(/:/, $tail);
3844:
3845: my $path=&propath($domain,$uname).
3846: '/userfiles/internal/studentphoto.'.$type;
3847: if (-e $path) {
3848: &Reply($client,"ok\n","$cmd:$tail");
3849: return 1;
3850: }
3851: &mkpath($path);
3852: my $file=&localstudentphoto::fetch($domain,$uname);
3853: if (!$file) {
3854: &Failure($client,"unavailable\n","$cmd:$tail");
3855: return 1;
3856: }
3857: if (!-e $path) { &convert_photo($file,$path); }
3858: if (-e $path) {
3859: &Reply($client,"ok\n","$cmd:$tail");
3860: return 1;
3861: }
3862: &Failure($client,"unable_to_convert\n","$cmd:$tail");
3863: return 1;
3864: }
3865: ®ister_handler("studentphoto", \&student_photo_handler, 0, 1, 0);
1.246 foxr 3866:
1.264 albertel 3867: # mkpath makes all directories for a file, expects an absolute path with a
3868: # file or a trailing / if just a dir is passed
3869: # returns 1 on success 0 on failure
3870: sub mkpath {
3871: my ($file)=@_;
3872: my @parts=split(/\//,$file,-1);
3873: my $now=$parts[0].'/'.$parts[1].'/'.$parts[2];
3874: for (my $i=3;$i<= ($#parts-1);$i++) {
1.265 albertel 3875: $now.='/'.$parts[$i];
1.264 albertel 3876: if (!-e $now) {
3877: if (!mkdir($now,0770)) { return 0; }
3878: }
3879: }
3880: return 1;
3881: }
3882:
1.207 foxr 3883: #---------------------------------------------------------------
3884: #
3885: # Getting, decoding and dispatching requests:
3886: #
3887: #
3888: # Get a Request:
3889: # Gets a Request message from the client. The transaction
3890: # is defined as a 'line' of text. We remove the new line
3891: # from the text line.
1.226 foxr 3892: #
1.211 albertel 3893: sub get_request {
1.207 foxr 3894: my $input = <$client>;
3895: chomp($input);
1.226 foxr 3896:
1.234 foxr 3897: &Debug("get_request: Request = $input\n");
1.207 foxr 3898:
3899: &status('Processing '.$clientname.':'.$input);
3900:
3901: return $input;
3902: }
1.212 foxr 3903: #---------------------------------------------------------------
3904: #
3905: # Process a request. This sub should shrink as each action
3906: # gets farmed out into a separat sub that is registered
3907: # with the dispatch hash.
3908: #
3909: # Parameters:
3910: # user_input - The request received from the client (lonc).
3911: # Returns:
3912: # true to keep processing, false if caller should exit.
3913: #
3914: sub process_request {
3915: my ($userinput) = @_; # Easier for now to break style than to
3916: # fix all the userinput -> user_input.
3917: my $wasenc = 0; # True if request was encrypted.
3918: # ------------------------------------------------------------ See if encrypted
3919: if ($userinput =~ /^enc/) {
3920: $userinput = decipher($userinput);
3921: $wasenc=1;
3922: if(!$userinput) { # Cipher not defined.
1.251 foxr 3923: &Failure($client, "error: Encrypted data without negotated key\n");
1.212 foxr 3924: return 0;
3925: }
3926: }
3927: Debug("process_request: $userinput\n");
3928:
1.213 foxr 3929: #
3930: # The 'correct way' to add a command to lond is now to
3931: # write a sub to execute it and Add it to the command dispatch
3932: # hash via a call to register_handler.. The comments to that
3933: # sub should give you enough to go on to show how to do this
3934: # along with the examples that are building up as this code
3935: # is getting refactored. Until all branches of the
3936: # if/elseif monster below have been factored out into
3937: # separate procesor subs, if the dispatch hash is missing
3938: # the command keyword, we will fall through to the remainder
3939: # of the if/else chain below in order to keep this thing in
3940: # working order throughout the transmogrification.
3941:
3942: my ($command, $tail) = split(/:/, $userinput, 2);
3943: chomp($command);
3944: chomp($tail);
3945: $tail =~ s/(\r)//; # This helps people debugging with e.g. telnet.
1.214 foxr 3946: $command =~ s/(\r)//; # And this too for parameterless commands.
3947: if(!$tail) {
3948: $tail =""; # defined but blank.
3949: }
1.213 foxr 3950:
3951: &Debug("Command received: $command, encoded = $wasenc");
3952:
3953: if(defined $Dispatcher{$command}) {
3954:
3955: my $dispatch_info = $Dispatcher{$command};
3956: my $handler = $$dispatch_info[0];
3957: my $need_encode = $$dispatch_info[1];
3958: my $client_types = $$dispatch_info[2];
3959: Debug("Matched dispatch hash: mustencode: $need_encode "
3960: ."ClientType $client_types");
3961:
3962: # Validate the request:
3963:
3964: my $ok = 1;
3965: my $requesterprivs = 0;
3966: if(&isClient()) {
3967: $requesterprivs |= $CLIENT_OK;
3968: }
3969: if(&isManager()) {
3970: $requesterprivs |= $MANAGER_OK;
3971: }
3972: if($need_encode && (!$wasenc)) {
3973: Debug("Must encode but wasn't: $need_encode $wasenc");
3974: $ok = 0;
3975: }
3976: if(($client_types & $requesterprivs) == 0) {
3977: Debug("Client not privileged to do this operation");
3978: $ok = 0;
3979: }
3980:
3981: if($ok) {
3982: Debug("Dispatching to handler $command $tail");
3983: my $keep_going = &$handler($command, $tail, $client);
3984: return $keep_going;
3985: } else {
3986: Debug("Refusing to dispatch because client did not match requirements");
3987: Failure($client, "refused\n", $userinput);
3988: return 1;
3989: }
3990:
3991: }
3992:
1.262 foxr 3993: print $client "unknown_cmd\n";
1.212 foxr 3994: # -------------------------------------------------------------------- complete
3995: Debug("process_request - returning 1");
3996: return 1;
3997: }
1.207 foxr 3998: #
3999: # Decipher encoded traffic
4000: # Parameters:
4001: # input - Encoded data.
4002: # Returns:
4003: # Decoded data or undef if encryption key was not yet negotiated.
4004: # Implicit input:
4005: # cipher - This global holds the negotiated encryption key.
4006: #
1.211 albertel 4007: sub decipher {
1.207 foxr 4008: my ($input) = @_;
4009: my $output = '';
1.212 foxr 4010:
4011:
1.207 foxr 4012: if($cipher) {
4013: my($enc, $enclength, $encinput) = split(/:/, $input);
4014: for(my $encidx = 0; $encidx < length($encinput); $encidx += 16) {
4015: $output .=
4016: $cipher->decrypt(pack("H16", substr($encinput, $encidx, 16)));
4017: }
4018: return substr($output, 0, $enclength);
4019: } else {
4020: return undef;
4021: }
4022: }
4023:
4024: #
4025: # Register a command processor. This function is invoked to register a sub
4026: # to process a request. Once registered, the ProcessRequest sub can automatically
4027: # dispatch requests to an appropriate sub, and do the top level validity checking
4028: # as well:
4029: # - Is the keyword recognized.
4030: # - Is the proper client type attempting the request.
4031: # - Is the request encrypted if it has to be.
4032: # Parameters:
4033: # $request_name - Name of the request being registered.
4034: # This is the command request that will match
4035: # against the hash keywords to lookup the information
4036: # associated with the dispatch information.
4037: # $procedure - Reference to a sub to call to process the request.
4038: # All subs get called as follows:
4039: # Procedure($cmd, $tail, $replyfd, $key)
4040: # $cmd - the actual keyword that invoked us.
4041: # $tail - the tail of the request that invoked us.
4042: # $replyfd- File descriptor connected to the client
4043: # $must_encode - True if the request must be encoded to be good.
4044: # $client_ok - True if it's ok for a client to request this.
4045: # $manager_ok - True if it's ok for a manager to request this.
4046: # Side effects:
4047: # - On success, the Dispatcher hash has an entry added for the key $RequestName
4048: # - On failure, the program will die as it's a bad internal bug to try to
4049: # register a duplicate command handler.
4050: #
1.211 albertel 4051: sub register_handler {
1.212 foxr 4052: my ($request_name,$procedure,$must_encode, $client_ok,$manager_ok) = @_;
1.207 foxr 4053:
4054: # Don't allow duplication#
4055:
4056: if (defined $Dispatcher{$request_name}) {
4057: die "Attempting to define a duplicate request handler for $request_name\n";
4058: }
4059: # Build the client type mask:
4060:
4061: my $client_type_mask = 0;
4062: if($client_ok) {
4063: $client_type_mask |= $CLIENT_OK;
4064: }
4065: if($manager_ok) {
4066: $client_type_mask |= $MANAGER_OK;
4067: }
4068:
4069: # Enter the hash:
4070:
4071: my @entry = ($procedure, $must_encode, $client_type_mask);
4072:
4073: $Dispatcher{$request_name} = \@entry;
4074:
4075: }
4076:
4077:
4078: #------------------------------------------------------------------
4079:
4080:
4081:
4082:
1.141 foxr 4083: #
1.96 foxr 4084: # Convert an error return code from lcpasswd to a string value.
4085: #
4086: sub lcpasswdstrerror {
4087: my $ErrorCode = shift;
1.97 foxr 4088: if(($ErrorCode < 0) || ($ErrorCode > $lastpwderror)) {
1.96 foxr 4089: return "lcpasswd Unrecognized error return value ".$ErrorCode;
4090: } else {
1.98 foxr 4091: return $passwderrors[$ErrorCode];
1.96 foxr 4092: }
4093: }
4094:
1.97 foxr 4095: #
4096: # Convert an error return code from lcuseradd to a string value:
4097: #
4098: sub lcuseraddstrerror {
4099: my $ErrorCode = shift;
4100: if(($ErrorCode < 0) || ($ErrorCode > $lastadderror)) {
4101: return "lcuseradd - Unrecognized error code: ".$ErrorCode;
4102: } else {
1.98 foxr 4103: return $adderrors[$ErrorCode];
1.97 foxr 4104: }
4105: }
4106:
1.23 harris41 4107: # grabs exception and records it to log before exiting
4108: sub catchexception {
1.27 albertel 4109: my ($error)=@_;
1.25 www 4110: $SIG{'QUIT'}='DEFAULT';
4111: $SIG{__DIE__}='DEFAULT';
1.165 albertel 4112: &status("Catching exception");
1.190 albertel 4113: &logthis("<font color='red'>CRITICAL: "
1.134 albertel 4114: ."ABNORMAL EXIT. Child $$ for server $thisserver died through "
1.27 albertel 4115: ."a crash with this error msg->[$error]</font>");
1.57 www 4116: &logthis('Famous last words: '.$status.' - '.$lastlog);
1.27 albertel 4117: if ($client) { print $client "error: $error\n"; }
1.59 www 4118: $server->close();
1.27 albertel 4119: die($error);
1.23 harris41 4120: }
1.63 www 4121: sub timeout {
1.165 albertel 4122: &status("Handling Timeout");
1.190 albertel 4123: &logthis("<font color='red'>CRITICAL: TIME OUT ".$$."</font>");
1.63 www 4124: &catchexception('Timeout');
4125: }
1.22 harris41 4126: # -------------------------------- Set signal handlers to record abnormal exits
4127:
1.226 foxr 4128:
1.22 harris41 4129: $SIG{'QUIT'}=\&catchexception;
4130: $SIG{__DIE__}=\&catchexception;
4131:
1.81 matthew 4132: # ---------------------------------- Read loncapa_apache.conf and loncapa.conf
1.95 harris41 4133: &status("Read loncapa.conf and loncapa_apache.conf");
4134: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
1.141 foxr 4135: %perlvar=%{$perlvarref};
1.80 harris41 4136: undef $perlvarref;
1.19 www 4137:
1.35 harris41 4138: # ----------------------------- Make sure this process is running from user=www
4139: my $wwwid=getpwnam('www');
4140: if ($wwwid!=$<) {
1.134 albertel 4141: my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
4142: my $subj="LON: $currenthostid User ID mismatch";
1.37 harris41 4143: system("echo 'User ID mismatch. lond must be run as user www.' |\
1.35 harris41 4144: mailto $emailto -s '$subj' > /dev/null");
4145: exit 1;
4146: }
4147:
1.19 www 4148: # --------------------------------------------- Check if other instance running
4149:
4150: my $pidfile="$perlvar{'lonDaemons'}/logs/lond.pid";
4151:
4152: if (-e $pidfile) {
4153: my $lfh=IO::File->new("$pidfile");
4154: my $pide=<$lfh>;
4155: chomp($pide);
1.29 harris41 4156: if (kill 0 => $pide) { die "already running"; }
1.19 www 4157: }
1.1 albertel 4158:
4159: # ------------------------------------------------------------- Read hosts file
4160:
4161:
4162:
4163: # establish SERVER socket, bind and listen.
4164: $server = IO::Socket::INET->new(LocalPort => $perlvar{'londPort'},
4165: Type => SOCK_STREAM,
4166: Proto => 'tcp',
4167: Reuse => 1,
4168: Listen => 10 )
1.29 harris41 4169: or die "making socket: $@\n";
1.1 albertel 4170:
4171: # --------------------------------------------------------- Do global variables
4172:
4173: # global variables
4174:
1.134 albertel 4175: my %children = (); # keys are current child process IDs
1.1 albertel 4176:
4177: sub REAPER { # takes care of dead children
4178: $SIG{CHLD} = \&REAPER;
1.165 albertel 4179: &status("Handling child death");
1.178 foxr 4180: my $pid;
4181: do {
4182: $pid = waitpid(-1,&WNOHANG());
4183: if (defined($children{$pid})) {
4184: &logthis("Child $pid died");
4185: delete($children{$pid});
1.183 albertel 4186: } elsif ($pid > 0) {
1.178 foxr 4187: &logthis("Unknown Child $pid died");
4188: }
4189: } while ( $pid > 0 );
4190: foreach my $child (keys(%children)) {
4191: $pid = waitpid($child,&WNOHANG());
4192: if ($pid > 0) {
4193: &logthis("Child $child - $pid looks like we missed it's death");
4194: delete($children{$pid});
4195: }
1.176 albertel 4196: }
1.165 albertel 4197: &status("Finished Handling child death");
1.1 albertel 4198: }
4199:
4200: sub HUNTSMAN { # signal handler for SIGINT
1.165 albertel 4201: &status("Killing children (INT)");
1.1 albertel 4202: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
4203: kill 'INT' => keys %children;
1.59 www 4204: &logthis("Free socket: ".shutdown($server,2)); # free up socket
1.1 albertel 4205: my $execdir=$perlvar{'lonDaemons'};
4206: unlink("$execdir/logs/lond.pid");
1.190 albertel 4207: &logthis("<font color='red'>CRITICAL: Shutting down</font>");
1.165 albertel 4208: &status("Done killing children");
1.1 albertel 4209: exit; # clean up with dignity
4210: }
4211:
4212: sub HUPSMAN { # signal handler for SIGHUP
4213: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
1.165 albertel 4214: &status("Killing children for restart (HUP)");
1.1 albertel 4215: kill 'INT' => keys %children;
1.59 www 4216: &logthis("Free socket: ".shutdown($server,2)); # free up socket
1.190 albertel 4217: &logthis("<font color='red'>CRITICAL: Restarting</font>");
1.134 albertel 4218: my $execdir=$perlvar{'lonDaemons'};
1.30 harris41 4219: unlink("$execdir/logs/lond.pid");
1.165 albertel 4220: &status("Restarting self (HUP)");
1.1 albertel 4221: exec("$execdir/lond"); # here we go again
4222: }
4223:
1.144 foxr 4224: #
1.148 foxr 4225: # Kill off hashes that describe the host table prior to re-reading it.
4226: # Hashes affected are:
1.200 matthew 4227: # %hostid, %hostdom %hostip %hostdns.
1.148 foxr 4228: #
4229: sub KillHostHashes {
4230: foreach my $key (keys %hostid) {
4231: delete $hostid{$key};
4232: }
4233: foreach my $key (keys %hostdom) {
4234: delete $hostdom{$key};
4235: }
4236: foreach my $key (keys %hostip) {
4237: delete $hostip{$key};
4238: }
1.200 matthew 4239: foreach my $key (keys %hostdns) {
4240: delete $hostdns{$key};
4241: }
1.148 foxr 4242: }
4243: #
4244: # Read in the host table from file and distribute it into the various hashes:
4245: #
4246: # - %hostid - Indexed by IP, the loncapa hostname.
4247: # - %hostdom - Indexed by loncapa hostname, the domain.
4248: # - %hostip - Indexed by hostid, the Ip address of the host.
4249: sub ReadHostTable {
4250:
4251: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
1.200 matthew 4252: my $myloncapaname = $perlvar{'lonHostID'};
4253: Debug("My loncapa name is : $myloncapaname");
1.148 foxr 4254: while (my $configline=<CONFIG>) {
1.178 foxr 4255: if (!($configline =~ /^\s*\#/)) {
4256: my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
4257: chomp($ip); $ip=~s/\D+$//;
1.200 matthew 4258: $hostid{$ip}=$id; # LonCAPA name of host by IP.
4259: $hostdom{$id}=$domain; # LonCAPA domain name of host.
4260: $hostip{$id}=$ip; # IP address of host.
4261: $hostdns{$name} = $id; # LonCAPA name of host by DNS.
4262:
4263: if ($id eq $perlvar{'lonHostID'}) {
4264: Debug("Found me in the host table: $name");
4265: $thisserver=$name;
4266: }
1.178 foxr 4267: }
1.148 foxr 4268: }
4269: close(CONFIG);
4270: }
4271: #
4272: # Reload the Apache daemon's state.
1.150 foxr 4273: # This is done by invoking /home/httpd/perl/apachereload
4274: # a setuid perl script that can be root for us to do this job.
1.148 foxr 4275: #
4276: sub ReloadApache {
1.150 foxr 4277: my $execdir = $perlvar{'lonDaemons'};
4278: my $script = $execdir."/apachereload";
4279: system($script);
1.148 foxr 4280: }
4281:
4282: #
1.144 foxr 4283: # Called in response to a USR2 signal.
4284: # - Reread hosts.tab
4285: # - All children connected to hosts that were removed from hosts.tab
4286: # are killed via SIGINT
4287: # - All children connected to previously existing hosts are sent SIGUSR1
4288: # - Our internal hosts hash is updated to reflect the new contents of
4289: # hosts.tab causing connections from hosts added to hosts.tab to
4290: # now be honored.
4291: #
4292: sub UpdateHosts {
1.165 albertel 4293: &status("Reload hosts.tab");
1.147 foxr 4294: logthis('<font color="blue"> Updating connections </font>');
1.148 foxr 4295: #
4296: # The %children hash has the set of IP's we currently have children
4297: # on. These need to be matched against records in the hosts.tab
4298: # Any ip's no longer in the table get killed off they correspond to
4299: # either dropped or changed hosts. Note that the re-read of the table
4300: # will take care of new and changed hosts as connections come into being.
4301:
4302:
4303: KillHostHashes;
4304: ReadHostTable;
4305:
4306: foreach my $child (keys %children) {
4307: my $childip = $children{$child};
4308: if(!$hostid{$childip}) {
1.149 foxr 4309: logthis('<font color="blue"> UpdateHosts killing child '
4310: ." $child for ip $childip </font>");
1.148 foxr 4311: kill('INT', $child);
1.149 foxr 4312: } else {
4313: logthis('<font color="green"> keeping child for ip '
4314: ." $childip (pid=$child) </font>");
1.148 foxr 4315: }
4316: }
4317: ReloadApache;
1.165 albertel 4318: &status("Finished reloading hosts.tab");
1.144 foxr 4319: }
4320:
1.148 foxr 4321:
1.57 www 4322: sub checkchildren {
1.165 albertel 4323: &status("Checking on the children (sending signals)");
1.57 www 4324: &initnewstatus();
4325: &logstatus();
4326: &logthis('Going to check on the children');
1.134 albertel 4327: my $docdir=$perlvar{'lonDocRoot'};
1.61 harris41 4328: foreach (sort keys %children) {
1.221 albertel 4329: #sleep 1;
1.57 www 4330: unless (kill 'USR1' => $_) {
4331: &logthis ('Child '.$_.' is dead');
4332: &logstatus($$.' is dead');
1.221 albertel 4333: delete($children{$_});
1.57 www 4334: }
1.61 harris41 4335: }
1.63 www 4336: sleep 5;
1.212 foxr 4337: $SIG{ALRM} = sub { Debug("timeout");
4338: die "timeout"; };
1.113 albertel 4339: $SIG{__DIE__} = 'DEFAULT';
1.165 albertel 4340: &status("Checking on the children (waiting for reports)");
1.63 www 4341: foreach (sort keys %children) {
4342: unless (-e "$docdir/lon-status/londchld/$_.txt") {
1.113 albertel 4343: eval {
4344: alarm(300);
1.63 www 4345: &logthis('Child '.$_.' did not respond');
1.67 albertel 4346: kill 9 => $_;
1.131 albertel 4347: #$emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
4348: #$subj="LON: $currenthostid killed lond process $_";
4349: #my $result=`echo 'Killed lond process $_.' | mailto $emailto -s '$subj' > /dev/null`;
4350: #$execdir=$perlvar{'lonDaemons'};
4351: #$result=`/bin/cp $execdir/logs/lond.log $execdir/logs/lond.log.$_`;
1.221 albertel 4352: delete($children{$_});
1.113 albertel 4353: alarm(0);
4354: }
1.63 www 4355: }
4356: }
1.113 albertel 4357: $SIG{ALRM} = 'DEFAULT';
1.155 albertel 4358: $SIG{__DIE__} = \&catchexception;
1.165 albertel 4359: &status("Finished checking children");
1.221 albertel 4360: &logthis('Finished Checking children');
1.57 www 4361: }
4362:
1.1 albertel 4363: # --------------------------------------------------------------------- Logging
4364:
4365: sub logthis {
4366: my $message=shift;
4367: my $execdir=$perlvar{'lonDaemons'};
4368: my $fh=IO::File->new(">>$execdir/logs/lond.log");
4369: my $now=time;
4370: my $local=localtime($now);
1.58 www 4371: $lastlog=$local.': '.$message;
1.1 albertel 4372: print $fh "$local ($$): $message\n";
4373: }
4374:
1.77 foxr 4375: # ------------------------- Conditional log if $DEBUG true.
4376: sub Debug {
4377: my $message = shift;
4378: if($DEBUG) {
4379: &logthis($message);
4380: }
4381: }
1.161 foxr 4382:
4383: #
4384: # Sub to do replies to client.. this gives a hook for some
4385: # debug tracing too:
4386: # Parameters:
4387: # fd - File open on client.
4388: # reply - Text to send to client.
4389: # request - Original request from client.
4390: #
4391: sub Reply {
1.192 foxr 4392: my ($fd, $reply, $request) = @_;
1.161 foxr 4393: print $fd $reply;
4394: Debug("Request was $request Reply was $reply");
4395:
1.212 foxr 4396: $Transactions++;
4397:
4398:
4399: }
4400:
4401:
4402: #
4403: # Sub to report a failure.
4404: # This function:
4405: # - Increments the failure statistic counters.
4406: # - Invokes Reply to send the error message to the client.
4407: # Parameters:
4408: # fd - File descriptor open on the client
4409: # reply - Reply text to emit.
4410: # request - The original request message (used by Reply
4411: # to debug if that's enabled.
4412: # Implicit outputs:
4413: # $Failures- The number of failures is incremented.
4414: # Reply (invoked here) sends a message to the
4415: # client:
4416: #
4417: sub Failure {
4418: my $fd = shift;
4419: my $reply = shift;
4420: my $request = shift;
4421:
4422: $Failures++;
4423: Reply($fd, $reply, $request); # That's simple eh?
1.161 foxr 4424: }
1.57 www 4425: # ------------------------------------------------------------------ Log status
4426:
4427: sub logstatus {
1.178 foxr 4428: &status("Doing logging");
4429: my $docdir=$perlvar{'lonDocRoot'};
4430: {
4431: my $fh=IO::File->new(">$docdir/lon-status/londchld/$$.txt");
1.200 matthew 4432: print $fh $status."\n".$lastlog."\n".time."\n$keymode";
1.178 foxr 4433: $fh->close();
4434: }
1.221 albertel 4435: &status("Finished $$.txt");
4436: {
4437: open(LOG,">>$docdir/lon-status/londstatus.txt");
4438: flock(LOG,LOCK_EX);
4439: print LOG $$."\t".$clientname."\t".$currenthostid."\t"
4440: .$status."\t".$lastlog."\t $keymode\n";
4441: flock(DB,LOCK_UN);
4442: close(LOG);
4443: }
1.178 foxr 4444: &status("Finished logging");
1.57 www 4445: }
4446:
4447: sub initnewstatus {
4448: my $docdir=$perlvar{'lonDocRoot'};
4449: my $fh=IO::File->new(">$docdir/lon-status/londstatus.txt");
4450: my $now=time;
4451: my $local=localtime($now);
4452: print $fh "LOND status $local - parent $$\n\n";
1.64 www 4453: opendir(DIR,"$docdir/lon-status/londchld");
1.134 albertel 4454: while (my $filename=readdir(DIR)) {
1.64 www 4455: unlink("$docdir/lon-status/londchld/$filename");
4456: }
4457: closedir(DIR);
1.57 www 4458: }
4459:
4460: # -------------------------------------------------------------- Status setting
4461:
4462: sub status {
4463: my $what=shift;
4464: my $now=time;
4465: my $local=localtime($now);
1.178 foxr 4466: $status=$local.': '.$what;
4467: $0='lond: '.$what.' '.$local;
1.57 www 4468: }
1.11 www 4469:
4470: # -------------------------------------------------------- Escape Special Chars
4471:
4472: sub escape {
4473: my $str=shift;
4474: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
4475: return $str;
4476: }
4477:
4478: # ----------------------------------------------------- Un-Escape Special Chars
4479:
4480: sub unescape {
4481: my $str=shift;
4482: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
4483: return $str;
4484: }
4485:
1.1 albertel 4486: # ----------------------------------------------------------- Send USR1 to lonc
4487:
4488: sub reconlonc {
4489: my $peerfile=shift;
4490: &logthis("Trying to reconnect for $peerfile");
4491: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
4492: if (my $fh=IO::File->new("$loncfile")) {
4493: my $loncpid=<$fh>;
4494: chomp($loncpid);
4495: if (kill 0 => $loncpid) {
4496: &logthis("lonc at pid $loncpid responding, sending USR1");
4497: kill USR1 => $loncpid;
4498: } else {
1.9 www 4499: &logthis(
1.190 albertel 4500: "<font color='red'>CRITICAL: "
1.9 www 4501: ."lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 4502: }
4503: } else {
1.190 albertel 4504: &logthis('<font color="red">CRITICAL: lonc not running, giving up</font>');
1.1 albertel 4505: }
4506: }
4507:
4508: # -------------------------------------------------- Non-critical communication
1.11 www 4509:
1.1 albertel 4510: sub subreply {
4511: my ($cmd,$server)=@_;
4512: my $peerfile="$perlvar{'lonSockDir'}/$server";
4513: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
4514: Type => SOCK_STREAM,
4515: Timeout => 10)
4516: or return "con_lost";
4517: print $sclient "$cmd\n";
4518: my $answer=<$sclient>;
4519: chomp($answer);
4520: if (!$answer) { $answer="con_lost"; }
4521: return $answer;
4522: }
4523:
4524: sub reply {
4525: my ($cmd,$server)=@_;
4526: my $answer;
1.115 albertel 4527: if ($server ne $currenthostid) {
1.1 albertel 4528: $answer=subreply($cmd,$server);
4529: if ($answer eq 'con_lost') {
4530: $answer=subreply("ping",$server);
4531: if ($answer ne $server) {
1.115 albertel 4532: &logthis("sub reply: answer != server answer is $answer, server is $server");
1.1 albertel 4533: &reconlonc("$perlvar{'lonSockDir'}/$server");
4534: }
4535: $answer=subreply($cmd,$server);
4536: }
4537: } else {
4538: $answer='self_reply';
4539: }
4540: return $answer;
4541: }
4542:
1.13 www 4543: # -------------------------------------------------------------- Talk to lonsql
4544:
1.234 foxr 4545: sub sql_reply {
1.12 harris41 4546: my ($cmd)=@_;
1.234 foxr 4547: my $answer=&sub_sql_reply($cmd);
4548: if ($answer eq 'con_lost') { $answer=&sub_sql_reply($cmd); }
1.12 harris41 4549: return $answer;
4550: }
4551:
1.234 foxr 4552: sub sub_sql_reply {
1.12 harris41 4553: my ($cmd)=@_;
4554: my $unixsock="mysqlsock";
4555: my $peerfile="$perlvar{'lonSockDir'}/$unixsock";
4556: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
4557: Type => SOCK_STREAM,
4558: Timeout => 10)
4559: or return "con_lost";
4560: print $sclient "$cmd\n";
4561: my $answer=<$sclient>;
4562: chomp($answer);
4563: if (!$answer) { $answer="con_lost"; }
4564: return $answer;
4565: }
4566:
1.1 albertel 4567: # -------------------------------------------- Return path to profile directory
1.11 www 4568:
1.1 albertel 4569: sub propath {
4570: my ($udom,$uname)=@_;
4571: $udom=~s/\W//g;
4572: $uname=~s/\W//g;
1.16 www 4573: my $subdir=$uname.'__';
1.1 albertel 4574: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
4575: my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
4576: return $proname;
4577: }
4578:
4579: # --------------------------------------- Is this the home server of an author?
1.11 www 4580:
1.1 albertel 4581: sub ishome {
4582: my $author=shift;
4583: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
4584: my ($udom,$uname)=split(/\//,$author);
4585: my $proname=propath($udom,$uname);
4586: if (-e $proname) {
4587: return 'owner';
4588: } else {
4589: return 'not_owner';
4590: }
4591: }
4592:
4593: # ======================================================= Continue main program
4594: # ---------------------------------------------------- Fork once and dissociate
4595:
1.134 albertel 4596: my $fpid=fork;
1.1 albertel 4597: exit if $fpid;
1.29 harris41 4598: die "Couldn't fork: $!" unless defined ($fpid);
1.1 albertel 4599:
1.29 harris41 4600: POSIX::setsid() or die "Can't start new session: $!";
1.1 albertel 4601:
4602: # ------------------------------------------------------- Write our PID on disk
4603:
1.134 albertel 4604: my $execdir=$perlvar{'lonDaemons'};
1.1 albertel 4605: open (PIDSAVE,">$execdir/logs/lond.pid");
4606: print PIDSAVE "$$\n";
4607: close(PIDSAVE);
1.190 albertel 4608: &logthis("<font color='red'>CRITICAL: ---------- Starting ----------</font>");
1.57 www 4609: &status('Starting');
1.1 albertel 4610:
1.106 foxr 4611:
1.1 albertel 4612:
4613: # ----------------------------------------------------- Install signal handlers
4614:
1.57 www 4615:
1.1 albertel 4616: $SIG{CHLD} = \&REAPER;
4617: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
4618: $SIG{HUP} = \&HUPSMAN;
1.57 www 4619: $SIG{USR1} = \&checkchildren;
1.144 foxr 4620: $SIG{USR2} = \&UpdateHosts;
1.106 foxr 4621:
1.148 foxr 4622: # Read the host hashes:
4623:
4624: ReadHostTable;
1.106 foxr 4625:
4626: # --------------------------------------------------------------
4627: # Accept connections. When a connection comes in, it is validated
4628: # and if good, a child process is created to process transactions
4629: # along the connection.
4630:
1.1 albertel 4631: while (1) {
1.165 albertel 4632: &status('Starting accept');
1.106 foxr 4633: $client = $server->accept() or next;
1.165 albertel 4634: &status('Accepted '.$client.' off to spawn');
1.106 foxr 4635: make_new_child($client);
1.165 albertel 4636: &status('Finished spawning');
1.1 albertel 4637: }
4638:
1.212 foxr 4639: sub make_new_child {
4640: my $pid;
4641: # my $cipher; # Now global
4642: my $sigset;
1.178 foxr 4643:
1.212 foxr 4644: $client = shift;
4645: &status('Starting new child '.$client);
4646: &logthis('<font color="green"> Attempting to start child ('.$client.
4647: ")</font>");
4648: # block signal for fork
4649: $sigset = POSIX::SigSet->new(SIGINT);
4650: sigprocmask(SIG_BLOCK, $sigset)
4651: or die "Can't block SIGINT for fork: $!\n";
1.178 foxr 4652:
1.212 foxr 4653: die "fork: $!" unless defined ($pid = fork);
1.178 foxr 4654:
1.212 foxr 4655: $client->sockopt(SO_KEEPALIVE, 1); # Enable monitoring of
4656: # connection liveness.
1.178 foxr 4657:
1.212 foxr 4658: #
4659: # Figure out who we're talking to so we can record the peer in
4660: # the pid hash.
4661: #
4662: my $caller = getpeername($client);
4663: my ($port,$iaddr);
4664: if (defined($caller) && length($caller) > 0) {
4665: ($port,$iaddr)=unpack_sockaddr_in($caller);
4666: } else {
4667: &logthis("Unable to determine who caller was, getpeername returned nothing");
4668: }
4669: if (defined($iaddr)) {
4670: $clientip = inet_ntoa($iaddr);
4671: Debug("Connected with $clientip");
4672: $clientdns = gethostbyaddr($iaddr, AF_INET);
4673: Debug("Connected with $clientdns by name");
4674: } else {
4675: &logthis("Unable to determine clientip");
4676: $clientip='Unavailable';
4677: }
4678:
4679: if ($pid) {
4680: # Parent records the child's birth and returns.
4681: sigprocmask(SIG_UNBLOCK, $sigset)
4682: or die "Can't unblock SIGINT for fork: $!\n";
4683: $children{$pid} = $clientip;
4684: &status('Started child '.$pid);
4685: return;
4686: } else {
4687: # Child can *not* return from this subroutine.
4688: $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before
4689: $SIG{CHLD} = 'DEFAULT'; #make this default so that pwauth returns
4690: #don't get intercepted
4691: $SIG{USR1}= \&logstatus;
4692: $SIG{ALRM}= \&timeout;
4693: $lastlog='Forked ';
4694: $status='Forked';
1.178 foxr 4695:
1.212 foxr 4696: # unblock signals
4697: sigprocmask(SIG_UNBLOCK, $sigset)
4698: or die "Can't unblock SIGINT for fork: $!\n";
1.178 foxr 4699:
1.212 foxr 4700: # my $tmpsnum=0; # Now global
4701: #---------------------------------------------------- kerberos 5 initialization
4702: &Authen::Krb5::init_context();
4703: &Authen::Krb5::init_ets();
1.209 albertel 4704:
1.212 foxr 4705: &status('Accepted connection');
4706: # =============================================================================
4707: # do something with the connection
4708: # -----------------------------------------------------------------------------
4709: # see if we know client and 'check' for spoof IP by ineffective challenge
1.178 foxr 4710:
1.212 foxr 4711: ReadManagerTable; # May also be a manager!!
4712:
4713: my $clientrec=($hostid{$clientip} ne undef);
4714: my $ismanager=($managers{$clientip} ne undef);
4715: $clientname = "[unknonwn]";
4716: if($clientrec) { # Establish client type.
4717: $ConnectionType = "client";
4718: $clientname = $hostid{$clientip};
4719: if($ismanager) {
4720: $ConnectionType = "both";
4721: }
4722: } else {
4723: $ConnectionType = "manager";
4724: $clientname = $managers{$clientip};
4725: }
4726: my $clientok;
1.178 foxr 4727:
1.212 foxr 4728: if ($clientrec || $ismanager) {
4729: &status("Waiting for init from $clientip $clientname");
4730: &logthis('<font color="yellow">INFO: Connection, '.
4731: $clientip.
4732: " ($clientname) connection type = $ConnectionType </font>" );
4733: &status("Connecting $clientip ($clientname))");
4734: my $remotereq=<$client>;
4735: chomp($remotereq);
4736: Debug("Got init: $remotereq");
4737: my $inikeyword = split(/:/, $remotereq);
4738: if ($remotereq =~ /^init/) {
4739: &sethost("sethost:$perlvar{'lonHostID'}");
4740: #
4741: # If the remote is attempting a local init... give that a try:
4742: #
4743: my ($i, $inittype) = split(/:/, $remotereq);
1.209 albertel 4744:
1.212 foxr 4745: # If the connection type is ssl, but I didn't get my
4746: # certificate files yet, then I'll drop back to
4747: # insecure (if allowed).
4748:
4749: if($inittype eq "ssl") {
4750: my ($ca, $cert) = lonssl::CertificateFile;
4751: my $kfile = lonssl::KeyFile;
4752: if((!$ca) ||
4753: (!$cert) ||
4754: (!$kfile)) {
4755: $inittype = ""; # This forces insecure attempt.
4756: &logthis("<font color=\"blue\"> Certificates not "
4757: ."installed -- trying insecure auth</font>");
1.224 foxr 4758: } else { # SSL certificates are in place so
1.212 foxr 4759: } # Leave the inittype alone.
4760: }
4761:
4762: if($inittype eq "local") {
4763: my $key = LocalConnection($client, $remotereq);
4764: if($key) {
4765: Debug("Got local key $key");
4766: $clientok = 1;
4767: my $cipherkey = pack("H32", $key);
4768: $cipher = new IDEA($cipherkey);
4769: print $client "ok:local\n";
4770: &logthis('<font color="green"'
4771: . "Successful local authentication </font>");
4772: $keymode = "local"
1.178 foxr 4773: } else {
1.212 foxr 4774: Debug("Failed to get local key");
4775: $clientok = 0;
4776: shutdown($client, 3);
4777: close $client;
1.178 foxr 4778: }
1.212 foxr 4779: } elsif ($inittype eq "ssl") {
4780: my $key = SSLConnection($client);
4781: if ($key) {
4782: $clientok = 1;
4783: my $cipherkey = pack("H32", $key);
4784: $cipher = new IDEA($cipherkey);
4785: &logthis('<font color="green">'
4786: ."Successfull ssl authentication with $clientname </font>");
4787: $keymode = "ssl";
4788:
1.178 foxr 4789: } else {
1.212 foxr 4790: $clientok = 0;
4791: close $client;
1.178 foxr 4792: }
1.212 foxr 4793:
4794: } else {
4795: my $ok = InsecureConnection($client);
4796: if($ok) {
4797: $clientok = 1;
4798: &logthis('<font color="green">'
4799: ."Successful insecure authentication with $clientname </font>");
4800: print $client "ok\n";
4801: $keymode = "insecure";
1.178 foxr 4802: } else {
1.212 foxr 4803: &logthis('<font color="yellow">'
4804: ."Attempted insecure connection disallowed </font>");
4805: close $client;
4806: $clientok = 0;
1.178 foxr 4807:
4808: }
4809: }
1.212 foxr 4810: } else {
4811: &logthis(
4812: "<font color='blue'>WARNING: "
4813: ."$clientip failed to initialize: >$remotereq< </font>");
4814: &status('No init '.$clientip);
4815: }
4816:
4817: } else {
4818: &logthis(
4819: "<font color='blue'>WARNING: Unknown client $clientip</font>");
4820: &status('Hung up on '.$clientip);
4821: }
4822:
4823: if ($clientok) {
4824: # ---------------- New known client connecting, could mean machine online again
4825:
4826: foreach my $id (keys(%hostip)) {
4827: if ($hostip{$id} ne $clientip ||
4828: $hostip{$currenthostid} eq $clientip) {
4829: # no need to try to do recon's to myself
4830: next;
4831: }
4832: &reconlonc("$perlvar{'lonSockDir'}/$id");
4833: }
4834: &logthis("<font color='green'>Established connection: $clientname</font>");
4835: &status('Will listen to '.$clientname);
4836: # ------------------------------------------------------------ Process requests
4837: my $keep_going = 1;
4838: my $user_input;
4839: while(($user_input = get_request) && $keep_going) {
4840: alarm(120);
4841: Debug("Main: Got $user_input\n");
4842: $keep_going = &process_request($user_input);
1.178 foxr 4843: alarm(0);
1.212 foxr 4844: &status('Listening to '.$clientname." ($keymode)");
1.161 foxr 4845: }
1.212 foxr 4846:
1.59 www 4847: # --------------------------------------------- client unknown or fishy, refuse
1.212 foxr 4848: } else {
1.161 foxr 4849: print $client "refused\n";
4850: $client->close();
1.190 albertel 4851: &logthis("<font color='blue'>WARNING: "
1.161 foxr 4852: ."Rejected client $clientip, closing connection</font>");
4853: }
1.212 foxr 4854: }
1.161 foxr 4855:
1.1 albertel 4856: # =============================================================================
1.161 foxr 4857:
1.190 albertel 4858: &logthis("<font color='red'>CRITICAL: "
1.161 foxr 4859: ."Disconnect from $clientip ($clientname)</font>");
4860:
4861:
4862: # this exit is VERY important, otherwise the child will become
4863: # a producer of more and more children, forking yourself into
4864: # process death.
4865: exit;
1.106 foxr 4866:
1.78 foxr 4867: }
1.261 foxr 4868: #
4869: # Determine if a user is an author for the indicated domain.
4870: #
4871: # Parameters:
4872: # domain - domain to check in .
4873: # user - Name of user to check.
4874: #
4875: # Return:
4876: # 1 - User is an author for domain.
4877: # 0 - User is not an author for domain.
4878: sub is_author {
4879: my ($domain, $user) = @_;
4880:
4881: &Debug("is_author: $user @ $domain");
4882:
4883: my $hashref = &tie_user_hash($domain, $user, "roles",
4884: &GDBM_READER());
4885:
4886: # Author role should show up as a key /domain/_au
1.78 foxr 4887:
1.261 foxr 4888: my $key = "/$domain/_au";
4889: my $value = $hashref->{$key};
1.78 foxr 4890:
1.261 foxr 4891: if(defined($value)) {
4892: &Debug("$user @ $domain is an author");
4893: }
4894:
4895: return defined($value);
4896: }
1.78 foxr 4897: #
4898: # Checks to see if the input roleput request was to set
4899: # an author role. If so, invokes the lchtmldir script to set
4900: # up a correct public_html
4901: # Parameters:
4902: # request - The request sent to the rolesput subchunk.
4903: # We're looking for /domain/_au
4904: # domain - The domain in which the user is having roles doctored.
4905: # user - Name of the user for which the role is being put.
4906: # authtype - The authentication type associated with the user.
4907: #
1.230 foxr 4908: sub manage_permissions
1.78 foxr 4909: {
1.192 foxr 4910:
1.261 foxr 4911:
1.192 foxr 4912: my ($request, $domain, $user, $authtype) = @_;
1.78 foxr 4913:
1.261 foxr 4914: &Debug("manage_permissions: $request $domain $user $authtype");
4915:
1.78 foxr 4916: # See if the request is of the form /$domain/_au
4917: if($request =~ /^(\/$domain\/_au)$/) { # It's an author rolesput...
4918: my $execdir = $perlvar{'lonDaemons'};
4919: my $userhome= "/home/$user" ;
1.134 albertel 4920: &logthis("system $execdir/lchtmldir $userhome $user $authtype");
1.261 foxr 4921: &Debug("Setting homedir permissions for $userhome");
1.78 foxr 4922: system("$execdir/lchtmldir $userhome $user $authtype");
4923: }
4924: }
1.222 foxr 4925:
4926:
4927: #
4928: # Return the full path of a user password file, whether it exists or not.
4929: # Parameters:
4930: # domain - Domain in which the password file lives.
4931: # user - name of the user.
4932: # Returns:
4933: # Full passwd path:
4934: #
4935: sub password_path {
4936: my ($domain, $user) = @_;
1.264 albertel 4937: return &propath($domain, $user).'/passwd';
1.222 foxr 4938: }
4939:
4940: # Password Filename
4941: # Returns the path to a passwd file given domain and user... only if
4942: # it exists.
4943: # Parameters:
4944: # domain - Domain in which to search.
4945: # user - username.
4946: # Returns:
4947: # - If the password file exists returns its path.
4948: # - If the password file does not exist, returns undefined.
4949: #
4950: sub password_filename {
4951: my ($domain, $user) = @_;
4952:
4953: Debug ("PasswordFilename called: dom = $domain user = $user");
4954:
4955: my $path = &password_path($domain, $user);
4956: Debug("PasswordFilename got path: $path");
4957: if(-e $path) {
4958: return $path;
4959: } else {
4960: return undef;
4961: }
4962: }
4963:
4964: #
4965: # Rewrite the contents of the user's passwd file.
4966: # Parameters:
4967: # domain - domain of the user.
4968: # name - User's name.
4969: # contents - New contents of the file.
4970: # Returns:
4971: # 0 - Failed.
4972: # 1 - Success.
4973: #
4974: sub rewrite_password_file {
4975: my ($domain, $user, $contents) = @_;
4976:
4977: my $file = &password_filename($domain, $user);
4978: if (defined $file) {
4979: my $pf = IO::File->new(">$file");
4980: if($pf) {
4981: print $pf "$contents\n";
4982: return 1;
4983: } else {
4984: return 0;
4985: }
4986: } else {
4987: return 0;
4988: }
4989:
4990: }
4991:
1.78 foxr 4992: #
1.222 foxr 4993: # get_auth_type - Determines the authorization type of a user in a domain.
1.78 foxr 4994:
4995: # Returns the authorization type or nouser if there is no such user.
4996: #
1.222 foxr 4997: sub get_auth_type
1.78 foxr 4998: {
1.192 foxr 4999:
5000: my ($domain, $user) = @_;
1.78 foxr 5001:
1.222 foxr 5002: Debug("get_auth_type( $domain, $user ) \n");
1.78 foxr 5003: my $proname = &propath($domain, $user);
5004: my $passwdfile = "$proname/passwd";
5005: if( -e $passwdfile ) {
5006: my $pf = IO::File->new($passwdfile);
5007: my $realpassword = <$pf>;
5008: chomp($realpassword);
1.79 foxr 5009: Debug("Password info = $realpassword\n");
1.78 foxr 5010: my ($authtype, $contentpwd) = split(/:/, $realpassword);
1.79 foxr 5011: Debug("Authtype = $authtype, content = $contentpwd\n");
1.259 raeburn 5012: return "$authtype:$contentpwd";
1.224 foxr 5013: } else {
1.79 foxr 5014: Debug("Returning nouser");
1.78 foxr 5015: return "nouser";
5016: }
1.1 albertel 5017: }
5018:
1.220 foxr 5019: #
5020: # Validate a user given their domain, name and password. This utility
5021: # function is used by both AuthenticateHandler and ChangePasswordHandler
5022: # to validate the login credentials of a user.
5023: # Parameters:
5024: # $domain - The domain being logged into (this is required due to
5025: # the capability for multihomed systems.
5026: # $user - The name of the user being validated.
5027: # $password - The user's propoposed password.
5028: #
5029: # Returns:
5030: # 1 - The domain,user,pasword triplet corresponds to a valid
5031: # user.
5032: # 0 - The domain,user,password triplet is not a valid user.
5033: #
5034: sub validate_user {
5035: my ($domain, $user, $password) = @_;
5036:
5037:
5038: # Why negative ~pi you may well ask? Well this function is about
5039: # authentication, and therefore very important to get right.
5040: # I've initialized the flag that determines whether or not I've
5041: # validated correctly to a value it's not supposed to get.
5042: # At the end of this function. I'll ensure that it's not still that
5043: # value so we don't just wind up returning some accidental value
5044: # as a result of executing an unforseen code path that
1.249 foxr 5045: # did not set $validated. At the end of valid execution paths,
5046: # validated shoule be 1 for success or 0 for failuer.
1.220 foxr 5047:
5048: my $validated = -3.14159;
5049:
5050: # How we authenticate is determined by the type of authentication
5051: # the user has been assigned. If the authentication type is
5052: # "nouser", the user does not exist so we will return 0.
5053:
1.222 foxr 5054: my $contents = &get_auth_type($domain, $user);
1.220 foxr 5055: my ($howpwd, $contentpwd) = split(/:/, $contents);
5056:
5057: my $null = pack("C",0); # Used by kerberos auth types.
5058:
5059: if ($howpwd ne 'nouser') {
5060:
5061: if($howpwd eq "internal") { # Encrypted is in local password file.
5062: $validated = (crypt($password, $contentpwd) eq $contentpwd);
5063: }
5064: elsif ($howpwd eq "unix") { # User is a normal unix user.
5065: $contentpwd = (getpwnam($user))[1];
5066: if($contentpwd) {
5067: if($contentpwd eq 'x') { # Shadow password file...
5068: my $pwauth_path = "/usr/local/sbin/pwauth";
5069: open PWAUTH, "|$pwauth_path" or
5070: die "Cannot invoke authentication";
5071: print PWAUTH "$user\n$password\n";
5072: close PWAUTH;
5073: $validated = ! $?;
5074:
5075: } else { # Passwords in /etc/passwd.
5076: $validated = (crypt($password,
5077: $contentpwd) eq $contentpwd);
5078: }
5079: } else {
5080: $validated = 0;
5081: }
5082: }
5083: elsif ($howpwd eq "krb4") { # user is in kerberos 4 auth. domain.
5084: if(! ($password =~ /$null/) ) {
5085: my $k4error = &Authen::Krb4::get_pw_in_tkt($user,
5086: "",
5087: $contentpwd,,
5088: 'krbtgt',
5089: $contentpwd,
5090: 1,
5091: $password);
5092: if(!$k4error) {
5093: $validated = 1;
1.224 foxr 5094: } else {
1.220 foxr 5095: $validated = 0;
5096: &logthis('krb4: '.$user.', '.$contentpwd.', '.
5097: &Authen::Krb4::get_err_txt($Authen::Krb4::error));
5098: }
1.224 foxr 5099: } else {
1.220 foxr 5100: $validated = 0; # Password has a match with null.
5101: }
1.224 foxr 5102: } elsif ($howpwd eq "krb5") { # User is in kerberos 5 auth. domain.
1.220 foxr 5103: if(!($password =~ /$null/)) { # Null password not allowed.
5104: my $krbclient = &Authen::Krb5::parse_name($user.'@'
5105: .$contentpwd);
5106: my $krbservice = "krbtgt/".$contentpwd."\@".$contentpwd;
5107: my $krbserver = &Authen::Krb5::parse_name($krbservice);
5108: my $credentials= &Authen::Krb5::cc_default();
5109: $credentials->initialize($krbclient);
1.270 matthew 5110: my $krbreturn = &Authen::Krb5::get_in_tkt_with_password($krbclient,
1.220 foxr 5111: $krbserver,
5112: $password,
5113: $credentials);
5114: $validated = ($krbreturn == 1);
1.224 foxr 5115: } else {
1.220 foxr 5116: $validated = 0;
5117: }
1.224 foxr 5118: } elsif ($howpwd eq "localauth") {
1.220 foxr 5119: # Authenticate via installation specific authentcation method:
5120: $validated = &localauth::localauth($user,
5121: $password,
5122: $contentpwd);
1.224 foxr 5123: } else { # Unrecognized auth is also bad.
1.220 foxr 5124: $validated = 0;
5125: }
5126: } else {
5127: $validated = 0;
5128: }
5129: #
5130: # $validated has the correct stat of the authentication:
5131: #
5132:
5133: unless ($validated != -3.14159) {
1.249 foxr 5134: # I >really really< want to know if this happens.
5135: # since it indicates that user authentication is badly
5136: # broken in some code path.
5137: #
5138: die "ValidateUser - failed to set the value of validated $domain, $user $password";
1.220 foxr 5139: }
5140: return $validated;
5141: }
5142:
5143:
1.84 albertel 5144: sub addline {
5145: my ($fname,$hostid,$ip,$newline)=@_;
5146: my $contents;
5147: my $found=0;
5148: my $expr='^'.$hostid.':'.$ip.':';
5149: $expr =~ s/\./\\\./g;
1.134 albertel 5150: my $sh;
1.84 albertel 5151: if ($sh=IO::File->new("$fname.subscription")) {
5152: while (my $subline=<$sh>) {
5153: if ($subline !~ /$expr/) {$contents.= $subline;} else {$found=1;}
5154: }
5155: $sh->close();
5156: }
5157: $sh=IO::File->new(">$fname.subscription");
5158: if ($contents) { print $sh $contents; }
5159: if ($newline) { print $sh $newline; }
5160: $sh->close();
5161: return $found;
1.86 www 5162: }
5163:
1.234 foxr 5164: sub get_chat {
1.122 www 5165: my ($cdom,$cname,$udom,$uname)=@_;
1.87 www 5166: my %hash;
5167: my $proname=&propath($cdom,$cname);
5168: my @entries=();
1.88 albertel 5169: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
5170: &GDBM_READER(),0640)) {
5171: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
5172: untie %hash;
1.123 www 5173: }
1.124 www 5174: my @participants=();
1.134 albertel 5175: my $cutoff=time-60;
1.123 www 5176: if (tie(%hash,'GDBM_File',"$proname/nohist_inchatroom.db",
1.124 www 5177: &GDBM_WRCREAT(),0640)) {
5178: $hash{$uname.':'.$udom}=time;
1.123 www 5179: foreach (sort keys %hash) {
5180: if ($hash{$_}>$cutoff) {
1.124 www 5181: $participants[$#participants+1]='active_participant:'.$_;
1.123 www 5182: }
5183: }
5184: untie %hash;
1.86 www 5185: }
1.124 www 5186: return (@participants,@entries);
1.86 www 5187: }
5188:
1.234 foxr 5189: sub chat_add {
1.88 albertel 5190: my ($cdom,$cname,$newchat)=@_;
5191: my %hash;
5192: my $proname=&propath($cdom,$cname);
5193: my @entries=();
1.142 www 5194: my $time=time;
1.88 albertel 5195: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
5196: &GDBM_WRCREAT(),0640)) {
5197: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
5198: my ($lastid)=($entries[$#entries]=~/^(\w+)\:/);
5199: my ($thentime,$idnum)=split(/\_/,$lastid);
5200: my $newid=$time.'_000000';
5201: if ($thentime==$time) {
5202: $idnum=~s/^0+//;
5203: $idnum++;
5204: $idnum=substr('000000'.$idnum,-6,6);
5205: $newid=$time.'_'.$idnum;
5206: }
5207: $hash{$newid}=$newchat;
5208: my $expired=$time-3600;
5209: foreach (keys %hash) {
5210: my ($thistime)=($_=~/(\d+)\_/);
5211: if ($thistime<$expired) {
1.89 www 5212: delete $hash{$_};
1.88 albertel 5213: }
5214: }
5215: untie %hash;
1.142 www 5216: }
5217: {
5218: my $hfh;
5219: if ($hfh=IO::File->new(">>$proname/chatroom.log")) {
5220: print $hfh "$time:".&unescape($newchat)."\n";
5221: }
1.86 www 5222: }
1.84 albertel 5223: }
5224:
5225: sub unsub {
5226: my ($fname,$clientip)=@_;
5227: my $result;
1.188 foxr 5228: my $unsubs = 0; # Number of successful unsubscribes:
5229:
5230:
5231: # An old way subscriptions were handled was to have a
5232: # subscription marker file:
5233:
5234: Debug("Attempting unlink of $fname.$clientname");
1.161 foxr 5235: if (unlink("$fname.$clientname")) {
1.188 foxr 5236: $unsubs++; # Successful unsub via marker file.
5237: }
5238:
5239: # The more modern way to do it is to have a subscription list
5240: # file:
5241:
1.84 albertel 5242: if (-e "$fname.subscription") {
1.161 foxr 5243: my $found=&addline($fname,$clientname,$clientip,'');
1.188 foxr 5244: if ($found) {
5245: $unsubs++;
5246: }
5247: }
5248:
5249: # If either or both of these mechanisms succeeded in unsubscribing a
5250: # resource we can return ok:
5251:
5252: if($unsubs) {
5253: $result = "ok\n";
1.84 albertel 5254: } else {
1.188 foxr 5255: $result = "not_subscribed\n";
1.84 albertel 5256: }
1.188 foxr 5257:
1.84 albertel 5258: return $result;
5259: }
5260:
1.101 www 5261: sub currentversion {
5262: my $fname=shift;
5263: my $version=-1;
5264: my $ulsdir='';
5265: if ($fname=~/^(.+)\/[^\/]+$/) {
5266: $ulsdir=$1;
5267: }
1.114 albertel 5268: my ($fnamere1,$fnamere2);
5269: # remove version if already specified
1.101 www 5270: $fname=~s/\.\d+\.(\w+(?:\.meta)*)$/\.$1/;
1.114 albertel 5271: # get the bits that go before and after the version number
5272: if ( $fname=~/^(.*\.)(\w+(?:\.meta)*)$/ ) {
5273: $fnamere1=$1;
5274: $fnamere2='.'.$2;
5275: }
1.101 www 5276: if (-e $fname) { $version=1; }
5277: if (-e $ulsdir) {
1.134 albertel 5278: if(-d $ulsdir) {
5279: if (opendir(LSDIR,$ulsdir)) {
5280: my $ulsfn;
5281: while ($ulsfn=readdir(LSDIR)) {
1.101 www 5282: # see if this is a regular file (ignore links produced earlier)
1.134 albertel 5283: my $thisfile=$ulsdir.'/'.$ulsfn;
5284: unless (-l $thisfile) {
1.160 www 5285: if ($thisfile=~/\Q$fnamere1\E(\d+)\Q$fnamere2\E$/) {
1.134 albertel 5286: if ($1>$version) { $version=$1; }
5287: }
5288: }
5289: }
5290: closedir(LSDIR);
5291: $version++;
5292: }
5293: }
5294: }
5295: return $version;
1.101 www 5296: }
5297:
5298: sub thisversion {
5299: my $fname=shift;
5300: my $version=-1;
5301: if ($fname=~/\.(\d+)\.\w+(?:\.meta)*$/) {
5302: $version=$1;
5303: }
5304: return $version;
5305: }
5306:
1.84 albertel 5307: sub subscribe {
5308: my ($userinput,$clientip)=@_;
5309: my $result;
5310: my ($cmd,$fname)=split(/:/,$userinput);
5311: my $ownership=&ishome($fname);
5312: if ($ownership eq 'owner') {
1.101 www 5313: # explitly asking for the current version?
5314: unless (-e $fname) {
5315: my $currentversion=¤tversion($fname);
5316: if (&thisversion($fname)==$currentversion) {
5317: if ($fname=~/^(.+)\.\d+\.(\w+(?:\.meta)*)$/) {
5318: my $root=$1;
5319: my $extension=$2;
5320: symlink($root.'.'.$extension,
5321: $root.'.'.$currentversion.'.'.$extension);
1.102 www 5322: unless ($extension=~/\.meta$/) {
5323: symlink($root.'.'.$extension.'.meta',
5324: $root.'.'.$currentversion.'.'.$extension.'.meta');
5325: }
1.101 www 5326: }
5327: }
5328: }
1.84 albertel 5329: if (-e $fname) {
5330: if (-d $fname) {
5331: $result="directory\n";
5332: } else {
1.161 foxr 5333: if (-e "$fname.$clientname") {&unsub($fname,$clientip);}
1.134 albertel 5334: my $now=time;
1.161 foxr 5335: my $found=&addline($fname,$clientname,$clientip,
5336: "$clientname:$clientip:$now\n");
1.84 albertel 5337: if ($found) { $result="$fname\n"; }
5338: # if they were subscribed to only meta data, delete that
5339: # subscription, when you subscribe to a file you also get
5340: # the metadata
5341: unless ($fname=~/\.meta$/) { &unsub("$fname.meta",$clientip); }
5342: $fname=~s/\/home\/httpd\/html\/res/raw/;
5343: $fname="http://$thisserver/".$fname;
5344: $result="$fname\n";
5345: }
5346: } else {
5347: $result="not_found\n";
5348: }
5349: } else {
5350: $result="rejected\n";
5351: }
5352: return $result;
5353: }
1.91 albertel 5354:
5355: sub make_passwd_file {
1.98 foxr 5356: my ($uname, $umode,$npass,$passfilename)=@_;
1.91 albertel 5357: my $result="ok\n";
5358: if ($umode eq 'krb4' or $umode eq 'krb5') {
5359: {
5360: my $pf = IO::File->new(">$passfilename");
1.261 foxr 5361: if ($pf) {
5362: print $pf "$umode:$npass\n";
5363: } else {
5364: $result = "pass_file_failed_error";
5365: }
1.91 albertel 5366: }
5367: } elsif ($umode eq 'internal') {
5368: my $salt=time;
5369: $salt=substr($salt,6,2);
5370: my $ncpass=crypt($npass,$salt);
5371: {
5372: &Debug("Creating internal auth");
5373: my $pf = IO::File->new(">$passfilename");
1.261 foxr 5374: if($pf) {
5375: print $pf "internal:$ncpass\n";
5376: } else {
5377: $result = "pass_file_failed_error";
5378: }
1.91 albertel 5379: }
5380: } elsif ($umode eq 'localauth') {
5381: {
5382: my $pf = IO::File->new(">$passfilename");
1.261 foxr 5383: if($pf) {
5384: print $pf "localauth:$npass\n";
5385: } else {
5386: $result = "pass_file_failed_error";
5387: }
1.91 albertel 5388: }
5389: } elsif ($umode eq 'unix') {
5390: {
1.186 foxr 5391: #
5392: # Don't allow the creation of privileged accounts!!! that would
5393: # be real bad!!!
5394: #
5395: my $uid = getpwnam($uname);
5396: if((defined $uid) && ($uid == 0)) {
5397: &logthis(">>>Attempted to create privilged account blocked");
5398: return "no_priv_account_error\n";
5399: }
5400:
1.223 foxr 5401: my $execpath ="$perlvar{'lonDaemons'}/"."lcuseradd";
1.224 foxr 5402:
5403: my $lc_error_file = $execdir."/tmp/lcuseradd".$$.".status";
1.91 albertel 5404: {
5405: &Debug("Executing external: ".$execpath);
1.98 foxr 5406: &Debug("user = ".$uname.", Password =". $npass);
1.132 matthew 5407: my $se = IO::File->new("|$execpath > $perlvar{'lonDaemons'}/logs/lcuseradd.log");
1.91 albertel 5408: print $se "$uname\n";
5409: print $se "$npass\n";
5410: print $se "$npass\n";
1.223 foxr 5411: print $se "$lc_error_file\n"; # Status -> unique file.
1.97 foxr 5412: }
1.223 foxr 5413: my $error = IO::File->new("< $lc_error_file");
5414: my $useraddok = <$error>;
5415: $error->close;
5416: unlink($lc_error_file);
5417:
5418: chomp $useraddok;
5419:
1.97 foxr 5420: if($useraddok > 0) {
1.223 foxr 5421: my $error_text = &lcuseraddstrerror($useraddok);
5422: &logthis("Failed lcuseradd: $error_text");
5423: $result = "lcuseradd_failed:$error_text\n";
1.224 foxr 5424: } else {
1.223 foxr 5425: my $pf = IO::File->new(">$passfilename");
1.261 foxr 5426: if($pf) {
5427: print $pf "unix:\n";
5428: } else {
5429: $result = "pass_file_failed_error";
5430: }
1.91 albertel 5431: }
5432: }
5433: } elsif ($umode eq 'none') {
5434: {
1.223 foxr 5435: my $pf = IO::File->new("> $passfilename");
1.261 foxr 5436: if($pf) {
5437: print $pf "none:\n";
5438: } else {
5439: $result = "pass_file_failed_error";
5440: }
1.91 albertel 5441: }
5442: } else {
5443: $result="auth_mode_error\n";
5444: }
5445: return $result;
1.121 albertel 5446: }
5447:
1.265 albertel 5448: sub convert_photo {
5449: my ($start,$dest)=@_;
5450: system("convert $start $dest");
5451: }
5452:
1.121 albertel 5453: sub sethost {
5454: my ($remotereq) = @_;
5455: my (undef,$hostid)=split(/:/,$remotereq);
5456: if (!defined($hostid)) { $hostid=$perlvar{'lonHostID'}; }
5457: if ($hostip{$perlvar{'lonHostID'}} eq $hostip{$hostid}) {
1.200 matthew 5458: $currenthostid =$hostid;
1.121 albertel 5459: $currentdomainid=$hostdom{$hostid};
5460: &logthis("Setting hostid to $hostid, and domain to $currentdomainid");
5461: } else {
5462: &logthis("Requested host id $hostid not an alias of ".
5463: $perlvar{'lonHostID'}." refusing connection");
5464: return 'unable_to_set';
5465: }
5466: return 'ok';
5467: }
5468:
5469: sub version {
5470: my ($userinput)=@_;
5471: $remoteVERSION=(split(/:/,$userinput))[1];
5472: return "version:$VERSION";
1.127 albertel 5473: }
1.178 foxr 5474:
1.128 albertel 5475: #There is a copy of this in lonnet.pm
1.127 albertel 5476: sub userload {
5477: my $numusers=0;
5478: {
5479: opendir(LONIDS,$perlvar{'lonIDsDir'});
5480: my $filename;
5481: my $curtime=time;
5482: while ($filename=readdir(LONIDS)) {
5483: if ($filename eq '.' || $filename eq '..') {next;}
1.138 albertel 5484: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.159 albertel 5485: if ($curtime-$mtime < 1800) { $numusers++; }
1.127 albertel 5486: }
5487: closedir(LONIDS);
5488: }
5489: my $userloadpercent=0;
5490: my $maxuserload=$perlvar{'lonUserLoadLim'};
5491: if ($maxuserload) {
1.129 albertel 5492: $userloadpercent=100*$numusers/$maxuserload;
1.127 albertel 5493: }
1.130 albertel 5494: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.127 albertel 5495: return $userloadpercent;
1.91 albertel 5496: }
5497:
1.205 raeburn 5498: # Routines for serializing arrays and hashes (copies from lonnet)
5499:
5500: sub array2str {
5501: my (@array) = @_;
5502: my $result=&arrayref2str(\@array);
5503: $result=~s/^__ARRAY_REF__//;
5504: $result=~s/__END_ARRAY_REF__$//;
5505: return $result;
5506: }
5507:
5508: sub arrayref2str {
5509: my ($arrayref) = @_;
5510: my $result='__ARRAY_REF__';
5511: foreach my $elem (@$arrayref) {
5512: if(ref($elem) eq 'ARRAY') {
5513: $result.=&arrayref2str($elem).'&';
5514: } elsif(ref($elem) eq 'HASH') {
5515: $result.=&hashref2str($elem).'&';
5516: } elsif(ref($elem)) {
5517: #print("Got a ref of ".(ref($elem))." skipping.");
5518: } else {
5519: $result.=&escape($elem).'&';
5520: }
5521: }
5522: $result=~s/\&$//;
5523: $result .= '__END_ARRAY_REF__';
5524: return $result;
5525: }
5526:
5527: sub hash2str {
5528: my (%hash) = @_;
5529: my $result=&hashref2str(\%hash);
5530: $result=~s/^__HASH_REF__//;
5531: $result=~s/__END_HASH_REF__$//;
5532: return $result;
5533: }
5534:
5535: sub hashref2str {
5536: my ($hashref)=@_;
5537: my $result='__HASH_REF__';
5538: foreach (sort(keys(%$hashref))) {
5539: if (ref($_) eq 'ARRAY') {
5540: $result.=&arrayref2str($_).'=';
5541: } elsif (ref($_) eq 'HASH') {
5542: $result.=&hashref2str($_).'=';
5543: } elsif (ref($_)) {
5544: $result.='=';
5545: #print("Got a ref of ".(ref($_))." skipping.");
5546: } else {
5547: if ($_) {$result.=&escape($_).'=';} else { last; }
5548: }
5549:
5550: if(ref($hashref->{$_}) eq 'ARRAY') {
5551: $result.=&arrayref2str($hashref->{$_}).'&';
5552: } elsif(ref($hashref->{$_}) eq 'HASH') {
5553: $result.=&hashref2str($hashref->{$_}).'&';
5554: } elsif(ref($hashref->{$_})) {
5555: $result.='&';
5556: #print("Got a ref of ".(ref($hashref->{$_}))." skipping.");
5557: } else {
5558: $result.=&escape($hashref->{$_}).'&';
5559: }
5560: }
5561: $result=~s/\&$//;
5562: $result .= '__END_HASH_REF__';
5563: return $result;
5564: }
1.200 matthew 5565:
1.61 harris41 5566: # ----------------------------------- POD (plain old documentation, CPAN style)
5567:
5568: =head1 NAME
5569:
5570: lond - "LON Daemon" Server (port "LOND" 5663)
5571:
5572: =head1 SYNOPSIS
5573:
1.74 harris41 5574: Usage: B<lond>
5575:
5576: Should only be run as user=www. This is a command-line script which
5577: is invoked by B<loncron>. There is no expectation that a typical user
5578: will manually start B<lond> from the command-line. (In other words,
5579: DO NOT START B<lond> YOURSELF.)
1.61 harris41 5580:
5581: =head1 DESCRIPTION
5582:
1.74 harris41 5583: There are two characteristics associated with the running of B<lond>,
5584: PROCESS MANAGEMENT (starting, stopping, handling child processes)
5585: and SERVER-SIDE ACTIVITIES (password authentication, user creation,
5586: subscriptions, etc). These are described in two large
5587: sections below.
5588:
5589: B<PROCESS MANAGEMENT>
5590:
1.61 harris41 5591: Preforker - server who forks first. Runs as a daemon. HUPs.
5592: Uses IDEA encryption
5593:
1.74 harris41 5594: B<lond> forks off children processes that correspond to the other servers
5595: in the network. Management of these processes can be done at the
5596: parent process level or the child process level.
5597:
5598: B<logs/lond.log> is the location of log messages.
5599:
5600: The process management is now explained in terms of linux shell commands,
5601: subroutines internal to this code, and signal assignments:
5602:
5603: =over 4
5604:
5605: =item *
5606:
5607: PID is stored in B<logs/lond.pid>
5608:
5609: This is the process id number of the parent B<lond> process.
5610:
5611: =item *
5612:
5613: SIGTERM and SIGINT
5614:
5615: Parent signal assignment:
5616: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
5617:
5618: Child signal assignment:
5619: $SIG{INT} = 'DEFAULT'; (and SIGTERM is DEFAULT also)
5620: (The child dies and a SIGALRM is sent to parent, awaking parent from slumber
5621: to restart a new child.)
5622:
5623: Command-line invocations:
5624: B<kill> B<-s> SIGTERM I<PID>
5625: B<kill> B<-s> SIGINT I<PID>
5626:
5627: Subroutine B<HUNTSMAN>:
5628: This is only invoked for the B<lond> parent I<PID>.
5629: This kills all the children, and then the parent.
5630: The B<lonc.pid> file is cleared.
5631:
5632: =item *
5633:
5634: SIGHUP
5635:
5636: Current bug:
5637: This signal can only be processed the first time
5638: on the parent process. Subsequent SIGHUP signals
5639: have no effect.
5640:
5641: Parent signal assignment:
5642: $SIG{HUP} = \&HUPSMAN;
5643:
5644: Child signal assignment:
5645: none (nothing happens)
5646:
5647: Command-line invocations:
5648: B<kill> B<-s> SIGHUP I<PID>
5649:
5650: Subroutine B<HUPSMAN>:
5651: This is only invoked for the B<lond> parent I<PID>,
5652: This kills all the children, and then the parent.
5653: The B<lond.pid> file is cleared.
5654:
5655: =item *
5656:
5657: SIGUSR1
5658:
5659: Parent signal assignment:
5660: $SIG{USR1} = \&USRMAN;
5661:
5662: Child signal assignment:
5663: $SIG{USR1}= \&logstatus;
5664:
5665: Command-line invocations:
5666: B<kill> B<-s> SIGUSR1 I<PID>
5667:
5668: Subroutine B<USRMAN>:
5669: When invoked for the B<lond> parent I<PID>,
5670: SIGUSR1 is sent to all the children, and the status of
5671: each connection is logged.
1.144 foxr 5672:
5673: =item *
5674:
5675: SIGUSR2
5676:
5677: Parent Signal assignment:
5678: $SIG{USR2} = \&UpdateHosts
5679:
5680: Child signal assignment:
5681: NONE
5682:
1.74 harris41 5683:
5684: =item *
5685:
5686: SIGCHLD
5687:
5688: Parent signal assignment:
5689: $SIG{CHLD} = \&REAPER;
5690:
5691: Child signal assignment:
5692: none
5693:
5694: Command-line invocations:
5695: B<kill> B<-s> SIGCHLD I<PID>
5696:
5697: Subroutine B<REAPER>:
5698: This is only invoked for the B<lond> parent I<PID>.
5699: Information pertaining to the child is removed.
5700: The socket port is cleaned up.
5701:
5702: =back
5703:
5704: B<SERVER-SIDE ACTIVITIES>
5705:
5706: Server-side information can be accepted in an encrypted or non-encrypted
5707: method.
5708:
5709: =over 4
5710:
5711: =item ping
5712:
5713: Query a client in the hosts.tab table; "Are you there?"
5714:
5715: =item pong
5716:
5717: Respond to a ping query.
5718:
5719: =item ekey
5720:
5721: Read in encrypted key, make cipher. Respond with a buildkey.
5722:
5723: =item load
5724:
5725: Respond with CPU load based on a computation upon /proc/loadavg.
5726:
5727: =item currentauth
5728:
5729: Reply with current authentication information (only over an
5730: encrypted channel).
5731:
5732: =item auth
5733:
5734: Only over an encrypted channel, reply as to whether a user's
5735: authentication information can be validated.
5736:
5737: =item passwd
5738:
5739: Allow for a password to be set.
5740:
5741: =item makeuser
5742:
5743: Make a user.
5744:
5745: =item passwd
5746:
5747: Allow for authentication mechanism and password to be changed.
5748:
5749: =item home
1.61 harris41 5750:
1.74 harris41 5751: Respond to a question "are you the home for a given user?"
5752:
5753: =item update
5754:
5755: Update contents of a subscribed resource.
5756:
5757: =item unsubscribe
5758:
5759: The server is unsubscribing from a resource.
5760:
5761: =item subscribe
5762:
5763: The server is subscribing to a resource.
5764:
5765: =item log
5766:
5767: Place in B<logs/lond.log>
5768:
5769: =item put
5770:
5771: stores hash in namespace
5772:
1.230 foxr 5773: =item rolesputy
1.74 harris41 5774:
5775: put a role into a user's environment
5776:
5777: =item get
5778:
5779: returns hash with keys from array
5780: reference filled in from namespace
5781:
5782: =item eget
5783:
5784: returns hash with keys from array
5785: reference filled in from namesp (encrypts the return communication)
5786:
5787: =item rolesget
5788:
5789: get a role from a user's environment
5790:
5791: =item del
5792:
5793: deletes keys out of array from namespace
5794:
5795: =item keys
5796:
5797: returns namespace keys
5798:
5799: =item dump
5800:
5801: dumps the complete (or key matching regexp) namespace into a hash
5802:
5803: =item store
5804:
5805: stores hash permanently
5806: for this url; hashref needs to be given and should be a \%hashname; the
5807: remaining args aren't required and if they aren't passed or are '' they will
5808: be derived from the ENV
5809:
5810: =item restore
5811:
5812: returns a hash for a given url
5813:
5814: =item querysend
5815:
5816: Tells client about the lonsql process that has been launched in response
5817: to a sent query.
5818:
5819: =item queryreply
5820:
5821: Accept information from lonsql and make appropriate storage in temporary
5822: file space.
5823:
5824: =item idput
5825:
5826: Defines usernames as corresponding to IDs. (These "IDs" are unique identifiers
5827: for each student, defined perhaps by the institutional Registrar.)
5828:
5829: =item idget
5830:
5831: Returns usernames corresponding to IDs. (These "IDs" are unique identifiers
5832: for each student, defined perhaps by the institutional Registrar.)
5833:
5834: =item tmpput
5835:
5836: Accept and store information in temporary space.
5837:
5838: =item tmpget
5839:
5840: Send along temporarily stored information.
5841:
5842: =item ls
5843:
5844: List part of a user's directory.
5845:
1.135 foxr 5846: =item pushtable
5847:
5848: Pushes a file in /home/httpd/lonTab directory. Currently limited to:
5849: hosts.tab and domain.tab. The old file is copied to *.tab.backup but
5850: must be restored manually in case of a problem with the new table file.
5851: pushtable requires that the request be encrypted and validated via
5852: ValidateManager. The form of the command is:
5853: enc:pushtable tablename <tablecontents> \n
5854: where pushtable, tablename and <tablecontents> will be encrypted, but \n is a
5855: cleartext newline.
5856:
1.74 harris41 5857: =item Hanging up (exit or init)
5858:
5859: What to do when a client tells the server that they (the client)
5860: are leaving the network.
5861:
5862: =item unknown command
5863:
5864: If B<lond> is sent an unknown command (not in the list above),
5865: it replys to the client "unknown_cmd".
1.135 foxr 5866:
1.74 harris41 5867:
5868: =item UNKNOWN CLIENT
5869:
5870: If the anti-spoofing algorithm cannot verify the client,
5871: the client is rejected (with a "refused" message sent
5872: to the client, and the connection is closed.
5873:
5874: =back
1.61 harris41 5875:
5876: =head1 PREREQUISITES
5877:
5878: IO::Socket
5879: IO::File
5880: Apache::File
5881: Symbol
5882: POSIX
5883: Crypt::IDEA
5884: LWP::UserAgent()
5885: GDBM_File
5886: Authen::Krb4
1.91 albertel 5887: Authen::Krb5
1.61 harris41 5888:
5889: =head1 COREQUISITES
5890:
5891: =head1 OSNAMES
5892:
5893: linux
5894:
5895: =head1 SCRIPT CATEGORIES
5896:
5897: Server/Process
5898:
5899: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>