home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / getstats.zip / statform.c < prev    next >
C/C++ Source or Header  |  1995-04-11  |  10KB  |  401 lines

  1. /*
  2. ** statform 1.0
  3. ** A getstats form interface
  4. ** By Brian Behlendorf, gw@wired.com
  5. ** Hacked on 4/15/94 by Kevin Hughes, kevinh@eit.com
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. /*
  13.  
  14. WARNING! THIS FORM HAS A SECURITY BUG!
  15. A new version will be out shortly which fixes the problem
  16. (and others associated with this version).
  17. I'll try to get it out soon, but in the meantime I'll 
  18. gladly accept patches! -- Kevin
  19.  
  20. */
  21.  
  22. /* User-definable options below */
  23.  
  24. char LOGFILE[80] = "/usr/local/httpd/logs/access_log";
  25.     /*
  26.     ** The full path to your log file. This is used as a default.
  27.     */
  28. char ROOTDIR[80] = "/usr/local/www/";
  29.     /*
  30.     ** The top of your Web (or Gopher) tree.
  31.     */
  32. char GETSTATS[80] = "/usr/local/httpd/cgi-bin/getstats";
  33.     /*
  34.     ** The full path to getstats, version 1.2 or better.
  35.     */
  36.  
  37. /* End of user-definable options */
  38.  
  39. #define SUBJECT "Getstats results"
  40. #define MAXENTRIES 10000
  41. #define MAXCMDLEN 1024
  42.  
  43. typedef struct {
  44.     char *name;
  45.     char *val;
  46. } entry;
  47.  
  48. char command[MAXCMDLEN];
  49.  
  50. char *makeword(char *line, char stop);
  51. char *fmakeword(FILE *f, char stop, int *len);
  52. char x2c(char *what);
  53. void unescape_url(char *url);
  54. void plustospace(char *str);
  55.  
  56. main(int argc, char *argv[])
  57. {
  58.     entry entries[MAXENTRIES];
  59.     register int x, m = 0;
  60.     int cl, invalid = 0, didroot = 0, didhtml = 0;
  61.     
  62.     char protocol;
  63.  
  64.     if (strcmp(getenv("REQUEST_METHOD"), "POST")) {
  65.         printf("We can't do much with a blank subscription card, ");
  66.         printf("can we?\n");
  67.         exit(1);
  68.     }
  69.  
  70.     if (strcmp(getenv("CONTENT_TYPE"),
  71.     "application/x-www-form-urlencoded")) {
  72.         printf("This script can only be used to decode form ");
  73.         printf("results.\n");
  74.         exit(1);
  75.     }
  76.  
  77.     cl = atoi(getenv("CONTENT_LENGTH"));
  78.     
  79.     sprintf(command, "%s ", GETSTATS);
  80.  
  81.     for(x = 0; cl && (!feof(stdin)); x++) {
  82.         m = x;
  83.         entries[x].val = fmakeword(stdin, '&', &cl);
  84.         plustospace(entries[x].val);
  85.         unescape_url(entries[x].val);
  86.         entries[x].name = makeword(entries[x].val,'=');
  87.  
  88.         if (!strcmp(entries[x].name, "protocol")) {
  89.             protocol = entries[x].val[0];
  90.  
  91.             if (protocol == 'G') 
  92.                 sprintf(command, "%s -G %s", command);
  93.             else if (protocol == 'N')
  94.                 sprintf(command, "%s -N %s", command);
  95.             else if (protocol == 'C')
  96.                 sprintf(command, "%s -C %s", command);
  97.             else if (protocol == 'P')
  98.                 sprintf(command, "%s -P %s", command);
  99.             else if (protocol == 'M')
  100.                 sprintf(command, "%s -A %s", command);
  101.             else if (protocol == 'U')
  102.                 sprintf(command, "%s -O %s", command);
  103.         }
  104.  
  105.         if (!didroot) {
  106.             sprintf(command, "%s -dr %s", command, ROOTDIR);
  107.             didroot = 1;
  108.         }
  109.  
  110.         if (!strcmp(entries[x].name, "logfilename"))
  111.             if (strlen(entries[x].val)) 
  112.                 sprintf(command, "%s -l %s", command,
  113.                 entries[x].val);
  114.             else 
  115.                 sprintf(command, "%s -l %s", command, LOGFILE);
  116.  
  117.         if (!strcmp(entries[x].name, "common") && 
  118.         !strcmp(entries[x].val, "on"))
  119.             sprintf(command, "%s -M", command);
  120.  
  121.         if (!strcmp(entries[x].name, "concise") && 
  122.         !strcmp(entries[x].val, "on"))
  123.             sprintf(command, "%s -c", command);
  124.  
  125.         if (!strcmp(entries[x].name, "toplines") && 
  126.         strlen(entries[x].val))
  127.             sprintf(command, "%s -t %s ", command, entries[x].val);
  128.  
  129.         if (!strcmp(entries[x].name, "all") && 
  130.         !strcmp(entries[x].val, "on"))
  131.             sprintf(command, "%s -a ", command);
  132.  
  133.         if (!strcmp(entries[x].name, "monthly") && 
  134.         !strcmp(entries[x].val, "on"))
  135.             sprintf(command, "%s -m ", command);
  136.  
  137.         if (!strcmp(entries[x].name, "weekly") && 
  138.         !strcmp(entries[x].val, "on"))
  139.             sprintf(command, "%s -w ", command);
  140.  
  141.         if (!strcmp(entries[x].name, "daysweek") && 
  142.         !strcmp(entries[x].val, "on"))
  143.             sprintf(command, "%s -ds ", command);
  144.  
  145.         if (!strcmp(entries[x].name, "daily") && 
  146.         !strcmp(entries[x].val, "on"))
  147.             sprintf(command, "%s -d ", command);
  148.  
  149.         if (!strcmp(entries[x].name, "hoursday") && 
  150.         !strcmp(entries[x].val, "on"))
  151.             sprintf(command, "%s -hs ", command);
  152.  
  153.         if (!strcmp(entries[x].name, "hourly") && 
  154.         !strcmp(entries[x].val, "on"))
  155.             sprintf(command, "%s -h ", command);
  156.  
  157.         if (!strcmp(entries[x].name, "full-hostname") && 
  158.         !strcmp(entries[x].val, "on"))
  159.             sprintf(command, "%s -f ", command);
  160.  
  161.         if (!strcmp(entries[x].name, "full-access") && 
  162.         !strcmp(entries[x].val, "on"))
  163.             sprintf(command, "%s -fa ", command);
  164.  
  165.         if (!strcmp(entries[x].name, "full-lastaccess") && 
  166.         !strcmp(entries[x].val, "on"))
  167.             sprintf(command, "%s -fd ", command);
  168.  
  169.         if (!strcmp(entries[x].name, "full-bytes") && 
  170.         !strcmp(entries[x].val, "on"))
  171.             sprintf(command, "%s -fb ", command);
  172.  
  173.         if (!strcmp(entries[x].name, "request-name") && 
  174.         !strcmp(entries[x].val, "on"))
  175.             sprintf(command, "%s -r ", command);
  176.  
  177.         if (!strcmp(entries[x].name, "request-accesses") && 
  178.         !strcmp(entries[x].val, "on"))
  179.             sprintf(command, "%s -ra ", command);
  180.  
  181.         if (!strcmp(entries[x].name, "request-lastaccesses") && 
  182.         !strcmp(entries[x].val, "on"))
  183.             sprintf(command, "%s -rd ", command);
  184.  
  185.         if (!strcmp(entries[x].name, "request-bytes") && 
  186.         !strcmp(entries[x].val, "on"))
  187.             sprintf(command, "%s -rb ", command);
  188.  
  189.         if (!strcmp(entries[x].name, "request-filesize") && 
  190.         !strcmp(entries[x].val, "on"))
  191.             sprintf(command, "%s -rf ", command);
  192.  
  193.         if (!strcmp(entries[x].name, "domain-name") && 
  194.         !strcmp(entries[x].val, "on"))
  195.             sprintf(command, "%s -dn ", command);
  196.  
  197.         if (!strcmp(entries[x].name, "domain-requests") && 
  198.         !strcmp(entries[x].val, "on"))
  199.             sprintf(command, "%s -da ", command);
  200.  
  201.         if (!strcmp(entries[x].name, "domain-lastaccesses") && 
  202.         !strcmp(entries[x].val, "on"))
  203.             sprintf(command, "%s -dd ", command);
  204.  
  205.         if (!strcmp(entries[x].name, "domain-bytes") && 
  206.         !strcmp(entries[x].val, "on"))
  207.             sprintf(command, "%s -db ", command);
  208.  
  209.         if (!strcmp(entries[x].name, "domain-sub") && 
  210.         !strcmp(entries[x].val, "on"))
  211.             sprintf(command, "%s -du ", command);
  212.  
  213.         if (!strcmp(entries[x].name, "filetree") && 
  214.         !strcmp(entries[x].val, "on"))
  215.             sprintf(command, "%s -dt ", command);
  216.  
  217.         if (!strcmp(entries[x].name, "error") && 
  218.         !strcmp(entries[x].val, "on"))
  219.             sprintf(command, "%s -e ", command);
  220.  
  221.         if (!strcmp(entries[x].name, "samask"))
  222.             if (strlen(entries[x].val) != 0)
  223.                 sprintf(command, "%s -sa \"%s\"", command,
  224.                 entries[x].val);
  225.             
  226.         if (!strcmp(entries[x].name, "ssmask"))
  227.             if (strlen(entries[x].val) != 0)
  228.                 sprintf(command, "%s -ss \"%s\"", command,
  229.                 entries[x].val);
  230.             
  231.         if (!strcmp(entries[x].name, "srmask"))
  232.             if (strlen(entries[x].val) != 0)
  233.                 sprintf(command, "%s -sr \"%s\"", command,
  234.                 entries[x].val);
  235.             
  236.         if (!strcmp(entries[x].name, "spmask"))
  237.             if (strlen(entries[x].val) != 0)
  238.                 sprintf(command, "%s -sp \"%s\"", command,
  239.                 entries[x].val);
  240.             
  241.         if (!strcmp(entries[x].name, "sdmask"))
  242.             if (strlen(entries[x].val) != 0)
  243.                 sprintf(command, "%s -sd \"%s\"", command,
  244.                 entries[x].val);
  245.             
  246.         if (!strcmp(entries[x].name, "shmask"))
  247.             if (strlen(entries[x].val) != 0)
  248.                 sprintf(command, "%s -sh \"%s\"", command,
  249.                 entries[x].val);
  250.             
  251.         if (!strcmp(entries[x].name, "swmask"))
  252.             if (strlen(entries[x].val) != 0)
  253.                 sprintf(command, "%s -sw \"%s\"", command,
  254.                 entries[x].val);
  255.             
  256.         if (!strcmp(entries[x].name, "mailme") && 
  257.         strlen(entries[x].val) != 0) {
  258.             sprintf(command, "%s | /usr/ucb/mail -s \"%s\" %s",
  259.             command, SUBJECT, entries[x].val);
  260.             printf("Content-type: text/html\n\n");
  261.             printf("<title>It's in the mail...</title>\n<p>\n");
  262.             printf("<h1>Your log request is being mailed to ");
  263.             printf("you.</h1>\n");
  264.             printf("The following command was executed:\n");
  265.             printf("<p>\n<code>%s</code>\n", command);
  266.         }
  267.         else if (!strcmp(entries[x].name, "mailme") &&
  268.         strlen(entries[x].val) == 0 && !didhtml) {
  269.             sprintf(command, "%s -ht", command);
  270.             didhtml = 1;
  271.         }
  272.     }
  273.     sprintf(command, "%s &\n", command);
  274.     system(command);
  275.  
  276.     exit(0);
  277. }
  278.  
  279. /* util.c from NCSA included below */
  280.  
  281. #define LF 10
  282. #define CR 13
  283.  
  284. void getword(char *word, char *line, char stop) {
  285.     int x = 0,y;
  286.  
  287.     for(x=0;((line[x]) && (line[x] != stop));x++)
  288.         word[x] = line[x];
  289.  
  290.     word[x] = '\0';
  291.     if(line[x]) ++x;
  292.     y=0;
  293.  
  294.     while(line[y++] = line[x++]);
  295. }
  296.  
  297. char *makeword(char *line, char stop) {
  298.     int x = 0,y;
  299.     char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
  300.  
  301.     for(x=0;((line[x]) && (line[x] != stop));x++)
  302.         word[x] = line[x];
  303.  
  304.     word[x] = '\0';
  305.     if(line[x]) ++x;
  306.     y=0;
  307.  
  308.     while(line[y++] = line[x++]);
  309.     return word;
  310. }
  311.  
  312. char *fmakeword(FILE *f, char stop, int *cl) {
  313.     int wsize;
  314.     char *word;
  315.     int ll;
  316.  
  317.     wsize = 102400;
  318.     ll=0;
  319.     word = (char *) malloc(sizeof(char) * (wsize + 1));
  320.  
  321.     while(1) {
  322.         word[ll] = (char)fgetc(f);
  323.         if(ll==wsize) {
  324.             word[ll+1] = '\0';
  325.             wsize+=102400;
  326.             word = (char *)realloc(word,sizeof(char)*(wsize+1));
  327.         }
  328.         --(*cl);
  329.         if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
  330.             if(word[ll] != stop) ll++;
  331.             word[ll] = '\0';
  332.             return word;
  333.         }
  334.         ++ll;
  335.     }
  336. }
  337.  
  338. char x2c(char *what) {
  339.     register char digit;
  340.  
  341.     digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
  342.     digit *= 16;
  343.     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
  344.     return(digit);
  345. }
  346.  
  347. void unescape_url(char *url) {
  348.     register int x,y;
  349.  
  350.     for(x=0,y=0;url[y];++x,++y) {
  351.         if((url[x] = url[y]) == '%') {
  352.             url[x] = x2c(&url[y+1]);
  353.             y+=2;
  354.         }
  355.     }
  356.     url[x] = '\0';
  357. }
  358.  
  359. void plustospace(char *str) {
  360.     register int x;
  361.  
  362.     for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
  363. }
  364.  
  365. int rind(char *s, char c) {
  366.     register int x;
  367.     for(x=strlen(s) - 1;x != -1; x--)
  368.         if(s[x] == c) return x;
  369.     return -1;
  370. }
  371.  
  372. int getline(char *s, int n, FILE *f) {
  373.     register int i=0;
  374.  
  375.     while(1) {
  376.         s[i] = (char)fgetc(f);
  377.  
  378.         if(s[i] == CR)
  379.             s[i] = fgetc(f);
  380.  
  381.         if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
  382.             s[i] = '\0';
  383.             return (feof(f) ? 1 : 0);
  384.         }
  385.         ++i;
  386.     }
  387. }
  388.  
  389. void send_fd(FILE *f, FILE *fd)
  390. {
  391.     int num_chars=0;
  392.     char c;
  393.  
  394.     while (1) {
  395.         c = fgetc(f);
  396.         if(feof(f))
  397.             return;
  398.         fputc(c,fd);
  399.     }
  400. }
  401.