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

  1. #include "headers.h" /* has all important stuff */
  2.  
  3. /* this file has various functions to get/process data from client */
  4.  
  5. /* process the data from the client */
  6. /* this is the main processing function.. it processes */
  7. /* data and calls the appropriate functions            */
  8. void procData(int stopit)
  9. {
  10.    int res;
  11.    int stop = 0;
  12.    char readbuf[MAXREADSIZE];
  13.  
  14.    errors = 0;
  15.  
  16.    /* make sure pids and structs are right */
  17.    if (stopit != 1) checkValid1();
  18.    else
  19.    {
  20.       timeout = 0, errors = 0;
  21.  
  22.       signal(SIGALRM, IDtimeout);
  23.       (void)alarm((MAXPAUSE * 3) * 2);
  24.  
  25.       /* first thing the client sends (this should be ID) */
  26.  
  27.       memset(readbuf, 0, sizeof(readbuf)); /* [re]clear buffer */
  28.       recv_data(clients[curClient].sockfd, readbuf, sizeof(readbuf)-1, 0);
  29.  
  30.       if ((timeout == 1) || (errors == 1))
  31.       {
  32.          (void)alarm(0);
  33.  
  34.          errors = 1;
  35.          return;
  36.       }
  37.  
  38.       debug("(in procData) ID part.. data is: %s\n", readbuf);
  39.  
  40.       if (strncmp(readbuf, "ID ", 3) == 0) 
  41.       {
  42.          (void)alarm(0);
  43.          authClient(readbuf, strlen(readbuf)); 
  44.  
  45.          if ((timeout == 1) || (errors == 1))
  46.          {
  47.             errors = 1;
  48.             return;
  49.          }
  50.       }
  51.  
  52.       else
  53.       {
  54.          alarm(0);
  55.  
  56.          error("client did not send \"ID\"\n\n");
  57.          send_data(clients[curClient].sockfd, "ERROR: %s\n", IDERROR);
  58.          
  59.          errors = 1;
  60.          return;
  61.       }
  62.  
  63.       return;
  64.    }
  65.  
  66.    res = seteuid(0);
  67.    if (res == ERROR)
  68.    {
  69.       error("error with seteuid: %s\n\n", strerror(errno));
  70.       quit(ERROR);
  71.    }
  72.  
  73.    /* we now have the client's ID.. parsed while inside authClient() */
  74.    /* create directories for clients.. in CLIENTDIRS */
  75.    createDirs(1); /* 1 == we have a client */
  76.  
  77.    res = seteuid(pwd->pw_uid);
  78.    if (res == ERROR)
  79.    {
  80.       error("error with seteuid: %s\n\n", strerror(errno));
  81.       quit(ERROR);
  82.    }
  83.  
  84.    /* loop until we get a command we understand */
  85.    /* next command will be VERS & SERVLIST, or SUBID...     */
  86.    /* depending on whether we're an info or a stream server */   
  87.  
  88.    signal(SIGALRM, InsTimeout);
  89.    (void)alarm(MAXTIMEOUT); /* give them a minute to send command */
  90.  
  91.    while(1)
  92.    {
  93.       debug("(in procData) waiting for next instruction\n\n");
  94.  
  95.       memset(readbuf, 0, sizeof(readbuf)); /* [re]clear buffer */
  96.  
  97.       recv_data(clients[curClient].sockfd, readbuf, sizeof(readbuf)-1,
  98.                 ((child == 1) ? 1 : 0));
  99.  
  100.       if ((timeout == 1) || (errors == 1))
  101.       {
  102.          (void)alarm(0);
  103.          errors = 1;
  104.          return;
  105.       }
  106.  
  107.       /* we use if and not else if for the next few instructions, */
  108.       /* in case they send several instructions at the same time  */
  109.  
  110.       /* client wants current client & server version */
  111.       if (strncmp(readbuf, "VERS", 4) == 0) 
  112.       {
  113.          if (!stop) (void)alarm(0);
  114.  
  115.          if (debugging == 1)
  116.          {
  117.             (void)putchar('\n');
  118.             (void)write(dblogfd, "\n", 1);
  119.          }
  120.  
  121.          debug("sending the client the current versions...\n");
  122.          sendVers(); /* send current server and client version */
  123.       }
  124.  
  125.       else if (strncmp(readbuf, "SERVLIST", 8) == 0)
  126.       {
  127.          if (!stop) (void)alarm(0);
  128.  
  129.          if (debugging == 1)
  130.          {
  131.             (void)putchar('\n');
  132.             (void)write(dblogfd, "\n", 1);
  133.          }
  134.  
  135.          debug("sending client the streaming server list...\n");
  136.          sendServList();
  137.       }
  138.  
  139.  
  140.       /* client wants its sub-ID */
  141.       else if (strncmp(readbuf, "SUBID", 5) == 0)
  142.       {
  143.          if (!stop) (void)alarm(0);
  144.  
  145.          if (debugging == 1)
  146.          {
  147.             (void)putchar('\n');
  148.             (void)write(dblogfd, "\n", 1);
  149.          }
  150.  
  151.          debug("sending client its sub-ID...\n");
  152.          sendSubID();
  153.       }
  154.  
  155.       /* client is going to start streaming */
  156.       else if (strncmp(readbuf, "START SYSLOG.CONF", 17) == 0)
  157.       {
  158.          if (!stop) (void)alarm(0);
  159.  
  160.          if (debugging == 1)
  161.          {
  162.             (void)putchar('\n');
  163.             (void)write(dblogfd, "\n", 1);
  164.          }
  165.  
  166.          debug("now getting syslog.conf from client...\n");
  167.          readSysConf();
  168.          debug("now finished with syslog.conf...\n");
  169.       }
  170.  
  171.       /* client is going to start streaming */
  172.       else if (strncmp(readbuf, "START STREAM", 12) == 0)
  173.       {
  174.          if (!stop) (void)alarm(0);
  175.  
  176.          if (debugging == 1)
  177.          {
  178.             (void)putchar('\n');
  179.             (void)write(dblogfd, "\n", 1);
  180.          }
  181.  
  182.          debug("received request to start streaming...\n");
  183.  
  184.          readStream();
  185.       }
  186.  
  187.  
  188.       else if (strncmp(readbuf, "START PINGS", 11) == 0)
  189.       {
  190.          if (!stop) (void)alarm(0);
  191.  
  192.          if (debugging == 1)
  193.          {
  194.             (void)putchar('\n');
  195.             (void)write(dblogfd, "\n", 1);
  196.          }
  197.  
  198.          if (clients[curClient].pinging != NOPING)
  199.          {
  200.             send_data(clients[curClient].sockfd, "YES PINGING\n");
  201.             startPings();
  202.          }
  203.  
  204.          else send_data(clients[curClient].sockfd, "NO PINGING\n");
  205.       }
  206.  
  207.       /* client's way of checking of ensuring it's okay */
  208.       else if (strncmp(readbuf, "PING CHECK", 10) == 0)
  209.       { 
  210.          /* do nothing */
  211.          continue;
  212.       }
  213.  
  214.       else 
  215.       {
  216.          error("command from client not understood.. ignoring\n\n");
  217.          continue;
  218.       }
  219.  
  220.       /* we had a timeout waiting for a valid instruction */
  221.       if (stop) break;
  222.    }
  223. }
  224.  
  225.  
  226. /* -------------------- */
  227.  
  228.  
  229. /* send client the current server/client versions */
  230. void sendVers()
  231. {
  232.    send_data((clients[curClient]).sockfd, "CVER %s, SVER %s\n", 
  233.              CVER, SVER);
  234. }
  235.  
  236.  
  237. /* -------------------- */
  238.  
  239.  
  240. /* send client the current stream server list */
  241. void sendServList()
  242. {
  243.    FILE *file;
  244.  
  245.    int res1;
  246.    char *res; /* has results from various functions */
  247.  
  248.    char buf[MAXWRITESIZE];
  249.    char servFile[MAXFNAMESIZE];
  250.  
  251.    memset(buf, 0, sizeof(buf));
  252.    memset(servFile, 0, sizeof(servFile));
  253.  
  254.    if (servListFile == NULL)
  255.       sprintf(servFile, "%s", SERVFILE);
  256.  
  257.    else
  258.       sprintf(servFile, "%s", servListFile);
  259.  
  260.    res1 = seteuid(0);
  261.    if (res1 == ERROR)
  262.    {
  263.       error("error with seteuid: %s\n\n", strerror(errno));
  264.       quit(ERROR);
  265.    }
  266.  
  267.    file = fopen(servFile, "r");
  268.    if (file == NULL)
  269.    {
  270.       error("error opening %s: %s..\n"
  271.             "put the server list into it to fix\n\n", SERVFILE, 
  272.              strerror(errno));
  273.  
  274.       quit(ERROR);
  275.    }
  276.    
  277.    res1 = seteuid(pwd->pw_uid);
  278.    if (res1 == ERROR)
  279.    {
  280.       error("error with seteuid: %s\n\n", strerror(errno));
  281.       quit(ERROR);
  282.    }
  283.  
  284.    while(1)
  285.    {
  286.       errno = 0;
  287.       memset(buf, 0, sizeof(buf));
  288.  
  289.       res = fgets(buf, sizeof(buf)-2, file);
  290.       buf[sizeof(buf)-1] = '\0';
  291.  
  292.       errors = 0;
  293.       if (res == NULL)
  294.       {
  295.          if (errno == EINTR) continue;
  296.          else if ((!feof(file)) && (errno > 0))
  297.          {
  298.             error("error reading from server list file:\n%s\n\n", 
  299.                   strerror(errno));
  300.  
  301.             quit(ERROR);
  302.          }
  303.  
  304.          else
  305.          {
  306.             (void)fclose(file);
  307.             break;
  308.          }
  309.       }
  310.  
  311.       if ((buf == NULL) || (buf[0] == '\0') || (isprint(buf[0]) == 0))
  312.          continue;
  313.  
  314.       else if (strchr(buf, '#') != NULL)
  315.       {
  316.          if (buf[0] == '#') continue;
  317.          else 
  318.          {
  319.             char *ptr = strchr(buf, '#');
  320.             if (ptr != NULL) *ptr = '\0';
  321.          }
  322.       }
  323.  
  324.       send_data((clients[curClient]).sockfd, "SERV %s%c", 
  325.                 buf, (strchr(buf, '\n') == NULL ? '\n' : '\0'));
  326.    }
  327.  
  328.    send_data((clients[curClient]).sockfd, "DONE\n");
  329. }
  330.  
  331.  
  332. /* -------------------- */
  333.  
  334.  
  335. /* send sub-ID to the client */
  336. void sendSubID()
  337. {
  338.    send_data((clients[curClient]).sockfd, "SUBID %04d\n", 
  339.              (clients[curClient]).numSubIDs);
  340. }
  341.  
  342.  
  343. /* -------------------- */
  344.  
  345.  
  346. /* make sure the structs and pids are valid */
  347. void checkValid1()
  348. {
  349.    if (getpid() != (clients[curClient]).pidstats[curPid].pid) 
  350.    {
  351.       error("my pid [pid %d] != pidstats[%d].pid (pid %d)\n\n",
  352.             getpid(), curPid, clients[curClient].pidstats[curPid].pid);
  353.  
  354.       quit(ERROR);
  355.    }
  356. }
  357.