home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / client / src / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  4.0 KB  |  192 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(time(NULL) + getpid()); */
  8.  
  9.    shrMemInit();
  10.  
  11.    /* just initialize stuff.. all we need to do for now is */
  12.    /* initialize setjmp().. that's located in quit()       */ 
  13.    dropstreamconn(INIT), quit(INIT);
  14. }
  15.  
  16.  
  17. /* -------------------------- */
  18.  
  19.  
  20. void shrMemInit()
  21. {
  22.    int res;
  23.  
  24.    if (debugging == 1) (void)putchar('\n');
  25.  
  26. #  ifdef HAVE_SHMGET
  27.    debug("getting the shared memory ID\n");
  28.  
  29.    shmID = shmget(IPC_PRIVATE, MAXSEGSIZE, IPC_CREAT | IPC_EXCL | 0600);
  30.    if (shmID == ERROR)
  31.    {
  32.       error("error getting the ID: %s\n\n", strerror(errno));
  33.       pinging = 0;
  34.       return;
  35.    }
  36.  
  37. #  else
  38.    pinging = 0;
  39.    return;
  40. #  endif
  41.  
  42. #  ifdef HAVE_SHMAT
  43.    debug("attaching the shared memory segment\n");
  44.  
  45.    segptr = shmat(shmID, 0, 0);
  46.    if (segptr == NULL)
  47.    {
  48.       error("error attaching the segment: %s\n\n", strerror(errno));
  49.       pinging = 0;
  50.       return;
  51.    }
  52.  
  53. #  else
  54.    pinging = 0;
  55.    return;
  56. #  endif
  57.  
  58.    memset(segptr, 0, MAXSEGSIZE);
  59.  
  60. #  ifdef HAVE_SHMCTL
  61.    debug("removing the shared memory ID\n");
  62.  
  63.    res = shmctl(shmID, IPC_RMID, 0);
  64.    if (res == ERROR)
  65.    {
  66.       pinging = 0;
  67.       error("error removing the ID: %s\n\n", strerror(errno));
  68.  
  69.       debug("now detaching the shared memory segment too\n");
  70.  
  71.       res = shmdt(segptr);
  72.       if (res == ERROR)
  73.          error("error detaching the segment: %s\n\n", strerror(errno));
  74.  
  75.       return;
  76.    }
  77.  
  78. #  else
  79.    pinging = 0;
  80.    return;
  81. #  endif
  82.  
  83.    if (debugging == 1) (void)putchar('\n');
  84. }
  85.  
  86.  
  87. /* ----------------------------- */
  88.  
  89.  
  90. /* setup necessary files */
  91. void setupFiles()
  92. {
  93.    char buf[128];
  94.    char dblog[MAXFNAMESIZE];
  95.    char errlog[MAXFNAMESIZE];      
  96.  
  97.    time_t tm = time(NULL);
  98.  
  99.    memset(buf, 0, sizeof(buf));
  100.    memset(dblog, 0, sizeof(dblog));
  101.    memset(errlog, 0, sizeof(errlog));
  102.  
  103.    /* ---------------- error log ----------------- */
  104.  
  105.    if (errorFile == NULL) (void)sprintf(errlog, "%s/%s", SRSdir, ERRLOG);
  106.    else (void)snprintf(errlog, sizeof(errlog)-1, "%s", errorFile);
  107.  
  108.    /* -------- */
  109.  
  110.    errlogfd = open(errlog, O_WRONLY | O_CREAT | O_APPEND, 0600);
  111.    if (errlogfd == ERROR)
  112.    {
  113.       (void)fprintf(stderr, "erroring opening %s: %s\n\n", errlog, 
  114.                     strerror(errno));
  115.  
  116.       exit(ERROR);
  117.    }
  118.  
  119.    /* -------- */
  120.  
  121.    errlogfd1 = fopen(errlog, "a+");
  122.    if (errlogfd1 == NULL)
  123.    {
  124.       (void)fprintf(stderr, "erroring opening %s: %s\n\n", errlog, 
  125.                     strerror(errno));
  126.  
  127.       exit(ERROR);
  128.    }
  129.  
  130.    /* -------- */
  131.    (void)chmod(errlog, S_IREAD | S_IWRITE);
  132.  
  133.    /* ---------------- debug log ----------------- */
  134.  
  135.    if (debugging == 1)
  136.    {
  137.       if (dbFile == NULL) 
  138.          (void)snprintf(dblog, sizeof(dblog)-1, "%s/%s",  SRSdir, DEBLOG);
  139.  
  140.       else (void)snprintf(dblog, sizeof(errlog)-1, "%s", dbFile);
  141.  
  142.       /* -------- */
  143.  
  144.       dblogfd = open(dblog, O_WRONLY | O_CREAT | O_APPEND, 0600);
  145.       if (dblogfd == ERROR)
  146.       {
  147.          (void)fprintf(stderr, "erroring opening %s: %s\n\n", dblog, 
  148.                        strerror(errno));
  149.  
  150.          exit(ERROR);
  151.       }
  152.  
  153.       /* -------- */
  154.  
  155.       dblogfd1 = fopen(dblog, "a+");
  156.       if (dblogfd1 == NULL)
  157.       {
  158.          (void)fprintf(stderr, "erroring opening %s: %s\n\n", dblog, 
  159.                        strerror(errno));
  160.  
  161.          exit(ERROR);
  162.       }
  163.  
  164.       /* -------- */
  165.       (void)chmod(dblog, S_IREAD | S_IWRITE);
  166.    }
  167.  
  168.    /* -------------------------------------- */
  169.  
  170.    sprintf(buf, "----------------------------------------------\n\n");
  171.    (void)write(errlogfd, buf, strlen(buf));
  172.  
  173.    if (debugging == 1) (void)write(dblogfd, buf, strlen(buf));
  174.  
  175.    memset(buf, 0, sizeof(buf));
  176.  
  177.    sprintf(buf, "SRS restarted on: %s\n", ctime(&tm));
  178.    (void)write(errlogfd, buf, strlen(buf));
  179.  
  180.    if (debugging == 1) (void)write(dblogfd, buf, strlen(buf));
  181.  
  182.    if (debugging == 1)
  183.    {
  184.       (void)putchar('\n');
  185.       (void)write(dblogfd, "\n", 1);
  186.    }
  187.  
  188.    debug("writing debug log to %s\n", dblog);
  189.    debug("writing error log to %s\n", errlog);
  190. }
  191.  
  192.