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

  1. #include "headers.h" /* has all important stuff */
  2.  
  3. /* this file has various functions to get/process data from client */
  4.  
  5. /* create directories for clients */
  6. void createDirs(int client)
  7. {
  8.    int res;
  9.    char curClientID[8], curSubID[8];
  10.  
  11.    memset(curClientID, 0, sizeof(curClientID));
  12.    memset(curSubID, 0, sizeof(curSubID));
  13.    
  14.    errno = 0;
  15.  
  16.    /* -----------  FOR SRS DIRECTORY ------- */
  17.  
  18.    debug("creating %s\n%c", SRSDIR, (client == 0 ? '\n' : '\0'));
  19.  
  20.    res = mkdir(SRSDIR, (0766 & ~077));
  21.    if (res == ERROR)
  22.       if (errno != EEXIST)
  23.       {
  24.          error("error making %s: %s\n\n", SRSDIR, strerror(errno));
  25.          quit(ERROR);
  26.       }
  27.  
  28.    errno = 0;
  29.    res = chdir(SRSDIR);
  30.    if (res == ERROR)
  31.    {
  32.       error("error chdir()'ing to %s: %s\n\n", SRSDIR, strerror(errno));
  33.       quit(ERROR);
  34.    }
  35.  
  36.    /* --------------------- FOR CERTS DIR ------------------------- */
  37.  
  38.    res = mkdir("certs", (0766 & ~077));
  39.    if (res == ERROR)
  40.       if (errno != EEXIST)
  41.       {
  42.          error("error making %s/%s: %s\n\n", SRSDIR, CLIENTDIRS,
  43.                strerror(errno));
  44.  
  45.          quit(ERROR);
  46.       }
  47.  
  48.    if (client == 0) return; /* no client.. so don't create client dirs */
  49.  
  50.    /* ---------- FOR CLIENTS DIRECTORY -------- */
  51.    errno = 0;
  52.  
  53.    debug("creating %s/%s\n", SRSDIR, CLIENTDIRS);
  54.  
  55.    res = mkdir(CLIENTDIRS, (0766 & ~077));
  56.    if (res == ERROR)
  57.       if (errno != EEXIST)
  58.       {
  59.          error("error making %s/%s: %s\n\n", SRSDIR, CLIENTDIRS,
  60.                strerror(errno));
  61.  
  62.          quit(ERROR);
  63.       }
  64.  
  65.    errno = 0;
  66.    res = chdir(CLIENTDIRS);
  67.    if (res == ERROR)
  68.    {
  69.       error("error chdir()'ing to %s/%s: %s\n\n", SRSDIR, CLIENTDIRS,
  70.             strerror(errno));
  71.  
  72.       quit(ERROR);
  73.    }
  74.  
  75.  
  76.    /* FOR CURRENT ID ------------------ */
  77.  
  78.  
  79.    (void)sprintf(curClientID, "%04d", (clients[curClient]).ID);
  80.  
  81.    debug("creating %s/%s/%s\n", SRSDIR, CLIENTDIRS, curClientID);
  82.  
  83.    errno = 0;
  84.  
  85.    res = mkdir(curClientID, (0766 & ~077)); 
  86.    if (res == ERROR)
  87.       if (errno != EEXIST) 
  88.       {
  89.          error("error creating %s/%s/%s: %s\n\n", SRSDIR, CLIENTDIRS,
  90.                curClientID, strerror(errno));
  91.  
  92.          quit(ERROR);
  93.       }
  94.  
  95.    errno = 0;
  96.    res = chdir(curClientID);
  97.    if (res == ERROR)
  98.    {
  99.       error("error chdir()'ing %s/%s/%s: %s\n\n", SRSDIR, CLIENTDIRS,
  100.             curClientID, strerror(errno));
  101.  
  102.       quit(ERROR);
  103.    }
  104.  
  105.    
  106. /* FOR SUB ID ------------------- */
  107.  
  108.    if (debugging == 1)
  109.    {
  110.       (void)putchar('\n');
  111.       (void)write(dblogfd, "\n", 1);
  112.    }
  113.  
  114.    debug("(in createDirs) numSubIDs = %d\n", clients[curClient].numSubIDs);
  115.  
  116.    (void)sprintf(curSubID, "%d", (clients[curClient]).numSubIDs);
  117.  
  118.    debug("creating %s/%s/%s/%s\n\n", SRSDIR, CLIENTDIRS, curClientID,
  119.          curSubID);
  120.  
  121.    errno = 0;
  122.    res = mkdir(curSubID, (0766 & ~077));
  123.    if (res == ERROR)
  124.       if (errno != EEXIST)
  125.       {
  126.          error("error creating %s/%s/%s/%s: %s\n\n", 
  127.                SRSDIR, CLIENTDIRS, curClientID, curSubID,
  128.                strerror(errno));
  129.  
  130.          quit(ERROR);
  131.  
  132.       }
  133.  
  134.    errno = 0;
  135.    res = chdir(curSubID);
  136.    if (res == ERROR)
  137.       if (errno != EEXIST) 
  138.       {
  139.          error("error chdir()'ing to %s/%s/%s/%s: %s\n\n", SRSDIR,
  140.                CLIENTDIRS, curClientID, curSubID, strerror(errno));
  141.  
  142.          quit(ERROR);
  143.       }
  144.  
  145.    if ((client != 1) && debugging == 1)
  146.    {
  147.       (void)putchar('\n');
  148.       (void)write(dblogfd, "\n", 1);
  149.    }
  150. }
  151.  
  152.  
  153. /* ----------------------- */
  154.  
  155.  
  156. /* make sure our parent pid is pidstats[curPid-1].pid */
  157. void checkValid()
  158. {
  159.    int res = 0;
  160.  
  161.    /* we have to kill our parent now... */
  162.    if ((clients[curClient].pidstats[curPid].ppid !=
  163.         clients[curClient].pidstats[curPid-1].pid) ||
  164.        (clients[curClient].pidstats[curPid].ppid != getppid()))
  165.    {
  166.       error("my parent pid [pid %d] is not pidstats[%d].pid (pid %d)!\n\n", 
  167.             getppid(), curPid-1, clients[curClient].pidstats[curPid-1].pid);
  168.  
  169.       res = seteuid(0);
  170.       if (res == ERROR)
  171.       {
  172.          error("error with seteuid: %s\n\n", strerror(errno));
  173.          quit(ERROR);
  174.       }
  175.  
  176.       if (clients[curClient].pidstats[curPid].ppid != getpid())
  177.       {
  178.          res = kill(clients[curClient].pidstats[curPid].ppid, SIGTERM);
  179.  
  180.          if (res == ERROR) 
  181.             error("error killing pid %d: %s\n\n", 
  182.                   clients[curClient].pidstats[curPid].ppid, strerror(errno));
  183.       }
  184.  
  185.       if (clients[curClient].pidstats[curPid-1].ppid != getpid())
  186.       {
  187.          res = kill(clients[curClient].pidstats[curPid-1].pid, SIGTERM);
  188.  
  189.          if (res == ERROR)
  190.             error("error killing pid %d: %s\n\n",
  191.                   clients[curClient].pidstats[curPid-1].pid, strerror(errno));
  192.       }
  193.  
  194.       res = kill(getppid(), SIGTERM);
  195.  
  196.       if (res == ERROR)
  197.          error("error killing pid %d: %s\n\n", getppid(), strerror(errno));
  198.  
  199.       res = seteuid(pwd->pw_uid);
  200.       if (res == ERROR)
  201.       {
  202.          error("error with seteuid: %s\n\n", strerror(errno));
  203.          quit(ERROR);
  204.       }
  205.  
  206.       quit(ERROR);
  207.    }
  208. }
  209.  
  210.  
  211. /* ----------------------- */
  212.  
  213.  
  214. /* check to see if we're over the max sub-ID limit */
  215. int checkMaxSubIDs()
  216. {
  217.    if (clients[curClient].numSubIDs > MAXSUBIDS)
  218.    {
  219.       signal(SIGPIPE, SIG_IGN);
  220.  
  221.       error("too many connections from client ID %04\n\n",
  222.             clients[curClient].ID);
  223.  
  224.       /* report error to client */
  225.       send_data((clients[curClient]).sockfd, "ERROR: %s\n", MAXSUBIDERROR);
  226.  
  227.       (void)sleep(MAXPAUSE); /* give them time to handle this */
  228.  
  229.  
  230.       clients[curClient].numSubIDs--;
  231.       close(clients[curClient].sockfd);
  232.  
  233.       return ERROR;
  234.    }
  235.  
  236.    return 0;
  237. }
  238.  
  239.  
  240. /* ----------------------- */
  241.  
  242.  
  243. /* parse and setup args */
  244. void doArgs(int argc, char **argv)
  245. {
  246.    int opt;
  247.  
  248.    if (argc > 1)
  249.    { 
  250.       /* -h == help, -d == debugging enabled,     */
  251.       /* -e == error file, -s == server list file */
  252.       /* -p == port to bind locally               */
  253.  
  254.       while ((opt = getopt(argc, argv, "hde:s:p:")) != ERROR)
  255.       {
  256.          int  testfd1;
  257.          FILE *testfd; /* test if user-defined files work */
  258.  
  259.          switch(opt)
  260.          {
  261.             case 'h':
  262.                usage(argv[0]);
  263.                exit(SUCCESS);
  264.  
  265.             case 'd':
  266.                debugging = 1;
  267.                debug("debugging enabled\n");
  268.  
  269.                break;
  270.  
  271.             case 'e':
  272.                errorFile = optarg;
  273.                debug("using %s to log errors\n", errorFile);
  274.  
  275.                while(1)
  276.                {
  277.                   testfd1 = open(errorFile, O_CREAT | O_WRONLY | O_APPEND, 
  278.                                  0600);
  279.  
  280.                   if (testfd1 == ERROR) 
  281.                   {
  282.                      if (errno == EINTR) continue;
  283.  
  284.                      (void)fprintf(stderr, "Unable to open %s: %s\n\n",
  285.                                    errorFile, strerror(errno));
  286.  
  287.                      exit(ERROR);
  288.                   }
  289.  
  290.                   else break;
  291.                }
  292.  
  293.                (void)close(testfd1);
  294.                break;
  295.  
  296.             case 's':
  297.                servListFile = optarg;
  298.                debug("using %s to get the server list\n", servListFile);
  299.  
  300.                while(1)
  301.                {
  302.                   testfd = fopen(servListFile, "r");
  303.                   if (testfd == NULL)
  304.                   {
  305.                      if (errno == EINTR) continue;
  306.  
  307.                      (void)fprintf(stderr, "Unable to open %s: %s\n\n",
  308.                                    servListFile, strerror(errno));
  309.  
  310.                      exit(ERROR);
  311.                   }
  312.  
  313.                   else break;                                 
  314.                   (void)chmod(servListFile, S_IREAD | S_IWRITE);
  315.                   (void)fclose(testfd);
  316.                }
  317.  
  318.                break;
  319.  
  320.             case 'p':
  321.                locPort = atoi(optarg);
  322.                break;
  323.  
  324.             case '?':
  325.                (void)fputc('\n', stderr), usage(argv[0]);
  326.                fprintf(stderr, "continuing anyway..\n\n");
  327.  
  328.                break;
  329.  
  330.             default:
  331.                fprintf(stderr, "getopt() returned %d.. exiting\n", opt);
  332.                exit(ERROR);
  333.  
  334.          }
  335.       }
  336.    }
  337. }
  338.  
  339.  
  340. /* ------------------------------- */
  341.  
  342.  
  343. /* get "OKAY" from the server */
  344. void getOkay()
  345. {
  346.    char readbuf[MAXREADSIZE];
  347.  
  348.    while(1)
  349.    {
  350.       memset(readbuf, 0, sizeof(readbuf));
  351.       recv_data(clients[curClient].sockfd, readbuf, sizeof(readbuf)-1,
  352.                 (child == 1 ? 1 : 0));
  353.  
  354.       if ((errors == 1) || (timeout == 1))
  355.       {
  356.          errors = 1;
  357.  
  358.          send_data(clients[curClient].sockfd, "ERROR: %s\n", CMDACKERROR);
  359.          longjmp(newconn, 1);
  360.       }
  361.  
  362.       if (strncmp(readbuf, "OKAY", 4) == 0)
  363.       {
  364.          if (silent != 1) debug("got OKAY\n");
  365.          return;
  366.       }
  367.  
  368.       else
  369.       {
  370.          error("OKAY not received... retrying\n\n");
  371.          continue;
  372.       }
  373.    }
  374. }
  375.  
  376.  
  377. /* ------------------------------- */
  378.  
  379.  
  380. /* put client's name in argv[] */
  381. void makeHostArgs(int argc, char **argv)
  382. {
  383.    register int i;
  384.  
  385.    if (argc == 2)
  386.       (void)sprintf(argv[1], "%s %04d %d", clients[curClient].hname,
  387.                     clients[curClient].ID, clients[curClient].numSubIDs);
  388.  
  389.    else if (argc > 2)
  390.    {
  391.       for (i = 0; argv[i] != NULL; i++)
  392.           memset(argv[i], 0, strlen(argv[i]));
  393.  
  394.       (void)sprintf(argv[1], "%s %04d %d", clients[curClient].hname,
  395.                     clients[curClient].ID, clients[curClient].numSubIDs);
  396.    }
  397. }
  398.  
  399.  
  400. /* ------------------------- */
  401.  
  402.  
  403. /* daemonize into background */
  404. void daemonize()
  405. {
  406.    int res;
  407.    res = fork();
  408.  
  409.    if (res == ERROR)
  410.    {
  411.       error("error forking into the background: %s\n\n",
  412.             strerror(errno));
  413.  
  414.       quit(ERROR);
  415.    }
  416.  
  417.    else if (res == 0) 
  418.    {
  419.       int res1;
  420.  
  421.       setsid();
  422.  
  423.       res1 = fork();
  424.       if (res1 == ERROR)
  425.       {
  426.          error("error with fork(): %s\n\n", strerror(errno));
  427.          quit(ERROR);
  428.       }
  429.  
  430.       else if (res1 == 0)
  431.       {
  432.          nullfd = open("/dev/null", O_RDONLY);
  433.          if (nullfd == ERROR)
  434.          {
  435.             error("error opening /dev/null: %s\n\n", strerror(errno));
  436.             quit(ERROR);
  437.          }
  438.  
  439.          (void)close(STDIN), (void)dup(nullfd);
  440.          (void)close(STDOUT), (void)dup(errlogfd);
  441.          (void)close(STDERR), (void)dup(errlogfd);
  442.       }
  443.  
  444.       else (void)exit(SUCCESS);
  445.    }
  446.  
  447.    else (void)exit(SUCCESS);
  448.  
  449.    umask(077);
  450.    mainpid = getpid();
  451. }
  452.  
  453.  
  454. /* ---------------------------------------- */
  455.  
  456.  
  457. /* export int for cmd len.. up to  65,535 bytes long) */
  458. char *exportInt(int value)
  459. {
  460.    static char out[2];
  461.  
  462.    out[0] = value, out[1] = value >> 8;
  463.    return out;
  464. }
  465.  
  466.  
  467. /* ---------------------------------------- */
  468.  
  469.  
  470. /* import int for cmd len.. up to  65,535 bytes long) */
  471. int importInt(char *value)
  472. {
  473.    u_char *in = (u_char *)value;
  474.    return ((int)in[0]) | ((int)in[1] << 8);
  475. }
  476.  
  477.  
  478. /* ---------------------------------------- */
  479.  
  480.  
  481. /* copy a log msg structure to a new one (to handle users) */
  482. void copyLogStruct(int srcstruct, int dststruct)
  483. {
  484.    clients[curClient].logs[dststruct].facility =
  485.         clients[curClient].logs[srcstruct].facility;
  486.  
  487.    clients[curClient].logs[dststruct].priority.single = 
  488.         clients[curClient].logs[srcstruct].priority.single;
  489.  
  490.    clients[curClient].logs[dststruct].priority.exclude = 
  491.         clients[curClient].logs[srcstruct].priority.exclude;
  492.  
  493.    clients[curClient].logs[dststruct].priority.ignpri = 
  494.         clients[curClient].logs[srcstruct].priority.ignpri;
  495.  
  496.    clients[curClient].logs[dststruct].priority.priority = 
  497.         clients[curClient].logs[srcstruct].priority.priority;
  498.  
  499.    clients[curClient].logs[dststruct].nsync = 
  500.         clients[curClient].logs[srcstruct].nsync;
  501. }
  502.  
  503.  
  504. /* ---------------------------------------- */
  505.  
  506.  
  507. /* get uid of SRS user */
  508. void getSRSuser()
  509. {
  510.    int count;
  511.    FILE *userfd;
  512.  
  513.    char *res;
  514.    char *userptr, *dataptr;
  515.  
  516.    char buf[MAXREADSIZE/4];
  517.    char srsuser[MAXUSERNAME+1];
  518.  
  519.    memset(srsuser, 0, sizeof(srsuser));
  520.  
  521.    userfd = fopen(USERFILE, "r");
  522.    if (userfd == NULL)
  523.    {
  524.       error("error opening %s: %s..\n"
  525.             "please run install.sh and read SRS.doc\n\n", 
  526.             USERFILE, strerror(errno));
  527.  
  528.       quit(ERROR);
  529.    }
  530.  
  531.    while(1)
  532.    {
  533.       memset(buf, 0, sizeof(buf));
  534.       res = fgets(buf, sizeof(buf)-1, userfd);
  535.  
  536.       if (res == NULL)
  537.       {
  538.          if (errno == EINTR) continue;
  539.          else if ((!feof(userfd)) && (errno > 0))
  540.          {
  541.             error("error reading from %s: %s\n\n", USERFILE, strerror(errno));
  542.             quit(ERROR);
  543.          }
  544.  
  545.          else
  546.          {
  547.             (void)fclose(userfd);
  548.             break;
  549.          }
  550.       }
  551.  
  552.       debug("(in getSRSuser) parsing line: %s%c", buf, 
  553.             (strchr(buf, '\n') == NULL ? '\n' : '\0'));
  554.  
  555.       if (isprint(buf[0]) == 0) continue;
  556.       else
  557.       {
  558.          if (buf[0] == '#') continue;
  559.          else
  560.          {
  561.             if (strchr(buf, '#') != NULL) (*(strchr(buf, '#'))) = '\0';
  562.             
  563.             count = 0, dataptr = buf, userptr = srsuser;
  564.             while ((*dataptr) && (isprint(*dataptr) != 0) &&
  565.                    (count < sizeof(srsuser)))
  566.             {
  567.                *userptr++ = *dataptr++;
  568.                count++;
  569.             }
  570.  
  571.             pwd = getpwnam(srsuser);
  572.  
  573.             if ((pwd != NULL) && (pwd->pw_uid > 0)) break;
  574.             else
  575.             {
  576.                if (pwd != NULL)
  577.                   error("the SRS user should not be root.. aborting\n\n");
  578.  
  579.                else error("error with getpwnam(%s): %s\n\n", srsuser,
  580.                           strerror(errno));
  581.  
  582.                quit(ERROR);
  583.             }
  584.          }
  585.       }
  586.    }
  587.  
  588.    (void)fclose(userfd);
  589. }
  590.