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