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

  1. #include "headers.h" /* has all the important stuff */
  2.  
  3. /* just called at startup to setup setjmp() */
  4. void init()
  5. {
  6.    tzset();
  7.    srandom((int)time(NULL) + (int)getpid());
  8.  
  9.    /* just initialize stuff.. all we need to do for now is */
  10.    /* initialize setjmp() that's located in quit()...      */
  11.    quit(INIT);
  12. }
  13.  
  14.  
  15. /* -------------------------------- */
  16.  
  17.  
  18. /* initialize the shared memory */
  19. void shrMemInit()
  20. {
  21.    int res;
  22.  
  23.    if (debugging == 1)
  24.    {
  25.       (void)putchar('\n');
  26.       (void)write(dblogfd, "\n", 1);
  27.    }
  28.  
  29. #  ifdef HAVE_SHMGET
  30.    debug("getting a new shared memory ID\n");
  31.  
  32.    clients[curClient].shmID = 
  33.         shmget(IPC_PRIVATE, MAXSEGSIZE, IPC_CREAT | IPC_EXCL | 0600);
  34.  
  35.    if (clients[curClient].shmID == ERROR)
  36.    {
  37.       clients[curClient].pinging = NOPING;
  38.       error("error getting shared memory ID: %s\n\n", strerror(errno));
  39.       return;
  40.    }
  41.  
  42. #  else
  43.    clients[curClient].pinging = NOPING;
  44.    return;
  45. #  endif
  46.  
  47. #  ifdef HAVE_SHMAT
  48.    debug("attaching the shared memory segment\n");
  49.  
  50.    clients[curClient].segptr = shmat(clients[curClient].shmID, 0, 0);
  51.    if (clients[curClient].segptr == NULL)
  52.    {
  53.       clients[curClient].pinging = NOPING;
  54.       error("error attaching shared memory: %s\n\n", strerror(errno));
  55.       return;
  56.    }
  57.  
  58. #  else
  59.    clients[curClient].pinging = NOPING;
  60.    return;
  61. #  endif
  62.  
  63.    memset(clients[curClient].segptr, 0, MAXSEGSIZE);
  64.  
  65. #  ifdef HAVE_SHMCTL
  66.    debug("removing the shared memory ID\n");
  67.  
  68.    res = shmctl(clients[curClient].shmID, IPC_RMID, 0);
  69.    if (res == ERROR)
  70.    {
  71.       clients[curClient].pinging = NOPING;
  72.       error("error removing the ID: %s\n\n", strerror(errno));
  73.  
  74.       debug("now detaching the shared memory segment too\n");
  75.  
  76.       res = shmdt(clients[curClient].segptr);
  77.       if (res == ERROR)
  78.          error("error detaching the segment: %s\n\n", strerror(errno));
  79.  
  80.       return;
  81.    }
  82.  
  83. #  else
  84.    clients[curClient].pinging = NOPING;
  85.    return;
  86. #  endif
  87. }
  88.  
  89.  
  90. /* -------------------------------- */
  91.  
  92.  
  93. /* setup files and things */
  94. void setup()
  95. {
  96.    if (locPort <= 0) locPort = PORT;
  97.  
  98.    createDirs(0); /* 0 == no client specifically yet */
  99.  
  100.    setupFiles();
  101.  
  102.    /* setup signals */
  103.    signal(SIGHUP, SIG_IGN);
  104.    signal(SIGINT, sighandler);
  105.    signal(SIGTERM, sighandler);
  106.    signal(SIGCHLD, sighandler);
  107.  
  108.    signal(SIGPIPE, SIG_IGN);
  109.  
  110.    signal(SIGUSR1, SIG_IGN);
  111.    signal(SIGUSR2, SIG_IGN);
  112.    /* ------------ */
  113.  
  114.    if (debugging != 1)
  115.    {
  116.       signal(SIGILL, sighandler);
  117.       signal(SIGBUS, sighandler);
  118.       signal(SIGSEGV, sighandler);
  119.  
  120.       signal(SIGQUIT, sighandler);
  121.    }
  122. }
  123.  
  124.  
  125. /* -------------------------------- */
  126.  
  127.  
  128. /* create/setup files to log */
  129. void setupFiles()
  130. {
  131.    int maxfds;
  132.    register int i;
  133.  
  134.    time_t tm = time(NULL);
  135.  
  136.    char buf[128];
  137.    char dblog[MAXFNAMESIZE]; 
  138.    char errlog[MAXFNAMESIZE]; 
  139.  
  140.    memset(dblog, 0, sizeof(dblog));
  141.    memset(errlog, 0, sizeof(errlog));
  142.  
  143.    maxfds = (int)sysconf(_SC_OPEN_MAX);
  144.    if (maxfds == ERROR) 
  145.    {
  146.       (void)fprintf(stderr,
  147.                     "error getting the max. # of open files: %s\n\n",
  148.                     strerror(errno));
  149.  
  150.       (void)fprintf(stderr,
  151.               "setting the maximum number of open files to 256..\n\n");
  152.  
  153.       maxfds = 256;
  154.    }
  155.  
  156.    else if (maxfds != ERROR)
  157.       if (debugging == 1)
  158.          (void)printf("Maximum # of open files supported: (%d)\n\n", maxfds);
  159.  
  160.    /* start with 3 to skip stdin, stdout, and stderr */
  161.    for (i = 3; i <= maxfds; i++) (void)close(i);
  162.  
  163.    /* -------------- open errlog junk ---------------- */
  164.  
  165.    if (errorFile == NULL) 
  166.       (void)snprintf(errlog, sizeof(errlog)-1, "%s/%s", SRSDIR, ERRLOG);
  167.  
  168.    else (void)snprintf(errlog, sizeof(errlog)-1, errorFile);
  169.  
  170.    errlogfd = open(errlog, O_CREAT | O_WRONLY | O_APPEND, 0600);
  171.    if (errlogfd == ERROR)
  172.    {
  173.       (void)fprintf(stderr, "error opening %s: %s\n\n", errlog,
  174.                     strerror(errno));
  175.  
  176.       exit(ERROR);
  177.    }
  178.  
  179.    errlogfd1 = fopen(errlog, "a");
  180.    if (errlogfd1 == NULL)
  181.    {
  182.       (void)fprintf(stderr, "error opening %s: %s\n\n", errlog,
  183.                     strerror(errno));
  184.  
  185.       exit(ERROR);
  186.    }
  187.  
  188.    /* --------------- open dblog junk ---------------- */
  189.  
  190.    if (debugging == 1)
  191.    {
  192.       snprintf(dblog, sizeof(dblog)-1, "%s/%s", SRSDIR, DEBLOG);
  193.  
  194.       dblogfd = open(dblog, O_CREAT | O_WRONLY | O_APPEND, 0600);
  195.       if (dblogfd == ERROR)
  196.       {
  197.          (void)fprintf(stderr, "error opening %s: %s\n\n", dblog,
  198.                        strerror(errno));
  199.  
  200.          exit(ERROR);
  201.       }
  202.  
  203.       dblogfd1 = fopen(dblog, "a");
  204.       if (dblogfd == ERROR)
  205.       {
  206.          (void)fprintf(stderr, "error opening %s: %s\n\n", dblog,
  207.                        strerror(errno));
  208.  
  209.          exit(ERROR);
  210.       }
  211.    }
  212.  
  213.    /* ------------------------------------------------ */
  214.  
  215.    memset(buf, 0, sizeof(buf));
  216.  
  217.    sprintf(buf, "----------------------------------------------\n\n");
  218.    (void)write(errlogfd, buf, strlen(buf));
  219.  
  220.    if (debugging == 1) (void)write(dblogfd, buf, strlen(buf));
  221.  
  222.    memset(buf, 0, sizeof(buf));
  223.  
  224.    sprintf(buf, "SRS restarted on: %s\n", ctime(&tm));
  225.    (void)write(errlogfd, buf, strlen(buf));
  226.  
  227.    if (debugging == 1) (void)write(dblogfd, buf, strlen(buf));
  228. }
  229.  
  230.  
  231. /* ------------------------------- */
  232.  
  233.  
  234. /* set the process ID's */
  235. void setPids(int pid)
  236. {
  237.    int res;
  238.  
  239.    (clients[curClient]).free = ERROR;
  240.    (clients[curClient]).pidstats[curPid].pid  = pid; 
  241.    (clients[curClient]).pidstats[curPid].ppid = getpid();
  242.    (clients[curClient]).pidstats[curPid].pgid = getpgid(pid);
  243.  
  244.    debug("clients[%d].pid = %d\n", curClient, pid);
  245.  
  246.    (void)sleep(NORMPAUSE); /* give the child time to setup    */
  247.  
  248.    res = seteuid(0); 
  249.    if (res == ERROR) 
  250.    {
  251.       error("error with seteuid: %s\n\n", strerror(errno));
  252.       quit(ERROR);
  253.    }
  254.  
  255.    /* send it the signal that it can now start up */
  256.    res = kill((clients[curClient]).pidstats[curPid].pid, SIGUSR1);
  257.  
  258.    if (res == ERROR) 
  259.       error("error killing pid %d: %s\n\n",
  260.             clients[curClient].pidstats[curPid].pid, strerror(errno));
  261.  
  262.    res = seteuid(pwd->pw_uid);
  263.    if (res == ERROR) 
  264.    {
  265.       error("error with seteuid: %s\n\n", strerror(errno));
  266.       quit(ERROR);
  267.    }
  268. }
  269.  
  270.  
  271. /* -------------------------------- */
  272.  
  273.  
  274. /* setup default client structures */
  275. void setupClients()
  276. {
  277.    register int i, j;
  278.  
  279.    /* setup start/default values.. */
  280.    for (i = 0; i < MAXCONNS; i++)
  281.    {
  282.       clients[i].free    = 1; /* yes, it's free   */
  283.       clients[i].running = 0; /* no.. not running */
  284.       clients[i].newData = 0; /* no new data yet  */
  285.  
  286.       memset(clients[i].oldbuf, 0, sizeof(clients[i].oldbuf));
  287.  
  288.       /* 1 main pid and 1 pid for pinging */
  289.       for (j = 0; j < MAXNUMKIDS; j++) 
  290.       {
  291.          clients[i].pidstats[j].pid  = ERROR;
  292.          clients[i].pidstats[j].ppid = ERROR;
  293.          clients[i].pidstats[j].pgid = ERROR;
  294.       }
  295.    }
  296. }
  297.  
  298.  
  299. /* -------------------------------- */
  300.  
  301.  
  302. /* count the clients sub-ID's */
  303. void setupSubIDs()
  304. {
  305.    int res;
  306.    register int i;
  307.  
  308.    for (i = 0; i < MAXCONNS; i++)
  309.    {
  310.        if (clients[i].free == 1) continue;
  311.        else if (i == curClient) continue; /* skip our own struct */
  312.        else if (clients[i].pidstats[0].pid <= 0) continue;
  313.  
  314.        if (clients[i].ID == clients[curClient].ID)
  315.        {
  316.           debug("clients[%d].ID = clients[%d].ID (ID %04d)\n", i, 
  317.                 curClient, clients[i].ID);
  318.  
  319.           clients[curClient].numSubIDs++;
  320.        }
  321.    }
  322.  
  323.    clients[curClient].numSubIDs++;
  324.  
  325.    debug("current sub-ID for ID %04d = %d\n\n", 
  326.          clients[curClient].ID, clients[curClient].numSubIDs);
  327.  
  328.    res = checkMaxSubIDs();
  329.    if (res == ERROR) 
  330.    {
  331.       errors = 1;
  332.       return;
  333.    }
  334. }
  335.