#include #include #include #include #include "cgi.h" #define MAX_ENTRIES 10000 void log_error(char *str) { FILE *logFile; time_t now; time(&now); logFile = fopen("errors.txt", "a"); if(logFile != NULL) { fprintf(logFile, "%s\t%s\n", ctime(&now), str); fclose(logFile); } printf("An error occured in processing.\n"); printf("Our apologies. Please try again later, or email wsnz@ihug.co.nz with your request.\n"); exit(0); } typedef struct { char *name; char *val; } entry; char *makeword(char *line, char stop); char *fmakeword(FILE *f, char stop, int *len); char x2c(char *what); void unescape_url(char *url); void plustospace(char *str); void missing_entry(char * entry); main(int argc, char *argv[]) { entry entries[MAX_ENTRIES]; register int x,m=0; int cl, count, alreadyIn = 0; FILE *my_f; char *my_b, file_name[80], email_address[80], line[1024]; printf("Content-type: text/html%c%c",10,10); if(strcmp(getenv("REQUEST_METHOD"),"POST")) { printf("This script should be referenced with a METHOD of POST.\n"); exit(1); } if(strcmp(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded")) { printf("This script can only be used to decode form results. \n"); exit(1); } 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,'='); } /* make sure that they've entered all the things that they need to enter */ if(entries[0].val[0] == '\0') missing_entry("file name"); if(entries[1].val[0] == '\0') missing_entry("email address"); /* convert everything to useful values */ strcpy(file_name, "lists/"); strcat(file_name, entries[0].val); /* don't want people sending anything other than lower case letters for the file name - no .. or any other commands */ count = 6; while(count < strlen(file_name)) { if((file_name[count] < 'a') || (file_name[count] > 'z')) log_error(file_name); count++; } strcat(file_name, ".txt"); strcpy(email_address, entries[1].val); /* first check and see if the email address is already in the file */ my_f = fopen(file_name, "r"); if(my_f == NULL) log_error("Unable to open email file for reading."); my_b = fgets(line, 101, my_f); while((!alreadyIn) && (my_b != NULL)) { line[strlen(line)-1] = '\0'; if(!strcmp(line, email_address)) alreadyIn = 1; my_b = fgets(line, 101, my_f); } close(my_f); /* write the email address to the file if it's not already there */ if(!alreadyIn) { my_f = fopen(file_name, "a"); if(my_f == NULL) log_error("Unable to open email file for appending."); fprintf(my_f, "%s\n", email_address); fclose(my_f); } /* print out the page to show the user */ my_f = fopen("./header.txt", "r"); if(my_f == NULL) log_error("Unable to open header file."); my_b = fgets(line, 1024, my_f); while(my_b != NULL) { printf("%s\n", line); my_b = fgets(line, 1024, my_f); } fclose(my_f); printf("

The email address %s has been added to the database. You will receive occasional mailings from OUSA. To change this, please email wsnz@ihug.co.nz

\n", email_address); my_f = fopen("./footer.txt", "r"); if(my_f == NULL) log_error("Unable to open footer file."); my_b = fgets(line, 1024, my_f); while(my_b != NULL) { printf("%s\n", line); my_b = fgets(line, 1024, my_f); } fclose(my_f); }