File:  [LON-CAPA] / loncom / lond
Revision 1.55: download - view: text, annotated - select for diffs
Fri Nov 16 06:19:33 2001 UTC (22 years, 5 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
changeuserauth implemented

    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 Scott Harrison
   20: #
   21: # $Id: lond,v 1.55 2001/11/16 06:19:33 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: 		     if ($wasenc==1) {
  571:                        my 
  572:                        ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
  573:                        chomp($npass);
  574:                        $npass=&unescape($npass);
  575:                        my $proname=propath($udom,$uname);
  576:                        my $passfilename="$proname/passwd";
  577:                        if (-e $passfilename) {
  578: 			   print $client "already_exists\n";
  579:                        } elsif ($udom ne $perlvar{'lonDefDomain'}) {
  580:                            print $client "not_right_domain\n";
  581:                        } else {
  582:                            @fpparts=split(/\//,$proname);
  583:                            $fpnow=$fpparts[0].'/'.$fpparts[1].'/'.$fpparts[2];
  584:                            $fperror='';
  585:                            for ($i=3;$i<=$#fpparts;$i++) {
  586:                                $fpnow.='/'.$fpparts[$i]; 
  587:                                unless (-e $fpnow) {
  588: 				   unless (mkdir($fpnow,0777)) {
  589:                                       $fperror="error:$!\n";
  590:                                    }
  591:                                }
  592:                            }
  593:                            unless ($fperror) {
  594: 			     if ($umode eq 'krb4') {
  595:                                { 
  596:                                  my $pf = IO::File->new(">$passfilename");
  597:  	  		         print $pf "krb4:$npass\n"; 
  598:                                }             
  599:                                print $client "ok\n";
  600:                              } elsif ($umode eq 'internal') {
  601: 			       my $salt=time;
  602:                                $salt=substr($salt,6,2);
  603: 			       my $ncpass=crypt($npass,$salt);
  604:                                { 
  605:                                  my $pf = IO::File->new(">$passfilename");
  606:  	  		         print $pf "internal:$ncpass\n"; 
  607:                                }
  608:                                print $client "ok\n";
  609: 			     } elsif ($umode eq 'localauth') {
  610: 			       {
  611: 				 my $pf = IO::File->new(">$passfilename");
  612:   	  		         print $pf "localauth:$npass\n";
  613: 			       }
  614: 			       print $client "ok\n";
  615: 			     } elsif ($umode eq 'unix') {
  616: 			       {
  617: 				 my $execpath="$perlvar{'lonDaemons'}/".
  618: 				              "lcuseradd";
  619: 				 {
  620: 				     my $se = IO::File->new("|$execpath");
  621: 				     print $se "$uname\n";
  622: 				     print $se "$npass\n";
  623: 				     print $se "$npass\n";
  624: 				 }
  625:                                  my $pf = IO::File->new(">$passfilename");
  626:  	  		         print $pf "unix:\n"; 
  627: 			       }
  628: 			       print $client "ok\n";
  629: 			     } elsif ($umode eq 'none') {
  630:                                { 
  631:                                  my $pf = IO::File->new(">$passfilename");
  632:  	  		         print $pf "none:\n"; 
  633:                                }             
  634:                                print $client "ok\n";
  635:                              } else {
  636:                                print $client "auth_mode_error\n";
  637:                              }  
  638:                            } else {
  639:                                print $client "$fperror\n";
  640:                            }
  641:                        }
  642: 		     } else {
  643: 		       print $client "refused\n";
  644: 		     }
  645: # -------------------------------------------------------------- changeuserauth
  646:                    } elsif ($userinput =~ /^changeuserauth/) {
  647: 		     if ($wasenc==1) {
  648:                        my 
  649:                        ($cmd,$udom,$uname,$umode,$npass)=split(/:/,$userinput);
  650:                        chomp($npass);
  651:                        $npass=&unescape($npass);
  652:                        my $proname=propath($udom,$uname);
  653:                        my $passfilename="$proname/passwd";
  654: 		       if ($udom ne $perlvar{'lonDefDomain'}) {
  655:                            print $client "not_right_domain\n";
  656:                        } else {
  657: 			   if ($umode eq 'krb4') {
  658:                                { 
  659: 				   my $pf = IO::File->new(">$passfilename");
  660: 				   print $pf "krb4:$npass\n"; 
  661:                                }             
  662:                                print $client "ok\n";
  663: 			   } elsif ($umode eq 'internal') {
  664: 			       my $salt=time;
  665:                                $salt=substr($salt,6,2);
  666: 			       my $ncpass=crypt($npass,$salt);
  667:                                { 
  668: 				   my $pf = IO::File->new(">$passfilename");
  669: 				   print $pf "internal:$ncpass\n"; 
  670:                                }
  671:                                print $client "ok\n";
  672: 			   } elsif ($umode eq 'localauth') {
  673: 			       {
  674: 				   my $pf = IO::File->new(">$passfilename");
  675: 				   print $pf "localauth:$npass\n";
  676: 			       }
  677: 			       print $client "ok\n";
  678: 			   } elsif ($umode eq 'unix') {
  679: 			       {
  680: 				   my $execpath="$perlvar{'lonDaemons'}/".
  681: 				       "lcuseradd";
  682: 				   {
  683: 				       my $se = IO::File->new("|$execpath");
  684: 				       print $se "$uname\n";
  685: 				       print $se "$npass\n";
  686: 				       print $se "$npass\n";
  687: 				   }
  688: 				   my $pf = IO::File->new(">$passfilename");
  689: 				   print $pf "unix:\n"; 
  690: 			       }
  691: 			       print $client "ok\n";
  692: 			   } elsif ($umode eq 'none') {
  693:                                { 
  694: 				   my $pf = IO::File->new(">$passfilename");
  695: 				   print $pf "none:\n"; 
  696:                                }             
  697:                                print $client "ok\n";
  698: 			   } else {
  699:                                print $client "auth_mode_error\n";
  700: 			   }  
  701:                        }
  702: 		     } else {
  703: 		       print $client "refused\n";
  704: 		     }
  705: # ------------------------------------------------------------------------ home
  706:                    } elsif ($userinput =~ /^home/) {
  707:                        my ($cmd,$udom,$uname)=split(/:/,$userinput);
  708:                        chomp($uname);
  709:                        my $proname=propath($udom,$uname);
  710:                        if (-e $proname) {
  711:                           print $client "found\n";
  712:                        } else {
  713: 			  print $client "not_found\n";
  714:                        }
  715: # ---------------------------------------------------------------------- update
  716:                    } elsif ($userinput =~ /^update/) {
  717:                        my ($cmd,$fname)=split(/:/,$userinput);
  718:                        my $ownership=ishome($fname);
  719:                        if ($ownership eq 'not_owner') {
  720:                         if (-e $fname) {
  721:                           my ($dev,$ino,$mode,$nlink,
  722:                               $uid,$gid,$rdev,$size,
  723:                               $atime,$mtime,$ctime,
  724:                               $blksize,$blocks)=stat($fname);
  725:                           $now=time;
  726:                           $since=$now-$atime;
  727:                           if ($since>$perlvar{'lonExpire'}) {
  728:                               $reply=
  729:                                     reply("unsub:$fname","$hostid{$clientip}");
  730:                               unlink("$fname");
  731:                           } else {
  732: 			     my $transname="$fname.in.transfer";
  733:                              my $remoteurl=
  734:                                     reply("sub:$fname","$hostid{$clientip}");
  735:                              my $response;
  736:                               {
  737:                              my $ua=new LWP::UserAgent;
  738:                              my $request=new HTTP::Request('GET',"$remoteurl");
  739:                              $response=$ua->request($request,$transname);
  740: 			      }
  741:                              if ($response->is_error()) {
  742: 				 unlink($transname);
  743:                                  my $message=$response->status_line;
  744:                                  &logthis(
  745:                                   "LWP GET: $message for $fname ($remoteurl)");
  746:                              } else {
  747: 	                         if ($remoteurl!~/\.meta$/) {
  748:                                   my $ua=new LWP::UserAgent;
  749:                                   my $mrequest=
  750:                                    new HTTP::Request('GET',$remoteurl.'.meta');
  751:                                   my $mresponse=
  752:                                    $ua->request($mrequest,$fname.'.meta');
  753:                                   if ($mresponse->is_error()) {
  754: 		                    unlink($fname.'.meta');
  755:                                   }
  756: 	                         }
  757:                                  rename($transname,$fname);
  758: 			     }
  759:                           }
  760:                           print $client "ok\n";
  761:                         } else {
  762:                           print $client "not_found\n";
  763:                         }
  764: 		       } else {
  765: 			print $client "rejected\n";
  766:                        }
  767: # ----------------------------------------------------------------- unsubscribe
  768:                    } elsif ($userinput =~ /^unsub/) {
  769:                        my ($cmd,$fname)=split(/:/,$userinput);
  770:                        if (-e $fname) {
  771:                            if (unlink("$fname.$hostid{$clientip}")) {
  772:                               print $client "ok\n";
  773: 			   } else {
  774:                               print $client "not_subscribed\n";
  775: 			   }
  776:                        } else {
  777: 			   print $client "not_found\n";
  778:                        }
  779: # ------------------------------------------------------------------- subscribe
  780:                    } elsif ($userinput =~ /^sub/) {
  781:                        my ($cmd,$fname)=split(/:/,$userinput);
  782:                        my $ownership=ishome($fname);
  783:                        if ($ownership eq 'owner') {
  784:                         if (-e $fname) {
  785: 			 if (-d $fname) {
  786: 			   print $client "directory\n";
  787:                          } else {
  788:                            $now=time;
  789:                            { 
  790: 			    my $sh;
  791:                             if ($sh=
  792:                              IO::File->new(">$fname.$hostid{$clientip}")) {
  793:                                print $sh "$clientip:$now\n";
  794: 			    }
  795: 			   }
  796:                            unless ($fname=~/\.meta$/) {
  797: 			       unlink("$fname.meta.$hostid{$clientip}");
  798:                            }
  799:                            $fname=~s/\/home\/httpd\/html\/res/raw/;
  800:                            $fname="http://$thisserver/".$fname;
  801:                            print $client "$fname\n";
  802: 		         }
  803:                         } else {
  804: 		      	   print $client "not_found\n";
  805:                         }
  806: 		       } else {
  807:                         print $client "rejected\n";
  808: 		       }
  809: # ------------------------------------------------------------------------- log
  810:                    } elsif ($userinput =~ /^log/) {
  811:                        my ($cmd,$udom,$uname,$what)=split(/:/,$userinput);
  812:                        chomp($what);
  813:                        my $proname=propath($udom,$uname);
  814:                        my $now=time;
  815:                        {
  816: 			 my $hfh;
  817: 			 if ($hfh=IO::File->new(">>$proname/activity.log")) { 
  818:                             print $hfh "$now:$hostid{$clientip}:$what\n";
  819:                             print $client "ok\n"; 
  820: 			} else {
  821:                             print $client "error:$!\n";
  822: 		        }
  823: 		       }
  824: # ------------------------------------------------------------------------- put
  825:                    } elsif ($userinput =~ /^put/) {
  826:                       my ($cmd,$udom,$uname,$namespace,$what)
  827:                           =split(/:/,$userinput);
  828:                       $namespace=~s/\//\_/g;
  829:                       $namespace=~s/\W//g;
  830:                       if ($namespace ne 'roles') {
  831:                        chomp($what);
  832:                        my $proname=propath($udom,$uname);
  833:                        my $now=time;
  834:                        unless ($namespace=~/^nohist\_/) {
  835: 			   my $hfh;
  836: 			   if (
  837:                              $hfh=IO::File->new(">>$proname/$namespace.hist")
  838: 			       ) { print $hfh "P:$now:$what\n"; }
  839: 		       }
  840:                        my @pairs=split(/\&/,$what);
  841:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
  842:                            foreach $pair (@pairs) {
  843: 			       ($key,$value)=split(/=/,$pair);
  844:                                $hash{$key}=$value;
  845:                            }
  846: 			   if (untie(%hash)) {
  847:                               print $client "ok\n";
  848:                            } else {
  849:                               print $client "error:$!\n";
  850:                            }
  851:                        } else {
  852:                            print $client "error:$!\n";
  853:                        }
  854: 		      } else {
  855:                           print $client "refused\n";
  856:                       }
  857: # -------------------------------------------------------------------- rolesput
  858:                    } elsif ($userinput =~ /^rolesput/) {
  859: 		    if ($wasenc==1) {
  860:                        my ($cmd,$exedom,$exeuser,$udom,$uname,$what)
  861:                           =split(/:/,$userinput);
  862:                        my $namespace='roles';
  863:                        chomp($what);
  864:                        my $proname=propath($udom,$uname);
  865:                        my $now=time;
  866:                        {
  867: 			   my $hfh;
  868: 			   if (
  869:                              $hfh=IO::File->new(">>$proname/$namespace.hist")
  870: 			       ) { 
  871:                                   print $hfh "P:$now:$exedom:$exeuser:$what\n";
  872:                                  }
  873: 		       }
  874:                        my @pairs=split(/\&/,$what);
  875:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
  876:                            foreach $pair (@pairs) {
  877: 			       ($key,$value)=split(/=/,$pair);
  878:                                $hash{$key}=$value;
  879:                            }
  880: 			   if (untie(%hash)) {
  881:                               print $client "ok\n";
  882:                            } else {
  883:                               print $client "error:$!\n";
  884:                            }
  885:                        } else {
  886:                            print $client "error:$!\n";
  887:                        }
  888: 		      } else {
  889:                           print $client "refused\n";
  890:                       }
  891: # ------------------------------------------------------------------------- get
  892:                    } elsif ($userinput =~ /^get/) {
  893:                        my ($cmd,$udom,$uname,$namespace,$what)
  894:                           =split(/:/,$userinput);
  895:                        $namespace=~s/\//\_/g;
  896:                        $namespace=~s/\W//g;
  897:                        chomp($what);
  898:                        my @queries=split(/\&/,$what);
  899:                        my $proname=propath($udom,$uname);
  900:                        my $qresult='';
  901:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
  902:                            for ($i=0;$i<=$#queries;$i++) {
  903:                                $qresult.="$hash{$queries[$i]}&";
  904:                            }
  905: 			   if (untie(%hash)) {
  906: 		              $qresult=~s/\&$//;
  907:                               print $client "$qresult\n";
  908:                            } else {
  909:                               print $client "error:$!\n";
  910:                            }
  911:                        } else {
  912:                            print $client "error:$!\n";
  913:                        }
  914: # ------------------------------------------------------------------------ eget
  915:                    } elsif ($userinput =~ /^eget/) {
  916:                        my ($cmd,$udom,$uname,$namespace,$what)
  917:                           =split(/:/,$userinput);
  918:                        $namespace=~s/\//\_/g;
  919:                        $namespace=~s/\W//g;
  920:                        chomp($what);
  921:                        my @queries=split(/\&/,$what);
  922:                        my $proname=propath($udom,$uname);
  923:                        my $qresult='';
  924:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
  925:                            for ($i=0;$i<=$#queries;$i++) {
  926:                                $qresult.="$hash{$queries[$i]}&";
  927:                            }
  928: 			   if (untie(%hash)) {
  929: 		              $qresult=~s/\&$//;
  930:                               if ($cipher) {
  931:                                 my $cmdlength=length($qresult);
  932:                                 $qresult.="         ";
  933:                                 my $encqresult='';
  934:                                 for 
  935: 				(my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
  936:                                  $encqresult.=
  937:                                  unpack("H16",
  938:                                  $cipher->encrypt(substr($qresult,$encidx,8)));
  939:                                 }
  940:                                 print $client "enc:$cmdlength:$encqresult\n";
  941: 			      } else {
  942: 			        print $client "error:no_key\n";
  943:                               }
  944:                            } else {
  945:                               print $client "error:$!\n";
  946:                            }
  947:                        } else {
  948:                            print $client "error:$!\n";
  949:                        }
  950: # ------------------------------------------------------------------------- del
  951:                    } elsif ($userinput =~ /^del/) {
  952:                        my ($cmd,$udom,$uname,$namespace,$what)
  953:                           =split(/:/,$userinput);
  954:                        $namespace=~s/\//\_/g;
  955:                        $namespace=~s/\W//g;
  956:                        chomp($what);
  957:                        my $proname=propath($udom,$uname);
  958:                        my $now=time;
  959:                        unless ($namespace=~/^nohist\_/) {
  960: 			   my $hfh;
  961: 			   if (
  962:                              $hfh=IO::File->new(">>$proname/$namespace.hist")
  963: 			       ) { print $hfh "D:$now:$what\n"; }
  964: 		       }
  965:                        my @keys=split(/\&/,$what);
  966:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
  967:                            foreach $key (@keys) {
  968:                                delete($hash{$key});
  969:                            }
  970: 			   if (untie(%hash)) {
  971:                               print $client "ok\n";
  972:                            } else {
  973:                               print $client "error:$!\n";
  974:                            }
  975:                        } else {
  976:                            print $client "error:$!\n";
  977:                        }
  978: # ------------------------------------------------------------------------ keys
  979:                    } elsif ($userinput =~ /^keys/) {
  980:                        my ($cmd,$udom,$uname,$namespace)
  981:                           =split(/:/,$userinput);
  982:                        $namespace=~s/\//\_/g;
  983:                        $namespace=~s/\W//g;
  984:                        my $proname=propath($udom,$uname);
  985:                        my $qresult='';
  986:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
  987:                            foreach $key (keys %hash) {
  988:                                $qresult.="$key&";
  989:                            }
  990: 			   if (untie(%hash)) {
  991: 		              $qresult=~s/\&$//;
  992:                               print $client "$qresult\n";
  993:                            } else {
  994:                               print $client "error:$!\n";
  995:                            }
  996:                        } else {
  997:                            print $client "error:$!\n";
  998:                        }
  999: # ------------------------------------------------------------------------ dump
 1000:                    } elsif ($userinput =~ /^dump/) {
 1001:                        my ($cmd,$udom,$uname,$namespace)
 1002:                           =split(/:/,$userinput);
 1003:                        $namespace=~s/\//\_/g;
 1004:                        $namespace=~s/\W//g;
 1005:                        my $proname=propath($udom,$uname);
 1006:                        my $qresult='';
 1007:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
 1008:                            foreach $key (keys %hash) {
 1009:                                $qresult.="$key=$hash{$key}&";
 1010:                            }
 1011: 			   if (untie(%hash)) {
 1012: 		              $qresult=~s/\&$//;
 1013:                               print $client "$qresult\n";
 1014:                            } else {
 1015:                               print $client "error:$!\n";
 1016:                            }
 1017:                        } else {
 1018:                            print $client "error:$!\n";
 1019:                        }
 1020: # ----------------------------------------------------------------------- store
 1021:                    } elsif ($userinput =~ /^store/) {
 1022:                       my ($cmd,$udom,$uname,$namespace,$rid,$what)
 1023:                           =split(/:/,$userinput);
 1024:                       $namespace=~s/\//\_/g;
 1025:                       $namespace=~s/\W//g;
 1026:                       if ($namespace ne 'roles') {
 1027:                        chomp($what);
 1028:                        my $proname=propath($udom,$uname);
 1029:                        my $now=time;
 1030:                        unless ($namespace=~/^nohist\_/) {
 1031: 			   my $hfh;
 1032: 			   if (
 1033:                              $hfh=IO::File->new(">>$proname/$namespace.hist")
 1034: 			       ) { print $hfh "P:$now:$rid:$what\n"; }
 1035: 		       }
 1036:                        my @pairs=split(/\&/,$what);
 1037:                          
 1038:     if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_WRCREAT,0640)) {
 1039:                            my @previouskeys=split(/&/,$hash{"keys:$rid"});
 1040:                            my $key;
 1041:                            $hash{"version:$rid"}++;
 1042:                            my $version=$hash{"version:$rid"};
 1043:                            my $allkeys=''; 
 1044:                            foreach $pair (@pairs) {
 1045: 			       ($key,$value)=split(/=/,$pair);
 1046:                                $allkeys.=$key.':';
 1047:                                $hash{"$version:$rid:$key"}=$value;
 1048:                            }
 1049:                            $hash{"$version:$rid:timestamp"}=$now;
 1050:                            $allkeys.='timestamp';
 1051:                            $hash{"$version:keys:$rid"}=$allkeys;
 1052: 			   if (untie(%hash)) {
 1053:                               print $client "ok\n";
 1054:                            } else {
 1055:                               print $client "error:$!\n";
 1056:                            }
 1057:                        } else {
 1058:                            print $client "error:$!\n";
 1059:                        }
 1060: 		      } else {
 1061:                           print $client "refused\n";
 1062:                       }
 1063: # --------------------------------------------------------------------- restore
 1064:                    } elsif ($userinput =~ /^restore/) {
 1065:                        my ($cmd,$udom,$uname,$namespace,$rid)
 1066:                           =split(/:/,$userinput);
 1067:                        $namespace=~s/\//\_/g;
 1068:                        $namespace=~s/\W//g;
 1069:                        chomp($rid);
 1070:                        my $proname=propath($udom,$uname);
 1071:                        my $qresult='';
 1072:       if (tie(%hash,'GDBM_File',"$proname/$namespace.db",&GDBM_READER,0640)) {
 1073:                 	   my $version=$hash{"version:$rid"};
 1074:                            $qresult.="version=$version&";
 1075:                            my $scope;
 1076:                            for ($scope=1;$scope<=$version;$scope++) {
 1077: 			      my $vkeys=$hash{"$scope:keys:$rid"};
 1078:                               my @keys=split(/:/,$vkeys);
 1079:                               my $key;
 1080:                               $qresult.="$scope:keys=$vkeys&";
 1081:                               foreach $key (@keys) {
 1082: 	     $qresult.="$scope:$key=".$hash{"$scope:$rid:$key"}."&";
 1083:                               }                                  
 1084:                            }
 1085: 			   if (untie(%hash)) {
 1086: 		              $qresult=~s/\&$//;
 1087:                               print $client "$qresult\n";
 1088:                            } else {
 1089:                               print $client "error:$!\n";
 1090:                            }
 1091:                        } else {
 1092:                            print $client "error:$!\n";
 1093:                        }
 1094: # ------------------------------------------------------------------- querysend
 1095:                    } elsif ($userinput =~ /^querysend/) {
 1096:                        my ($cmd,$query,
 1097: 			   $custom,$customshow)=split(/:/,$userinput);
 1098: 		       $query=~s/\n*$//g;
 1099: 		       unless ($custom or $customshow) {
 1100: 			   print $client "".
 1101: 			       sqlreply("$hostid{$clientip}\&$query")."\n";
 1102: 		       }
 1103: 		       else {
 1104: 			   print $client "".
 1105: 			       sqlreply("$hostid{$clientip}\&$query".
 1106: 					"\&$custom"."\&$customshow")."\n";
 1107: 		       }
 1108: # ------------------------------------------------------------------ queryreply
 1109:                    } elsif ($userinput =~ /^queryreply/) {
 1110:                        my ($cmd,$id,$reply)=split(/:/,$userinput); 
 1111: 		       my $store;
 1112:                        my $execdir=$perlvar{'lonDaemons'};
 1113:                        if ($store=IO::File->new(">$execdir/tmp/$id")) {
 1114: 			   $reply=~s/\&/\n/g;
 1115: 			   print $store $reply;
 1116: 			   close $store;
 1117: 			   my $store2=IO::File->new(">$execdir/tmp/$id.end");
 1118: 			   print $store2 "done\n";
 1119: 			   close $store2;
 1120: 			   print $client "ok\n";
 1121: 		       }
 1122: 		       else {
 1123: 			   print $client "error:$!\n";
 1124: 		       }
 1125: # ----------------------------------------------------------------------- idput
 1126:                    } elsif ($userinput =~ /^idput/) {
 1127:                        my ($cmd,$udom,$what)=split(/:/,$userinput);
 1128:                        chomp($what);
 1129:                        $udom=~s/\W//g;
 1130:                        my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
 1131:                        my $now=time;
 1132:                        {
 1133: 			   my $hfh;
 1134: 			   if (
 1135:                              $hfh=IO::File->new(">>$proname.hist")
 1136: 			       ) { print $hfh "P:$now:$what\n"; }
 1137: 		       }
 1138:                        my @pairs=split(/\&/,$what);
 1139:                  if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_WRCREAT,0640)) {
 1140:                            foreach $pair (@pairs) {
 1141: 			       ($key,$value)=split(/=/,$pair);
 1142:                                $hash{$key}=$value;
 1143:                            }
 1144: 			   if (untie(%hash)) {
 1145:                               print $client "ok\n";
 1146:                            } else {
 1147:                               print $client "error:$!\n";
 1148:                            }
 1149:                        } else {
 1150:                            print $client "error:$!\n";
 1151:                        }
 1152: # ----------------------------------------------------------------------- idget
 1153:                    } elsif ($userinput =~ /^idget/) {
 1154:                        my ($cmd,$udom,$what)=split(/:/,$userinput);
 1155:                        chomp($what);
 1156:                        $udom=~s/\W//g;
 1157:                        my $proname="$perlvar{'lonUsersDir'}/$udom/ids";
 1158:                        my @queries=split(/\&/,$what);
 1159:                        my $qresult='';
 1160:                  if (tie(%hash,'GDBM_File',"$proname.db",&GDBM_READER,0640)) {
 1161:                            for ($i=0;$i<=$#queries;$i++) {
 1162:                                $qresult.="$hash{$queries[$i]}&";
 1163:                            }
 1164: 			   if (untie(%hash)) {
 1165: 		              $qresult=~s/\&$//;
 1166:                               print $client "$qresult\n";
 1167:                            } else {
 1168:                               print $client "error:$!\n";
 1169:                            }
 1170:                        } else {
 1171:                            print $client "error:$!\n";
 1172:                        }
 1173: # ---------------------------------------------------------------------- tmpput
 1174:                    } elsif ($userinput =~ /^tmpput/) {
 1175:                        my ($cmd,$what)=split(/:/,$userinput);
 1176: 		       my $store;
 1177:                        $tmpsnum++;
 1178:                        my $id=$$.'_'.$clientip.'_'.$tmpsnum;
 1179:                        $id=~s/\W/\_/g;
 1180:                        $what=~s/\n//g;
 1181:                        my $execdir=$perlvar{'lonDaemons'};
 1182:                        if ($store=IO::File->new(">$execdir/tmp/$id.tmp")) {
 1183: 			   print $store $what;
 1184: 			   close $store;
 1185: 			   print $client "$id\n";
 1186: 		       }
 1187: 		       else {
 1188: 			   print $client "error:$!\n";
 1189: 		       }
 1190: 
 1191: # ---------------------------------------------------------------------- tmpget
 1192:                    } elsif ($userinput =~ /^tmpget/) {
 1193:                        my ($cmd,$id)=split(/:/,$userinput);
 1194:                        chomp($id);
 1195:                        $id=~s/\W/\_/g;
 1196:                        my $store;
 1197:                        my $execdir=$perlvar{'lonDaemons'};
 1198:                        if ($store=IO::File->new("$execdir/tmp/$id.tmp")) {
 1199:                            my $reply=<$store>;
 1200: 			   print $client "$reply\n";
 1201:                            close $store;
 1202: 		       }
 1203: 		       else {
 1204: 			   print $client "error:$!\n";
 1205: 		       }
 1206: 
 1207: # -------------------------------------------------------------------------- ls
 1208:                    } elsif ($userinput =~ /^ls/) {
 1209:                        my ($cmd,$ulsdir)=split(/:/,$userinput);
 1210:                        my $ulsout='';
 1211:                        my $ulsfn;
 1212:                        if (-e $ulsdir) {
 1213: 			if (opendir(LSDIR,$ulsdir)) {
 1214:                           while ($ulsfn=readdir(LSDIR)) {
 1215: 			     my @ulsstats=stat($ulsdir.'/'.$ulsfn);
 1216:                              $ulsout.=$ulsfn.'&'.join('&',@ulsstats).':';
 1217:                           }
 1218:                           closedir(LSDIR);
 1219: 		        }
 1220: 		       } else {
 1221:                           $ulsout='no_such_dir';
 1222:                        }
 1223:                        if ($ulsout eq '') { $ulsout='empty'; }
 1224:                        print $client "$ulsout\n";
 1225: # ------------------------------------------------------------------ Hanging up
 1226:                    } elsif (($userinput =~ /^exit/) ||
 1227:                             ($userinput =~ /^init/)) {
 1228:                        &logthis(
 1229:       "Client $clientip ($hostid{$clientip}) hanging up: $userinput");
 1230:                        print $client "bye\n";
 1231: 		       last;
 1232: # ------------------------------------------------------------- unknown command
 1233:                    } else {
 1234:                        # unknown command
 1235:                        print $client "unknown_cmd\n";
 1236:                    }
 1237: # ------------------------------------------------------ client unknown, refuse
 1238: 	       }
 1239:             } else {
 1240: 	        print $client "refused\n";
 1241:                 &logthis("<font color=blue>WARNING: "
 1242:                 ."Rejected client $clientip, closing connection</font>");
 1243:             }              
 1244:             &logthis("<font color=red>CRITICAL: "
 1245:                     ."Disconnect from $clientip ($hostid{$clientip})</font>");
 1246: # =============================================================================
 1247:         }
 1248:     
 1249:         # tidy up gracefully and finish
 1250:     
 1251:         # this exit is VERY important, otherwise the child will become
 1252:         # a producer of more and more children, forking yourself into
 1253:         # process death.
 1254:         exit;
 1255:     }
 1256: }
 1257: 
 1258: 
 1259: 
 1260: 
 1261: 

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