File:  [LON-CAPA] / loncom / lond
Revision 1.56: download - view: text, annotated - select for diffs
Fri Nov 16 16:26:01 2001 UTC (22 years, 5 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
umask to protect contents of lonUsers/*/passwd -Scott

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

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