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