home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / server / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  6.1 KB  |  238 lines

  1. /* FIX - fix reentrancy in sighandler() */
  2.  
  3. /* FIX - if the HD fills up.. the load average gets too high.. etc..  */
  4. /*       redirect the client to the next server in the list           */
  5.  
  6. /* FIX - add K&R style function declartions to all source in client   */
  7. /*       and server (to suppport old compilers                        */
  8.  
  9. /* ------------------------------------------ */
  10.  
  11. #include "headers.h"
  12.  
  13. /* ------ data definitions ------ */
  14. int mainfd;                    /* fd to listen on...       */
  15. int mainpid;                   /* main pid of server       */
  16.  
  17. int child = 0;                 /* is there a child?        */ 
  18.  
  19. int locPort = 0;               /* local bind port          */
  20.  
  21. int nullfd = 0;                /* /dev/null for stdin      */
  22.  
  23. int  dblogfd = 0;              /* write log msgs to here.. */
  24. FILE *dblogfd1 = NULL;         /* same file but FILE *     */
  25.  
  26. int  errlogfd = 0;             /* write errors to here..   */
  27. FILE *errlogfd1 = NULL;        /* same file but FILE *     */
  28.  
  29. int curlogfd = 0;              /* index into logs[]..      */
  30.  
  31. char *errorFile = NULL;        /* error log file           */
  32. char *servListFile = NULL;     /* server list file         */
  33.  
  34. struct sockaddr_in laddr;      /* our local serv address   */
  35.  
  36. int curPid = ERROR;            /* index into pidstats[]    */
  37. int curClient = ERROR;         /* index into clients[]     */
  38.  
  39. long timeVal;                  /* time() value from client */
  40. long prevTimeVal;              /* prev. time() from client */
  41.  
  42. int errors;                    /* indicates an error occured */
  43. int silent = 0;                /* are we outputting data?    */
  44.  
  45. int repcount = 0;
  46. long prevlogtime = 0;
  47. char prevlogmsg[MAXLOGSIZE];
  48.  
  49. #ifndef NOSSL
  50. DH *dhparms; /* reads the DH parameters */
  51. #endif
  52.  
  53. int prevmsg = ERROR; /* previous message type  */ 
  54.  
  55. struct passwd *pwd;
  56.  
  57. volatile sig_atomic_t nofree;     /* free structure found? */
  58. volatile sig_atomic_t stopped;    /* is the child stopped? */
  59. volatile sig_atomic_t timeout;    /* have we timed out?    */
  60.  
  61. #ifdef DEBUG
  62. volatile int debugging = 1; /* are we in debugging mode or not */
  63. #else
  64. volatile int debugging = 0; /* are we in debugging mode or not */
  65. #endif
  66.  
  67. jmp_buf doquit, newconn;
  68.  
  69. /* ------------------------------ */
  70.  
  71. /* reports proper usage (local function only)      */
  72. /* [technically this should probably be in misc.c] */
  73. void usage(char *progname)
  74. {
  75.    fprintf(stderr, "Usage: %s [-h] [-d] [-p local port] "
  76.                    "[-e error file] [-s server list file]\n\n", progname);
  77.  
  78.    return;
  79. }
  80. /* ------------------------------ */
  81.  
  82.  
  83. int main(int argc, char **argv)
  84. {
  85.    int res;
  86.  
  87.    if (getuid() != 0)
  88.    {
  89.       fprintf(stderr, "Error: This must be run as root (uid 0).\n");
  90.       exit(ERROR);
  91.    }
  92.  
  93.    memset(prevlogmsg, 0, sizeof(prevlogmsg));
  94.  
  95.    mainpid = getpid();
  96.    doArgs(argc, argv), setup();
  97.  
  98.    (void)printf("Now starting SRS.. by RSI.\n");
  99.    (void)printf("[if you have problems, try: %s -h, and read SRS.doc]\n\n", 
  100.                 argv[0]);
  101.  
  102.    if (debugging != 1) daemonize();
  103.    else debug("running in debug mode.. not forking/daemonizing\n");
  104.  
  105.    init(), setupClients(), initconn();
  106.  
  107.    getSRSuser();
  108.  
  109.    res = seteuid(pwd->pw_uid);
  110.    if (res == ERROR)
  111.    {
  112.       error("error with seteuid: %s\n\n", strerror(errno));
  113.       quit(ERROR);
  114.    }
  115.  
  116.    if (debugging == 1)
  117.    {
  118.       (void)putchar('\n');
  119.       (void)write(dblogfd, "\n", 1);
  120.    }
  121.  
  122.    debug("local timezone: %s\n", tzname[0]);
  123.    
  124.    /* main loop.. wait for connection and then handle it */
  125.    while(1)
  126.    {
  127.       res = setjmp(newconn);
  128.  
  129.       if ((res == 1) || (errors == 1) || (timeout == 1))
  130.          if (curClient > 0)
  131.          {
  132.             if (clients[curClient].sockfd > 0)
  133.             {
  134.                debug("now dropping client\n");
  135.                (void)close(clients[curClient].sockfd);
  136.                clients[curClient].free = 1;
  137.             }
  138.          }
  139.  
  140.       res = seteuid(pwd->pw_uid);
  141.       if (res == ERROR)
  142.       {
  143.          error("error with seteuid: %s\n\n", strerror(errno));
  144.          quit(ERROR);
  145.       }
  146.  
  147.       timeout = 0, errors = 0, errno = 0;
  148.  
  149.       res = getconn(); /* this accepts the connection and sets stuff up */
  150.  
  151.       if (res == ERROR) 
  152.       {
  153.          while(1)
  154.          {
  155.             if (nofree != 1) break;
  156.             sleep(MAXPAUSE * 3);
  157.          }
  158.  
  159.          error("error in getconn().. restarting at top\n\n");
  160.  
  161.          if (clients[curClient].sockfd > 0)
  162.             (void)close(clients[curClient].sockfd);
  163.  
  164.          clients[curClient].free = 1;
  165.          continue;
  166.       }
  167.  
  168.       procData(1); /* just to get the ID and auth client */
  169.  
  170.       /* we had an error in procData() before returning */
  171.       if ((errors == 1) || (timeout == 1)) 
  172.       {
  173.          if (timeout == 1)
  174.             error("timed out waiting for ID (and/or authentication)\n\n");
  175.  
  176.          else error("error in procData() or authClient.. "
  177.                     "restarting at top\n\n");
  178.  
  179.          if (clients[curClient].sockfd > 0)
  180.             (void)close(clients[curClient].sockfd);
  181.  
  182.          clients[curClient].free = 1;
  183.  
  184.          continue;
  185.       }
  186.  
  187.       setupSubIDs();
  188.  
  189.       if (errors == 1)
  190.       {
  191.          error("error setting up sub-ID's\n\n"), errors = 0; 
  192.  
  193.          if (clients[curClient].sockfd > 0)
  194.             (void)close(clients[curClient].sockfd);
  195.  
  196.          clients[curClient].free = 1;
  197.  
  198.          continue;
  199.       }
  200.  
  201.       debug("now forking a child to handle the client\n");
  202.  
  203.       res = fork();
  204.       if (res == ERROR)
  205.       {
  206.          error("error forking child process for new connection: %s\n\n",
  207.                strerror(errno));
  208.  
  209.          quit(ERROR);
  210.       }   
  211.  
  212.       else if (res == 0) /* 0 == child */
  213.       {
  214.          signal(SIGPIPE, sighandler);
  215.          signal(SIGCHLD, SIG_IGN);
  216.  
  217.          stopped = 1; /* child can't run yet */
  218.  
  219.          makeHostArgs(argc, argv);
  220.  
  221.          (clients[curClient]).pidstats[curPid].pid  = getpid(); 
  222.          (clients[curClient]).pidstats[curPid].ppid = getppid();
  223.          (clients[curClient]).pidstats[curPid].pgid = getpgid(0);
  224.  
  225.          shrMemInit();
  226.  
  227.          signal(SIGUSR1, sighandler); 
  228.          while (stopped == 1) (void)sleep(1);
  229.  
  230.          procData(0); /* process data from client */
  231.       }
  232.  
  233.       else setPids(res);
  234.    }
  235.  
  236.    return SUCCESS;
  237. }
  238.