/* cgi-lib.c - C routines that make writing CGI scripts in C a breeze Eugene Kim, $Id: cgi-lib.c,v 1.17 1998/05/04 02:12:34 eekim Exp $ Motivation: Perl is a much more convenient language to use when writing CGI scripts. Unfortunately, it is also a larger drain on the system. Hopefully, these routines will make writing CGI scripts just as easy in C. Copyright (C) 1996,1997 Eugene Eric Kim All Rights Reserved 04-July-2000 Modified by Nikolai Golovchenko to add command line interface. For this purpose, get_DEBUG was rewritten. It requires global pointer CommandLineArguments to command line arguments argv [see void main( int argc, char *argv[], char **envp )] */ #include #include #include #include #ifdef WINDOWS #include #endif #include "cgi-lib.h" #include "html-lib.h" #include "string-lib.h" /* symbol table for CGI encoding */ #define _NAME 0 #define _VALUE 1 char** CommandLineArguments; short accept_image() { char *httpaccept = getenv("HTTP_ACCEPT"); if (strstr(httpaccept,"image") == NULL) return 0; else return 1; } /* x2c() and unescape_url() stolen from NCSA code */ char x2c(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); } void unescape_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'; } char *get_DEBUG() { int bufsize=1; int pos=0; char* buffer = (char*)malloc(sizeof(char) * bufsize); char** argv; buffer[0] = '\0'; argv = CommandLineArguments; while(*++argv) { bufsize += strlen(*argv) + 1; //add 1 char for & buffer = (char*)realloc(buffer, bufsize); pos += sprintf(buffer + pos, "%s&", *argv); } //remove final & if(pos >= 2) { buffer[bufsize - 2] = '\0'; } /* int bufsize = 1024; char *buffer = (char *)malloc(sizeof(char) * bufsize + 1); int i = 0; char ch; fprintf(stderr,"\n--- cgihtml Interactive Mode ---\n"); fprintf(stderr,"Enter CGI input string. Remember to encode appropriate "); fprintf(stderr,"characters.\nPress ENTER when done:\n\n"); while ( (i<=bufsize) && ((ch = getc(stdin)) != '\n') ) { buffer[i] = ch; i++; if (i>bufsize) { bufsize *= 2; buffer = (char *)realloc(buffer,bufsize); } } buffer[i] = '\0'; fprintf(stderr,"\n Input string: %s\nString length: %d\n",buffer,i); fprintf(stderr,"--- end cgihtml Interactive Mode ---\n\n");*/ return buffer; } char *get_POST() { unsigned int content_length; char *buffer; if (CONTENT_LENGTH != NULL) { content_length = atoi(CONTENT_LENGTH); buffer = (char *)malloc(sizeof(char) * content_length + 1); if (fread(buffer,sizeof(char),content_length,stdin) != content_length) { /* consistency error. */ fprintf(stderr,"caught by cgihtml: input length < CONTENT_LENGTH\n"); exit(1); } buffer[content_length] = '\0'; } return buffer; } char *get_GET() { char *buffer; if (QUERY_STRING == NULL) return NULL; buffer = newstr(QUERY_STRING); return buffer; } int parse_CGI_encoded(llist *entries, char *buffer) { int i, j, num, token; int len = strlen(buffer); char *lexeme = (char *)malloc(sizeof(char) * len + 1); entrytype entry; node *window; list_create(entries); window = entries->head; entry.name = NULL; entry.value = NULL; i = 0; num = 0; token = _NAME; while (i < len) { j = 0; while ( (buffer[i] != '=') && (buffer[i] != '&') && (i < len) ) { lexeme[j] = (buffer[i] == '+') ? ' ' : buffer[i]; i++; j++; } lexeme[j] = '\0'; if (token == _NAME) { entry.name = newstr(lexeme); unescape_url(entry.name); if ( (buffer[i] != '=') || (i == len - 1) ) { entry.value = (char *)malloc(sizeof(char)); strcpy(entry.value,""); window = list_insafter(entries, window, entry); free(entry.name); entry.name = NULL; free(entry.value); entry.value = NULL; if (i == len - 1) /* null value at end of expression */ num++; else { /* error in expression */ free(lexeme); return -1; } } else token = _VALUE; } else { entry.value = newstr(lexeme); unescape_url(entry.value); window = list_insafter(entries, window, entry); free(entry.name); entry.name = NULL; free(entry.value); entry.value = NULL; token = _NAME; num++; } i++; j = 0; } free(lexeme); if (entry.name != NULL) free(entry.name); if (entry.value != NULL) free(entry.value); return num; } /* stolen from k&r and seriously modified to do what I want */ int getline(char s[], int lim) { int c, i=0, num; for (i=0; (ihead; /* ignore first boundary; this isn't so robust; improve it later */ getline(buffer,BUFSIZ); /* now start parsing */ while ((bytesread=getline(buffer,BUFSIZ)) != 0) { start = 1; /* this assumes that buffer contains no binary characters. if the buffer contains the first valid header, then this is a safe assumption. however, it should be improved for robustness sake. */ buffer[bytesread] = '\0'; tempstr = newstr(buffer); tempstr += (sizeof(char) * 38); /* 38 is header up to name */ entry.name = tempstr; entry.value = (char *)malloc(sizeof(char) * BUFSIZ + 1); buffersize = BUFSIZ; strcpy(entry.value,""); while (*tempstr != '"') tempstr++; *tempstr = '\0'; if (strstr(buffer,"filename=\"") != NULL) { isfile = 1; tempstr = newstr(buffer); tempstr = strstr(tempstr,"filename=\""); tempstr += (sizeof(char) * 10); if (strlen(tempstr) >= BUFSIZ) entry.value = (char *) realloc(entry.value, sizeof(char) * strlen(tempstr)+1); entry.value = tempstr; while (*tempstr != '"') tempstr++; *tempstr = '\0'; /* Netscape's Windows browsers handle paths differently from its UNIX and Mac browsers. It delivers a full path for the uploaded file (which it shouldn't do), and it uses backslashes rather than forward slashes. No need to worry about Internet Explorer, since it doesn't support HTTP File Upload at all. */ if (strstr(lower_case(HTTP_USER_AGENT),"win") != 0) { tempstr = strrchr(entry.value, '\\'); if (tempstr) { tempstr++; entry.value = tempstr; } } window = list_insafter(entries,window,entry); numentries++; uploadfname = (char *)malloc(strlen(UPLOADDIR)+strlen(entry.value)+2); #ifdef WINDOWS sprintf(uploadfname,"%s\\%s",UPLOADDIR,entry.value); #else sprintf(uploadfname,"%s/%s",UPLOADDIR,entry.value); #endif if ( (uploadfile = fopen(uploadfname,"w")) == NULL) { /* null filename; for now, just don't save info. later, save to default file */ isfile = 0; } } else isfile = 0; /* ignore rest of headers and first blank line */ while (getline(buffer, BUFSIZ) > 1) { /* DOS style blank line? */ if ((buffer[0] == '\r') && (buffer[1] == '\n')) break; } done = 0; j = 0; while (!done) { bytesread = getline(buffer,BUFSIZ); buffer[bytesread] = '\0'; if (bytesread && strstr(buffer,boundary) == NULL) { if (start) { i = 0; while (i < bytesread) { prevbuf[i] = buffer[i]; i++; } prevbytesread = bytesread; start = 0; } else { /* flush buffer */ i = 0; while (i < prevbytesread) { if (isfile) fputc(prevbuf[i],uploadfile); else { if (j > buffersize) { buffersize += BUFSIZ; entry.value = (char *) realloc(entry.value, sizeof(char) * buffersize+1); } entry.value[j] = prevbuf[i]; j++; } i++; } /* buffer new input */ i = 0; while (i < bytesread) { prevbuf[i] = buffer[i]; i++; } prevbytesread = bytesread; } } else { done = 1; /* flush buffer except last two characters */ i = 0; while (i < prevbytesread - 2) { if (isfile) fputc(prevbuf[i],uploadfile); else { if (j > buffersize) { buffersize += BUFSIZ; entry.value = (char *) realloc(entry.value, sizeof(char) * buffersize+1); } entry.value[j] = prevbuf[i]; j++; } i++; } } } if (isfile) fclose(uploadfile); else { entry.value[j] = '\0'; window = list_insafter(entries,window,entry); numentries++; j = 0; } } return numentries; } int read_cgi_input(llist* entries) { char *input; int status; /* check for form upload. this needs to be first, because the standard way of checking for POST data is inadequate. If you are uploading a 100 MB file, you are unlikely to have a buffer in memory large enough to store the raw data for parsing. Instead, parse_form_encoded parses stdin directly. In the future, I may modify parse_CGI_encoded so that it also parses POST directly from stdin. I'm undecided on this issue, because doing so will make parse_CGI_encoded less general. */ if ((CONTENT_TYPE != NULL) && (strstr(CONTENT_TYPE,"multipart/form-data") != NULL) ) return parse_form_encoded(entries); /* get the input */ if (REQUEST_METHOD == NULL) { input = get_DEBUG(); } else { if (!strcmp(REQUEST_METHOD,"POST")) input = get_POST(); else if (!strcmp(REQUEST_METHOD,"GET")) input = get_GET(); else { /* error: invalid request method */ fprintf(stderr,"caught by cgihtml: REQUEST_METHOD invalid\n"); exit(1); } } /* parse the input */ if (input == NULL) return 0; status = parse_CGI_encoded(entries,input); free(input); return status; } int read_file_upload(llist *entries, int maxfilesize) { return parse_form_encoded(entries); } char *cgi_val(llist l, char *name) { short FOUND = 0; node* window; window = l.head; while ( (window != 0) && (!FOUND) ) if (!_stricmp(window->entry.name,name)) //case-insensitive FOUND = 1; else window = window->next; if (FOUND) return window->entry.value; else return NULL; } /* cgi_val_multi - contributed by Mitch Garnaat ; modified by me */ char **cgi_val_multi(llist l, char *name) { short FOUND = 0; node* window; char **ret_val = 0; int num_vals = 0, i; window = l.head; while (window != 0) { if (!strcmp(window->entry.name,name)) { FOUND = 1; num_vals++; } window = window->next; } if (FOUND) { /* copy the value pointers into the returned array */ ret_val = (char**) malloc(sizeof(char*) * (num_vals + 1)); window = l.head; i = 0; while (window != NULL) { if (!strcmp(window->entry.name,name)) { ret_val[i] = window->entry.value; i++; } window = window->next; } /* NULL terminate the array */ ret_val[i] = 0; return ret_val; } else return NULL; } char *cgi_name(llist l, char *value) { short FOUND = 0; node* window; window = l.head; while ( (window != 0) && (!FOUND) ) if (!strcmp(window->entry.value,value)) FOUND = 1; else window = window->next; if (FOUND) return window->entry.name; else return NULL; } char **cgi_name_multi(llist l, char *value) { short FOUND = 0; node* window; char **ret_val = 0; int num_vals = 0, i; window = l.head; while (window != 0) { if (!strcmp(window->entry.value,value)) { FOUND = 1; num_vals++; } window = window->next; } if (FOUND) { /* copy the value pointers into the returned array */ ret_val = (char**) malloc(sizeof(char*) * (num_vals + 1)); window = l.head; i = 0; while (window != NULL) { if (!strcmp(window->entry.value,value)) { ret_val[i] = window->entry.name; i++; } window = window->next; } /* NULL terminate the array */ ret_val[i] = 0; return ret_val; } else return NULL; } /* miscellaneous useful CGI routines */ int parse_cookies(llist *entries) { char *cookies = getenv("HTTP_COOKIE"); node* window; entrytype entry; int i,len; int j = 0; int numcookies = 0; short NM = 1; if (cookies == NULL) return 0; list_create(entries); window = entries->head; len = strlen(cookies); entry.name = (char *)malloc(sizeof(char) * len + 1); entry.value = (char *)malloc(sizeof(char) * len + 1); for (i = 0; i < len; i++) { if (cookies[i] == '=') { entry.name[j] = '\0'; if (i == len - 1) { strcpy(entry.value,""); window = list_insafter(entries,window,entry); numcookies++; } j = 0; NM = 0; } else if ( (cookies[i] == '&') || (i == len - 1) ) { if (!NM) { if (i == len - 1) { entry.value[j] = cookies[i]; j++; } entry.value[j] = '\0'; window = list_insafter(entries,window,entry); numcookies++; j = 0; NM = 1; } } else if ( (cookies[i] == ';') || (i == len - 1) ) { if (!NM) { if (i == len - 1) { entry.value[j] = cookies[i]; j++; } entry.value[j] = '\0'; window = list_insafter(entries,window,entry); numcookies++; i++; /* erases trailing space */ j = 0; NM = 1; } } else if (NM) { entry.name[j] = cookies[i]; j++; } else if (!NM) { entry.value[j] = cookies[i]; j++; } } return numcookies; } void print_cgi_env() { if (SERVER_SOFTWARE != NULL) printf("

