/************************************************************************/ /* */ /* Generic CGI responder */ /* Version 3.2 */ /* */ /************************************************************************/ #include #include #include /***************************************************************/ /* Do we want to echo the form results into an email message? */ /* Define as 1 for Yes, or 0 for No */ /***************************************************************/ #define SEND_EMAIL_RESULTS 1 /**************************************************/ /* Who should the email go to? */ /**************************************************/ #define CONTACT "root" /**************************************************/ /* If no subject is given, what should we use? */ /**************************************************/ #define DEFAULT_SUBJECT "WWW: Form output from a WWW user" /***************************************************************/ /* Do we want to save the form results to a file? */ /* Define as 1 for Yes, or 0 for No */ /***************************************************************/ #define SAVE_RESULTS_TO_FILE 1 /*************************************************/ /* What file do we want to save the results in? */ /*************************************************/ #define SAVE_FILE "/www/output/sample-www-results" /***********************************************************/ /* The HEADER of the HTML page we send for an OK response */ /***********************************************************/ #define OK_PAGE_HEADER "Thanks!

Thanks!


Your submission was accepted

" /******************************************************************/ /* Do we want to echo the results of the form back to the user? */ /* Define as 1 for Yes, or 0 for No */ /******************************************************************/ #define ECHO_RESULTS 1 /*****************************************************/ /* The FOOTER of the page we send for an OK response */ /*****************************************************/ #define OK_PAGE_FOOTER " Go back to the HomePage " /****************************************************/ #define MAX_ENTRIES 10000 #define LF 10 #define CR 13 extern char *getenv(); char *makeword(); char *fmakeword(); char x2c(); void unescape_url(); void plustospace(); typedef struct { char *name; char *val; } entry; /**********************************************************************/ /* Main */ /**********************************************************************/ main(argc, argv) int argc; char *argv[]; { entry entries[MAX_ENTRIES]; register int x,m=0; int cl; int last_entry; FILE *mf; /* pointer to the mail file */ FILE *lf; /* pointer to the log file */ char buf[999]; /************************/ /* Send out the header */ /************************/ printf("Content-type: text/html%c%c",10,10); /**************************/ /* Do some error checking */ /**************************/ if(strcmp(getenv("REQUEST_METHOD"),"POST")) reject_not_post(); if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) reject_not_decode(); /*************************************/ /* Main loop to extract POST content */ /*************************************/ cl = atoi(getenv("CONTENT_LENGTH")); for(x=0;cl && (!feof(stdin));x++) { m=x; entries[x].val = fmakeword(stdin,'&',&cl); plustospace(entries[x].val); unescape_url(entries[x].val); entries[x].name = makeword(entries[x].val,'='); } last_entry=m; /*******************************************/ /* Send mail with the contents of the form */ /*******************************************/ if( SEND_EMAIL_RESULTS == 1 ) { sprintf(buf, "/usr/bin/mail -s '%s' %s", DEFAULT_SUBJECT, CONTACT); mf = popen(buf, "w"); fprintf(mf, "\n\n"); fprintf(mf, "*** These are form results from a WWW user:\n\n"); for(x=0; x <= last_entry; x++) { fprintf(mf, "%s: %s\n", entries[x].name, entries[x].val); } fprintf(mf, "\n\n----------\n"); pclose(mf); } /*******************************************/ /* Save form contents to file */ /*******************************************/ if( SAVE_RESULTS_TO_FILE == 1 ) { /* Open the file, appending data to end if the file exists, */ /* or creating the file if it does not exist */ lf = fopen(SAVE_FILE, "a"); sprintf(buf, ">>>>>>>>>>>>>>> WWW form results:\n"); fputs(buf, lf); for(x=0; x <= last_entry; x++) { sprintf(buf, "%s: %s\n", entries[x].name, entries[x].val); fputs(buf, lf); } sprintf(buf, "\n\n"); fputs(buf, lf); fclose(lf); /* Close the log file */ } /*******************************************/ /* Send HTML page confirmation to web user */ /*******************************************/ printf("%s", OK_PAGE_HEADER); if(ECHO_RESULTS == 1) { printf("


You sent the following:

");
      for(x=0; x <= last_entry; x++) {
       printf("%s: %s\n", entries[x].name, entries[x].val);
      }
      printf("


"); } printf("%s", OK_PAGE_FOOTER); exit(1); } /************************* End of Main ****************************/ /****************************************************************/ /* REJECT pages: */ /****************************************************************/ /************************************************************/ /* User did not access with method of POST, so we reject it */ /************************************************************/ reject_not_post() { printf("Forum Refused"); printf("

Form Refused

"); printf("This script should be referenced with a METHOD of POST.

"); printf("%c", 10); exit(1); } /***********************************************************/ /* User did not send from a form, so we reject it */ /***********************************************************/ reject_not_decode() { printf("Forum Refused"); printf("

Form Refused

"); printf("This script can only be used to decode form results.

"); printf("%c", 10); exit(1); } /***********************************************************/ /* POST utilities: */ /***********************************************************/ /*****************************/ /* getword */ /*****************************/ void getword(word, line, stop) char *word, *line, stop; { int x = 0,y; for(x=0;((line[x]) && (line[x] != stop));x++) word[x] = line[x]; word[x] = '\0'; if(line[x]) ++x; y=0; while(line[y++] = line[x++]); } /*****************************/ /* makeword */ /*****************************/ char *makeword(line, stop) char *line, stop; { int x = 0,y; char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1)); for(x=0;((line[x]) && (line[x] != stop));x++) word[x] = line[x]; word[x] = '\0'; if(line[x]) ++x; y=0; while(line[y++] = line[x++]); return word; } /*****************************/ /* fmakeword */ /*****************************/ char *fmakeword(f, stop, cl) FILE *f; char stop; int *cl; { int wsize; char *word; int ll; wsize = 102400; ll=0; word = (char *) malloc(sizeof(char) * (wsize + 1)); while(1) { word[ll] = (char)fgetc(f); if(ll==wsize) { word[ll+1] = '\0'; wsize+=102400; word = (char *)realloc(word,sizeof(char)*(wsize+1)); } --(*cl); if((word[ll] == stop) || (feof(f)) || (!(*cl))) { if(word[ll] != stop) ll++; word[ll] = '\0'; return word; } ++ll; } } /*****************************/ /* x2c */ /*****************************/ char x2c(what) char *what; { register char digit; digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0')); digit *= 16; digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0')); return(digit); } /*****************************/ /* unescape_url */ /*****************************/ void unescape_url(url) char *url; { register int x,y; for(x=0,y=0;url[y];++x,++y) { if((url[x] = url[y]) == '%') { url[x] = x2c(&url[y+1]); y+=2; } } url[x] = '\0'; } /*****************************/ /* plustospace */ /*****************************/ void plustospace(str) char *str; { register int x; for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' '; } /*****************************/ /* rind */ /*****************************/ int rind(s,c) char *s,c; { register int x; for(x=strlen(s) - 1;x != -1; x--) if(s[x] == c) return x; return -1; } /*****************************/ /* getline */ /*****************************/ int getline(s, n, f) char *s; int n; FILE *f; { register int i=0; while(1) { s[i] = (char)fgetc(f); if(s[i] == CR) s[i] = fgetc(f); if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) { s[i] = '\0'; return (feof(f) ? 1 : 0); } ++i; } } /*****************************/ /* send_fd */ /*****************************/ void send_fd(f, fd) FILE *f, *fd; { int num_chars=0; char c; while (1) { c = fgetc(f); if(feof(f)) return; fputc(c,fd); } } /**** END ****/