Annotation of loncom/lond, revision 1.502
1.1 albertel 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
1.60 www 4: #
1.502 ! raeburn 5: # $Id: lond,v 1.501 2013/07/25 19:11:07 raeburn 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
1.469 foxr 18:
1.60 www 19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20: # GNU General Public License for more details.
21: #
22: # You should have received a copy of the GNU General Public License
23: # along with LON-CAPA; if not, write to the Free Software
1.178 foxr 24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.60 www 25: #
26: # /home/httpd/html/adm/gpl.txt
27: #
1.161 foxr 28:
29:
1.60 www 30: # http://www.lon-capa.org/
31: #
1.54 harris41 32:
1.134 albertel 33: use strict;
1.80 harris41 34: use lib '/home/httpd/lib/perl/';
1.325 albertel 35: use LONCAPA;
1.80 harris41 36: use LONCAPA::Configuration;
1.490 droeschl 37: use LONCAPA::Lond;
1.80 harris41 38:
1.1 albertel 39: use IO::Socket;
40: use IO::File;
1.126 albertel 41: #use Apache::File;
1.1 albertel 42: use POSIX;
43: use Crypt::IDEA;
44: use LWP::UserAgent();
1.347 raeburn 45: use Digest::MD5 qw(md5_hex);
1.3 www 46: use GDBM_File;
1.91 albertel 47: use Authen::Krb5;
1.49 albertel 48: use localauth;
1.193 raeburn 49: use localenroll;
1.265 albertel 50: use localstudentphoto;
1.143 foxr 51: use File::Copy;
1.292 albertel 52: use File::Find;
1.200 matthew 53: use LONCAPA::lonlocal;
54: use LONCAPA::lonssl;
1.221 albertel 55: use Fcntl qw(:flock);
1.383 raeburn 56: use Apache::lonnet;
1.472 raeburn 57: use Mail::Send;
1.1 albertel 58:
1.463 foxr 59: my $DEBUG = 0; # Non zero to enable debug log entries.
1.77 foxr 60:
1.57 www 61: my $status='';
62: my $lastlog='';
63:
1.502 ! raeburn 64: my $VERSION='$Revision: 1.501 $'; #' stupid emacs
1.121 albertel 65: my $remoteVERSION;
1.214 foxr 66: my $currenthostid="default";
1.115 albertel 67: my $currentdomainid;
1.134 albertel 68:
69: my $client;
1.200 matthew 70: my $clientip; # IP address of client.
71: my $clientname; # LonCAPA name of client.
1.448 raeburn 72: my $clientversion; # LonCAPA version running on client.
73: my $clienthomedom; # LonCAPA domain of homeID for client.
74: # primary library server.
1.140 foxr 75:
1.134 albertel 76: my $server;
1.200 matthew 77:
78: my $keymode;
1.198 foxr 79:
1.207 foxr 80: my $cipher; # Cipher key negotiated with client
81: my $tmpsnum = 0; # Id of tmpputs.
82:
1.178 foxr 83: #
84: # Connection type is:
85: # client - All client actions are allowed
86: # manager - only management functions allowed.
87: # both - Both management and client actions are allowed
88: #
1.161 foxr 89:
1.178 foxr 90: my $ConnectionType;
1.161 foxr 91:
1.178 foxr 92: my %managers; # Ip -> manager names
1.161 foxr 93:
1.178 foxr 94: my %perlvar; # Will have the apache conf defined perl vars.
1.134 albertel 95:
1.480 raeburn 96: my $dist;
97:
1.178 foxr 98: #
1.207 foxr 99: # The hash below is used for command dispatching, and is therefore keyed on the request keyword.
100: # Each element of the hash contains a reference to an array that contains:
101: # A reference to a sub that executes the request corresponding to the keyword.
102: # A flag that is true if the request must be encoded to be acceptable.
103: # A mask with bits as follows:
104: # CLIENT_OK - Set when the function is allowed by ordinary clients
105: # MANAGER_OK - Set when the function is allowed to manager clients.
106: #
107: my $CLIENT_OK = 1;
108: my $MANAGER_OK = 2;
109: my %Dispatcher;
110:
111:
112: #
1.178 foxr 113: # The array below are password error strings."
114: #
115: my $lastpwderror = 13; # Largest error number from lcpasswd.
116: my @passwderrors = ("ok",
1.287 foxr 117: "pwchange_failure - lcpasswd must be run as user 'www'",
118: "pwchange_failure - lcpasswd got incorrect number of arguments",
119: "pwchange_failure - lcpasswd did not get the right nubmer of input text lines",
120: "pwchange_failure - lcpasswd too many simultaneous pwd changes in progress",
121: "pwchange_failure - lcpasswd User does not exist.",
122: "pwchange_failure - lcpasswd Incorrect current passwd",
123: "pwchange_failure - lcpasswd Unable to su to root.",
124: "pwchange_failure - lcpasswd Cannot set new passwd.",
125: "pwchange_failure - lcpasswd Username has invalid characters",
126: "pwchange_failure - lcpasswd Invalid characters in password",
127: "pwchange_failure - lcpasswd User already exists",
128: "pwchange_failure - lcpasswd Something went wrong with user addition.",
129: "pwchange_failure - lcpasswd Password mismatch",
130: "pwchange_failure - lcpasswd Error filename is invalid");
1.97 foxr 131:
132:
1.412 foxr 133: # This array are the errors from lcinstallfile:
134:
135: my @installerrors = ("ok",
136: "Initial user id of client not that of www",
137: "Usage error, not enough command line arguments",
1.500 bisitz 138: "Source filename does not exist",
139: "Destination filename does not exist",
1.412 foxr 140: "Some file operation failed",
141: "Invalid table filename."
142: );
1.207 foxr 143:
144: #
145: # Statistics that are maintained and dislayed in the status line.
146: #
1.212 foxr 147: my $Transactions = 0; # Number of attempted transactions.
148: my $Failures = 0; # Number of transcations failed.
1.207 foxr 149:
150: # ResetStatistics:
151: # Resets the statistics counters:
152: #
153: sub ResetStatistics {
154: $Transactions = 0;
155: $Failures = 0;
156: }
157:
1.200 matthew 158: #------------------------------------------------------------------------
159: #
160: # LocalConnection
161: # Completes the formation of a locally authenticated connection.
162: # This function will ensure that the 'remote' client is really the
163: # local host. If not, the connection is closed, and the function fails.
164: # If so, initcmd is parsed for the name of a file containing the
165: # IDEA session key. The fie is opened, read, deleted and the session
166: # key returned to the caller.
167: #
168: # Parameters:
169: # $Socket - Socket open on client.
170: # $initcmd - The full text of the init command.
171: #
172: # Returns:
173: # IDEA session key on success.
174: # undef on failure.
175: #
176: sub LocalConnection {
177: my ($Socket, $initcmd) = @_;
1.373 albertel 178: Debug("Attempting local connection: $initcmd client: $clientip");
1.277 albertel 179: if($clientip ne "127.0.0.1") {
1.200 matthew 180: &logthis('<font color="red"> LocalConnection rejecting non local: '
1.373 albertel 181: ."$clientip ne 127.0.0.1 </font>");
1.200 matthew 182: close $Socket;
183: return undef;
1.224 foxr 184: } else {
1.200 matthew 185: chomp($initcmd); # Get rid of \n in filename.
186: my ($init, $type, $name) = split(/:/, $initcmd);
187: Debug(" Init command: $init $type $name ");
188:
189: # Require that $init = init, and $type = local: Otherwise
190: # the caller is insane:
191:
192: if(($init ne "init") && ($type ne "local")) {
193: &logthis('<font color = "red"> LocalConnection: caller is insane! '
194: ."init = $init, and type = $type </font>");
195: close($Socket);;
196: return undef;
197:
198: }
199: # Now get the key filename:
200:
201: my $IDEAKey = lonlocal::ReadKeyFile($name);
202: return $IDEAKey;
203: }
204: }
205: #------------------------------------------------------------------------------
206: #
207: # SSLConnection
208: # Completes the formation of an ssh authenticated connection. The
209: # socket is promoted to an ssl socket. If this promotion and the associated
210: # certificate exchange are successful, the IDEA key is generated and sent
211: # to the remote peer via the SSL tunnel. The IDEA key is also returned to
212: # the caller after the SSL tunnel is torn down.
213: #
214: # Parameters:
215: # Name Type Purpose
216: # $Socket IO::Socket::INET Plaintext socket.
217: #
218: # Returns:
219: # IDEA key on success.
220: # undef on failure.
221: #
222: sub SSLConnection {
223: my $Socket = shift;
224:
225: Debug("SSLConnection: ");
226: my $KeyFile = lonssl::KeyFile();
227: if(!$KeyFile) {
228: my $err = lonssl::LastError();
229: &logthis("<font color=\"red\"> CRITICAL"
230: ."Can't get key file $err </font>");
231: return undef;
232: }
233: my ($CACertificate,
234: $Certificate) = lonssl::CertificateFile();
235:
236:
237: # If any of the key, certificate or certificate authority
238: # certificate filenames are not defined, this can't work.
239:
240: if((!$Certificate) || (!$CACertificate)) {
241: my $err = lonssl::LastError();
242: &logthis("<font color=\"red\"> CRITICAL"
243: ."Can't get certificates: $err </font>");
244:
245: return undef;
246: }
247: Debug("Key: $KeyFile CA: $CACertificate Cert: $Certificate");
248:
249: # Indicate to our peer that we can procede with
250: # a transition to ssl authentication:
251:
252: print $Socket "ok:ssl\n";
253:
254: Debug("Approving promotion -> ssl");
255: # And do so:
256:
257: my $SSLSocket = lonssl::PromoteServerSocket($Socket,
258: $CACertificate,
259: $Certificate,
260: $KeyFile);
261: if(! ($SSLSocket) ) { # SSL socket promotion failed.
262: my $err = lonssl::LastError();
263: &logthis("<font color=\"red\"> CRITICAL "
264: ."SSL Socket promotion failed: $err </font>");
265: return undef;
266: }
267: Debug("SSL Promotion successful");
268:
269: #
270: # The only thing we'll use the socket for is to send the IDEA key
271: # to the peer:
272:
273: my $Key = lonlocal::CreateCipherKey();
274: print $SSLSocket "$Key\n";
275:
276: lonssl::Close($SSLSocket);
277:
278: Debug("Key exchange complete: $Key");
279:
280: return $Key;
281: }
282: #
283: # InsecureConnection:
284: # If insecure connections are allowd,
285: # exchange a challenge with the client to 'validate' the
286: # client (not really, but that's the protocol):
287: # We produce a challenge string that's sent to the client.
288: # The client must then echo the challenge verbatim to us.
289: #
290: # Parameter:
291: # Socket - Socket open on the client.
292: # Returns:
293: # 1 - success.
294: # 0 - failure (e.g.mismatch or insecure not allowed).
295: #
296: sub InsecureConnection {
297: my $Socket = shift;
298:
299: # Don't even start if insecure connections are not allowed.
300:
301: if(! $perlvar{londAllowInsecure}) { # Insecure connections not allowed.
302: return 0;
303: }
304:
305: # Fabricate a challenge string and send it..
306:
307: my $challenge = "$$".time; # pid + time.
308: print $Socket "$challenge\n";
309: &status("Waiting for challenge reply");
310:
311: my $answer = <$Socket>;
312: $answer =~s/\W//g;
313: if($challenge eq $answer) {
314: return 1;
1.224 foxr 315: } else {
1.200 matthew 316: logthis("<font color='blue'>WARNING client did not respond to challenge</font>");
317: &status("No challenge reqply");
318: return 0;
319: }
320:
321:
322: }
1.251 foxr 323: #
324: # Safely execute a command (as long as it's not a shel command and doesn
325: # not require/rely on shell escapes. The function operates by doing a
326: # a pipe based fork and capturing stdout and stderr from the pipe.
327: #
328: # Formal Parameters:
329: # $line - A line of text to be executed as a command.
330: # Returns:
331: # The output from that command. If the output is multiline the caller
332: # must know how to split up the output.
333: #
334: #
335: sub execute_command {
336: my ($line) = @_;
337: my @words = split(/\s/, $line); # Bust the command up into words.
338: my $output = "";
339:
340: my $pid = open(CHILD, "-|");
341:
342: if($pid) { # Parent process
343: Debug("In parent process for execute_command");
344: my @data = <CHILD>; # Read the child's outupt...
345: close CHILD;
346: foreach my $output_line (@data) {
347: Debug("Adding $output_line");
348: $output .= $output_line; # Presumably has a \n on it.
349: }
350:
351: } else { # Child process
352: close (STDERR);
353: open (STDERR, ">&STDOUT");# Combine stderr, and stdout...
354: exec(@words); # won't return.
355: }
356: return $output;
357: }
358:
1.200 matthew 359:
1.140 foxr 360: # GetCertificate: Given a transaction that requires a certificate,
361: # this function will extract the certificate from the transaction
362: # request. Note that at this point, the only concept of a certificate
363: # is the hostname to which we are connected.
364: #
365: # Parameter:
366: # request - The request sent by our client (this parameterization may
367: # need to change when we really use a certificate granting
368: # authority.
369: #
370: sub GetCertificate {
371: my $request = shift;
372:
373: return $clientip;
374: }
1.161 foxr 375:
1.178 foxr 376: #
377: # Return true if client is a manager.
378: #
379: sub isManager {
380: return (($ConnectionType eq "manager") || ($ConnectionType eq "both"));
381: }
382: #
383: # Return tru if client can do client functions
384: #
385: sub isClient {
386: return (($ConnectionType eq "client") || ($ConnectionType eq "both"));
387: }
1.161 foxr 388:
389:
1.156 foxr 390: #
391: # ReadManagerTable: Reads in the current manager table. For now this is
392: # done on each manager authentication because:
393: # - These authentications are not frequent
394: # - This allows dynamic changes to the manager table
395: # without the need to signal to the lond.
396: #
397: sub ReadManagerTable {
398:
1.412 foxr 399: &Debug("Reading manager table");
1.156 foxr 400: # Clean out the old table first..
401:
1.166 foxr 402: foreach my $key (keys %managers) {
403: delete $managers{$key};
404: }
405:
406: my $tablename = $perlvar{'lonTabDir'}."/managers.tab";
407: if (!open (MANAGERS, $tablename)) {
1.473 raeburn 408: my $hostname = &Apache::lonnet::hostname($perlvar{'lonHostID'});
409: if (&Apache::lonnet::is_LC_dns($hostname)) {
1.472 raeburn 410: &logthis('<font color="red">No manager table. Nobody can manage!!</font>');
411: }
412: return;
1.166 foxr 413: }
414: while(my $host = <MANAGERS>) {
415: chomp($host);
416: if ($host =~ "^#") { # Comment line.
417: next;
418: }
1.368 albertel 419: if (!defined &Apache::lonnet::get_host_ip($host)) { # This is a non cluster member
1.161 foxr 420: # The entry is of the form:
421: # cluname:hostname
422: # cluname - A 'cluster hostname' is needed in order to negotiate
423: # the host key.
424: # hostname- The dns name of the host.
425: #
1.166 foxr 426: my($cluname, $dnsname) = split(/:/, $host);
427:
428: my $ip = gethostbyname($dnsname);
429: if(defined($ip)) { # bad names don't deserve entry.
430: my $hostip = inet_ntoa($ip);
431: $managers{$hostip} = $cluname;
432: logthis('<font color="green"> registering manager '.
433: "$dnsname as $cluname with $hostip </font>\n");
434: }
435: } else {
436: logthis('<font color="green"> existing host'." $host</font>\n");
1.472 raeburn 437: $managers{&Apache::lonnet::get_host_ip($host)} = $host; # Use info from cluster tab if cluster memeber
1.166 foxr 438: }
439: }
1.156 foxr 440: }
1.140 foxr 441:
442: #
443: # ValidManager: Determines if a given certificate represents a valid manager.
444: # in this primitive implementation, the 'certificate' is
445: # just the connecting loncapa client name. This is checked
446: # against a valid client list in the configuration.
447: #
448: #
449: sub ValidManager {
450: my $certificate = shift;
451:
1.163 foxr 452: return isManager;
1.140 foxr 453: }
454: #
1.143 foxr 455: # CopyFile: Called as part of the process of installing a
456: # new configuration file. This function copies an existing
457: # file to a backup file.
458: # Parameters:
459: # oldfile - Name of the file to backup.
460: # newfile - Name of the backup file.
461: # Return:
462: # 0 - Failure (errno has failure reason).
463: # 1 - Success.
464: #
465: sub CopyFile {
1.192 foxr 466:
467: my ($oldfile, $newfile) = @_;
1.143 foxr 468:
1.281 matthew 469: if (! copy($oldfile,$newfile)) {
470: return 0;
1.143 foxr 471: }
1.281 matthew 472: chmod(0660, $newfile);
473: return 1;
1.143 foxr 474: }
1.157 foxr 475: #
476: # Host files are passed out with externally visible host IPs.
477: # If, for example, we are behind a fire-wall or NAT host, our
478: # internally visible IP may be different than the externally
479: # visible IP. Therefore, we always adjust the contents of the
480: # host file so that the entry for ME is the IP that we believe
481: # we have. At present, this is defined as the entry that
482: # DNS has for us. If by some chance we are not able to get a
483: # DNS translation for us, then we assume that the host.tab file
484: # is correct.
485: # BUGBUGBUG - in the future, we really should see if we can
486: # easily query the interface(s) instead.
487: # Parameter(s):
488: # contents - The contents of the host.tab to check.
489: # Returns:
490: # newcontents - The adjusted contents.
491: #
492: #
493: sub AdjustHostContents {
494: my $contents = shift;
495: my $adjusted;
496: my $me = $perlvar{'lonHostID'};
497:
1.354 albertel 498: foreach my $line (split(/\n/,$contents)) {
1.472 raeburn 499: if(!(($line eq "") || ($line =~ /^ *\#/) || ($line =~ /^ *$/) ||
500: ($line =~ /^\s*\^/))) {
1.157 foxr 501: chomp($line);
502: my ($id,$domain,$role,$name,$ip,$maxcon,$idleto,$mincon)=split(/:/,$line);
503: if ($id eq $me) {
1.354 albertel 504: my $ip = gethostbyname($name);
505: my $ipnew = inet_ntoa($ip);
506: $ip = $ipnew;
1.157 foxr 507: # Reconstruct the host line and append to adjusted:
508:
1.354 albertel 509: my $newline = "$id:$domain:$role:$name:$ip";
510: if($maxcon ne "") { # Not all hosts have loncnew tuning params
511: $newline .= ":$maxcon:$idleto:$mincon";
512: }
513: $adjusted .= $newline."\n";
1.157 foxr 514:
1.354 albertel 515: } else { # Not me, pass unmodified.
516: $adjusted .= $line."\n";
517: }
1.157 foxr 518: } else { # Blank or comment never re-written.
519: $adjusted .= $line."\n"; # Pass blanks and comments as is.
520: }
1.354 albertel 521: }
522: return $adjusted;
1.157 foxr 523: }
1.143 foxr 524: #
525: # InstallFile: Called to install an administrative file:
1.412 foxr 526: # - The file is created int a temp directory called <name>.tmp
527: # - lcinstall file is called to install the file.
528: # since the web app has no direct write access to the table directory
1.143 foxr 529: #
530: # Parameters:
531: # Name of the file
532: # File Contents.
533: # Return:
534: # nonzero - success.
535: # 0 - failure and $! has an errno.
1.412 foxr 536: # Assumptions:
537: # File installtion is a relatively infrequent
1.143 foxr 538: #
539: sub InstallFile {
1.192 foxr 540:
541: my ($Filename, $Contents) = @_;
1.412 foxr 542: # my $TempFile = $Filename.".tmp";
543: my $exedir = $perlvar{'lonDaemons'};
544: my $tmpdir = $exedir.'/tmp/';
545: my $TempFile = $tmpdir."TempTableFile.tmp";
1.143 foxr 546:
547: # Open the file for write:
548:
549: my $fh = IO::File->new("> $TempFile"); # Write to temp.
550: if(!(defined $fh)) {
551: &logthis('<font color="red"> Unable to create '.$TempFile."</font>");
552: return 0;
553: }
554: # write the contents of the file:
555:
556: print $fh ($Contents);
557: $fh->close; # In case we ever have a filesystem w. locking
558:
1.412 foxr 559: chmod(0664, $TempFile); # Everyone can write it.
560:
561: # Use lcinstall file to put the file in the table directory...
562:
563: &Debug("Opening pipe to $exedir/lcinstallfile $TempFile $Filename");
564: my $pf = IO::File->new("| $exedir/lcinstallfile $TempFile $Filename > $exedir/logs/lcinstallfile.log");
565: close $pf;
566: my $err = $?;
567: &Debug("Status is $err");
568: if ($err != 0) {
569: my $msg = $err;
570: if ($err < @installerrors) {
571: $msg = $installerrors[$err];
572: }
573: &logthis("Install failed for table file $Filename : $msg");
574: return 0;
575: }
576:
577: # Remove the temp file:
1.143 foxr 578:
1.412 foxr 579: unlink($TempFile);
1.143 foxr 580:
581: return 1;
582: }
1.200 matthew 583:
584:
1.169 foxr 585: #
586: # ConfigFileFromSelector: converts a configuration file selector
1.411 foxr 587: # into a configuration file pathname.
1.472 raeburn 588: # Supports the following file selectors:
589: # hosts, domain, dns_hosts, dns_domain
1.411 foxr 590: #
1.169 foxr 591: #
592: # Parameters:
593: # selector - Configuration file selector.
594: # Returns:
595: # Full path to the file or undef if the selector is invalid.
596: #
597: sub ConfigFileFromSelector {
598: my $selector = shift;
599: my $tablefile;
600:
601: my $tabledir = $perlvar{'lonTabDir'}.'/';
1.472 raeburn 602: if (($selector eq "hosts") || ($selector eq "domain") ||
603: ($selector eq "dns_hosts") || ($selector eq "dns_domain")) {
1.411 foxr 604: $tablefile = $tabledir.$selector.'.tab';
1.169 foxr 605: }
606: return $tablefile;
607: }
1.143 foxr 608: #
1.141 foxr 609: # PushFile: Called to do an administrative push of a file.
610: # - Ensure the file being pushed is one we support.
611: # - Backup the old file to <filename.saved>
612: # - Separate the contents of the new file out from the
613: # rest of the request.
614: # - Write the new file.
615: # Parameter:
616: # Request - The entire user request. This consists of a : separated
617: # string pushfile:tablename:contents.
618: # NOTE: The contents may have :'s in it as well making things a bit
619: # more interesting... but not much.
620: # Returns:
621: # String to send to client ("ok" or "refused" if bad file).
622: #
623: sub PushFile {
624: my $request = shift;
625: my ($command, $filename, $contents) = split(":", $request, 3);
1.412 foxr 626: &Debug("PushFile");
1.141 foxr 627:
628: # At this point in time, pushes for only the following tables are
629: # supported:
630: # hosts.tab ($filename eq host).
631: # domain.tab ($filename eq domain).
1.472 raeburn 632: # dns_hosts.tab ($filename eq dns_host).
633: # dns_domain.tab ($filename eq dns_domain).
1.141 foxr 634: # Construct the destination filename or reject the request.
635: #
636: # lonManage is supposed to ensure this, however this session could be
637: # part of some elaborate spoof that managed somehow to authenticate.
638: #
639:
1.169 foxr 640:
641: my $tablefile = ConfigFileFromSelector($filename);
642: if(! (defined $tablefile)) {
1.141 foxr 643: return "refused";
644: }
1.412 foxr 645:
1.157 foxr 646: # If the file being pushed is the host file, we adjust the entry for ourself so that the
647: # IP will be our current IP as looked up in dns. Note this is only 99% good as it's possible
648: # to conceive of conditions where we don't have a DNS entry locally. This is possible in a
649: # network sense but it doesn't make much sense in a LonCAPA sense so we ignore (for now)
650: # that possibilty.
651:
652: if($filename eq "host") {
653: $contents = AdjustHostContents($contents);
654: }
655:
1.141 foxr 656: # Install the new file:
657:
1.412 foxr 658: &logthis("Installing new $tablefile contents:\n$contents");
1.143 foxr 659: if(!InstallFile($tablefile, $contents)) {
660: &logthis('<font color="red"> Pushfile: unable to install '
1.145 foxr 661: .$tablefile." $! </font>");
1.143 foxr 662: return "error:$!";
1.224 foxr 663: } else {
1.143 foxr 664: &logthis('<font color="green"> Installed new '.$tablefile
1.473 raeburn 665: ." - transaction by: $clientname ($clientip)</font>");
1.472 raeburn 666: my $adminmail = $perlvar{'lonAdmEMail'};
667: my $admindom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
668: if ($admindom ne '') {
669: my %domconfig =
670: &Apache::lonnet::get_dom('configuration',['contacts'],$admindom);
671: if (ref($domconfig{'contacts'}) eq 'HASH') {
672: if ($domconfig{'contacts'}{'adminemail'} ne '') {
673: $adminmail = $domconfig{'contacts'}{'adminemail'};
674: }
675: }
676: }
677: if ($adminmail =~ /^[^\@]+\@[^\@]+$/) {
678: my $msg = new Mail::Send;
679: $msg->to($adminmail);
680: $msg->subject('LON-CAPA DNS update on '.$perlvar{'lonHostID'});
681: $msg->add('Content-type','text/plain; charset=UTF-8');
682: if (my $fh = $msg->open()) {
683: print $fh 'Update to '.$tablefile.' from Cluster Manager '.
1.473 raeburn 684: "$clientname ($clientip)\n";
1.472 raeburn 685: $fh->close;
686: }
687: }
1.143 foxr 688: }
689:
1.141 foxr 690: # Indicate success:
691:
692: return "ok";
693:
694: }
1.145 foxr 695:
696: #
697: # Called to re-init either lonc or lond.
698: #
699: # Parameters:
700: # request - The full request by the client. This is of the form
701: # reinit:<process>
702: # where <process> is allowed to be either of
703: # lonc or lond
704: #
705: # Returns:
706: # The string to be sent back to the client either:
707: # ok - Everything worked just fine.
708: # error:why - There was a failure and why describes the reason.
709: #
710: #
711: sub ReinitProcess {
712: my $request = shift;
713:
1.146 foxr 714:
715: # separate the request (reinit) from the process identifier and
716: # validate it producing the name of the .pid file for the process.
717: #
718: #
719: my ($junk, $process) = split(":", $request);
1.147 foxr 720: my $processpidfile = $perlvar{'lonDaemons'}.'/logs/';
1.146 foxr 721: if($process eq 'lonc') {
722: $processpidfile = $processpidfile."lonc.pid";
1.147 foxr 723: if (!open(PIDFILE, "< $processpidfile")) {
724: return "error:Open failed for $processpidfile";
725: }
726: my $loncpid = <PIDFILE>;
727: close(PIDFILE);
728: logthis('<font color="red"> Reinitializing lonc pid='.$loncpid
729: ."</font>");
730: kill("USR2", $loncpid);
1.146 foxr 731: } elsif ($process eq 'lond') {
1.147 foxr 732: logthis('<font color="red"> Reinitializing self (lond) </font>');
733: &UpdateHosts; # Lond is us!!
1.146 foxr 734: } else {
735: &logthis('<font color="yellow" Invalid reinit request for '.$process
736: ."</font>");
737: return "error:Invalid process identifier $process";
738: }
1.145 foxr 739: return 'ok';
740: }
1.168 foxr 741: # Validate a line in a configuration file edit script:
742: # Validation includes:
743: # - Ensuring the command is valid.
744: # - Ensuring the command has sufficient parameters
745: # Parameters:
746: # scriptline - A line to validate (\n has been stripped for what it's worth).
1.167 foxr 747: #
1.168 foxr 748: # Return:
749: # 0 - Invalid scriptline.
750: # 1 - Valid scriptline
751: # NOTE:
752: # Only the command syntax is checked, not the executability of the
753: # command.
754: #
755: sub isValidEditCommand {
756: my $scriptline = shift;
757:
758: # Line elements are pipe separated:
759:
760: my ($command, $key, $newline) = split(/\|/, $scriptline);
761: &logthis('<font color="green"> isValideditCommand checking: '.
762: "Command = '$command', Key = '$key', Newline = '$newline' </font>\n");
763:
764: if ($command eq "delete") {
765: #
766: # key with no newline.
767: #
768: if( ($key eq "") || ($newline ne "")) {
769: return 0; # Must have key but no newline.
770: } else {
771: return 1; # Valid syntax.
772: }
1.169 foxr 773: } elsif ($command eq "replace") {
1.168 foxr 774: #
775: # key and newline:
776: #
777: if (($key eq "") || ($newline eq "")) {
778: return 0;
779: } else {
780: return 1;
781: }
1.169 foxr 782: } elsif ($command eq "append") {
783: if (($key ne "") && ($newline eq "")) {
784: return 1;
785: } else {
786: return 0;
787: }
1.168 foxr 788: } else {
789: return 0; # Invalid command.
790: }
791: return 0; # Should not get here!!!
792: }
1.169 foxr 793: #
794: # ApplyEdit - Applies an edit command to a line in a configuration
795: # file. It is the caller's responsiblity to validate the
796: # edit line.
797: # Parameters:
798: # $directive - A single edit directive to apply.
799: # Edit directives are of the form:
800: # append|newline - Appends a new line to the file.
801: # replace|key|newline - Replaces the line with key value 'key'
802: # delete|key - Deletes the line with key value 'key'.
803: # $editor - A config file editor object that contains the
804: # file being edited.
805: #
806: sub ApplyEdit {
1.192 foxr 807:
808: my ($directive, $editor) = @_;
1.169 foxr 809:
810: # Break the directive down into its command and its parameters
811: # (at most two at this point. The meaning of the parameters, if in fact
812: # they exist depends on the command).
813:
814: my ($command, $p1, $p2) = split(/\|/, $directive);
815:
816: if($command eq "append") {
817: $editor->Append($p1); # p1 - key p2 null.
818: } elsif ($command eq "replace") {
819: $editor->ReplaceLine($p1, $p2); # p1 - key p2 = newline.
820: } elsif ($command eq "delete") {
821: $editor->DeleteLine($p1); # p1 - key p2 null.
822: } else { # Should not get here!!!
823: die "Invalid command given to ApplyEdit $command"
824: }
825: }
826: #
827: # AdjustOurHost:
828: # Adjusts a host file stored in a configuration file editor object
829: # for the true IP address of this host. This is necessary for hosts
830: # that live behind a firewall.
831: # Those hosts have a publicly distributed IP of the firewall, but
832: # internally must use their actual IP. We assume that a given
833: # host only has a single IP interface for now.
834: # Formal Parameters:
835: # editor - The configuration file editor to adjust. This
836: # editor is assumed to contain a hosts.tab file.
837: # Strategy:
838: # - Figure out our hostname.
839: # - Lookup the entry for this host.
840: # - Modify the line to contain our IP
841: # - Do a replace for this host.
842: sub AdjustOurHost {
843: my $editor = shift;
844:
845: # figure out who I am.
846:
847: my $myHostName = $perlvar{'lonHostID'}; # LonCAPA hostname.
848:
849: # Get my host file entry.
850:
851: my $ConfigLine = $editor->Find($myHostName);
852: if(! (defined $ConfigLine)) {
853: die "AdjustOurHost - no entry for me in hosts file $myHostName";
854: }
855: # figure out my IP:
856: # Use the config line to get my hostname.
857: # Use gethostbyname to translate that into an IP address.
858: #
1.338 albertel 859: my ($id,$domain,$role,$name,$maxcon,$idleto,$mincon) = split(/:/,$ConfigLine);
1.169 foxr 860: #
861: # Reassemble the config line from the elements in the list.
862: # Note that if the loncnew items were not present before, they will
863: # be now even if they would be empty
864: #
865: my $newConfigLine = $id;
1.338 albertel 866: foreach my $item ($domain, $role, $name, $maxcon, $idleto, $mincon) {
1.169 foxr 867: $newConfigLine .= ":".$item;
868: }
869: # Replace the line:
870:
871: $editor->ReplaceLine($id, $newConfigLine);
872:
873: }
874: #
875: # ReplaceConfigFile:
876: # Replaces a configuration file with the contents of a
877: # configuration file editor object.
878: # This is done by:
879: # - Copying the target file to <filename>.old
880: # - Writing the new file to <filename>.tmp
881: # - Moving <filename.tmp> -> <filename>
882: # This laborious process ensures that the system is never without
883: # a configuration file that's at least valid (even if the contents
884: # may be dated).
885: # Parameters:
886: # filename - Name of the file to modify... this is a full path.
887: # editor - Editor containing the file.
888: #
889: sub ReplaceConfigFile {
1.192 foxr 890:
891: my ($filename, $editor) = @_;
1.168 foxr 892:
1.169 foxr 893: CopyFile ($filename, $filename.".old");
894:
895: my $contents = $editor->Get(); # Get the contents of the file.
896:
897: InstallFile($filename, $contents);
898: }
1.168 foxr 899: #
900: #
901: # Called to edit a configuration table file
1.167 foxr 902: # Parameters:
903: # request - The entire command/request sent by lonc or lonManage
904: # Return:
905: # The reply to send to the client.
1.168 foxr 906: #
1.167 foxr 907: sub EditFile {
908: my $request = shift;
909:
910: # Split the command into it's pieces: edit:filetype:script
911:
1.339 albertel 912: my ($cmd, $filetype, $script) = split(/:/, $request,3); # : in script
1.167 foxr 913:
914: # Check the pre-coditions for success:
915:
1.339 albertel 916: if($cmd != "edit") { # Something is amiss afoot alack.
1.167 foxr 917: return "error:edit request detected, but request != 'edit'\n";
918: }
919: if( ($filetype ne "hosts") &&
920: ($filetype ne "domain")) {
921: return "error:edit requested with invalid file specifier: $filetype \n";
922: }
923:
924: # Split the edit script and check it's validity.
1.168 foxr 925:
926: my @scriptlines = split(/\n/, $script); # one line per element.
927: my $linecount = scalar(@scriptlines);
928: for(my $i = 0; $i < $linecount; $i++) {
929: chomp($scriptlines[$i]);
930: if(!isValidEditCommand($scriptlines[$i])) {
931: return "error:edit with bad script line: '$scriptlines[$i]' \n";
932: }
933: }
1.145 foxr 934:
1.167 foxr 935: # Execute the edit operation.
1.169 foxr 936: # - Create a config file editor for the appropriate file and
937: # - execute each command in the script:
938: #
939: my $configfile = ConfigFileFromSelector($filetype);
940: if (!(defined $configfile)) {
941: return "refused\n";
942: }
943: my $editor = ConfigFileEdit->new($configfile);
1.167 foxr 944:
1.169 foxr 945: for (my $i = 0; $i < $linecount; $i++) {
946: ApplyEdit($scriptlines[$i], $editor);
947: }
948: # If the file is the host file, ensure that our host is
949: # adjusted to have our ip:
950: #
951: if($filetype eq "host") {
952: AdjustOurHost($editor);
953: }
954: # Finally replace the current file with our file.
955: #
956: ReplaceConfigFile($configfile, $editor);
1.167 foxr 957:
958: return "ok\n";
959: }
1.207 foxr 960:
1.255 foxr 961: # read_profile
962: #
963: # Returns a set of specific entries from a user's profile file.
964: # this is a utility function that is used by both get_profile_entry and
965: # get_profile_entry_encrypted.
966: #
967: # Parameters:
968: # udom - Domain in which the user exists.
969: # uname - User's account name (loncapa account)
970: # namespace - The profile namespace to open.
971: # what - A set of & separated queries.
972: # Returns:
973: # If all ok: - The string that needs to be shipped back to the user.
974: # If failure - A string that starts with error: followed by the failure
975: # reason.. note that this probabyl gets shipped back to the
976: # user as well.
977: #
978: sub read_profile {
979: my ($udom, $uname, $namespace, $what) = @_;
980:
981: my $hashref = &tie_user_hash($udom, $uname, $namespace,
982: &GDBM_READER());
983: if ($hashref) {
984: my @queries=split(/\&/,$what);
1.440 raeburn 985: if ($namespace eq 'roles') {
986: @queries = map { &unescape($_); } @queries;
987: }
1.255 foxr 988: my $qresult='';
989:
990: for (my $i=0;$i<=$#queries;$i++) {
991: $qresult.="$hashref->{$queries[$i]}&"; # Presumably failure gives empty string.
992: }
993: $qresult=~s/\&$//; # Remove trailing & from last lookup.
1.311 albertel 994: if (&untie_user_hash($hashref)) {
1.255 foxr 995: return $qresult;
996: } else {
997: return "error: ".($!+0)." untie (GDBM) Failed";
998: }
999: } else {
1000: if ($!+0 == 2) {
1001: return "error:No such file or GDBM reported bad block error";
1002: } else {
1003: return "error: ".($!+0)." tie (GDBM) Failed";
1004: }
1005: }
1006:
1007: }
1.214 foxr 1008: #--------------------- Request Handlers --------------------------------------------
1009: #
1.215 foxr 1010: # By convention each request handler registers itself prior to the sub
1011: # declaration:
1.214 foxr 1012: #
1013:
1.216 foxr 1014: #++
1015: #
1.214 foxr 1016: # Handles ping requests.
1017: # Parameters:
1018: # $cmd - the actual keyword that invoked us.
1019: # $tail - the tail of the request that invoked us.
1020: # $replyfd- File descriptor connected to the client
1021: # Implicit Inputs:
1022: # $currenthostid - Global variable that carries the name of the host we are
1023: # known as.
1024: # Returns:
1025: # 1 - Ok to continue processing.
1026: # 0 - Program should exit.
1027: # Side effects:
1028: # Reply information is sent to the client.
1029: sub ping_handler {
1030: my ($cmd, $tail, $client) = @_;
1031: Debug("$cmd $tail $client .. $currenthostid:");
1032:
1.387 albertel 1033: Reply( $client,\$currenthostid,"$cmd:$tail");
1.214 foxr 1034:
1035: return 1;
1036: }
1037: ®ister_handler("ping", \&ping_handler, 0, 1, 1); # Ping unencoded, client or manager.
1038:
1.216 foxr 1039: #++
1.215 foxr 1040: #
1041: # Handles pong requests. Pong replies with our current host id, and
1042: # the results of a ping sent to us via our lonc.
1043: #
1044: # Parameters:
1045: # $cmd - the actual keyword that invoked us.
1046: # $tail - the tail of the request that invoked us.
1047: # $replyfd- File descriptor connected to the client
1048: # Implicit Inputs:
1049: # $currenthostid - Global variable that carries the name of the host we are
1050: # connected to.
1051: # Returns:
1052: # 1 - Ok to continue processing.
1053: # 0 - Program should exit.
1054: # Side effects:
1055: # Reply information is sent to the client.
1056: sub pong_handler {
1057: my ($cmd, $tail, $replyfd) = @_;
1058:
1.365 albertel 1059: my $reply=&Apache::lonnet::reply("ping",$clientname);
1.215 foxr 1060: &Reply( $replyfd, "$currenthostid:$reply\n", "$cmd:$tail");
1061: return 1;
1062: }
1063: ®ister_handler("pong", \&pong_handler, 0, 1, 1); # Pong unencoded, client or manager
1064:
1.216 foxr 1065: #++
1066: # Called to establish an encrypted session key with the remote client.
1067: # Note that with secure lond, in most cases this function is never
1068: # invoked. Instead, the secure session key is established either
1069: # via a local file that's locked down tight and only lives for a short
1070: # time, or via an ssl tunnel...and is generated from a bunch-o-random
1071: # bits from /dev/urandom, rather than the predictable pattern used by
1072: # by this sub. This sub is only used in the old-style insecure
1073: # key negotiation.
1074: # Parameters:
1075: # $cmd - the actual keyword that invoked us.
1076: # $tail - the tail of the request that invoked us.
1077: # $replyfd- File descriptor connected to the client
1078: # Implicit Inputs:
1079: # $currenthostid - Global variable that carries the name of the host
1080: # known as.
1.448 raeburn 1081: # $clientname - Global variable that carries the name of the host we're connected to.
1.216 foxr 1082: # Returns:
1083: # 1 - Ok to continue processing.
1084: # 0 - Program should exit.
1085: # Implicit Outputs:
1086: # Reply information is sent to the client.
1087: # $cipher is set with a reference to a new IDEA encryption object.
1088: #
1089: sub establish_key_handler {
1090: my ($cmd, $tail, $replyfd) = @_;
1091:
1092: my $buildkey=time.$$.int(rand 100000);
1093: $buildkey=~tr/1-6/A-F/;
1094: $buildkey=int(rand 100000).$buildkey.int(rand 100000);
1095: my $key=$currenthostid.$clientname;
1096: $key=~tr/a-z/A-Z/;
1097: $key=~tr/G-P/0-9/;
1098: $key=~tr/Q-Z/0-9/;
1099: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
1100: $key=substr($key,0,32);
1101: my $cipherkey=pack("H32",$key);
1102: $cipher=new IDEA $cipherkey;
1.387 albertel 1103: &Reply($replyfd, \$buildkey, "$cmd:$tail");
1.216 foxr 1104:
1105: return 1;
1106:
1107: }
1108: ®ister_handler("ekey", \&establish_key_handler, 0, 1,1);
1109:
1.217 foxr 1110: # Handler for the load command. Returns the current system load average
1111: # to the requestor.
1112: #
1113: # Parameters:
1114: # $cmd - the actual keyword that invoked us.
1115: # $tail - the tail of the request that invoked us.
1116: # $replyfd- File descriptor connected to the client
1117: # Implicit Inputs:
1118: # $currenthostid - Global variable that carries the name of the host
1119: # known as.
1.448 raeburn 1120: # $clientname - Global variable that carries the name of the host we're connected to.
1.217 foxr 1121: # Returns:
1122: # 1 - Ok to continue processing.
1123: # 0 - Program should exit.
1124: # Side effects:
1125: # Reply information is sent to the client.
1126: sub load_handler {
1127: my ($cmd, $tail, $replyfd) = @_;
1128:
1.463 foxr 1129:
1130:
1.217 foxr 1131: # Get the load average from /proc/loadavg and calculate it as a percentage of
1132: # the allowed load limit as set by the perl global variable lonLoadLim
1133:
1134: my $loadavg;
1135: my $loadfile=IO::File->new('/proc/loadavg');
1136:
1137: $loadavg=<$loadfile>;
1138: $loadavg =~ s/\s.*//g; # Extract the first field only.
1139:
1140: my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
1141:
1.387 albertel 1142: &Reply( $replyfd, \$loadpercent, "$cmd:$tail");
1.217 foxr 1143:
1144: return 1;
1145: }
1.263 albertel 1146: ®ister_handler("load", \&load_handler, 0, 1, 0);
1.217 foxr 1147:
1148: #
1149: # Process the userload request. This sub returns to the client the current
1150: # user load average. It can be invoked either by clients or managers.
1151: #
1152: # Parameters:
1153: # $cmd - the actual keyword that invoked us.
1154: # $tail - the tail of the request that invoked us.
1155: # $replyfd- File descriptor connected to the client
1156: # Implicit Inputs:
1157: # $currenthostid - Global variable that carries the name of the host
1158: # known as.
1.448 raeburn 1159: # $clientname - Global variable that carries the name of the host we're connected to.
1.217 foxr 1160: # Returns:
1161: # 1 - Ok to continue processing.
1162: # 0 - Program should exit
1163: # Implicit inputs:
1164: # whatever the userload() function requires.
1165: # Implicit outputs:
1166: # the reply is written to the client.
1167: #
1168: sub user_load_handler {
1169: my ($cmd, $tail, $replyfd) = @_;
1170:
1.365 albertel 1171: my $userloadpercent=&Apache::lonnet::userload();
1.387 albertel 1172: &Reply($replyfd, \$userloadpercent, "$cmd:$tail");
1.217 foxr 1173:
1174: return 1;
1175: }
1.263 albertel 1176: ®ister_handler("userload", \&user_load_handler, 0, 1, 0);
1.217 foxr 1177:
1.218 foxr 1178: # Process a request for the authorization type of a user:
1179: # (userauth).
1180: #
1181: # Parameters:
1182: # $cmd - the actual keyword that invoked us.
1183: # $tail - the tail of the request that invoked us.
1184: # $replyfd- File descriptor connected to the client
1185: # Returns:
1186: # 1 - Ok to continue processing.
1187: # 0 - Program should exit
1188: # Implicit outputs:
1189: # The user authorization type is written to the client.
1190: #
1191: sub user_authorization_type {
1192: my ($cmd, $tail, $replyfd) = @_;
1193:
1194: my $userinput = "$cmd:$tail";
1195:
1196: # Pull the domain and username out of the command tail.
1.222 foxr 1197: # and call get_auth_type to determine the authentication type.
1.218 foxr 1198:
1199: my ($udom,$uname)=split(/:/,$tail);
1.222 foxr 1200: my $result = &get_auth_type($udom, $uname);
1.218 foxr 1201: if($result eq "nouser") {
1202: &Failure( $replyfd, "unknown_user\n", $userinput);
1203: } else {
1204: #
1.222 foxr 1205: # We only want to pass the second field from get_auth_type
1.218 foxr 1206: # for ^krb.. otherwise we'll be handing out the encrypted
1207: # password for internals e.g.
1208: #
1209: my ($type,$otherinfo) = split(/:/,$result);
1210: if($type =~ /^krb/) {
1211: $type = $result;
1.269 raeburn 1212: } else {
1213: $type .= ':';
1214: }
1.387 albertel 1215: &Reply( $replyfd, \$type, $userinput);
1.218 foxr 1216: }
1217:
1218: return 1;
1219: }
1220: ®ister_handler("currentauth", \&user_authorization_type, 1, 1, 0);
1221:
1222: # Process a request by a manager to push a hosts or domain table
1223: # to us. We pick apart the command and pass it on to the subs
1224: # that already exist to do this.
1225: #
1226: # Parameters:
1227: # $cmd - the actual keyword that invoked us.
1228: # $tail - the tail of the request that invoked us.
1229: # $client - File descriptor connected to the client
1230: # Returns:
1231: # 1 - Ok to continue processing.
1232: # 0 - Program should exit
1233: # Implicit Output:
1234: # a reply is written to the client.
1235: sub push_file_handler {
1236: my ($cmd, $tail, $client) = @_;
1.412 foxr 1237: &Debug("In push file handler");
1.218 foxr 1238: my $userinput = "$cmd:$tail";
1239:
1240: # At this time we only know that the IP of our partner is a valid manager
1241: # the code below is a hook to do further authentication (e.g. to resolve
1242: # spoofing).
1243:
1244: my $cert = &GetCertificate($userinput);
1.412 foxr 1245: if(&ValidManager($cert)) {
1246: &Debug("Valid manager: $client");
1.218 foxr 1247:
1248: # Now presumably we have the bona fides of both the peer host and the
1249: # process making the request.
1250:
1251: my $reply = &PushFile($userinput);
1.387 albertel 1252: &Reply($client, \$reply, $userinput);
1.218 foxr 1253:
1254: } else {
1.412 foxr 1255: &logthis("push_file_handler $client is not valid");
1.218 foxr 1256: &Failure( $client, "refused\n", $userinput);
1257: }
1.219 foxr 1258: return 1;
1.218 foxr 1259: }
1260: ®ister_handler("pushfile", \&push_file_handler, 1, 0, 1);
1261:
1.399 raeburn 1262: # The du_handler routine should be considered obsolete and is retained
1263: # for communication with legacy servers. Please see the du2_handler.
1.243 banghart 1264: #
1.399 raeburn 1265: # du - list the disk usage of a directory recursively.
1.243 banghart 1266: #
1267: # note: stolen code from the ls file handler
1268: # under construction by Rick Banghart
1269: # .
1270: # Parameters:
1271: # $cmd - The command that dispatched us (du).
1272: # $ududir - The directory path to list... I'm not sure what this
1273: # is relative as things like ls:. return e.g.
1274: # no_such_dir.
1275: # $client - Socket open on the client.
1276: # Returns:
1277: # 1 - indicating that the daemon should not disconnect.
1278: # Side Effects:
1279: # The reply is written to $client.
1280: #
1281: sub du_handler {
1282: my ($cmd, $ududir, $client) = @_;
1.339 albertel 1283: ($ududir) = split(/:/,$ududir); # Make 'telnet' testing easier.
1.251 foxr 1284: my $userinput = "$cmd:$ududir";
1285:
1.245 albertel 1286: if ($ududir=~/\.\./ || $ududir!~m|^/home/httpd/|) {
1287: &Failure($client,"refused\n","$cmd:$ududir");
1288: return 1;
1289: }
1.249 foxr 1290: # Since $ududir could have some nasties in it,
1291: # we will require that ududir is a valid
1292: # directory. Just in case someone tries to
1293: # slip us a line like .;(cd /home/httpd rm -rf*)
1294: # etc.
1295: #
1296: if (-d $ududir) {
1.292 albertel 1297: my $total_size=0;
1298: my $code=sub {
1299: if ($_=~/\.\d+\./) { return;}
1300: if ($_=~/\.meta$/) { return;}
1.362 albertel 1301: if (-d $_) { return;}
1.292 albertel 1302: $total_size+=(stat($_))[7];
1303: };
1.295 raeburn 1304: chdir($ududir);
1.292 albertel 1305: find($code,$ududir);
1306: $total_size=int($total_size/1024);
1.387 albertel 1307: &Reply($client,\$total_size,"$cmd:$ududir");
1.249 foxr 1308: } else {
1.251 foxr 1309: &Failure($client, "bad_directory:$ududir\n","$cmd:$ududir");
1.249 foxr 1310: }
1.243 banghart 1311: return 1;
1312: }
1313: ®ister_handler("du", \&du_handler, 0, 1, 0);
1.218 foxr 1314:
1.399 raeburn 1315: # Please also see the du_handler, which is obsoleted by du2.
1316: # du2_handler differs from du_handler in that required path to directory
1317: # provided by &propath() is prepended in the handler instead of on the
1318: # client side.
1.239 foxr 1319: #
1.399 raeburn 1320: # du2 - list the disk usage of a directory recursively.
1321: #
1322: # Parameters:
1323: # $cmd - The command that dispatched us (du).
1324: # $tail - The tail of the request that invoked us.
1325: # $tail is a : separated list of the following:
1326: # - $ududir - directory path to list (before prepending)
1327: # - $getpropath = 1 if &propath() should prepend
1328: # - $uname - username to use for &propath or user dir
1329: # - $udom - domain to use for &propath or user dir
1330: # All are escaped.
1331: # $client - Socket open on the client.
1332: # Returns:
1333: # 1 - indicating that the daemon should not disconnect.
1334: # Side Effects:
1335: # The reply is written to $client.
1336: #
1337:
1338: sub du2_handler {
1339: my ($cmd, $tail, $client) = @_;
1340: my ($ududir,$getpropath,$uname,$udom) = map { &unescape($_) } (split(/:/, $tail));
1341: my $userinput = "$cmd:$tail";
1342: if (($ududir=~/\.\./) || (($ududir!~m|^/home/httpd/|) && (!$getpropath))) {
1343: &Failure($client,"refused\n","$cmd:$tail");
1344: return 1;
1345: }
1346: if ($getpropath) {
1347: if (($uname =~ /^$LONCAPA::match_name$/) && ($udom =~ /^$LONCAPA::match_domain$/)) {
1348: $ududir = &propath($udom,$uname).'/'.$ududir;
1349: } else {
1350: &Failure($client,"refused\n","$cmd:$tail");
1351: return 1;
1352: }
1353: }
1354: # Since $ududir could have some nasties in it,
1355: # we will require that ududir is a valid
1356: # directory. Just in case someone tries to
1357: # slip us a line like .;(cd /home/httpd rm -rf*)
1358: # etc.
1359: #
1360: if (-d $ududir) {
1361: my $total_size=0;
1362: my $code=sub {
1363: if ($_=~/\.\d+\./) { return;}
1364: if ($_=~/\.meta$/) { return;}
1365: if (-d $_) { return;}
1366: $total_size+=(stat($_))[7];
1367: };
1368: chdir($ududir);
1369: find($code,$ududir);
1370: $total_size=int($total_size/1024);
1371: &Reply($client,\$total_size,"$cmd:$ududir");
1372: } else {
1373: &Failure($client, "bad_directory:$ududir\n","$cmd:$tail");
1374: }
1375: return 1;
1376: }
1377: ®ister_handler("du2", \&du2_handler, 0, 1, 0);
1378:
1379: #
1380: # The ls_handler routine should be considered obsolete and is retained
1381: # for communication with legacy servers. Please see the ls3_handler.
1.280 matthew 1382: #
1.239 foxr 1383: # ls - list the contents of a directory. For each file in the
1384: # selected directory the filename followed by the full output of
1385: # the stat function is returned. The returned info for each
1386: # file are separated by ':'. The stat fields are separated by &'s.
1387: # Parameters:
1388: # $cmd - The command that dispatched us (ls).
1389: # $ulsdir - The directory path to list... I'm not sure what this
1390: # is relative as things like ls:. return e.g.
1391: # no_such_dir.
1392: # $client - Socket open on the client.
1393: # Returns:
1394: # 1 - indicating that the daemon should not disconnect.
1395: # Side Effects:
1396: # The reply is written to $client.
1397: #
1398: sub ls_handler {
1.280 matthew 1399: # obsoleted by ls2_handler
1.239 foxr 1400: my ($cmd, $ulsdir, $client) = @_;
1401:
1402: my $userinput = "$cmd:$ulsdir";
1403:
1404: my $obs;
1405: my $rights;
1406: my $ulsout='';
1407: my $ulsfn;
1408: if (-e $ulsdir) {
1409: if(-d $ulsdir) {
1410: if (opendir(LSDIR,$ulsdir)) {
1411: while ($ulsfn=readdir(LSDIR)) {
1.291 albertel 1412: undef($obs);
1413: undef($rights);
1.239 foxr 1414: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1415: #We do some obsolete checking here
1416: if(-e $ulsdir.'/'.$ulsfn.".meta") {
1417: open(FILE, $ulsdir.'/'.$ulsfn.".meta");
1418: my @obsolete=<FILE>;
1419: foreach my $obsolete (@obsolete) {
1.301 www 1420: if($obsolete =~ m/(<obsolete>)(on|1)/) { $obs = 1; }
1.239 foxr 1421: if($obsolete =~ m|(<copyright>)(default)|) { $rights = 1; }
1422: }
1423: }
1424: $ulsout.=$ulsfn.'&'.join('&',@ulsstats);
1425: if($obs eq '1') { $ulsout.="&1"; }
1426: else { $ulsout.="&0"; }
1427: if($rights eq '1') { $ulsout.="&1:"; }
1428: else { $ulsout.="&0:"; }
1429: }
1430: closedir(LSDIR);
1431: }
1432: } else {
1433: my @ulsstats=stat($ulsdir);
1434: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1435: }
1436: } else {
1437: $ulsout='no_such_dir';
1438: }
1439: if ($ulsout eq '') { $ulsout='empty'; }
1.387 albertel 1440: &Reply($client, \$ulsout, $userinput); # This supports debug logging.
1.239 foxr 1441:
1442: return 1;
1443:
1444: }
1445: ®ister_handler("ls", \&ls_handler, 0, 1, 0);
1446:
1.399 raeburn 1447: # The ls2_handler routine should be considered obsolete and is retained
1448: # for communication with legacy servers. Please see the ls3_handler.
1449: # Please also see the ls_handler, which was itself obsoleted by ls2.
1.280 matthew 1450: # ls2_handler differs from ls_handler in that it escapes its return
1451: # values before concatenating them together with ':'s.
1452: #
1453: # ls2 - list the contents of a directory. For each file in the
1454: # selected directory the filename followed by the full output of
1455: # the stat function is returned. The returned info for each
1456: # file are separated by ':'. The stat fields are separated by &'s.
1457: # Parameters:
1458: # $cmd - The command that dispatched us (ls).
1459: # $ulsdir - The directory path to list... I'm not sure what this
1460: # is relative as things like ls:. return e.g.
1461: # no_such_dir.
1462: # $client - Socket open on the client.
1463: # Returns:
1464: # 1 - indicating that the daemon should not disconnect.
1465: # Side Effects:
1466: # The reply is written to $client.
1467: #
1468: sub ls2_handler {
1469: my ($cmd, $ulsdir, $client) = @_;
1470:
1471: my $userinput = "$cmd:$ulsdir";
1472:
1473: my $obs;
1474: my $rights;
1475: my $ulsout='';
1476: my $ulsfn;
1477: if (-e $ulsdir) {
1478: if(-d $ulsdir) {
1479: if (opendir(LSDIR,$ulsdir)) {
1480: while ($ulsfn=readdir(LSDIR)) {
1.291 albertel 1481: undef($obs);
1482: undef($rights);
1.280 matthew 1483: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1484: #We do some obsolete checking here
1485: if(-e $ulsdir.'/'.$ulsfn.".meta") {
1486: open(FILE, $ulsdir.'/'.$ulsfn.".meta");
1487: my @obsolete=<FILE>;
1488: foreach my $obsolete (@obsolete) {
1.301 www 1489: if($obsolete =~ m/(<obsolete>)(on|1)/) { $obs = 1; }
1.280 matthew 1490: if($obsolete =~ m|(<copyright>)(default)|) {
1491: $rights = 1;
1492: }
1493: }
1494: }
1495: my $tmp = $ulsfn.'&'.join('&',@ulsstats);
1496: if ($obs eq '1') { $tmp.="&1"; } else { $tmp.="&0"; }
1497: if ($rights eq '1') { $tmp.="&1"; } else { $tmp.="&0"; }
1498: $ulsout.= &escape($tmp).':';
1499: }
1500: closedir(LSDIR);
1501: }
1502: } else {
1503: my @ulsstats=stat($ulsdir);
1504: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1505: }
1506: } else {
1507: $ulsout='no_such_dir';
1508: }
1509: if ($ulsout eq '') { $ulsout='empty'; }
1.387 albertel 1510: &Reply($client, \$ulsout, $userinput); # This supports debug logging.
1.280 matthew 1511: return 1;
1512: }
1513: ®ister_handler("ls2", \&ls2_handler, 0, 1, 0);
1.399 raeburn 1514: #
1515: # ls3 - list the contents of a directory. For each file in the
1516: # selected directory the filename followed by the full output of
1517: # the stat function is returned. The returned info for each
1518: # file are separated by ':'. The stat fields are separated by &'s.
1519: # Parameters:
1520: # $cmd - The command that dispatched us (ls).
1521: # $tail - The tail of the request that invoked us.
1522: # $tail is a : separated list of the following:
1523: # - $ulsdir - directory path to list (before prepending)
1524: # - $getpropath = 1 if &propath() should prepend
1525: # - $getuserdir = 1 if path to user dir in lonUsers should
1526: # prepend
1527: # - $alternate_root - path to prepend
1528: # - $uname - username to use for &propath or user dir
1529: # - $udom - domain to use for &propath or user dir
1530: # All of these except $getpropath and &getuserdir are escaped.
1531: # no_such_dir.
1532: # $client - Socket open on the client.
1533: # Returns:
1534: # 1 - indicating that the daemon should not disconnect.
1535: # Side Effects:
1536: # The reply is written to $client.
1537: #
1538:
1539: sub ls3_handler {
1540: my ($cmd, $tail, $client) = @_;
1541: my $userinput = "$cmd:$tail";
1542: my ($ulsdir,$getpropath,$getuserdir,$alternate_root,$uname,$udom) =
1543: split(/:/,$tail);
1544: if (defined($ulsdir)) {
1545: $ulsdir = &unescape($ulsdir);
1546: }
1547: if (defined($alternate_root)) {
1548: $alternate_root = &unescape($alternate_root);
1549: }
1550: if (defined($uname)) {
1551: $uname = &unescape($uname);
1552: }
1553: if (defined($udom)) {
1554: $udom = &unescape($udom);
1555: }
1556:
1557: my $dir_root = $perlvar{'lonDocRoot'};
1558: if ($getpropath) {
1559: if (($uname =~ /^$LONCAPA::match_name$/) && ($udom =~ /^$LONCAPA::match_domain$/)) {
1560: $dir_root = &propath($udom,$uname);
1561: $dir_root =~ s/\/$//;
1562: } else {
1563: &Failure($client,"refused\n","$cmd:$tail");
1564: return 1;
1565: }
1566: } elsif ($getuserdir) {
1567: if (($uname =~ /^$LONCAPA::match_name$/) && ($udom =~ /^$LONCAPA::match_domain$/)) {
1568: my $subdir=$uname.'__';
1569: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
1570: $dir_root = $Apache::lonnet::perlvar{'lonUsersDir'}
1571: ."/$udom/$subdir/$uname";
1572: } else {
1573: &Failure($client,"refused\n","$cmd:$tail");
1574: return 1;
1575: }
1.400 raeburn 1576: } elsif ($alternate_root ne '') {
1.399 raeburn 1577: $dir_root = $alternate_root;
1578: }
1.408 raeburn 1579: if (($dir_root ne '') && ($dir_root ne '/')) {
1.400 raeburn 1580: if ($ulsdir =~ /^\//) {
1581: $ulsdir = $dir_root.$ulsdir;
1582: } else {
1583: $ulsdir = $dir_root.'/'.$ulsdir;
1584: }
1.399 raeburn 1585: }
1586: my $obs;
1587: my $rights;
1588: my $ulsout='';
1589: my $ulsfn;
1590: if (-e $ulsdir) {
1591: if(-d $ulsdir) {
1592: if (opendir(LSDIR,$ulsdir)) {
1593: while ($ulsfn=readdir(LSDIR)) {
1594: undef($obs);
1595: undef($rights);
1596: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1597: #We do some obsolete checking here
1598: if(-e $ulsdir.'/'.$ulsfn.".meta") {
1599: open(FILE, $ulsdir.'/'.$ulsfn.".meta");
1600: my @obsolete=<FILE>;
1601: foreach my $obsolete (@obsolete) {
1602: if($obsolete =~ m/(<obsolete>)(on|1)/) { $obs = 1; }
1603: if($obsolete =~ m|(<copyright>)(default)|) {
1604: $rights = 1;
1605: }
1606: }
1607: }
1608: my $tmp = $ulsfn.'&'.join('&',@ulsstats);
1609: if ($obs eq '1') { $tmp.="&1"; } else { $tmp.="&0"; }
1610: if ($rights eq '1') { $tmp.="&1"; } else { $tmp.="&0"; }
1611: $ulsout.= &escape($tmp).':';
1612: }
1613: closedir(LSDIR);
1614: }
1615: } else {
1616: my @ulsstats=stat($ulsdir);
1617: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1618: }
1619: } else {
1620: $ulsout='no_such_dir';
1.400 raeburn 1621: }
1622: if ($ulsout eq '') { $ulsout='empty'; }
1623: &Reply($client, \$ulsout, $userinput); # This supports debug logging.
1624: return 1;
1.399 raeburn 1625: }
1626: ®ister_handler("ls3", \&ls3_handler, 0, 1, 0);
1.280 matthew 1627:
1.477 raeburn 1628: sub read_lonnet_global {
1629: my ($cmd,$tail,$client) = @_;
1630: my $userinput = "$cmd:$tail";
1631: my $requested = &Apache::lonnet::thaw_unescape($tail);
1632: my $result;
1.480 raeburn 1633: my %packagevars = (
1634: spareid => \%Apache::lonnet::spareid,
1635: perlvar => \%Apache::lonnet::perlvar,
1636: );
1637: my %limit_to = (
1638: perlvar => {
1639: lonOtherAuthen => 1,
1640: lonBalancer => 1,
1641: lonVersion => 1,
1642: lonSysEMail => 1,
1643: lonHostID => 1,
1644: lonRole => 1,
1645: lonDefDomain => 1,
1646: lonLoadLim => 1,
1647: lonUserLoadLim => 1,
1648: }
1649: );
1.477 raeburn 1650: if (ref($requested) eq 'HASH') {
1651: foreach my $what (keys(%{$requested})) {
1652: my $response;
1.480 raeburn 1653: my $items = {};
1654: if (exists($packagevars{$what})) {
1655: if (ref($limit_to{$what}) eq 'HASH') {
1656: foreach my $varname (keys(%{$packagevars{$what}})) {
1657: if ($limit_to{$what}{$varname}) {
1658: $items->{$varname} = $packagevars{$what}{$varname};
1659: }
1660: }
1661: } else {
1662: $items = $packagevars{$what};
1.477 raeburn 1663: }
1.480 raeburn 1664: if ($what eq 'perlvar') {
1665: if (!exists($packagevars{$what}{'lonBalancer'})) {
1666: if ($dist =~ /^(centos|rhes|fedora|scientific)/) {
1667: my $othervarref=LONCAPA::Configuration::read_conf('httpd.conf');
1668: if (ref($othervarref) eq 'HASH') {
1669: $items->{'lonBalancer'} = $othervarref->{'lonBalancer'};
1670: }
1671: }
1672: }
1.477 raeburn 1673: }
1.480 raeburn 1674: $response = &Apache::lonnet::freeze_escape($items);
1.477 raeburn 1675: }
1.478 raeburn 1676: $result .= &escape($what).'='.$response.'&';
1.477 raeburn 1677: }
1678: }
1679: $result =~ s/\&$//;
1680: &Reply($client,\$result,$userinput);
1681: return 1;
1682: }
1683: ®ister_handler("readlonnetglobal", \&read_lonnet_global, 0, 1, 0);
1684:
1.479 raeburn 1685: sub server_devalidatecache_handler {
1686: my ($cmd,$tail,$client) = @_;
1687: my $userinput = "$cmd:$tail";
1688: my ($name,$id) = map { &unescape($_); } split(/:/,$tail);
1689: &Apache::lonnet::devalidate_cache_new($name,$id);
1690: my $result = 'ok';
1691: &Reply($client,\$result,$userinput);
1692: return 1;
1693: }
1.481 raeburn 1694: ®ister_handler("devalidatecache", \&server_devalidatecache_handler, 0, 1, 0);
1.479 raeburn 1695:
1.410 raeburn 1696: sub server_timezone_handler {
1697: my ($cmd,$tail,$client) = @_;
1698: my $userinput = "$cmd:$tail";
1699: my $timezone;
1700: my $clockfile = '/etc/sysconfig/clock'; # Fedora/CentOS/SuSE
1701: my $tzfile = '/etc/timezone'; # Debian/Ubuntu
1702: if (-e $clockfile) {
1703: if (open(my $fh,"<$clockfile")) {
1704: while (<$fh>) {
1705: next if (/^[\#\s]/);
1706: if (/^(?:TIME)?ZONE\s*=\s*['"]?\s*([\w\/]+)/) {
1707: $timezone = $1;
1708: last;
1709: }
1710: }
1711: close($fh);
1712: }
1713: } elsif (-e $tzfile) {
1714: if (open(my $fh,"<$tzfile")) {
1715: $timezone = <$fh>;
1716: close($fh);
1717: chomp($timezone);
1718: if ($timezone =~ m{^Etc/(\w+)$}) {
1719: $timezone = $1;
1720: }
1721: }
1722: }
1723: &Reply($client,\$timezone,$userinput); # This supports debug logging.
1724: return 1;
1725: }
1726: ®ister_handler("servertimezone", \&server_timezone_handler, 0, 1, 0);
1727:
1.413 raeburn 1728: sub server_loncaparev_handler {
1729: my ($cmd,$tail,$client) = @_;
1730: my $userinput = "$cmd:$tail";
1731: &Reply($client,\$perlvar{'lonVersion'},$userinput);
1732: return 1;
1733: }
1734: ®ister_handler("serverloncaparev", \&server_loncaparev_handler, 0, 1, 0);
1735:
1.448 raeburn 1736: sub server_homeID_handler {
1737: my ($cmd,$tail,$client) = @_;
1738: my $userinput = "$cmd:$tail";
1739: &Reply($client,\$perlvar{'lonHostID'},$userinput);
1740: return 1;
1741: }
1742: ®ister_handler("serverhomeID", \&server_homeID_handler, 0, 1, 0);
1743:
1.471 raeburn 1744: sub server_distarch_handler {
1745: my ($cmd,$tail,$client) = @_;
1746: my $userinput = "$cmd:$tail";
1747: my $reply = &distro_and_arch();
1748: &Reply($client,\$reply,$userinput);
1749: return 1;
1750: }
1751: ®ister_handler("serverdistarch", \&server_distarch_handler, 0, 1, 0);
1752:
1.218 foxr 1753: # Process a reinit request. Reinit requests that either
1754: # lonc or lond be reinitialized so that an updated
1755: # host.tab or domain.tab can be processed.
1756: #
1757: # Parameters:
1758: # $cmd - the actual keyword that invoked us.
1759: # $tail - the tail of the request that invoked us.
1760: # $client - File descriptor connected to the client
1761: # Returns:
1762: # 1 - Ok to continue processing.
1763: # 0 - Program should exit
1764: # Implicit output:
1765: # a reply is sent to the client.
1766: #
1767: sub reinit_process_handler {
1768: my ($cmd, $tail, $client) = @_;
1769:
1770: my $userinput = "$cmd:$tail";
1771:
1772: my $cert = &GetCertificate($userinput);
1773: if(&ValidManager($cert)) {
1774: chomp($userinput);
1775: my $reply = &ReinitProcess($userinput);
1.387 albertel 1776: &Reply( $client, \$reply, $userinput);
1.218 foxr 1777: } else {
1778: &Failure( $client, "refused\n", $userinput);
1779: }
1780: return 1;
1781: }
1782: ®ister_handler("reinit", \&reinit_process_handler, 1, 0, 1);
1783:
1784: # Process the editing script for a table edit operation.
1785: # the editing operation must be encrypted and requested by
1786: # a manager host.
1787: #
1788: # Parameters:
1789: # $cmd - the actual keyword that invoked us.
1790: # $tail - the tail of the request that invoked us.
1791: # $client - File descriptor connected to the client
1792: # Returns:
1793: # 1 - Ok to continue processing.
1794: # 0 - Program should exit
1795: # Implicit output:
1796: # a reply is sent to the client.
1797: #
1798: sub edit_table_handler {
1799: my ($command, $tail, $client) = @_;
1800:
1801: my $userinput = "$command:$tail";
1802:
1803: my $cert = &GetCertificate($userinput);
1804: if(&ValidManager($cert)) {
1805: my($filetype, $script) = split(/:/, $tail);
1806: if (($filetype eq "hosts") ||
1807: ($filetype eq "domain")) {
1808: if($script ne "") {
1809: &Reply($client, # BUGBUG - EditFile
1810: &EditFile($userinput), # could fail.
1811: $userinput);
1812: } else {
1813: &Failure($client,"refused\n",$userinput);
1814: }
1815: } else {
1816: &Failure($client,"refused\n",$userinput);
1817: }
1818: } else {
1819: &Failure($client,"refused\n",$userinput);
1820: }
1821: return 1;
1822: }
1.263 albertel 1823: ®ister_handler("edit", \&edit_table_handler, 1, 0, 1);
1.218 foxr 1824:
1.220 foxr 1825: #
1826: # Authenticate a user against the LonCAPA authentication
1827: # database. Note that there are several authentication
1828: # possibilities:
1829: # - unix - The user can be authenticated against the unix
1830: # password file.
1831: # - internal - The user can be authenticated against a purely
1832: # internal per user password file.
1833: # - kerberos - The user can be authenticated against either a kerb4 or kerb5
1834: # ticket granting authority.
1835: # - user - The person tailoring LonCAPA can supply a user authentication
1836: # mechanism that is per system.
1837: #
1838: # Parameters:
1839: # $cmd - The command that got us here.
1840: # $tail - Tail of the command (remaining parameters).
1841: # $client - File descriptor connected to client.
1842: # Returns
1843: # 0 - Requested to exit, caller should shut down.
1844: # 1 - Continue processing.
1845: # Implicit inputs:
1846: # The authentication systems describe above have their own forms of implicit
1847: # input into the authentication process that are described above.
1848: #
1849: sub authenticate_handler {
1850: my ($cmd, $tail, $client) = @_;
1851:
1852:
1853: # Regenerate the full input line
1854:
1855: my $userinput = $cmd.":".$tail;
1856:
1857: # udom - User's domain.
1858: # uname - Username.
1859: # upass - User's password.
1.396 raeburn 1860: # checkdefauth - Pass to validate_user() to try authentication
1861: # with default auth type(s) if no user account.
1.447 raeburn 1862: # clientcancheckhost - Passed by clients with functionality in lonauth.pm
1863: # to check if session can be hosted.
1.220 foxr 1864:
1.447 raeburn 1865: my ($udom, $uname, $upass, $checkdefauth, $clientcancheckhost)=split(/:/,$tail);
1.399 raeburn 1866: &Debug(" Authenticate domain = $udom, user = $uname, password = $upass, checkdefauth = $checkdefauth");
1.220 foxr 1867: chomp($upass);
1868: $upass=&unescape($upass);
1869:
1.396 raeburn 1870: my $pwdcorrect = &validate_user($udom,$uname,$upass,$checkdefauth);
1.220 foxr 1871: if($pwdcorrect) {
1.447 raeburn 1872: my $canhost = 1;
1873: unless ($clientcancheckhost) {
1.448 raeburn 1874: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1875: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.452 raeburn 1876: my @intdoms;
1877: my $internet_names = &Apache::lonnet::get_internet_names($clientname);
1878: if (ref($internet_names) eq 'ARRAY') {
1879: @intdoms = @{$internet_names};
1880: }
1.448 raeburn 1881: unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
1.447 raeburn 1882: my ($remote,$hosted);
1883: my $remotesession = &get_usersession_config($udom,'remotesession');
1884: if (ref($remotesession) eq 'HASH') {
1885: $remote = $remotesession->{'remote'}
1886: }
1.448 raeburn 1887: my $hostedsession = &get_usersession_config($clienthomedom,'hostedsession');
1.447 raeburn 1888: if (ref($hostedsession) eq 'HASH') {
1889: $hosted = $hostedsession->{'hosted'};
1890: }
1.448 raeburn 1891: $canhost = &Apache::lonnet::can_host_session($udom,$clientname,
1.492 droeschl 1892: $clientversion,
1.447 raeburn 1893: $remote,$hosted);
1894: }
1895: }
1896: if ($canhost) {
1897: &Reply( $client, "authorized\n", $userinput);
1898: } else {
1899: &Reply( $client, "not_allowed_to_host\n", $userinput);
1900: }
1.220 foxr 1901: #
1902: # Bad credentials: Failed to authorize
1903: #
1904: } else {
1905: &Failure( $client, "non_authorized\n", $userinput);
1906: }
1907:
1908: return 1;
1909: }
1.263 albertel 1910: ®ister_handler("auth", \&authenticate_handler, 1, 1, 0);
1.214 foxr 1911:
1.222 foxr 1912: #
1913: # Change a user's password. Note that this function is complicated by
1914: # the fact that a user may be authenticated in more than one way:
1915: # At present, we are not able to change the password for all types of
1916: # authentication methods. Only for:
1917: # unix - unix password or shadow passoword style authentication.
1918: # local - Locally written authentication mechanism.
1919: # For now, kerb4 and kerb5 password changes are not supported and result
1920: # in an error.
1921: # FUTURE WORK:
1922: # Support kerberos passwd changes?
1923: # Parameters:
1924: # $cmd - The command that got us here.
1925: # $tail - Tail of the command (remaining parameters).
1926: # $client - File descriptor connected to client.
1927: # Returns
1928: # 0 - Requested to exit, caller should shut down.
1929: # 1 - Continue processing.
1930: # Implicit inputs:
1931: # The authentication systems describe above have their own forms of implicit
1932: # input into the authentication process that are described above.
1933: sub change_password_handler {
1934: my ($cmd, $tail, $client) = @_;
1935:
1936: my $userinput = $cmd.":".$tail; # Reconstruct client's string.
1937:
1938: #
1939: # udom - user's domain.
1940: # uname - Username.
1941: # upass - Current password.
1942: # npass - New password.
1.346 raeburn 1943: # context - Context in which this was called
1944: # (preferences or reset_by_email).
1.428 raeburn 1945: # lonhost - HostID of server where request originated
1.222 foxr 1946:
1.428 raeburn 1947: my ($udom,$uname,$upass,$npass,$context,$lonhost)=split(/:/,$tail);
1.222 foxr 1948:
1949: $upass=&unescape($upass);
1950: $npass=&unescape($npass);
1951: &Debug("Trying to change password for $uname");
1952:
1953: # First require that the user can be authenticated with their
1.346 raeburn 1954: # old password unless context was 'reset_by_email':
1955:
1.428 raeburn 1956: my ($validated,$failure);
1.346 raeburn 1957: if ($context eq 'reset_by_email') {
1.428 raeburn 1958: if ($lonhost eq '') {
1959: $failure = 'invalid_client';
1960: } else {
1961: $validated = 1;
1962: }
1.346 raeburn 1963: } else {
1964: $validated = &validate_user($udom, $uname, $upass);
1965: }
1.222 foxr 1966: if($validated) {
1967: my $realpasswd = &get_auth_type($udom, $uname); # Defined since authd.
1968:
1969: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
1970: if ($howpwd eq 'internal') {
1971: &Debug("internal auth");
1972: my $salt=time;
1973: $salt=substr($salt,6,2);
1974: my $ncpass=crypt($npass,$salt);
1975: if(&rewrite_password_file($udom, $uname, "internal:$ncpass")) {
1.428 raeburn 1976: my $msg="Result of password change for $uname: pwchange_success";
1977: if ($lonhost) {
1978: $msg .= " - request originated from: $lonhost";
1979: }
1980: &logthis($msg);
1.222 foxr 1981: &Reply($client, "ok\n", $userinput);
1982: } else {
1983: &logthis("Unable to open $uname passwd "
1984: ."to change password");
1985: &Failure( $client, "non_authorized\n",$userinput);
1986: }
1.346 raeburn 1987: } elsif ($howpwd eq 'unix' && $context ne 'reset_by_email') {
1.287 foxr 1988: my $result = &change_unix_password($uname, $npass);
1.222 foxr 1989: &logthis("Result of password change for $uname: ".
1.287 foxr 1990: $result);
1.387 albertel 1991: &Reply($client, \$result, $userinput);
1.222 foxr 1992: } else {
1993: # this just means that the current password mode is not
1994: # one we know how to change (e.g the kerberos auth modes or
1995: # locally written auth handler).
1996: #
1997: &Failure( $client, "auth_mode_error\n", $userinput);
1998: }
1999:
1.224 foxr 2000: } else {
1.428 raeburn 2001: if ($failure eq '') {
2002: $failure = 'non_authorized';
2003: }
2004: &Failure( $client, "$failure\n", $userinput);
1.222 foxr 2005: }
2006:
2007: return 1;
2008: }
1.263 albertel 2009: ®ister_handler("passwd", \&change_password_handler, 1, 1, 0);
1.222 foxr 2010:
1.225 foxr 2011: #
2012: # Create a new user. User in this case means a lon-capa user.
2013: # The user must either already exist in some authentication realm
2014: # like kerberos or the /etc/passwd. If not, a user completely local to
2015: # this loncapa system is created.
2016: #
2017: # Parameters:
2018: # $cmd - The command that got us here.
2019: # $tail - Tail of the command (remaining parameters).
2020: # $client - File descriptor connected to client.
2021: # Returns
2022: # 0 - Requested to exit, caller should shut down.
2023: # 1 - Continue processing.
2024: # Implicit inputs:
2025: # The authentication systems describe above have their own forms of implicit
2026: # input into the authentication process that are described above.
2027: sub add_user_handler {
2028:
2029: my ($cmd, $tail, $client) = @_;
2030:
2031:
2032: my ($udom,$uname,$umode,$npass)=split(/:/,$tail);
2033: my $userinput = $cmd.":".$tail; # Reconstruct the full request line.
2034:
2035: &Debug("cmd =".$cmd." $udom =".$udom." uname=".$uname);
2036:
2037:
2038: if($udom eq $currentdomainid) { # Reject new users for other domains...
2039:
2040: my $oldumask=umask(0077);
2041: chomp($npass);
2042: $npass=&unescape($npass);
2043: my $passfilename = &password_path($udom, $uname);
2044: &Debug("Password file created will be:".$passfilename);
2045: if (-e $passfilename) {
2046: &Failure( $client, "already_exists\n", $userinput);
2047: } else {
2048: my $fperror='';
1.264 albertel 2049: if (!&mkpath($passfilename)) {
2050: $fperror="error: ".($!+0)." mkdir failed while attempting "
2051: ."makeuser";
1.225 foxr 2052: }
2053: unless ($fperror) {
1.483 www 2054: my $result=&make_passwd_file($uname,$udom,$umode,$npass, $passfilename);
1.390 raeburn 2055: &Reply($client,\$result, $userinput); #BUGBUG - could be fail
1.225 foxr 2056: } else {
1.387 albertel 2057: &Failure($client, \$fperror, $userinput);
1.225 foxr 2058: }
2059: }
2060: umask($oldumask);
2061: } else {
2062: &Failure($client, "not_right_domain\n",
2063: $userinput); # Even if we are multihomed.
2064:
2065: }
2066: return 1;
2067:
2068: }
2069: ®ister_handler("makeuser", \&add_user_handler, 1, 1, 0);
2070:
2071: #
2072: # Change the authentication method of a user. Note that this may
2073: # also implicitly change the user's password if, for example, the user is
2074: # joining an existing authentication realm. Known authentication realms at
2075: # this time are:
2076: # internal - Purely internal password file (only loncapa knows this user)
2077: # local - Institutionally written authentication module.
2078: # unix - Unix user (/etc/passwd with or without /etc/shadow).
2079: # kerb4 - kerberos version 4
2080: # kerb5 - kerberos version 5
2081: #
2082: # Parameters:
2083: # $cmd - The command that got us here.
2084: # $tail - Tail of the command (remaining parameters).
2085: # $client - File descriptor connected to client.
2086: # Returns
2087: # 0 - Requested to exit, caller should shut down.
2088: # 1 - Continue processing.
2089: # Implicit inputs:
2090: # The authentication systems describe above have their own forms of implicit
2091: # input into the authentication process that are described above.
1.287 foxr 2092: # NOTE:
2093: # This is also used to change the authentication credential values (e.g. passwd).
2094: #
1.225 foxr 2095: #
2096: sub change_authentication_handler {
2097:
2098: my ($cmd, $tail, $client) = @_;
2099:
2100: my $userinput = "$cmd:$tail"; # Reconstruct user input.
2101:
2102: my ($udom,$uname,$umode,$npass)=split(/:/,$tail);
2103: &Debug("cmd = ".$cmd." domain= ".$udom."uname =".$uname." umode= ".$umode);
2104: if ($udom ne $currentdomainid) {
2105: &Failure( $client, "not_right_domain\n", $client);
2106: } else {
2107:
2108: chomp($npass);
2109:
2110: $npass=&unescape($npass);
1.261 foxr 2111: my $oldauth = &get_auth_type($udom, $uname); # Get old auth info.
1.225 foxr 2112: my $passfilename = &password_path($udom, $uname);
2113: if ($passfilename) { # Not allowed to create a new user!!
1.287 foxr 2114: # If just changing the unix passwd. need to arrange to run
1.502 ! raeburn 2115: # passwd since otherwise make_passwd_file will fail as
! 2116: # creation of unix authenticated users is no longer supported
! 2117: # except from the command line, when running make_domain_coordinator.pl
1.287 foxr 2118:
2119: if(($oldauth =~/^unix/) && ($umode eq "unix")) {
2120: my $result = &change_unix_password($uname, $npass);
2121: &logthis("Result of password change for $uname: ".$result);
2122: if ($result eq "ok") {
1.390 raeburn 2123: &Reply($client, \$result);
1.288 albertel 2124: } else {
1.387 albertel 2125: &Failure($client, \$result);
1.287 foxr 2126: }
1.288 albertel 2127: } else {
1.483 www 2128: my $result=&make_passwd_file($uname,$udom,$umode,$npass,$passfilename);
1.287 foxr 2129: #
2130: # If the current auth mode is internal, and the old auth mode was
2131: # unix, or krb*, and the user is an author for this domain,
2132: # re-run manage_permissions for that role in order to be able
2133: # to take ownership of the construction space back to www:www
2134: #
1.502 ! raeburn 2135:
! 2136:
1.387 albertel 2137: &Reply($client, \$result, $userinput);
1.261 foxr 2138: }
2139:
2140:
1.225 foxr 2141: } else {
1.251 foxr 2142: &Failure($client, "non_authorized\n", $userinput); # Fail the user now.
1.225 foxr 2143: }
2144: }
2145: return 1;
2146: }
2147: ®ister_handler("changeuserauth", \&change_authentication_handler, 1,1, 0);
2148:
2149: #
2150: # Determines if this is the home server for a user. The home server
2151: # for a user will have his/her lon-capa passwd file. Therefore all we need
2152: # to do is determine if this file exists.
2153: #
2154: # Parameters:
2155: # $cmd - The command that got us here.
2156: # $tail - Tail of the command (remaining parameters).
2157: # $client - File descriptor connected to client.
2158: # Returns
2159: # 0 - Requested to exit, caller should shut down.
2160: # 1 - Continue processing.
2161: # Implicit inputs:
2162: # The authentication systems describe above have their own forms of implicit
2163: # input into the authentication process that are described above.
2164: #
2165: sub is_home_handler {
2166: my ($cmd, $tail, $client) = @_;
2167:
2168: my $userinput = "$cmd:$tail";
2169:
2170: my ($udom,$uname)=split(/:/,$tail);
2171: chomp($uname);
2172: my $passfile = &password_filename($udom, $uname);
2173: if($passfile) {
2174: &Reply( $client, "found\n", $userinput);
2175: } else {
2176: &Failure($client, "not_found\n", $userinput);
2177: }
2178: return 1;
2179: }
2180: ®ister_handler("home", \&is_home_handler, 0,1,0);
2181:
2182: #
1.434 www 2183: # Process an update request for a resource.
2184: # A resource has been modified that we hold a subscription to.
1.225 foxr 2185: # If the resource is not local, then we must update, or at least invalidate our
2186: # cached copy of the resource.
2187: # Parameters:
2188: # $cmd - The command that got us here.
2189: # $tail - Tail of the command (remaining parameters).
2190: # $client - File descriptor connected to client.
2191: # Returns
2192: # 0 - Requested to exit, caller should shut down.
2193: # 1 - Continue processing.
2194: # Implicit inputs:
2195: # The authentication systems describe above have their own forms of implicit
2196: # input into the authentication process that are described above.
2197: #
2198: sub update_resource_handler {
2199:
2200: my ($cmd, $tail, $client) = @_;
2201:
2202: my $userinput = "$cmd:$tail";
2203:
2204: my $fname= $tail; # This allows interactive testing
2205:
2206:
2207: my $ownership=ishome($fname);
2208: if ($ownership eq 'not_owner') {
2209: if (-e $fname) {
1.434 www 2210: # Delete preview file, if exists
2211: unlink("$fname.tmp");
2212: # Get usage stats
1.225 foxr 2213: my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
2214: $atime,$mtime,$ctime,$blksize,$blocks)=stat($fname);
2215: my $now=time;
2216: my $since=$now-$atime;
1.434 www 2217: # If the file has not been used within lonExpire seconds,
2218: # unsubscribe from it and delete local copy
1.225 foxr 2219: if ($since>$perlvar{'lonExpire'}) {
1.365 albertel 2220: my $reply=&Apache::lonnet::reply("unsub:$fname","$clientname");
1.308 albertel 2221: &devalidate_meta_cache($fname);
1.225 foxr 2222: unlink("$fname");
1.334 albertel 2223: unlink("$fname.meta");
1.225 foxr 2224: } else {
1.434 www 2225: # Yes, this is in active use. Get a fresh copy. Since it might be in
2226: # very active use and huge (like a movie), copy it to "in.transfer" filename first.
1.225 foxr 2227: my $transname="$fname.in.transfer";
1.365 albertel 2228: my $remoteurl=&Apache::lonnet::reply("sub:$fname","$clientname");
1.225 foxr 2229: my $response;
1.455 www 2230: # FIXME: cannot replicate files that take more than two minutes to transfer?
2231: # alarm(120);
2232: # FIXME: this should use the LWP mechanism, not internal alarms.
2233: alarm(1200);
1.225 foxr 2234: {
2235: my $ua=new LWP::UserAgent;
2236: my $request=new HTTP::Request('GET',"$remoteurl");
2237: $response=$ua->request($request,$transname);
2238: }
2239: alarm(0);
2240: if ($response->is_error()) {
1.455 www 2241: # FIXME: we should probably clean up here instead of just whine
1.225 foxr 2242: unlink($transname);
2243: my $message=$response->status_line;
2244: &logthis("LWP GET: $message for $fname ($remoteurl)");
2245: } else {
2246: if ($remoteurl!~/\.meta$/) {
1.455 www 2247: # FIXME: isn't there an internal LWP mechanism for this?
1.225 foxr 2248: alarm(120);
2249: {
2250: my $ua=new LWP::UserAgent;
2251: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
2252: my $mresponse=$ua->request($mrequest,$fname.'.meta');
2253: if ($mresponse->is_error()) {
2254: unlink($fname.'.meta');
2255: }
2256: }
2257: alarm(0);
2258: }
1.434 www 2259: # we successfully transfered, copy file over to real name
1.225 foxr 2260: rename($transname,$fname);
1.308 albertel 2261: &devalidate_meta_cache($fname);
1.225 foxr 2262: }
2263: }
2264: &Reply( $client, "ok\n", $userinput);
2265: } else {
2266: &Failure($client, "not_found\n", $userinput);
2267: }
2268: } else {
2269: &Failure($client, "rejected\n", $userinput);
2270: }
2271: return 1;
2272: }
2273: ®ister_handler("update", \&update_resource_handler, 0 ,1, 0);
2274:
1.308 albertel 2275: sub devalidate_meta_cache {
2276: my ($url) = @_;
2277: use Cache::Memcached;
2278: my $memcache = new Cache::Memcached({'servers'=>['127.0.0.1:11211']});
1.365 albertel 2279: $url = &Apache::lonnet::declutter($url);
1.308 albertel 2280: $url =~ s-\.meta$--;
2281: my $id = &escape('meta:'.$url);
2282: $memcache->delete($id);
2283: }
2284:
1.225 foxr 2285: #
1.226 foxr 2286: # Fetch a user file from a remote server to the user's home directory
2287: # userfiles subdir.
1.225 foxr 2288: # Parameters:
2289: # $cmd - The command that got us here.
2290: # $tail - Tail of the command (remaining parameters).
2291: # $client - File descriptor connected to client.
2292: # Returns
2293: # 0 - Requested to exit, caller should shut down.
2294: # 1 - Continue processing.
2295: #
2296: sub fetch_user_file_handler {
2297:
2298: my ($cmd, $tail, $client) = @_;
2299:
2300: my $userinput = "$cmd:$tail";
2301: my $fname = $tail;
1.232 foxr 2302: my ($udom,$uname,$ufile) = ($fname =~ m|^([^/]+)/([^/]+)/(.+)$|);
1.225 foxr 2303: my $udir=&propath($udom,$uname).'/userfiles';
2304: unless (-e $udir) {
2305: mkdir($udir,0770);
2306: }
1.232 foxr 2307: Debug("fetch user file for $fname");
1.225 foxr 2308: if (-e $udir) {
2309: $ufile=~s/^[\.\~]+//;
1.232 foxr 2310:
2311: # IF necessary, create the path right down to the file.
2312: # Note that any regular files in the way of this path are
2313: # wiped out to deal with some earlier folly of mine.
2314:
1.267 raeburn 2315: if (!&mkpath($udir.'/'.$ufile)) {
1.264 albertel 2316: &Failure($client, "unable_to_create\n", $userinput);
1.232 foxr 2317: }
2318:
1.225 foxr 2319: my $destname=$udir.'/'.$ufile;
2320: my $transname=$udir.'/'.$ufile.'.in.transit';
1.476 raeburn 2321: my $clientprotocol=$Apache::lonnet::protocol{$clientname};
2322: $clientprotocol = 'http' if ($clientprotocol ne 'https');
1.486 raeburn 2323: my $clienthost = &Apache::lonnet::hostname($clientname);
2324: my $remoteurl=$clientprotocol.'://'.$clienthost.'/userfiles/'.$fname;
1.225 foxr 2325: my $response;
1.232 foxr 2326: Debug("Remote URL : $remoteurl Transfername $transname Destname: $destname");
1.225 foxr 2327: alarm(120);
2328: {
2329: my $ua=new LWP::UserAgent;
2330: my $request=new HTTP::Request('GET',"$remoteurl");
2331: $response=$ua->request($request,$transname);
2332: }
2333: alarm(0);
2334: if ($response->is_error()) {
2335: unlink($transname);
2336: my $message=$response->status_line;
2337: &logthis("LWP GET: $message for $fname ($remoteurl)");
2338: &Failure($client, "failed\n", $userinput);
2339: } else {
1.232 foxr 2340: Debug("Renaming $transname to $destname");
1.225 foxr 2341: if (!rename($transname,$destname)) {
2342: &logthis("Unable to move $transname to $destname");
2343: unlink($transname);
2344: &Failure($client, "failed\n", $userinput);
2345: } else {
1.495 raeburn 2346: if ($fname =~ /^default.+\.(page|sequence)$/) {
2347: my ($major,$minor) = split(/\./,$clientversion);
2348: if (($major < 2) || ($major == 2 && $minor < 11)) {
2349: my $now = time;
2350: &Apache::lonnet::do_cache_new('crschange',$udom.'_'.$uname,$now,600);
2351: my $key = &escape('internal.contentchange');
2352: my $what = "$key=$now";
2353: my $hashref = &tie_user_hash($udom,$uname,'environment',
2354: &GDBM_WRCREAT(),"P",$what);
2355: if ($hashref) {
2356: $hashref->{$key}=$now;
2357: if (!&untie_user_hash($hashref)) {
2358: &logthis("error: ".($!+0)." untie (GDBM) failed ".
2359: "when updating internal.contentchange");
2360: }
2361: }
2362: }
2363: }
1.225 foxr 2364: &Reply($client, "ok\n", $userinput);
2365: }
2366: }
2367: } else {
2368: &Failure($client, "not_home\n", $userinput);
2369: }
2370: return 1;
2371: }
2372: ®ister_handler("fetchuserfile", \&fetch_user_file_handler, 0, 1, 0);
2373:
1.226 foxr 2374: #
2375: # Remove a file from a user's home directory userfiles subdirectory.
2376: # Parameters:
2377: # cmd - the Lond request keyword that got us here.
2378: # tail - the part of the command past the keyword.
2379: # client- File descriptor connected with the client.
2380: #
2381: # Returns:
2382: # 1 - Continue processing.
2383: sub remove_user_file_handler {
2384: my ($cmd, $tail, $client) = @_;
2385:
2386: my ($fname) = split(/:/, $tail); # Get rid of any tailing :'s lonc may have sent.
2387:
2388: my ($udom,$uname,$ufile) = ($fname =~ m|^([^/]+)/([^/]+)/(.+)$|);
2389: if ($ufile =~m|/\.\./|) {
2390: # any files paths with /../ in them refuse
2391: # to deal with
2392: &Failure($client, "refused\n", "$cmd:$tail");
2393: } else {
2394: my $udir = &propath($udom,$uname);
2395: if (-e $udir) {
2396: my $file=$udir.'/userfiles/'.$ufile;
2397: if (-e $file) {
1.253 foxr 2398: #
2399: # If the file is a regular file unlink is fine...
2400: # However it's possible the client wants a dir.
2401: # removed, in which case rmdir is more approprate:
2402: #
1.240 banghart 2403: if (-f $file){
1.241 albertel 2404: unlink($file);
2405: } elsif(-d $file) {
2406: rmdir($file);
1.240 banghart 2407: }
1.226 foxr 2408: if (-e $file) {
1.253 foxr 2409: # File is still there after we deleted it ?!?
2410:
1.226 foxr 2411: &Failure($client, "failed\n", "$cmd:$tail");
2412: } else {
2413: &Reply($client, "ok\n", "$cmd:$tail");
2414: }
2415: } else {
2416: &Failure($client, "not_found\n", "$cmd:$tail");
2417: }
2418: } else {
2419: &Failure($client, "not_home\n", "$cmd:$tail");
2420: }
2421: }
2422: return 1;
2423: }
2424: ®ister_handler("removeuserfile", \&remove_user_file_handler, 0,1,0);
2425:
1.236 albertel 2426: #
2427: # make a directory in a user's home directory userfiles subdirectory.
2428: # Parameters:
2429: # cmd - the Lond request keyword that got us here.
2430: # tail - the part of the command past the keyword.
2431: # client- File descriptor connected with the client.
2432: #
2433: # Returns:
2434: # 1 - Continue processing.
2435: sub mkdir_user_file_handler {
2436: my ($cmd, $tail, $client) = @_;
2437:
2438: my ($dir) = split(/:/, $tail); # Get rid of any tailing :'s lonc may have sent.
2439: $dir=&unescape($dir);
2440: my ($udom,$uname,$ufile) = ($dir =~ m|^([^/]+)/([^/]+)/(.+)$|);
2441: if ($ufile =~m|/\.\./|) {
2442: # any files paths with /../ in them refuse
2443: # to deal with
2444: &Failure($client, "refused\n", "$cmd:$tail");
2445: } else {
2446: my $udir = &propath($udom,$uname);
2447: if (-e $udir) {
1.264 albertel 2448: my $newdir=$udir.'/userfiles/'.$ufile.'/';
2449: if (!&mkpath($newdir)) {
2450: &Failure($client, "failed\n", "$cmd:$tail");
1.236 albertel 2451: }
1.264 albertel 2452: &Reply($client, "ok\n", "$cmd:$tail");
1.236 albertel 2453: } else {
2454: &Failure($client, "not_home\n", "$cmd:$tail");
2455: }
2456: }
2457: return 1;
2458: }
2459: ®ister_handler("mkdiruserfile", \&mkdir_user_file_handler, 0,1,0);
2460:
1.237 albertel 2461: #
2462: # rename a file in a user's home directory userfiles subdirectory.
2463: # Parameters:
2464: # cmd - the Lond request keyword that got us here.
2465: # tail - the part of the command past the keyword.
2466: # client- File descriptor connected with the client.
2467: #
2468: # Returns:
2469: # 1 - Continue processing.
2470: sub rename_user_file_handler {
2471: my ($cmd, $tail, $client) = @_;
2472:
2473: my ($udom,$uname,$old,$new) = split(/:/, $tail);
2474: $old=&unescape($old);
2475: $new=&unescape($new);
2476: if ($new =~m|/\.\./| || $old =~m|/\.\./|) {
2477: # any files paths with /../ in them refuse to deal with
2478: &Failure($client, "refused\n", "$cmd:$tail");
2479: } else {
2480: my $udir = &propath($udom,$uname);
2481: if (-e $udir) {
2482: my $oldfile=$udir.'/userfiles/'.$old;
2483: my $newfile=$udir.'/userfiles/'.$new;
2484: if (-e $newfile) {
2485: &Failure($client, "exists\n", "$cmd:$tail");
2486: } elsif (! -e $oldfile) {
2487: &Failure($client, "not_found\n", "$cmd:$tail");
2488: } else {
2489: if (!rename($oldfile,$newfile)) {
2490: &Failure($client, "failed\n", "$cmd:$tail");
2491: } else {
2492: &Reply($client, "ok\n", "$cmd:$tail");
2493: }
2494: }
2495: } else {
2496: &Failure($client, "not_home\n", "$cmd:$tail");
2497: }
2498: }
2499: return 1;
2500: }
2501: ®ister_handler("renameuserfile", \&rename_user_file_handler, 0,1,0);
2502:
1.227 foxr 2503: #
1.382 albertel 2504: # Checks if the specified user has an active session on the server
2505: # return ok if so, not_found if not
2506: #
2507: # Parameters:
2508: # cmd - The request keyword that dispatched to tus.
2509: # tail - The tail of the request (colon separated parameters).
2510: # client - Filehandle open on the client.
2511: # Return:
2512: # 1.
2513: sub user_has_session_handler {
2514: my ($cmd, $tail, $client) = @_;
2515:
2516: my ($udom, $uname) = map { &unescape($_) } (split(/:/, $tail));
2517:
2518: opendir(DIR,$perlvar{'lonIDsDir'});
2519: my $filename;
2520: while ($filename=readdir(DIR)) {
2521: last if ($filename=~/^\Q$uname\E_\d+_\Q$udom\E_/);
2522: }
2523: if ($filename) {
2524: &Reply($client, "ok\n", "$cmd:$tail");
2525: } else {
2526: &Failure($client, "not_found\n", "$cmd:$tail");
2527: }
2528: return 1;
2529:
2530: }
2531: ®ister_handler("userhassession", \&user_has_session_handler, 0,1,0);
2532:
2533: #
1.263 albertel 2534: # Authenticate access to a user file by checking that the token the user's
2535: # passed also exists in their session file
1.227 foxr 2536: #
2537: # Parameters:
2538: # cmd - The request keyword that dispatched to tus.
2539: # tail - The tail of the request (colon separated parameters).
2540: # client - Filehandle open on the client.
2541: # Return:
2542: # 1.
2543: sub token_auth_user_file_handler {
2544: my ($cmd, $tail, $client) = @_;
2545:
2546: my ($fname, $session) = split(/:/, $tail);
2547:
2548: chomp($session);
1.393 raeburn 2549: my $reply="non_auth";
1.343 albertel 2550: my $file = $perlvar{'lonIDsDir'}.'/'.$session.'.id';
2551: if (open(ENVIN,"$file")) {
1.332 albertel 2552: flock(ENVIN,LOCK_SH);
1.343 albertel 2553: tie(my %disk_env,'GDBM_File',"$file",&GDBM_READER(),0640);
2554: if (exists($disk_env{"userfile.$fname"})) {
1.393 raeburn 2555: $reply="ok";
1.343 albertel 2556: } else {
2557: foreach my $envname (keys(%disk_env)) {
2558: if ($envname=~ m|^userfile\.\Q$fname\E|) {
1.393 raeburn 2559: $reply="ok";
1.343 albertel 2560: last;
2561: }
2562: }
1.227 foxr 2563: }
1.343 albertel 2564: untie(%disk_env);
1.227 foxr 2565: close(ENVIN);
1.387 albertel 2566: &Reply($client, \$reply, "$cmd:$tail");
1.227 foxr 2567: } else {
2568: &Failure($client, "invalid_token\n", "$cmd:$tail");
2569: }
2570: return 1;
2571:
2572: }
2573: ®ister_handler("tokenauthuserfile", \&token_auth_user_file_handler, 0,1,0);
1.229 foxr 2574:
2575: #
2576: # Unsubscribe from a resource.
2577: #
2578: # Parameters:
2579: # $cmd - The command that got us here.
2580: # $tail - Tail of the command (remaining parameters).
2581: # $client - File descriptor connected to client.
2582: # Returns
2583: # 0 - Requested to exit, caller should shut down.
2584: # 1 - Continue processing.
2585: #
2586: sub unsubscribe_handler {
2587: my ($cmd, $tail, $client) = @_;
2588:
2589: my $userinput= "$cmd:$tail";
2590:
2591: my ($fname) = split(/:/,$tail); # Split in case there's extrs.
2592:
2593: &Debug("Unsubscribing $fname");
2594: if (-e $fname) {
2595: &Debug("Exists");
2596: &Reply($client, &unsub($fname,$clientip), $userinput);
2597: } else {
2598: &Failure($client, "not_found\n", $userinput);
2599: }
2600: return 1;
2601: }
2602: ®ister_handler("unsub", \&unsubscribe_handler, 0, 1, 0);
1.263 albertel 2603:
1.230 foxr 2604: # Subscribe to a resource
2605: #
2606: # Parameters:
2607: # $cmd - The command that got us here.
2608: # $tail - Tail of the command (remaining parameters).
2609: # $client - File descriptor connected to client.
2610: # Returns
2611: # 0 - Requested to exit, caller should shut down.
2612: # 1 - Continue processing.
2613: #
2614: sub subscribe_handler {
2615: my ($cmd, $tail, $client)= @_;
2616:
2617: my $userinput = "$cmd:$tail";
2618:
2619: &Reply( $client, &subscribe($userinput,$clientip), $userinput);
2620:
2621: return 1;
2622: }
2623: ®ister_handler("sub", \&subscribe_handler, 0, 1, 0);
2624:
2625: #
1.379 albertel 2626: # Determine the latest version of a resource (it looks for the highest
2627: # past version and then returns that +1)
1.230 foxr 2628: #
2629: # Parameters:
2630: # $cmd - The command that got us here.
2631: # $tail - Tail of the command (remaining parameters).
1.379 albertel 2632: # (Should consist of an absolute path to a file)
1.230 foxr 2633: # $client - File descriptor connected to client.
2634: # Returns
2635: # 0 - Requested to exit, caller should shut down.
2636: # 1 - Continue processing.
2637: #
2638: sub current_version_handler {
2639: my ($cmd, $tail, $client) = @_;
2640:
2641: my $userinput= "$cmd:$tail";
2642:
2643: my $fname = $tail;
2644: &Reply( $client, ¤tversion($fname)."\n", $userinput);
2645: return 1;
2646:
2647: }
2648: ®ister_handler("currentversion", \¤t_version_handler, 0, 1, 0);
2649:
2650: # Make an entry in a user's activity log.
2651: #
2652: # Parameters:
2653: # $cmd - The command that got us here.
2654: # $tail - Tail of the command (remaining parameters).
2655: # $client - File descriptor connected to client.
2656: # Returns
2657: # 0 - Requested to exit, caller should shut down.
2658: # 1 - Continue processing.
2659: #
2660: sub activity_log_handler {
2661: my ($cmd, $tail, $client) = @_;
2662:
2663:
2664: my $userinput= "$cmd:$tail";
2665:
2666: my ($udom,$uname,$what)=split(/:/,$tail);
2667: chomp($what);
2668: my $proname=&propath($udom,$uname);
2669: my $now=time;
2670: my $hfh;
2671: if ($hfh=IO::File->new(">>$proname/activity.log")) {
2672: print $hfh "$now:$clientname:$what\n";
2673: &Reply( $client, "ok\n", $userinput);
2674: } else {
2675: &Failure($client, "error: ".($!+0)." IO::File->new Failed "
2676: ."while attempting log\n",
2677: $userinput);
2678: }
2679:
2680: return 1;
2681: }
1.263 albertel 2682: ®ister_handler("log", \&activity_log_handler, 0, 1, 0);
1.230 foxr 2683:
2684: #
2685: # Put a namespace entry in a user profile hash.
2686: # My druthers would be for this to be an encrypted interaction too.
2687: # anything that might be an inadvertent covert channel about either
2688: # user authentication or user personal information....
2689: #
2690: # Parameters:
2691: # $cmd - The command that got us here.
2692: # $tail - Tail of the command (remaining parameters).
2693: # $client - File descriptor connected to client.
2694: # Returns
2695: # 0 - Requested to exit, caller should shut down.
2696: # 1 - Continue processing.
2697: #
2698: sub put_user_profile_entry {
2699: my ($cmd, $tail, $client) = @_;
1.229 foxr 2700:
1.230 foxr 2701: my $userinput = "$cmd:$tail";
2702:
1.242 raeburn 2703: my ($udom,$uname,$namespace,$what) =split(/:/,$tail,4);
1.230 foxr 2704: if ($namespace ne 'roles') {
2705: chomp($what);
2706: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2707: &GDBM_WRCREAT(),"P",$what);
2708: if($hashref) {
2709: my @pairs=split(/\&/,$what);
2710: foreach my $pair (@pairs) {
2711: my ($key,$value)=split(/=/,$pair);
2712: $hashref->{$key}=$value;
2713: }
1.311 albertel 2714: if (&untie_user_hash($hashref)) {
1.230 foxr 2715: &Reply( $client, "ok\n", $userinput);
2716: } else {
2717: &Failure($client, "error: ".($!+0)." untie(GDBM) failed ".
2718: "while attempting put\n",
2719: $userinput);
2720: }
2721: } else {
1.316 albertel 2722: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
1.230 foxr 2723: "while attempting put\n", $userinput);
2724: }
2725: } else {
2726: &Failure( $client, "refused\n", $userinput);
2727: }
2728:
2729: return 1;
2730: }
2731: ®ister_handler("put", \&put_user_profile_entry, 0, 1, 0);
2732:
1.283 albertel 2733: # Put a piece of new data in hash, returns error if entry already exists
2734: # Parameters:
2735: # $cmd - The command that got us here.
2736: # $tail - Tail of the command (remaining parameters).
2737: # $client - File descriptor connected to client.
2738: # Returns
2739: # 0 - Requested to exit, caller should shut down.
2740: # 1 - Continue processing.
2741: #
2742: sub newput_user_profile_entry {
2743: my ($cmd, $tail, $client) = @_;
2744:
2745: my $userinput = "$cmd:$tail";
2746:
2747: my ($udom,$uname,$namespace,$what) =split(/:/,$tail,4);
2748: if ($namespace eq 'roles') {
2749: &Failure( $client, "refused\n", $userinput);
2750: return 1;
2751: }
2752:
2753: chomp($what);
2754:
2755: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2756: &GDBM_WRCREAT(),"N",$what);
2757: if(!$hashref) {
1.316 albertel 2758: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
1.283 albertel 2759: "while attempting put\n", $userinput);
2760: return 1;
2761: }
2762:
2763: my @pairs=split(/\&/,$what);
2764: foreach my $pair (@pairs) {
2765: my ($key,$value)=split(/=/,$pair);
2766: if (exists($hashref->{$key})) {
2767: &Failure($client, "key_exists: ".$key."\n",$userinput);
2768: return 1;
2769: }
2770: }
2771:
2772: foreach my $pair (@pairs) {
2773: my ($key,$value)=split(/=/,$pair);
2774: $hashref->{$key}=$value;
2775: }
2776:
1.311 albertel 2777: if (&untie_user_hash($hashref)) {
1.283 albertel 2778: &Reply( $client, "ok\n", $userinput);
2779: } else {
2780: &Failure($client, "error: ".($!+0)." untie(GDBM) failed ".
2781: "while attempting put\n",
2782: $userinput);
2783: }
2784: return 1;
2785: }
2786: ®ister_handler("newput", \&newput_user_profile_entry, 0, 1, 0);
2787:
1.230 foxr 2788: #
2789: # Increment a profile entry in the user history file.
2790: # The history contains keyword value pairs. In this case,
2791: # The value itself is a pair of numbers. The first, the current value
2792: # the second an increment that this function applies to the current
2793: # value.
2794: #
2795: # Parameters:
2796: # $cmd - The command that got us here.
2797: # $tail - Tail of the command (remaining parameters).
2798: # $client - File descriptor connected to client.
2799: # Returns
2800: # 0 - Requested to exit, caller should shut down.
2801: # 1 - Continue processing.
2802: #
2803: sub increment_user_value_handler {
2804: my ($cmd, $tail, $client) = @_;
2805:
2806: my $userinput = "$cmd:$tail";
2807:
2808: my ($udom,$uname,$namespace,$what) =split(/:/,$tail);
2809: if ($namespace ne 'roles') {
2810: chomp($what);
2811: my $hashref = &tie_user_hash($udom, $uname,
2812: $namespace, &GDBM_WRCREAT(),
2813: "P",$what);
2814: if ($hashref) {
2815: my @pairs=split(/\&/,$what);
2816: foreach my $pair (@pairs) {
2817: my ($key,$value)=split(/=/,$pair);
1.284 raeburn 2818: $value = &unescape($value);
1.230 foxr 2819: # We could check that we have a number...
2820: if (! defined($value) || $value eq '') {
2821: $value = 1;
2822: }
2823: $hashref->{$key}+=$value;
1.284 raeburn 2824: if ($namespace eq 'nohist_resourcetracker') {
2825: if ($hashref->{$key} < 0) {
2826: $hashref->{$key} = 0;
2827: }
2828: }
1.230 foxr 2829: }
1.311 albertel 2830: if (&untie_user_hash($hashref)) {
1.230 foxr 2831: &Reply( $client, "ok\n", $userinput);
2832: } else {
2833: &Failure($client, "error: ".($!+0)." untie(GDBM) failed ".
2834: "while attempting inc\n", $userinput);
2835: }
2836: } else {
2837: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
2838: "while attempting inc\n", $userinput);
2839: }
2840: } else {
2841: &Failure($client, "refused\n", $userinput);
2842: }
2843:
2844: return 1;
2845: }
2846: ®ister_handler("inc", \&increment_user_value_handler, 0, 1, 0);
2847:
2848: #
2849: # Put a new role for a user. Roles are LonCAPA's packaging of permissions.
2850: # Each 'role' a user has implies a set of permissions. Adding a new role
2851: # for a person grants the permissions packaged with that role
2852: # to that user when the role is selected.
2853: #
2854: # Parameters:
2855: # $cmd - The command string (rolesput).
2856: # $tail - The remainder of the request line. For rolesput this
2857: # consists of a colon separated list that contains:
2858: # The domain and user that is granting the role (logged).
2859: # The domain and user that is getting the role.
2860: # The roles being granted as a set of & separated pairs.
2861: # each pair a key value pair.
2862: # $client - File descriptor connected to the client.
2863: # Returns:
2864: # 0 - If the daemon should exit
2865: # 1 - To continue processing.
2866: #
2867: #
2868: sub roles_put_handler {
2869: my ($cmd, $tail, $client) = @_;
2870:
2871: my $userinput = "$cmd:$tail";
2872:
2873: my ( $exedom, $exeuser, $udom, $uname, $what) = split(/:/,$tail);
2874:
2875:
2876: my $namespace='roles';
2877: chomp($what);
2878: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2879: &GDBM_WRCREAT(), "P",
2880: "$exedom:$exeuser:$what");
2881: #
2882: # Log the attempt to set a role. The {}'s here ensure that the file
2883: # handle is open for the minimal amount of time. Since the flush
2884: # is done on close this improves the chances the log will be an un-
2885: # corrupted ordered thing.
2886: if ($hashref) {
1.261 foxr 2887: my $pass_entry = &get_auth_type($udom, $uname);
2888: my ($auth_type,$pwd) = split(/:/, $pass_entry);
2889: $auth_type = $auth_type.":";
1.230 foxr 2890: my @pairs=split(/\&/,$what);
2891: foreach my $pair (@pairs) {
2892: my ($key,$value)=split(/=/,$pair);
2893: &manage_permissions($key, $udom, $uname,
1.260 foxr 2894: $auth_type);
1.230 foxr 2895: $hashref->{$key}=$value;
2896: }
1.311 albertel 2897: if (&untie_user_hash($hashref)) {
1.230 foxr 2898: &Reply($client, "ok\n", $userinput);
2899: } else {
2900: &Failure( $client, "error: ".($!+0)." untie(GDBM) Failed ".
2901: "while attempting rolesput\n", $userinput);
2902: }
2903: } else {
2904: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
2905: "while attempting rolesput\n", $userinput);
2906: }
2907: return 1;
2908: }
2909: ®ister_handler("rolesput", \&roles_put_handler, 1,1,0); # Encoded client only.
2910:
2911: #
1.231 foxr 2912: # Deletes (removes) a role for a user. This is equivalent to removing
2913: # a permissions package associated with the role from the user's profile.
2914: #
2915: # Parameters:
2916: # $cmd - The command (rolesdel)
2917: # $tail - The remainder of the request line. This consists
2918: # of:
2919: # The domain and user requesting the change (logged)
2920: # The domain and user being changed.
2921: # The roles being revoked. These are shipped to us
2922: # as a bunch of & separated role name keywords.
2923: # $client - The file handle open on the client.
2924: # Returns:
2925: # 1 - Continue processing
2926: # 0 - Exit.
2927: #
2928: sub roles_delete_handler {
2929: my ($cmd, $tail, $client) = @_;
2930:
2931: my $userinput = "$cmd:$tail";
2932:
2933: my ($exedom,$exeuser,$udom,$uname,$what)=split(/:/,$tail);
2934: &Debug("cmd = ".$cmd." exedom= ".$exedom."user = ".$exeuser." udom=".$udom.
2935: "what = ".$what);
2936: my $namespace='roles';
2937: chomp($what);
2938: my $hashref = &tie_user_hash($udom, $uname, $namespace,
2939: &GDBM_WRCREAT(), "D",
2940: "$exedom:$exeuser:$what");
2941:
2942: if ($hashref) {
2943: my @rolekeys=split(/\&/,$what);
2944:
2945: foreach my $key (@rolekeys) {
2946: delete $hashref->{$key};
2947: }
1.315 albertel 2948: if (&untie_user_hash($hashref)) {
1.231 foxr 2949: &Reply($client, "ok\n", $userinput);
2950: } else {
2951: &Failure( $client, "error: ".($!+0)." untie(GDBM) Failed ".
2952: "while attempting rolesdel\n", $userinput);
2953: }
2954: } else {
2955: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
2956: "while attempting rolesdel\n", $userinput);
2957: }
2958:
2959: return 1;
2960: }
2961: ®ister_handler("rolesdel", \&roles_delete_handler, 1,1, 0); # Encoded client only
2962:
2963: # Unencrypted get from a user's profile database. See
2964: # GetProfileEntryEncrypted for a version that does end-to-end encryption.
2965: # This function retrieves a keyed item from a specific named database in the
2966: # user's directory.
2967: #
2968: # Parameters:
2969: # $cmd - Command request keyword (get).
2970: # $tail - Tail of the command. This is a colon separated list
2971: # consisting of the domain and username that uniquely
2972: # identifies the profile,
2973: # The 'namespace' which selects the gdbm file to
2974: # do the lookup in,
2975: # & separated list of keys to lookup. Note that
2976: # the values are returned as an & separated list too.
2977: # $client - File descriptor open on the client.
2978: # Returns:
2979: # 1 - Continue processing.
2980: # 0 - Exit.
2981: #
2982: sub get_profile_entry {
2983: my ($cmd, $tail, $client) = @_;
2984:
2985: my $userinput= "$cmd:$tail";
2986:
2987: my ($udom,$uname,$namespace,$what) = split(/:/,$tail);
2988: chomp($what);
1.255 foxr 2989:
1.390 raeburn 2990:
1.255 foxr 2991: my $replystring = read_profile($udom, $uname, $namespace, $what);
2992: my ($first) = split(/:/,$replystring);
2993: if($first ne "error") {
1.387 albertel 2994: &Reply($client, \$replystring, $userinput);
1.231 foxr 2995: } else {
1.255 foxr 2996: &Failure($client, $replystring." while attempting get\n", $userinput);
1.231 foxr 2997: }
2998: return 1;
1.255 foxr 2999:
3000:
1.231 foxr 3001: }
3002: ®ister_handler("get", \&get_profile_entry, 0,1,0);
3003:
3004: #
3005: # Process the encrypted get request. Note that the request is sent
3006: # in clear, but the reply is encrypted. This is a small covert channel:
3007: # information about the sensitive keys is given to the snooper. Just not
3008: # information about the values of the sensitive key. Hmm if I wanted to
3009: # know these I'd snoop for the egets. Get the profile item names from them
3010: # and then issue a get for them since there's no enforcement of the
3011: # requirement of an encrypted get for particular profile items. If I
3012: # were re-doing this, I'd force the request to be encrypted as well as the
3013: # reply. I'd also just enforce encrypted transactions for all gets since
3014: # that would prevent any covert channel snooping.
3015: #
3016: # Parameters:
3017: # $cmd - Command keyword of request (eget).
3018: # $tail - Tail of the command. See GetProfileEntry
# for more information about this.
3019: # $client - File open on the client.
3020: # Returns:
3021: # 1 - Continue processing
3022: # 0 - server should exit.
3023: sub get_profile_entry_encrypted {
3024: my ($cmd, $tail, $client) = @_;
3025:
3026: my $userinput = "$cmd:$tail";
3027:
1.339 albertel 3028: my ($udom,$uname,$namespace,$what) = split(/:/,$tail);
1.231 foxr 3029: chomp($what);
1.255 foxr 3030: my $qresult = read_profile($udom, $uname, $namespace, $what);
3031: my ($first) = split(/:/, $qresult);
3032: if($first ne "error") {
3033:
3034: if ($cipher) {
3035: my $cmdlength=length($qresult);
3036: $qresult.=" ";
3037: my $encqresult='';
3038: for(my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
3039: $encqresult.= unpack("H16",
3040: $cipher->encrypt(substr($qresult,
3041: $encidx,
3042: 8)));
3043: }
3044: &Reply( $client, "enc:$cmdlength:$encqresult\n", $userinput);
3045: } else {
1.231 foxr 3046: &Failure( $client, "error:no_key\n", $userinput);
3047: }
3048: } else {
1.255 foxr 3049: &Failure($client, "$qresult while attempting eget\n", $userinput);
3050:
1.231 foxr 3051: }
3052:
3053: return 1;
3054: }
1.255 foxr 3055: ®ister_handler("eget", \&get_profile_entry_encrypted, 0, 1, 0);
1.263 albertel 3056:
1.231 foxr 3057: #
3058: # Deletes a key in a user profile database.
3059: #
3060: # Parameters:
3061: # $cmd - Command keyword (del).
3062: # $tail - Command tail. IN this case a colon
3063: # separated list containing:
3064: # The domain and user that identifies uniquely
3065: # the identity of the user.
3066: # The profile namespace (name of the profile
3067: # database file).
3068: # & separated list of keywords to delete.
3069: # $client - File open on client socket.
3070: # Returns:
3071: # 1 - Continue processing
3072: # 0 - Exit server.
3073: #
3074: #
3075: sub delete_profile_entry {
3076: my ($cmd, $tail, $client) = @_;
3077:
3078: my $userinput = "cmd:$tail";
3079:
3080: my ($udom,$uname,$namespace,$what) = split(/:/,$tail);
3081: chomp($what);
3082: my $hashref = &tie_user_hash($udom, $uname, $namespace,
3083: &GDBM_WRCREAT(),
3084: "D",$what);
3085: if ($hashref) {
3086: my @keys=split(/\&/,$what);
3087: foreach my $key (@keys) {
3088: delete($hashref->{$key});
3089: }
1.315 albertel 3090: if (&untie_user_hash($hashref)) {
1.231 foxr 3091: &Reply($client, "ok\n", $userinput);
3092: } else {
3093: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
3094: "while attempting del\n", $userinput);
3095: }
3096: } else {
3097: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
3098: "while attempting del\n", $userinput);
3099: }
3100: return 1;
3101: }
3102: ®ister_handler("del", \&delete_profile_entry, 0, 1, 0);
1.263 albertel 3103:
1.231 foxr 3104: #
3105: # List the set of keys that are defined in a profile database file.
3106: # A successful reply from this will contain an & separated list of
3107: # the keys.
3108: # Parameters:
3109: # $cmd - Command request (keys).
3110: # $tail - Remainder of the request, a colon separated
3111: # list containing domain/user that identifies the
3112: # user being queried, and the database namespace
3113: # (database filename essentially).
3114: # $client - File open on the client.
3115: # Returns:
3116: # 1 - Continue processing.
3117: # 0 - Exit the server.
3118: #
3119: sub get_profile_keys {
3120: my ($cmd, $tail, $client) = @_;
3121:
3122: my $userinput = "$cmd:$tail";
3123:
3124: my ($udom,$uname,$namespace)=split(/:/,$tail);
3125: my $qresult='';
3126: my $hashref = &tie_user_hash($udom, $uname, $namespace,
3127: &GDBM_READER());
3128: if ($hashref) {
3129: foreach my $key (keys %$hashref) {
3130: $qresult.="$key&";
3131: }
1.315 albertel 3132: if (&untie_user_hash($hashref)) {
1.231 foxr 3133: $qresult=~s/\&$//;
1.387 albertel 3134: &Reply($client, \$qresult, $userinput);
1.231 foxr 3135: } else {
3136: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
3137: "while attempting keys\n", $userinput);
3138: }
3139: } else {
3140: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
3141: "while attempting keys\n", $userinput);
3142: }
3143:
3144: return 1;
3145: }
3146: ®ister_handler("keys", \&get_profile_keys, 0, 1, 0);
3147:
3148: #
3149: # Dump the contents of a user profile database.
3150: # Note that this constitutes a very large covert channel too since
3151: # the dump will return sensitive information that is not encrypted.
3152: # The naive security assumption is that the session negotiation ensures
3153: # our client is trusted and I don't believe that's assured at present.
3154: # Sure want badly to go to ssl or tls. Of course if my peer isn't really
3155: # a LonCAPA node they could have negotiated an encryption key too so >sigh<.
3156: #
3157: # Parameters:
3158: # $cmd - The command request keyword (currentdump).
3159: # $tail - Remainder of the request, consisting of a colon
3160: # separated list that has the domain/username and
3161: # the namespace to dump (database file).
3162: # $client - file open on the remote client.
3163: # Returns:
3164: # 1 - Continue processing.
3165: # 0 - Exit the server.
3166: #
3167: sub dump_profile_database {
3168: my ($cmd, $tail, $client) = @_;
3169:
1.494 droeschl 3170: my $res = LONCAPA::Lond::dump_profile_database($tail);
3171:
3172: if ($res =~ /^error:/) {
3173: Failure($client, \$res, "$cmd:$tail");
3174: } else {
3175: Reply($client, \$res, "$cmd:$tail");
3176: }
3177:
3178: return 1;
3179:
3180: #TODO remove
1.231 foxr 3181: my $userinput = "$cmd:$tail";
3182:
3183: my ($udom,$uname,$namespace) = split(/:/,$tail);
3184: my $hashref = &tie_user_hash($udom, $uname, $namespace,
3185: &GDBM_READER());
3186: if ($hashref) {
3187: # Structure of %data:
3188: # $data{$symb}->{$parameter}=$value;
3189: # $data{$symb}->{'v.'.$parameter}=$version;
3190: # since $parameter will be unescaped, we do not
3191: # have to worry about silly parameter names...
3192:
3193: my $qresult='';
3194: my %data = (); # A hash of anonymous hashes..
3195: while (my ($key,$value) = each(%$hashref)) {
3196: my ($v,$symb,$param) = split(/:/,$key);
3197: next if ($v eq 'version' || $symb eq 'keys');
3198: next if (exists($data{$symb}) &&
3199: exists($data{$symb}->{$param}) &&
3200: $data{$symb}->{'v.'.$param} > $v);
3201: $data{$symb}->{$param}=$value;
3202: $data{$symb}->{'v.'.$param}=$v;
3203: }
1.311 albertel 3204: if (&untie_user_hash($hashref)) {
1.231 foxr 3205: while (my ($symb,$param_hash) = each(%data)) {
3206: while(my ($param,$value) = each (%$param_hash)){
3207: next if ($param =~ /^v\./); # Ignore versions...
3208: #
3209: # Just dump the symb=value pairs separated by &
3210: #
3211: $qresult.=$symb.':'.$param.'='.$value.'&';
3212: }
3213: }
3214: chop($qresult);
1.387 albertel 3215: &Reply($client , \$qresult, $userinput);
1.231 foxr 3216: } else {
3217: &Failure( $client, "error: ".($!+0)." untie(GDBM) Failed ".
3218: "while attempting currentdump\n", $userinput);
3219: }
3220: } else {
3221: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
3222: "while attempting currentdump\n", $userinput);
3223: }
3224:
3225: return 1;
3226: }
3227: ®ister_handler("currentdump", \&dump_profile_database, 0, 1, 0);
3228:
3229: #
3230: # Dump a profile database with an optional regular expression
3231: # to match against the keys. In this dump, no effort is made
3232: # to separate symb from version information. Presumably the
3233: # databases that are dumped by this command are of a different
3234: # structure. Need to look at this and improve the documentation of
3235: # both this and the currentdump handler.
3236: # Parameters:
3237: # $cmd - The command keyword.
3238: # $tail - All of the characters after the $cmd:
3239: # These are expected to be a colon
3240: # separated list containing:
3241: # domain/user - identifying the user.
3242: # namespace - identifying the database.
3243: # regexp - optional regular expression
3244: # that is matched against
3245: # database keywords to do
3246: # selective dumps.
1.488 raeburn 3247: # range - optional range of entries
3248: # e.g., 10-20 would return the
3249: # 10th to 19th items, etc.
1.231 foxr 3250: # $client - Channel open on the client.
3251: # Returns:
3252: # 1 - Continue processing.
3253: # Side effects:
3254: # response is written to $client.
3255: #
3256: sub dump_with_regexp {
3257: my ($cmd, $tail, $client) = @_;
3258:
1.494 droeschl 3259: my $res = LONCAPA::Lond::dump_with_regexp($tail, $clientversion);
1.490 droeschl 3260:
3261: if ($res =~ /^error:/) {
3262: Failure($client, \$res, "$cmd:$tail");
3263: } else {
3264: Reply($client, \$res, "$cmd:$tail");
3265: }
1.231 foxr 3266:
3267: return 1;
3268: }
3269: ®ister_handler("dump", \&dump_with_regexp, 0, 1, 0);
3270:
3271: # Store a set of key=value pairs associated with a versioned name.
3272: #
3273: # Parameters:
3274: # $cmd - Request command keyword.
3275: # $tail - Tail of the request. This is a colon
3276: # separated list containing:
3277: # domain/user - User and authentication domain.
3278: # namespace - Name of the database being modified
3279: # rid - Resource keyword to modify.
3280: # what - new value associated with rid.
3281: #
3282: # $client - Socket open on the client.
3283: #
3284: #
3285: # Returns:
3286: # 1 (keep on processing).
3287: # Side-Effects:
3288: # Writes to the client
3289: sub store_handler {
3290: my ($cmd, $tail, $client) = @_;
3291:
3292: my $userinput = "$cmd:$tail";
3293:
3294: my ($udom,$uname,$namespace,$rid,$what) =split(/:/,$tail);
3295: if ($namespace ne 'roles') {
3296:
3297: chomp($what);
3298: my @pairs=split(/\&/,$what);
3299: my $hashref = &tie_user_hash($udom, $uname, $namespace,
1.268 albertel 3300: &GDBM_WRCREAT(), "S",
1.231 foxr 3301: "$rid:$what");
3302: if ($hashref) {
3303: my $now = time;
3304: my @previouskeys=split(/&/,$hashref->{"keys:$rid"});
3305: my $key;
3306: $hashref->{"version:$rid"}++;
3307: my $version=$hashref->{"version:$rid"};
3308: my $allkeys='';
3309: foreach my $pair (@pairs) {
3310: my ($key,$value)=split(/=/,$pair);
3311: $allkeys.=$key.':';
3312: $hashref->{"$version:$rid:$key"}=$value;
3313: }
3314: $hashref->{"$version:$rid:timestamp"}=$now;
3315: $allkeys.='timestamp';
3316: $hashref->{"$version:keys:$rid"}=$allkeys;
1.311 albertel 3317: if (&untie_user_hash($hashref)) {
1.231 foxr 3318: &Reply($client, "ok\n", $userinput);
3319: } else {
3320: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
3321: "while attempting store\n", $userinput);
3322: }
3323: } else {
3324: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
3325: "while attempting store\n", $userinput);
3326: }
3327: } else {
3328: &Failure($client, "refused\n", $userinput);
3329: }
3330:
3331: return 1;
3332: }
3333: ®ister_handler("store", \&store_handler, 0, 1, 0);
1.263 albertel 3334:
1.323 albertel 3335: # Modify a set of key=value pairs associated with a versioned name.
3336: #
3337: # Parameters:
3338: # $cmd - Request command keyword.
3339: # $tail - Tail of the request. This is a colon
3340: # separated list containing:
3341: # domain/user - User and authentication domain.
3342: # namespace - Name of the database being modified
3343: # rid - Resource keyword to modify.
3344: # v - Version item to modify
3345: # what - new value associated with rid.
3346: #
3347: # $client - Socket open on the client.
3348: #
3349: #
3350: # Returns:
3351: # 1 (keep on processing).
3352: # Side-Effects:
3353: # Writes to the client
3354: sub putstore_handler {
3355: my ($cmd, $tail, $client) = @_;
3356:
3357: my $userinput = "$cmd:$tail";
3358:
3359: my ($udom,$uname,$namespace,$rid,$v,$what) =split(/:/,$tail);
3360: if ($namespace ne 'roles') {
3361:
3362: chomp($what);
3363: my $hashref = &tie_user_hash($udom, $uname, $namespace,
3364: &GDBM_WRCREAT(), "M",
3365: "$rid:$v:$what");
3366: if ($hashref) {
3367: my $now = time;
3368: my %data = &hash_extract($what);
3369: my @allkeys;
3370: while (my($key,$value) = each(%data)) {
3371: push(@allkeys,$key);
3372: $hashref->{"$v:$rid:$key"} = $value;
3373: }
3374: my $allkeys = join(':',@allkeys);
3375: $hashref->{"$v:keys:$rid"}=$allkeys;
3376:
3377: if (&untie_user_hash($hashref)) {
3378: &Reply($client, "ok\n", $userinput);
3379: } else {
3380: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
3381: "while attempting store\n", $userinput);
3382: }
3383: } else {
3384: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
3385: "while attempting store\n", $userinput);
3386: }
3387: } else {
3388: &Failure($client, "refused\n", $userinput);
3389: }
3390:
3391: return 1;
3392: }
3393: ®ister_handler("putstore", \&putstore_handler, 0, 1, 0);
3394:
3395: sub hash_extract {
3396: my ($str)=@_;
3397: my %hash;
3398: foreach my $pair (split(/\&/,$str)) {
3399: my ($key,$value)=split(/=/,$pair);
3400: $hash{$key}=$value;
3401: }
3402: return (%hash);
3403: }
3404: sub hash_to_str {
3405: my ($hash_ref)=@_;
3406: my $str;
3407: foreach my $key (keys(%$hash_ref)) {
3408: $str.=$key.'='.$hash_ref->{$key}.'&';
3409: }
3410: $str=~s/\&$//;
3411: return $str;
3412: }
3413:
1.231 foxr 3414: #
3415: # Dump out all versions of a resource that has key=value pairs associated
3416: # with it for each version. These resources are built up via the store
3417: # command.
3418: #
3419: # Parameters:
3420: # $cmd - Command keyword.
3421: # $tail - Remainder of the request which consists of:
3422: # domain/user - User and auth. domain.
3423: # namespace - name of resource database.
3424: # rid - Resource id.
3425: # $client - socket open on the client.
3426: #
3427: # Returns:
3428: # 1 indicating the caller should not yet exit.
3429: # Side-effects:
3430: # Writes a reply to the client.
3431: # The reply is a string of the following shape:
3432: # version=current&version:keys=k1:k2...&1:k1=v1&1:k2=v2...
3433: # Where the 1 above represents version 1.
3434: # this continues for all pairs of keys in all versions.
3435: #
3436: #
3437: #
3438: #
3439: sub restore_handler {
3440: my ($cmd, $tail, $client) = @_;
3441:
3442: my $userinput = "$cmd:$tail"; # Only used for logging purposes.
1.351 banghart 3443: my ($udom,$uname,$namespace,$rid) = split(/:/,$tail);
1.352 albertel 3444: $namespace=~s/\//\_/g;
1.350 albertel 3445: $namespace = &LONCAPA::clean_username($namespace);
1.349 albertel 3446:
1.231 foxr 3447: chomp($rid);
3448: my $qresult='';
1.309 albertel 3449: my $hashref = &tie_user_hash($udom, $uname, $namespace, &GDBM_READER());
3450: if ($hashref) {
3451: my $version=$hashref->{"version:$rid"};
1.231 foxr 3452: $qresult.="version=$version&";
3453: my $scope;
3454: for ($scope=1;$scope<=$version;$scope++) {
1.309 albertel 3455: my $vkeys=$hashref->{"$scope:keys:$rid"};
1.231 foxr 3456: my @keys=split(/:/,$vkeys);
3457: my $key;
3458: $qresult.="$scope:keys=$vkeys&";
3459: foreach $key (@keys) {
1.309 albertel 3460: $qresult.="$scope:$key=".$hashref->{"$scope:$rid:$key"}."&";
1.231 foxr 3461: }
3462: }
1.311 albertel 3463: if (&untie_user_hash($hashref)) {
1.231 foxr 3464: $qresult=~s/\&$//;
1.387 albertel 3465: &Reply( $client, \$qresult, $userinput);
1.231 foxr 3466: } else {
3467: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
3468: "while attempting restore\n", $userinput);
3469: }
3470: } else {
3471: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
3472: "while attempting restore\n", $userinput);
3473: }
3474:
3475: return 1;
3476:
3477:
3478: }
3479: ®ister_handler("restore", \&restore_handler, 0,1,0);
1.234 foxr 3480:
3481: #
1.324 raeburn 3482: # Add a chat message to a synchronous discussion board.
1.234 foxr 3483: #
3484: # Parameters:
3485: # $cmd - Request keyword.
3486: # $tail - Tail of the command. A colon separated list
3487: # containing:
3488: # cdom - Domain on which the chat board lives
1.324 raeburn 3489: # cnum - Course containing the chat board.
3490: # newpost - Body of the posting.
3491: # group - Optional group, if chat board is only
3492: # accessible in a group within the course
1.234 foxr 3493: # $client - Socket open on the client.
3494: # Returns:
3495: # 1 - Indicating caller should keep on processing.
3496: #
3497: # Side-effects:
3498: # writes a reply to the client.
3499: #
3500: #
3501: sub send_chat_handler {
3502: my ($cmd, $tail, $client) = @_;
3503:
3504:
3505: my $userinput = "$cmd:$tail";
3506:
1.324 raeburn 3507: my ($cdom,$cnum,$newpost,$group)=split(/\:/,$tail);
3508: &chat_add($cdom,$cnum,$newpost,$group);
1.234 foxr 3509: &Reply($client, "ok\n", $userinput);
3510:
3511: return 1;
3512: }
3513: ®ister_handler("chatsend", \&send_chat_handler, 0, 1, 0);
1.263 albertel 3514:
1.234 foxr 3515: #
1.324 raeburn 3516: # Retrieve the set of chat messages from a discussion board.
1.234 foxr 3517: #
3518: # Parameters:
3519: # $cmd - Command keyword that initiated the request.
3520: # $tail - Remainder of the request after the command
3521: # keyword. In this case a colon separated list of
3522: # chat domain - Which discussion board.
3523: # chat id - Discussion thread(?)
3524: # domain/user - Authentication domain and username
3525: # of the requesting person.
1.324 raeburn 3526: # group - Optional course group containing
3527: # the board.
1.234 foxr 3528: # $client - Socket open on the client program.
3529: # Returns:
3530: # 1 - continue processing
3531: # Side effects:
3532: # Response is written to the client.
3533: #
3534: sub retrieve_chat_handler {
3535: my ($cmd, $tail, $client) = @_;
3536:
3537:
3538: my $userinput = "$cmd:$tail";
3539:
1.324 raeburn 3540: my ($cdom,$cnum,$udom,$uname,$group)=split(/\:/,$tail);
1.234 foxr 3541: my $reply='';
1.324 raeburn 3542: foreach (&get_chat($cdom,$cnum,$udom,$uname,$group)) {
1.234 foxr 3543: $reply.=&escape($_).':';
3544: }
3545: $reply=~s/\:$//;
1.387 albertel 3546: &Reply($client, \$reply, $userinput);
1.234 foxr 3547:
3548:
3549: return 1;
3550: }
3551: ®ister_handler("chatretr", \&retrieve_chat_handler, 0, 1, 0);
3552:
3553: #
3554: # Initiate a query of an sql database. SQL query repsonses get put in
3555: # a file for later retrieval. This prevents sql query results from
3556: # bottlenecking the system. Note that with loncnew, perhaps this is
3557: # less of an issue since multiple outstanding requests can be concurrently
3558: # serviced.
3559: #
3560: # Parameters:
3561: # $cmd - COmmand keyword that initiated the request.
3562: # $tail - Remainder of the command after the keyword.
3563: # For this function, this consists of a query and
3564: # 3 arguments that are self-documentingly labelled
3565: # in the original arg1, arg2, arg3.
3566: # $client - Socket open on the client.
3567: # Return:
3568: # 1 - Indicating processing should continue.
3569: # Side-effects:
3570: # a reply is written to $client.
3571: #
3572: sub send_query_handler {
3573: my ($cmd, $tail, $client) = @_;
3574:
3575:
3576: my $userinput = "$cmd:$tail";
3577:
3578: my ($query,$arg1,$arg2,$arg3)=split(/\:/,$tail);
3579: $query=~s/\n*$//g;
3580: &Reply($client, "". &sql_reply("$clientname\&$query".
3581: "\&$arg1"."\&$arg2"."\&$arg3")."\n",
3582: $userinput);
3583:
3584: return 1;
3585: }
3586: ®ister_handler("querysend", \&send_query_handler, 0, 1, 0);
3587:
3588: #
3589: # Add a reply to an sql query. SQL queries are done asyncrhonously.
3590: # The query is submitted via a "querysend" transaction.
3591: # There it is passed on to the lonsql daemon, queued and issued to
3592: # mysql.
3593: # This transaction is invoked when the sql transaction is complete
3594: # it stores the query results in flie and indicates query completion.
3595: # presumably local software then fetches this response... I'm guessing
3596: # the sequence is: lonc does a querysend, we ask lonsql to do it.
3597: # lonsql on completion of the query interacts with the lond of our
3598: # client to do a query reply storing two files:
3599: # - id - The results of the query.
3600: # - id.end - Indicating the transaction completed.
3601: # NOTE: id is a unique id assigned to the query and querysend time.
3602: # Parameters:
3603: # $cmd - Command keyword that initiated this request.
3604: # $tail - Remainder of the tail. In this case that's a colon
3605: # separated list containing the query Id and the
3606: # results of the query.
3607: # $client - Socket open on the client.
3608: # Return:
3609: # 1 - Indicating that we should continue processing.
3610: # Side effects:
3611: # ok written to the client.
3612: #
3613: sub reply_query_handler {
3614: my ($cmd, $tail, $client) = @_;
3615:
3616:
3617: my $userinput = "$cmd:$tail";
3618:
1.339 albertel 3619: my ($id,$reply)=split(/:/,$tail);
1.234 foxr 3620: my $store;
3621: my $execdir=$perlvar{'lonDaemons'};
3622: if ($store=IO::File->new(">$execdir/tmp/$id")) {
3623: $reply=~s/\&/\n/g;
3624: print $store $reply;
3625: close $store;
3626: my $store2=IO::File->new(">$execdir/tmp/$id.end");
3627: print $store2 "done\n";
3628: close $store2;
3629: &Reply($client, "ok\n", $userinput);
3630: } else {
3631: &Failure($client, "error: ".($!+0)
3632: ." IO::File->new Failed ".
3633: "while attempting queryreply\n", $userinput);
3634: }
3635:
3636:
3637: return 1;
3638: }
3639: ®ister_handler("queryreply", \&reply_query_handler, 0, 1, 0);
3640:
3641: #
3642: # Process the courseidput request. Not quite sure what this means
3643: # at the system level sense. It appears a gdbm file in the
3644: # /home/httpd/lonUsers/$domain/nohist_courseids is tied and
3645: # a set of entries made in that database.
3646: #
3647: # Parameters:
3648: # $cmd - The command keyword that initiated this request.
3649: # $tail - Tail of the command. In this case consists of a colon
3650: # separated list contaning the domain to apply this to and
3651: # an ampersand separated list of keyword=value pairs.
1.272 raeburn 3652: # Each value is a colon separated list that includes:
3653: # description, institutional code and course owner.
3654: # For backward compatibility with versions included
3655: # in LON-CAPA 1.1.X (and earlier) and 1.2.X, institutional
3656: # code and/or course owner are preserved from the existing
3657: # record when writing a new record in response to 1.1 or
3658: # 1.2 implementations of lonnet::flushcourselogs().
3659: #
1.234 foxr 3660: # $client - Socket open on the client.
3661: # Returns:
3662: # 1 - indicating that processing should continue
3663: #
3664: # Side effects:
3665: # reply is written to the client.
3666: #
3667: sub put_course_id_handler {
3668: my ($cmd, $tail, $client) = @_;
3669:
3670:
3671: my $userinput = "$cmd:$tail";
3672:
1.266 raeburn 3673: my ($udom, $what) = split(/:/, $tail,2);
1.234 foxr 3674: chomp($what);
3675: my $now=time;
3676: my @pairs=split(/\&/,$what);
3677:
3678: my $hashref = &tie_domain_hash($udom, "nohist_courseids", &GDBM_WRCREAT());
3679: if ($hashref) {
3680: foreach my $pair (@pairs) {
1.271 raeburn 3681: my ($key,$courseinfo) = split(/=/,$pair,2);
3682: $courseinfo =~ s/=/:/g;
1.384 raeburn 3683: if (defined($hashref->{$key})) {
3684: my $value = &Apache::lonnet::thaw_unescape($hashref->{$key});
3685: if (ref($value) eq 'HASH') {
3686: my @items = ('description','inst_code','owner','type');
3687: my @new_items = split(/:/,$courseinfo,-1);
3688: my %storehash;
3689: for (my $i=0; $i<@new_items; $i++) {
1.391 raeburn 3690: $storehash{$items[$i]} = &unescape($new_items[$i]);
1.384 raeburn 3691: }
3692: $hashref->{$key} =
3693: &Apache::lonnet::freeze_escape(\%storehash);
3694: my $unesc_key = &unescape($key);
3695: $hashref->{&escape('lasttime:'.$unesc_key)} = $now;
3696: next;
1.383 raeburn 3697: }
1.384 raeburn 3698: }
3699: my @current_items = split(/:/,$hashref->{$key},-1);
3700: shift(@current_items); # remove description
3701: pop(@current_items); # remove last access
3702: my $numcurrent = scalar(@current_items);
3703: if ($numcurrent > 3) {
3704: $numcurrent = 3;
3705: }
3706: my @new_items = split(/:/,$courseinfo,-1);
3707: my $numnew = scalar(@new_items);
3708: if ($numcurrent > 0) {
3709: if ($numnew <= $numcurrent) { # flushcourselogs() from pre 2.2
3710: for (my $j=$numcurrent-$numnew; $j>=0; $j--) {
3711: $courseinfo .= ':'.$current_items[$numcurrent-$j-1];
1.333 raeburn 3712: }
1.272 raeburn 3713: }
3714: }
1.384 raeburn 3715: $hashref->{$key}=$courseinfo.':'.$now;
1.234 foxr 3716: }
1.311 albertel 3717: if (&untie_domain_hash($hashref)) {
1.253 foxr 3718: &Reply( $client, "ok\n", $userinput);
1.234 foxr 3719: } else {
1.253 foxr 3720: &Failure($client, "error: ".($!+0)
1.234 foxr 3721: ." untie(GDBM) Failed ".
3722: "while attempting courseidput\n", $userinput);
3723: }
3724: } else {
1.253 foxr 3725: &Failure($client, "error: ".($!+0)
1.234 foxr 3726: ." tie(GDBM) Failed ".
3727: "while attempting courseidput\n", $userinput);
3728: }
3729:
3730: return 1;
3731: }
3732: ®ister_handler("courseidput", \&put_course_id_handler, 0, 1, 0);
3733:
1.383 raeburn 3734: sub put_course_id_hash_handler {
3735: my ($cmd, $tail, $client) = @_;
3736: my $userinput = "$cmd:$tail";
1.384 raeburn 3737: my ($udom,$mode,$what) = split(/:/, $tail,3);
1.383 raeburn 3738: chomp($what);
3739: my $now=time;
3740: my @pairs=split(/\&/,$what);
1.384 raeburn 3741: my $hashref = &tie_domain_hash($udom, "nohist_courseids", &GDBM_WRCREAT());
1.383 raeburn 3742: if ($hashref) {
3743: foreach my $pair (@pairs) {
3744: my ($key,$value)=split(/=/,$pair);
1.384 raeburn 3745: my $unesc_key = &unescape($key);
3746: if ($mode ne 'timeonly') {
3747: if (!defined($hashref->{&escape('lasttime:'.$unesc_key)})) {
3748: my $curritems = &Apache::lonnet::thaw_unescape($key);
3749: if (ref($curritems) ne 'HASH') {
3750: my @current_items = split(/:/,$hashref->{$key},-1);
3751: my $lasttime = pop(@current_items);
3752: $hashref->{&escape('lasttime:'.$unesc_key)} = $lasttime;
3753: } else {
3754: $hashref->{&escape('lasttime:'.$unesc_key)} = '';
3755: }
3756: }
3757: $hashref->{$key} = $value;
3758: }
3759: if ($mode ne 'notime') {
3760: $hashref->{&escape('lasttime:'.$unesc_key)} = $now;
3761: }
1.383 raeburn 3762: }
3763: if (&untie_domain_hash($hashref)) {
3764: &Reply($client, "ok\n", $userinput);
3765: } else {
3766: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
3767: "while attempting courseidputhash\n", $userinput);
3768: }
3769: } else {
3770: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
3771: "while attempting courseidputhash\n", $userinput);
3772: }
3773: return 1;
3774: }
3775: ®ister_handler("courseidputhash", \&put_course_id_hash_handler, 0, 1, 0);
3776:
1.234 foxr 3777: # Retrieves the value of a course id resource keyword pattern
3778: # defined since a starting date. Both the starting date and the
3779: # keyword pattern are optional. If the starting date is not supplied it
3780: # is treated as the beginning of time. If the pattern is not found,
3781: # it is treatred as "." matching everything.
3782: #
3783: # Parameters:
3784: # $cmd - Command keyword that resulted in us being dispatched.
3785: # $tail - The remainder of the command that, in this case, consists
3786: # of a colon separated list of:
3787: # domain - The domain in which the course database is
3788: # defined.
3789: # since - Optional parameter describing the minimum
3790: # time of definition(?) of the resources that
3791: # will match the dump.
3792: # description - regular expression that is used to filter
3793: # the dump. Only keywords matching this regexp
3794: # will be used.
1.272 raeburn 3795: # institutional code - optional supplied code to filter
3796: # the dump. Only courses with an institutional code
3797: # that match the supplied code will be returned.
1.336 raeburn 3798: # owner - optional supplied username and domain of owner to
3799: # filter the dump. Only courses for which the course
3800: # owner matches the supplied username and/or domain
3801: # will be returned. Pre-2.2.0 legacy entries from
3802: # nohist_courseiddump will only contain usernames.
1.384 raeburn 3803: # type - optional parameter for selection
1.418 raeburn 3804: # regexp_ok - if 1 or -1 allow the supplied institutional code
3805: # filter to behave as a regular expression:
3806: # 1 will not exclude the course if the instcode matches the RE
3807: # -1 will exclude the course if the instcode matches the RE
1.384 raeburn 3808: # rtn_as_hash - whether to return the information available for
3809: # each matched item as a frozen hash of all
3810: # key, value pairs in the item's hash, or as a
3811: # colon-separated list of (in order) description,
3812: # institutional code, and course owner.
1.404 raeburn 3813: # selfenrollonly - filter by courses allowing self-enrollment
3814: # now or in the future (selfenrollonly = 1).
3815: # catfilter - filter by course category, assigned to a course
3816: # using manually defined categories (i.e., not
1.407 raeburn 3817: # self-cataloging based on on institutional code).
1.404 raeburn 3818: # showhidden - include course in results even if course
1.407 raeburn 3819: # was set to be excluded from course catalog (DC only).
1.404 raeburn 3820: # caller - if set to 'coursecatalog', courses set to be hidden
3821: # from course catalog will be excluded from results (unless
3822: # overridden by "showhidden".
1.427 raeburn 3823: # cloner - escaped username:domain of course cloner (if picking course to
1.419 raeburn 3824: # clone).
3825: # cc_clone_list - escaped comma separated list of courses for which
3826: # course cloner has active CC role (and so can clone
3827: # automatically).
1.427 raeburn 3828: # cloneonly - filter by courses for which cloner has rights to clone.
3829: # createdbefore - include courses for which creation date preceeded this date.
3830: # createdafter - include courses for which creation date followed this date.
3831: # creationcontext - include courses created in specified context
1.404 raeburn 3832: #
1.445 raeburn 3833: # domcloner - flag to indicate if user can create CCs in course's domain.
3834: # If so, ability to clone course is automatic.
3835: #
1.234 foxr 3836: # $client - The socket open on the client.
3837: # Returns:
3838: # 1 - Continue processing.
3839: # Side Effects:
3840: # a reply is written to $client.
3841: sub dump_course_id_handler {
3842: my ($cmd, $tail, $client) = @_;
1.494 droeschl 3843:
3844: my $res = LONCAPA::Lond::dump_course_id_handler($tail);
3845: if ($res =~ /^error:/) {
3846: Failure($client, \$res, "$cmd:$tail");
3847: } else {
3848: Reply($client, \$res, "$cmd:$tail");
3849: }
3850:
3851: return 1;
3852:
3853: #TODO remove
1.234 foxr 3854: my $userinput = "$cmd:$tail";
3855:
1.333 raeburn 3856: my ($udom,$since,$description,$instcodefilter,$ownerfilter,$coursefilter,
1.404 raeburn 3857: $typefilter,$regexp_ok,$rtn_as_hash,$selfenrollonly,$catfilter,$showhidden,
1.427 raeburn 3858: $caller,$cloner,$cc_clone_list,$cloneonly,$createdbefore,$createdafter,
1.445 raeburn 3859: $creationcontext,$domcloner) =split(/:/,$tail);
1.397 raeburn 3860: my $now = time;
1.419 raeburn 3861: my ($cloneruname,$clonerudom,%cc_clone);
1.234 foxr 3862: if (defined($description)) {
3863: $description=&unescape($description);
3864: } else {
3865: $description='.';
3866: }
1.266 raeburn 3867: if (defined($instcodefilter)) {
3868: $instcodefilter=&unescape($instcodefilter);
3869: } else {
3870: $instcodefilter='.';
3871: }
1.336 raeburn 3872: my ($ownerunamefilter,$ownerdomfilter);
1.266 raeburn 3873: if (defined($ownerfilter)) {
3874: $ownerfilter=&unescape($ownerfilter);
1.336 raeburn 3875: if ($ownerfilter ne '.' && defined($ownerfilter)) {
3876: if ($ownerfilter =~ /^([^:]*):([^:]*)$/) {
3877: $ownerunamefilter = $1;
3878: $ownerdomfilter = $2;
3879: } else {
3880: $ownerunamefilter = $ownerfilter;
3881: $ownerdomfilter = '';
3882: }
3883: }
1.266 raeburn 3884: } else {
3885: $ownerfilter='.';
3886: }
1.336 raeburn 3887:
1.282 raeburn 3888: if (defined($coursefilter)) {
3889: $coursefilter=&unescape($coursefilter);
3890: } else {
3891: $coursefilter='.';
3892: }
1.333 raeburn 3893: if (defined($typefilter)) {
3894: $typefilter=&unescape($typefilter);
3895: } else {
3896: $typefilter='.';
3897: }
1.344 raeburn 3898: if (defined($regexp_ok)) {
3899: $regexp_ok=&unescape($regexp_ok);
3900: }
1.401 raeburn 3901: if (defined($catfilter)) {
3902: $catfilter=&unescape($catfilter);
3903: }
1.419 raeburn 3904: if (defined($cloner)) {
3905: $cloner = &unescape($cloner);
3906: ($cloneruname,$clonerudom) = ($cloner =~ /^($LONCAPA::match_username):($LONCAPA::match_domain)$/);
3907: }
3908: if (defined($cc_clone_list)) {
3909: $cc_clone_list = &unescape($cc_clone_list);
3910: my @cc_cloners = split('&',$cc_clone_list);
3911: foreach my $cid (@cc_cloners) {
3912: my ($clonedom,$clonenum) = split(':',$cid);
3913: next if ($clonedom ne $udom);
3914: $cc_clone{$clonedom.'_'.$clonenum} = 1;
3915: }
3916: }
1.431 raeburn 3917: if ($createdbefore ne '') {
1.427 raeburn 3918: $createdbefore = &unescape($createdbefore);
3919: } else {
3920: $createdbefore = 0;
3921: }
1.431 raeburn 3922: if ($createdafter ne '') {
1.427 raeburn 3923: $createdafter = &unescape($createdafter);
3924: } else {
3925: $createdafter = 0;
3926: }
1.431 raeburn 3927: if ($creationcontext ne '') {
1.427 raeburn 3928: $creationcontext = &unescape($creationcontext);
3929: } else {
3930: $creationcontext = '.';
3931: }
1.384 raeburn 3932: my $unpack = 1;
1.485 raeburn 3933: if ($description eq '.' && $instcodefilter eq '.' && $ownerfilter eq '.' &&
1.384 raeburn 3934: $typefilter eq '.') {
3935: $unpack = 0;
3936: }
3937: if (!defined($since)) { $since=0; }
1.234 foxr 3938: my $qresult='';
3939: my $hashref = &tie_domain_hash($udom, "nohist_courseids", &GDBM_WRCREAT());
3940: if ($hashref) {
1.384 raeburn 3941: while (my ($key,$value) = each(%$hashref)) {
1.397 raeburn 3942: my ($unesc_key,$lasttime_key,$lasttime,$is_hash,%val,
1.427 raeburn 3943: %unesc_val,$selfenroll_end,$selfenroll_types,$created,
3944: $context);
1.384 raeburn 3945: $unesc_key = &unescape($key);
3946: if ($unesc_key =~ /^lasttime:/) {
3947: next;
3948: } else {
3949: $lasttime_key = &escape('lasttime:'.$unesc_key);
3950: }
3951: if ($hashref->{$lasttime_key} ne '') {
3952: $lasttime = $hashref->{$lasttime_key};
3953: next if ($lasttime<$since);
3954: }
1.419 raeburn 3955: my ($canclone,$valchange);
1.384 raeburn 3956: my $items = &Apache::lonnet::thaw_unescape($value);
3957: if (ref($items) eq 'HASH') {
1.429 raeburn 3958: if ($hashref->{$lasttime_key} eq '') {
1.430 raeburn 3959: next if ($since > 1);
1.429 raeburn 3960: }
1.384 raeburn 3961: $is_hash = 1;
1.445 raeburn 3962: if ($domcloner) {
3963: $canclone = 1;
3964: } elsif (defined($clonerudom)) {
1.419 raeburn 3965: if ($items->{'cloners'}) {
3966: my @cloneable = split(',',$items->{'cloners'});
3967: if (@cloneable) {
3968: if (grep(/^\*$/,@cloneable)) {
3969: $canclone = 1;
3970: } elsif (grep(/^\*:\Q$clonerudom\E$/,@cloneable)) {
3971: $canclone = 1;
3972: } elsif (grep(/^\Q$cloneruname\E:\Q$clonerudom\E$/,@cloneable)) {
3973: $canclone = 1;
3974: }
3975: }
3976: unless ($canclone) {
3977: if ($cloneruname ne '' && $clonerudom ne '') {
3978: if ($cc_clone{$unesc_key}) {
3979: $canclone = 1;
3980: $items->{'cloners'} .= ','.$cloneruname.':'.
3981: $clonerudom;
3982: $valchange = 1;
3983: }
3984: }
3985: }
3986: } elsif (defined($cloneruname)) {
3987: if ($cc_clone{$unesc_key}) {
3988: $canclone = 1;
3989: $items->{'cloners'} = $cloneruname.':'.$clonerudom;
3990: $valchange = 1;
3991: }
1.437 raeburn 3992: unless ($canclone) {
3993: if ($items->{'owner'} =~ /:/) {
3994: if ($items->{'owner'} eq $cloner) {
3995: $canclone = 1;
3996: }
1.444 raeburn 3997: } elsif ($cloner eq $items->{'owner'}.':'.$udom) {
1.437 raeburn 3998: $canclone = 1;
3999: }
4000: if ($canclone) {
4001: $items->{'cloners'} = $cloneruname.':'.$clonerudom;
4002: $valchange = 1;
4003: }
4004: }
1.419 raeburn 4005: }
4006: }
1.384 raeburn 4007: if ($unpack || !$rtn_as_hash) {
4008: $unesc_val{'descr'} = $items->{'description'};
4009: $unesc_val{'inst_code'} = $items->{'inst_code'};
4010: $unesc_val{'owner'} = $items->{'owner'};
4011: $unesc_val{'type'} = $items->{'type'};
1.419 raeburn 4012: $unesc_val{'cloners'} = $items->{'cloners'};
1.427 raeburn 4013: $unesc_val{'created'} = $items->{'created'};
4014: $unesc_val{'context'} = $items->{'context'};
1.404 raeburn 4015: }
4016: $selfenroll_types = $items->{'selfenroll_types'};
4017: $selfenroll_end = $items->{'selfenroll_end_date'};
1.427 raeburn 4018: $created = $items->{'created'};
4019: $context = $items->{'context'};
1.404 raeburn 4020: if ($selfenrollonly) {
4021: next if (!$selfenroll_types);
4022: if (($selfenroll_end > 0) && ($selfenroll_end <= $now)) {
4023: next;
1.397 raeburn 4024: }
1.404 raeburn 4025: }
1.427 raeburn 4026: if ($creationcontext ne '.') {
4027: next if (($context ne '') && ($context ne $creationcontext));
4028: }
4029: if ($createdbefore > 0) {
4030: next if (($created eq '') || ($created > $createdbefore));
4031: }
4032: if ($createdafter > 0) {
4033: next if (($created eq '') || ($created <= $createdafter));
4034: }
1.404 raeburn 4035: if ($catfilter ne '') {
1.406 raeburn 4036: next if ($items->{'categories'} eq '');
4037: my @categories = split('&',$items->{'categories'});
1.407 raeburn 4038: next if (@categories == 0);
4039: my @subcats = split('&',$catfilter);
4040: my $matchcat = 0;
4041: foreach my $cat (@categories) {
4042: if (grep(/^\Q$cat\E$/,@subcats)) {
4043: $matchcat = 1;
4044: last;
4045: }
4046: }
4047: next if (!$matchcat);
1.404 raeburn 4048: }
4049: if ($caller eq 'coursecatalog') {
1.405 raeburn 4050: if ($items->{'hidefromcat'} eq 'yes') {
4051: next if !$showhidden;
1.401 raeburn 4052: }
1.384 raeburn 4053: }
1.383 raeburn 4054: } else {
1.401 raeburn 4055: next if ($catfilter ne '');
1.419 raeburn 4056: next if ($selfenrollonly);
1.427 raeburn 4057: next if ($createdbefore || $createdafter);
4058: next if ($creationcontext ne '.');
1.419 raeburn 4059: if ((defined($clonerudom)) && (defined($cloneruname))) {
4060: if ($cc_clone{$unesc_key}) {
4061: $canclone = 1;
4062: $val{'cloners'} = &escape($cloneruname.':'.$clonerudom);
4063: }
4064: }
1.384 raeburn 4065: $is_hash = 0;
1.388 raeburn 4066: my @courseitems = split(/:/,$value);
1.403 raeburn 4067: $lasttime = pop(@courseitems);
1.402 raeburn 4068: if ($hashref->{$lasttime_key} eq '') {
4069: next if ($lasttime<$since);
4070: }
1.384 raeburn 4071: ($val{'descr'},$val{'inst_code'},$val{'owner'},$val{'type'}) = @courseitems;
1.383 raeburn 4072: }
1.419 raeburn 4073: if ($cloneonly) {
4074: next unless ($canclone);
4075: }
1.266 raeburn 4076: my $match = 1;
1.384 raeburn 4077: if ($description ne '.') {
4078: if (!$is_hash) {
4079: $unesc_val{'descr'} = &unescape($val{'descr'});
4080: }
4081: if (eval{$unesc_val{'descr'} !~ /\Q$description\E/i}) {
1.266 raeburn 4082: $match = 0;
1.384 raeburn 4083: }
1.266 raeburn 4084: }
1.384 raeburn 4085: if ($instcodefilter ne '.') {
4086: if (!$is_hash) {
4087: $unesc_val{'inst_code'} = &unescape($val{'inst_code'});
4088: }
1.418 raeburn 4089: if ($regexp_ok == 1) {
1.384 raeburn 4090: if (eval{$unesc_val{'inst_code'} !~ /$instcodefilter/}) {
1.344 raeburn 4091: $match = 0;
4092: }
1.418 raeburn 4093: } elsif ($regexp_ok == -1) {
4094: if (eval{$unesc_val{'inst_code'} =~ /$instcodefilter/}) {
4095: $match = 0;
4096: }
1.344 raeburn 4097: } else {
1.384 raeburn 4098: if (eval{$unesc_val{'inst_code'} !~ /\Q$instcodefilter\E/i}) {
1.344 raeburn 4099: $match = 0;
4100: }
1.266 raeburn 4101: }
1.234 foxr 4102: }
1.384 raeburn 4103: if ($ownerfilter ne '.') {
4104: if (!$is_hash) {
4105: $unesc_val{'owner'} = &unescape($val{'owner'});
4106: }
1.336 raeburn 4107: if (($ownerunamefilter ne '') && ($ownerdomfilter ne '')) {
1.384 raeburn 4108: if ($unesc_val{'owner'} =~ /:/) {
4109: if (eval{$unesc_val{'owner'} !~
4110: /\Q$ownerunamefilter\E:\Q$ownerdomfilter\E$/i}) {
1.336 raeburn 4111: $match = 0;
4112: }
4113: } else {
1.384 raeburn 4114: if (eval{$unesc_val{'owner'} !~ /\Q$ownerunamefilter\E/i}) {
1.336 raeburn 4115: $match = 0;
4116: }
4117: }
4118: } elsif ($ownerunamefilter ne '') {
1.384 raeburn 4119: if ($unesc_val{'owner'} =~ /:/) {
4120: if (eval{$unesc_val{'owner'} !~ /\Q$ownerunamefilter\E:[^:]+$/i}) {
1.336 raeburn 4121: $match = 0;
4122: }
4123: } else {
1.384 raeburn 4124: if (eval{$unesc_val{'owner'} !~ /\Q$ownerunamefilter\E/i}) {
1.336 raeburn 4125: $match = 0;
4126: }
4127: }
4128: } elsif ($ownerdomfilter ne '') {
1.384 raeburn 4129: if ($unesc_val{'owner'} =~ /:/) {
4130: if (eval{$unesc_val{'owner'} !~ /^[^:]+:\Q$ownerdomfilter\E/}) {
1.336 raeburn 4131: $match = 0;
4132: }
4133: } else {
4134: if ($ownerdomfilter ne $udom) {
4135: $match = 0;
4136: }
4137: }
1.266 raeburn 4138: }
4139: }
1.384 raeburn 4140: if ($coursefilter ne '.') {
4141: if (eval{$unesc_key !~ /^$udom(_)\Q$coursefilter\E$/}) {
1.282 raeburn 4142: $match = 0;
4143: }
4144: }
1.384 raeburn 4145: if ($typefilter ne '.') {
4146: if (!$is_hash) {
4147: $unesc_val{'type'} = &unescape($val{'type'});
4148: }
4149: if ($unesc_val{'type'} eq '') {
1.333 raeburn 4150: if ($typefilter ne 'Course') {
4151: $match = 0;
4152: }
1.383 raeburn 4153: } else {
1.384 raeburn 4154: if (eval{$unesc_val{'type'} !~ /^\Q$typefilter\E$/}) {
1.333 raeburn 4155: $match = 0;
4156: }
4157: }
4158: }
1.266 raeburn 4159: if ($match == 1) {
1.384 raeburn 4160: if ($rtn_as_hash) {
4161: if ($is_hash) {
1.419 raeburn 4162: if ($valchange) {
4163: my $newvalue = &Apache::lonnet::freeze_escape($items);
4164: $qresult.=$key.'='.$newvalue.'&';
4165: } else {
4166: $qresult.=$key.'='.$value.'&';
4167: }
1.384 raeburn 4168: } else {
1.388 raeburn 4169: my %rtnhash = ( 'description' => &unescape($val{'descr'}),
4170: 'inst_code' => &unescape($val{'inst_code'}),
4171: 'owner' => &unescape($val{'owner'}),
4172: 'type' => &unescape($val{'type'}),
1.419 raeburn 4173: 'cloners' => &unescape($val{'cloners'}),
1.384 raeburn 4174: );
4175: my $items = &Apache::lonnet::freeze_escape(\%rtnhash);
4176: $qresult.=$key.'='.$items.'&';
4177: }
1.383 raeburn 4178: } else {
1.384 raeburn 4179: if ($is_hash) {
4180: $qresult .= $key.'='.&escape($unesc_val{'descr'}).':'.
4181: &escape($unesc_val{'inst_code'}).':'.
4182: &escape($unesc_val{'owner'}).'&';
4183: } else {
4184: $qresult .= $key.'='.$val{'descr'}.':'.$val{'inst_code'}.
4185: ':'.$val{'owner'}.'&';
4186: }
1.383 raeburn 4187: }
1.266 raeburn 4188: }
1.234 foxr 4189: }
1.311 albertel 4190: if (&untie_domain_hash($hashref)) {
1.234 foxr 4191: chop($qresult);
1.387 albertel 4192: &Reply($client, \$qresult, $userinput);
1.234 foxr 4193: } else {
4194: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
4195: "while attempting courseiddump\n", $userinput);
4196: }
4197: } else {
4198: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
4199: "while attempting courseiddump\n", $userinput);
4200: }
4201: return 1;
4202: }
4203: ®ister_handler("courseiddump", \&dump_course_id_handler, 0, 1, 0);
1.238 foxr 4204:
1.438 raeburn 4205: sub course_lastaccess_handler {
4206: my ($cmd, $tail, $client) = @_;
4207: my $userinput = "$cmd:$tail";
4208: my ($cdom,$cnum) = split(':',$tail);
4209: my (%lastaccess,$qresult);
4210: my $hashref = &tie_domain_hash($cdom, "nohist_courseids", &GDBM_WRCREAT());
4211: if ($hashref) {
4212: while (my ($key,$value) = each(%$hashref)) {
4213: my ($unesc_key,$lasttime);
4214: $unesc_key = &unescape($key);
4215: if ($cnum) {
4216: next unless ($unesc_key =~ /\Q$cdom\E_\Q$cnum\E$/);
4217: }
4218: if ($unesc_key =~ /^lasttime:($LONCAPA::match_domain\_$LONCAPA::match_courseid)/) {
4219: $lastaccess{$1} = $value;
4220: } else {
4221: my $items = &Apache::lonnet::thaw_unescape($value);
4222: if (ref($items) eq 'HASH') {
4223: unless ($lastaccess{$unesc_key}) {
4224: $lastaccess{$unesc_key} = '';
4225: }
4226: } else {
4227: my @courseitems = split(':',$value);
4228: $lastaccess{$unesc_key} = pop(@courseitems);
4229: }
4230: }
4231: }
4232: foreach my $cid (sort(keys(%lastaccess))) {
4233: $qresult.=&escape($cid).'='.$lastaccess{$cid}.'&';
4234: }
4235: if (&untie_domain_hash($hashref)) {
4236: if ($qresult) {
4237: chop($qresult);
4238: }
4239: &Reply($client, \$qresult, $userinput);
4240: } else {
4241: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
4242: "while attempting lastacourseaccess\n", $userinput);
4243: }
4244: } else {
4245: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
4246: "while attempting lastcourseaccess\n", $userinput);
4247: }
4248: return 1;
4249: }
4250: ®ister_handler("courselastaccess",\&course_lastaccess_handler, 0, 1, 0);
4251:
1.238 foxr 4252: #
1.348 raeburn 4253: # Puts an unencrypted entry in a namespace db file at the domain level
4254: #
4255: # Parameters:
4256: # $cmd - The command that got us here.
4257: # $tail - Tail of the command (remaining parameters).
4258: # $client - File descriptor connected to client.
4259: # Returns
4260: # 0 - Requested to exit, caller should shut down.
4261: # 1 - Continue processing.
4262: # Side effects:
4263: # reply is written to $client.
4264: #
4265: sub put_domain_handler {
4266: my ($cmd,$tail,$client) = @_;
4267:
4268: my $userinput = "$cmd:$tail";
4269:
4270: my ($udom,$namespace,$what) =split(/:/,$tail,3);
4271: chomp($what);
4272: my @pairs=split(/\&/,$what);
4273: my $hashref = &tie_domain_hash($udom, "$namespace", &GDBM_WRCREAT(),
4274: "P", $what);
4275: if ($hashref) {
4276: foreach my $pair (@pairs) {
4277: my ($key,$value)=split(/=/,$pair);
4278: $hashref->{$key}=$value;
4279: }
4280: if (&untie_domain_hash($hashref)) {
4281: &Reply($client, "ok\n", $userinput);
4282: } else {
4283: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
4284: "while attempting putdom\n", $userinput);
4285: }
4286: } else {
4287: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
4288: "while attempting putdom\n", $userinput);
4289: }
4290:
4291: return 1;
4292: }
4293: ®ister_handler("putdom", \&put_domain_handler, 0, 1, 0);
4294:
4295: # Unencrypted get from the namespace database file at the domain level.
4296: # This function retrieves a keyed item from a specific named database in the
4297: # domain directory.
4298: #
4299: # Parameters:
4300: # $cmd - Command request keyword (get).
4301: # $tail - Tail of the command. This is a colon separated list
4302: # consisting of the domain and the 'namespace'
4303: # which selects the gdbm file to do the lookup in,
4304: # & separated list of keys to lookup. Note that
4305: # the values are returned as an & separated list too.
4306: # $client - File descriptor open on the client.
4307: # Returns:
4308: # 1 - Continue processing.
4309: # 0 - Exit.
4310: # Side effects:
4311: # reply is written to $client.
4312: #
4313:
4314: sub get_domain_handler {
4315: my ($cmd, $tail, $client) = @_;
4316:
1.461 foxr 4317:
1.348 raeburn 4318: my $userinput = "$client:$tail";
4319:
4320: my ($udom,$namespace,$what)=split(/:/,$tail,3);
4321: chomp($what);
4322: my @queries=split(/\&/,$what);
4323: my $qresult='';
4324: my $hashref = &tie_domain_hash($udom, "$namespace", &GDBM_READER());
4325: if ($hashref) {
4326: for (my $i=0;$i<=$#queries;$i++) {
4327: $qresult.="$hashref->{$queries[$i]}&";
4328: }
4329: if (&untie_domain_hash($hashref)) {
4330: $qresult=~s/\&$//;
1.387 albertel 4331: &Reply($client, \$qresult, $userinput);
1.348 raeburn 4332: } else {
4333: &Failure( $client, "error: ".($!+0)." untie(GDBM) Failed ".
4334: "while attempting getdom\n",$userinput);
4335: }
4336: } else {
4337: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
4338: "while attempting getdom\n",$userinput);
4339: }
4340:
4341: return 1;
4342: }
1.360 raeburn 4343: ®ister_handler("getdom", \&get_domain_handler, 0, 1, 0);
1.348 raeburn 4344:
1.420 raeburn 4345: #
1.238 foxr 4346: # Puts an id to a domains id database.
4347: #
4348: # Parameters:
4349: # $cmd - The command that triggered us.
4350: # $tail - Remainder of the request other than the command. This is a
4351: # colon separated list containing:
4352: # $domain - The domain for which we are writing the id.
4353: # $pairs - The id info to write... this is and & separated list
4354: # of keyword=value.
4355: # $client - Socket open on the client.
4356: # Returns:
4357: # 1 - Continue processing.
4358: # Side effects:
4359: # reply is written to $client.
4360: #
4361: sub put_id_handler {
4362: my ($cmd,$tail,$client) = @_;
4363:
4364:
4365: my $userinput = "$cmd:$tail";
4366:
4367: my ($udom,$what)=split(/:/,$tail);
4368: chomp($what);
4369: my @pairs=split(/\&/,$what);
4370: my $hashref = &tie_domain_hash($udom, "ids", &GDBM_WRCREAT(),
4371: "P", $what);
4372: if ($hashref) {
4373: foreach my $pair (@pairs) {
4374: my ($key,$value)=split(/=/,$pair);
4375: $hashref->{$key}=$value;
4376: }
1.311 albertel 4377: if (&untie_domain_hash($hashref)) {
1.238 foxr 4378: &Reply($client, "ok\n", $userinput);
4379: } else {
4380: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
4381: "while attempting idput\n", $userinput);
4382: }
4383: } else {
4384: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
4385: "while attempting idput\n", $userinput);
4386: }
4387:
4388: return 1;
4389: }
1.263 albertel 4390: ®ister_handler("idput", \&put_id_handler, 0, 1, 0);
1.238 foxr 4391:
4392: #
4393: # Retrieves a set of id values from the id database.
4394: # Returns an & separated list of results, one for each requested id to the
4395: # client.
4396: #
4397: # Parameters:
4398: # $cmd - Command keyword that caused us to be dispatched.
4399: # $tail - Tail of the command. Consists of a colon separated:
4400: # domain - the domain whose id table we dump
4401: # ids Consists of an & separated list of
4402: # id keywords whose values will be fetched.
4403: # nonexisting keywords will have an empty value.
4404: # $client - Socket open on the client.
4405: #
4406: # Returns:
4407: # 1 - indicating processing should continue.
4408: # Side effects:
4409: # An & separated list of results is written to $client.
4410: #
4411: sub get_id_handler {
4412: my ($cmd, $tail, $client) = @_;
4413:
4414:
4415: my $userinput = "$client:$tail";
4416:
4417: my ($udom,$what)=split(/:/,$tail);
4418: chomp($what);
4419: my @queries=split(/\&/,$what);
4420: my $qresult='';
4421: my $hashref = &tie_domain_hash($udom, "ids", &GDBM_READER());
4422: if ($hashref) {
4423: for (my $i=0;$i<=$#queries;$i++) {
4424: $qresult.="$hashref->{$queries[$i]}&";
4425: }
1.311 albertel 4426: if (&untie_domain_hash($hashref)) {
1.238 foxr 4427: $qresult=~s/\&$//;
1.387 albertel 4428: &Reply($client, \$qresult, $userinput);
1.238 foxr 4429: } else {
4430: &Failure( $client, "error: ".($!+0)." untie(GDBM) Failed ".
4431: "while attempting idget\n",$userinput);
4432: }
4433: } else {
4434: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
4435: "while attempting idget\n",$userinput);
4436: }
4437:
4438: return 1;
4439: }
1.263 albertel 4440: ®ister_handler("idget", \&get_id_handler, 0, 1, 0);
1.238 foxr 4441:
1.501 raeburn 4442: # Deletes one or more ids in a domain's id database.
4443: #
4444: # Parameters:
4445: # $cmd - Command keyword (iddel).
4446: # $tail - Command tail. In this case a colon
4447: # separated list containing:
4448: # The domain for which we are deleting the id(s).
4449: # &-separated list of id(s) to delete.
4450: # $client - File open on client socket.
4451: # Returns:
4452: # 1 - Continue processing
4453: # 0 - Exit server.
4454: #
4455: #
4456:
4457: sub del_id_handler {
4458: my ($cmd,$tail,$client) = @_;
4459:
4460: my $userinput = "$cmd:$tail";
4461:
4462: my ($udom,$what)=split(/:/,$tail);
4463: chomp($what);
4464: my $hashref = &tie_domain_hash($udom, "ids", &GDBM_WRCREAT(),
4465: "D", $what);
4466: if ($hashref) {
4467: my @keys=split(/\&/,$what);
4468: foreach my $key (@keys) {
4469: delete($hashref->{$key});
4470: }
4471: if (&untie_user_hash($hashref)) {
4472: &Reply($client, "ok\n", $userinput);
4473: } else {
4474: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
4475: "while attempting iddel\n", $userinput);
4476: }
4477: } else {
4478: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
4479: "while attempting iddel\n", $userinput);
4480: }
4481: return 1;
4482: }
4483: ®ister_handler("iddel", \&del_id_handler, 0, 1, 0);
4484:
1.238 foxr 4485: #
1.299 raeburn 4486: # Puts broadcast e-mail sent by Domain Coordinator in nohist_dcmail database
4487: #
4488: # Parameters
4489: # $cmd - Command keyword that caused us to be dispatched.
4490: # $tail - Tail of the command. Consists of a colon separated:
4491: # domain - the domain whose dcmail we are recording
4492: # email Consists of key=value pair
4493: # where key is unique msgid
4494: # and value is message (in XML)
4495: # $client - Socket open on the client.
4496: #
4497: # Returns:
4498: # 1 - indicating processing should continue.
4499: # Side effects
4500: # reply is written to $client.
4501: #
4502: sub put_dcmail_handler {
4503: my ($cmd,$tail,$client) = @_;
4504: my $userinput = "$cmd:$tail";
1.463 foxr 4505:
4506:
1.299 raeburn 4507: my ($udom,$what)=split(/:/,$tail);
4508: chomp($what);
4509: my $hashref = &tie_domain_hash($udom, "nohist_dcmail", &GDBM_WRCREAT());
4510: if ($hashref) {
4511: my ($key,$value)=split(/=/,$what);
4512: $hashref->{$key}=$value;
4513: }
1.311 albertel 4514: if (&untie_domain_hash($hashref)) {
1.299 raeburn 4515: &Reply($client, "ok\n", $userinput);
4516: } else {
4517: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
4518: "while attempting dcmailput\n", $userinput);
4519: }
4520: return 1;
4521: }
4522: ®ister_handler("dcmailput", \&put_dcmail_handler, 0, 1, 0);
4523:
4524: #
4525: # Retrieves broadcast e-mail from nohist_dcmail database
4526: # Returns to client an & separated list of key=value pairs,
4527: # where key is msgid and value is message information.
4528: #
4529: # Parameters
4530: # $cmd - Command keyword that caused us to be dispatched.
4531: # $tail - Tail of the command. Consists of a colon separated:
4532: # domain - the domain whose dcmail table we dump
4533: # startfilter - beginning of time window
4534: # endfilter - end of time window
4535: # sendersfilter - & separated list of username:domain
4536: # for senders to search for.
4537: # $client - Socket open on the client.
4538: #
4539: # Returns:
4540: # 1 - indicating processing should continue.
4541: # Side effects
4542: # reply (& separated list of msgid=messageinfo pairs) is
4543: # written to $client.
4544: #
4545: sub dump_dcmail_handler {
4546: my ($cmd, $tail, $client) = @_;
4547:
4548: my $userinput = "$cmd:$tail";
4549: my ($udom,$startfilter,$endfilter,$sendersfilter) = split(/:/,$tail);
4550: chomp($sendersfilter);
4551: my @senders = ();
4552: if (defined($startfilter)) {
4553: $startfilter=&unescape($startfilter);
4554: } else {
4555: $startfilter='.';
4556: }
4557: if (defined($endfilter)) {
4558: $endfilter=&unescape($endfilter);
4559: } else {
4560: $endfilter='.';
4561: }
4562: if (defined($sendersfilter)) {
4563: $sendersfilter=&unescape($sendersfilter);
1.300 albertel 4564: @senders = map { &unescape($_) } split(/\&/,$sendersfilter);
1.299 raeburn 4565: }
4566:
4567: my $qresult='';
4568: my $hashref = &tie_domain_hash($udom, "nohist_dcmail", &GDBM_WRCREAT());
4569: if ($hashref) {
4570: while (my ($key,$value) = each(%$hashref)) {
4571: my $match = 1;
1.303 albertel 4572: my ($timestamp,$subj,$uname,$udom) =
4573: split(/:/,&unescape(&unescape($key)),5); # yes, twice really
1.299 raeburn 4574: $subj = &unescape($subj);
4575: unless ($startfilter eq '.' || !defined($startfilter)) {
4576: if ($timestamp < $startfilter) {
4577: $match = 0;
4578: }
4579: }
4580: unless ($endfilter eq '.' || !defined($endfilter)) {
4581: if ($timestamp > $endfilter) {
4582: $match = 0;
4583: }
4584: }
4585: unless (@senders < 1) {
4586: unless (grep/^$uname:$udom$/,@senders) {
4587: $match = 0;
4588: }
4589: }
4590: if ($match == 1) {
4591: $qresult.=$key.'='.$value.'&';
4592: }
4593: }
1.311 albertel 4594: if (&untie_domain_hash($hashref)) {
1.299 raeburn 4595: chop($qresult);
1.387 albertel 4596: &Reply($client, \$qresult, $userinput);
1.299 raeburn 4597: } else {
4598: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
4599: "while attempting dcmaildump\n", $userinput);
4600: }
4601: } else {
4602: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
4603: "while attempting dcmaildump\n", $userinput);
4604: }
4605: return 1;
4606: }
4607:
4608: ®ister_handler("dcmaildump", \&dump_dcmail_handler, 0, 1, 0);
4609:
4610: #
4611: # Puts domain roles in nohist_domainroles database
4612: #
4613: # Parameters
4614: # $cmd - Command keyword that caused us to be dispatched.
4615: # $tail - Tail of the command. Consists of a colon separated:
4616: # domain - the domain whose roles we are recording
4617: # role - Consists of key=value pair
4618: # where key is unique role
4619: # and value is start/end date information
4620: # $client - Socket open on the client.
4621: #
4622: # Returns:
4623: # 1 - indicating processing should continue.
4624: # Side effects
4625: # reply is written to $client.
4626: #
4627:
4628: sub put_domainroles_handler {
4629: my ($cmd,$tail,$client) = @_;
4630:
4631: my $userinput = "$cmd:$tail";
4632: my ($udom,$what)=split(/:/,$tail);
4633: chomp($what);
4634: my @pairs=split(/\&/,$what);
4635: my $hashref = &tie_domain_hash($udom, "nohist_domainroles", &GDBM_WRCREAT());
4636: if ($hashref) {
4637: foreach my $pair (@pairs) {
4638: my ($key,$value)=split(/=/,$pair);
4639: $hashref->{$key}=$value;
4640: }
1.311 albertel 4641: if (&untie_domain_hash($hashref)) {
1.299 raeburn 4642: &Reply($client, "ok\n", $userinput);
4643: } else {
4644: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
4645: "while attempting domroleput\n", $userinput);
4646: }
4647: } else {
4648: &Failure( $client, "error: ".($!+0)." tie(GDBM) Failed ".
4649: "while attempting domroleput\n", $userinput);
4650: }
4651:
4652: return 1;
4653: }
4654:
4655: ®ister_handler("domroleput", \&put_domainroles_handler, 0, 1, 0);
4656:
4657: #
4658: # Retrieves domain roles from nohist_domainroles database
4659: # Returns to client an & separated list of key=value pairs,
4660: # where key is role and value is start and end date information.
4661: #
4662: # Parameters
4663: # $cmd - Command keyword that caused us to be dispatched.
4664: # $tail - Tail of the command. Consists of a colon separated:
4665: # domain - the domain whose domain roles table we dump
4666: # $client - Socket open on the client.
4667: #
4668: # Returns:
4669: # 1 - indicating processing should continue.
4670: # Side effects
4671: # reply (& separated list of role=start/end info pairs) is
4672: # written to $client.
4673: #
4674: sub dump_domainroles_handler {
4675: my ($cmd, $tail, $client) = @_;
4676:
4677: my $userinput = "$cmd:$tail";
4678: my ($udom,$startfilter,$endfilter,$rolesfilter) = split(/:/,$tail);
4679: chomp($rolesfilter);
4680: my @roles = ();
4681: if (defined($startfilter)) {
4682: $startfilter=&unescape($startfilter);
4683: } else {
4684: $startfilter='.';
4685: }
4686: if (defined($endfilter)) {
4687: $endfilter=&unescape($endfilter);
4688: } else {
4689: $endfilter='.';
4690: }
4691: if (defined($rolesfilter)) {
4692: $rolesfilter=&unescape($rolesfilter);
1.300 albertel 4693: @roles = split(/\&/,$rolesfilter);
1.299 raeburn 4694: }
1.421 raeburn 4695:
1.299 raeburn 4696: my $hashref = &tie_domain_hash($udom, "nohist_domainroles", &GDBM_WRCREAT());
4697: if ($hashref) {
4698: my $qresult = '';
4699: while (my ($key,$value) = each(%$hashref)) {
4700: my $match = 1;
1.421 raeburn 4701: my ($end,$start) = split(/:/,&unescape($value));
1.299 raeburn 4702: my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,&unescape($key));
1.421 raeburn 4703: unless (@roles < 1) {
4704: unless (grep/^\Q$trole\E$/,@roles) {
4705: $match = 0;
4706: next;
4707: }
4708: }
1.299 raeburn 4709: unless ($startfilter eq '.' || !defined($startfilter)) {
1.415 raeburn 4710: if ((defined($start)) && ($start >= $startfilter)) {
1.299 raeburn 4711: $match = 0;
1.421 raeburn 4712: next;
1.299 raeburn 4713: }
4714: }
4715: unless ($endfilter eq '.' || !defined($endfilter)) {
1.421 raeburn 4716: if ((defined($end)) && (($end > 0) && ($end <= $endfilter))) {
1.299 raeburn 4717: $match = 0;
1.421 raeburn 4718: next;
1.299 raeburn 4719: }
4720: }
4721: if ($match == 1) {
4722: $qresult.=$key.'='.$value.'&';
4723: }
4724: }
1.311 albertel 4725: if (&untie_domain_hash($hashref)) {
1.299 raeburn 4726: chop($qresult);
1.387 albertel 4727: &Reply($client, \$qresult, $userinput);
1.299 raeburn 4728: } else {
4729: &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
4730: "while attempting domrolesdump\n", $userinput);
4731: }
4732: } else {
4733: &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
4734: "while attempting domrolesdump\n", $userinput);
4735: }
4736: return 1;
4737: }
4738:
4739: ®ister_handler("domrolesdump", \&dump_domainroles_handler, 0, 1, 0);
4740:
4741:
1.238 foxr 4742: # Process the tmpput command I'm not sure what this does.. Seems to
4743: # create a file in the lonDaemons/tmp directory of the form $id.tmp
4744: # where Id is the client's ip concatenated with a sequence number.
4745: # The file will contain some value that is passed in. Is this e.g.
4746: # a login token?
4747: #
4748: # Parameters:
4749: # $cmd - The command that got us dispatched.
4750: # $tail - The remainder of the request following $cmd:
4751: # In this case this will be the contents of the file.
4752: # $client - Socket connected to the client.
4753: # Returns:
4754: # 1 indicating processing can continue.
4755: # Side effects:
4756: # A file is created in the local filesystem.
4757: # A reply is sent to the client.
4758: sub tmp_put_handler {
4759: my ($cmd, $what, $client) = @_;
4760:
4761: my $userinput = "$cmd:$what"; # Reconstruct for logging.
4762:
1.347 raeburn 4763: my ($record,$context) = split(/:/,$what);
4764: if ($context ne '') {
4765: chomp($context);
4766: $context = &unescape($context);
4767: }
4768: my ($id,$store);
1.238 foxr 4769: $tmpsnum++;
1.454 raeburn 4770: if (($context eq 'resetpw') || ($context eq 'createaccount')) {
1.347 raeburn 4771: $id = &md5_hex(&md5_hex(time.{}.rand().$$));
4772: } else {
4773: $id = $$.'_'.$clientip.'_'.$tmpsnum;
4774: }
1.238 foxr 4775: $id=~s/\W/\_/g;
1.347 raeburn 4776: $record=~s/\n//g;
1.238 foxr 4777: my $execdir=$perlvar{'lonDaemons'};
4778: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1.347 raeburn 4779: print $store $record;
1.238 foxr 4780: close $store;
1.387 albertel 4781: &Reply($client, \$id, $userinput);
1.238 foxr 4782: } else {
4783: &Failure( $client, "error: ".($!+0)."IO::File->new Failed ".
4784: "while attempting tmpput\n", $userinput);
4785: }
4786: return 1;
4787:
4788: }
4789: ®ister_handler("tmpput", \&tmp_put_handler, 0, 1, 0);
1.263 albertel 4790:
1.238 foxr 4791: # Processes the tmpget command. This command returns the contents
4792: # of a temporary resource file(?) created via tmpput.
4793: #
4794: # Paramters:
4795: # $cmd - Command that got us dispatched.
4796: # $id - Tail of the command, contain the id of the resource
4797: # we want to fetch.
4798: # $client - socket open on the client.
4799: # Return:
4800: # 1 - Inidcating processing can continue.
4801: # Side effects:
4802: # A reply is sent to the client.
4803: #
4804: sub tmp_get_handler {
4805: my ($cmd, $id, $client) = @_;
4806:
4807: my $userinput = "$cmd:$id";
4808:
4809:
4810: $id=~s/\W/\_/g;
4811: my $store;
4812: my $execdir=$perlvar{'lonDaemons'};
4813: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
4814: my $reply=<$store>;
1.387 albertel 4815: &Reply( $client, \$reply, $userinput);
1.238 foxr 4816: close $store;
4817: } else {
4818: &Failure( $client, "error: ".($!+0)."IO::File->new Failed ".
4819: "while attempting tmpget\n", $userinput);
4820: }
4821:
4822: return 1;
4823: }
4824: ®ister_handler("tmpget", \&tmp_get_handler, 0, 1, 0);
1.263 albertel 4825:
1.238 foxr 4826: #
4827: # Process the tmpdel command. This command deletes a temp resource
4828: # created by the tmpput command.
4829: #
4830: # Parameters:
4831: # $cmd - Command that got us here.
4832: # $id - Id of the temporary resource created.
4833: # $client - socket open on the client process.
4834: #
4835: # Returns:
4836: # 1 - Indicating processing should continue.
4837: # Side Effects:
4838: # A file is deleted
4839: # A reply is sent to the client.
4840: sub tmp_del_handler {
4841: my ($cmd, $id, $client) = @_;
4842:
4843: my $userinput= "$cmd:$id";
4844:
4845: chomp($id);
4846: $id=~s/\W/\_/g;
4847: my $execdir=$perlvar{'lonDaemons'};
4848: if (unlink("$execdir/tmp/$id.tmp")) {
4849: &Reply($client, "ok\n", $userinput);
4850: } else {
4851: &Failure( $client, "error: ".($!+0)."Unlink tmp Failed ".
4852: "while attempting tmpdel\n", $userinput);
4853: }
4854:
4855: return 1;
4856:
4857: }
4858: ®ister_handler("tmpdel", \&tmp_del_handler, 0, 1, 0);
1.263 albertel 4859:
1.238 foxr 4860: #
1.246 foxr 4861: # Processes the setannounce command. This command
4862: # creates a file named announce.txt in the top directory of
4863: # the documentn root and sets its contents. The announce.txt file is
4864: # printed in its entirety at the LonCAPA login page. Note:
4865: # once the announcement.txt fileis created it cannot be deleted.
4866: # However, setting the contents of the file to empty removes the
4867: # announcement from the login page of loncapa so who cares.
4868: #
4869: # Parameters:
4870: # $cmd - The command that got us dispatched.
4871: # $announcement - The text of the announcement.
4872: # $client - Socket open on the client process.
4873: # Retunrns:
4874: # 1 - Indicating request processing should continue
4875: # Side Effects:
4876: # The file {DocRoot}/announcement.txt is created.
4877: # A reply is sent to $client.
4878: #
4879: sub set_announce_handler {
4880: my ($cmd, $announcement, $client) = @_;
4881:
4882: my $userinput = "$cmd:$announcement";
4883:
4884: chomp($announcement);
4885: $announcement=&unescape($announcement);
4886: if (my $store=IO::File->new('>'.$perlvar{'lonDocRoot'}.
4887: '/announcement.txt')) {
4888: print $store $announcement;
4889: close $store;
4890: &Reply($client, "ok\n", $userinput);
4891: } else {
4892: &Failure($client, "error: ".($!+0)."\n", $userinput);
4893: }
4894:
4895: return 1;
4896: }
4897: ®ister_handler("setannounce", \&set_announce_handler, 0, 1, 0);
1.263 albertel 4898:
1.246 foxr 4899: #
4900: # Return the version of the daemon. This can be used to determine
4901: # the compatibility of cross version installations or, alternatively to
4902: # simply know who's out of date and who isn't. Note that the version
4903: # is returned concatenated with the tail.
4904: # Parameters:
4905: # $cmd - the request that dispatched to us.
4906: # $tail - Tail of the request (client's version?).
4907: # $client - Socket open on the client.
4908: #Returns:
4909: # 1 - continue processing requests.
4910: # Side Effects:
4911: # Replies with version to $client.
4912: sub get_version_handler {
4913: my ($cmd, $tail, $client) = @_;
4914:
4915: my $userinput = $cmd.$tail;
4916:
4917: &Reply($client, &version($userinput)."\n", $userinput);
4918:
4919:
4920: return 1;
4921: }
4922: ®ister_handler("version", \&get_version_handler, 0, 1, 0);
1.263 albertel 4923:
1.246 foxr 4924: # Set the current host and domain. This is used to support
4925: # multihomed systems. Each IP of the system, or even separate daemons
4926: # on the same IP can be treated as handling a separate lonCAPA virtual
4927: # machine. This command selects the virtual lonCAPA. The client always
4928: # knows the right one since it is lonc and it is selecting the domain/system
4929: # from the hosts.tab file.
4930: # Parameters:
4931: # $cmd - Command that dispatched us.
4932: # $tail - Tail of the command (domain/host requested).
4933: # $socket - Socket open on the client.
4934: #
4935: # Returns:
4936: # 1 - Indicates the program should continue to process requests.
4937: # Side-effects:
4938: # The default domain/system context is modified for this daemon.
4939: # a reply is sent to the client.
4940: #
4941: sub set_virtual_host_handler {
4942: my ($cmd, $tail, $socket) = @_;
4943:
4944: my $userinput ="$cmd:$tail";
4945:
4946: &Reply($client, &sethost($userinput)."\n", $userinput);
4947:
4948:
4949: return 1;
4950: }
1.247 albertel 4951: ®ister_handler("sethost", \&set_virtual_host_handler, 0, 1, 0);
1.246 foxr 4952:
4953: # Process a request to exit:
4954: # - "bye" is sent to the client.
4955: # - The client socket is shutdown and closed.
4956: # - We indicate to the caller that we should exit.
4957: # Formal Parameters:
4958: # $cmd - The command that got us here.
4959: # $tail - Tail of the command (empty).
4960: # $client - Socket open on the tail.
4961: # Returns:
4962: # 0 - Indicating the program should exit!!
4963: #
4964: sub exit_handler {
4965: my ($cmd, $tail, $client) = @_;
4966:
4967: my $userinput = "$cmd:$tail";
4968:
4969: &logthis("Client $clientip ($clientname) hanging up: $userinput");
4970: &Reply($client, "bye\n", $userinput);
4971: $client->shutdown(2); # shutdown the socket forcibly.
4972: $client->close();
4973:
4974: return 0;
4975: }
1.248 foxr 4976: ®ister_handler("exit", \&exit_handler, 0,1,1);
4977: ®ister_handler("init", \&exit_handler, 0,1,1);
4978: ®ister_handler("quit", \&exit_handler, 0,1,1);
4979:
4980: # Determine if auto-enrollment is enabled.
4981: # Note that the original had what I believe to be a defect.
4982: # The original returned 0 if the requestor was not a registerd client.
4983: # It should return "refused".
4984: # Formal Parameters:
4985: # $cmd - The command that invoked us.
4986: # $tail - The tail of the command (Extra command parameters.
4987: # $client - The socket open on the client that issued the request.
4988: # Returns:
4989: # 1 - Indicating processing should continue.
4990: #
4991: sub enrollment_enabled_handler {
4992: my ($cmd, $tail, $client) = @_;
4993: my $userinput = $cmd.":".$tail; # For logging purposes.
4994:
4995:
1.337 albertel 4996: my ($cdom) = split(/:/, $tail, 2); # Domain we're asking about.
4997:
1.248 foxr 4998: my $outcome = &localenroll::run($cdom);
1.387 albertel 4999: &Reply($client, \$outcome, $userinput);
1.248 foxr 5000:
5001: return 1;
5002: }
5003: ®ister_handler("autorun", \&enrollment_enabled_handler, 0, 1, 0);
5004:
1.417 raeburn 5005: #
1.423 raeburn 5006: # Validate an institutional code used for a LON-CAPA course.
1.417 raeburn 5007: #
5008: # Formal Parameters:
5009: # $cmd - The command request that got us dispatched.
5010: # $tail - The tail of the command. In this case,
5011: # this is a colon separated set of words that will be split
5012: # into:
1.424 raeburn 5013: # $dom - The domain for which the check of
5014: # institutional course code will occur.
5015: #
5016: # $instcode - The institutional code for the course
5017: # being requested, or validated for rights
5018: # to request.
5019: #
5020: # $owner - The course requestor (who will be the
5021: # course owner, in the form username:domain
5022: #
1.417 raeburn 5023: # $client - Socket open on the client.
5024: # Returns:
5025: # 1 - Indicating processing should continue.
5026: #
5027: sub validate_instcode_handler {
5028: my ($cmd, $tail, $client) = @_;
5029: my $userinput = "$cmd:$tail";
1.423 raeburn 5030: my ($dom,$instcode,$owner) = split(/:/, $tail);
1.422 raeburn 5031: $instcode = &unescape($instcode);
5032: $owner = &unescape($owner);
1.498 raeburn 5033: my ($outcome,$description,$credits) =
1.426 raeburn 5034: &localenroll::validate_instcode($dom,$instcode,$owner);
1.498 raeburn 5035: my $result = &escape($outcome).'&'.&escape($description).'&'.
5036: &escape($credits);
1.426 raeburn 5037: &Reply($client, \$result, $userinput);
1.417 raeburn 5038:
5039: return 1;
5040: }
5041: ®ister_handler("autovalidateinstcode", \&validate_instcode_handler, 0, 1, 0);
5042:
1.248 foxr 5043: # Get the official sections for which auto-enrollment is possible.
5044: # Since the admin people won't know about 'unofficial sections'
5045: # we cannot auto-enroll on them.
5046: # Formal Parameters:
5047: # $cmd - The command request that got us dispatched here.
5048: # $tail - The remainder of the request. In our case this
5049: # will be split into:
5050: # $coursecode - The course name from the admin point of view.
5051: # $cdom - The course's domain(?).
5052: # $client - Socket open on the client.
5053: # Returns:
5054: # 1 - Indiciting processing should continue.
5055: #
5056: sub get_sections_handler {
5057: my ($cmd, $tail, $client) = @_;
5058: my $userinput = "$cmd:$tail";
5059:
5060: my ($coursecode, $cdom) = split(/:/, $tail);
5061: my @secs = &localenroll::get_sections($coursecode,$cdom);
5062: my $seclist = &escape(join(':',@secs));
5063:
1.387 albertel 5064: &Reply($client, \$seclist, $userinput);
1.248 foxr 5065:
5066:
5067: return 1;
5068: }
5069: ®ister_handler("autogetsections", \&get_sections_handler, 0, 1, 0);
5070:
5071: # Validate the owner of a new course section.
5072: #
5073: # Formal Parameters:
5074: # $cmd - Command that got us dispatched.
5075: # $tail - the remainder of the command. For us this consists of a
5076: # colon separated string containing:
5077: # $inst - Course Id from the institutions point of view.
5078: # $owner - Proposed owner of the course.
5079: # $cdom - Domain of the course (from the institutions
5080: # point of view?)..
5081: # $client - Socket open on the client.
5082: #
5083: # Returns:
5084: # 1 - Processing should continue.
5085: #
5086: sub validate_course_owner_handler {
5087: my ($cmd, $tail, $client) = @_;
5088: my $userinput = "$cmd:$tail";
1.470 raeburn 5089: my ($inst_course_id, $owner, $cdom, $coowners) = split(/:/, $tail);
5090:
1.336 raeburn 5091: $owner = &unescape($owner);
1.470 raeburn 5092: $coowners = &unescape($coowners);
5093: my $outcome = &localenroll::new_course($inst_course_id,$owner,$cdom,$coowners);
1.387 albertel 5094: &Reply($client, \$outcome, $userinput);
1.248 foxr 5095:
5096:
5097:
5098: return 1;
5099: }
5100: ®ister_handler("autonewcourse", \&validate_course_owner_handler, 0, 1, 0);
1.263 albertel 5101:
1.248 foxr 5102: #
5103: # Validate a course section in the official schedule of classes
5104: # from the institutions point of view (part of autoenrollment).
5105: #
5106: # Formal Parameters:
5107: # $cmd - The command request that got us dispatched.
5108: # $tail - The tail of the command. In this case,
5109: # this is a colon separated set of words that will be split
5110: # into:
5111: # $inst_course_id - The course/section id from the
5112: # institutions point of view.
5113: # $cdom - The domain from the institutions
5114: # point of view.
5115: # $client - Socket open on the client.
5116: # Returns:
5117: # 1 - Indicating processing should continue.
5118: #
5119: sub validate_course_section_handler {
5120: my ($cmd, $tail, $client) = @_;
5121: my $userinput = "$cmd:$tail";
5122: my ($inst_course_id, $cdom) = split(/:/, $tail);
5123:
5124: my $outcome=&localenroll::validate_courseID($inst_course_id,$cdom);
1.387 albertel 5125: &Reply($client, \$outcome, $userinput);
1.248 foxr 5126:
5127:
5128: return 1;
5129: }
5130: ®ister_handler("autovalidatecourse", \&validate_course_section_handler, 0, 1, 0);
5131:
5132: #
1.340 raeburn 5133: # Validate course owner's access to enrollment data for specific class section.
5134: #
5135: #
5136: # Formal Parameters:
5137: # $cmd - The command request that got us dispatched.
5138: # $tail - The tail of the command. In this case this is a colon separated
5139: # set of words that will be split into:
5140: # $inst_class - Institutional code for the specific class section
5141: # $courseowner - The escaped username:domain of the course owner
5142: # $cdom - The domain of the course from the institution's
5143: # point of view.
5144: # $client - The socket open on the client.
5145: # Returns:
5146: # 1 - continue processing.
5147: #
5148:
5149: sub validate_class_access_handler {
5150: my ($cmd, $tail, $client) = @_;
5151: my $userinput = "$cmd:$tail";
1.383 raeburn 5152: my ($inst_class,$ownerlist,$cdom) = split(/:/, $tail);
1.392 raeburn 5153: my $owners = &unescape($ownerlist);
1.341 albertel 5154: my $outcome;
5155: eval {
5156: local($SIG{__DIE__})='DEFAULT';
1.392 raeburn 5157: $outcome=&localenroll::check_section($inst_class,$owners,$cdom);
1.341 albertel 5158: };
1.387 albertel 5159: &Reply($client,\$outcome, $userinput);
1.340 raeburn 5160:
5161: return 1;
5162: }
5163: ®ister_handler("autovalidateclass_sec", \&validate_class_access_handler, 0, 1, 0);
5164:
5165: #
5166: # Create a password for a new LON-CAPA user added by auto-enrollment.
5167: # Only used for case where authentication method for new user is localauth
1.248 foxr 5168: #
5169: # Formal Parameters:
5170: # $cmd - The command request that got us dispatched.
5171: # $tail - The tail of the command. In this case this is a colon separated
5172: # set of words that will be split into:
1.340 raeburn 5173: # $authparam - An authentication parameter (localauth parameter).
1.248 foxr 5174: # $cdom - The domain of the course from the institution's
5175: # point of view.
5176: # $client - The socket open on the client.
5177: # Returns:
5178: # 1 - continue processing.
5179: #
5180: sub create_auto_enroll_password_handler {
5181: my ($cmd, $tail, $client) = @_;
5182: my $userinput = "$cmd:$tail";
5183:
5184: my ($authparam, $cdom) = split(/:/, $userinput);
5185:
5186: my ($create_passwd,$authchk);
5187: ($authparam,
5188: $create_passwd,
5189: $authchk) = &localenroll::create_password($authparam,$cdom);
5190:
5191: &Reply($client, &escape($authparam.':'.$create_passwd.':'.$authchk)."\n",
5192: $userinput);
5193:
5194:
5195: return 1;
5196: }
5197: ®ister_handler("autocreatepassword", \&create_auto_enroll_password_handler,
5198: 0, 1, 0);
5199:
5200: # Retrieve and remove temporary files created by/during autoenrollment.
5201: #
5202: # Formal Parameters:
5203: # $cmd - The command that got us dispatched.
5204: # $tail - The tail of the command. In our case this is a colon
5205: # separated list that will be split into:
5206: # $filename - The name of the file to remove.
5207: # The filename is given as a path relative to
5208: # the LonCAPA temp file directory.
5209: # $client - Socket open on the client.
5210: #
5211: # Returns:
5212: # 1 - Continue processing.
5213: sub retrieve_auto_file_handler {
5214: my ($cmd, $tail, $client) = @_;
5215: my $userinput = "cmd:$tail";
5216:
5217: my ($filename) = split(/:/, $tail);
5218:
5219: my $source = $perlvar{'lonDaemons'}.'/tmp/'.$filename;
5220: if ( (-e $source) && ($filename ne '') ) {
5221: my $reply = '';
5222: if (open(my $fh,$source)) {
5223: while (<$fh>) {
5224: chomp($_);
5225: $_ =~ s/^\s+//g;
5226: $_ =~ s/\s+$//g;
5227: $reply .= $_;
5228: }
5229: close($fh);
5230: &Reply($client, &escape($reply)."\n", $userinput);
5231:
5232: # Does this have to be uncommented??!? (RF).
5233: #
5234: # unlink($source);
5235: } else {
5236: &Failure($client, "error\n", $userinput);
5237: }
5238: } else {
5239: &Failure($client, "error\n", $userinput);
5240: }
5241:
5242:
5243: return 1;
5244: }
5245: ®ister_handler("autoretrieve", \&retrieve_auto_file_handler, 0,1,0);
5246:
1.423 raeburn 5247: sub crsreq_checks_handler {
5248: my ($cmd, $tail, $client) = @_;
5249: my $userinput = "$cmd:$tail";
5250: my $dom = $tail;
5251: my $result;
1.424 raeburn 5252: my @reqtypes = ('official','unofficial','community');
1.423 raeburn 5253: eval {
5254: local($SIG{__DIE__})='DEFAULT';
5255: my %validations;
1.424 raeburn 5256: my $response = &localenroll::crsreq_checks($dom,\@reqtypes,
5257: \%validations);
1.423 raeburn 5258: if ($response eq 'ok') {
5259: foreach my $key (keys(%validations)) {
5260: $result .= &escape($key).'='.&Apache::lonnet::freeze_escape($validations{$key}).'&';
5261: }
5262: $result =~ s/\&$//;
5263: } else {
5264: $result = 'error';
5265: }
5266: };
5267: if (!$@) {
5268: &Reply($client, \$result, $userinput);
5269: } else {
5270: &Failure($client,"unknown_cmd\n",$userinput);
5271: }
5272: return 1;
5273: }
5274: ®ister_handler("autocrsreqchecks", \&crsreq_checks_handler, 0, 1, 0);
5275:
5276: sub validate_crsreq_handler {
5277: my ($cmd, $tail, $client) = @_;
5278: my $userinput = "$cmd:$tail";
5279: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist) = split(/:/, $tail);
5280: $instcode = &unescape($instcode);
5281: $owner = &unescape($owner);
5282: $crstype = &unescape($crstype);
5283: $inststatuslist = &unescape($inststatuslist);
5284: $instcode = &unescape($instcode);
5285: $instseclist = &unescape($instseclist);
5286: my $outcome;
5287: eval {
5288: local($SIG{__DIE__})='DEFAULT';
5289: $outcome = &localenroll::validate_crsreq($dom,$owner,$crstype,
5290: $inststatuslist,$instcode,
5291: $instseclist);
5292: };
5293: if (!$@) {
5294: &Reply($client, \$outcome, $userinput);
5295: } else {
5296: &Failure($client,"unknown_cmd\n",$userinput);
5297: }
5298: return 1;
5299: }
5300: ®ister_handler("autocrsreqvalidation", \&validate_crsreq_handler, 0, 1, 0);
5301:
1.248 foxr 5302: #
5303: # Read and retrieve institutional code format (for support form).
5304: # Formal Parameters:
5305: # $cmd - Command that dispatched us.
5306: # $tail - Tail of the command. In this case it conatins
5307: # the course domain and the coursename.
5308: # $client - Socket open on the client.
5309: # Returns:
5310: # 1 - Continue processing.
5311: #
5312: sub get_institutional_code_format_handler {
5313: my ($cmd, $tail, $client) = @_;
5314: my $userinput = "$cmd:$tail";
5315:
5316: my $reply;
5317: my($cdom,$course) = split(/:/,$tail);
5318: my @pairs = split/\&/,$course;
5319: my %instcodes = ();
5320: my %codes = ();
5321: my @codetitles = ();
5322: my %cat_titles = ();
5323: my %cat_order = ();
5324: foreach (@pairs) {
5325: my ($key,$value) = split/=/,$_;
5326: $instcodes{&unescape($key)} = &unescape($value);
5327: }
5328: my $formatreply = &localenroll::instcode_format($cdom,
5329: \%instcodes,
5330: \%codes,
5331: \@codetitles,
5332: \%cat_titles,
5333: \%cat_order);
5334: if ($formatreply eq 'ok') {
1.365 albertel 5335: my $codes_str = &Apache::lonnet::hash2str(%codes);
5336: my $codetitles_str = &Apache::lonnet::array2str(@codetitles);
5337: my $cat_titles_str = &Apache::lonnet::hash2str(%cat_titles);
5338: my $cat_order_str = &Apache::lonnet::hash2str(%cat_order);
1.248 foxr 5339: &Reply($client,
5340: $codes_str.':'.$codetitles_str.':'.$cat_titles_str.':'
5341: .$cat_order_str."\n",
5342: $userinput);
5343: } else {
5344: # this else branch added by RF since if not ok, lonc will
5345: # hang waiting on reply until timeout.
5346: #
5347: &Reply($client, "format_error\n", $userinput);
5348: }
5349:
5350: return 1;
5351: }
1.265 albertel 5352: ®ister_handler("autoinstcodeformat",
5353: \&get_institutional_code_format_handler,0,1,0);
1.246 foxr 5354:
1.345 raeburn 5355: sub get_institutional_defaults_handler {
5356: my ($cmd, $tail, $client) = @_;
5357: my $userinput = "$cmd:$tail";
5358:
5359: my $dom = $tail;
5360: my %defaults_hash;
5361: my @code_order;
5362: my $outcome;
5363: eval {
5364: local($SIG{__DIE__})='DEFAULT';
5365: $outcome = &localenroll::instcode_defaults($dom,\%defaults_hash,
5366: \@code_order);
5367: };
5368: if (!$@) {
5369: if ($outcome eq 'ok') {
5370: my $result='';
5371: while (my ($key,$value) = each(%defaults_hash)) {
5372: $result.=&escape($key).'='.&escape($value).'&';
5373: }
5374: $result .= 'code_order='.&escape(join('&',@code_order));
1.387 albertel 5375: &Reply($client,\$result,$userinput);
1.345 raeburn 5376: } else {
5377: &Reply($client,"error\n", $userinput);
5378: }
5379: } else {
5380: &Failure($client,"unknown_cmd\n",$userinput);
5381: }
5382: }
5383: ®ister_handler("autoinstcodedefaults",
5384: \&get_institutional_defaults_handler,0,1,0);
5385:
1.416 raeburn 5386: sub get_possible_instcodes_handler {
5387: my ($cmd, $tail, $client) = @_;
5388: my $userinput = "$cmd:$tail";
5389:
5390: my $reply;
5391: my $cdom = $tail;
1.417 raeburn 5392: my (@codetitles,%cat_titles,%cat_order,@code_order);
1.416 raeburn 5393: my $formatreply = &localenroll::possible_instcodes($cdom,
5394: \@codetitles,
5395: \%cat_titles,
1.417 raeburn 5396: \%cat_order,
5397: \@code_order);
1.416 raeburn 5398: if ($formatreply eq 'ok') {
5399: my $result = join('&',map {&escape($_);} (@codetitles)).':';
1.417 raeburn 5400: $result .= join('&',map {&escape($_);} (@code_order)).':';
1.416 raeburn 5401: foreach my $key (keys(%cat_titles)) {
5402: $result .= &escape($key).'='.&Apache::lonnet::freeze_escape($cat_titles{$key}).'&';
5403: }
5404: $result =~ s/\&$//;
5405: $result .= ':';
5406: foreach my $key (keys(%cat_order)) {
5407: $result .= &escape($key).'='.&Apache::lonnet::freeze_escape($cat_order{$key}).'&';
5408: }
5409: $result =~ s/\&$//;
5410: &Reply($client,\$result,$userinput);
5411: } else {
5412: &Reply($client, "format_error\n", $userinput);
5413: }
5414: return 1;
5415: }
5416: ®ister_handler("autopossibleinstcodes",
5417: \&get_possible_instcodes_handler,0,1,0);
5418:
1.381 raeburn 5419: sub get_institutional_user_rules {
5420: my ($cmd, $tail, $client) = @_;
5421: my $userinput = "$cmd:$tail";
5422: my $dom = &unescape($tail);
5423: my (%rules_hash,@rules_order);
5424: my $outcome;
5425: eval {
5426: local($SIG{__DIE__})='DEFAULT';
5427: $outcome = &localenroll::username_rules($dom,\%rules_hash,\@rules_order);
5428: };
5429: if (!$@) {
5430: if ($outcome eq 'ok') {
5431: my $result;
5432: foreach my $key (keys(%rules_hash)) {
5433: $result .= &escape($key).'='.&Apache::lonnet::freeze_escape($rules_hash{$key}).'&';
5434: }
5435: $result =~ s/\&$//;
5436: $result .= ':';
5437: if (@rules_order > 0) {
5438: foreach my $item (@rules_order) {
5439: $result .= &escape($item).'&';
5440: }
5441: }
5442: $result =~ s/\&$//;
1.387 albertel 5443: &Reply($client,\$result,$userinput);
1.381 raeburn 5444: } else {
5445: &Reply($client,"error\n", $userinput);
5446: }
5447: } else {
5448: &Failure($client,"unknown_cmd\n",$userinput);
5449: }
5450: }
5451: ®ister_handler("instuserrules",\&get_institutional_user_rules,0,1,0);
5452:
1.389 raeburn 5453: sub get_institutional_id_rules {
5454: my ($cmd, $tail, $client) = @_;
5455: my $userinput = "$cmd:$tail";
5456: my $dom = &unescape($tail);
5457: my (%rules_hash,@rules_order);
5458: my $outcome;
5459: eval {
5460: local($SIG{__DIE__})='DEFAULT';
5461: $outcome = &localenroll::id_rules($dom,\%rules_hash,\@rules_order);
5462: };
5463: if (!$@) {
5464: if ($outcome eq 'ok') {
5465: my $result;
5466: foreach my $key (keys(%rules_hash)) {
5467: $result .= &escape($key).'='.&Apache::lonnet::freeze_escape($rules_hash{$key}).'&';
5468: }
5469: $result =~ s/\&$//;
5470: $result .= ':';
5471: if (@rules_order > 0) {
5472: foreach my $item (@rules_order) {
5473: $result .= &escape($item).'&';
5474: }
5475: }
5476: $result =~ s/\&$//;
5477: &Reply($client,\$result,$userinput);
5478: } else {
5479: &Reply($client,"error\n", $userinput);
5480: }
5481: } else {
5482: &Failure($client,"unknown_cmd\n",$userinput);
5483: }
5484: }
5485: ®ister_handler("instidrules",\&get_institutional_id_rules,0,1,0);
5486:
1.397 raeburn 5487: sub get_institutional_selfcreate_rules {
1.396 raeburn 5488: my ($cmd, $tail, $client) = @_;
5489: my $userinput = "$cmd:$tail";
5490: my $dom = &unescape($tail);
5491: my (%rules_hash,@rules_order);
5492: my $outcome;
5493: eval {
5494: local($SIG{__DIE__})='DEFAULT';
1.397 raeburn 5495: $outcome = &localenroll::selfcreate_rules($dom,\%rules_hash,\@rules_order);
1.396 raeburn 5496: };
5497: if (!$@) {
5498: if ($outcome eq 'ok') {
5499: my $result;
5500: foreach my $key (keys(%rules_hash)) {
5501: $result .= &escape($key).'='.&Apache::lonnet::freeze_escape($rules_hash{$key}).'&';
5502: }
5503: $result =~ s/\&$//;
5504: $result .= ':';
5505: if (@rules_order > 0) {
5506: foreach my $item (@rules_order) {
5507: $result .= &escape($item).'&';
5508: }
5509: }
5510: $result =~ s/\&$//;
5511: &Reply($client,\$result,$userinput);
5512: } else {
5513: &Reply($client,"error\n", $userinput);
5514: }
5515: } else {
5516: &Failure($client,"unknown_cmd\n",$userinput);
5517: }
5518: }
1.397 raeburn 5519: ®ister_handler("instemailrules",\&get_institutional_selfcreate_rules,0,1,0);
1.396 raeburn 5520:
1.381 raeburn 5521:
5522: sub institutional_username_check {
5523: my ($cmd, $tail, $client) = @_;
5524: my $userinput = "$cmd:$tail";
5525: my %rulecheck;
5526: my $outcome;
5527: my ($udom,$uname,@rules) = split(/:/,$tail);
5528: $udom = &unescape($udom);
5529: $uname = &unescape($uname);
5530: @rules = map {&unescape($_);} (@rules);
5531: eval {
5532: local($SIG{__DIE__})='DEFAULT';
5533: $outcome = &localenroll::username_check($udom,$uname,\@rules,\%rulecheck);
5534: };
5535: if (!$@) {
5536: if ($outcome eq 'ok') {
5537: my $result='';
5538: foreach my $key (keys(%rulecheck)) {
5539: $result.=&escape($key).'='.&Apache::lonnet::freeze_escape($rulecheck{$key}).'&';
5540: }
1.387 albertel 5541: &Reply($client,\$result,$userinput);
1.381 raeburn 5542: } else {
5543: &Reply($client,"error\n", $userinput);
5544: }
5545: } else {
5546: &Failure($client,"unknown_cmd\n",$userinput);
5547: }
5548: }
5549: ®ister_handler("instrulecheck",\&institutional_username_check,0,1,0);
5550:
1.389 raeburn 5551: sub institutional_id_check {
5552: my ($cmd, $tail, $client) = @_;
5553: my $userinput = "$cmd:$tail";
5554: my %rulecheck;
5555: my $outcome;
5556: my ($udom,$id,@rules) = split(/:/,$tail);
5557: $udom = &unescape($udom);
5558: $id = &unescape($id);
5559: @rules = map {&unescape($_);} (@rules);
5560: eval {
5561: local($SIG{__DIE__})='DEFAULT';
5562: $outcome = &localenroll::id_check($udom,$id,\@rules,\%rulecheck);
5563: };
5564: if (!$@) {
5565: if ($outcome eq 'ok') {
5566: my $result='';
5567: foreach my $key (keys(%rulecheck)) {
5568: $result.=&escape($key).'='.&Apache::lonnet::freeze_escape($rulecheck{$key}).'&';
5569: }
5570: &Reply($client,\$result,$userinput);
5571: } else {
5572: &Reply($client,"error\n", $userinput);
5573: }
5574: } else {
5575: &Failure($client,"unknown_cmd\n",$userinput);
5576: }
5577: }
5578: ®ister_handler("instidrulecheck",\&institutional_id_check,0,1,0);
1.345 raeburn 5579:
1.397 raeburn 5580: sub institutional_selfcreate_check {
1.396 raeburn 5581: my ($cmd, $tail, $client) = @_;
5582: my $userinput = "$cmd:$tail";
5583: my %rulecheck;
5584: my $outcome;
5585: my ($udom,$email,@rules) = split(/:/,$tail);
5586: $udom = &unescape($udom);
5587: $email = &unescape($email);
5588: @rules = map {&unescape($_);} (@rules);
5589: eval {
5590: local($SIG{__DIE__})='DEFAULT';
1.397 raeburn 5591: $outcome = &localenroll::selfcreate_check($udom,$email,\@rules,\%rulecheck);
1.396 raeburn 5592: };
5593: if (!$@) {
5594: if ($outcome eq 'ok') {
5595: my $result='';
5596: foreach my $key (keys(%rulecheck)) {
5597: $result.=&escape($key).'='.&Apache::lonnet::freeze_escape($rulecheck{$key}).'&';
5598: }
5599: &Reply($client,\$result,$userinput);
5600: } else {
5601: &Reply($client,"error\n", $userinput);
5602: }
5603: } else {
5604: &Failure($client,"unknown_cmd\n",$userinput);
5605: }
5606: }
1.397 raeburn 5607: ®ister_handler("instselfcreatecheck",\&institutional_selfcreate_check,0,1,0);
1.396 raeburn 5608:
1.317 raeburn 5609: # Get domain specific conditions for import of student photographs to a course
5610: #
5611: # Retrieves information from photo_permission subroutine in localenroll.
5612: # Returns outcome (ok) if no processing errors, and whether course owner is
5613: # required to accept conditions of use (yes/no).
5614: #
5615: #
5616: sub photo_permission_handler {
5617: my ($cmd, $tail, $client) = @_;
5618: my $userinput = "$cmd:$tail";
5619: my $cdom = $tail;
5620: my ($perm_reqd,$conditions);
1.320 albertel 5621: my $outcome;
5622: eval {
5623: local($SIG{__DIE__})='DEFAULT';
5624: $outcome = &localenroll::photo_permission($cdom,\$perm_reqd,
5625: \$conditions);
5626: };
5627: if (!$@) {
5628: &Reply($client, &escape($outcome.':'.$perm_reqd.':'. $conditions)."\n",
5629: $userinput);
5630: } else {
5631: &Failure($client,"unknown_cmd\n",$userinput);
5632: }
5633: return 1;
1.317 raeburn 5634: }
5635: ®ister_handler("autophotopermission",\&photo_permission_handler,0,1,0);
5636:
5637: #
5638: # Checks if student photo is available for a user in the domain, in the user's
5639: # directory (in /userfiles/internal/studentphoto.jpg).
5640: # Uses localstudentphoto:fetch() to ensure there is an up to date copy of
5641: # the student's photo.
5642:
5643: sub photo_check_handler {
5644: my ($cmd, $tail, $client) = @_;
5645: my $userinput = "$cmd:$tail";
5646: my ($udom,$uname,$pid) = split(/:/,$tail);
5647: $udom = &unescape($udom);
5648: $uname = &unescape($uname);
5649: $pid = &unescape($pid);
5650: my $path=&propath($udom,$uname).'/userfiles/internal/';
5651: if (!-e $path) {
5652: &mkpath($path);
5653: }
5654: my $response;
5655: my $result = &localstudentphoto::fetch($udom,$uname,$pid,\$response);
5656: $result .= ':'.$response;
5657: &Reply($client, &escape($result)."\n",$userinput);
1.320 albertel 5658: return 1;
1.317 raeburn 5659: }
5660: ®ister_handler("autophotocheck",\&photo_check_handler,0,1,0);
5661:
5662: #
5663: # Retrieve information from localenroll about whether to provide a button
5664: # for users who have enbled import of student photos to initiate an
5665: # update of photo files for registered students. Also include
5666: # comment to display alongside button.
5667:
5668: sub photo_choice_handler {
5669: my ($cmd, $tail, $client) = @_;
5670: my $userinput = "$cmd:$tail";
5671: my $cdom = &unescape($tail);
1.320 albertel 5672: my ($update,$comment);
5673: eval {
5674: local($SIG{__DIE__})='DEFAULT';
5675: ($update,$comment) = &localenroll::manager_photo_update($cdom);
5676: };
5677: if (!$@) {
5678: &Reply($client,&escape($update).':'.&escape($comment)."\n",$userinput);
5679: } else {
5680: &Failure($client,"unknown_cmd\n",$userinput);
5681: }
5682: return 1;
1.317 raeburn 5683: }
5684: ®ister_handler("autophotochoice",\&photo_choice_handler,0,1,0);
5685:
1.265 albertel 5686: #
5687: # Gets a student's photo to exist (in the correct image type) in the user's
5688: # directory.
5689: # Formal Parameters:
5690: # $cmd - The command request that got us dispatched.
5691: # $tail - A colon separated set of words that will be split into:
5692: # $domain - student's domain
5693: # $uname - student username
5694: # $type - image type desired
5695: # $client - The socket open on the client.
5696: # Returns:
5697: # 1 - continue processing.
1.317 raeburn 5698:
1.265 albertel 5699: sub student_photo_handler {
5700: my ($cmd, $tail, $client) = @_;
1.317 raeburn 5701: my ($domain,$uname,$ext,$type) = split(/:/, $tail);
1.265 albertel 5702:
1.317 raeburn 5703: my $path=&propath($domain,$uname). '/userfiles/internal/';
5704: my $filename = 'studentphoto.'.$ext;
5705: if ($type eq 'thumbnail') {
5706: $filename = 'studentphoto_tn.'.$ext;
5707: }
5708: if (-e $path.$filename) {
1.265 albertel 5709: &Reply($client,"ok\n","$cmd:$tail");
5710: return 1;
5711: }
5712: &mkpath($path);
1.317 raeburn 5713: my $file;
5714: if ($type eq 'thumbnail') {
1.320 albertel 5715: eval {
5716: local($SIG{__DIE__})='DEFAULT';
5717: $file=&localstudentphoto::fetch_thumbnail($domain,$uname);
5718: };
1.317 raeburn 5719: } else {
5720: $file=&localstudentphoto::fetch($domain,$uname);
5721: }
1.265 albertel 5722: if (!$file) {
5723: &Failure($client,"unavailable\n","$cmd:$tail");
5724: return 1;
5725: }
1.317 raeburn 5726: if (!-e $path.$filename) { &convert_photo($file,$path.$filename); }
5727: if (-e $path.$filename) {
1.265 albertel 5728: &Reply($client,"ok\n","$cmd:$tail");
5729: return 1;
5730: }
5731: &Failure($client,"unable_to_convert\n","$cmd:$tail");
5732: return 1;
5733: }
5734: ®ister_handler("studentphoto", \&student_photo_handler, 0, 1, 0);
1.246 foxr 5735:
1.361 raeburn 5736: sub inst_usertypes_handler {
5737: my ($cmd, $domain, $client) = @_;
5738: my $res;
5739: my $userinput = $cmd.":".$domain; # For logging purposes.
1.370 albertel 5740: my (%typeshash,@order,$result);
5741: eval {
5742: local($SIG{__DIE__})='DEFAULT';
5743: $result=&localenroll::inst_usertypes($domain,\%typeshash,\@order);
5744: };
5745: if ($result eq 'ok') {
1.361 raeburn 5746: if (keys(%typeshash) > 0) {
5747: foreach my $key (keys(%typeshash)) {
5748: $res.=&escape($key).'='.&escape($typeshash{$key}).'&';
5749: }
5750: }
5751: $res=~s/\&$//;
5752: $res .= ':';
5753: if (@order > 0) {
5754: foreach my $item (@order) {
5755: $res .= &escape($item).'&';
5756: }
5757: }
5758: $res=~s/\&$//;
5759: }
1.387 albertel 5760: &Reply($client, \$res, $userinput);
1.361 raeburn 5761: return 1;
5762: }
5763: ®ister_handler("inst_usertypes", \&inst_usertypes_handler, 0, 1, 0);
5764:
1.264 albertel 5765: # mkpath makes all directories for a file, expects an absolute path with a
5766: # file or a trailing / if just a dir is passed
5767: # returns 1 on success 0 on failure
5768: sub mkpath {
5769: my ($file)=@_;
5770: my @parts=split(/\//,$file,-1);
5771: my $now=$parts[0].'/'.$parts[1].'/'.$parts[2];
5772: for (my $i=3;$i<= ($#parts-1);$i++) {
1.265 albertel 5773: $now.='/'.$parts[$i];
1.264 albertel 5774: if (!-e $now) {
5775: if (!mkdir($now,0770)) { return 0; }
5776: }
5777: }
5778: return 1;
5779: }
5780:
1.207 foxr 5781: #---------------------------------------------------------------
5782: #
5783: # Getting, decoding and dispatching requests:
5784: #
5785: #
5786: # Get a Request:
5787: # Gets a Request message from the client. The transaction
5788: # is defined as a 'line' of text. We remove the new line
5789: # from the text line.
1.226 foxr 5790: #
1.211 albertel 5791: sub get_request {
1.207 foxr 5792: my $input = <$client>;
5793: chomp($input);
1.226 foxr 5794:
1.234 foxr 5795: &Debug("get_request: Request = $input\n");
1.207 foxr 5796:
5797: &status('Processing '.$clientname.':'.$input);
5798:
5799: return $input;
5800: }
1.212 foxr 5801: #---------------------------------------------------------------
5802: #
5803: # Process a request. This sub should shrink as each action
5804: # gets farmed out into a separat sub that is registered
5805: # with the dispatch hash.
5806: #
5807: # Parameters:
5808: # user_input - The request received from the client (lonc).
5809: # Returns:
5810: # true to keep processing, false if caller should exit.
5811: #
5812: sub process_request {
5813: my ($userinput) = @_; # Easier for now to break style than to
5814: # fix all the userinput -> user_input.
5815: my $wasenc = 0; # True if request was encrypted.
5816: # ------------------------------------------------------------ See if encrypted
1.322 albertel 5817: # for command
5818: # sethost:<server>
5819: # <command>:<args>
5820: # we just send it to the processor
5821: # for
5822: # sethost:<server>:<command>:<args>
5823: # we do the implict set host and then do the command
5824: if ($userinput =~ /^sethost:/) {
5825: (my $cmd,my $newid,$userinput) = split(':',$userinput,3);
5826: if (defined($userinput)) {
5827: &sethost("$cmd:$newid");
5828: } else {
5829: $userinput = "$cmd:$newid";
5830: }
5831: }
5832:
1.212 foxr 5833: if ($userinput =~ /^enc/) {
5834: $userinput = decipher($userinput);
5835: $wasenc=1;
5836: if(!$userinput) { # Cipher not defined.
1.251 foxr 5837: &Failure($client, "error: Encrypted data without negotated key\n");
1.212 foxr 5838: return 0;
5839: }
5840: }
5841: Debug("process_request: $userinput\n");
5842:
1.213 foxr 5843: #
5844: # The 'correct way' to add a command to lond is now to
5845: # write a sub to execute it and Add it to the command dispatch
5846: # hash via a call to register_handler.. The comments to that
5847: # sub should give you enough to go on to show how to do this
5848: # along with the examples that are building up as this code
5849: # is getting refactored. Until all branches of the
5850: # if/elseif monster below have been factored out into
5851: # separate procesor subs, if the dispatch hash is missing
5852: # the command keyword, we will fall through to the remainder
5853: # of the if/else chain below in order to keep this thing in
5854: # working order throughout the transmogrification.
5855:
5856: my ($command, $tail) = split(/:/, $userinput, 2);
5857: chomp($command);
5858: chomp($tail);
5859: $tail =~ s/(\r)//; # This helps people debugging with e.g. telnet.
1.214 foxr 5860: $command =~ s/(\r)//; # And this too for parameterless commands.
5861: if(!$tail) {
5862: $tail =""; # defined but blank.
5863: }
1.213 foxr 5864:
5865: &Debug("Command received: $command, encoded = $wasenc");
5866:
5867: if(defined $Dispatcher{$command}) {
5868:
5869: my $dispatch_info = $Dispatcher{$command};
5870: my $handler = $$dispatch_info[0];
5871: my $need_encode = $$dispatch_info[1];
5872: my $client_types = $$dispatch_info[2];
5873: Debug("Matched dispatch hash: mustencode: $need_encode "
5874: ."ClientType $client_types");
5875:
5876: # Validate the request:
5877:
5878: my $ok = 1;
5879: my $requesterprivs = 0;
5880: if(&isClient()) {
5881: $requesterprivs |= $CLIENT_OK;
5882: }
5883: if(&isManager()) {
5884: $requesterprivs |= $MANAGER_OK;
5885: }
5886: if($need_encode && (!$wasenc)) {
5887: Debug("Must encode but wasn't: $need_encode $wasenc");
5888: $ok = 0;
5889: }
5890: if(($client_types & $requesterprivs) == 0) {
5891: Debug("Client not privileged to do this operation");
5892: $ok = 0;
5893: }
5894:
5895: if($ok) {
5896: Debug("Dispatching to handler $command $tail");
5897: my $keep_going = &$handler($command, $tail, $client);
5898: return $keep_going;
5899: } else {
5900: Debug("Refusing to dispatch because client did not match requirements");
5901: Failure($client, "refused\n", $userinput);
5902: return 1;
5903: }
5904:
5905: }
5906:
1.262 foxr 5907: print $client "unknown_cmd\n";
1.212 foxr 5908: # -------------------------------------------------------------------- complete
5909: Debug("process_request - returning 1");
5910: return 1;
5911: }
1.207 foxr 5912: #
5913: # Decipher encoded traffic
5914: # Parameters:
5915: # input - Encoded data.
5916: # Returns:
5917: # Decoded data or undef if encryption key was not yet negotiated.
5918: # Implicit input:
5919: # cipher - This global holds the negotiated encryption key.
5920: #
1.211 albertel 5921: sub decipher {
1.207 foxr 5922: my ($input) = @_;
5923: my $output = '';
1.212 foxr 5924:
5925:
1.207 foxr 5926: if($cipher) {
5927: my($enc, $enclength, $encinput) = split(/:/, $input);
5928: for(my $encidx = 0; $encidx < length($encinput); $encidx += 16) {
5929: $output .=
5930: $cipher->decrypt(pack("H16", substr($encinput, $encidx, 16)));
5931: }
5932: return substr($output, 0, $enclength);
5933: } else {
5934: return undef;
5935: }
5936: }
5937:
5938: #
5939: # Register a command processor. This function is invoked to register a sub
5940: # to process a request. Once registered, the ProcessRequest sub can automatically
5941: # dispatch requests to an appropriate sub, and do the top level validity checking
5942: # as well:
5943: # - Is the keyword recognized.
5944: # - Is the proper client type attempting the request.
5945: # - Is the request encrypted if it has to be.
5946: # Parameters:
5947: # $request_name - Name of the request being registered.
5948: # This is the command request that will match
5949: # against the hash keywords to lookup the information
5950: # associated with the dispatch information.
5951: # $procedure - Reference to a sub to call to process the request.
5952: # All subs get called as follows:
5953: # Procedure($cmd, $tail, $replyfd, $key)
5954: # $cmd - the actual keyword that invoked us.
5955: # $tail - the tail of the request that invoked us.
5956: # $replyfd- File descriptor connected to the client
5957: # $must_encode - True if the request must be encoded to be good.
5958: # $client_ok - True if it's ok for a client to request this.
5959: # $manager_ok - True if it's ok for a manager to request this.
5960: # Side effects:
5961: # - On success, the Dispatcher hash has an entry added for the key $RequestName
5962: # - On failure, the program will die as it's a bad internal bug to try to
5963: # register a duplicate command handler.
5964: #
1.211 albertel 5965: sub register_handler {
1.212 foxr 5966: my ($request_name,$procedure,$must_encode, $client_ok,$manager_ok) = @_;
1.207 foxr 5967:
5968: # Don't allow duplication#
5969:
5970: if (defined $Dispatcher{$request_name}) {
5971: die "Attempting to define a duplicate request handler for $request_name\n";
5972: }
5973: # Build the client type mask:
5974:
5975: my $client_type_mask = 0;
5976: if($client_ok) {
5977: $client_type_mask |= $CLIENT_OK;
5978: }
5979: if($manager_ok) {
5980: $client_type_mask |= $MANAGER_OK;
5981: }
5982:
5983: # Enter the hash:
5984:
5985: my @entry = ($procedure, $must_encode, $client_type_mask);
5986:
5987: $Dispatcher{$request_name} = \@entry;
5988:
5989: }
5990:
5991:
5992: #------------------------------------------------------------------
5993:
5994:
5995:
5996:
1.141 foxr 5997: #
1.96 foxr 5998: # Convert an error return code from lcpasswd to a string value.
5999: #
6000: sub lcpasswdstrerror {
6001: my $ErrorCode = shift;
1.97 foxr 6002: if(($ErrorCode < 0) || ($ErrorCode > $lastpwderror)) {
1.96 foxr 6003: return "lcpasswd Unrecognized error return value ".$ErrorCode;
6004: } else {
1.98 foxr 6005: return $passwderrors[$ErrorCode];
1.96 foxr 6006: }
6007: }
6008:
1.23 harris41 6009: # grabs exception and records it to log before exiting
6010: sub catchexception {
1.27 albertel 6011: my ($error)=@_;
1.25 www 6012: $SIG{'QUIT'}='DEFAULT';
6013: $SIG{__DIE__}='DEFAULT';
1.165 albertel 6014: &status("Catching exception");
1.190 albertel 6015: &logthis("<font color='red'>CRITICAL: "
1.373 albertel 6016: ."ABNORMAL EXIT. Child $$ for server ".$perlvar{'lonHostID'}." died through "
1.27 albertel 6017: ."a crash with this error msg->[$error]</font>");
1.57 www 6018: &logthis('Famous last words: '.$status.' - '.$lastlog);
1.27 albertel 6019: if ($client) { print $client "error: $error\n"; }
1.59 www 6020: $server->close();
1.27 albertel 6021: die($error);
1.23 harris41 6022: }
1.63 www 6023: sub timeout {
1.165 albertel 6024: &status("Handling Timeout");
1.190 albertel 6025: &logthis("<font color='red'>CRITICAL: TIME OUT ".$$."</font>");
1.63 www 6026: &catchexception('Timeout');
6027: }
1.22 harris41 6028: # -------------------------------- Set signal handlers to record abnormal exits
6029:
1.226 foxr 6030:
1.22 harris41 6031: $SIG{'QUIT'}=\&catchexception;
6032: $SIG{__DIE__}=\&catchexception;
6033:
1.81 matthew 6034: # ---------------------------------- Read loncapa_apache.conf and loncapa.conf
1.95 harris41 6035: &status("Read loncapa.conf and loncapa_apache.conf");
6036: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
1.141 foxr 6037: %perlvar=%{$perlvarref};
1.80 harris41 6038: undef $perlvarref;
1.19 www 6039:
1.35 harris41 6040: # ----------------------------- Make sure this process is running from user=www
6041: my $wwwid=getpwnam('www');
6042: if ($wwwid!=$<) {
1.134 albertel 6043: my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
6044: my $subj="LON: $currenthostid User ID mismatch";
1.37 harris41 6045: system("echo 'User ID mismatch. lond must be run as user www.' |\
1.35 harris41 6046: mailto $emailto -s '$subj' > /dev/null");
6047: exit 1;
6048: }
6049:
1.19 www 6050: # --------------------------------------------- Check if other instance running
6051:
6052: my $pidfile="$perlvar{'lonDaemons'}/logs/lond.pid";
6053:
6054: if (-e $pidfile) {
6055: my $lfh=IO::File->new("$pidfile");
6056: my $pide=<$lfh>;
6057: chomp($pide);
1.29 harris41 6058: if (kill 0 => $pide) { die "already running"; }
1.19 www 6059: }
1.1 albertel 6060:
6061: # ------------------------------------------------------------- Read hosts file
6062:
6063:
6064:
6065: # establish SERVER socket, bind and listen.
6066: $server = IO::Socket::INET->new(LocalPort => $perlvar{'londPort'},
6067: Type => SOCK_STREAM,
6068: Proto => 'tcp',
1.469 foxr 6069: ReuseAddr => 1,
1.1 albertel 6070: Listen => 10 )
1.29 harris41 6071: or die "making socket: $@\n";
1.1 albertel 6072:
6073: # --------------------------------------------------------- Do global variables
6074:
6075: # global variables
6076:
1.134 albertel 6077: my %children = (); # keys are current child process IDs
1.1 albertel 6078:
6079: sub REAPER { # takes care of dead children
6080: $SIG{CHLD} = \&REAPER;
1.165 albertel 6081: &status("Handling child death");
1.178 foxr 6082: my $pid;
6083: do {
6084: $pid = waitpid(-1,&WNOHANG());
6085: if (defined($children{$pid})) {
6086: &logthis("Child $pid died");
6087: delete($children{$pid});
1.183 albertel 6088: } elsif ($pid > 0) {
1.178 foxr 6089: &logthis("Unknown Child $pid died");
6090: }
6091: } while ( $pid > 0 );
6092: foreach my $child (keys(%children)) {
6093: $pid = waitpid($child,&WNOHANG());
6094: if ($pid > 0) {
6095: &logthis("Child $child - $pid looks like we missed it's death");
6096: delete($children{$pid});
6097: }
1.176 albertel 6098: }
1.165 albertel 6099: &status("Finished Handling child death");
1.1 albertel 6100: }
6101:
6102: sub HUNTSMAN { # signal handler for SIGINT
1.165 albertel 6103: &status("Killing children (INT)");
1.1 albertel 6104: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
6105: kill 'INT' => keys %children;
1.59 www 6106: &logthis("Free socket: ".shutdown($server,2)); # free up socket
1.1 albertel 6107: my $execdir=$perlvar{'lonDaemons'};
6108: unlink("$execdir/logs/lond.pid");
1.190 albertel 6109: &logthis("<font color='red'>CRITICAL: Shutting down</font>");
1.165 albertel 6110: &status("Done killing children");
1.1 albertel 6111: exit; # clean up with dignity
6112: }
6113:
6114: sub HUPSMAN { # signal handler for SIGHUP
6115: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
1.165 albertel 6116: &status("Killing children for restart (HUP)");
1.1 albertel 6117: kill 'INT' => keys %children;
1.59 www 6118: &logthis("Free socket: ".shutdown($server,2)); # free up socket
1.190 albertel 6119: &logthis("<font color='red'>CRITICAL: Restarting</font>");
1.134 albertel 6120: my $execdir=$perlvar{'lonDaemons'};
1.30 harris41 6121: unlink("$execdir/logs/lond.pid");
1.165 albertel 6122: &status("Restarting self (HUP)");
1.1 albertel 6123: exec("$execdir/lond"); # here we go again
6124: }
6125:
1.144 foxr 6126: #
1.148 foxr 6127: # Reload the Apache daemon's state.
1.150 foxr 6128: # This is done by invoking /home/httpd/perl/apachereload
6129: # a setuid perl script that can be root for us to do this job.
1.148 foxr 6130: #
6131: sub ReloadApache {
1.473 raeburn 6132: # --------------------------- Handle case of another apachereload process (locking)
1.474 raeburn 6133: if (&LONCAPA::try_to_lock('/tmp/lock_apachereload')) {
6134: my $execdir = $perlvar{'lonDaemons'};
6135: my $script = $execdir."/apachereload";
6136: system($script);
6137: unlink('/tmp/lock_apachereload'); # Remove the lock file.
6138: }
1.148 foxr 6139: }
6140:
6141: #
1.144 foxr 6142: # Called in response to a USR2 signal.
6143: # - Reread hosts.tab
6144: # - All children connected to hosts that were removed from hosts.tab
6145: # are killed via SIGINT
6146: # - All children connected to previously existing hosts are sent SIGUSR1
6147: # - Our internal hosts hash is updated to reflect the new contents of
6148: # hosts.tab causing connections from hosts added to hosts.tab to
6149: # now be honored.
6150: #
6151: sub UpdateHosts {
1.165 albertel 6152: &status("Reload hosts.tab");
1.147 foxr 6153: logthis('<font color="blue"> Updating connections </font>');
1.148 foxr 6154: #
6155: # The %children hash has the set of IP's we currently have children
6156: # on. These need to be matched against records in the hosts.tab
6157: # Any ip's no longer in the table get killed off they correspond to
6158: # either dropped or changed hosts. Note that the re-read of the table
6159: # will take care of new and changed hosts as connections come into being.
6160:
1.371 albertel 6161: &Apache::lonnet::reset_hosts_info();
1.148 foxr 6162:
1.368 albertel 6163: foreach my $child (keys(%children)) {
1.148 foxr 6164: my $childip = $children{$child};
1.374 albertel 6165: if ($childip ne '127.0.0.1'
6166: && !defined(&Apache::lonnet::get_hosts_from_ip($childip))) {
1.149 foxr 6167: logthis('<font color="blue"> UpdateHosts killing child '
6168: ." $child for ip $childip </font>");
1.148 foxr 6169: kill('INT', $child);
1.149 foxr 6170: } else {
6171: logthis('<font color="green"> keeping child for ip '
6172: ." $childip (pid=$child) </font>");
1.148 foxr 6173: }
6174: }
6175: ReloadApache;
1.165 albertel 6176: &status("Finished reloading hosts.tab");
1.144 foxr 6177: }
6178:
1.148 foxr 6179:
1.57 www 6180: sub checkchildren {
1.165 albertel 6181: &status("Checking on the children (sending signals)");
1.57 www 6182: &initnewstatus();
6183: &logstatus();
6184: &logthis('Going to check on the children');
1.134 albertel 6185: my $docdir=$perlvar{'lonDocRoot'};
1.61 harris41 6186: foreach (sort keys %children) {
1.221 albertel 6187: #sleep 1;
1.57 www 6188: unless (kill 'USR1' => $_) {
6189: &logthis ('Child '.$_.' is dead');
6190: &logstatus($$.' is dead');
1.221 albertel 6191: delete($children{$_});
1.57 www 6192: }
1.61 harris41 6193: }
1.63 www 6194: sleep 5;
1.212 foxr 6195: $SIG{ALRM} = sub { Debug("timeout");
6196: die "timeout"; };
1.113 albertel 6197: $SIG{__DIE__} = 'DEFAULT';
1.165 albertel 6198: &status("Checking on the children (waiting for reports)");
1.63 www 6199: foreach (sort keys %children) {
6200: unless (-e "$docdir/lon-status/londchld/$_.txt") {
1.113 albertel 6201: eval {
6202: alarm(300);
1.63 www 6203: &logthis('Child '.$_.' did not respond');
1.67 albertel 6204: kill 9 => $_;
1.131 albertel 6205: #$emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
6206: #$subj="LON: $currenthostid killed lond process $_";
6207: #my $result=`echo 'Killed lond process $_.' | mailto $emailto -s '$subj' > /dev/null`;
6208: #$execdir=$perlvar{'lonDaemons'};
6209: #$result=`/bin/cp $execdir/logs/lond.log $execdir/logs/lond.log.$_`;
1.221 albertel 6210: delete($children{$_});
1.113 albertel 6211: alarm(0);
6212: }
1.63 www 6213: }
6214: }
1.113 albertel 6215: $SIG{ALRM} = 'DEFAULT';
1.155 albertel 6216: $SIG{__DIE__} = \&catchexception;
1.165 albertel 6217: &status("Finished checking children");
1.221 albertel 6218: &logthis('Finished Checking children');
1.57 www 6219: }
6220:
1.1 albertel 6221: # --------------------------------------------------------------------- Logging
6222:
6223: sub logthis {
6224: my $message=shift;
6225: my $execdir=$perlvar{'lonDaemons'};
6226: my $fh=IO::File->new(">>$execdir/logs/lond.log");
6227: my $now=time;
6228: my $local=localtime($now);
1.58 www 6229: $lastlog=$local.': '.$message;
1.1 albertel 6230: print $fh "$local ($$): $message\n";
6231: }
6232:
1.77 foxr 6233: # ------------------------- Conditional log if $DEBUG true.
6234: sub Debug {
6235: my $message = shift;
6236: if($DEBUG) {
6237: &logthis($message);
6238: }
6239: }
1.161 foxr 6240:
6241: #
6242: # Sub to do replies to client.. this gives a hook for some
6243: # debug tracing too:
6244: # Parameters:
6245: # fd - File open on client.
6246: # reply - Text to send to client.
6247: # request - Original request from client.
6248: #
1.490 droeschl 6249: #NOTE $reply must be terminated by exactly *one* \n. If $reply is a reference
6250: #this is done automatically ($$reply must not contain any \n in this case).
6251: #If $reply is a string the caller has to ensure this.
1.161 foxr 6252: sub Reply {
1.192 foxr 6253: my ($fd, $reply, $request) = @_;
1.387 albertel 6254: if (ref($reply)) {
6255: print $fd $$reply;
6256: print $fd "\n";
6257: if ($DEBUG) { Debug("Request was $request Reply was $$reply"); }
6258: } else {
6259: print $fd $reply;
6260: if ($DEBUG) { Debug("Request was $request Reply was $reply"); }
6261: }
1.212 foxr 6262: $Transactions++;
6263: }
6264:
6265:
6266: #
6267: # Sub to report a failure.
6268: # This function:
6269: # - Increments the failure statistic counters.
6270: # - Invokes Reply to send the error message to the client.
6271: # Parameters:
6272: # fd - File descriptor open on the client
6273: # reply - Reply text to emit.
6274: # request - The original request message (used by Reply
6275: # to debug if that's enabled.
6276: # Implicit outputs:
6277: # $Failures- The number of failures is incremented.
6278: # Reply (invoked here) sends a message to the
6279: # client:
6280: #
6281: sub Failure {
6282: my $fd = shift;
6283: my $reply = shift;
6284: my $request = shift;
6285:
6286: $Failures++;
6287: Reply($fd, $reply, $request); # That's simple eh?
1.161 foxr 6288: }
1.57 www 6289: # ------------------------------------------------------------------ Log status
6290:
6291: sub logstatus {
1.178 foxr 6292: &status("Doing logging");
6293: my $docdir=$perlvar{'lonDocRoot'};
6294: {
6295: my $fh=IO::File->new(">$docdir/lon-status/londchld/$$.txt");
1.200 matthew 6296: print $fh $status."\n".$lastlog."\n".time."\n$keymode";
1.178 foxr 6297: $fh->close();
6298: }
1.221 albertel 6299: &status("Finished $$.txt");
6300: {
6301: open(LOG,">>$docdir/lon-status/londstatus.txt");
6302: flock(LOG,LOCK_EX);
6303: print LOG $$."\t".$clientname."\t".$currenthostid."\t"
6304: .$status."\t".$lastlog."\t $keymode\n";
1.275 albertel 6305: flock(LOG,LOCK_UN);
1.221 albertel 6306: close(LOG);
6307: }
1.178 foxr 6308: &status("Finished logging");
1.57 www 6309: }
6310:
6311: sub initnewstatus {
6312: my $docdir=$perlvar{'lonDocRoot'};
6313: my $fh=IO::File->new(">$docdir/lon-status/londstatus.txt");
1.460 foxr 6314: my $now=time();
1.57 www 6315: my $local=localtime($now);
6316: print $fh "LOND status $local - parent $$\n\n";
1.64 www 6317: opendir(DIR,"$docdir/lon-status/londchld");
1.134 albertel 6318: while (my $filename=readdir(DIR)) {
1.64 www 6319: unlink("$docdir/lon-status/londchld/$filename");
6320: }
6321: closedir(DIR);
1.57 www 6322: }
6323:
6324: # -------------------------------------------------------------- Status setting
6325:
6326: sub status {
6327: my $what=shift;
6328: my $now=time;
6329: my $local=localtime($now);
1.178 foxr 6330: $status=$local.': '.$what;
6331: $0='lond: '.$what.' '.$local;
1.57 www 6332: }
1.11 www 6333:
1.13 www 6334: # -------------------------------------------------------------- Talk to lonsql
6335:
1.234 foxr 6336: sub sql_reply {
1.12 harris41 6337: my ($cmd)=@_;
1.234 foxr 6338: my $answer=&sub_sql_reply($cmd);
6339: if ($answer eq 'con_lost') { $answer=&sub_sql_reply($cmd); }
1.12 harris41 6340: return $answer;
6341: }
6342:
1.234 foxr 6343: sub sub_sql_reply {
1.12 harris41 6344: my ($cmd)=@_;
6345: my $unixsock="mysqlsock";
6346: my $peerfile="$perlvar{'lonSockDir'}/$unixsock";
6347: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
6348: Type => SOCK_STREAM,
6349: Timeout => 10)
6350: or return "con_lost";
1.319 www 6351: print $sclient "$cmd:$currentdomainid\n";
1.12 harris41 6352: my $answer=<$sclient>;
6353: chomp($answer);
6354: if (!$answer) { $answer="con_lost"; }
6355: return $answer;
6356: }
6357:
1.1 albertel 6358: # --------------------------------------- Is this the home server of an author?
1.11 www 6359:
1.1 albertel 6360: sub ishome {
6361: my $author=shift;
6362: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
6363: my ($udom,$uname)=split(/\//,$author);
6364: my $proname=propath($udom,$uname);
6365: if (-e $proname) {
6366: return 'owner';
6367: } else {
6368: return 'not_owner';
6369: }
6370: }
6371:
6372: # ======================================================= Continue main program
6373: # ---------------------------------------------------- Fork once and dissociate
6374:
1.134 albertel 6375: my $fpid=fork;
1.1 albertel 6376: exit if $fpid;
1.29 harris41 6377: die "Couldn't fork: $!" unless defined ($fpid);
1.1 albertel 6378:
1.29 harris41 6379: POSIX::setsid() or die "Can't start new session: $!";
1.1 albertel 6380:
6381: # ------------------------------------------------------- Write our PID on disk
6382:
1.134 albertel 6383: my $execdir=$perlvar{'lonDaemons'};
1.1 albertel 6384: open (PIDSAVE,">$execdir/logs/lond.pid");
6385: print PIDSAVE "$$\n";
6386: close(PIDSAVE);
1.190 albertel 6387: &logthis("<font color='red'>CRITICAL: ---------- Starting ----------</font>");
1.57 www 6388: &status('Starting');
1.1 albertel 6389:
1.106 foxr 6390:
1.1 albertel 6391:
6392: # ----------------------------------------------------- Install signal handlers
6393:
1.57 www 6394:
1.1 albertel 6395: $SIG{CHLD} = \&REAPER;
6396: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
6397: $SIG{HUP} = \&HUPSMAN;
1.57 www 6398: $SIG{USR1} = \&checkchildren;
1.144 foxr 6399: $SIG{USR2} = \&UpdateHosts;
1.106 foxr 6400:
1.148 foxr 6401: # Read the host hashes:
1.368 albertel 6402: &Apache::lonnet::load_hosts_tab();
1.447 raeburn 6403: my %iphost = &Apache::lonnet::get_iphost(1);
1.106 foxr 6404:
1.480 raeburn 6405: $dist=`$perlvar{'lonDaemons'}/distprobe`;
1.286 albertel 6406:
1.471 raeburn 6407: my $arch = `uname -i`;
1.475 raeburn 6408: chomp($arch);
1.471 raeburn 6409: if ($arch eq 'unknown') {
6410: $arch = `uname -m`;
1.475 raeburn 6411: chomp($arch);
1.471 raeburn 6412: }
6413:
1.106 foxr 6414: # --------------------------------------------------------------
6415: # Accept connections. When a connection comes in, it is validated
6416: # and if good, a child process is created to process transactions
6417: # along the connection.
6418:
1.1 albertel 6419: while (1) {
1.165 albertel 6420: &status('Starting accept');
1.106 foxr 6421: $client = $server->accept() or next;
1.165 albertel 6422: &status('Accepted '.$client.' off to spawn');
1.386 albertel 6423: make_new_child($client);
1.165 albertel 6424: &status('Finished spawning');
1.1 albertel 6425: }
6426:
1.212 foxr 6427: sub make_new_child {
6428: my $pid;
6429: # my $cipher; # Now global
6430: my $sigset;
1.178 foxr 6431:
1.212 foxr 6432: $client = shift;
6433: &status('Starting new child '.$client);
6434: &logthis('<font color="green"> Attempting to start child ('.$client.
6435: ")</font>");
6436: # block signal for fork
6437: $sigset = POSIX::SigSet->new(SIGINT);
6438: sigprocmask(SIG_BLOCK, $sigset)
6439: or die "Can't block SIGINT for fork: $!\n";
1.178 foxr 6440:
1.212 foxr 6441: die "fork: $!" unless defined ($pid = fork);
1.178 foxr 6442:
1.212 foxr 6443: $client->sockopt(SO_KEEPALIVE, 1); # Enable monitoring of
6444: # connection liveness.
1.178 foxr 6445:
1.212 foxr 6446: #
6447: # Figure out who we're talking to so we can record the peer in
6448: # the pid hash.
6449: #
6450: my $caller = getpeername($client);
6451: my ($port,$iaddr);
6452: if (defined($caller) && length($caller) > 0) {
6453: ($port,$iaddr)=unpack_sockaddr_in($caller);
6454: } else {
6455: &logthis("Unable to determine who caller was, getpeername returned nothing");
6456: }
6457: if (defined($iaddr)) {
6458: $clientip = inet_ntoa($iaddr);
6459: Debug("Connected with $clientip");
6460: } else {
6461: &logthis("Unable to determine clientip");
6462: $clientip='Unavailable';
6463: }
6464:
6465: if ($pid) {
6466: # Parent records the child's birth and returns.
6467: sigprocmask(SIG_UNBLOCK, $sigset)
6468: or die "Can't unblock SIGINT for fork: $!\n";
6469: $children{$pid} = $clientip;
6470: &status('Started child '.$pid);
1.462 foxr 6471: close($client);
1.212 foxr 6472: return;
6473: } else {
6474: # Child can *not* return from this subroutine.
6475: $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before
6476: $SIG{CHLD} = 'DEFAULT'; #make this default so that pwauth returns
6477: #don't get intercepted
6478: $SIG{USR1}= \&logstatus;
6479: $SIG{ALRM}= \&timeout;
1.468 foxr 6480: #
6481: # Block sigpipe as it gets thrownon socket disconnect and we want to
6482: # deal with that as a read faiure instead.
6483: #
6484: my $blockset = POSIX::SigSet->new(SIGPIPE);
6485: sigprocmask(SIG_BLOCK, $blockset);
6486:
1.212 foxr 6487: $lastlog='Forked ';
6488: $status='Forked';
1.178 foxr 6489:
1.212 foxr 6490: # unblock signals
6491: sigprocmask(SIG_UNBLOCK, $sigset)
6492: or die "Can't unblock SIGINT for fork: $!\n";
1.178 foxr 6493:
1.212 foxr 6494: # my $tmpsnum=0; # Now global
6495: #---------------------------------------------------- kerberos 5 initialization
6496: &Authen::Krb5::init_context();
1.359 raeburn 6497: unless (($dist eq 'fedora5') || ($dist eq 'fedora4') ||
1.497 raeburn 6498: ($dist eq 'fedora6') || ($dist eq 'suse9.3') ||
1.499 raeburn 6499: ($dist eq 'suse12.2') || ($dist eq 'suse12.3')) {
1.286 albertel 6500: &Authen::Krb5::init_ets();
6501: }
1.209 albertel 6502:
1.212 foxr 6503: &status('Accepted connection');
6504: # =============================================================================
6505: # do something with the connection
6506: # -----------------------------------------------------------------------------
6507: # see if we know client and 'check' for spoof IP by ineffective challenge
1.178 foxr 6508:
1.278 albertel 6509: my $outsideip=$clientip;
6510: if ($clientip eq '127.0.0.1') {
1.368 albertel 6511: $outsideip=&Apache::lonnet::get_host_ip($perlvar{'lonHostID'});
1.278 albertel 6512: }
1.412 foxr 6513: &ReadManagerTable();
1.368 albertel 6514: my $clientrec=defined(&Apache::lonnet::get_hosts_from_ip($outsideip));
1.278 albertel 6515: my $ismanager=($managers{$outsideip} ne undef);
1.432 raeburn 6516: $clientname = "[unknown]";
1.212 foxr 6517: if($clientrec) { # Establish client type.
6518: $ConnectionType = "client";
1.368 albertel 6519: $clientname = (&Apache::lonnet::get_hosts_from_ip($outsideip))[-1];
1.212 foxr 6520: if($ismanager) {
6521: $ConnectionType = "both";
6522: }
6523: } else {
6524: $ConnectionType = "manager";
1.278 albertel 6525: $clientname = $managers{$outsideip};
1.212 foxr 6526: }
6527: my $clientok;
1.178 foxr 6528:
1.212 foxr 6529: if ($clientrec || $ismanager) {
6530: &status("Waiting for init from $clientip $clientname");
6531: &logthis('<font color="yellow">INFO: Connection, '.
6532: $clientip.
6533: " ($clientname) connection type = $ConnectionType </font>" );
6534: &status("Connecting $clientip ($clientname))");
6535: my $remotereq=<$client>;
6536: chomp($remotereq);
6537: Debug("Got init: $remotereq");
1.337 albertel 6538:
1.212 foxr 6539: if ($remotereq =~ /^init/) {
6540: &sethost("sethost:$perlvar{'lonHostID'}");
6541: #
6542: # If the remote is attempting a local init... give that a try:
6543: #
1.494 droeschl 6544: logthis("remotereq: $remotereq");
1.432 raeburn 6545: (my $i, my $inittype, $clientversion) = split(/:/, $remotereq);
1.492 droeschl 6546: # For LON-CAPA 2.9, the client session will have sent its LON-CAPA
6547: # version when initiating the connection. For LON-CAPA 2.8 and older,
6548: # the version is retrieved from the global %loncaparevs in lonnet.pm.
1.494 droeschl 6549: # $clientversion contains path to keyfile if $inittype eq 'local'
6550: # it's overridden below in this case
1.492 droeschl 6551: $clientversion ||= $Apache::lonnet::loncaparevs{$clientname};
1.209 albertel 6552:
1.212 foxr 6553: # If the connection type is ssl, but I didn't get my
6554: # certificate files yet, then I'll drop back to
6555: # insecure (if allowed).
6556:
6557: if($inittype eq "ssl") {
6558: my ($ca, $cert) = lonssl::CertificateFile;
6559: my $kfile = lonssl::KeyFile;
6560: if((!$ca) ||
6561: (!$cert) ||
6562: (!$kfile)) {
6563: $inittype = ""; # This forces insecure attempt.
6564: &logthis("<font color=\"blue\"> Certificates not "
6565: ."installed -- trying insecure auth</font>");
1.224 foxr 6566: } else { # SSL certificates are in place so
1.212 foxr 6567: } # Leave the inittype alone.
6568: }
6569:
6570: if($inittype eq "local") {
1.432 raeburn 6571: $clientversion = $perlvar{'lonVersion'};
1.212 foxr 6572: my $key = LocalConnection($client, $remotereq);
6573: if($key) {
6574: Debug("Got local key $key");
6575: $clientok = 1;
6576: my $cipherkey = pack("H32", $key);
6577: $cipher = new IDEA($cipherkey);
6578: print $client "ok:local\n";
1.442 www 6579: &logthis('<font color="green">'
1.212 foxr 6580: . "Successful local authentication </font>");
6581: $keymode = "local"
1.178 foxr 6582: } else {
1.212 foxr 6583: Debug("Failed to get local key");
6584: $clientok = 0;
6585: shutdown($client, 3);
6586: close $client;
1.178 foxr 6587: }
1.212 foxr 6588: } elsif ($inittype eq "ssl") {
6589: my $key = SSLConnection($client);
6590: if ($key) {
6591: $clientok = 1;
6592: my $cipherkey = pack("H32", $key);
6593: $cipher = new IDEA($cipherkey);
6594: &logthis('<font color="green">'
6595: ."Successfull ssl authentication with $clientname </font>");
6596: $keymode = "ssl";
6597:
1.178 foxr 6598: } else {
1.212 foxr 6599: $clientok = 0;
6600: close $client;
1.178 foxr 6601: }
1.212 foxr 6602:
6603: } else {
6604: my $ok = InsecureConnection($client);
6605: if($ok) {
6606: $clientok = 1;
6607: &logthis('<font color="green">'
6608: ."Successful insecure authentication with $clientname </font>");
6609: print $client "ok\n";
6610: $keymode = "insecure";
1.178 foxr 6611: } else {
1.212 foxr 6612: &logthis('<font color="yellow">'
6613: ."Attempted insecure connection disallowed </font>");
6614: close $client;
6615: $clientok = 0;
1.178 foxr 6616:
6617: }
6618: }
1.212 foxr 6619: } else {
6620: &logthis(
6621: "<font color='blue'>WARNING: "
6622: ."$clientip failed to initialize: >$remotereq< </font>");
6623: &status('No init '.$clientip);
6624: }
6625:
6626: } else {
6627: &logthis(
6628: "<font color='blue'>WARNING: Unknown client $clientip</font>");
6629: &status('Hung up on '.$clientip);
6630: }
6631:
6632: if ($clientok) {
6633: # ---------------- New known client connecting, could mean machine online again
1.368 albertel 6634: if (&Apache::lonnet::get_host_ip($currenthostid) ne $clientip
1.367 albertel 6635: && $clientip ne '127.0.0.1') {
1.375 albertel 6636: &Apache::lonnet::reconlonc($clientname);
1.212 foxr 6637: }
6638: &logthis("<font color='green'>Established connection: $clientname</font>");
6639: &status('Will listen to '.$clientname);
6640: # ------------------------------------------------------------ Process requests
6641: my $keep_going = 1;
6642: my $user_input;
1.448 raeburn 6643: my $clienthost = &Apache::lonnet::hostname($clientname);
6644: my $clientserverhomeID = &Apache::lonnet::get_server_homeID($clienthost);
6645: $clienthomedom = &Apache::lonnet::host_domain($clientserverhomeID);
1.212 foxr 6646: while(($user_input = get_request) && $keep_going) {
6647: alarm(120);
6648: Debug("Main: Got $user_input\n");
6649: $keep_going = &process_request($user_input);
1.178 foxr 6650: alarm(0);
1.212 foxr 6651: &status('Listening to '.$clientname." ($keymode)");
1.161 foxr 6652: }
1.212 foxr 6653:
1.59 www 6654: # --------------------------------------------- client unknown or fishy, refuse
1.212 foxr 6655: } else {
1.161 foxr 6656: print $client "refused\n";
6657: $client->close();
1.190 albertel 6658: &logthis("<font color='blue'>WARNING: "
1.161 foxr 6659: ."Rejected client $clientip, closing connection</font>");
6660: }
1.212 foxr 6661: }
1.161 foxr 6662:
1.1 albertel 6663: # =============================================================================
1.161 foxr 6664:
1.190 albertel 6665: &logthis("<font color='red'>CRITICAL: "
1.161 foxr 6666: ."Disconnect from $clientip ($clientname)</font>");
6667:
6668:
6669: # this exit is VERY important, otherwise the child will become
6670: # a producer of more and more children, forking yourself into
6671: # process death.
6672: exit;
1.106 foxr 6673:
1.78 foxr 6674: }
1.261 foxr 6675: #
6676: # Determine if a user is an author for the indicated domain.
6677: #
6678: # Parameters:
6679: # domain - domain to check in .
6680: # user - Name of user to check.
6681: #
6682: # Return:
6683: # 1 - User is an author for domain.
6684: # 0 - User is not an author for domain.
6685: sub is_author {
6686: my ($domain, $user) = @_;
6687:
6688: &Debug("is_author: $user @ $domain");
6689:
6690: my $hashref = &tie_user_hash($domain, $user, "roles",
6691: &GDBM_READER());
6692:
6693: # Author role should show up as a key /domain/_au
1.78 foxr 6694:
1.321 albertel 6695: my $value;
1.487 foxr 6696: if ($hashref) {
1.78 foxr 6697:
1.487 foxr 6698: my $key = "/$domain/_au";
6699: if (defined($hashref)) {
6700: $value = $hashref->{$key};
6701: if(!untie_user_hash($hashref)) {
6702: return 'error: ' . ($!+0)." untie (GDBM) Failed";
6703: }
6704: }
6705:
6706: if(defined($value)) {
6707: &Debug("$user @ $domain is an author");
6708: }
6709: } else {
6710: return 'error: '.($!+0)." tie (GDBM) Failed";
1.261 foxr 6711: }
6712:
6713: return defined($value);
6714: }
1.78 foxr 6715: #
6716: # Checks to see if the input roleput request was to set
1.482 www 6717: # an author role. If so, creates construction space
1.78 foxr 6718: # Parameters:
6719: # request - The request sent to the rolesput subchunk.
6720: # We're looking for /domain/_au
6721: # domain - The domain in which the user is having roles doctored.
6722: # user - Name of the user for which the role is being put.
6723: # authtype - The authentication type associated with the user.
6724: #
1.289 albertel 6725: sub manage_permissions {
1.192 foxr 6726: my ($request, $domain, $user, $authtype) = @_;
1.78 foxr 6727: # See if the request is of the form /$domain/_au
1.289 albertel 6728: if($request =~ /^(\/\Q$domain\E\/_au)$/) { # It's an author rolesput...
1.484 raeburn 6729: my $path=$perlvar{'lonDocRoot'}."/priv/$domain";
1.482 www 6730: unless (-e $path) {
6731: mkdir($path);
6732: }
6733: unless (-e $path.'/'.$user) {
6734: mkdir($path.'/'.$user);
6735: }
1.78 foxr 6736: }
6737: }
1.222 foxr 6738:
6739:
6740: #
6741: # Return the full path of a user password file, whether it exists or not.
6742: # Parameters:
6743: # domain - Domain in which the password file lives.
6744: # user - name of the user.
6745: # Returns:
6746: # Full passwd path:
6747: #
6748: sub password_path {
6749: my ($domain, $user) = @_;
1.264 albertel 6750: return &propath($domain, $user).'/passwd';
1.222 foxr 6751: }
6752:
6753: # Password Filename
6754: # Returns the path to a passwd file given domain and user... only if
6755: # it exists.
6756: # Parameters:
6757: # domain - Domain in which to search.
6758: # user - username.
6759: # Returns:
6760: # - If the password file exists returns its path.
6761: # - If the password file does not exist, returns undefined.
6762: #
6763: sub password_filename {
6764: my ($domain, $user) = @_;
6765:
6766: Debug ("PasswordFilename called: dom = $domain user = $user");
6767:
6768: my $path = &password_path($domain, $user);
6769: Debug("PasswordFilename got path: $path");
6770: if(-e $path) {
6771: return $path;
6772: } else {
6773: return undef;
6774: }
6775: }
6776:
6777: #
6778: # Rewrite the contents of the user's passwd file.
6779: # Parameters:
6780: # domain - domain of the user.
6781: # name - User's name.
6782: # contents - New contents of the file.
6783: # Returns:
6784: # 0 - Failed.
6785: # 1 - Success.
6786: #
6787: sub rewrite_password_file {
6788: my ($domain, $user, $contents) = @_;
6789:
6790: my $file = &password_filename($domain, $user);
6791: if (defined $file) {
6792: my $pf = IO::File->new(">$file");
6793: if($pf) {
6794: print $pf "$contents\n";
6795: return 1;
6796: } else {
6797: return 0;
6798: }
6799: } else {
6800: return 0;
6801: }
6802:
6803: }
6804:
1.78 foxr 6805: #
1.222 foxr 6806: # get_auth_type - Determines the authorization type of a user in a domain.
1.78 foxr 6807:
6808: # Returns the authorization type or nouser if there is no such user.
6809: #
1.436 raeburn 6810: sub get_auth_type {
1.192 foxr 6811: my ($domain, $user) = @_;
1.78 foxr 6812:
1.222 foxr 6813: Debug("get_auth_type( $domain, $user ) \n");
1.78 foxr 6814: my $proname = &propath($domain, $user);
6815: my $passwdfile = "$proname/passwd";
6816: if( -e $passwdfile ) {
6817: my $pf = IO::File->new($passwdfile);
6818: my $realpassword = <$pf>;
6819: chomp($realpassword);
1.79 foxr 6820: Debug("Password info = $realpassword\n");
1.78 foxr 6821: my ($authtype, $contentpwd) = split(/:/, $realpassword);
1.79 foxr 6822: Debug("Authtype = $authtype, content = $contentpwd\n");
1.259 raeburn 6823: return "$authtype:$contentpwd";
1.224 foxr 6824: } else {
1.79 foxr 6825: Debug("Returning nouser");
1.78 foxr 6826: return "nouser";
6827: }
1.1 albertel 6828: }
6829:
1.220 foxr 6830: #
6831: # Validate a user given their domain, name and password. This utility
6832: # function is used by both AuthenticateHandler and ChangePasswordHandler
6833: # to validate the login credentials of a user.
6834: # Parameters:
6835: # $domain - The domain being logged into (this is required due to
6836: # the capability for multihomed systems.
6837: # $user - The name of the user being validated.
6838: # $password - The user's propoposed password.
6839: #
6840: # Returns:
6841: # 1 - The domain,user,pasword triplet corresponds to a valid
6842: # user.
6843: # 0 - The domain,user,password triplet is not a valid user.
6844: #
6845: sub validate_user {
1.396 raeburn 6846: my ($domain, $user, $password, $checkdefauth) = @_;
1.220 foxr 6847:
6848: # Why negative ~pi you may well ask? Well this function is about
6849: # authentication, and therefore very important to get right.
6850: # I've initialized the flag that determines whether or not I've
6851: # validated correctly to a value it's not supposed to get.
6852: # At the end of this function. I'll ensure that it's not still that
6853: # value so we don't just wind up returning some accidental value
6854: # as a result of executing an unforseen code path that
1.249 foxr 6855: # did not set $validated. At the end of valid execution paths,
6856: # validated shoule be 1 for success or 0 for failuer.
1.220 foxr 6857:
6858: my $validated = -3.14159;
6859:
6860: # How we authenticate is determined by the type of authentication
6861: # the user has been assigned. If the authentication type is
6862: # "nouser", the user does not exist so we will return 0.
6863:
1.222 foxr 6864: my $contents = &get_auth_type($domain, $user);
1.220 foxr 6865: my ($howpwd, $contentpwd) = split(/:/, $contents);
6866:
6867: my $null = pack("C",0); # Used by kerberos auth types.
6868:
1.395 raeburn 6869: if ($howpwd eq 'nouser') {
1.396 raeburn 6870: if ($checkdefauth) {
6871: my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
6872: if ($domdefaults{'auth_def'} eq 'localauth') {
6873: $howpwd = $domdefaults{'auth_def'};
6874: $contentpwd = $domdefaults{'auth_arg_def'};
6875: } elsif ((($domdefaults{'auth_def'} eq 'krb4') ||
6876: ($domdefaults{'auth_def'} eq 'krb5')) &&
6877: ($domdefaults{'auth_arg_def'} ne '')) {
6878: $howpwd = $domdefaults{'auth_def'};
6879: $contentpwd = $domdefaults{'auth_arg_def'};
6880: }
1.395 raeburn 6881: }
6882: }
1.220 foxr 6883: if ($howpwd ne 'nouser') {
6884: if($howpwd eq "internal") { # Encrypted is in local password file.
6885: $validated = (crypt($password, $contentpwd) eq $contentpwd);
6886: }
6887: elsif ($howpwd eq "unix") { # User is a normal unix user.
6888: $contentpwd = (getpwnam($user))[1];
6889: if($contentpwd) {
6890: if($contentpwd eq 'x') { # Shadow password file...
6891: my $pwauth_path = "/usr/local/sbin/pwauth";
6892: open PWAUTH, "|$pwauth_path" or
6893: die "Cannot invoke authentication";
6894: print PWAUTH "$user\n$password\n";
6895: close PWAUTH;
6896: $validated = ! $?;
6897:
6898: } else { # Passwords in /etc/passwd.
6899: $validated = (crypt($password,
6900: $contentpwd) eq $contentpwd);
6901: }
6902: } else {
6903: $validated = 0;
6904: }
1.439 raeburn 6905: } elsif ($howpwd eq "krb4") { # user is in kerberos 4 auth. domain.
6906: my $checkwithkrb5 = 0;
6907: if ($dist =~/^fedora(\d+)$/) {
6908: if ($1 > 11) {
6909: $checkwithkrb5 = 1;
6910: }
6911: } elsif ($dist =~ /^suse([\d.]+)$/) {
6912: if ($1 > 11.1) {
6913: $checkwithkrb5 = 1;
6914: }
6915: }
6916: if ($checkwithkrb5) {
6917: $validated = &krb5_authen($password,$null,$user,$contentpwd);
6918: } else {
6919: $validated = &krb4_authen($password,$null,$user,$contentpwd);
6920: }
1.224 foxr 6921: } elsif ($howpwd eq "krb5") { # User is in kerberos 5 auth. domain.
1.439 raeburn 6922: $validated = &krb5_authen($password,$null,$user,$contentpwd);
1.224 foxr 6923: } elsif ($howpwd eq "localauth") {
1.220 foxr 6924: # Authenticate via installation specific authentcation method:
6925: $validated = &localauth::localauth($user,
6926: $password,
1.353 albertel 6927: $contentpwd,
6928: $domain);
1.358 albertel 6929: if ($validated < 0) {
1.357 albertel 6930: &logthis("localauth for $contentpwd $user:$domain returned a $validated");
6931: $validated = 0;
6932: }
1.224 foxr 6933: } else { # Unrecognized auth is also bad.
1.220 foxr 6934: $validated = 0;
6935: }
6936: } else {
6937: $validated = 0;
6938: }
6939: #
6940: # $validated has the correct stat of the authentication:
6941: #
6942:
6943: unless ($validated != -3.14159) {
1.249 foxr 6944: # I >really really< want to know if this happens.
6945: # since it indicates that user authentication is badly
6946: # broken in some code path.
6947: #
6948: die "ValidateUser - failed to set the value of validated $domain, $user $password";
1.220 foxr 6949: }
6950: return $validated;
6951: }
6952:
1.439 raeburn 6953: sub krb4_authen {
6954: my ($password,$null,$user,$contentpwd) = @_;
6955: my $validated = 0;
6956: if (!($password =~ /$null/) ) { # Null password not allowed.
6957: eval {
6958: require Authen::Krb4;
6959: };
6960: if (!$@) {
6961: my $k4error = &Authen::Krb4::get_pw_in_tkt($user,
6962: "",
6963: $contentpwd,,
6964: 'krbtgt',
6965: $contentpwd,
6966: 1,
6967: $password);
6968: if(!$k4error) {
6969: $validated = 1;
6970: } else {
6971: $validated = 0;
6972: &logthis('krb4: '.$user.', '.$contentpwd.', '.
6973: &Authen::Krb4::get_err_txt($Authen::Krb4::error));
6974: }
6975: } else {
6976: $validated = krb5_authen($password,$null,$user,$contentpwd);
6977: }
6978: }
6979: return $validated;
6980: }
6981:
6982: sub krb5_authen {
6983: my ($password,$null,$user,$contentpwd) = @_;
6984: my $validated = 0;
6985: if(!($password =~ /$null/)) { # Null password not allowed.
6986: my $krbclient = &Authen::Krb5::parse_name($user.'@'
6987: .$contentpwd);
6988: my $krbservice = "krbtgt/".$contentpwd."\@".$contentpwd;
6989: my $krbserver = &Authen::Krb5::parse_name($krbservice);
6990: my $credentials= &Authen::Krb5::cc_default();
6991: $credentials->initialize(&Authen::Krb5::parse_name($user.'@'
6992: .$contentpwd));
6993: my $krbreturn;
6994: if (exists(&Authen::Krb5::get_init_creds_password)) {
6995: $krbreturn =
6996: &Authen::Krb5::get_init_creds_password($krbclient,$password,
6997: $krbservice);
6998: $validated = (ref($krbreturn) eq 'Authen::Krb5::Creds');
6999: } else {
7000: $krbreturn =
7001: &Authen::Krb5::get_in_tkt_with_password($krbclient,$krbserver,
7002: $password,$credentials);
7003: $validated = ($krbreturn == 1);
7004: }
7005: if (!$validated) {
7006: &logthis('krb5: '.$user.', '.$contentpwd.', '.
7007: &Authen::Krb5::error());
7008: }
7009: }
7010: return $validated;
7011: }
1.220 foxr 7012:
1.84 albertel 7013: sub addline {
7014: my ($fname,$hostid,$ip,$newline)=@_;
7015: my $contents;
7016: my $found=0;
1.355 albertel 7017: my $expr='^'.quotemeta($hostid).':'.quotemeta($ip).':';
1.134 albertel 7018: my $sh;
1.84 albertel 7019: if ($sh=IO::File->new("$fname.subscription")) {
7020: while (my $subline=<$sh>) {
7021: if ($subline !~ /$expr/) {$contents.= $subline;} else {$found=1;}
7022: }
7023: $sh->close();
7024: }
7025: $sh=IO::File->new(">$fname.subscription");
7026: if ($contents) { print $sh $contents; }
7027: if ($newline) { print $sh $newline; }
7028: $sh->close();
7029: return $found;
1.86 www 7030: }
7031:
1.234 foxr 7032: sub get_chat {
1.324 raeburn 7033: my ($cdom,$cname,$udom,$uname,$group)=@_;
1.310 albertel 7034:
1.87 www 7035: my @entries=();
1.324 raeburn 7036: my $namespace = 'nohist_chatroom';
7037: my $namespace_inroom = 'nohist_inchatroom';
1.335 albertel 7038: if ($group ne '') {
1.324 raeburn 7039: $namespace .= '_'.$group;
7040: $namespace_inroom .= '_'.$group;
7041: }
7042: my $hashref = &tie_user_hash($cdom, $cname, $namespace,
1.310 albertel 7043: &GDBM_READER());
7044: if ($hashref) {
7045: @entries=map { $_.':'.$hashref->{$_} } sort(keys(%$hashref));
1.311 albertel 7046: &untie_user_hash($hashref);
1.123 www 7047: }
1.124 www 7048: my @participants=();
1.134 albertel 7049: my $cutoff=time-60;
1.324 raeburn 7050: $hashref = &tie_user_hash($cdom, $cname, $namespace_inroom,
1.310 albertel 7051: &GDBM_WRCREAT());
7052: if ($hashref) {
7053: $hashref->{$uname.':'.$udom}=time;
7054: foreach my $user (sort(keys(%$hashref))) {
7055: if ($hashref->{$user}>$cutoff) {
7056: push(@participants, 'active_participant:'.$user);
1.123 www 7057: }
7058: }
1.311 albertel 7059: &untie_user_hash($hashref);
1.86 www 7060: }
1.124 www 7061: return (@participants,@entries);
1.86 www 7062: }
7063:
1.234 foxr 7064: sub chat_add {
1.324 raeburn 7065: my ($cdom,$cname,$newchat,$group)=@_;
1.88 albertel 7066: my @entries=();
1.142 www 7067: my $time=time;
1.324 raeburn 7068: my $namespace = 'nohist_chatroom';
7069: my $logfile = 'chatroom.log';
1.335 albertel 7070: if ($group ne '') {
1.324 raeburn 7071: $namespace .= '_'.$group;
7072: $logfile = 'chatroom_'.$group.'.log';
7073: }
7074: my $hashref = &tie_user_hash($cdom, $cname, $namespace,
1.310 albertel 7075: &GDBM_WRCREAT());
7076: if ($hashref) {
7077: @entries=map { $_.':'.$hashref->{$_} } sort(keys(%$hashref));
1.88 albertel 7078: my ($lastid)=($entries[$#entries]=~/^(\w+)\:/);
7079: my ($thentime,$idnum)=split(/\_/,$lastid);
7080: my $newid=$time.'_000000';
7081: if ($thentime==$time) {
7082: $idnum=~s/^0+//;
7083: $idnum++;
7084: $idnum=substr('000000'.$idnum,-6,6);
7085: $newid=$time.'_'.$idnum;
7086: }
1.310 albertel 7087: $hashref->{$newid}=$newchat;
1.88 albertel 7088: my $expired=$time-3600;
1.310 albertel 7089: foreach my $comment (keys(%$hashref)) {
7090: my ($thistime) = ($comment=~/(\d+)\_/);
1.88 albertel 7091: if ($thistime<$expired) {
1.310 albertel 7092: delete $hashref->{$comment};
1.88 albertel 7093: }
7094: }
1.310 albertel 7095: {
7096: my $proname=&propath($cdom,$cname);
1.324 raeburn 7097: if (open(CHATLOG,">>$proname/$logfile")) {
1.310 albertel 7098: print CHATLOG ("$time:".&unescape($newchat)."\n");
7099: }
7100: close(CHATLOG);
1.142 www 7101: }
1.311 albertel 7102: &untie_user_hash($hashref);
1.86 www 7103: }
1.84 albertel 7104: }
7105:
7106: sub unsub {
7107: my ($fname,$clientip)=@_;
7108: my $result;
1.188 foxr 7109: my $unsubs = 0; # Number of successful unsubscribes:
7110:
7111:
7112: # An old way subscriptions were handled was to have a
7113: # subscription marker file:
7114:
7115: Debug("Attempting unlink of $fname.$clientname");
1.161 foxr 7116: if (unlink("$fname.$clientname")) {
1.188 foxr 7117: $unsubs++; # Successful unsub via marker file.
7118: }
7119:
7120: # The more modern way to do it is to have a subscription list
7121: # file:
7122:
1.84 albertel 7123: if (-e "$fname.subscription") {
1.161 foxr 7124: my $found=&addline($fname,$clientname,$clientip,'');
1.188 foxr 7125: if ($found) {
7126: $unsubs++;
7127: }
7128: }
7129:
7130: # If either or both of these mechanisms succeeded in unsubscribing a
7131: # resource we can return ok:
7132:
7133: if($unsubs) {
7134: $result = "ok\n";
1.84 albertel 7135: } else {
1.188 foxr 7136: $result = "not_subscribed\n";
1.84 albertel 7137: }
1.188 foxr 7138:
1.84 albertel 7139: return $result;
7140: }
7141:
1.101 www 7142: sub currentversion {
7143: my $fname=shift;
7144: my $version=-1;
7145: my $ulsdir='';
7146: if ($fname=~/^(.+)\/[^\/]+$/) {
7147: $ulsdir=$1;
7148: }
1.114 albertel 7149: my ($fnamere1,$fnamere2);
7150: # remove version if already specified
1.101 www 7151: $fname=~s/\.\d+\.(\w+(?:\.meta)*)$/\.$1/;
1.114 albertel 7152: # get the bits that go before and after the version number
7153: if ( $fname=~/^(.*\.)(\w+(?:\.meta)*)$/ ) {
7154: $fnamere1=$1;
7155: $fnamere2='.'.$2;
7156: }
1.101 www 7157: if (-e $fname) { $version=1; }
7158: if (-e $ulsdir) {
1.134 albertel 7159: if(-d $ulsdir) {
7160: if (opendir(LSDIR,$ulsdir)) {
7161: my $ulsfn;
7162: while ($ulsfn=readdir(LSDIR)) {
1.101 www 7163: # see if this is a regular file (ignore links produced earlier)
1.134 albertel 7164: my $thisfile=$ulsdir.'/'.$ulsfn;
7165: unless (-l $thisfile) {
1.160 www 7166: if ($thisfile=~/\Q$fnamere1\E(\d+)\Q$fnamere2\E$/) {
1.134 albertel 7167: if ($1>$version) { $version=$1; }
7168: }
7169: }
7170: }
7171: closedir(LSDIR);
7172: $version++;
7173: }
7174: }
7175: }
7176: return $version;
1.101 www 7177: }
7178:
7179: sub thisversion {
7180: my $fname=shift;
7181: my $version=-1;
7182: if ($fname=~/\.(\d+)\.\w+(?:\.meta)*$/) {
7183: $version=$1;
7184: }
7185: return $version;
7186: }
7187:
1.84 albertel 7188: sub subscribe {
7189: my ($userinput,$clientip)=@_;
7190: my $result;
1.293 albertel 7191: my ($cmd,$fname)=split(/:/,$userinput,2);
1.84 albertel 7192: my $ownership=&ishome($fname);
7193: if ($ownership eq 'owner') {
1.101 www 7194: # explitly asking for the current version?
7195: unless (-e $fname) {
7196: my $currentversion=¤tversion($fname);
7197: if (&thisversion($fname)==$currentversion) {
7198: if ($fname=~/^(.+)\.\d+\.(\w+(?:\.meta)*)$/) {
7199: my $root=$1;
7200: my $extension=$2;
7201: symlink($root.'.'.$extension,
7202: $root.'.'.$currentversion.'.'.$extension);
1.102 www 7203: unless ($extension=~/\.meta$/) {
7204: symlink($root.'.'.$extension.'.meta',
7205: $root.'.'.$currentversion.'.'.$extension.'.meta');
7206: }
1.101 www 7207: }
7208: }
7209: }
1.84 albertel 7210: if (-e $fname) {
7211: if (-d $fname) {
7212: $result="directory\n";
7213: } else {
1.161 foxr 7214: if (-e "$fname.$clientname") {&unsub($fname,$clientip);}
1.134 albertel 7215: my $now=time;
1.161 foxr 7216: my $found=&addline($fname,$clientname,$clientip,
7217: "$clientname:$clientip:$now\n");
1.84 albertel 7218: if ($found) { $result="$fname\n"; }
7219: # if they were subscribed to only meta data, delete that
7220: # subscription, when you subscribe to a file you also get
7221: # the metadata
7222: unless ($fname=~/\.meta$/) { &unsub("$fname.meta",$clientip); }
7223: $fname=~s/\/home\/httpd\/html\/res/raw/;
1.476 raeburn 7224: my $protocol = $Apache::lonnet::protocol{$perlvar{'lonHostID'}};
7225: $protocol = 'http' if ($protocol ne 'https');
7226: $fname=$protocol.'://'.&Apache::lonnet::hostname($perlvar{'lonHostID'})."/".$fname;
1.84 albertel 7227: $result="$fname\n";
7228: }
7229: } else {
7230: $result="not_found\n";
7231: }
7232: } else {
7233: $result="rejected\n";
7234: }
7235: return $result;
7236: }
1.287 foxr 7237: # Change the passwd of a unix user. The caller must have
7238: # first verified that the user is a loncapa user.
7239: #
7240: # Parameters:
7241: # user - Unix user name to change.
7242: # pass - New password for the user.
7243: # Returns:
7244: # ok - if success
7245: # other - Some meaningfule error message string.
7246: # NOTE:
7247: # invokes a setuid script to change the passwd.
7248: sub change_unix_password {
7249: my ($user, $pass) = @_;
7250:
7251: &Debug("change_unix_password");
7252: my $execdir=$perlvar{'lonDaemons'};
7253: &Debug("Opening lcpasswd pipeline");
7254: my $pf = IO::File->new("|$execdir/lcpasswd > "
7255: ."$perlvar{'lonDaemons'}"
7256: ."/logs/lcpasswd.log");
7257: print $pf "$user\n$pass\n$pass\n";
7258: close $pf;
7259: my $err = $?;
7260: return ($err < @passwderrors) ? $passwderrors[$err] :
7261: "pwchange_falure - unknown error";
7262:
7263:
7264: }
7265:
1.91 albertel 7266:
7267: sub make_passwd_file {
1.483 www 7268: my ($uname,$udom,$umode,$npass,$passfilename)=@_;
1.390 raeburn 7269: my $result="ok";
1.91 albertel 7270: if ($umode eq 'krb4' or $umode eq 'krb5') {
7271: {
7272: my $pf = IO::File->new(">$passfilename");
1.261 foxr 7273: if ($pf) {
7274: print $pf "$umode:$npass\n";
7275: } else {
7276: $result = "pass_file_failed_error";
7277: }
1.91 albertel 7278: }
7279: } elsif ($umode eq 'internal') {
7280: my $salt=time;
7281: $salt=substr($salt,6,2);
7282: my $ncpass=crypt($npass,$salt);
7283: {
7284: &Debug("Creating internal auth");
7285: my $pf = IO::File->new(">$passfilename");
1.261 foxr 7286: if($pf) {
7287: print $pf "internal:$ncpass\n";
7288: } else {
7289: $result = "pass_file_failed_error";
7290: }
1.91 albertel 7291: }
7292: } elsif ($umode eq 'localauth') {
7293: {
7294: my $pf = IO::File->new(">$passfilename");
1.261 foxr 7295: if($pf) {
7296: print $pf "localauth:$npass\n";
7297: } else {
7298: $result = "pass_file_failed_error";
7299: }
1.91 albertel 7300: }
7301: } elsif ($umode eq 'unix') {
1.502 ! raeburn 7302: &logthis(">>>Attempt to create unix account blocked -- unix auth not available for new users.");
! 7303: $result="no_new_unix_accounts";
1.91 albertel 7304: } elsif ($umode eq 'none') {
7305: {
1.223 foxr 7306: my $pf = IO::File->new("> $passfilename");
1.261 foxr 7307: if($pf) {
7308: print $pf "none:\n";
7309: } else {
7310: $result = "pass_file_failed_error";
7311: }
1.91 albertel 7312: }
7313: } else {
1.390 raeburn 7314: $result="auth_mode_error";
1.91 albertel 7315: }
7316: return $result;
1.121 albertel 7317: }
7318:
1.265 albertel 7319: sub convert_photo {
7320: my ($start,$dest)=@_;
7321: system("convert $start $dest");
7322: }
7323:
1.121 albertel 7324: sub sethost {
7325: my ($remotereq) = @_;
7326: my (undef,$hostid)=split(/:/,$remotereq);
1.322 albertel 7327: # ignore sethost if we are already correct
7328: if ($hostid eq $currenthostid) {
7329: return 'ok';
7330: }
7331:
1.121 albertel 7332: if (!defined($hostid)) { $hostid=$perlvar{'lonHostID'}; }
1.368 albertel 7333: if (&Apache::lonnet::get_host_ip($perlvar{'lonHostID'})
7334: eq &Apache::lonnet::get_host_ip($hostid)) {
1.200 matthew 7335: $currenthostid =$hostid;
1.369 albertel 7336: $currentdomainid=&Apache::lonnet::host_domain($hostid);
1.443 www 7337: # &logthis("Setting hostid to $hostid, and domain to $currentdomainid");
1.121 albertel 7338: } else {
7339: &logthis("Requested host id $hostid not an alias of ".
7340: $perlvar{'lonHostID'}." refusing connection");
7341: return 'unable_to_set';
7342: }
7343: return 'ok';
7344: }
7345:
7346: sub version {
7347: my ($userinput)=@_;
7348: $remoteVERSION=(split(/:/,$userinput))[1];
7349: return "version:$VERSION";
1.127 albertel 7350: }
1.178 foxr 7351:
1.447 raeburn 7352: sub get_usersession_config {
7353: my ($dom,$name) = @_;
7354: my ($usersessionconf,$cached)=&Apache::lonnet::is_cached_new($name,$dom);
7355: if (defined($cached)) {
7356: return $usersessionconf;
7357: } else {
7358: my %domconfig = &Apache::lonnet::get_dom('configuration',['usersessions'],$dom);
7359: if (ref($domconfig{'usersessions'}) eq 'HASH') {
7360: &Apache::lonnet::do_cache_new($name,$dom,$domconfig{'usersessions'},3600);
7361: return $domconfig{'usersessions'};
7362: }
7363: }
7364: return;
7365: }
1.200 matthew 7366:
1.453 raeburn 7367:
7368:
1.450 raeburn 7369:
1.471 raeburn 7370: sub distro_and_arch {
7371: return $dist.':'.$arch;
7372: }
7373:
1.61 harris41 7374: # ----------------------------------- POD (plain old documentation, CPAN style)
7375:
7376: =head1 NAME
7377:
7378: lond - "LON Daemon" Server (port "LOND" 5663)
7379:
7380: =head1 SYNOPSIS
7381:
1.74 harris41 7382: Usage: B<lond>
7383:
7384: Should only be run as user=www. This is a command-line script which
7385: is invoked by B<loncron>. There is no expectation that a typical user
7386: will manually start B<lond> from the command-line. (In other words,
7387: DO NOT START B<lond> YOURSELF.)
1.61 harris41 7388:
7389: =head1 DESCRIPTION
7390:
1.74 harris41 7391: There are two characteristics associated with the running of B<lond>,
7392: PROCESS MANAGEMENT (starting, stopping, handling child processes)
7393: and SERVER-SIDE ACTIVITIES (password authentication, user creation,
7394: subscriptions, etc). These are described in two large
7395: sections below.
7396:
7397: B<PROCESS MANAGEMENT>
7398:
1.61 harris41 7399: Preforker - server who forks first. Runs as a daemon. HUPs.
7400: Uses IDEA encryption
7401:
1.74 harris41 7402: B<lond> forks off children processes that correspond to the other servers
7403: in the network. Management of these processes can be done at the
7404: parent process level or the child process level.
7405:
7406: B<logs/lond.log> is the location of log messages.
7407:
7408: The process management is now explained in terms of linux shell commands,
7409: subroutines internal to this code, and signal assignments:
7410:
7411: =over 4
7412:
7413: =item *
7414:
7415: PID is stored in B<logs/lond.pid>
7416:
7417: This is the process id number of the parent B<lond> process.
7418:
7419: =item *
7420:
7421: SIGTERM and SIGINT
7422:
7423: Parent signal assignment:
7424: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
7425:
7426: Child signal assignment:
7427: $SIG{INT} = 'DEFAULT'; (and SIGTERM is DEFAULT also)
7428: (The child dies and a SIGALRM is sent to parent, awaking parent from slumber
7429: to restart a new child.)
7430:
7431: Command-line invocations:
7432: B<kill> B<-s> SIGTERM I<PID>
7433: B<kill> B<-s> SIGINT I<PID>
7434:
7435: Subroutine B<HUNTSMAN>:
7436: This is only invoked for the B<lond> parent I<PID>.
7437: This kills all the children, and then the parent.
7438: The B<lonc.pid> file is cleared.
7439:
7440: =item *
7441:
7442: SIGHUP
7443:
7444: Current bug:
7445: This signal can only be processed the first time
7446: on the parent process. Subsequent SIGHUP signals
7447: have no effect.
7448:
7449: Parent signal assignment:
7450: $SIG{HUP} = \&HUPSMAN;
7451:
7452: Child signal assignment:
7453: none (nothing happens)
7454:
7455: Command-line invocations:
7456: B<kill> B<-s> SIGHUP I<PID>
7457:
7458: Subroutine B<HUPSMAN>:
7459: This is only invoked for the B<lond> parent I<PID>,
7460: This kills all the children, and then the parent.
7461: The B<lond.pid> file is cleared.
7462:
7463: =item *
7464:
7465: SIGUSR1
7466:
7467: Parent signal assignment:
7468: $SIG{USR1} = \&USRMAN;
7469:
7470: Child signal assignment:
7471: $SIG{USR1}= \&logstatus;
7472:
7473: Command-line invocations:
7474: B<kill> B<-s> SIGUSR1 I<PID>
7475:
7476: Subroutine B<USRMAN>:
7477: When invoked for the B<lond> parent I<PID>,
7478: SIGUSR1 is sent to all the children, and the status of
7479: each connection is logged.
1.144 foxr 7480:
7481: =item *
7482:
7483: SIGUSR2
7484:
7485: Parent Signal assignment:
7486: $SIG{USR2} = \&UpdateHosts
7487:
7488: Child signal assignment:
7489: NONE
7490:
1.74 harris41 7491:
7492: =item *
7493:
7494: SIGCHLD
7495:
7496: Parent signal assignment:
7497: $SIG{CHLD} = \&REAPER;
7498:
7499: Child signal assignment:
7500: none
7501:
7502: Command-line invocations:
7503: B<kill> B<-s> SIGCHLD I<PID>
7504:
7505: Subroutine B<REAPER>:
7506: This is only invoked for the B<lond> parent I<PID>.
7507: Information pertaining to the child is removed.
7508: The socket port is cleaned up.
7509:
7510: =back
7511:
7512: B<SERVER-SIDE ACTIVITIES>
7513:
7514: Server-side information can be accepted in an encrypted or non-encrypted
7515: method.
7516:
7517: =over 4
7518:
7519: =item ping
7520:
7521: Query a client in the hosts.tab table; "Are you there?"
7522:
7523: =item pong
7524:
7525: Respond to a ping query.
7526:
7527: =item ekey
7528:
7529: Read in encrypted key, make cipher. Respond with a buildkey.
7530:
7531: =item load
7532:
7533: Respond with CPU load based on a computation upon /proc/loadavg.
7534:
7535: =item currentauth
7536:
7537: Reply with current authentication information (only over an
7538: encrypted channel).
7539:
7540: =item auth
7541:
7542: Only over an encrypted channel, reply as to whether a user's
7543: authentication information can be validated.
7544:
7545: =item passwd
7546:
7547: Allow for a password to be set.
7548:
7549: =item makeuser
7550:
7551: Make a user.
7552:
7553: =item passwd
7554:
7555: Allow for authentication mechanism and password to be changed.
7556:
7557: =item home
1.61 harris41 7558:
1.74 harris41 7559: Respond to a question "are you the home for a given user?"
7560:
7561: =item update
7562:
7563: Update contents of a subscribed resource.
7564:
7565: =item unsubscribe
7566:
7567: The server is unsubscribing from a resource.
7568:
7569: =item subscribe
7570:
7571: The server is subscribing to a resource.
7572:
7573: =item log
7574:
7575: Place in B<logs/lond.log>
7576:
7577: =item put
7578:
7579: stores hash in namespace
7580:
1.496 raeburn 7581: =item rolesput
1.74 harris41 7582:
7583: put a role into a user's environment
7584:
7585: =item get
7586:
7587: returns hash with keys from array
7588: reference filled in from namespace
7589:
7590: =item eget
7591:
7592: returns hash with keys from array
7593: reference filled in from namesp (encrypts the return communication)
7594:
7595: =item rolesget
7596:
7597: get a role from a user's environment
7598:
7599: =item del
7600:
7601: deletes keys out of array from namespace
7602:
7603: =item keys
7604:
7605: returns namespace keys
7606:
7607: =item dump
7608:
7609: dumps the complete (or key matching regexp) namespace into a hash
7610:
7611: =item store
7612:
7613: stores hash permanently
7614: for this url; hashref needs to be given and should be a \%hashname; the
7615: remaining args aren't required and if they aren't passed or are '' they will
7616: be derived from the ENV
7617:
7618: =item restore
7619:
7620: returns a hash for a given url
7621:
7622: =item querysend
7623:
7624: Tells client about the lonsql process that has been launched in response
7625: to a sent query.
7626:
7627: =item queryreply
7628:
7629: Accept information from lonsql and make appropriate storage in temporary
7630: file space.
7631:
7632: =item idput
7633:
7634: Defines usernames as corresponding to IDs. (These "IDs" are unique identifiers
7635: for each student, defined perhaps by the institutional Registrar.)
7636:
7637: =item idget
7638:
7639: Returns usernames corresponding to IDs. (These "IDs" are unique identifiers
7640: for each student, defined perhaps by the institutional Registrar.)
7641:
7642: =item tmpput
7643:
7644: Accept and store information in temporary space.
7645:
7646: =item tmpget
7647:
7648: Send along temporarily stored information.
7649:
7650: =item ls
7651:
7652: List part of a user's directory.
7653:
1.135 foxr 7654: =item pushtable
7655:
7656: Pushes a file in /home/httpd/lonTab directory. Currently limited to:
7657: hosts.tab and domain.tab. The old file is copied to *.tab.backup but
7658: must be restored manually in case of a problem with the new table file.
7659: pushtable requires that the request be encrypted and validated via
7660: ValidateManager. The form of the command is:
7661: enc:pushtable tablename <tablecontents> \n
7662: where pushtable, tablename and <tablecontents> will be encrypted, but \n is a
7663: cleartext newline.
7664:
1.74 harris41 7665: =item Hanging up (exit or init)
7666:
7667: What to do when a client tells the server that they (the client)
7668: are leaving the network.
7669:
7670: =item unknown command
7671:
7672: If B<lond> is sent an unknown command (not in the list above),
7673: it replys to the client "unknown_cmd".
1.135 foxr 7674:
1.74 harris41 7675:
7676: =item UNKNOWN CLIENT
7677:
7678: If the anti-spoofing algorithm cannot verify the client,
7679: the client is rejected (with a "refused" message sent
7680: to the client, and the connection is closed.
7681:
7682: =back
1.61 harris41 7683:
7684: =head1 PREREQUISITES
7685:
7686: IO::Socket
7687: IO::File
7688: Apache::File
7689: POSIX
7690: Crypt::IDEA
7691: LWP::UserAgent()
7692: GDBM_File
7693: Authen::Krb4
1.91 albertel 7694: Authen::Krb5
1.61 harris41 7695:
7696: =head1 COREQUISITES
7697:
1.490 droeschl 7698: none
7699:
1.61 harris41 7700: =head1 OSNAMES
7701:
7702: linux
7703:
7704: =head1 SCRIPT CATEGORIES
7705:
7706: Server/Process
7707:
7708: =cut
1.409 foxr 7709:
7710:
7711: =pod
7712:
7713: =head1 LOG MESSAGES
7714:
7715: The messages below can be emitted in the lond log. This log is located
7716: in ~httpd/perl/logs/lond.log Many log messages have HTML encapsulation
7717: to provide coloring if examined from inside a web page. Some do not.
7718: Where color is used, the colors are; Red for sometihhng to get excited
7719: about and to follow up on. Yellow for something to keep an eye on to
7720: be sure it does not get worse, Green,and Blue for informational items.
7721:
7722: In the discussions below, sometimes reference is made to ~httpd
7723: when describing file locations. There isn't really an httpd
7724: user, however there is an httpd directory that gets installed in the
7725: place that user home directories go. On linux, this is usually
7726: (always?) /home/httpd.
7727:
7728:
7729: Some messages are colorless. These are usually (not always)
7730: Green/Blue color level messages.
7731:
7732: =over 2
7733:
7734: =item (Red) LocalConnection rejecting non local: <ip> ne 127.0.0.1
7735:
7736: A local connection negotiation was attempted by
7737: a host whose IP address was not 127.0.0.1.
7738: The socket is closed and the child will exit.
7739: lond has three ways to establish an encyrption
7740: key with a client:
7741:
7742: =over 2
7743:
7744: =item local
7745:
7746: The key is written and read from a file.
7747: This is only valid for connections from localhost.
7748:
7749: =item insecure
7750:
7751: The key is generated by the server and
7752: transmitted to the client.
7753:
7754: =item ssl (secure)
7755:
7756: An ssl connection is negotiated with the client,
7757: the key is generated by the server and sent to the
7758: client across this ssl connection before the
7759: ssl connectionis terminated and clear text
7760: transmission resumes.
7761:
7762: =back
7763:
7764: =item (Red) LocalConnection: caller is insane! init = <init> and type = <type>
7765:
7766: The client is local but has not sent an initialization
7767: string that is the literal "init:local" The connection
7768: is closed and the child exits.
7769:
7770: =item Red CRITICAL Can't get key file <error>
7771:
7772: SSL key negotiation is being attempted but the call to
7773: lonssl::KeyFile failed. This usually means that the
7774: configuration file is not correctly defining or protecting
7775: the directories/files lonCertificateDirectory or
7776: lonnetPrivateKey
7777: <error> is a string that describes the reason that
7778: the key file could not be located.
7779:
7780: =item (Red) CRITICAL Can't get certificates <error>
7781:
7782: SSL key negotiation failed because we were not able to retrives our certificate
7783: or the CA's certificate in the call to lonssl::CertificateFile
7784: <error> is the textual reason this failed. Usual reasons:
7785:
7786: =over 2
1.490 droeschl 7787:
1.409 foxr 7788: =item Apache config file for loncapa incorrect:
1.490 droeschl 7789:
1.409 foxr 7790: one of the variables
7791: lonCertificateDirectory, lonnetCertificateAuthority, or lonnetCertificate
7792: undefined or incorrect
7793:
7794: =item Permission error:
7795:
7796: The directory pointed to by lonCertificateDirectory is not readable by lond
7797:
7798: =item Permission error:
7799:
7800: Files in the directory pointed to by lonCertificateDirectory are not readable by lond.
7801:
7802: =item Installation error:
7803:
7804: Either the certificate authority file or the certificate have not
7805: been installed in lonCertificateDirectory.
7806:
7807: =item (Red) CRITICAL SSL Socket promotion failed: <err>
7808:
7809: The promotion of the connection from plaintext to SSL failed
7810: <err> is the reason for the failure. There are two
7811: system calls involved in the promotion (one of which failed),
7812: a dup to produce
7813: a second fd on the raw socket over which the encrypted data
7814: will flow and IO::SOcket::SSL->new_from_fd which creates
7815: the SSL connection on the duped fd.
7816:
7817: =item (Blue) WARNING client did not respond to challenge
7818:
7819: This occurs on an insecure (non SSL) connection negotiation request.
7820: lond generates some number from the time, the PID and sends it to
7821: the client. The client must respond by echoing this information back.
7822: If the client does not do so, that's a violation of the challenge
7823: protocols and the connection will be failed.
7824:
7825: =item (Red) No manager table. Nobody can manage!!
7826:
7827: lond has the concept of privileged hosts that
7828: can perform remote management function such
7829: as update the hosts.tab. The manager hosts
7830: are described in the
7831: ~httpd/lonTabs/managers.tab file.
7832: this message is logged if this file is missing.
7833:
7834:
7835: =item (Green) Registering manager <dnsname> as <cluster_name> with <ipaddress>
7836:
7837: Reports the successful parse and registration
7838: of a specific manager.
7839:
7840: =item Green existing host <clustername:dnsname>
7841:
7842: The manager host is already defined in the hosts.tab
7843: the information in that table, rather than the info in the
7844: manager table will be used to determine the manager's ip.
7845:
7846: =item (Red) Unable to craete <filename>
7847:
7848: lond has been asked to create new versions of an administrative
7849: file (by a manager). When this is done, the new file is created
7850: in a temp file and then renamed into place so that there are always
7851: usable administrative files, even if the update fails. This failure
7852: message means that the temp file could not be created.
7853: The update is abandoned, and the old file is available for use.
7854:
7855: =item (Green) CopyFile from <oldname> to <newname> failed
7856:
7857: In an update of administrative files, the copy of the existing file to a
7858: backup file failed. The installation of the new file may still succeed,
7859: but there will not be a back up file to rever to (this should probably
7860: be yellow).
7861:
7862: =item (Green) Pushfile: backed up <oldname> to <newname>
7863:
7864: See above, the backup of the old administrative file succeeded.
7865:
7866: =item (Red) Pushfile: Unable to install <filename> <reason>
7867:
7868: The new administrative file could not be installed. In this case,
7869: the old administrative file is still in use.
7870:
7871: =item (Green) Installed new < filename>.
7872:
7873: The new administrative file was successfullly installed.
7874:
7875: =item (Red) Reinitializing lond pid=<pid>
7876:
7877: The lonc child process <pid> will be sent a USR2
7878: signal.
7879:
7880: =item (Red) Reinitializing self
7881:
7882: We've been asked to re-read our administrative files,and
7883: are doing so.
7884:
7885: =item (Yellow) error:Invalid process identifier <ident>
7886:
7887: A reinit command was received, but the target part of the
7888: command was not valid. It must be either
7889: 'lond' or 'lonc' but was <ident>
7890:
7891: =item (Green) isValideditCommand checking: Command = <command> Key = <key> newline = <newline>
7892:
7893: Checking to see if lond has been handed a valid edit
7894: command. It is possible the edit command is not valid
7895: in that case there are no log messages to indicate that.
7896:
7897: =item Result of password change for <username> pwchange_success
7898:
7899: The password for <username> was
7900: successfully changed.
7901:
7902: =item Unable to open <user> passwd to change password
7903:
7904: Could not rewrite the
7905: internal password file for a user
7906:
7907: =item Result of password change for <user> : <result>
1.490 droeschl 7908:
1.409 foxr 7909: A unix password change for <user> was attempted
7910: and the pipe returned <result>
7911:
7912: =item LWP GET: <message> for <fname> (<remoteurl>)
7913:
7914: The lightweight process fetch for a resource failed
7915: with <message> the local filename that should
7916: have existed/been created was <fname> the
7917: corresponding URI: <remoteurl> This is emitted in several
7918: places.
7919:
7920: =item Unable to move <transname> to <destname>
7921:
7922: From fetch_user_file_handler - the user file was replicated but could not
7923: be mv'd to its final location.
7924:
7925: =item Looking for <domain> <username>
7926:
7927: From user_has_session_handler - This should be a Debug call instead
7928: it indicates lond is about to check whether the specified user has a
7929: session active on the specified domain on the local host.
7930:
7931: =item Client <ip> (<name>) hanging up: <input>
7932:
7933: lond has been asked to exit by its client. The <ip> and <name> identify the
7934: client systemand <input> is the full exit command sent to the server.
7935:
7936: =item Red CRITICAL: ABNORMAL EXIT. child <pid> for server <hostname> died through a crass with this error->[<message>].
1.490 droeschl 7937:
1.409 foxr 7938: A lond child terminated. NOte that this termination can also occur when the
7939: child receives the QUIT or DIE signals. <pid> is the process id of the child,
7940: <hostname> the host lond is working for, and <message> the reason the child died
7941: to the best of our ability to get it (I would guess that any numeric value
7942: represents and errno value). This is immediately followed by
7943:
7944: =item Famous last words: Catching exception - <log>
7945:
7946: Where log is some recent information about the state of the child.
7947:
7948: =item Red CRITICAL: TIME OUT <pid>
7949:
7950: Some timeout occured for server <pid>. THis is normally a timeout on an LWP
7951: doing an HTTP::GET.
7952:
7953: =item child <pid> died
7954:
7955: The reaper caught a SIGCHILD for the lond child process <pid>
7956: This should be modified to also display the IP of the dying child
7957: $children{$pid}
7958:
7959: =item Unknown child 0 died
7960: A child died but the wait for it returned a pid of zero which really should not
7961: ever happen.
7962:
7963: =item Child <which> - <pid> looks like we missed it's death
7964:
7965: When a sigchild is received, the reaper process checks all children to see if they are
7966: alive. If children are dying quite quickly, the lack of signal queuing can mean
7967: that a signal hearalds the death of more than one child. If so this message indicates
7968: which other one died. <which> is the ip of a dead child
7969:
7970: =item Free socket: <shutdownretval>
7971:
7972: The HUNTSMAN sub was called due to a SIGINT in a child process. The socket is being shutdown.
7973: for whatever reason, <shutdownretval> is printed but in fact shutdown() is not documented
7974: to return anything. This is followed by:
7975:
7976: =item Red CRITICAL: Shutting down
7977:
7978: Just prior to exit.
7979:
7980: =item Free socket: <shutdownretval>
7981:
7982: The HUPSMAN sub was called due to a SIGHUP. all children get killsed, and lond execs itself.
7983: This is followed by:
7984:
7985: =item (Red) CRITICAL: Restarting
7986:
7987: lond is about to exec itself to restart.
7988:
7989: =item (Blue) Updating connections
7990:
7991: (In response to a USR2). All the children (except the one for localhost)
7992: are about to be killed, the hosts tab reread, and Apache reloaded via apachereload.
7993:
7994: =item (Blue) UpdateHosts killing child <pid> for ip <ip>
7995:
7996: Due to USR2 as above.
7997:
7998: =item (Green) keeping child for ip <ip> (pid = <pid>)
7999:
8000: In response to USR2 as above, the child indicated is not being restarted because
8001: it's assumed that we'll always need a child for the localhost.
8002:
8003:
8004: =item Going to check on the children
8005:
8006: Parent is about to check on the health of the child processes.
8007: Note that this is in response to a USR1 sent to the parent lond.
8008: there may be one or more of the next two messages:
8009:
8010: =item <pid> is dead
8011:
8012: A child that we have in our child hash as alive has evidently died.
8013:
8014: =item Child <pid> did not respond
8015:
8016: In the health check the child <pid> did not update/produce a pid_.txt
8017: file when sent it's USR1 signal. That process is killed with a 9 signal, as it's
8018: assumed to be hung in some un-fixable way.
8019:
8020: =item Finished checking children
1.490 droeschl 8021:
1.409 foxr 8022: Master processs's USR1 processing is cojmplete.
8023:
8024: =item (Red) CRITICAL: ------- Starting ------
8025:
8026: (There are more '-'s on either side). Lond has forked itself off to
8027: form a new session and is about to start actual initialization.
8028:
8029: =item (Green) Attempting to start child (<client>)
8030:
8031: Started a new child process for <client>. Client is IO::Socket object
8032: connected to the child. This was as a result of a TCP/IP connection from a client.
8033:
8034: =item Unable to determine who caller was, getpeername returned nothing
1.490 droeschl 8035:
1.409 foxr 8036: In child process initialization. either getpeername returned undef or
8037: a zero sized object was returned. Processing continues, but in my opinion,
8038: this should be cause for the child to exit.
8039:
8040: =item Unable to determine clientip
8041:
8042: In child process initialization. The peer address from getpeername was not defined.
8043: The client address is stored as "Unavailable" and processing continues.
8044:
8045: =item (Yellow) INFO: Connection <ip> <name> connection type = <type>
1.490 droeschl 8046:
1.409 foxr 8047: In child initialization. A good connectionw as received from <ip>.
8048:
8049: =over 2
8050:
8051: =item <name>
8052:
8053: is the name of the client from hosts.tab.
8054:
8055: =item <type>
8056:
8057: Is the connection type which is either
8058:
8059: =over 2
8060:
8061: =item manager
8062:
8063: The connection is from a manager node, not in hosts.tab
8064:
8065: =item client
8066:
8067: the connection is from a non-manager in the hosts.tab
8068:
8069: =item both
8070:
8071: The connection is from a manager in the hosts.tab.
8072:
8073: =back
8074:
8075: =back
8076:
8077: =item (Blue) Certificates not installed -- trying insecure auth
8078:
8079: One of the certificate file, key file or
8080: certificate authority file could not be found for a client attempting
8081: SSL connection intiation. COnnection will be attemptied in in-secure mode.
8082: (this would be a system with an up to date lond that has not gotten a
8083: certificate from us).
8084:
8085: =item (Green) Successful local authentication
8086:
8087: A local connection successfully negotiated the encryption key.
8088: In this case the IDEA key is in a file (that is hopefully well protected).
8089:
8090: =item (Green) Successful ssl authentication with <client>
8091:
8092: The client (<client> is the peer's name in hosts.tab), has successfully
8093: negotiated an SSL connection with this child process.
8094:
8095: =item (Green) Successful insecure authentication with <client>
1.490 droeschl 8096:
1.409 foxr 8097:
8098: The client has successfully negotiated an insecure connection withthe child process.
8099:
8100: =item (Yellow) Attempted insecure connection disallowed
8101:
8102: The client attempted and failed to successfully negotiate a successful insecure
8103: connection. This can happen either because the variable londAllowInsecure is false
8104: or undefined, or becuse the child did not successfully echo back the challenge
8105: string.
8106:
8107:
8108: =back
8109:
1.441 raeburn 8110: =back
8111:
1.409 foxr 8112:
8113: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>