SERVER_SOFTWARE = %s
\n",SERVER_SOFTWARE); if (SERVER_NAME != NULL) printf("SERVER_NAME = %s
\n",SERVER_NAME); if (GATEWAY_INTERFACE !=NULL) printf("GATEWAY_INTERFACE = %s
\n",GATEWAY_INTERFACE); if (SERVER_PROTOCOL != NULL) printf("SERVER_PROTOCOL = %s
\n",SERVER_PROTOCOL); if (SERVER_PORT != NULL) printf("SERVER_PORT = %s
\n",SERVER_PORT); if (REQUEST_METHOD != NULL) printf("REQUEST_METHOD = %s
\n",REQUEST_METHOD); if (PATH_INFO != NULL) printf("PATH_INFO = %s
\n",PATH_INFO); if (PATH_TRANSLATED != NULL) printf("PATH_TRANSLATED = %s
\n",PATH_TRANSLATED); if (SCRIPT_NAME != NULL) printf("SCRIPT_NAME = %s
\n",SCRIPT_NAME); if (QUERY_STRING != NULL) printf("QUERY_STRING = %s
\n",QUERY_STRING); if (REMOTE_HOST != NULL) printf("REMOTE_HOST = %s
\n",REMOTE_HOST); if (REMOTE_ADDR != NULL) printf("REMOTE_ADDR = %s
\n",REMOTE_ADDR); if (AUTH_TYPE != NULL) printf("AUTH_TYPE = %s
\n",AUTH_TYPE); if (REMOTE_USER != NULL) printf("REMOTE_USER = %s
\n",REMOTE_USER); if (REMOTE_IDENT != NULL) printf("REMOTE_IDENT = %s
\n",REMOTE_IDENT); if (CONTENT_TYPE != NULL) printf("CONTENT_TYPE = %s
\n",CONTENT_TYPE); if (CONTENT_LENGTH != NULL) printf("CONTENT_LENGTH = %s

