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