Annotation of loncom/lond, revision 1.143
1.1 albertel 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # lond "LON Daemon" Server (port "LOND" 5663)
1.60 www 4: #
1.143 ! foxr 5: # $Id: lond,v 1.142 2003/09/09 20:47:46 www 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
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
28: #
1.1 albertel 29: # 5/26/99,6/4,6/10,6/11,6/14,6/15,6/26,6/28,6/30,
1.2 www 30: # 7/8,7/9,7/10,7/12,7/17,7/19,9/21,
1.6 www 31: # 10/7,10/8,10/9,10/11,10/13,10/15,11/4,11/16,
1.11 www 32: # 12/7,12/15,01/06,01/11,01/12,01/14,2/8,
1.12 harris41 33: # 03/07,05/31 Gerd Kortemeyer
1.20 www 34: # 06/29,06/30,07/14,07/15,07/17,07/20,07/25,09/18 Gerd Kortemeyer
1.34 www 35: # 12/05,12/13,12/29 Gerd Kortemeyer
1.61 harris41 36: # YEAR=2001
1.36 www 37: # 02/12 Gerd Kortemeyer
1.41 www 38: # 03/24 Gerd Kortemeyer
1.51 www 39: # 05/11,05/28,08/30 Gerd Kortemeyer
1.59 www 40: # 11/26,11/27 Gerd Kortemeyer
1.62 www 41: # 12/22 Gerd Kortemeyer
1.63 www 42: # YEAR=2002
1.65 www 43: # 01/20/02,02/05 Gerd Kortemeyer
1.71 www 44: # 02/05 Guy Albertelli
45: # 02/12 Gerd Kortemeyer
1.73 www 46: # 02/19 Matthew Hall
47: # 02/25 Gerd Kortemeyer
1.106 foxr 48: # 01/xx/2003 Ron Fox.. Remove preforking. This makes the general daemon
49: # logic simpler (and there were problems maintaining the preforked
50: # population). Since the time averaged connection rate is close to zero
51: # because lonc's purpose is to maintain near continuous connnections,
52: # preforking is not really needed.
1.135 foxr 53: # 08/xx/2003 Ron Fox: Add management requests. Management requests
54: # will be validated via a call to ValidateManager. At present, this
55: # is done by simple host verification. In the future we can modify
56: # this function to do a certificate check.
57: # Management functions supported include:
58: # - pushing /home/httpd/lonTabs/hosts.tab
59: # - pushing /home/httpd/lonTabs/domain.tab
1.141 foxr 60: # 09/08/2003 Ron Fox: Told lond to take care of change logging so we
61: # don't have to remember it:
1.142 www 62: # $Log: lond,v $
1.143 ! foxr 63: # Revision 1.142 2003/09/09 20:47:46 www
! 64: # Permanently store chatroom entries in chatroom.log
! 65: #
1.142 www 66: # Revision 1.141 2003/09/08 10:32:07 foxr
67: # Added PushFile sub This sub oversees the push of a new configuration table file
68: # Currently supported files are:
69: # - hosts.tab (transaction pushfile:hosts:contents)
70: # - domain.tab (transaction pushfile:domain:contents)
71: #
1.141 foxr 72:
1.54 harris41 73:
1.134 albertel 74: use strict;
1.80 harris41 75: use lib '/home/httpd/lib/perl/';
76: use LONCAPA::Configuration;
77:
1.1 albertel 78: use IO::Socket;
79: use IO::File;
1.126 albertel 80: #use Apache::File;
1.1 albertel 81: use Symbol;
82: use POSIX;
83: use Crypt::IDEA;
84: use LWP::UserAgent();
1.3 www 85: use GDBM_File;
86: use Authen::Krb4;
1.91 albertel 87: use Authen::Krb5;
1.49 albertel 88: use lib '/home/httpd/lib/perl/';
89: use localauth;
1.143 ! foxr 90: use File::Copy;
1.1 albertel 91:
1.77 foxr 92: my $DEBUG = 0; # Non zero to enable debug log entries.
93:
1.57 www 94: my $status='';
95: my $lastlog='';
96:
1.143 ! foxr 97: my $VERSION='$Revision: 1.142 $'; #' stupid emacs
1.121 albertel 98: my $remoteVERSION;
1.115 albertel 99: my $currenthostid;
100: my $currentdomainid;
1.134 albertel 101:
102: my $client;
1.140 foxr 103: my $clientip;
104:
1.134 albertel 105: my $server;
106: my $thisserver;
107:
108: my %hostid;
109: my %hostdom;
110: my %hostip;
1.141 foxr 111: my %perlvar; # Will have the apache conf defined perl vars.
1.134 albertel 112:
1.96 foxr 113: #
114: # The array below are password error strings."
115: #
1.97 foxr 116: my $lastpwderror = 13; # Largest error number from lcpasswd.
1.96 foxr 117: my @passwderrors = ("ok",
118: "lcpasswd must be run as user 'www'",
119: "lcpasswd got incorrect number of arguments",
120: "lcpasswd did not get the right nubmer of input text lines",
121: "lcpasswd too many simultaneous pwd changes in progress",
122: "lcpasswd User does not exist.",
123: "lcpasswd Incorrect current passwd",
124: "lcpasswd Unable to su to root.",
125: "lcpasswd Cannot set new passwd.",
126: "lcpasswd Username has invalid characters",
1.97 foxr 127: "lcpasswd Invalid characters in password",
128: "11", "12",
129: "lcpasswd Password mismatch");
130:
131:
132: # The array below are lcuseradd error strings.:
133:
134: my $lastadderror = 13;
135: my @adderrors = ("ok",
136: "User ID mismatch, lcuseradd must run as user www",
137: "lcuseradd Incorrect number of command line parameters must be 3",
138: "lcuseradd Incorrect number of stdinput lines, must be 3",
139: "lcuseradd Too many other simultaneous pwd changes in progress",
140: "lcuseradd User does not exist",
141: "lcuseradd Unabel to mak ewww member of users's group",
142: "lcuseradd Unable to su to root",
143: "lcuseradd Unable to set password",
144: "lcuseradd Usrname has invbalid charcters",
145: "lcuseradd Password has an invalid character",
146: "lcuseradd User already exists",
147: "lcuseradd Could not add user.",
148: "lcuseradd Password mismatch");
149:
1.96 foxr 150:
151: #
1.140 foxr 152: # GetCertificate: Given a transaction that requires a certificate,
153: # this function will extract the certificate from the transaction
154: # request. Note that at this point, the only concept of a certificate
155: # is the hostname to which we are connected.
156: #
157: # Parameter:
158: # request - The request sent by our client (this parameterization may
159: # need to change when we really use a certificate granting
160: # authority.
161: #
162: sub GetCertificate {
163: my $request = shift;
164:
165: return $clientip;
166: }
167:
168:
169: #
170: # ValidManager: Determines if a given certificate represents a valid manager.
171: # in this primitive implementation, the 'certificate' is
172: # just the connecting loncapa client name. This is checked
173: # against a valid client list in the configuration.
174: #
175: #
176: sub ValidManager {
177: my $certificate = shift;
178:
179: my $hostentry = $hostid{$certificate};
180: if ($hostentry ne undef) {
181: &logthis('<font color="yellow">Authenticating manager'.
182: " $hostentry</font>");
183: return 1;
184: } else {
185: &logthis('<font color="red"> Failed manager authentication '.
186: "$certificate </font>");
187: }
188: }
189: #
1.143 ! foxr 190: # CopyFile: Called as part of the process of installing a
! 191: # new configuration file. This function copies an existing
! 192: # file to a backup file.
! 193: # Parameters:
! 194: # oldfile - Name of the file to backup.
! 195: # newfile - Name of the backup file.
! 196: # Return:
! 197: # 0 - Failure (errno has failure reason).
! 198: # 1 - Success.
! 199: #
! 200: sub CopyFile {
! 201: my $oldfile = shift;
! 202: my $newfile = shift;
! 203:
! 204: # The file must exist:
! 205:
! 206: if(-e $oldfile) {
! 207:
! 208: # Read the old file.
! 209:
! 210: my $oldfh = IO::File->new("< $oldfile");
! 211: if(!$oldfh) {
! 212: return 0;
! 213: }
! 214: my @contents = <$oldfh>; # Suck in the entire file.
! 215:
! 216: # write the backup file:
! 217:
! 218: my $newfh = IO::File->new("> $newfile");
! 219: if(!(defined $newfh)){
! 220: return 0;
! 221: }
! 222: my $lines = scalar @contents;
! 223: for (my $i =0; $i < $lines; $i++) {
! 224: print $newfh ($contents[$i]);
! 225: }
! 226:
! 227: $oldfh->close;
! 228: $newfh->close;
! 229:
! 230: chmod(0660, $newfile);
! 231:
! 232: return 1;
! 233:
! 234: } else {
! 235: return 0;
! 236: }
! 237: }
! 238:
! 239: #
! 240: # InstallFile: Called to install an administrative file:
! 241: # - The file is created with <name>.tmp
! 242: # - The <name>.tmp file is then mv'd to <name>
! 243: # This lugubrious procedure is done to ensure that we are never without
! 244: # a valid, even if dated, version of the file regardless of who crashes
! 245: # and when the crash occurs.
! 246: #
! 247: # Parameters:
! 248: # Name of the file
! 249: # File Contents.
! 250: # Return:
! 251: # nonzero - success.
! 252: # 0 - failure and $! has an errno.
! 253: #
! 254: sub InstallFile {
! 255: my $Filename = shift;
! 256: my $Contents = shift;
! 257: my $TempFile = $Filename.".tmp";
! 258:
! 259: # Open the file for write:
! 260:
! 261: my $fh = IO::File->new("> $TempFile"); # Write to temp.
! 262: if(!(defined $fh)) {
! 263: &logthis('<font color="red"> Unable to create '.$TempFile."</font>");
! 264: return 0;
! 265: }
! 266: # write the contents of the file:
! 267:
! 268: print $fh ($Contents);
! 269: $fh->close; # In case we ever have a filesystem w. locking
! 270:
! 271: chmod(0660, $TempFile);
! 272:
! 273: # Now we can move install the file in position.
! 274:
! 275: move($TempFile, $Filename);
! 276:
! 277: return 1;
! 278: }
! 279:
! 280: #
1.141 foxr 281: # PushFile: Called to do an administrative push of a file.
282: # - Ensure the file being pushed is one we support.
283: # - Backup the old file to <filename.saved>
284: # - Separate the contents of the new file out from the
285: # rest of the request.
286: # - Write the new file.
287: # Parameter:
288: # Request - The entire user request. This consists of a : separated
289: # string pushfile:tablename:contents.
290: # NOTE: The contents may have :'s in it as well making things a bit
291: # more interesting... but not much.
292: # Returns:
293: # String to send to client ("ok" or "refused" if bad file).
294: #
295: sub PushFile {
296: my $request = shift;
297: my ($command, $filename, $contents) = split(":", $request, 3);
298:
299: # At this point in time, pushes for only the following tables are
300: # supported:
301: # hosts.tab ($filename eq host).
302: # domain.tab ($filename eq domain).
303: # Construct the destination filename or reject the request.
304: #
305: # lonManage is supposed to ensure this, however this session could be
306: # part of some elaborate spoof that managed somehow to authenticate.
307: #
308:
309: my $tablefile = $perlvar{'lonTabDir'}.'/'; # need to precede with dir.
310: if ($filename eq "host") {
311: $tablefile .= "hosts.tab";
312: } elsif ($filename eq "domain") {
313: $tablefile .= "domain.tab";
314: } else {
315: return "refused";
316: }
317: #
318: # >copy< the old table to the backup table
319: # don't rename in case system crashes/reboots etc. in the time
320: # window between a rename and write.
321: #
322: my $backupfile = $tablefile;
323: $backupfile =~ s/\.tab$/.old/;
1.143 ! foxr 324: if(!CopyFile($tablefile, $backupfile)) {
! 325: &logthis('<font color="green"> CopyFile from '.$tablefile." to ".$backupfile." failed </font>");
! 326: return "error:$!";
! 327: }
1.141 foxr 328: &logthis('<font color="green"> Pushfile: backed up '
329: .$tablefile." to $backupfile</font>");
330:
331: # Install the new file:
332:
1.143 ! foxr 333: if(!InstallFile($tablefile, $contents)) {
! 334: &logthis('<font color="red"> Pushfile: unable to install '
! 335: .$tablefile." $! </font>");
! 336: return "error:$!";
! 337: }
! 338: else {
! 339: &logthis('<font color="green"> Installed new '.$tablefile
! 340: ."</font>");
! 341:
! 342: }
! 343:
1.141 foxr 344:
345: # Indicate success:
346:
347: return "ok";
348:
349: }
350: #
1.96 foxr 351: # Convert an error return code from lcpasswd to a string value.
352: #
353: sub lcpasswdstrerror {
354: my $ErrorCode = shift;
1.97 foxr 355: if(($ErrorCode < 0) || ($ErrorCode > $lastpwderror)) {
1.96 foxr 356: return "lcpasswd Unrecognized error return value ".$ErrorCode;
357: } else {
1.98 foxr 358: return $passwderrors[$ErrorCode];
1.96 foxr 359: }
360: }
361:
1.97 foxr 362: #
363: # Convert an error return code from lcuseradd to a string value:
364: #
365: sub lcuseraddstrerror {
366: my $ErrorCode = shift;
367: if(($ErrorCode < 0) || ($ErrorCode > $lastadderror)) {
368: return "lcuseradd - Unrecognized error code: ".$ErrorCode;
369: } else {
1.98 foxr 370: return $adderrors[$ErrorCode];
1.97 foxr 371: }
372: }
373:
1.23 harris41 374: # grabs exception and records it to log before exiting
375: sub catchexception {
1.27 albertel 376: my ($error)=@_;
1.25 www 377: $SIG{'QUIT'}='DEFAULT';
378: $SIG{__DIE__}='DEFAULT';
1.23 harris41 379: &logthis("<font color=red>CRITICAL: "
1.134 albertel 380: ."ABNORMAL EXIT. Child $$ for server $thisserver died through "
1.27 albertel 381: ."a crash with this error msg->[$error]</font>");
1.57 www 382: &logthis('Famous last words: '.$status.' - '.$lastlog);
1.27 albertel 383: if ($client) { print $client "error: $error\n"; }
1.59 www 384: $server->close();
1.27 albertel 385: die($error);
1.23 harris41 386: }
387:
1.63 www 388: sub timeout {
389: &logthis("<font color=ref>CRITICAL: TIME OUT ".$$."</font>");
390: &catchexception('Timeout');
391: }
1.22 harris41 392: # -------------------------------- Set signal handlers to record abnormal exits
393:
394: $SIG{'QUIT'}=\&catchexception;
395: $SIG{__DIE__}=\&catchexception;
396:
1.81 matthew 397: # ---------------------------------- Read loncapa_apache.conf and loncapa.conf
1.95 harris41 398: &status("Read loncapa.conf and loncapa_apache.conf");
399: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
1.141 foxr 400: %perlvar=%{$perlvarref};
1.80 harris41 401: undef $perlvarref;
1.19 www 402:
1.35 harris41 403: # ----------------------------- Make sure this process is running from user=www
404: my $wwwid=getpwnam('www');
405: if ($wwwid!=$<) {
1.134 albertel 406: my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
407: my $subj="LON: $currenthostid User ID mismatch";
1.37 harris41 408: system("echo 'User ID mismatch. lond must be run as user www.' |\
1.35 harris41 409: mailto $emailto -s '$subj' > /dev/null");
410: exit 1;
411: }
412:
1.19 www 413: # --------------------------------------------- Check if other instance running
414:
415: my $pidfile="$perlvar{'lonDaemons'}/logs/lond.pid";
416:
417: if (-e $pidfile) {
418: my $lfh=IO::File->new("$pidfile");
419: my $pide=<$lfh>;
420: chomp($pide);
1.29 harris41 421: if (kill 0 => $pide) { die "already running"; }
1.19 www 422: }
1.1 albertel 423:
424: # ------------------------------------------------------------- Read hosts file
425:
1.29 harris41 426: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
1.1 albertel 427:
1.134 albertel 428: while (my $configline=<CONFIG>) {
1.1 albertel 429: my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
1.70 harris41 430: chomp($ip); $ip=~s/\D+$//;
1.1 albertel 431: $hostid{$ip}=$id;
1.115 albertel 432: $hostdom{$id}=$domain;
433: $hostip{$id}=$ip;
1.116 albertel 434: if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
1.1 albertel 435: }
436: close(CONFIG);
437:
438: # establish SERVER socket, bind and listen.
439: $server = IO::Socket::INET->new(LocalPort => $perlvar{'londPort'},
440: Type => SOCK_STREAM,
441: Proto => 'tcp',
442: Reuse => 1,
443: Listen => 10 )
1.29 harris41 444: or die "making socket: $@\n";
1.1 albertel 445:
446: # --------------------------------------------------------- Do global variables
447:
448: # global variables
449:
1.134 albertel 450: my %children = (); # keys are current child process IDs
451: my $children = 0; # current number of children
1.1 albertel 452:
453: sub REAPER { # takes care of dead children
454: $SIG{CHLD} = \&REAPER;
455: my $pid = wait;
1.67 albertel 456: if (defined($children{$pid})) {
457: &logthis("Child $pid died");
458: $children --;
459: delete $children{$pid};
460: } else {
461: &logthis("Unknown Child $pid died");
462: }
1.1 albertel 463: }
464:
465: sub HUNTSMAN { # signal handler for SIGINT
466: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
467: kill 'INT' => keys %children;
1.59 www 468: &logthis("Free socket: ".shutdown($server,2)); # free up socket
1.1 albertel 469: my $execdir=$perlvar{'lonDaemons'};
470: unlink("$execdir/logs/lond.pid");
1.9 www 471: &logthis("<font color=red>CRITICAL: Shutting down</font>");
1.1 albertel 472: exit; # clean up with dignity
473: }
474:
475: sub HUPSMAN { # signal handler for SIGHUP
476: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
477: kill 'INT' => keys %children;
1.59 www 478: &logthis("Free socket: ".shutdown($server,2)); # free up socket
1.9 www 479: &logthis("<font color=red>CRITICAL: Restarting</font>");
1.134 albertel 480: my $execdir=$perlvar{'lonDaemons'};
1.30 harris41 481: unlink("$execdir/logs/lond.pid");
1.1 albertel 482: exec("$execdir/lond"); # here we go again
483: }
484:
1.57 www 485: sub checkchildren {
486: &initnewstatus();
487: &logstatus();
488: &logthis('Going to check on the children');
1.134 albertel 489: my $docdir=$perlvar{'lonDocRoot'};
1.61 harris41 490: foreach (sort keys %children) {
1.57 www 491: sleep 1;
492: unless (kill 'USR1' => $_) {
493: &logthis ('Child '.$_.' is dead');
494: &logstatus($$.' is dead');
495: }
1.61 harris41 496: }
1.63 www 497: sleep 5;
1.113 albertel 498: $SIG{ALRM} = sub { die "timeout" };
499: $SIG{__DIE__} = 'DEFAULT';
1.63 www 500: foreach (sort keys %children) {
501: unless (-e "$docdir/lon-status/londchld/$_.txt") {
1.113 albertel 502: eval {
503: alarm(300);
1.63 www 504: &logthis('Child '.$_.' did not respond');
1.67 albertel 505: kill 9 => $_;
1.131 albertel 506: #$emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
507: #$subj="LON: $currenthostid killed lond process $_";
508: #my $result=`echo 'Killed lond process $_.' | mailto $emailto -s '$subj' > /dev/null`;
509: #$execdir=$perlvar{'lonDaemons'};
510: #$result=`/bin/cp $execdir/logs/lond.log $execdir/logs/lond.log.$_`;
1.113 albertel 511: alarm(0);
512: }
1.63 www 513: }
514: }
1.113 albertel 515: $SIG{ALRM} = 'DEFAULT';
516: $SIG{__DIE__} = \&cathcexception;
1.57 www 517: }
518:
1.1 albertel 519: # --------------------------------------------------------------------- Logging
520:
521: sub logthis {
522: my $message=shift;
523: my $execdir=$perlvar{'lonDaemons'};
524: my $fh=IO::File->new(">>$execdir/logs/lond.log");
525: my $now=time;
526: my $local=localtime($now);
1.58 www 527: $lastlog=$local.': '.$message;
1.1 albertel 528: print $fh "$local ($$): $message\n";
529: }
530:
1.77 foxr 531: # ------------------------- Conditional log if $DEBUG true.
532: sub Debug {
533: my $message = shift;
534: if($DEBUG) {
535: &logthis($message);
536: }
537: }
1.57 www 538: # ------------------------------------------------------------------ Log status
539:
540: sub logstatus {
541: my $docdir=$perlvar{'lonDocRoot'};
1.63 www 542: {
1.57 www 543: my $fh=IO::File->new(">>$docdir/lon-status/londstatus.txt");
1.115 albertel 544: print $fh $$."\t".$currenthostid."\t".$status."\t".$lastlog."\n";
1.63 www 545: $fh->close();
546: }
547: {
548: my $fh=IO::File->new(">$docdir/lon-status/londchld/$$.txt");
549: print $fh $status."\n".$lastlog."\n".time;
550: $fh->close();
551: }
1.57 www 552: }
553:
554: sub initnewstatus {
555: my $docdir=$perlvar{'lonDocRoot'};
556: my $fh=IO::File->new(">$docdir/lon-status/londstatus.txt");
557: my $now=time;
558: my $local=localtime($now);
559: print $fh "LOND status $local - parent $$\n\n";
1.64 www 560: opendir(DIR,"$docdir/lon-status/londchld");
1.134 albertel 561: while (my $filename=readdir(DIR)) {
1.64 www 562: unlink("$docdir/lon-status/londchld/$filename");
563: }
564: closedir(DIR);
1.57 www 565: }
566:
567: # -------------------------------------------------------------- Status setting
568:
569: sub status {
570: my $what=shift;
571: my $now=time;
572: my $local=localtime($now);
573: $status=$local.': '.$what;
1.103 www 574: $0='lond: '.$what.' '.$local;
1.57 www 575: }
1.11 www 576:
577: # -------------------------------------------------------- Escape Special Chars
578:
579: sub escape {
580: my $str=shift;
581: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
582: return $str;
583: }
584:
585: # ----------------------------------------------------- Un-Escape Special Chars
586:
587: sub unescape {
588: my $str=shift;
589: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
590: return $str;
591: }
592:
1.1 albertel 593: # ----------------------------------------------------------- Send USR1 to lonc
594:
595: sub reconlonc {
596: my $peerfile=shift;
597: &logthis("Trying to reconnect for $peerfile");
598: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
599: if (my $fh=IO::File->new("$loncfile")) {
600: my $loncpid=<$fh>;
601: chomp($loncpid);
602: if (kill 0 => $loncpid) {
603: &logthis("lonc at pid $loncpid responding, sending USR1");
604: kill USR1 => $loncpid;
605: } else {
1.9 www 606: &logthis(
607: "<font color=red>CRITICAL: "
608: ."lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 609: }
610: } else {
1.9 www 611: &logthis('<font color=red>CRITICAL: lonc not running, giving up</font>');
1.1 albertel 612: }
613: }
614:
615: # -------------------------------------------------- Non-critical communication
1.11 www 616:
1.1 albertel 617: sub subreply {
618: my ($cmd,$server)=@_;
619: my $peerfile="$perlvar{'lonSockDir'}/$server";
620: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
621: Type => SOCK_STREAM,
622: Timeout => 10)
623: or return "con_lost";
624: print $sclient "$cmd\n";
625: my $answer=<$sclient>;
626: chomp($answer);
627: if (!$answer) { $answer="con_lost"; }
628: return $answer;
629: }
630:
631: sub reply {
632: my ($cmd,$server)=@_;
633: my $answer;
1.115 albertel 634: if ($server ne $currenthostid) {
1.1 albertel 635: $answer=subreply($cmd,$server);
636: if ($answer eq 'con_lost') {
637: $answer=subreply("ping",$server);
638: if ($answer ne $server) {
1.115 albertel 639: &logthis("sub reply: answer != server answer is $answer, server is $server");
1.1 albertel 640: &reconlonc("$perlvar{'lonSockDir'}/$server");
641: }
642: $answer=subreply($cmd,$server);
643: }
644: } else {
645: $answer='self_reply';
646: }
647: return $answer;
648: }
649:
1.13 www 650: # -------------------------------------------------------------- Talk to lonsql
651:
1.12 harris41 652: sub sqlreply {
653: my ($cmd)=@_;
654: my $answer=subsqlreply($cmd);
655: if ($answer eq 'con_lost') { $answer=subsqlreply($cmd); }
656: return $answer;
657: }
658:
659: sub subsqlreply {
660: my ($cmd)=@_;
661: my $unixsock="mysqlsock";
662: my $peerfile="$perlvar{'lonSockDir'}/$unixsock";
663: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
664: Type => SOCK_STREAM,
665: Timeout => 10)
666: or return "con_lost";
667: print $sclient "$cmd\n";
668: my $answer=<$sclient>;
669: chomp($answer);
670: if (!$answer) { $answer="con_lost"; }
671: return $answer;
672: }
673:
1.1 albertel 674: # -------------------------------------------- Return path to profile directory
1.11 www 675:
1.1 albertel 676: sub propath {
677: my ($udom,$uname)=@_;
678: $udom=~s/\W//g;
679: $uname=~s/\W//g;
1.16 www 680: my $subdir=$uname.'__';
1.1 albertel 681: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
682: my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
683: return $proname;
684: }
685:
686: # --------------------------------------- Is this the home server of an author?
1.11 www 687:
1.1 albertel 688: sub ishome {
689: my $author=shift;
690: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
691: my ($udom,$uname)=split(/\//,$author);
692: my $proname=propath($udom,$uname);
693: if (-e $proname) {
694: return 'owner';
695: } else {
696: return 'not_owner';
697: }
698: }
699:
700: # ======================================================= Continue main program
701: # ---------------------------------------------------- Fork once and dissociate
702:
1.134 albertel 703: my $fpid=fork;
1.1 albertel 704: exit if $fpid;
1.29 harris41 705: die "Couldn't fork: $!" unless defined ($fpid);
1.1 albertel 706:
1.29 harris41 707: POSIX::setsid() or die "Can't start new session: $!";
1.1 albertel 708:
709: # ------------------------------------------------------- Write our PID on disk
710:
1.134 albertel 711: my $execdir=$perlvar{'lonDaemons'};
1.1 albertel 712: open (PIDSAVE,">$execdir/logs/lond.pid");
713: print PIDSAVE "$$\n";
714: close(PIDSAVE);
1.9 www 715: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
1.57 www 716: &status('Starting');
1.1 albertel 717:
1.106 foxr 718:
1.1 albertel 719:
720: # ----------------------------------------------------- Install signal handlers
721:
1.57 www 722:
1.1 albertel 723: $SIG{CHLD} = \&REAPER;
724: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
725: $SIG{HUP} = \&HUPSMAN;
1.57 www 726: $SIG{USR1} = \&checkchildren;
1.1 albertel 727:
1.106 foxr 728:
729:
730: # --------------------------------------------------------------
731: # Accept connections. When a connection comes in, it is validated
732: # and if good, a child process is created to process transactions
733: # along the connection.
734:
1.1 albertel 735: while (1) {
1.106 foxr 736: $client = $server->accept() or next;
737: make_new_child($client);
1.1 albertel 738: }
739:
740: sub make_new_child {
741: my $pid;
742: my $cipher;
743: my $sigset;
1.106 foxr 744:
745: $client = shift;
1.1 albertel 746: &logthis("Attempting to start child");
747: # block signal for fork
748: $sigset = POSIX::SigSet->new(SIGINT);
749: sigprocmask(SIG_BLOCK, $sigset)
1.29 harris41 750: or die "Can't block SIGINT for fork: $!\n";
1.134 albertel 751:
1.29 harris41 752: die "fork: $!" unless defined ($pid = fork);
1.1 albertel 753:
754: if ($pid) {
755: # Parent records the child's birth and returns.
756: sigprocmask(SIG_UNBLOCK, $sigset)
1.29 harris41 757: or die "Can't unblock SIGINT for fork: $!\n";
1.1 albertel 758: $children{$pid} = 1;
759: $children++;
1.57 www 760: &status('Started child '.$pid);
1.1 albertel 761: return;
762: } else {
763: # Child can *not* return from this subroutine.
764: $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before
1.126 albertel 765: $SIG{CHLD} = 'DEFAULT'; #make this default so that pwauth returns
766: #don't get intercepted
1.57 www 767: $SIG{USR1}= \&logstatus;
1.63 www 768: $SIG{ALRM}= \&timeout;
1.57 www 769: $lastlog='Forked ';
770: $status='Forked';
771:
1.1 albertel 772: # unblock signals
773: sigprocmask(SIG_UNBLOCK, $sigset)
1.29 harris41 774: or die "Can't unblock SIGINT for fork: $!\n";
1.13 www 775:
1.134 albertel 776: my $tmpsnum=0;
1.91 albertel 777: #---------------------------------------------------- kerberos 5 initialization
778: &Authen::Krb5::init_context();
779: &Authen::Krb5::init_ets();
780:
1.57 www 781: &status('Accepted connection');
1.1 albertel 782: # =============================================================================
783: # do something with the connection
784: # -----------------------------------------------------------------------------
1.94 albertel 785: $client->sockopt(SO_KEEPALIVE, 1);# Enable monitoring of
786: # connection liveness.
1.2 www 787: # see if we know client and check for spoof IP by challenge
1.106 foxr 788: my $caller = getpeername($client);
1.1 albertel 789: my ($port,$iaddr)=unpack_sockaddr_in($caller);
1.134 albertel 790: $clientip=inet_ntoa($iaddr);
1.1 albertel 791: my $clientrec=($hostid{$clientip} ne undef);
1.9 www 792: &logthis(
1.115 albertel 793: "<font color=yellow>INFO: Connection, $clientip ($hostid{$clientip})</font>"
1.51 www 794: );
1.57 www 795: &status("Connecting $clientip ($hostid{$clientip})");
1.2 www 796: my $clientok;
1.1 albertel 797: if ($clientrec) {
1.57 www 798: &status("Waiting for init from $clientip ($hostid{$clientip})");
1.2 www 799: my $remotereq=<$client>;
1.115 albertel 800: $remotereq=~s/[^\w:]//g;
801: if ($remotereq =~ /^init/) {
1.121 albertel 802: &sethost("sethost:$perlvar{'lonHostID'}");
1.2 www 803: my $challenge="$$".time;
804: print $client "$challenge\n";
1.57 www 805: &status(
806: "Waiting for challenge reply from $clientip ($hostid{$clientip})");
1.2 www 807: $remotereq=<$client>;
808: $remotereq=~s/\W//g;
809: if ($challenge eq $remotereq) {
810: $clientok=1;
811: print $client "ok\n";
812: } else {
1.9 www 813: &logthis(
814: "<font color=blue>WARNING: $clientip did not reply challenge</font>");
1.57 www 815: &status('No challenge reply '.$clientip);
1.2 www 816: }
817: } else {
1.9 www 818: &logthis(
819: "<font color=blue>WARNING: "
820: ."$clientip failed to initialize: >$remotereq< </font>");
1.57 www 821: &status('No init '.$clientip);
1.2 www 822: }
823: } else {
1.9 www 824: &logthis(
825: "<font color=blue>WARNING: Unknown client $clientip</font>");
1.57 www 826: &status('Hung up on '.$clientip);
1.2 www 827: }
828: if ($clientok) {
1.1 albertel 829: # ---------------- New known client connecting, could mean machine online again
1.75 foxr 830:
1.115 albertel 831: foreach my $id (keys(%hostip)) {
832: if ($hostip{$id} ne $clientip ||
833: $hostip{$currenthostid} eq $clientip) {
834: # no need to try to do recon's to myself
835: next;
836: }
837: &reconlonc("$perlvar{'lonSockDir'}/$id");
838: }
839: &logthis("<font color=green>Established connection: $hostid{$clientip}</font>");
1.58 www 840: &status('Will listen to '.$hostid{$clientip});
1.1 albertel 841: # ------------------------------------------------------------ Process requests
842: while (my $userinput=<$client>) {
843: chomp($userinput);
1.79 foxr 844: Debug("Request = $userinput\n");
1.57 www 845: &status('Processing '.$hostid{$clientip}.': '.$userinput);
1.1 albertel 846: my $wasenc=0;
1.63 www 847: alarm(120);
1.1 albertel 848: # ------------------------------------------------------------ See if encrypted
849: if ($userinput =~ /^enc/) {
850: if ($cipher) {
851: my ($cmd,$cmdlength,$encinput)=split(/:/,$userinput);
852: $userinput='';
853: for (my $encidx=0;$encidx<length($encinput);$encidx+=16) {
854: $userinput.=
855: $cipher->decrypt(
856: pack("H16",substr($encinput,$encidx,16))
857: );
858: }
859: $userinput=substr($userinput,0,$cmdlength);
860: $wasenc=1;
861: }
1.75 foxr 862: }
863:
1.1 albertel 864: # ------------------------------------------------------------- Normal commands
865: # ------------------------------------------------------------------------ ping
866: if ($userinput =~ /^ping/) {
1.115 albertel 867: print $client "$currenthostid\n";
1.1 albertel 868: # ------------------------------------------------------------------------ pong
1.137 foxr 869: }elsif ($userinput =~ /^pong/) {
1.134 albertel 870: my $reply=&reply("ping",$hostid{$clientip});
1.115 albertel 871: print $client "$currenthostid:$reply\n";
1.1 albertel 872: # ------------------------------------------------------------------------ ekey
873: } elsif ($userinput =~ /^ekey/) {
874: my $buildkey=time.$$.int(rand 100000);
875: $buildkey=~tr/1-6/A-F/;
876: $buildkey=int(rand 100000).$buildkey.int(rand 100000);
1.115 albertel 877: my $key=$currenthostid.$hostid{$clientip};
1.1 albertel 878: $key=~tr/a-z/A-Z/;
879: $key=~tr/G-P/0-9/;
880: $key=~tr/Q-Z/0-9/;
881: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
882: $key=substr($key,0,32);
883: my $cipherkey=pack("H32",$key);
884: $cipher=new IDEA $cipherkey;
885: print $client "$buildkey\n";
886: # ------------------------------------------------------------------------ load
887: } elsif ($userinput =~ /^load/) {
888: my $loadavg;
889: {
890: my $loadfile=IO::File->new('/proc/loadavg');
891: $loadavg=<$loadfile>;
892: }
893: $loadavg =~ s/\s.*//g;
1.127 albertel 894: my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
1.1 albertel 895: print $client "$loadpercent\n";
1.127 albertel 896: # -------------------------------------------------------------------- userload
897: } elsif ($userinput =~ /^userload/) {
898: my $userloadpercent=&userload();
899: print $client "$userloadpercent\n";
1.137 foxr 900:
901: #
902: # Transactions requiring encryption:
903: #
1.54 harris41 904: # ----------------------------------------------------------------- currentauth
905: } elsif ($userinput =~ /^currentauth/) {
906: if ($wasenc==1) {
907: my ($cmd,$udom,$uname)=split(/:/,$userinput);
1.79 foxr 908: my $result = GetAuthType($udom, $uname);
1.78 foxr 909: if($result eq "nouser") {
910: print $client "unknown_user\n";
911: }
912: else {
913: print $client "$result\n"
914: }
1.54 harris41 915: } else {
916: print $client "refused\n";
917: }
1.137 foxr 918: #--------------------------------------------------------------------- pushfile
919: } elsif($userinput =~ /^pushfile/) {
1.140 foxr 920: if($wasenc == 1) {
921: my $cert = GetCertificate($userinput);
922: if(ValidManager($cert)) {
1.141 foxr 923: my $reply = PushFile($userinput);
924: print $client "$reply\n";
1.140 foxr 925: } else {
926: print $client "refused\n";
927: }
928: } else {
929: print $client "refused\n";
930: }
1.137 foxr 931: #--------------------------------------------------------------------- reinit
932: } elsif($userinput =~ /^reinit/) {
1.140 foxr 933: if ($wasenc == 1) {
934: my $cert = GetCertificate($userinput);
935: if(ValidManager($cert)) {
936: print $client "ok\n";
937: } else {
938: print $client "refused\n";
939: }
940: } else {
941: print $client "refused\n";
942: }
1.1 albertel 943: # ------------------------------------------------------------------------ auth
944: } elsif ($userinput =~ /^auth/) {
945: if ($wasenc==1) {
946: my ($cmd,$udom,$uname,$upass)=split(/:/,$userinput);
947: chomp($upass);
1.11 www 948: $upass=unescape($upass);
1.1 albertel 949: my $proname=propath($udom,$uname);
950: my $passfilename="$proname/passwd";
951: if (-e $passfilename) {
952: my $pf = IO::File->new($passfilename);
953: my $realpasswd=<$pf>;
954: chomp($realpasswd);
1.2 www 955: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
956: my $pwdcorrect=0;
957: if ($howpwd eq 'internal') {
1.99 foxr 958: &Debug("Internal auth");
1.2 www 959: $pwdcorrect=
960: (crypt($upass,$contentpwd) eq $contentpwd);
961: } elsif ($howpwd eq 'unix') {
1.99 foxr 962: &Debug("Unix auth");
963: if((getpwnam($uname))[1] eq "") { #no such user!
964: $pwdcorrect = 0;
965: } else {
966: $contentpwd=(getpwnam($uname))[1];
967: my $pwauth_path="/usr/local/sbin/pwauth";
968: unless ($contentpwd eq 'x') {
969: $pwdcorrect=
970: (crypt($upass,$contentpwd) eq
971: $contentpwd);
972: }
973:
1.52 harris41 974: elsif (-e $pwauth_path) {
975: open PWAUTH, "|$pwauth_path" or
976: die "Cannot invoke authentication";
977: print PWAUTH "$uname\n$upass\n";
978: close PWAUTH;
979: $pwdcorrect=!$?;
1.99 foxr 980: }
1.52 harris41 981: }
1.3 www 982: } elsif ($howpwd eq 'krb4') {
1.134 albertel 983: my $null=pack("C",0);
1.104 matthew 984: unless ($upass=~/$null/) {
985: my $krb4_error = &Authen::Krb4::get_pw_in_tkt
986: ($uname,"",$contentpwd,'krbtgt',
987: $contentpwd,1,$upass);
988: if (!$krb4_error) {
989: $pwdcorrect = 1;
990: } else {
991: $pwdcorrect=0;
992: # log error if it is not a bad password
993: if ($krb4_error != 62) {
994: &logthis('krb4:'.$uname.','.$contentpwd.','.
995: &Authen::Krb4::get_err_txt($Authen::Krb4::error));
996: }
997: }
998: }
1.91 albertel 999: } elsif ($howpwd eq 'krb5') {
1.134 albertel 1000: my $null=pack("C",0);
1.91 albertel 1001: unless ($upass=~/$null/) {
1002: my $krbclient=&Authen::Krb5::parse_name($uname.'@'.$contentpwd);
1003: my $krbservice="krbtgt/".$contentpwd."\@".$contentpwd;
1004: my $krbserver=&Authen::Krb5::parse_name($krbservice);
1005: my $credentials=&Authen::Krb5::cc_default();
1006: $credentials->initialize($krbclient);
1007: my $krbreturn =
1008: &Authen::Krb5::get_in_tkt_with_password(
1009: $krbclient,$krbserver,$upass,$credentials);
1.92 albertel 1010: # unless ($krbreturn) {
1011: # &logthis("Krb5 Error: ".
1012: # &Authen::Krb5::error());
1013: # }
1.91 albertel 1014: $pwdcorrect = ($krbreturn == 1);
1015: } else { $pwdcorrect=0; }
1.50 albertel 1016: } elsif ($howpwd eq 'localauth') {
1.49 albertel 1017: $pwdcorrect=&localauth::localauth($uname,$upass,
1018: $contentpwd);
1019: }
1.2 www 1020: if ($pwdcorrect) {
1.1 albertel 1021: print $client "authorized\n";
1022: } else {
1023: print $client "non_authorized\n";
1024: }
1025: } else {
1026: print $client "unknown_user\n";
1027: }
1028: } else {
1029: print $client "refused\n";
1030: }
1031: # ---------------------------------------------------------------------- passwd
1032: } elsif ($userinput =~ /^passwd/) {
1033: if ($wasenc==1) {
1034: my
1035: ($cmd,$udom,$uname,$upass,$npass)=split(/:/,$userinput);
1036: chomp($npass);
1.32 www 1037: $upass=&unescape($upass);
1038: $npass=&unescape($npass);
1.98 foxr 1039: &Debug("Trying to change password for $uname");
1.72 matthew 1040: my $proname=propath($udom,$uname);
1.1 albertel 1041: my $passfilename="$proname/passwd";
1042: if (-e $passfilename) {
1043: my $realpasswd;
1044: { my $pf = IO::File->new($passfilename);
1045: $realpasswd=<$pf>; }
1046: chomp($realpasswd);
1.2 www 1047: my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
1048: if ($howpwd eq 'internal') {
1.98 foxr 1049: &Debug("internal auth");
1.2 www 1050: if (crypt($upass,$contentpwd) eq $contentpwd) {
1051: my $salt=time;
1052: $salt=substr($salt,6,2);
1053: my $ncpass=crypt($npass,$salt);
1.139 albertel 1054: {
1055: my $pf;
1056: if ($pf = IO::File->new(">$passfilename")) {
1057: print $pf "internal:$ncpass\n";
1058: &logthis("Result of password change for $uname: pwchange_success");
1059: print $client "ok\n";
1060: } else {
1061: &logthis("Unable to open $uname passwd to change password");
1062: print $client "non_authorized\n";
1063: }
1064: }
1065:
1.2 www 1066: } else {
1067: print $client "non_authorized\n";
1068: }
1.72 matthew 1069: } elsif ($howpwd eq 'unix') {
1070: # Unix means we have to access /etc/password
1071: # one way or another.
1072: # First: Make sure the current password is
1073: # correct
1.98 foxr 1074: &Debug("auth is unix");
1.72 matthew 1075: $contentpwd=(getpwnam($uname))[1];
1076: my $pwdcorrect = "0";
1077: my $pwauth_path="/usr/local/sbin/pwauth";
1078: unless ($contentpwd eq 'x') {
1079: $pwdcorrect=
1080: (crypt($upass,$contentpwd) eq $contentpwd);
1081: } elsif (-e $pwauth_path) {
1082: open PWAUTH, "|$pwauth_path" or
1083: die "Cannot invoke authentication";
1084: print PWAUTH "$uname\n$upass\n";
1085: close PWAUTH;
1.98 foxr 1086: &Debug("exited pwauth with $? ($uname,$upass) ");
1087: $pwdcorrect=($? == 0);
1.72 matthew 1088: }
1089: if ($pwdcorrect) {
1090: my $execdir=$perlvar{'lonDaemons'};
1.98 foxr 1091: &Debug("Opening lcpasswd pipeline");
1.132 matthew 1092: my $pf = IO::File->new("|$execdir/lcpasswd > $perlvar{'lonDaemons'}/logs/lcpasswd.log");
1.72 matthew 1093: print $pf "$uname\n$npass\n$npass\n";
1094: close $pf;
1.97 foxr 1095: my $err = $?;
1096: my $result = ($err>0 ? 'pwchange_failure'
1.72 matthew 1097: : 'ok');
1.96 foxr 1098: &logthis("Result of password change for $uname: ".
1099: &lcpasswdstrerror($?));
1.72 matthew 1100: print $client "$result\n";
1101: } else {
1102: print $client "non_authorized\n";
1103: }
1104: } else {
1.2 www 1105: print $client "auth_mode_error\n";
1.1 albertel 1106: }
1107: } else {
1108: print $client "unknown_user\n";
1.31 www 1109: }
1110: } else {
1111: print $client "refused\n";
1112: }
1113: # -------------------------------------------------------------------- makeuser
1114: } elsif ($userinput =~ /^makeuser/) {
1.91 albertel 1115: &Debug("Make user received");
1.56 harris41 1116: my $oldumask=umask(0077);
1.31 www 1117: if ($wasenc==1) {
1118: my
1119: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
1.77 foxr 1120: &Debug("cmd =".$cmd." $udom =".$udom.
1121: " uname=".$uname);
1.31 www 1122: chomp($npass);
1.32 www 1123: $npass=&unescape($npass);
1.31 www 1124: my $proname=propath($udom,$uname);
1125: my $passfilename="$proname/passwd";
1.77 foxr 1126: &Debug("Password file created will be:".
1127: $passfilename);
1.31 www 1128: if (-e $passfilename) {
1129: print $client "already_exists\n";
1.115 albertel 1130: } elsif ($udom ne $currentdomainid) {
1.31 www 1131: print $client "not_right_domain\n";
1132: } else {
1.134 albertel 1133: my @fpparts=split(/\//,$proname);
1134: my $fpnow=$fpparts[0].'/'.$fpparts[1].'/'.$fpparts[2];
1135: my $fperror='';
1136: for (my $i=3;$i<=$#fpparts;$i++) {
1.31 www 1137: $fpnow.='/'.$fpparts[$i];
1138: unless (-e $fpnow) {
1139: unless (mkdir($fpnow,0777)) {
1.109 foxr 1140: $fperror="error: ".($!+0)
1.111 matthew 1141: ." mkdir failed while attempting "
1142: ."makeuser\n";
1.31 www 1143: }
1144: }
1145: }
1146: unless ($fperror) {
1.98 foxr 1147: my $result=&make_passwd_file($uname, $umode,$npass,
1.91 albertel 1148: $passfilename);
1149: print $client $result;
1.31 www 1150: } else {
1151: print $client "$fperror\n";
1152: }
1.55 harris41 1153: }
1154: } else {
1155: print $client "refused\n";
1156: }
1.56 harris41 1157: umask($oldumask);
1.55 harris41 1158: # -------------------------------------------------------------- changeuserauth
1159: } elsif ($userinput =~ /^changeuserauth/) {
1.77 foxr 1160: &Debug("Changing authorization");
1161: if ($wasenc==1) {
1.55 harris41 1162: my
1.91 albertel 1163: ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
1.55 harris41 1164: chomp($npass);
1.77 foxr 1165: &Debug("cmd = ".$cmd." domain= ".$udom.
1166: "uname =".$uname." umode= ".$umode);
1.55 harris41 1167: $npass=&unescape($npass);
1.91 albertel 1168: my $proname=&propath($udom,$uname);
1.55 harris41 1169: my $passfilename="$proname/passwd";
1.115 albertel 1170: if ($udom ne $currentdomainid) {
1.55 harris41 1171: print $client "not_right_domain\n";
1172: } else {
1.98 foxr 1173: my $result=&make_passwd_file($uname, $umode,$npass,
1.93 albertel 1174: $passfilename);
1.91 albertel 1175: print $client $result;
1.1 albertel 1176: }
1177: } else {
1178: print $client "refused\n";
1179: }
1180: # ------------------------------------------------------------------------ home
1181: } elsif ($userinput =~ /^home/) {
1182: my ($cmd,$udom,$uname)=split(/:/,$userinput);
1183: chomp($uname);
1184: my $proname=propath($udom,$uname);
1185: if (-e $proname) {
1186: print $client "found\n";
1187: } else {
1188: print $client "not_found\n";
1189: }
1190: # ---------------------------------------------------------------------- update
1191: } elsif ($userinput =~ /^update/) {
1192: my ($cmd,$fname)=split(/:/,$userinput);
1193: my $ownership=ishome($fname);
1194: if ($ownership eq 'not_owner') {
1195: if (-e $fname) {
1196: my ($dev,$ino,$mode,$nlink,
1197: $uid,$gid,$rdev,$size,
1198: $atime,$mtime,$ctime,
1199: $blksize,$blocks)=stat($fname);
1.134 albertel 1200: my $now=time;
1201: my $since=$now-$atime;
1.1 albertel 1202: if ($since>$perlvar{'lonExpire'}) {
1.134 albertel 1203: my $reply=
1204: &reply("unsub:$fname","$hostid{$clientip}");
1.1 albertel 1205: unlink("$fname");
1206: } else {
1207: my $transname="$fname.in.transfer";
1208: my $remoteurl=
1209: reply("sub:$fname","$hostid{$clientip}");
1210: my $response;
1211: {
1212: my $ua=new LWP::UserAgent;
1213: my $request=new HTTP::Request('GET',"$remoteurl");
1214: $response=$ua->request($request,$transname);
1215: }
1216: if ($response->is_error()) {
1.24 albertel 1217: unlink($transname);
1.1 albertel 1218: my $message=$response->status_line;
1219: &logthis(
1220: "LWP GET: $message for $fname ($remoteurl)");
1221: } else {
1.14 www 1222: if ($remoteurl!~/\.meta$/) {
1.28 www 1223: my $ua=new LWP::UserAgent;
1.14 www 1224: my $mrequest=
1225: new HTTP::Request('GET',$remoteurl.'.meta');
1226: my $mresponse=
1227: $ua->request($mrequest,$fname.'.meta');
1228: if ($mresponse->is_error()) {
1229: unlink($fname.'.meta');
1230: }
1231: }
1.1 albertel 1232: rename($transname,$fname);
1233: }
1234: }
1235: print $client "ok\n";
1236: } else {
1237: print $client "not_found\n";
1238: }
1239: } else {
1240: print $client "rejected\n";
1241: }
1.85 www 1242: # -------------------------------------- fetch a user file from a remote server
1243: } elsif ($userinput =~ /^fetchuserfile/) {
1.86 www 1244: my ($cmd,$fname)=split(/:/,$userinput);
1245: my ($udom,$uname,$ufile)=split(/\//,$fname);
1246: my $udir=propath($udom,$uname).'/userfiles';
1.88 albertel 1247: unless (-e $udir) { mkdir($udir,0770); }
1.86 www 1248: if (-e $udir) {
1249: $ufile=~s/^[\.\~]+//;
1250: $ufile=~s/\///g;
1251: my $transname=$udir.'/'.$ufile;
1252: my $remoteurl='http://'.$clientip.'/userfiles/'.$fname;
1253: my $response;
1254: {
1255: my $ua=new LWP::UserAgent;
1256: my $request=new HTTP::Request('GET',"$remoteurl");
1257: $response=$ua->request($request,$transname);
1258: }
1259: if ($response->is_error()) {
1260: unlink($transname);
1261: my $message=$response->status_line;
1262: &logthis(
1263: "LWP GET: $message for $fname ($remoteurl)");
1264: print $client "failed\n";
1265: } else {
1266: print $client "ok\n";
1267: }
1268: } else {
1269: print $client "not_home\n";
1270: }
1.85 www 1271: # ------------------------------------------ authenticate access to a user file
1.86 www 1272: } elsif ($userinput =~ /^tokenauthuserfile/) {
1.85 www 1273: my ($cmd,$fname,$session)=split(/:/,$userinput);
1.86 www 1274: chomp($session);
1.134 albertel 1275: my $reply='non_auth';
1.86 www 1276: if (open(ENVIN,$perlvar{'lonIDsDir'}.'/'.
1.134 albertel 1277: $session.'.id')) {
1278: while (my $line=<ENVIN>) {
1279: if ($line=~/userfile\.$fname\=/) { $reply='ok'; }
1280: }
1281: close(ENVIN);
1282: print $client $reply."\n";
1.86 www 1283: } else {
1.134 albertel 1284: print $client "invalid_token\n";
1.86 www 1285: }
1.1 albertel 1286: # ----------------------------------------------------------------- unsubscribe
1287: } elsif ($userinput =~ /^unsub/) {
1288: my ($cmd,$fname)=split(/:/,$userinput);
1289: if (-e $fname) {
1.84 albertel 1290: print $client &unsub($client,$fname,$clientip);
1.1 albertel 1291: } else {
1292: print $client "not_found\n";
1293: }
1294: # ------------------------------------------------------------------- subscribe
1295: } elsif ($userinput =~ /^sub/) {
1.84 albertel 1296: print $client &subscribe($userinput,$clientip);
1.102 www 1297: # ------------------------------------------------------------- current version
1298: } elsif ($userinput =~ /^currentversion/) {
1299: my ($cmd,$fname)=split(/:/,$userinput);
1300: print $client ¤tversion($fname)."\n";
1.12 harris41 1301: # ------------------------------------------------------------------------- log
1302: } elsif ($userinput =~ /^log/) {
1303: my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
1304: chomp($what);
1305: my $proname=propath($udom,$uname);
1306: my $now=time;
1307: {
1308: my $hfh;
1309: if ($hfh=IO::File->new(">>$proname/activity.log")) {
1310: print $hfh "$now:$hostid{$clientip}:$what\n";
1311: print $client "ok\n";
1312: } else {
1.109 foxr 1313: print $client "error: ".($!+0)
1.111 matthew 1314: ." IO::File->new Failed "
1315: ."while attempting log\n";
1.12 harris41 1316: }
1317: }
1.1 albertel 1318: # ------------------------------------------------------------------------- put
1319: } elsif ($userinput =~ /^put/) {
1.6 www 1320: my ($cmd,$udom,$uname,$namespace,$what)
1.1 albertel 1321: =split(/:/,$userinput);
1.8 www 1322: $namespace=~s/\//\_/g;
1.6 www 1323: $namespace=~s/\W//g;
1324: if ($namespace ne 'roles') {
1.1 albertel 1325: chomp($what);
1326: my $proname=propath($udom,$uname);
1327: my $now=time;
1.48 www 1328: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1329: my $hfh;
1330: if (
1331: $hfh=IO::File->new(">>$proname/$namespace.hist")
1332: ) { print $hfh "P:$now:$what\n"; }
1333: }
1334: my @pairs=split(/\&/,$what);
1.134 albertel 1335: my %hash;
1336: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT(),0640)) {
1337: foreach my $pair (@pairs) {
1338: my ($key,$value)=split(/=/,$pair);
1.1 albertel 1339: $hash{$key}=$value;
1340: }
1.4 www 1341: if (untie(%hash)) {
1.1 albertel 1342: print $client "ok\n";
1343: } else {
1.109 foxr 1344: print $client "error: ".($!+0)
1.111 matthew 1345: ." untie(GDBM) failed ".
1346: "while attempting put\n";
1.1 albertel 1347: }
1348: } else {
1.109 foxr 1349: print $client "error: ".($!)
1.111 matthew 1350: ." tie(GDBM) Failed ".
1351: "while attempting put\n";
1.1 albertel 1352: }
1.6 www 1353: } else {
1354: print $client "refused\n";
1355: }
1356: # -------------------------------------------------------------------- rolesput
1357: } elsif ($userinput =~ /^rolesput/) {
1.77 foxr 1358: &Debug("rolesput");
1.6 www 1359: if ($wasenc==1) {
1360: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
1361: =split(/:/,$userinput);
1.77 foxr 1362: &Debug("cmd = ".$cmd." exedom= ".$exedom.
1363: "user = ".$exeuser." udom=".$udom.
1364: "what = ".$what);
1.6 www 1365: my $namespace='roles';
1366: chomp($what);
1367: my $proname=propath($udom,$uname);
1368: my $now=time;
1369: {
1370: my $hfh;
1371: if (
1372: $hfh=IO::File->new(">>$proname/$namespace.hist")
1373: ) {
1374: print $hfh "P:$now:$exedom:$exeuser:$what\n";
1375: }
1376: }
1377: my @pairs=split(/\&/,$what);
1.134 albertel 1378: my %hash;
1379: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT(),0640)) {
1380: foreach my $pair (@pairs) {
1381: my ($key,$value)=split(/=/,$pair);
1.78 foxr 1382: &ManagePermissions($key, $udom, $uname,
1383: &GetAuthType( $udom,
1384: $uname));
1.6 www 1385: $hash{$key}=$value;
1386: }
1387: if (untie(%hash)) {
1388: print $client "ok\n";
1389: } else {
1.109 foxr 1390: print $client "error: ".($!+0)
1.111 matthew 1391: ." untie(GDBM) Failed ".
1392: "while attempting rolesput\n";
1.6 www 1393: }
1394: } else {
1.109 foxr 1395: print $client "error: ".($!+0)
1.111 matthew 1396: ." tie(GDBM) Failed ".
1397: "while attempting rolesput\n";
1.117 www 1398: }
1399: } else {
1400: print $client "refused\n";
1401: }
1402: # -------------------------------------------------------------------- rolesdel
1403: } elsif ($userinput =~ /^rolesdel/) {
1404: &Debug("rolesdel");
1405: if ($wasenc==1) {
1406: my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
1407: =split(/:/,$userinput);
1408: &Debug("cmd = ".$cmd." exedom= ".$exedom.
1409: "user = ".$exeuser." udom=".$udom.
1410: "what = ".$what);
1411: my $namespace='roles';
1412: chomp($what);
1413: my $proname=propath($udom,$uname);
1414: my $now=time;
1415: {
1416: my $hfh;
1417: if (
1418: $hfh=IO::File->new(">>$proname/$namespace.hist")
1419: ) {
1420: print $hfh "D:$now:$exedom:$exeuser:$what\n";
1421: }
1422: }
1423: my @rolekeys=split(/\&/,$what);
1.134 albertel 1424: my %hash;
1425: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT(),0640)) {
1426: foreach my $key (@rolekeys) {
1.117 www 1427: delete $hash{$key};
1428: }
1429: if (untie(%hash)) {
1430: print $client "ok\n";
1431: } else {
1432: print $client "error: ".($!+0)
1433: ." untie(GDBM) Failed ".
1434: "while attempting rolesdel\n";
1435: }
1436: } else {
1437: print $client "error: ".($!+0)
1438: ." tie(GDBM) Failed ".
1439: "while attempting rolesdel\n";
1.6 www 1440: }
1441: } else {
1442: print $client "refused\n";
1443: }
1.1 albertel 1444: # ------------------------------------------------------------------------- get
1445: } elsif ($userinput =~ /^get/) {
1446: my ($cmd,$udom,$uname,$namespace,$what)
1447: =split(/:/,$userinput);
1.8 www 1448: $namespace=~s/\//\_/g;
1.1 albertel 1449: $namespace=~s/\W//g;
1450: chomp($what);
1451: my @queries=split(/\&/,$what);
1452: my $proname=propath($udom,$uname);
1453: my $qresult='';
1.134 albertel 1454: my %hash;
1455: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER(),0640)) {
1456: for (my $i=0;$i<=$#queries;$i++) {
1.1 albertel 1457: $qresult.="$hash{$queries[$i]}&";
1458: }
1.4 www 1459: if (untie(%hash)) {
1.1 albertel 1460: $qresult=~s/\&$//;
1461: print $client "$qresult\n";
1462: } else {
1.109 foxr 1463: print $client "error: ".($!+0)
1.111 matthew 1464: ." untie(GDBM) Failed ".
1465: "while attempting get\n";
1.1 albertel 1466: }
1467: } else {
1.112 matthew 1468: if ($!+0 == 2) {
1469: print $client "error:No such file or ".
1470: "GDBM reported bad block error\n";
1471: } else {
1472: print $client "error: ".($!+0)
1473: ." tie(GDBM) Failed ".
1474: "while attempting get\n";
1475: }
1.1 albertel 1476: }
1477: # ------------------------------------------------------------------------ eget
1478: } elsif ($userinput =~ /^eget/) {
1479: my ($cmd,$udom,$uname,$namespace,$what)
1480: =split(/:/,$userinput);
1.8 www 1481: $namespace=~s/\//\_/g;
1.1 albertel 1482: $namespace=~s/\W//g;
1483: chomp($what);
1484: my @queries=split(/\&/,$what);
1485: my $proname=propath($udom,$uname);
1486: my $qresult='';
1.134 albertel 1487: my %hash;
1488: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER(),0640)) {
1489: for (my $i=0;$i<=$#queries;$i++) {
1.1 albertel 1490: $qresult.="$hash{$queries[$i]}&";
1491: }
1.4 www 1492: if (untie(%hash)) {
1.1 albertel 1493: $qresult=~s/\&$//;
1494: if ($cipher) {
1495: my $cmdlength=length($qresult);
1496: $qresult.=" ";
1497: my $encqresult='';
1498: for
1499: (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
1500: $encqresult.=
1501: unpack("H16",
1502: $cipher->encrypt(substr($qresult,$encidx,8)));
1503: }
1504: print $client "enc:$cmdlength:$encqresult\n";
1505: } else {
1506: print $client "error:no_key\n";
1507: }
1508: } else {
1.109 foxr 1509: print $client "error: ".($!+0)
1.111 matthew 1510: ." untie(GDBM) Failed ".
1511: "while attempting eget\n";
1.1 albertel 1512: }
1513: } else {
1.109 foxr 1514: print $client "error: ".($!+0)
1.111 matthew 1515: ." tie(GDBM) Failed ".
1516: "while attempting eget\n";
1.1 albertel 1517: }
1518: # ------------------------------------------------------------------------- del
1519: } elsif ($userinput =~ /^del/) {
1520: my ($cmd,$udom,$uname,$namespace,$what)
1521: =split(/:/,$userinput);
1.8 www 1522: $namespace=~s/\//\_/g;
1.1 albertel 1523: $namespace=~s/\W//g;
1524: chomp($what);
1525: my $proname=propath($udom,$uname);
1526: my $now=time;
1.48 www 1527: unless ($namespace=~/^nohist\_/) {
1.1 albertel 1528: my $hfh;
1529: if (
1530: $hfh=IO::File->new(">>$proname/$namespace.hist")
1531: ) { print $hfh "D:$now:$what\n"; }
1532: }
1533: my @keys=split(/\&/,$what);
1.134 albertel 1534: my %hash;
1535: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT(),0640)) {
1536: foreach my $key (@keys) {
1.1 albertel 1537: delete($hash{$key});
1538: }
1.4 www 1539: if (untie(%hash)) {
1.1 albertel 1540: print $client "ok\n";
1541: } else {
1.109 foxr 1542: print $client "error: ".($!+0)
1.111 matthew 1543: ." untie(GDBM) Failed ".
1544: "while attempting del\n";
1.1 albertel 1545: }
1546: } else {
1.109 foxr 1547: print $client "error: ".($!+0)
1.111 matthew 1548: ." tie(GDBM) Failed ".
1549: "while attempting del\n";
1.1 albertel 1550: }
1551: # ------------------------------------------------------------------------ keys
1552: } elsif ($userinput =~ /^keys/) {
1553: my ($cmd,$udom,$uname,$namespace)
1554: =split(/:/,$userinput);
1.8 www 1555: $namespace=~s/\//\_/g;
1.1 albertel 1556: $namespace=~s/\W//g;
1557: my $proname=propath($udom,$uname);
1558: my $qresult='';
1.134 albertel 1559: my %hash;
1560: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER(),0640)) {
1561: foreach my $key (keys %hash) {
1.1 albertel 1562: $qresult.="$key&";
1563: }
1.4 www 1564: if (untie(%hash)) {
1.1 albertel 1565: $qresult=~s/\&$//;
1566: print $client "$qresult\n";
1567: } else {
1.109 foxr 1568: print $client "error: ".($!+0)
1.111 matthew 1569: ." untie(GDBM) Failed ".
1570: "while attempting keys\n";
1.105 matthew 1571: }
1572: } else {
1.109 foxr 1573: print $client "error: ".($!+0)
1.111 matthew 1574: ." tie(GDBM) Failed ".
1575: "while attempting keys\n";
1.105 matthew 1576: }
1577: # ----------------------------------------------------------------- dumpcurrent
1.107 matthew 1578: } elsif ($userinput =~ /^currentdump/) {
1.105 matthew 1579: my ($cmd,$udom,$uname,$namespace)
1580: =split(/:/,$userinput);
1581: $namespace=~s/\//\_/g;
1582: $namespace=~s/\W//g;
1583: my $qresult='';
1584: my $proname=propath($udom,$uname);
1.134 albertel 1585: my %hash;
1.105 matthew 1586: if (tie(%hash,'GDBM_File',
1587: "$proname/$namespace.db",
1588: &GDBM_READER(),0640)) {
1589: # Structure of %data:
1590: # $data{$symb}->{$parameter}=$value;
1591: # $data{$symb}->{'v.'.$parameter}=$version;
1592: # since $parameter will be unescaped, we do not
1593: # have to worry about silly parameter names...
1594: my %data = ();
1595: while (my ($key,$value) = each(%hash)) {
1596: my ($v,$symb,$param) = split(/:/,$key);
1597: next if ($v eq 'version' || $symb eq 'keys');
1598: next if (exists($data{$symb}) &&
1599: exists($data{$symb}->{$param}) &&
1600: $data{$symb}->{'v.'.$param} > $v);
1601: $data{$symb}->{$param}=$value;
1.107 matthew 1602: $data{$symb}->{'v.'.$param}=$v;
1.105 matthew 1603: }
1604: if (untie(%hash)) {
1605: while (my ($symb,$param_hash) = each(%data)) {
1606: while(my ($param,$value) = each (%$param_hash)){
1607: next if ($param =~ /^v\./);
1608: $qresult.=$symb.':'.$param.'='.$value.'&';
1609: }
1610: }
1611: chop($qresult);
1612: print $client "$qresult\n";
1613: } else {
1.109 foxr 1614: print $client "error: ".($!+0)
1.111 matthew 1615: ." untie(GDBM) Failed ".
1616: "while attempting currentdump\n";
1.1 albertel 1617: }
1618: } else {
1.109 foxr 1619: print $client "error: ".($!+0)
1.111 matthew 1620: ." tie(GDBM) Failed ".
1621: "while attempting currentdump\n";
1.1 albertel 1622: }
1623: # ------------------------------------------------------------------------ dump
1624: } elsif ($userinput =~ /^dump/) {
1.62 www 1625: my ($cmd,$udom,$uname,$namespace,$regexp)
1.1 albertel 1626: =split(/:/,$userinput);
1.8 www 1627: $namespace=~s/\//\_/g;
1.1 albertel 1628: $namespace=~s/\W//g;
1.62 www 1629: if (defined($regexp)) {
1630: $regexp=&unescape($regexp);
1631: } else {
1632: $regexp='.';
1633: }
1.100 matthew 1634: my $qresult='';
1.1 albertel 1635: my $proname=propath($udom,$uname);
1.134 albertel 1636: my %hash;
1637: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER(),0640)) {
1.90 stredwic 1638: study($regexp);
1.134 albertel 1639: while (my ($key,$value) = each(%hash)) {
1.100 matthew 1640: if ($regexp eq '.') {
1641: $qresult.=$key.'='.$value.'&';
1642: } else {
1643: my $unescapeKey = &unescape($key);
1644: if (eval('$unescapeKey=~/$regexp/')) {
1645: $qresult.="$key=$value&";
1646: }
1647: }
1.7 www 1648: }
1.100 matthew 1649: if (untie(%hash)) {
1650: chop($qresult);
1651: print $client "$qresult\n";
1.7 www 1652: } else {
1.109 foxr 1653: print $client "error: ".($!+0)
1.111 matthew 1654: ." untie(GDBM) Failed ".
1655: "while attempting dump\n";
1.7 www 1656: }
1657: } else {
1.109 foxr 1658: print $client "error: ".($!+0)
1.111 matthew 1659: ." tie(GDBM) Failed ".
1660: "while attempting dump\n";
1.7 www 1661: }
1662: # ----------------------------------------------------------------------- store
1663: } elsif ($userinput =~ /^store/) {
1664: my ($cmd,$udom,$uname,$namespace,$rid,$what)
1665: =split(/:/,$userinput);
1.8 www 1666: $namespace=~s/\//\_/g;
1.7 www 1667: $namespace=~s/\W//g;
1668: if ($namespace ne 'roles') {
1669: chomp($what);
1670: my $proname=propath($udom,$uname);
1671: my $now=time;
1.48 www 1672: unless ($namespace=~/^nohist\_/) {
1.7 www 1673: my $hfh;
1674: if (
1675: $hfh=IO::File->new(">>$proname/$namespace.hist")
1676: ) { print $hfh "P:$now:$rid:$what\n"; }
1677: }
1678: my @pairs=split(/\&/,$what);
1.134 albertel 1679: my %hash;
1680: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT(),0640)) {
1.7 www 1681: my @previouskeys=split(/&/,$hash{"keys:$rid"});
1682: my $key;
1683: $hash{"version:$rid"}++;
1684: my $version=$hash{"version:$rid"};
1685: my $allkeys='';
1.134 albertel 1686: foreach my $pair (@pairs) {
1687: my ($key,$value)=split(/=/,$pair);
1.7 www 1688: $allkeys.=$key.':';
1689: $hash{"$version:$rid:$key"}=$value;
1690: }
1.36 www 1691: $hash{"$version:$rid:timestamp"}=$now;
1692: $allkeys.='timestamp';
1.7 www 1693: $hash{"$version:keys:$rid"}=$allkeys;
1694: if (untie(%hash)) {
1695: print $client "ok\n";
1696: } else {
1.109 foxr 1697: print $client "error: ".($!+0)
1.111 matthew 1698: ." untie(GDBM) Failed ".
1699: "while attempting store\n";
1.7 www 1700: }
1701: } else {
1.109 foxr 1702: print $client "error: ".($!+0)
1.111 matthew 1703: ." tie(GDBM) Failed ".
1704: "while attempting store\n";
1.7 www 1705: }
1706: } else {
1707: print $client "refused\n";
1708: }
1709: # --------------------------------------------------------------------- restore
1710: } elsif ($userinput =~ /^restore/) {
1711: my ($cmd,$udom,$uname,$namespace,$rid)
1712: =split(/:/,$userinput);
1.8 www 1713: $namespace=~s/\//\_/g;
1.7 www 1714: $namespace=~s/\W//g;
1715: chomp($rid);
1716: my $proname=propath($udom,$uname);
1717: my $qresult='';
1.134 albertel 1718: my %hash;
1719: if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER(),0640)) {
1.7 www 1720: my $version=$hash{"version:$rid"};
1721: $qresult.="version=$version&";
1722: my $scope;
1723: for ($scope=1;$scope<=$version;$scope++) {
1724: my $vkeys=$hash{"$scope:keys:$rid"};
1725: my @keys=split(/:/,$vkeys);
1726: my $key;
1727: $qresult.="$scope:keys=$vkeys&";
1728: foreach $key (@keys) {
1.21 www 1729: $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
1.7 www 1730: }
1.1 albertel 1731: }
1.4 www 1732: if (untie(%hash)) {
1.1 albertel 1733: $qresult=~s/\&$//;
1734: print $client "$qresult\n";
1735: } else {
1.109 foxr 1736: print $client "error: ".($!+0)
1.111 matthew 1737: ." untie(GDBM) Failed ".
1738: "while attempting restore\n";
1.1 albertel 1739: }
1740: } else {
1.109 foxr 1741: print $client "error: ".($!+0)
1.111 matthew 1742: ." tie(GDBM) Failed ".
1743: "while attempting restore\n";
1.1 albertel 1744: }
1.86 www 1745: # -------------------------------------------------------------------- chatsend
1746: } elsif ($userinput =~ /^chatsend/) {
1747: my ($cmd,$cdom,$cnum,$newpost)=split(/\:/,$userinput);
1748: &chatadd($cdom,$cnum,$newpost);
1749: print $client "ok\n";
1750: # -------------------------------------------------------------------- chatretr
1751: } elsif ($userinput =~ /^chatretr/) {
1.122 www 1752: my
1753: ($cmd,$cdom,$cnum,$udom,$uname)=split(/\:/,$userinput);
1.86 www 1754: my $reply='';
1.122 www 1755: foreach (&getchat($cdom,$cnum,$udom,$uname)) {
1.86 www 1756: $reply.=&escape($_).':';
1757: }
1758: $reply=~s/\:$//;
1759: print $client $reply."\n";
1.12 harris41 1760: # ------------------------------------------------------------------- querysend
1761: } elsif ($userinput =~ /^querysend/) {
1.44 harris41 1762: my ($cmd,$query,
1.82 www 1763: $arg1,$arg2,$arg3)=split(/\:/,$userinput);
1.12 harris41 1764: $query=~s/\n*$//g;
1.82 www 1765: print $client "".
1.40 harris41 1766: sqlreply("$hostid{$clientip}\&$query".
1.82 www 1767: "\&$arg1"."\&$arg2"."\&$arg3")."\n";
1.12 harris41 1768: # ------------------------------------------------------------------ queryreply
1769: } elsif ($userinput =~ /^queryreply/) {
1770: my ($cmd,$id,$reply)=split(/:/,$userinput);
1771: my $store;
1.13 www 1772: my $execdir=$perlvar{'lonDaemons'};
1773: if ($store=IO::File->new(">$execdir/tmp/$id")) {
1.43 harris41 1774: $reply=~s/\&/\n/g;
1.12 harris41 1775: print $store $reply;
1776: close $store;
1.46 harris41 1777: my $store2=IO::File->new(">$execdir/tmp/$id.end");
1778: print $store2 "done\n";
1779: close $store2;
1.12 harris41 1780: print $client "ok\n";
1781: }
1782: else {
1.109 foxr 1783: print $client "error: ".($!+0)
1.111 matthew 1784: ." IO::File->new Failed ".
1785: "while attempting queryreply\n";
1.12 harris41 1786: }
1.118 www 1787: # ----------------------------------------------------------------- courseidput
1788: } elsif ($userinput =~ /^courseidput/) {
1789: my ($cmd,$udom,$what)=split(/:/,$userinput);
1790: chomp($what);
1791: $udom=~s/\W//g;
1792: my $proname=
1793: "$perlvar{'lonUsersDir'}/$udom/nohist_courseids";
1794: my $now=time;
1795: my @pairs=split(/\&/,$what);
1.134 albertel 1796: my %hash;
1797: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT(),0640)) {
1798: foreach my $pair (@pairs) {
1799: my ($key,$value)=split(/=/,$pair);
1.118 www 1800: $hash{$key}=$value.':'.$now;
1801: }
1802: if (untie(%hash)) {
1803: print $client "ok\n";
1804: } else {
1805: print $client "error: ".($!+0)
1806: ." untie(GDBM) Failed ".
1807: "while attempting courseidput\n";
1808: }
1809: } else {
1810: print $client "error: ".($!+0)
1811: ." tie(GDBM) Failed ".
1812: "while attempting courseidput\n";
1813: }
1814: # ---------------------------------------------------------------- courseiddump
1815: } elsif ($userinput =~ /^courseiddump/) {
1816: my ($cmd,$udom,$since,$description)
1817: =split(/:/,$userinput);
1818: if (defined($description)) {
1819: $description=&unescape($description);
1820: } else {
1821: $description='.';
1822: }
1823: unless (defined($since)) { $since=0; }
1824: my $qresult='';
1825: my $proname=
1826: "$perlvar{'lonUsersDir'}/$udom/nohist_courseids";
1.134 albertel 1827: my %hash;
1828: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER(),0640)) {
1829: while (my ($key,$value) = each(%hash)) {
1.118 www 1830: my ($descr,$lasttime)=split(/\:/,$value);
1831: if ($lasttime<$since) { next; }
1.133 www 1832: if ($description eq '.') {
1.118 www 1833: $qresult.=$key.'='.$descr.'&';
1834: } else {
1835: my $unescapeVal = &unescape($descr);
1.120 www 1836: if (eval('$unescapeVal=~/$description/i')) {
1.118 www 1837: $qresult.="$key=$descr&";
1838: }
1839: }
1840: }
1841: if (untie(%hash)) {
1842: chop($qresult);
1843: print $client "$qresult\n";
1844: } else {
1845: print $client "error: ".($!+0)
1846: ." untie(GDBM) Failed ".
1847: "while attempting courseiddump\n";
1848: }
1849: } else {
1850: print $client "error: ".($!+0)
1851: ." tie(GDBM) Failed ".
1852: "while attempting courseiddump\n";
1853: }
1.1 albertel 1854: # ----------------------------------------------------------------------- idput
1855: } elsif ($userinput =~ /^idput/) {
1856: my ($cmd,$udom,$what)=split(/:/,$userinput);
1857: chomp($what);
1858: $udom=~s/\W//g;
1859: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1860: my $now=time;
1861: {
1862: my $hfh;
1863: if (
1864: $hfh=IO::File->new(">>$proname.hist")
1865: ) { print $hfh "P:$now:$what\n"; }
1866: }
1867: my @pairs=split(/\&/,$what);
1.134 albertel 1868: my %hash;
1869: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT(),0640)) {
1870: foreach my $pair (@pairs) {
1871: my ($key,$value)=split(/=/,$pair);
1.1 albertel 1872: $hash{$key}=$value;
1873: }
1.4 www 1874: if (untie(%hash)) {
1.1 albertel 1875: print $client "ok\n";
1876: } else {
1.109 foxr 1877: print $client "error: ".($!+0)
1.111 matthew 1878: ." untie(GDBM) Failed ".
1879: "while attempting idput\n";
1.1 albertel 1880: }
1881: } else {
1.109 foxr 1882: print $client "error: ".($!+0)
1.111 matthew 1883: ." tie(GDBM) Failed ".
1884: "while attempting idput\n";
1.1 albertel 1885: }
1886: # ----------------------------------------------------------------------- idget
1887: } elsif ($userinput =~ /^idget/) {
1888: my ($cmd,$udom,$what)=split(/:/,$userinput);
1889: chomp($what);
1890: $udom=~s/\W//g;
1891: my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
1892: my @queries=split(/\&/,$what);
1893: my $qresult='';
1.134 albertel 1894: my %hash;
1895: if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER(),0640)) {
1896: for (my $i=0;$i<=$#queries;$i++) {
1.1 albertel 1897: $qresult.="$hash{$queries[$i]}&";
1898: }
1.4 www 1899: if (untie(%hash)) {
1.134 albertel 1900: $qresult=~s/\&$//;
1901: print $client "$qresult\n";
1.1 albertel 1902: } else {
1.134 albertel 1903: print $client "error: ".($!+0)
1904: ." untie(GDBM) Failed ".
1905: "while attempting idget\n";
1.1 albertel 1906: }
1907: } else {
1.109 foxr 1908: print $client "error: ".($!+0)
1.111 matthew 1909: ." tie(GDBM) Failed ".
1910: "while attempting idget\n";
1.1 albertel 1911: }
1.13 www 1912: # ---------------------------------------------------------------------- tmpput
1913: } elsif ($userinput =~ /^tmpput/) {
1914: my ($cmd,$what)=split(/:/,$userinput);
1915: my $store;
1916: $tmpsnum++;
1917: my $id=$$.'_'.$clientip.'_'.$tmpsnum;
1918: $id=~s/\W/\_/g;
1919: $what=~s/\n//g;
1920: my $execdir=$perlvar{'lonDaemons'};
1921: if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
1922: print $store $what;
1923: close $store;
1924: print $client "$id\n";
1925: }
1926: else {
1.109 foxr 1927: print $client "error: ".($!+0)
1.111 matthew 1928: ."IO::File->new Failed ".
1929: "while attempting tmpput\n";
1.13 www 1930: }
1931:
1932: # ---------------------------------------------------------------------- tmpget
1933: } elsif ($userinput =~ /^tmpget/) {
1934: my ($cmd,$id)=split(/:/,$userinput);
1935: chomp($id);
1936: $id=~s/\W/\_/g;
1937: my $store;
1938: my $execdir=$perlvar{'lonDaemons'};
1939: if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
1940: my $reply=<$store>;
1941: print $client "$reply\n";
1942: close $store;
1943: }
1944: else {
1.109 foxr 1945: print $client "error: ".($!+0)
1.111 matthew 1946: ."IO::File->new Failed ".
1947: "while attempting tmpget\n";
1.13 www 1948: }
1949:
1.110 www 1950: # ---------------------------------------------------------------------- tmpdel
1951: } elsif ($userinput =~ /^tmpdel/) {
1952: my ($cmd,$id)=split(/:/,$userinput);
1953: chomp($id);
1954: $id=~s/\W/\_/g;
1955: my $execdir=$perlvar{'lonDaemons'};
1956: if (unlink("$execdir/tmp/$id.tmp")) {
1957: print $client "ok\n";
1958: } else {
1959: print $client "error: ".($!+0)
1.111 matthew 1960: ."Unlink tmp Failed ".
1961: "while attempting tmpdel\n";
1.110 www 1962: }
1.5 www 1963: # -------------------------------------------------------------------------- ls
1964: } elsif ($userinput =~ /^ls/) {
1965: my ($cmd,$ulsdir)=split(/:/,$userinput);
1966: my $ulsout='';
1967: my $ulsfn;
1968: if (-e $ulsdir) {
1.83 stredwic 1969: if(-d $ulsdir) {
1970: if (opendir(LSDIR,$ulsdir)) {
1971: while ($ulsfn=readdir(LSDIR)) {
1972: my @ulsstats=stat($ulsdir.'/'.$ulsfn);
1973: $ulsout.=$ulsfn.'&'.
1974: join('&',@ulsstats).':';
1975: }
1976: closedir(LSDIR);
1977: }
1978: } else {
1979: my @ulsstats=stat($ulsdir);
1980: $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
1981: }
1982: } else {
1.5 www 1983: $ulsout='no_such_dir';
1984: }
1.17 www 1985: if ($ulsout eq '') { $ulsout='empty'; }
1.5 www 1986: print $client "$ulsout\n";
1.136 www 1987: # ----------------------------------------------------------------- setannounce
1988: } elsif ($userinput =~ /^setannounce/) {
1989: my ($cmd,$announcement)=split(/:/,$userinput);
1990: chomp($announcement);
1991: $announcement=&unescape($announcement);
1992: if (my $store=IO::File->new('>'.$perlvar{'lonDocRoot'}.
1993: '/announcement.txt')) {
1994: print $store $announcement;
1995: close $store;
1996: print $client "ok\n";
1997: } else {
1998: print $client "error: ".($!+0)."\n";
1999: }
1.51 www 2000: # ------------------------------------------------------------------ Hanging up
2001: } elsif (($userinput =~ /^exit/) ||
2002: ($userinput =~ /^init/)) {
2003: &logthis(
2004: "Client $clientip ($hostid{$clientip}) hanging up: $userinput");
2005: print $client "bye\n";
1.59 www 2006: $client->close();
1.51 www 2007: last;
1.1 albertel 2008: # ------------------------------------------------------------- unknown command
1.121 albertel 2009: } elsif ($userinput =~ /^sethost:/) {
2010: print $client &sethost($userinput)."\n";
2011: } elsif ($userinput =~/^version:/) {
2012: print $client &version($userinput)."\n";
1.1 albertel 2013: } else {
2014: # unknown command
2015: print $client "unknown_cmd\n";
2016: }
1.58 www 2017: # -------------------------------------------------------------------- complete
1.63 www 2018: alarm(0);
1.58 www 2019: &status('Listening to '.$hostid{$clientip});
2020: }
1.59 www 2021: # --------------------------------------------- client unknown or fishy, refuse
1.1 albertel 2022: } else {
2023: print $client "refused\n";
1.59 www 2024: $client->close();
1.9 www 2025: &logthis("<font color=blue>WARNING: "
2026: ."Rejected client $clientip, closing connection</font>");
1.75 foxr 2027: }
1.106 foxr 2028: }
1.75 foxr 2029:
1.1 albertel 2030: # =============================================================================
1.75 foxr 2031:
2032: &logthis("<font color=red>CRITICAL: "
2033: ."Disconnect from $clientip ($hostid{$clientip})</font>");
1.106 foxr 2034:
1.59 www 2035:
1.1 albertel 2036: # this exit is VERY important, otherwise the child will become
2037: # a producer of more and more children, forking yourself into
2038: # process death.
2039: exit;
1.106 foxr 2040:
1.78 foxr 2041: }
2042:
2043:
2044: #
2045: # Checks to see if the input roleput request was to set
2046: # an author role. If so, invokes the lchtmldir script to set
2047: # up a correct public_html
2048: # Parameters:
2049: # request - The request sent to the rolesput subchunk.
2050: # We're looking for /domain/_au
2051: # domain - The domain in which the user is having roles doctored.
2052: # user - Name of the user for which the role is being put.
2053: # authtype - The authentication type associated with the user.
2054: #
2055: sub ManagePermissions
2056: {
2057: my $request = shift;
2058: my $domain = shift;
2059: my $user = shift;
2060: my $authtype= shift;
2061:
2062: # See if the request is of the form /$domain/_au
1.134 albertel 2063: &logthis("ruequest is $request");
1.78 foxr 2064: if($request =~ /^(\/$domain\/_au)$/) { # It's an author rolesput...
2065: my $execdir = $perlvar{'lonDaemons'};
2066: my $userhome= "/home/$user" ;
1.134 albertel 2067: &logthis("system $execdir/lchtmldir $userhome $user $authtype");
1.78 foxr 2068: system("$execdir/lchtmldir $userhome $user $authtype");
2069: }
2070: }
2071: #
2072: # GetAuthType - Determines the authorization type of a user in a domain.
2073:
2074: # Returns the authorization type or nouser if there is no such user.
2075: #
2076: sub GetAuthType
2077: {
2078: my $domain = shift;
2079: my $user = shift;
2080:
1.79 foxr 2081: Debug("GetAuthType( $domain, $user ) \n");
1.78 foxr 2082: my $proname = &propath($domain, $user);
2083: my $passwdfile = "$proname/passwd";
2084: if( -e $passwdfile ) {
2085: my $pf = IO::File->new($passwdfile);
2086: my $realpassword = <$pf>;
2087: chomp($realpassword);
1.79 foxr 2088: Debug("Password info = $realpassword\n");
1.78 foxr 2089: my ($authtype, $contentpwd) = split(/:/, $realpassword);
1.79 foxr 2090: Debug("Authtype = $authtype, content = $contentpwd\n");
1.78 foxr 2091: my $availinfo = '';
1.91 albertel 2092: if($authtype eq 'krb4' or $authtype eq 'krb5') {
1.78 foxr 2093: $availinfo = $contentpwd;
2094: }
1.79 foxr 2095:
1.78 foxr 2096: return "$authtype:$availinfo";
2097: }
2098: else {
1.79 foxr 2099: Debug("Returning nouser");
1.78 foxr 2100: return "nouser";
2101: }
1.1 albertel 2102: }
2103:
1.84 albertel 2104: sub addline {
2105: my ($fname,$hostid,$ip,$newline)=@_;
2106: my $contents;
2107: my $found=0;
2108: my $expr='^'.$hostid.':'.$ip.':';
2109: $expr =~ s/\./\\\./g;
1.134 albertel 2110: my $sh;
1.84 albertel 2111: if ($sh=IO::File->new("$fname.subscription")) {
2112: while (my $subline=<$sh>) {
2113: if ($subline !~ /$expr/) {$contents.= $subline;} else {$found=1;}
2114: }
2115: $sh->close();
2116: }
2117: $sh=IO::File->new(">$fname.subscription");
2118: if ($contents) { print $sh $contents; }
2119: if ($newline) { print $sh $newline; }
2120: $sh->close();
2121: return $found;
1.86 www 2122: }
2123:
2124: sub getchat {
1.122 www 2125: my ($cdom,$cname,$udom,$uname)=@_;
1.87 www 2126: my %hash;
2127: my $proname=&propath($cdom,$cname);
2128: my @entries=();
1.88 albertel 2129: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
2130: &GDBM_READER(),0640)) {
2131: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
2132: untie %hash;
1.123 www 2133: }
1.124 www 2134: my @participants=();
1.134 albertel 2135: my $cutoff=time-60;
1.123 www 2136: if (tie(%hash,'GDBM_File',"$proname/nohist_inchatroom.db",
1.124 www 2137: &GDBM_WRCREAT(),0640)) {
2138: $hash{$uname.':'.$udom}=time;
1.123 www 2139: foreach (sort keys %hash) {
2140: if ($hash{$_}>$cutoff) {
1.124 www 2141: $participants[$#participants+1]='active_participant:'.$_;
1.123 www 2142: }
2143: }
2144: untie %hash;
1.86 www 2145: }
1.124 www 2146: return (@participants,@entries);
1.86 www 2147: }
2148:
2149: sub chatadd {
1.88 albertel 2150: my ($cdom,$cname,$newchat)=@_;
2151: my %hash;
2152: my $proname=&propath($cdom,$cname);
2153: my @entries=();
1.142 www 2154: my $time=time;
1.88 albertel 2155: if (tie(%hash,'GDBM_File',"$proname/nohist_chatroom.db",
2156: &GDBM_WRCREAT(),0640)) {
2157: @entries=map { $_.':'.$hash{$_} } sort keys %hash;
2158: my ($lastid)=($entries[$#entries]=~/^(\w+)\:/);
2159: my ($thentime,$idnum)=split(/\_/,$lastid);
2160: my $newid=$time.'_000000';
2161: if ($thentime==$time) {
2162: $idnum=~s/^0+//;
2163: $idnum++;
2164: $idnum=substr('000000'.$idnum,-6,6);
2165: $newid=$time.'_'.$idnum;
2166: }
2167: $hash{$newid}=$newchat;
2168: my $expired=$time-3600;
2169: foreach (keys %hash) {
2170: my ($thistime)=($_=~/(\d+)\_/);
2171: if ($thistime<$expired) {
1.89 www 2172: delete $hash{$_};
1.88 albertel 2173: }
2174: }
2175: untie %hash;
1.142 www 2176: }
2177: {
2178: my $hfh;
2179: if ($hfh=IO::File->new(">>$proname/chatroom.log")) {
2180: print $hfh "$time:".&unescape($newchat)."\n";
2181: }
1.86 www 2182: }
1.84 albertel 2183: }
2184:
2185: sub unsub {
2186: my ($fname,$clientip)=@_;
2187: my $result;
2188: if (unlink("$fname.$hostid{$clientip}")) {
2189: $result="ok\n";
2190: } else {
2191: $result="not_subscribed\n";
2192: }
2193: if (-e "$fname.subscription") {
2194: my $found=&addline($fname,$hostid{$clientip},$clientip,'');
2195: if ($found) { $result="ok\n"; }
2196: } else {
2197: if ($result != "ok\n") { $result="not_subscribed\n"; }
2198: }
2199: return $result;
2200: }
2201:
1.101 www 2202: sub currentversion {
2203: my $fname=shift;
2204: my $version=-1;
2205: my $ulsdir='';
2206: if ($fname=~/^(.+)\/[^\/]+$/) {
2207: $ulsdir=$1;
2208: }
1.114 albertel 2209: my ($fnamere1,$fnamere2);
2210: # remove version if already specified
1.101 www 2211: $fname=~s/\.\d+\.(\w+(?:\.meta)*)$/\.$1/;
1.114 albertel 2212: # get the bits that go before and after the version number
2213: if ( $fname=~/^(.*\.)(\w+(?:\.meta)*)$/ ) {
2214: $fnamere1=$1;
2215: $fnamere2='.'.$2;
2216: }
1.101 www 2217: if (-e $fname) { $version=1; }
2218: if (-e $ulsdir) {
1.134 albertel 2219: if(-d $ulsdir) {
2220: if (opendir(LSDIR,$ulsdir)) {
2221: my $ulsfn;
2222: while ($ulsfn=readdir(LSDIR)) {
1.101 www 2223: # see if this is a regular file (ignore links produced earlier)
1.134 albertel 2224: my $thisfile=$ulsdir.'/'.$ulsfn;
2225: unless (-l $thisfile) {
2226: if ($thisfile=~/\Q$fnamere1\E(\d+)\Q$fnamere2\E/) {
2227: if ($1>$version) { $version=$1; }
2228: }
2229: }
2230: }
2231: closedir(LSDIR);
2232: $version++;
2233: }
2234: }
2235: }
2236: return $version;
1.101 www 2237: }
2238:
2239: sub thisversion {
2240: my $fname=shift;
2241: my $version=-1;
2242: if ($fname=~/\.(\d+)\.\w+(?:\.meta)*$/) {
2243: $version=$1;
2244: }
2245: return $version;
2246: }
2247:
1.84 albertel 2248: sub subscribe {
2249: my ($userinput,$clientip)=@_;
2250: my $result;
2251: my ($cmd,$fname)=split(/:/,$userinput);
2252: my $ownership=&ishome($fname);
2253: if ($ownership eq 'owner') {
1.101 www 2254: # explitly asking for the current version?
2255: unless (-e $fname) {
2256: my $currentversion=¤tversion($fname);
2257: if (&thisversion($fname)==$currentversion) {
2258: if ($fname=~/^(.+)\.\d+\.(\w+(?:\.meta)*)$/) {
2259: my $root=$1;
2260: my $extension=$2;
2261: symlink($root.'.'.$extension,
2262: $root.'.'.$currentversion.'.'.$extension);
1.102 www 2263: unless ($extension=~/\.meta$/) {
2264: symlink($root.'.'.$extension.'.meta',
2265: $root.'.'.$currentversion.'.'.$extension.'.meta');
2266: }
1.101 www 2267: }
2268: }
2269: }
1.84 albertel 2270: if (-e $fname) {
2271: if (-d $fname) {
2272: $result="directory\n";
2273: } else {
2274: if (-e "$fname.$hostid{$clientip}") {&unsub($fname,$clientip);}
1.134 albertel 2275: my $now=time;
1.84 albertel 2276: my $found=&addline($fname,$hostid{$clientip},$clientip,
2277: "$hostid{$clientip}:$clientip:$now\n");
2278: if ($found) { $result="$fname\n"; }
2279: # if they were subscribed to only meta data, delete that
2280: # subscription, when you subscribe to a file you also get
2281: # the metadata
2282: unless ($fname=~/\.meta$/) { &unsub("$fname.meta",$clientip); }
2283: $fname=~s/\/home\/httpd\/html\/res/raw/;
2284: $fname="http://$thisserver/".$fname;
2285: $result="$fname\n";
2286: }
2287: } else {
2288: $result="not_found\n";
2289: }
2290: } else {
2291: $result="rejected\n";
2292: }
2293: return $result;
2294: }
1.91 albertel 2295:
2296: sub make_passwd_file {
1.98 foxr 2297: my ($uname, $umode,$npass,$passfilename)=@_;
1.91 albertel 2298: my $result="ok\n";
2299: if ($umode eq 'krb4' or $umode eq 'krb5') {
2300: {
2301: my $pf = IO::File->new(">$passfilename");
2302: print $pf "$umode:$npass\n";
2303: }
2304: } elsif ($umode eq 'internal') {
2305: my $salt=time;
2306: $salt=substr($salt,6,2);
2307: my $ncpass=crypt($npass,$salt);
2308: {
2309: &Debug("Creating internal auth");
2310: my $pf = IO::File->new(">$passfilename");
2311: print $pf "internal:$ncpass\n";
2312: }
2313: } elsif ($umode eq 'localauth') {
2314: {
2315: my $pf = IO::File->new(">$passfilename");
2316: print $pf "localauth:$npass\n";
2317: }
2318: } elsif ($umode eq 'unix') {
2319: {
2320: my $execpath="$perlvar{'lonDaemons'}/"."lcuseradd";
2321: {
2322: &Debug("Executing external: ".$execpath);
1.98 foxr 2323: &Debug("user = ".$uname.", Password =". $npass);
1.132 matthew 2324: my $se = IO::File->new("|$execpath > $perlvar{'lonDaemons'}/logs/lcuseradd.log");
1.91 albertel 2325: print $se "$uname\n";
2326: print $se "$npass\n";
2327: print $se "$npass\n";
1.97 foxr 2328: }
2329: my $useraddok = $?;
2330: if($useraddok > 0) {
2331: &logthis("Failed lcuseradd: ".&lcuseraddstrerror($useraddok));
1.91 albertel 2332: }
2333: my $pf = IO::File->new(">$passfilename");
2334: print $pf "unix:\n";
2335: }
2336: } elsif ($umode eq 'none') {
2337: {
2338: my $pf = IO::File->new(">$passfilename");
2339: print $pf "none:\n";
2340: }
2341: } else {
2342: $result="auth_mode_error\n";
2343: }
2344: return $result;
1.121 albertel 2345: }
2346:
2347: sub sethost {
2348: my ($remotereq) = @_;
2349: my (undef,$hostid)=split(/:/,$remotereq);
2350: if (!defined($hostid)) { $hostid=$perlvar{'lonHostID'}; }
2351: if ($hostip{$perlvar{'lonHostID'}} eq $hostip{$hostid}) {
2352: $currenthostid=$hostid;
2353: $currentdomainid=$hostdom{$hostid};
2354: &logthis("Setting hostid to $hostid, and domain to $currentdomainid");
2355: } else {
2356: &logthis("Requested host id $hostid not an alias of ".
2357: $perlvar{'lonHostID'}." refusing connection");
2358: return 'unable_to_set';
2359: }
2360: return 'ok';
2361: }
2362:
2363: sub version {
2364: my ($userinput)=@_;
2365: $remoteVERSION=(split(/:/,$userinput))[1];
2366: return "version:$VERSION";
1.127 albertel 2367: }
2368:
1.128 albertel 2369: #There is a copy of this in lonnet.pm
1.127 albertel 2370: sub userload {
2371: my $numusers=0;
2372: {
2373: opendir(LONIDS,$perlvar{'lonIDsDir'});
2374: my $filename;
2375: my $curtime=time;
2376: while ($filename=readdir(LONIDS)) {
2377: if ($filename eq '.' || $filename eq '..') {next;}
1.138 albertel 2378: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
2379: if ($curtime-$mtime < 3600) { $numusers++; }
1.127 albertel 2380: }
2381: closedir(LONIDS);
2382: }
2383: my $userloadpercent=0;
2384: my $maxuserload=$perlvar{'lonUserLoadLim'};
2385: if ($maxuserload) {
1.129 albertel 2386: $userloadpercent=100*$numusers/$maxuserload;
1.127 albertel 2387: }
1.130 albertel 2388: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.127 albertel 2389: return $userloadpercent;
1.91 albertel 2390: }
2391:
1.61 harris41 2392: # ----------------------------------- POD (plain old documentation, CPAN style)
2393:
2394: =head1 NAME
2395:
2396: lond - "LON Daemon" Server (port "LOND" 5663)
2397:
2398: =head1 SYNOPSIS
2399:
1.74 harris41 2400: Usage: B<lond>
2401:
2402: Should only be run as user=www. This is a command-line script which
2403: is invoked by B<loncron>. There is no expectation that a typical user
2404: will manually start B<lond> from the command-line. (In other words,
2405: DO NOT START B<lond> YOURSELF.)
1.61 harris41 2406:
2407: =head1 DESCRIPTION
2408:
1.74 harris41 2409: There are two characteristics associated with the running of B<lond>,
2410: PROCESS MANAGEMENT (starting, stopping, handling child processes)
2411: and SERVER-SIDE ACTIVITIES (password authentication, user creation,
2412: subscriptions, etc). These are described in two large
2413: sections below.
2414:
2415: B<PROCESS MANAGEMENT>
2416:
1.61 harris41 2417: Preforker - server who forks first. Runs as a daemon. HUPs.
2418: Uses IDEA encryption
2419:
1.74 harris41 2420: B<lond> forks off children processes that correspond to the other servers
2421: in the network. Management of these processes can be done at the
2422: parent process level or the child process level.
2423:
2424: B<logs/lond.log> is the location of log messages.
2425:
2426: The process management is now explained in terms of linux shell commands,
2427: subroutines internal to this code, and signal assignments:
2428:
2429: =over 4
2430:
2431: =item *
2432:
2433: PID is stored in B<logs/lond.pid>
2434:
2435: This is the process id number of the parent B<lond> process.
2436:
2437: =item *
2438:
2439: SIGTERM and SIGINT
2440:
2441: Parent signal assignment:
2442: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
2443:
2444: Child signal assignment:
2445: $SIG{INT} = 'DEFAULT'; (and SIGTERM is DEFAULT also)
2446: (The child dies and a SIGALRM is sent to parent, awaking parent from slumber
2447: to restart a new child.)
2448:
2449: Command-line invocations:
2450: B<kill> B<-s> SIGTERM I<PID>
2451: B<kill> B<-s> SIGINT I<PID>
2452:
2453: Subroutine B<HUNTSMAN>:
2454: This is only invoked for the B<lond> parent I<PID>.
2455: This kills all the children, and then the parent.
2456: The B<lonc.pid> file is cleared.
2457:
2458: =item *
2459:
2460: SIGHUP
2461:
2462: Current bug:
2463: This signal can only be processed the first time
2464: on the parent process. Subsequent SIGHUP signals
2465: have no effect.
2466:
2467: Parent signal assignment:
2468: $SIG{HUP} = \&HUPSMAN;
2469:
2470: Child signal assignment:
2471: none (nothing happens)
2472:
2473: Command-line invocations:
2474: B<kill> B<-s> SIGHUP I<PID>
2475:
2476: Subroutine B<HUPSMAN>:
2477: This is only invoked for the B<lond> parent I<PID>,
2478: This kills all the children, and then the parent.
2479: The B<lond.pid> file is cleared.
2480:
2481: =item *
2482:
2483: SIGUSR1
2484:
2485: Parent signal assignment:
2486: $SIG{USR1} = \&USRMAN;
2487:
2488: Child signal assignment:
2489: $SIG{USR1}= \&logstatus;
2490:
2491: Command-line invocations:
2492: B<kill> B<-s> SIGUSR1 I<PID>
2493:
2494: Subroutine B<USRMAN>:
2495: When invoked for the B<lond> parent I<PID>,
2496: SIGUSR1 is sent to all the children, and the status of
2497: each connection is logged.
2498:
2499: =item *
2500:
2501: SIGCHLD
2502:
2503: Parent signal assignment:
2504: $SIG{CHLD} = \&REAPER;
2505:
2506: Child signal assignment:
2507: none
2508:
2509: Command-line invocations:
2510: B<kill> B<-s> SIGCHLD I<PID>
2511:
2512: Subroutine B<REAPER>:
2513: This is only invoked for the B<lond> parent I<PID>.
2514: Information pertaining to the child is removed.
2515: The socket port is cleaned up.
2516:
2517: =back
2518:
2519: B<SERVER-SIDE ACTIVITIES>
2520:
2521: Server-side information can be accepted in an encrypted or non-encrypted
2522: method.
2523:
2524: =over 4
2525:
2526: =item ping
2527:
2528: Query a client in the hosts.tab table; "Are you there?"
2529:
2530: =item pong
2531:
2532: Respond to a ping query.
2533:
2534: =item ekey
2535:
2536: Read in encrypted key, make cipher. Respond with a buildkey.
2537:
2538: =item load
2539:
2540: Respond with CPU load based on a computation upon /proc/loadavg.
2541:
2542: =item currentauth
2543:
2544: Reply with current authentication information (only over an
2545: encrypted channel).
2546:
2547: =item auth
2548:
2549: Only over an encrypted channel, reply as to whether a user's
2550: authentication information can be validated.
2551:
2552: =item passwd
2553:
2554: Allow for a password to be set.
2555:
2556: =item makeuser
2557:
2558: Make a user.
2559:
2560: =item passwd
2561:
2562: Allow for authentication mechanism and password to be changed.
2563:
2564: =item home
1.61 harris41 2565:
1.74 harris41 2566: Respond to a question "are you the home for a given user?"
2567:
2568: =item update
2569:
2570: Update contents of a subscribed resource.
2571:
2572: =item unsubscribe
2573:
2574: The server is unsubscribing from a resource.
2575:
2576: =item subscribe
2577:
2578: The server is subscribing to a resource.
2579:
2580: =item log
2581:
2582: Place in B<logs/lond.log>
2583:
2584: =item put
2585:
2586: stores hash in namespace
2587:
2588: =item rolesput
2589:
2590: put a role into a user's environment
2591:
2592: =item get
2593:
2594: returns hash with keys from array
2595: reference filled in from namespace
2596:
2597: =item eget
2598:
2599: returns hash with keys from array
2600: reference filled in from namesp (encrypts the return communication)
2601:
2602: =item rolesget
2603:
2604: get a role from a user's environment
2605:
2606: =item del
2607:
2608: deletes keys out of array from namespace
2609:
2610: =item keys
2611:
2612: returns namespace keys
2613:
2614: =item dump
2615:
2616: dumps the complete (or key matching regexp) namespace into a hash
2617:
2618: =item store
2619:
2620: stores hash permanently
2621: for this url; hashref needs to be given and should be a \%hashname; the
2622: remaining args aren't required and if they aren't passed or are '' they will
2623: be derived from the ENV
2624:
2625: =item restore
2626:
2627: returns a hash for a given url
2628:
2629: =item querysend
2630:
2631: Tells client about the lonsql process that has been launched in response
2632: to a sent query.
2633:
2634: =item queryreply
2635:
2636: Accept information from lonsql and make appropriate storage in temporary
2637: file space.
2638:
2639: =item idput
2640:
2641: Defines usernames as corresponding to IDs. (These "IDs" are unique identifiers
2642: for each student, defined perhaps by the institutional Registrar.)
2643:
2644: =item idget
2645:
2646: Returns usernames corresponding to IDs. (These "IDs" are unique identifiers
2647: for each student, defined perhaps by the institutional Registrar.)
2648:
2649: =item tmpput
2650:
2651: Accept and store information in temporary space.
2652:
2653: =item tmpget
2654:
2655: Send along temporarily stored information.
2656:
2657: =item ls
2658:
2659: List part of a user's directory.
2660:
1.135 foxr 2661: =item pushtable
2662:
2663: Pushes a file in /home/httpd/lonTab directory. Currently limited to:
2664: hosts.tab and domain.tab. The old file is copied to *.tab.backup but
2665: must be restored manually in case of a problem with the new table file.
2666: pushtable requires that the request be encrypted and validated via
2667: ValidateManager. The form of the command is:
2668: enc:pushtable tablename <tablecontents> \n
2669: where pushtable, tablename and <tablecontents> will be encrypted, but \n is a
2670: cleartext newline.
2671:
1.74 harris41 2672: =item Hanging up (exit or init)
2673:
2674: What to do when a client tells the server that they (the client)
2675: are leaving the network.
2676:
2677: =item unknown command
2678:
2679: If B<lond> is sent an unknown command (not in the list above),
2680: it replys to the client "unknown_cmd".
1.135 foxr 2681:
1.74 harris41 2682:
2683: =item UNKNOWN CLIENT
2684:
2685: If the anti-spoofing algorithm cannot verify the client,
2686: the client is rejected (with a "refused" message sent
2687: to the client, and the connection is closed.
2688:
2689: =back
1.61 harris41 2690:
2691: =head1 PREREQUISITES
2692:
2693: IO::Socket
2694: IO::File
2695: Apache::File
2696: Symbol
2697: POSIX
2698: Crypt::IDEA
2699: LWP::UserAgent()
2700: GDBM_File
2701: Authen::Krb4
1.91 albertel 2702: Authen::Krb5
1.61 harris41 2703:
2704: =head1 COREQUISITES
2705:
2706: =head1 OSNAMES
2707:
2708: linux
2709:
2710: =head1 SCRIPT CATEGORIES
2711:
2712: Server/Process
2713:
2714: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>