Annotation of loncom/lond, revision 1.489.2.35.2.5

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>