File:  [LON-CAPA] / loncom / lond
Revision 1.57: download - view: text, annotated - select for diffs
Mon Nov 26 20:31:01 2001 UTC (22 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Status

    1: #!/usr/bin/perl
    2: # The LearningOnline Network
    3: # lond "LON Daemon" Server (port "LOND" 5663)
    4: # 5/26/99,6/4,6/10,6/11,6/14,6/15,6/26,6/28,6/30,
    5: # 7/8,7/9,7/10,7/12,7/17,7/19,9/21,
    6: # 10/7,10/8,10/9,10/11,10/13,10/15,11/4,11/16,
    7: # 12/7,12/15,01/06,01/11,01/12,01/14,2/8,
    8: # 03/07,05/31 Gerd Kortemeyer
    9: # 06/26 Scott Harrison
   10: # 06/29,06/30,07/14,07/15,07/17,07/20,07/25,09/18 Gerd Kortemeyer
   11: # 12/05 Scott Harrison
   12: # 12/05,12/13,12/29 Gerd Kortemeyer
   13: # Jan 01 Scott Harrison
   14: # 02/12 Gerd Kortemeyer
   15: # 03/15 Scott Harrison
   16: # 03/24 Gerd Kortemeyer
   17: # 04/02 Scott Harrison
   18: # 05/11,05/28,08/30 Gerd Kortemeyer
   19: # 9/30,10/22,11/13,11/15,11/16 Scott Harrison
   20: # 11/26 Gerd Kortemeyer
   21: #
   22: # $Id: lond,v 1.57 2001/11/26 20:31:01 www Exp $
   23: ###
   24: 
   25: # based on "Perl Cookbook" ISBN 1-56592-243-3
   26: # preforker - server who forks first
   27: # runs as a daemon
   28: # HUPs
   29: # uses IDEA encryption
   30: 
   31: use IO::Socket;
   32: use IO::File;
   33: use Apache::File;
   34: use Symbol;
   35: use POSIX;
   36: use Crypt::IDEA;
   37: use LWP::UserAgent();
   38: use GDBM_File;
   39: use Authen::Krb4;
   40: use lib '/home/httpd/lib/perl/';
   41: use localauth;
   42: 
   43: my $status='';
   44: my $lastlog='';
   45: 
   46: # grabs exception and records it to log before exiting
   47: sub catchexception {
   48:     my ($error)=@_;
   49:     $SIG{'QUIT'}='DEFAULT';
   50:     $SIG{__DIE__}='DEFAULT';
   51:     &logthis("<font color=red>CRITICAL: "
   52:      ."ABNORMAL EXIT. Child $$ for server $wasserver died through "
   53:      ."a crash with this error msg->[$error]</font>");
   54:     &logthis('Famous last words: '.$status.' - '.$lastlog);
   55:     if ($client) { print $client "error: $error\n"; }
   56:     die($error);
   57: }
   58: 
   59: # -------------------------------- Set signal handlers to record abnormal exits
   60: 
   61: $SIG{'QUIT'}=\&catchexception;
   62: $SIG{__DIE__}=\&catchexception;
   63: 
   64: # ------------------------------------ Read httpd access.conf and get variables
   65: 
   66: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
   67: 
   68: while ($configline=<CONFIG>) {
   69:     if ($configline =~ /PerlSetVar/) {
   70: 	my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
   71:         chomp($varvalue);
   72:         $perlvar{$varname}=$varvalue;
   73:     }
   74: }
   75: close(CONFIG);
   76: 
   77: # ----------------------------- Make sure this process is running from user=www
   78: my $wwwid=getpwnam('www');
   79: if ($wwwid!=$<) {
   80:    $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
   81:    $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
   82:    system("echo 'User ID mismatch.  lond must be run as user www.' |\
   83:  mailto $emailto -s '$subj' > /dev/null");
   84:    exit 1;
   85: }
   86: 
   87: # --------------------------------------------- Check if other instance running
   88: 
   89: my $pidfile="$perlvar{'lonDaemons'}/logs/lond.pid";
   90: 
   91: if (-e $pidfile) {
   92:    my $lfh=IO::File->new("$pidfile");
   93:    my $pide=<$lfh>;
   94:    chomp($pide);
   95:    if (kill 0 => $pide) { die "already running"; }
   96: }
   97: 
   98: $PREFORK=4; # number of children to maintain, at least four spare
   99: 
  100: # ------------------------------------------------------------- Read hosts file
  101: 
  102: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
  103: 
  104: while ($configline=<CONFIG>) {
  105:     my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
  106:     chomp($ip);
  107:     $hostid{$ip}=$id;
  108:     if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
  109:     $PREFORK++;
  110: }
  111: close(CONFIG);
  112: 
  113: # establish SERVER socket, bind and listen.
  114: $server = IO::Socket::INET->new(LocalPort => $perlvar{'londPort'},
  115:                                 Type      => SOCK_STREAM,
  116:                                 Proto     => 'tcp',
  117:                                 Reuse     => 1,
  118:                                 Listen    => 10 )
  119:   or die "making socket: $@\n";
  120: 
  121: # --------------------------------------------------------- Do global variables
  122: 
  123: # global variables
  124: 
  125: $MAX_CLIENTS_PER_CHILD  = 5;        # number of clients each child should 
  126:                                     # process
  127: %children               = ();       # keys are current child process IDs
  128: $children               = 0;        # current number of children
  129: 
  130: sub REAPER {                        # takes care of dead children
  131:     $SIG{CHLD} = \&REAPER;
  132:     my $pid = wait;
  133:     $children --;
  134:     &logthis("Child $pid died");
  135:     delete $children{$pid};
  136: }
  137: 
  138: sub HUNTSMAN {                      # signal handler for SIGINT
  139:     local($SIG{CHLD}) = 'IGNORE';   # we're going to kill our children
  140:     kill 'INT' => keys %children;
  141:     my $execdir=$perlvar{'lonDaemons'};
  142:     unlink("$execdir/logs/lond.pid");
  143:     &logthis("<font color=red>CRITICAL: Shutting down</font>");
  144:     exit;                           # clean up with dignity
  145: }
  146: 
  147: sub HUPSMAN {                      # signal handler for SIGHUP
  148:     local($SIG{CHLD}) = 'IGNORE';  # we're going to kill our children
  149:     kill 'INT' => keys %children;
  150:     close($server);                # free up socket
  151:     &logthis("<font color=red>CRITICAL: Restarting</font>");
  152:     unlink("$execdir/logs/lond.pid");
  153:     my $execdir=$perlvar{'lonDaemons'};
  154:     exec("$execdir/lond");         # here we go again
  155: }
  156: 
  157: sub checkchildren {
  158:     &initnewstatus();
  159:     &logstatus();
  160:     &logthis('Going to check on the children');
  161:     map {
  162: 	sleep 1;
  163:         unless (kill 'USR1' => $_) {
  164: 	    &logthis ('Child '.$_.' is dead');
  165:             &logstatus($$.' is dead');
  166:         } 
  167:     } sort keys %children;
  168: }
  169: 
  170: # --------------------------------------------------------------------- Logging
  171: 
  172: sub logthis {
  173:     my $message=shift;
  174:     my $execdir=$perlvar{'lonDaemons'};
  175:     my $fh=IO::File->new(">>$execdir/logs/lond.log");
  176:     my $now=time;
  177:     my $local=localtime($now);
  178:     print $fh "$local ($$): $message\n";
  179: }
  180: 
  181: # ------------------------------------------------------------------ Log status
  182: 
  183: sub logstatus {
  184:     my $docdir=$perlvar{'lonDocRoot'};
  185:     my $fh=IO::File->new(">>$docdir/lon-status/londstatus.txt");
  186:     print $fh $$."\t".$status."\t".$lastlog."\n";
  187: }
  188: 
  189: sub initnewstatus {
  190:     my $docdir=$perlvar{'lonDocRoot'};
  191:     my $fh=IO::File->new(">$docdir/lon-status/londstatus.txt");
  192:     my $now=time;
  193:     my $local=localtime($now);
  194:     print $fh "LOND status $local - parent $$\n\n";
  195: }
  196: 
  197: # -------------------------------------------------------------- Status setting
  198: 
  199: sub status {
  200:     my $what=shift;
  201:     my $now=time;
  202:     my $local=localtime($now);
  203:     $status=$local.': '.$what;
  204: }
  205: 
  206: # -------------------------------------------------------- Escape Special Chars
  207: 
  208: sub escape {
  209:     my $str=shift;
  210:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
  211:     return $str;
  212: }
  213: 
  214: # ----------------------------------------------------- Un-Escape Special Chars
  215: 
  216: sub unescape {
  217:     my $str=shift;
  218:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  219:     return $str;
  220: }
  221: 
  222: # ----------------------------------------------------------- Send USR1 to lonc
  223: 
  224: sub reconlonc {
  225:     my $peerfile=shift;
  226:     &logthis("Trying to reconnect for $peerfile");
  227:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  228:     if (my $fh=IO::File->new("$loncfile")) {
  229: 	my $loncpid=<$fh>;
  230:         chomp($loncpid);
  231:         if (kill 0 => $loncpid) {
  232: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  233:             kill USR1 => $loncpid;
  234:             sleep 1;
  235:             if (-e "$peerfile") { return; }
  236:             &logthis("$peerfile still not there, give it another try");
  237:             sleep 5;
  238:             if (-e "$peerfile") { return; }
  239:             &logthis(
  240:  "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
  241:         } else {
  242: 	    &logthis(
  243:               "<font color=red>CRITICAL: "
  244:              ."lonc at pid $loncpid not responding, giving up</font>");
  245:         }
  246:     } else {
  247:       &logthis('<font color=red>CRITICAL: lonc not running, giving up</font>');
  248:     }
  249: }
  250: 
  251: # -------------------------------------------------- Non-critical communication
  252: 
  253: sub subreply {
  254:     my ($cmd,$server)=@_;
  255:     my $peerfile="$perlvar{'lonSockDir'}/$server";
  256:     my $sclient=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  257:                                       Type    => SOCK_STREAM,
  258:                                       Timeout => 10)
  259:        or return "con_lost";
  260:     print $sclient "$cmd\n";
  261:     my $answer=<$sclient>;
  262:     chomp($answer);
  263:     if (!$answer) { $answer="con_lost"; }
  264:     return $answer;
  265: }
  266: 
  267: sub reply {
  268:   my ($cmd,$server)=@_;
  269:   my $answer;
  270:   if ($server ne $perlvar{'lonHostID'}) { 
  271:     $answer=subreply($cmd,$server);
  272:     if ($answer eq 'con_lost') {
  273: 	$answer=subreply("ping",$server);
  274:         if ($answer ne $server) {
  275:            &reconlonc("$perlvar{'lonSockDir'}/$server");
  276:         }
  277:         $answer=subreply($cmd,$server);
  278:     }
  279:   } else {
  280:     $answer='self_reply';
  281:   } 
  282:   return $answer;
  283: }
  284: 
  285: # -------------------------------------------------------------- Talk to lonsql
  286: 
  287: sub sqlreply {
  288:     my ($cmd)=@_;
  289:     my $answer=subsqlreply($cmd);
  290:     if ($answer eq 'con_lost') { $answer=subsqlreply($cmd); }
  291:     return $answer;
  292: }
  293: 
  294: sub subsqlreply {
  295:     my ($cmd)=@_;
  296:     my $unixsock="mysqlsock";
  297:     my $peerfile="$perlvar{'lonSockDir'}/$unixsock";
  298:     my $sclient=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  299:                                       Type    => SOCK_STREAM,
  300:                                       Timeout => 10)
  301:        or return "con_lost";
  302:     print $sclient "$cmd\n";
  303:     my $answer=<$sclient>;
  304:     chomp($answer);
  305:     if (!$answer) { $answer="con_lost"; }
  306:     return $answer;
  307: }
  308: 
  309: # -------------------------------------------- Return path to profile directory
  310: 
  311: sub propath {
  312:     my ($udom,$uname)=@_;
  313:     $udom=~s/\W//g;
  314:     $uname=~s/\W//g;
  315:     my $subdir=$uname.'__';
  316:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
  317:     my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
  318:     return $proname;
  319: } 
  320: 
  321: # --------------------------------------- Is this the home server of an author?
  322: 
  323: sub ishome {
  324:     my $author=shift;
  325:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
  326:     my ($udom,$uname)=split(/\//,$author);
  327:     my $proname=propath($udom,$uname);
  328:     if (-e $proname) {
  329: 	return 'owner';
  330:     } else {
  331:         return 'not_owner';
  332:     }
  333: }
  334: 
  335: # ======================================================= Continue main program
  336: # ---------------------------------------------------- Fork once and dissociate
  337: 
  338: $fpid=fork;
  339: exit if $fpid;
  340: die "Couldn't fork: $!" unless defined ($fpid);
  341: 
  342: POSIX::setsid() or die "Can't start new session: $!";
  343: 
  344: # ------------------------------------------------------- Write our PID on disk
  345: 
  346: $execdir=$perlvar{'lonDaemons'};
  347: open (PIDSAVE,">$execdir/logs/lond.pid");
  348: print PIDSAVE "$$\n";
  349: close(PIDSAVE);
  350: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
  351: &status('Starting');
  352: 
  353: # ------------------------------------------------------- Now we are on our own
  354:     
  355: # Fork off our children.
  356: for (1 .. $PREFORK) {
  357:     make_new_child();
  358: }
  359: 
  360: # ----------------------------------------------------- Install signal handlers
  361: 
  362: &status('Forked children');
  363: 
  364: $SIG{CHLD} = \&REAPER;
  365: $SIG{INT}  = $SIG{TERM} = \&HUNTSMAN;
  366: $SIG{HUP}  = \&HUPSMAN;
  367: $SIG{USR1} = \&checkchildren;
  368: 
  369: # And maintain the population.
  370: while (1) {
  371:     &status('Sleeping');
  372:     sleep;                          # wait for a signal (i.e., child's death)
  373:     &logthis('Woke up');
  374:     &status('Woke up');
  375:     for ($i = $children; $i < $PREFORK; $i++) {
  376:         make_new_child();           # top up the child pool
  377:     }
  378: }
  379: 
  380: sub make_new_child {
  381:     my $pid;
  382:     my $cipher;
  383:     my $sigset;
  384:     &logthis("Attempting to start child");    
  385:     # block signal for fork
  386:     $sigset = POSIX::SigSet->new(SIGINT);
  387:     sigprocmask(SIG_BLOCK, $sigset)
  388:         or die "Can't block SIGINT for fork: $!\n";
  389:     
  390:     die "fork: $!" unless defined ($pid = fork);
  391:     
  392:     if ($pid) {
  393:         # Parent records the child's birth and returns.
  394:         sigprocmask(SIG_UNBLOCK, $sigset)
  395:             or die "Can't unblock SIGINT for fork: $!\n";
  396:         $children{$pid} = 1;
  397:         $children++;
  398:         &status('Started child '.$pid);
  399:         return;
  400:     } else {
  401:         # Child can *not* return from this subroutine.
  402:         $SIG{INT} = 'DEFAULT';      # make SIGINT kill us as it did before
  403:         $SIG{USR1}= \&logstatus;
  404:         $lastlog='Forked ';
  405:         $status='Forked';
  406: 
  407:         # unblock signals
  408:         sigprocmask(SIG_UNBLOCK, $sigset)
  409:             or die "Can't unblock SIGINT for fork: $!\n";
  410: 
  411:         $tmpsnum=0;
  412:     
  413:         # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
  414:         for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
  415:             &status('Idle, waiting for connection');
  416:             $client = $server->accept()     or last;
  417:             &status('Accepted connection');
  418: # =============================================================================
  419:             # do something with the connection
  420: # -----------------------------------------------------------------------------
  421:             # see if we know client and check for spoof IP by challenge
  422:             my $caller=getpeername($client);
  423:             my ($port,$iaddr)=unpack_sockaddr_in($caller);
  424:             my $clientip=inet_ntoa($iaddr);
  425:             my $clientrec=($hostid{$clientip} ne undef);
  426:             &logthis(
  427: "<font color=yellow>INFO: Connection $i, $clientip ($hostid{$clientip})</font>"
  428:             );
  429:             &status("Connecting $clientip ($hostid{$clientip})"); 
  430:             my $clientok;
  431:             if ($clientrec) {
  432: 	      &status("Waiting for init from $clientip ($hostid{$clientip})");
  433: 	      my $remotereq=<$client>;
  434:               $remotereq=~s/\W//g;
  435:               if ($remotereq eq 'init') {
  436: 		  my $challenge="$$".time;
  437:                   print $client "$challenge\n";
  438:                   &status(
  439:            "Waiting for challenge reply from $clientip ($hostid{$clientip})"); 
  440:                   $remotereq=<$client>;
  441:                   $remotereq=~s/\W//g;
  442:                   if ($challenge eq $remotereq) {
  443: 		      $clientok=1;
  444:                       print $client "ok\n";
  445:                   } else {
  446: 		      &logthis(
  447:  "<font color=blue>WARNING: $clientip did not reply challenge</font>");
  448:                       print $client "bye\n";
  449:                       &status('No challenge reply '.$clientip);
  450:                   }
  451:               } else {
  452: 		  &logthis(
  453:                     "<font color=blue>WARNING: "
  454:                    ."$clientip failed to initialize: >$remotereq< </font>");
  455: 		  print $client "bye\n";
  456:                   &status('No init '.$clientip);
  457:               }
  458: 	    } else {
  459:               &logthis(
  460:  "<font color=blue>WARNING: Unknown client $clientip</font>");
  461:               print $client "bye\n";
  462:               &status('Hung up on '.$clientip);
  463:             }
  464:             if ($clientok) {
  465: # ---------------- New known client connecting, could mean machine online again
  466: 	      &reconlonc("$perlvar{'lonSockDir'}/$hostid{$clientip}");
  467:               &logthis(
  468:        "<font color=green>Established connection: $hostid{$clientip}</font>");
  469:               &status('Listening to '.$hostid{$clientip});
  470: # ------------------------------------------------------------ Process requests
  471:               while (my $userinput=<$client>) {
  472:                 chomp($userinput);
  473:                 &status('Processing '.$hostid{$clientip}.': '.$userinput);
  474:                 my $wasenc=0;
  475: # ------------------------------------------------------------ See if encrypted
  476: 		if ($userinput =~ /^enc/) {
  477: 		  if ($cipher) {
  478:                     my ($cmd,$cmdlength,$encinput)=split(/:/,$userinput);
  479: 		    $userinput='';
  480:                     for (my $encidx=0;$encidx<length($encinput);$encidx+=16) {
  481:                        $userinput.=
  482: 			   $cipher->decrypt(
  483:                             pack("H16",substr($encinput,$encidx,16))
  484:                            );
  485: 		    }
  486: 		    $userinput=substr($userinput,0,$cmdlength);
  487:                     $wasenc=1;
  488: 		  }
  489: 		}
  490: # ------------------------------------------------------------- Normal commands
  491: # ------------------------------------------------------------------------ ping
  492: 		   if ($userinput =~ /^ping/) {
  493:                        print $client "$perlvar{'lonHostID'}\n";
  494: # ------------------------------------------------------------------------ pong
  495: 		   } elsif ($userinput =~ /^pong/) {
  496:                        $reply=reply("ping",$hostid{$clientip});
  497:                        print $client "$perlvar{'lonHostID'}:$reply\n"; 
  498: # ------------------------------------------------------------------------ ekey
  499: 		   } elsif ($userinput =~ /^ekey/) {
  500:                        my $buildkey=time.$$.int(rand 100000);
  501:                        $buildkey=~tr/1-6/A-F/;
  502:                        $buildkey=int(rand 100000).$buildkey.int(rand 100000);
  503:                        my $key=$perlvar{'lonHostID'}.$hostid{$clientip};
  504:                        $key=~tr/a-z/A-Z/;
  505:                        $key=~tr/G-P/0-9/;
  506:                        $key=~tr/Q-Z/0-9/;
  507:                        $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
  508:                        $key=substr($key,0,32);
  509:                        my $cipherkey=pack("H32",$key);
  510:                        $cipher=new IDEA $cipherkey;
  511:                        print $client "$buildkey\n"; 
  512: # ------------------------------------------------------------------------ load
  513: 		   } elsif ($userinput =~ /^load/) {
  514:                        my $loadavg;
  515:                        {
  516:                           my $loadfile=IO::File->new('/proc/loadavg');
  517:                           $loadavg=<$loadfile>;
  518:                        }
  519:                        $loadavg =~ s/\s.*//g;
  520:                        my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
  521: 		       print $client "$loadpercent\n";
  522: # ----------------------------------------------------------------- currentauth
  523: 		   } elsif ($userinput =~ /^currentauth/) {
  524: 		     if ($wasenc==1) {
  525:                        my ($cmd,$udom,$uname)=split(/:/,$userinput);
  526:                        my $proname=propath($udom,$uname);
  527:                        my $passfilename="$proname/passwd";
  528:                        if (-e $passfilename) {
  529: 			   my $pf = IO::File->new($passfilename);
  530: 			   my $realpasswd=<$pf>;
  531: 			   chomp($realpasswd);
  532: 			   my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
  533: 			   my $availablecontent='';
  534: 			   if ($howpwd eq 'krb4') {
  535: 			       $availablecontent=$contentpwd;
  536: 			   }
  537: 			   print $client "$howpwd:$availablecontent\n";
  538: 		       } else {
  539:                           print $client "unknown_user\n";
  540:                        }
  541: 		     } else {
  542: 		       print $client "refused\n";
  543: 		     }
  544: # ------------------------------------------------------------------------ auth
  545:                    } elsif ($userinput =~ /^auth/) {
  546: 		     if ($wasenc==1) {
  547:                        my ($cmd,$udom,$uname,$upass)=split(/:/,$userinput);
  548:                        chomp($upass);
  549:                        $upass=unescape($upass);
  550:                        my $proname=propath($udom,$uname);
  551:                        my $passfilename="$proname/passwd";
  552:                        if (-e $passfilename) {
  553:                           my $pf = IO::File->new($passfilename);
  554:                           my $realpasswd=<$pf>;
  555:                           chomp($realpasswd);
  556:                           my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
  557:                           my $pwdcorrect=0;
  558:                           if ($howpwd eq 'internal') {
  559: 			      $pwdcorrect=
  560: 				  (crypt($upass,$contentpwd) eq $contentpwd);
  561:                           } elsif ($howpwd eq 'unix') {
  562:                               $contentpwd=(getpwnam($uname))[1];
  563: 			      my $pwauth_path="/usr/local/sbin/pwauth";
  564: 			      unless ($contentpwd eq 'x') {
  565: 				  $pwdcorrect=
  566:                                     (crypt($upass,$contentpwd) eq $contentpwd);
  567: 			      }
  568: 			      elsif (-e $pwauth_path) {
  569: 				  open PWAUTH, "|$pwauth_path" or
  570: 				      die "Cannot invoke authentication";
  571: 				  print PWAUTH "$uname\n$upass\n";
  572: 				  close PWAUTH;
  573: 				  $pwdcorrect=!$?;
  574: 			      }
  575:                           } elsif ($howpwd eq 'krb4') {
  576:                               $pwdcorrect=(
  577:                                  Authen::Krb4::get_pw_in_tkt($uname,"",
  578:                                         $contentpwd,'krbtgt',$contentpwd,1,
  579: 							     $upass) == 0);
  580:                           } elsif ($howpwd eq 'localauth') {
  581: 			    $pwdcorrect=&localauth::localauth($uname,$upass,
  582: 							      $contentpwd);
  583: 			  }
  584:                           if ($pwdcorrect) {
  585:                              print $client "authorized\n";
  586:                           } else {
  587:                              print $client "non_authorized\n";
  588:                           }  
  589: 		       } else {
  590:                           print $client "unknown_user\n";
  591:                        }
  592: 		     } else {
  593: 		       print $client "refused\n";
  594: 		     }
  595: # ---------------------------------------------------------------------- passwd
  596:                    } elsif ($userinput =~ /^passwd/) {
  597: 		     if ($wasenc==1) {
  598:                        my 
  599:                        ($cmd,$udom,$uname,$upass,$npass)=split(/:/,$userinput);
  600:                        chomp($npass);
  601:                        $upass=&unescape($upass);
  602:                        $npass=&unescape($npass);
  603:                        my $proname=propath($udom,$uname);
  604:                        my $passfilename="$proname/passwd";
  605:                        if (-e $passfilename) {
  606: 			   my $realpasswd;
  607:                           { my $pf = IO::File->new($passfilename);
  608: 			    $realpasswd=<$pf>; }
  609:                           chomp($realpasswd);
  610:                           my ($howpwd,$contentpwd)=split(/:/,$realpasswd);
  611:                           if ($howpwd eq 'internal') {
  612: 			   if (crypt($upass,$contentpwd) eq $contentpwd) {
  613: 			     my $salt=time;
  614:                              $salt=substr($salt,6,2);
  615: 			     my $ncpass=crypt($npass,$salt);
  616:                              { my $pf = IO::File->new(">$passfilename");
  617:  	  		       print $pf "internal:$ncpass\n"; }             
  618:                              print $client "ok\n";
  619:                            } else {
  620:                              print $client "non_authorized\n";
  621:                            }
  622:                           } else {
  623:                             print $client "auth_mode_error\n";
  624:                           }  
  625: 		       } else {
  626:                           print $client "unknown_user\n";
  627:                        }
  628: 		     } else {
  629: 		       print $client "refused\n";
  630: 		     }
  631: # -------------------------------------------------------------------- makeuser
  632:                    } elsif ($userinput =~ /^makeuser/) {
  633:     	             my $oldumask=umask(0077);
  634: 		     if ($wasenc==1) {
  635:                        my 
  636:                        ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
  637:                        chomp($npass);
  638:                        $npass=&unescape($npass);
  639:                        my $proname=propath($udom,$uname);
  640:                        my $passfilename="$proname/passwd";
  641:                        if (-e $passfilename) {
  642: 			   print $client "already_exists\n";
  643:                        } elsif ($udom ne $perlvar{'lonDefDomain'}) {
  644:                            print $client "not_right_domain\n";
  645:                        } else {
  646:                            @fpparts=split(/\//,$proname);
  647:                            $fpnow=$fpparts[0].'/'.$fpparts[1].'/'.$fpparts[2];
  648:                            $fperror='';
  649:                            for ($i=3;$i<=$#fpparts;$i++) {
  650:                                $fpnow.='/'.$fpparts[$i]; 
  651:                                unless (-e $fpnow) {
  652: 				   unless (mkdir($fpnow,0777)) {
  653:                                       $fperror="error:$!\n";
  654:                                    }
  655:                                }
  656:                            }
  657:                            unless ($fperror) {
  658: 			     if ($umode eq 'krb4') {
  659:                                { 
  660:                                  my $pf = IO::File->new(">$passfilename");
  661:  	  		         print $pf "krb4:$npass\n"; 
  662:                                }             
  663:                                print $client "ok\n";
  664:                              } elsif ($umode eq 'internal') {
  665: 			       my $salt=time;
  666:                                $salt=substr($salt,6,2);
  667: 			       my $ncpass=crypt($npass,$salt);
  668:                                { 
  669:                                  my $pf = IO::File->new(">$passfilename");
  670:  	  		         print $pf "internal:$ncpass\n"; 
  671:                                }
  672:                                print $client "ok\n";
  673: 			     } elsif ($umode eq 'localauth') {
  674: 			       {
  675: 				 my $pf = IO::File->new(">$passfilename");
  676:   	  		         print $pf "localauth:$npass\n";
  677: 			       }
  678: 			       print $client "ok\n";
  679: 			     } elsif ($umode eq 'unix') {
  680: 			       {
  681: 				 my $execpath="$perlvar{'lonDaemons'}/".
  682: 				              "lcuseradd";
  683: 				 {
  684: 				     my $se = IO::File->new("|$execpath");
  685: 				     print $se "$uname\n";
  686: 				     print $se "$npass\n";
  687: 				     print $se "$npass\n";
  688: 				 }
  689:                                  my $pf = IO::File->new(">$passfilename");
  690:  	  		         print $pf "unix:\n"; 
  691: 			       }
  692: 			       print $client "ok\n";
  693: 			     } elsif ($umode eq 'none') {
  694:                                { 
  695:                                  my $pf = IO::File->new(">$passfilename");
  696:  	  		         print $pf "none:\n"; 
  697:                                }             
  698:                                print $client "ok\n";
  699:                              } else {
  700:                                print $client "auth_mode_error\n";
  701:                              }  
  702:                            } else {
  703:                                print $client "$fperror\n";
  704:                            }
  705:                        }
  706: 		     } else {
  707: 		       print $client "refused\n";
  708: 		     }
  709: 		     umask($oldumask);
  710: # -------------------------------------------------------------- changeuserauth
  711:                    } elsif ($userinput =~ /^changeuserauth/) {
  712: 		     if ($wasenc==1) {
  713:                        my 
  714:                        ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
  715:                        chomp($npass);
  716:                        $npass=&unescape($npass);
  717:                        my $proname=propath($udom,$uname);
  718:                        my $passfilename="$proname/passwd";
  719: 		       if ($udom ne $perlvar{'lonDefDomain'}) {
  720:                            print $client "not_right_domain\n";
  721:                        } else {
  722: 			   if ($umode eq 'krb4') {
  723:                                { 
  724: 				   my $pf = IO::File->new(">$passfilename");
  725: 				   print $pf "krb4:$npass\n"; 
  726:                                }             
  727:                                print $client "ok\n";
  728: 			   } elsif ($umode eq 'internal') {
  729: 			       my $salt=time;
  730:                                $salt=substr($salt,6,2);
  731: 			       my $ncpass=crypt($npass,$salt);
  732:                                { 
  733: 				   my $pf = IO::File->new(">$passfilename");
  734: 				   print $pf "internal:$ncpass\n"; 
  735:                                }
  736:                                print $client "ok\n";
  737: 			   } elsif ($umode eq 'localauth') {
  738: 			       {
  739: 				   my $pf = IO::File->new(">$passfilename");
  740: 				   print $pf "localauth:$npass\n";
  741: 			       }
  742: 			       print $client "ok\n";
  743: 			   } elsif ($umode eq 'unix') {
  744: 			       {
  745: 				   my $execpath="$perlvar{'lonDaemons'}/".
  746: 				       "lcuseradd";
  747: 				   {
  748: 				       my $se = IO::File->new("|$execpath");
  749: 				       print $se "$uname\n";
  750: 				       print $se "$npass\n";
  751: 				       print $se "$npass\n";
  752: 				   }
  753: 				   my $pf = IO::File->new(">$passfilename");
  754: 				   print $pf "unix:\n"; 
  755: 			       }
  756: 			       print $client "ok\n";
  757: 			   } elsif ($umode eq 'none') {
  758:                                { 
  759: 				   my $pf = IO::File->new(">$passfilename");
  760: 				   print $pf "none:\n"; 
  761:                                }             
  762:                                print $client "ok\n";
  763: 			   } else {
  764:                                print $client "auth_mode_error\n";
  765: 			   }  
  766:                        }
  767: 		     } else {
  768: 		       print $client "refused\n";
  769: 		     }
  770: # ------------------------------------------------------------------------ home
  771:                    } elsif ($userinput =~ /^home/) {
  772:                        my ($cmd,$udom,$uname)=split(/:/,$userinput);
  773:                        chomp($uname);
  774:                        my $proname=propath($udom,$uname);
  775:                        if (-e $proname) {
  776:                           print $client "found\n";
  777:                        } else {
  778: 			  print $client "not_found\n";
  779:                        }
  780: # ---------------------------------------------------------------------- update
  781:                    } elsif ($userinput =~ /^update/) {
  782:                        my ($cmd,$fname)=split(/:/,$userinput);
  783:                        my $ownership=ishome($fname);
  784:                        if ($ownership eq 'not_owner') {
  785:                         if (-e $fname) {
  786:                           my ($dev,$ino,$mode,$nlink,
  787:                               $uid,$gid,$rdev,$size,
  788:                               $atime,$mtime,$ctime,
  789:                               $blksize,$blocks)=stat($fname);
  790:                           $now=time;
  791:                           $since=$now-$atime;
  792:                           if ($since>$perlvar{'lonExpire'}) {
  793:                               $reply=
  794:                                     reply("unsub:$fname","$hostid{$clientip}");
  795:                               unlink("$fname");
  796:                           } else {
  797: 			     my $transname="$fname.in.transfer";
  798:                              my $remoteurl=
  799:                                     reply("sub:$fname","$hostid{$clientip}");
  800:                              my $response;
  801:                               {
  802:                              my $ua=new LWP::UserAgent;
  803:                              my $request=new HTTP::Request('GET',"$remoteurl");
  804:                              $response=$ua->request($request,$transname);
  805: 			      }
  806:                              if ($response->is_error()) {
  807: 				 unlink($transname);
  808:                                  my $message=$response->status_line;
  809:                                  &logthis(
  810:                                   "LWP GET: $message for $fname ($remoteurl)");
  811:                              } else {
  812: 	                         if ($remoteurl!~/\.meta$/) {
  813:                                   my $ua=new LWP::UserAgent;
  814:                                   my $mrequest=
  815:                                    new HTTP::Request('GET',$remoteurl.'.meta');
  816:                                   my $mresponse=
  817:                                    $ua->request($mrequest,$fname.'.meta');
  818:                                   if ($mresponse->is_error()) {
  819: 		                    unlink($fname.'.meta');
  820:                                   }
  821: 	                         }
  822:                                  rename($transname,$fname);
  823: 			     }
  824:                           }
  825:                           print $client "ok\n";
  826:                         } else {
  827:                           print $client "not_found\n";
  828:                         }
  829: 		       } else {
  830: 			print $client "rejected\n";
  831:                        }
  832: # ----------------------------------------------------------------- unsubscribe
  833:                    } elsif ($userinput =~ /^unsub/) {
  834:                        my ($cmd,$fname)=split(/:/,$userinput);
  835:                        if (-e $fname) {
  836:                            if (unlink("$fname.$hostid{$clientip}")) {
  837:                               print $client "ok\n";
  838: 			   } else {
  839:                               print $client "not_subscribed\n";
  840: 			   }
  841:                        } else {
  842: 			   print $client "not_found\n";
  843:                        }
  844: # ------------------------------------------------------------------- subscribe
  845:                    } elsif ($userinput =~ /^sub/) {
  846:                        my ($cmd,$fname)=split(/:/,$userinput);
  847:                        my $ownership=ishome($fname);
  848:                        if ($ownership eq 'owner') {
  849:                         if (-e $fname) {
  850: 			 if (-d $fname) {
  851: 			   print $client "directory\n";
  852:                          } else {
  853:                            $now=time;
  854:                            { 
  855: 			    my $sh;
  856:                             if ($sh=
  857:                              IO::File->new(">$fname.$hostid{$clientip}")) {
  858:                                print $sh "$clientip:$now\n";
  859: 			    }
  860: 			   }
  861:                            unless ($fname=~/\.meta$/) {
  862: 			       unlink("$fname.meta.$hostid{$clientip}");
  863:                            }
  864:                            $fname=~s/\/home\/httpd\/html\/res/raw/;
  865:                            $fname="http://$thisserver/".$fname;
  866:                            print $client "$fname\n";
  867: 		         }
  868:                         } else {
  869: 		      	   print $client "not_found\n";
  870:                         }
  871: 		       } else {
  872:                         print $client "rejected\n";
  873: 		       }
  874: # ------------------------------------------------------------------------- log
  875:                    } elsif ($userinput =~ /^log/) {
  876:                        my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
  877:                        chomp($what);
  878:                        my $proname=propath($udom,$uname);
  879:                        my $now=time;
  880:                        {
  881: 			 my $hfh;
  882: 			 if ($hfh=IO::File->new(">>$proname/activity.log")) { 
  883:                             print $hfh "$now:$hostid{$clientip}:$what\n";
  884:                             print $client "ok\n"; 
  885: 			} else {
  886:                             print $client "error:$!\n";
  887: 		        }
  888: 		       }
  889: # ------------------------------------------------------------------------- put
  890:                    } elsif ($userinput =~ /^put/) {
  891:                       my ($cmd,$udom,$uname,$namespace,$what)
  892:                           =split(/:/,$userinput);
  893:                       $namespace=~s/\//\_/g;
  894:                       $namespace=~s/\W//g;
  895:                       if ($namespace ne 'roles') {
  896:                        chomp($what);
  897:                        my $proname=propath($udom,$uname);
  898:                        my $now=time;
  899:                        unless ($namespace=~/^nohist\_/) {
  900: 			   my $hfh;
  901: 			   if (
  902:                              $hfh=IO::File->new(">>$proname/$namespace.hist")
  903: 			       ) { print $hfh "P:$now:$what\n"; }
  904: 		       }
  905:                        my @pairs=split(/\&/,$what);
  906:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
  907:                            foreach $pair (@pairs) {
  908: 			       ($key,$value)=split(/=/,$pair);
  909:                                $hash{$key}=$value;
  910:                            }
  911: 			   if (untie(%hash)) {
  912:                               print $client "ok\n";
  913:                            } else {
  914:                               print $client "error:$!\n";
  915:                            }
  916:                        } else {
  917:                            print $client "error:$!\n";
  918:                        }
  919: 		      } else {
  920:                           print $client "refused\n";
  921:                       }
  922: # -------------------------------------------------------------------- rolesput
  923:                    } elsif ($userinput =~ /^rolesput/) {
  924: 		    if ($wasenc==1) {
  925:                        my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
  926:                           =split(/:/,$userinput);
  927:                        my $namespace='roles';
  928:                        chomp($what);
  929:                        my $proname=propath($udom,$uname);
  930:                        my $now=time;
  931:                        {
  932: 			   my $hfh;
  933: 			   if (
  934:                              $hfh=IO::File->new(">>$proname/$namespace.hist")
  935: 			       ) { 
  936:                                   print $hfh "P:$now:$exedom:$exeuser:$what\n";
  937:                                  }
  938: 		       }
  939:                        my @pairs=split(/\&/,$what);
  940:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
  941:                            foreach $pair (@pairs) {
  942: 			       ($key,$value)=split(/=/,$pair);
  943:                                $hash{$key}=$value;
  944:                            }
  945: 			   if (untie(%hash)) {
  946:                               print $client "ok\n";
  947:                            } else {
  948:                               print $client "error:$!\n";
  949:                            }
  950:                        } else {
  951:                            print $client "error:$!\n";
  952:                        }
  953: 		      } else {
  954:                           print $client "refused\n";
  955:                       }
  956: # ------------------------------------------------------------------------- get
  957:                    } elsif ($userinput =~ /^get/) {
  958:                        my ($cmd,$udom,$uname,$namespace,$what)
  959:                           =split(/:/,$userinput);
  960:                        $namespace=~s/\//\_/g;
  961:                        $namespace=~s/\W//g;
  962:                        chomp($what);
  963:                        my @queries=split(/\&/,$what);
  964:                        my $proname=propath($udom,$uname);
  965:                        my $qresult='';
  966:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
  967:                            for ($i=0;$i<=$#queries;$i++) {
  968:                                $qresult.="$hash{$queries[$i]}&";
  969:                            }
  970: 			   if (untie(%hash)) {
  971: 		              $qresult=~s/\&$//;
  972:                               print $client "$qresult\n";
  973:                            } else {
  974:                               print $client "error:$!\n";
  975:                            }
  976:                        } else {
  977:                            print $client "error:$!\n";
  978:                        }
  979: # ------------------------------------------------------------------------ eget
  980:                    } elsif ($userinput =~ /^eget/) {
  981:                        my ($cmd,$udom,$uname,$namespace,$what)
  982:                           =split(/:/,$userinput);
  983:                        $namespace=~s/\//\_/g;
  984:                        $namespace=~s/\W//g;
  985:                        chomp($what);
  986:                        my @queries=split(/\&/,$what);
  987:                        my $proname=propath($udom,$uname);
  988:                        my $qresult='';
  989:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
  990:                            for ($i=0;$i<=$#queries;$i++) {
  991:                                $qresult.="$hash{$queries[$i]}&";
  992:                            }
  993: 			   if (untie(%hash)) {
  994: 		              $qresult=~s/\&$//;
  995:                               if ($cipher) {
  996:                                 my $cmdlength=length($qresult);
  997:                                 $qresult.="         ";
  998:                                 my $encqresult='';
  999:                                 for 
 1000: 				(my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
 1001:                                  $encqresult.=
 1002:                                  unpack("H16",
 1003:                                  $cipher->encrypt(substr($qresult,$encidx,8)));
 1004:                                 }
 1005:                                 print $client "enc:$cmdlength:$encqresult\n";
 1006: 			      } else {
 1007: 			        print $client "error:no_key\n";
 1008:                               }
 1009:                            } else {
 1010:                               print $client "error:$!\n";
 1011:                            }
 1012:                        } else {
 1013:                            print $client "error:$!\n";
 1014:                        }
 1015: # ------------------------------------------------------------------------- del
 1016:                    } elsif ($userinput =~ /^del/) {
 1017:                        my ($cmd,$udom,$uname,$namespace,$what)
 1018:                           =split(/:/,$userinput);
 1019:                        $namespace=~s/\//\_/g;
 1020:                        $namespace=~s/\W//g;
 1021:                        chomp($what);
 1022:                        my $proname=propath($udom,$uname);
 1023:                        my $now=time;
 1024:                        unless ($namespace=~/^nohist\_/) {
 1025: 			   my $hfh;
 1026: 			   if (
 1027:                              $hfh=IO::File->new(">>$proname/$namespace.hist")
 1028: 			       ) { print $hfh "D:$now:$what\n"; }
 1029: 		       }
 1030:                        my @keys=split(/\&/,$what);
 1031:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
 1032:                            foreach $key (@keys) {
 1033:                                delete($hash{$key});
 1034:                            }
 1035: 			   if (untie(%hash)) {
 1036:                               print $client "ok\n";
 1037:                            } else {
 1038:                               print $client "error:$!\n";
 1039:                            }
 1040:                        } else {
 1041:                            print $client "error:$!\n";
 1042:                        }
 1043: # ------------------------------------------------------------------------ keys
 1044:                    } elsif ($userinput =~ /^keys/) {
 1045:                        my ($cmd,$udom,$uname,$namespace)
 1046:                           =split(/:/,$userinput);
 1047:                        $namespace=~s/\//\_/g;
 1048:                        $namespace=~s/\W//g;
 1049:                        my $proname=propath($udom,$uname);
 1050:                        my $qresult='';
 1051:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
 1052:                            foreach $key (keys %hash) {
 1053:                                $qresult.="$key&";
 1054:                            }
 1055: 			   if (untie(%hash)) {
 1056: 		              $qresult=~s/\&$//;
 1057:                               print $client "$qresult\n";
 1058:                            } else {
 1059:                               print $client "error:$!\n";
 1060:                            }
 1061:                        } else {
 1062:                            print $client "error:$!\n";
 1063:                        }
 1064: # ------------------------------------------------------------------------ dump
 1065:                    } elsif ($userinput =~ /^dump/) {
 1066:                        my ($cmd,$udom,$uname,$namespace)
 1067:                           =split(/:/,$userinput);
 1068:                        $namespace=~s/\//\_/g;
 1069:                        $namespace=~s/\W//g;
 1070:                        my $proname=propath($udom,$uname);
 1071:                        my $qresult='';
 1072:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
 1073:                            foreach $key (keys %hash) {
 1074:                                $qresult.="$key=$hash{$key}&";
 1075:                            }
 1076: 			   if (untie(%hash)) {
 1077: 		              $qresult=~s/\&$//;
 1078:                               print $client "$qresult\n";
 1079:                            } else {
 1080:                               print $client "error:$!\n";
 1081:                            }
 1082:                        } else {
 1083:                            print $client "error:$!\n";
 1084:                        }
 1085: # ----------------------------------------------------------------------- store
 1086:                    } elsif ($userinput =~ /^store/) {
 1087:                       my ($cmd,$udom,$uname,$namespace,$rid,$what)
 1088:                           =split(/:/,$userinput);
 1089:                       $namespace=~s/\//\_/g;
 1090:                       $namespace=~s/\W//g;
 1091:                       if ($namespace ne 'roles') {
 1092:                        chomp($what);
 1093:                        my $proname=propath($udom,$uname);
 1094:                        my $now=time;
 1095:                        unless ($namespace=~/^nohist\_/) {
 1096: 			   my $hfh;
 1097: 			   if (
 1098:                              $hfh=IO::File->new(">>$proname/$namespace.hist")
 1099: 			       ) { print $hfh "P:$now:$rid:$what\n"; }
 1100: 		       }
 1101:                        my @pairs=split(/\&/,$what);
 1102:                          
 1103:     if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
 1104:                            my @previouskeys=split(/&/,$hash{"keys:$rid"});
 1105:                            my $key;
 1106:                            $hash{"version:$rid"}++;
 1107:                            my $version=$hash{"version:$rid"};
 1108:                            my $allkeys=''; 
 1109:                            foreach $pair (@pairs) {
 1110: 			       ($key,$value)=split(/=/,$pair);
 1111:                                $allkeys.=$key.':';
 1112:                                $hash{"$version:$rid:$key"}=$value;
 1113:                            }
 1114:                            $hash{"$version:$rid:timestamp"}=$now;
 1115:                            $allkeys.='timestamp';
 1116:                            $hash{"$version:keys:$rid"}=$allkeys;
 1117: 			   if (untie(%hash)) {
 1118:                               print $client "ok\n";
 1119:                            } else {
 1120:                               print $client "error:$!\n";
 1121:                            }
 1122:                        } else {
 1123:                            print $client "error:$!\n";
 1124:                        }
 1125: 		      } else {
 1126:                           print $client "refused\n";
 1127:                       }
 1128: # --------------------------------------------------------------------- restore
 1129:                    } elsif ($userinput =~ /^restore/) {
 1130:                        my ($cmd,$udom,$uname,$namespace,$rid)
 1131:                           =split(/:/,$userinput);
 1132:                        $namespace=~s/\//\_/g;
 1133:                        $namespace=~s/\W//g;
 1134:                        chomp($rid);
 1135:                        my $proname=propath($udom,$uname);
 1136:                        my $qresult='';
 1137:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
 1138:                 	   my $version=$hash{"version:$rid"};
 1139:                            $qresult.="version=$version&";
 1140:                            my $scope;
 1141:                            for ($scope=1;$scope<=$version;$scope++) {
 1142: 			      my $vkeys=$hash{"$scope:keys:$rid"};
 1143:                               my @keys=split(/:/,$vkeys);
 1144:                               my $key;
 1145:                               $qresult.="$scope:keys=$vkeys&";
 1146:                               foreach $key (@keys) {
 1147: 	     $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
 1148:                               }                                  
 1149:                            }
 1150: 			   if (untie(%hash)) {
 1151: 		              $qresult=~s/\&$//;
 1152:                               print $client "$qresult\n";
 1153:                            } else {
 1154:                               print $client "error:$!\n";
 1155:                            }
 1156:                        } else {
 1157:                            print $client "error:$!\n";
 1158:                        }
 1159: # ------------------------------------------------------------------- querysend
 1160:                    } elsif ($userinput =~ /^querysend/) {
 1161:                        my ($cmd,$query,
 1162: 			   $custom,$customshow)=split(/:/,$userinput);
 1163: 		       $query=~s/\n*$//g;
 1164: 		       unless ($custom or $customshow) {
 1165: 			   print $client "".
 1166: 			       sqlreply("$hostid{$clientip}\&$query")."\n";
 1167: 		       }
 1168: 		       else {
 1169: 			   print $client "".
 1170: 			       sqlreply("$hostid{$clientip}\&$query".
 1171: 					"\&$custom"."\&$customshow")."\n";
 1172: 		       }
 1173: # ------------------------------------------------------------------ queryreply
 1174:                    } elsif ($userinput =~ /^queryreply/) {
 1175:                        my ($cmd,$id,$reply)=split(/:/,$userinput); 
 1176: 		       my $store;
 1177:                        my $execdir=$perlvar{'lonDaemons'};
 1178:                        if ($store=IO::File->new(">$execdir/tmp/$id")) {
 1179: 			   $reply=~s/\&/\n/g;
 1180: 			   print $store $reply;
 1181: 			   close $store;
 1182: 			   my $store2=IO::File->new(">$execdir/tmp/$id.end");
 1183: 			   print $store2 "done\n";
 1184: 			   close $store2;
 1185: 			   print $client "ok\n";
 1186: 		       }
 1187: 		       else {
 1188: 			   print $client "error:$!\n";
 1189: 		       }
 1190: # ----------------------------------------------------------------------- idput
 1191:                    } elsif ($userinput =~ /^idput/) {
 1192:                        my ($cmd,$udom,$what)=split(/:/,$userinput);
 1193:                        chomp($what);
 1194:                        $udom=~s/\W//g;
 1195:                        my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
 1196:                        my $now=time;
 1197:                        {
 1198: 			   my $hfh;
 1199: 			   if (
 1200:                              $hfh=IO::File->new(">>$proname.hist")
 1201: 			       ) { print $hfh "P:$now:$what\n"; }
 1202: 		       }
 1203:                        my @pairs=split(/\&/,$what);
 1204:                  if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
 1205:                            foreach $pair (@pairs) {
 1206: 			       ($key,$value)=split(/=/,$pair);
 1207:                                $hash{$key}=$value;
 1208:                            }
 1209: 			   if (untie(%hash)) {
 1210:                               print $client "ok\n";
 1211:                            } else {
 1212:                               print $client "error:$!\n";
 1213:                            }
 1214:                        } else {
 1215:                            print $client "error:$!\n";
 1216:                        }
 1217: # ----------------------------------------------------------------------- idget
 1218:                    } elsif ($userinput =~ /^idget/) {
 1219:                        my ($cmd,$udom,$what)=split(/:/,$userinput);
 1220:                        chomp($what);
 1221:                        $udom=~s/\W//g;
 1222:                        my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
 1223:                        my @queries=split(/\&/,$what);
 1224:                        my $qresult='';
 1225:                  if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER,0640)) {
 1226:                            for ($i=0;$i<=$#queries;$i++) {
 1227:                                $qresult.="$hash{$queries[$i]}&";
 1228:                            }
 1229: 			   if (untie(%hash)) {
 1230: 		              $qresult=~s/\&$//;
 1231:                               print $client "$qresult\n";
 1232:                            } else {
 1233:                               print $client "error:$!\n";
 1234:                            }
 1235:                        } else {
 1236:                            print $client "error:$!\n";
 1237:                        }
 1238: # ---------------------------------------------------------------------- tmpput
 1239:                    } elsif ($userinput =~ /^tmpput/) {
 1240:                        my ($cmd,$what)=split(/:/,$userinput);
 1241: 		       my $store;
 1242:                        $tmpsnum++;
 1243:                        my $id=$$.'_'.$clientip.'_'.$tmpsnum;
 1244:                        $id=~s/\W/\_/g;
 1245:                        $what=~s/\n//g;
 1246:                        my $execdir=$perlvar{'lonDaemons'};
 1247:                        if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
 1248: 			   print $store $what;
 1249: 			   close $store;
 1250: 			   print $client "$id\n";
 1251: 		       }
 1252: 		       else {
 1253: 			   print $client "error:$!\n";
 1254: 		       }
 1255: 
 1256: # ---------------------------------------------------------------------- tmpget
 1257:                    } elsif ($userinput =~ /^tmpget/) {
 1258:                        my ($cmd,$id)=split(/:/,$userinput);
 1259:                        chomp($id);
 1260:                        $id=~s/\W/\_/g;
 1261:                        my $store;
 1262:                        my $execdir=$perlvar{'lonDaemons'};
 1263:                        if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
 1264:                            my $reply=<$store>;
 1265: 			   print $client "$reply\n";
 1266:                            close $store;
 1267: 		       }
 1268: 		       else {
 1269: 			   print $client "error:$!\n";
 1270: 		       }
 1271: 
 1272: # -------------------------------------------------------------------------- ls
 1273:                    } elsif ($userinput =~ /^ls/) {
 1274:                        my ($cmd,$ulsdir)=split(/:/,$userinput);
 1275:                        my $ulsout='';
 1276:                        my $ulsfn;
 1277:                        if (-e $ulsdir) {
 1278: 			if (opendir(LSDIR,$ulsdir)) {
 1279:                           while ($ulsfn=readdir(LSDIR)) {
 1280: 			     my @ulsstats=stat($ulsdir.'/'.$ulsfn);
 1281:                              $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
 1282:                           }
 1283:                           closedir(LSDIR);
 1284: 		        }
 1285: 		       } else {
 1286:                           $ulsout='no_such_dir';
 1287:                        }
 1288:                        if ($ulsout eq '') { $ulsout='empty'; }
 1289:                        print $client "$ulsout\n";
 1290: # ------------------------------------------------------------------ Hanging up
 1291:                    } elsif (($userinput =~ /^exit/) ||
 1292:                             ($userinput =~ /^init/)) {
 1293:                        &logthis(
 1294:       "Client $clientip ($hostid{$clientip}) hanging up: $userinput");
 1295:                        print $client "bye\n";
 1296: 		       last;
 1297: # ------------------------------------------------------------- unknown command
 1298:                    } else {
 1299:                        # unknown command
 1300:                        print $client "unknown_cmd\n";
 1301:                    }
 1302: # ------------------------------------------------------ client unknown, refuse
 1303: 	       }
 1304:             } else {
 1305: 	        print $client "refused\n";
 1306:                 &logthis("<font color=blue>WARNING: "
 1307:                 ."Rejected client $clientip, closing connection</font>");
 1308:             }              
 1309:             &logthis("<font color=red>CRITICAL: "
 1310:                     ."Disconnect from $clientip ($hostid{$clientip})</font>");
 1311: # =============================================================================
 1312:         }
 1313:     
 1314:         # tidy up gracefully and finish
 1315:     
 1316:         # this exit is VERY important, otherwise the child will become
 1317:         # a producer of more and more children, forking yourself into
 1318:         # process death.
 1319:         exit;
 1320:     }
 1321: }
 1322: 
 1323: 
 1324: 
 1325: 
 1326: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>