home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume34 / fsp / part03 / server_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-18  |  6.3 KB  |  220 lines

  1.     /*********************************************************************\
  2.     *  Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu)   *
  3.     *                                                                     *
  4.     *  You may copy or modify this file in any manner you wish, provided  *
  5.     *  that this notice is always included, and that you hold the author  *
  6.     *  harmless for any loss or damage resulting from the installation or *
  7.     *  use of this software.                                              *
  8.     \*********************************************************************/
  9.  
  10. #include "server_def.h"
  11.  
  12. #define ERR(S) { send_error(from,ub,S); return; }
  13.  
  14. int max_nlen   = FILE_NAME_LIMIT;
  15. int inetd_mode =               0;
  16. int dbug       =               0;
  17. int   udp_port =    DEF_FSP_PORT;
  18. char *home_dir =    DEF_FSP_HOME;
  19. char *run_uid  =               0;
  20.  
  21. int always_use_cache_dir = ALWAYS_USE_CACHE_DIR;
  22. int dir_cache_limit      = MAX_DIR_CACHE_COUNT;
  23. char *dir_cache_dir      = DEF_DIRECTORY_CACHE;
  24.  
  25. /****************************************************************************
  26. *  This is the dispatch loop for message that has been accepted.
  27. *    bytes: size of the message received.
  28. *       ub: pointer to the message buffer.
  29. *      old: true if this message contains old sequence number (retransmit).
  30. *       hp: pointer to the entry for the client host who sent this message.
  31. *     from: pointer to the socket address structure of the client host.
  32. ****************************************************************************/
  33.  
  34. server_get_packet(bytes,ub,old,hp,from)
  35.     int   bytes,old;
  36.     UBUF *ub;
  37.     HTAB *hp;
  38.     struct sockaddr_in *from;
  39. {
  40.     unsigned long  inet_num;
  41.     unsigned short port_num;
  42.     unsigned l1, l2;
  43.     char *s1, *s2, *pe;
  44.     FILE *fp;
  45.     PPATH pp;
  46.  
  47.     l1 = ub->len;
  48.     l2 = bytes - l1 - UBUF_HSIZE;
  49.     s1 = ub->buf;
  50.     s2 = ub->buf + l1;
  51.  
  52.     if(dbug) fprintf(stderr,"rcv (%c,%d,%d,%lu) <--- %d.%d.%d.%d\n",
  53.         ub->cmd, l1, l2, ub->pos,
  54.         ((unsigned char *)(&hp->inet_num))[0],
  55.         ((unsigned char *)(&hp->inet_num))[1],
  56.         ((unsigned char *)(&hp->inet_num))[2],
  57.         ((unsigned char *)(&hp->inet_num))[3]);
  58.  
  59.     if(!old) { hp->last_key = hp->next_key;
  60.            hp->next_key = get_next_key() + ((hp->last_key+1) & 0x00ff); }
  61.  
  62.     ub->key  = hp->next_key;
  63.     inet_num = hp->inet_num;
  64.     port_num = from->sin_port;
  65.  
  66.     switch(ub->cmd)
  67.     {
  68.       case CC_VERSION : { ERR(VERSION_STR); }
  69.  
  70.       case CC_BYE     : { if(!old) hp->active = 0;
  71.               server_reply(from,ub,0,0);
  72.               return; }
  73.             
  74.       case CC_GET_DIR : { if((pe = check_path(s1,l1,&pp)) ||
  75.                  (pe = server_get_dir(&pp,&fp))) ERR(pe);
  76.               send_file(from,ub,fp,l2,s2);
  77.               fclose(fp);
  78.               return; }
  79.  
  80.       case CC_GET_FILE: { if((pe = check_path(s1,l1,&pp)) ||
  81.                      (pe = server_get_file(&pp,&fp))) ERR(pe);
  82.               send_file(from,ub,fp,l2,s2);
  83.               fclose(fp);
  84.               return; }
  85.  
  86.       case CC_DEL_FILE: { if(!old)
  87.                 if((pe = check_path(s1,l1,&pp)) ||
  88.                    (pe = server_del_file(&pp,inet_num))) ERR(pe);
  89.               server_reply(from,ub,0,0);
  90.               return; }
  91.  
  92.       case CC_DEL_DIR : { if(!old)
  93.                 if((pe = check_path(s1,l1,&pp)) ||
  94.                    (pe = server_del_dir(&pp,inet_num))) ERR(pe);
  95.               server_reply(from,ub,0,0);
  96.               return; }
  97.  
  98.       case CC_UP_LOAD : { if(!old)
  99.                 if(pe = server_up_load(s1,l1,ub->pos,
  100.                            inet_num,port_num)) ERR(pe);
  101.               server_reply(from,ub,0,0);
  102.               return; }
  103.  
  104.       case CC_INSTALL : { if(!old)
  105.                 if((pe = check_path(s1,l1,&pp)) ||
  106.                    (pe = server_install(&pp,inet_num,port_num)))
  107.                                        ERR(pe);
  108.               server_reply(from,ub,0,0);
  109.               return; }
  110.  
  111.       case CC_MAKE_DIR: { if(!old)
  112.                 if((pe = check_path(s1,l1,&pp)) ||
  113.                    (pe = server_make_dir(&pp,inet_num))) ERR(pe);
  114.               if(pe = server_get_pro(&pp,s1,inet_num)) ERR(pe);
  115.               server_reply(from,ub,strlen(ub->buf)+1,0);
  116.               return; }
  117.  
  118.       case CC_GET_PRO : { if((pe = check_path(s1,l1,&pp)) ||
  119.                  (pe = server_get_pro(&pp,s1,inet_num))) ERR(pe);
  120.               server_reply(from,ub,strlen(ub->buf)+1,0);
  121.               return; }
  122.  
  123.       case CC_SET_PRO : { if(!old)
  124.                 if((pe = check_path(s1,l1,&pp)) ||
  125.                    (pe = server_set_pro(&pp,s2,inet_num))) ERR(pe);
  126.               if(pe = server_get_pro(&pp,s1,inet_num)) ERR(pe);
  127.               server_reply(from,ub,strlen(ub->buf)+1,0);
  128.               return; }
  129.  
  130.       case CC_GRAB_FILE: { if(pe = check_path(s1,l1,&pp)) ERR(pe);
  131.                if(!old && !ub->pos)
  132.                  if(pe = server_secure_file(&pp,inet_num,port_num))
  133.                                        ERR(pe);
  134.                if(pe = server_grab_file(&pp,&fp,inet_num,port_num))
  135.                                        ERR(pe);
  136.                send_file(from,ub,fp,l2,s2);
  137.                fclose(fp);
  138.                return; }
  139.  
  140.       case CC_GRAB_DONE: { if(pe = check_path(s1,l1,&pp)) ERR(pe);
  141.                if(!old)
  142.                  if(pe = server_grab_done(&pp,inet_num,port_num))
  143.                                        ERR(pe);
  144.                server_reply(from,ub,0,0);
  145.                return; }
  146.  
  147.     }
  148. }
  149.  
  150. arg_err()
  151. {
  152.     fputs("arg: -h absolute_path    set home directory.\n",stderr);
  153.     fputs("     -p udp_port_number  set port number.\n",stderr);
  154.     fputs("     -u uid_number       assume this uid after startup.\n",stderr);
  155.     fputs("     -d                  turn on debug mode.\n",stderr);
  156.     exit(1);
  157. }
  158.  
  159. main(argc,argv)
  160.     int argc;
  161.     char **argv;
  162. {
  163.     int t;
  164.  
  165.     inetd_mode = !strcmp(argv[0],"in.fspd");
  166.  
  167.     if(inetd_mode)
  168.     {
  169.     init_inetd();
  170.     freopen("/dev/null","r",stdin);
  171.     freopen("/dev/null","w",stdout);
  172.     freopen("/dev/null","w",stderr);
  173.     }
  174.  
  175.     for(t = 1; t < argc; t++)
  176.     {
  177.     if(argv[t][0] == '-') switch(argv[t][1])
  178.     {
  179.         case 'd':    dbug++; break;
  180.  
  181.         case 'h':    if(argv[t][2]) home_dir = argv[t]+2; else
  182.             if(argv[t+1] ) home_dir = argv[++t]; else
  183.                                 arg_err();
  184.             break;
  185.  
  186.         case 'u':    if(argv[t][2]) run_uid = argv[t]+2; else
  187.             if(argv[t+1] ) run_uid = argv[++t]; else
  188.                                 arg_err();
  189.             break;
  190.  
  191.         case 'p':    if(argv[t][2]) udp_port = atoi(argv[t]+2); else
  192.             if(argv[t+1] ) udp_port = atoi(argv[++t]); else
  193.                                 arg_err();
  194.             break;
  195.  
  196.         default : arg_err();
  197.  
  198.     } else arg_err();
  199.     }
  200.  
  201.     if(!inetd_mode)
  202.     {
  203.     init_network(udp_port);
  204.     if(!dbug) { freopen("/dev/null","r",stdin);
  205.             freopen("/dev/null","w",stdout);
  206.             freopen("/dev/null","w",stderr); }
  207.     }
  208.  
  209.     if(run_uid) if(setuid(atoi(run_uid)) != 0) exit(1);
  210.     init_home_dir();
  211.     init_htab();
  212.  
  213.     srandom(getpid());
  214.  
  215.     if(inetd_mode) {          server_loop(120*1000L); }  /* 2 minutes */
  216.           else { while(1) server_loop(      -1L); }
  217.  
  218.     exit(0);
  219. }
  220.