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