File:  [LON-CAPA] / loncom / lond
Revision 1.59: download - view: text, annotated - select for diffs
Tue Nov 27 21:59:07 2001 UTC (22 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Trying to close up sockets

    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,11/27 Gerd Kortemeyer
   21: #
   22: # $Id: lond,v 1.59 2001/11/27 21:59:07 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:     $server->close();
   57:     die($error);
   58: }
   59: 
   60: # -------------------------------- Set signal handlers to record abnormal exits
   61: 
   62: $SIG{'QUIT'}=\&catchexception;
   63: $SIG{__DIE__}=\&catchexception;
   64: 
   65: # ------------------------------------ Read httpd access.conf and get variables
   66: 
   67: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
   68: 
   69: while ($configline=<CONFIG>) {
   70:     if ($configline =~ /PerlSetVar/) {
   71: 	my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
   72:         chomp($varvalue);
   73:         $perlvar{$varname}=$varvalue;
   74:     }
   75: }
   76: close(CONFIG);
   77: 
   78: # ----------------------------- Make sure this process is running from user=www
   79: my $wwwid=getpwnam('www');
   80: if ($wwwid!=$<) {
   81:    $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
   82:    $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
   83:    system("echo 'User ID mismatch.  lond must be run as user www.' |\
   84:  mailto $emailto -s '$subj' > /dev/null");
   85:    exit 1;
   86: }
   87: 
   88: # --------------------------------------------- Check if other instance running
   89: 
   90: my $pidfile="$perlvar{'lonDaemons'}/logs/lond.pid";
   91: 
   92: if (-e $pidfile) {
   93:    my $lfh=IO::File->new("$pidfile");
   94:    my $pide=<$lfh>;
   95:    chomp($pide);
   96:    if (kill 0 => $pide) { die "already running"; }
   97: }
   98: 
   99: $PREFORK=4; # number of children to maintain, at least four spare
  100: 
  101: # ------------------------------------------------------------- Read hosts file
  102: 
  103: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
  104: 
  105: while ($configline=<CONFIG>) {
  106:     my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
  107:     chomp($ip);
  108:     $hostid{$ip}=$id;
  109:     if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
  110:     $PREFORK++;
  111: }
  112: close(CONFIG);
  113: 
  114: # establish SERVER socket, bind and listen.
  115: $server = IO::Socket::INET->new(LocalPort => $perlvar{'londPort'},
  116:                                 Type      => SOCK_STREAM,
  117:                                 Proto     => 'tcp',
  118:                                 Reuse     => 1,
  119:                                 Listen    => 10 )
  120:   or die "making socket: $@\n";
  121: 
  122: # --------------------------------------------------------- Do global variables
  123: 
  124: # global variables
  125: 
  126: $MAX_CLIENTS_PER_CHILD  = 5;        # number of clients each child should 
  127:                                     # process
  128: %children               = ();       # keys are current child process IDs
  129: $children               = 0;        # current number of children
  130: 
  131: sub REAPER {                        # takes care of dead children
  132:     $SIG{CHLD} = \&REAPER;
  133:     my $pid = wait;
  134:     $children --;
  135:     &logthis("Child $pid died");
  136:     delete $children{$pid};
  137: }
  138: 
  139: sub HUNTSMAN {                      # signal handler for SIGINT
  140:     local($SIG{CHLD}) = 'IGNORE';   # we're going to kill our children
  141:     kill 'INT' => keys %children;
  142:     &logthis("Free socket: ".shutdown($server,2)); # free up socket
  143:     my $execdir=$perlvar{'lonDaemons'};
  144:     unlink("$execdir/logs/lond.pid");
  145:     &logthis("<font color=red>CRITICAL: Shutting down</font>");
  146:     exit;                           # clean up with dignity
  147: }
  148: 
  149: sub HUPSMAN {                      # signal handler for SIGHUP
  150:     local($SIG{CHLD}) = 'IGNORE';  # we're going to kill our children
  151:     kill 'INT' => keys %children;
  152:     &logthis("Free socket: ".shutdown($server,2)); # free up socket
  153:     &logthis("<font color=red>CRITICAL: Restarting</font>");
  154:     unlink("$execdir/logs/lond.pid");
  155:     my $execdir=$perlvar{'lonDaemons'};
  156:     exec("$execdir/lond");         # here we go again
  157: }
  158: 
  159: sub checkchildren {
  160:     &initnewstatus();
  161:     &logstatus();
  162:     &logthis('Going to check on the children');
  163:     map {
  164: 	sleep 1;
  165:         unless (kill 'USR1' => $_) {
  166: 	    &logthis ('Child '.$_.' is dead');
  167:             &logstatus($$.' is dead');
  168:         } 
  169:     } sort keys %children;
  170: }
  171: 
  172: # --------------------------------------------------------------------- Logging
  173: 
  174: sub logthis {
  175:     my $message=shift;
  176:     my $execdir=$perlvar{'lonDaemons'};
  177:     my $fh=IO::File->new(">>$execdir/logs/lond.log");
  178:     my $now=time;
  179:     my $local=localtime($now);
  180:     $lastlog=$local.': '.$message;
  181:     print $fh "$local ($$): $message\n";
  182: }
  183: 
  184: # ------------------------------------------------------------------ Log status
  185: 
  186: sub logstatus {
  187:     my $docdir=$perlvar{'lonDocRoot'};
  188:     my $fh=IO::File->new(">>$docdir/lon-status/londstatus.txt");
  189:     print $fh $$."\t".$status."\t".$lastlog."\n";
  190: }
  191: 
  192: sub initnewstatus {
  193:     my $docdir=$perlvar{'lonDocRoot'};
  194:     my $fh=IO::File->new(">$docdir/lon-status/londstatus.txt");
  195:     my $now=time;
  196:     my $local=localtime($now);
  197:     print $fh "LOND status $local - parent $$\n\n";
  198: }
  199: 
  200: # -------------------------------------------------------------- Status setting
  201: 
  202: sub status {
  203:     my $what=shift;
  204:     my $now=time;
  205:     my $local=localtime($now);
  206:     $status=$local.': '.$what;
  207: }
  208: 
  209: # -------------------------------------------------------- Escape Special Chars
  210: 
  211: sub escape {
  212:     my $str=shift;
  213:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
  214:     return $str;
  215: }
  216: 
  217: # ----------------------------------------------------- Un-Escape Special Chars
  218: 
  219: sub unescape {
  220:     my $str=shift;
  221:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  222:     return $str;
  223: }
  224: 
  225: # ----------------------------------------------------------- Send USR1 to lonc
  226: 
  227: sub reconlonc {
  228:     my $peerfile=shift;
  229:     &logthis("Trying to reconnect for $peerfile");
  230:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  231:     if (my $fh=IO::File->new("$loncfile")) {
  232: 	my $loncpid=<$fh>;
  233:         chomp($loncpid);
  234:         if (kill 0 => $loncpid) {
  235: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  236:             kill USR1 => $loncpid;
  237:             sleep 1;
  238:             if (-e "$peerfile") { return; }
  239:             &logthis("$peerfile still not there, give it another try");
  240:             sleep 5;
  241:             if (-e "$peerfile") { return; }
  242:             &logthis(
  243:  "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
  244:         } else {
  245: 	    &logthis(
  246:               "<font color=red>CRITICAL: "
  247:              ."lonc at pid $loncpid not responding, giving up</font>");
  248:         }
  249:     } else {
  250:       &logthis('<font color=red>CRITICAL: lonc not running, giving up</font>');
  251:     }
  252: }
  253: 
  254: # -------------------------------------------------- Non-critical communication
  255: 
  256: sub subreply {
  257:     my ($cmd,$server)=@_;
  258:     my $peerfile="$perlvar{'lonSockDir'}/$server";
  259:     my $sclient=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  260:                                       Type    => SOCK_STREAM,
  261:                                       Timeout => 10)
  262:        or return "con_lost";
  263:     print $sclient "$cmd\n";
  264:     my $answer=<$sclient>;
  265:     chomp($answer);
  266:     if (!$answer) { $answer="con_lost"; }
  267:     return $answer;
  268: }
  269: 
  270: sub reply {
  271:   my ($cmd,$server)=@_;
  272:   my $answer;
  273:   if ($server ne $perlvar{'lonHostID'}) { 
  274:     $answer=subreply($cmd,$server);
  275:     if ($answer eq 'con_lost') {
  276: 	$answer=subreply("ping",$server);
  277:         if ($answer ne $server) {
  278:            &reconlonc("$perlvar{'lonSockDir'}/$server");
  279:         }
  280:         $answer=subreply($cmd,$server);
  281:     }
  282:   } else {
  283:     $answer='self_reply';
  284:   } 
  285:   return $answer;
  286: }
  287: 
  288: # -------------------------------------------------------------- Talk to lonsql
  289: 
  290: sub sqlreply {
  291:     my ($cmd)=@_;
  292:     my $answer=subsqlreply($cmd);
  293:     if ($answer eq 'con_lost') { $answer=subsqlreply($cmd); }
  294:     return $answer;
  295: }
  296: 
  297: sub subsqlreply {
  298:     my ($cmd)=@_;
  299:     my $unixsock="mysqlsock";
  300:     my $peerfile="$perlvar{'lonSockDir'}/$unixsock";
  301:     my $sclient=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  302:                                       Type    => SOCK_STREAM,
  303:                                       Timeout => 10)
  304:        or return "con_lost";
  305:     print $sclient "$cmd\n";
  306:     my $answer=<$sclient>;
  307:     chomp($answer);
  308:     if (!$answer) { $answer="con_lost"; }
  309:     return $answer;
  310: }
  311: 
  312: # -------------------------------------------- Return path to profile directory
  313: 
  314: sub propath {
  315:     my ($udom,$uname)=@_;
  316:     $udom=~s/\W//g;
  317:     $uname=~s/\W//g;
  318:     my $subdir=$uname.'__';
  319:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
  320:     my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
  321:     return $proname;
  322: } 
  323: 
  324: # --------------------------------------- Is this the home server of an author?
  325: 
  326: sub ishome {
  327:     my $author=shift;
  328:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
  329:     my ($udom,$uname)=split(/\//,$author);
  330:     my $proname=propath($udom,$uname);
  331:     if (-e $proname) {
  332: 	return 'owner';
  333:     } else {
  334:         return 'not_owner';
  335:     }
  336: }
  337: 
  338: # ======================================================= Continue main program
  339: # ---------------------------------------------------- Fork once and dissociate
  340: 
  341: $fpid=fork;
  342: exit if $fpid;
  343: die "Couldn't fork: $!" unless defined ($fpid);
  344: 
  345: POSIX::setsid() or die "Can't start new session: $!";
  346: 
  347: # ------------------------------------------------------- Write our PID on disk
  348: 
  349: $execdir=$perlvar{'lonDaemons'};
  350: open (PIDSAVE,">$execdir/logs/lond.pid");
  351: print PIDSAVE "$$\n";
  352: close(PIDSAVE);
  353: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
  354: &status('Starting');
  355: 
  356: # ------------------------------------------------------- Now we are on our own
  357:     
  358: # Fork off our children.
  359: for (1 .. $PREFORK) {
  360:     make_new_child();
  361: }
  362: 
  363: # ----------------------------------------------------- Install signal handlers
  364: 
  365: &status('Forked children');
  366: 
  367: $SIG{CHLD} = \&REAPER;
  368: $SIG{INT}  = $SIG{TERM} = \&HUNTSMAN;
  369: $SIG{HUP}  = \&HUPSMAN;
  370: $SIG{USR1} = \&checkchildren;
  371: 
  372: # And maintain the population.
  373: while (1) {
  374:     &status('Sleeping');
  375:     sleep;                          # wait for a signal (i.e., child's death)
  376:     &logthis('Woke up');
  377:     &status('Woke up');
  378:     for ($i = $children; $i < $PREFORK; $i++) {
  379:         make_new_child();           # top up the child pool
  380:     }
  381: }
  382: 
  383: sub make_new_child {
  384:     my $pid;
  385:     my $cipher;
  386:     my $sigset;
  387:     &logthis("Attempting to start child");    
  388:     # block signal for fork
  389:     $sigset = POSIX::SigSet->new(SIGINT);
  390:     sigprocmask(SIG_BLOCK, $sigset)
  391:         or die "Can't block SIGINT for fork: $!\n";
  392:     
  393:     die "fork: $!" unless defined ($pid = fork);
  394:     
  395:     if ($pid) {
  396:         # Parent records the child's birth and returns.
  397:         sigprocmask(SIG_UNBLOCK, $sigset)
  398:             or die "Can't unblock SIGINT for fork: $!\n";
  399:         $children{$pid} = 1;
  400:         $children++;
  401:         &status('Started child '.$pid);
  402:         return;
  403:     } else {
  404:         # Child can *not* return from this subroutine.
  405:         $SIG{INT} = 'DEFAULT';      # make SIGINT kill us as it did before
  406:         $SIG{USR1}= \&logstatus;
  407:         $lastlog='Forked ';
  408:         $status='Forked';
  409: 
  410:         # unblock signals
  411:         sigprocmask(SIG_UNBLOCK, $sigset)
  412:             or die "Can't unblock SIGINT for fork: $!\n";
  413: 
  414:         $tmpsnum=0;
  415:     
  416:         # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
  417:         for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
  418:             &status('Idle, waiting for connection');
  419:             $client = $server->accept()     or last;
  420:             &status('Accepted connection');
  421: # =============================================================================
  422:             # do something with the connection
  423: # -----------------------------------------------------------------------------
  424:             # see if we know client and check for spoof IP by challenge
  425:             my $caller=getpeername($client);
  426:             my ($port,$iaddr)=unpack_sockaddr_in($caller);
  427:             my $clientip=inet_ntoa($iaddr);
  428:             my $clientrec=($hostid{$clientip} ne undef);
  429:             &logthis(
  430: "<font color=yellow>INFO: Connection $i, $clientip ($hostid{$clientip})</font>"
  431:             );
  432:             &status("Connecting $clientip ($hostid{$clientip})"); 
  433:             my $clientok;
  434:             if ($clientrec) {
  435: 	      &status("Waiting for init from $clientip ($hostid{$clientip})");
  436: 	      my $remotereq=<$client>;
  437:               $remotereq=~s/\W//g;
  438:               if ($remotereq eq 'init') {
  439: 		  my $challenge="$$".time;
  440:                   print $client "$challenge\n";
  441:                   &status(
  442:            "Waiting for challenge reply from $clientip ($hostid{$clientip})"); 
  443:                   $remotereq=<$client>;
  444:                   $remotereq=~s/\W//g;
  445:                   if ($challenge eq $remotereq) {
  446: 		      $clientok=1;
  447:                       print $client "ok\n";
  448:                   } else {
  449: 		      &logthis(
  450:  "<font color=blue>WARNING: $clientip did not reply challenge</font>");
  451:                       &status('No challenge reply '.$clientip);
  452:                   }
  453:               } else {
  454: 		  &logthis(
  455:                     "<font color=blue>WARNING: "
  456:                    ."$clientip failed to initialize: >$remotereq< </font>");
  457:                   &status('No init '.$clientip);
  458:               }
  459: 	    } else {
  460:               &logthis(
  461:  "<font color=blue>WARNING: Unknown client $clientip</font>");
  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('Will listen 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:                        $client->close();
 1297: 		       last;
 1298: # ------------------------------------------------------------- unknown command
 1299:                    } else {
 1300:                        # unknown command
 1301:                        print $client "unknown_cmd\n";
 1302:                    }
 1303: # -------------------------------------------------------------------- complete
 1304:                    &status('Listening to '.$hostid{$clientip});
 1305: 	       }
 1306: # --------------------------------------------- client unknown or fishy, refuse
 1307:             } else {
 1308: 	        print $client "refused\n";
 1309:                 $client->close();
 1310:                 &logthis("<font color=blue>WARNING: "
 1311:                 ."Rejected client $clientip, closing connection</font>");
 1312:             }              
 1313:             &logthis("<font color=red>CRITICAL: "
 1314:                     ."Disconnect from $clientip ($hostid{$clientip})</font>");
 1315: # =============================================================================
 1316:         }
 1317:     
 1318:         # tidy up gracefully and finish
 1319:     
 1320:         $client->close();
 1321:         $server->close();
 1322: 
 1323:         # this exit is VERY important, otherwise the child will become
 1324:         # a producer of more and more children, forking yourself into
 1325:         # process death.
 1326:         exit;
 1327:     }
 1328: }
 1329: 
 1330: 
 1331: 
 1332: 
 1333: 

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