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