\n",CONTENT_LENGTH); if (HTTP_USER_AGENT != NULL) printf("HTTP_USER_AGENT = %s

\n",HTTP_USER_AGENT); } void print_entries(llist l) { node* window; window = l.head; printf("
\n"); while (window != NULL) { printf("
%s\n",window->entry.name); printf("
%s\n",replace_ltgt(window->entry.value)); window = window->next; } printf("
\n"); } char *escape_input(char *str) /* takes string and escapes all metacharacters. should be used before including string in system() or similar call. */ { unsigned int i,j = 0; char *newstring = (char *)malloc(sizeof(char) * (strlen(str) * 2 + 1)); for (i = 0; i < strlen(str); i++) { if (!( ((str[i] >= 'A') && (str[i] <= 'Z')) || ((str[i] >= 'a') && (str[i] <= 'z')) || ((str[i] >= '0') && (str[i] <= '9')) )) { newstring[j] = '\\'; j++; } newstring[j] = str[i]; j++; } newstring[j] = '\0'; return newstring; } /* boolean functions */ short is_form_empty(llist l) { node* window; short EMPTY = 1; window = l.head; while ( (window != NULL) && (EMPTY == 1) ) { if (strcmp(window->entry.value,"")) EMPTY = 0; window = window->next; } return EMPTY; } short is_field_exists(llist l, char *str) { if (cgi_val(l,str) == NULL) return 0; else return 1; } /* is_field_empty returns true either if the field exists but is empty or if the field does not exist. */ short is_field_empty(llist l, char *str) { char *temp = cgi_val(l,str); if ( (temp == NULL) || (!strcmp(temp,"")) ) return 1; else return 0; }