/* CGI program that handles multiple forms */ /* Each form includes a hidden field that specifies the name of the form */ #include #include #include #include #include #include #include "cgilib.h" /* simple CGI request parsing routines */ /* print HTML error message and exit */ void fatal_error( char *s) { printf("

\n"); printf("%s

\n",s); exit(1); } /* Here is a routine that simply prints the contents of a file This is used so we can save most of the HTML we need to generate in files, so we can change the appearance of the pizza system without recompiling */ void sendfile( char *filename ) { FILE *f; char buf[1000]; if ( (f=fopen(filename,"r")) == NULL) { fatal_error("can't find a file"); } while (fgets(buf,1000,f)) { printf("%s",buf); } fclose(f); } #define MAXFIELDS 10 /* Max number of fields we will handle */ /* see if a string contains any nonblank chars */ int empty( char *s) { return(strlen(s)==strspn( s, " \t\n")); } /* Some file names */ char *logo = "logo"; /* the file containing the pizza logo */ char *login = "loginform.tmpl"; /* the file with the login form */ char *order = "orderform.tmpl"; /* most of the order form */ char *baduser = "baduser"; /* Nasty bad user message */ char *nicetry = "nicetry"; /* another bad user message */ char *pwfile="passwords"; /* validate a user by seeing of a (name,password) pair is found in the password database (not the system password database, just a file we use to keep track of names and passwords */ int validate_user(char *name, char *password) { FILE *pw; char u[100],p[100]; /* empty name or password is invalid */ if ((empty(name)) || (empty(password))) return(0); /* open the password database */ if ( (pw=fopen(pwfile,"r"))==NULL) fatal_error("Sorry we lost our password file"); /* check each entry in the database against the name and password we got with the query. The file contains one word per line, the first line holds a name, the second a password (and so on). */ while (!feof(pw)) { /* read a username and password from the database */ if (!fgets(u,100,pw)) { fclose(pw); return(0); } if (!fgets(p,100,pw)) { fclose(pw); return(0); } /* Eliminate trailing whitespace */ while (isspace(u[strlen(u)-1])) u[strlen(u)-1]=0; while (isspace(p[strlen(p)-1])) p[strlen(p)-1]=0; /* see if we got a match */ if ( (strcmp(u,name)==0) && (strcmp(p,password)==0) ) { fclose(pw); return(1); /* we found it! */ } } fclose(pw); return(0); /* we didn't find it */ } /* takes care of login requests. Each valid login request should include the following fields: name password The names of the fields submitted in the query are in the array names, and the field values are in vals We ignore any other fields. */ void handle_login( char **names, char **vals, int n) { char *name; char *password; int i; /* find the name and password entered */ name=password=NULL; for (i=0;i