/****************************************** * * GETCGI.C - Nagios CGI Input Routines * * Last Modified: 05-15-2006 * *****************************************/ #include "../include/config.h" #include "../include/getcgi.h" #include #include #undef PARANOID_CGI_INPUT /* Remove potentially harmful characters from CGI input that we don't need or want */ void sanitize_cgi_input(char **cgivars){ char *strptr; int x,y,i; int keep; /* don't strip for now... */ return; for(strptr=cgivars[i=0];strptr!=NULL;strptr=cgivars[++i]){ for(x=0,y=0;strptr[x]!='\x0';x++){ keep=1; /* remove potentially nasty characters */ if(strptr[x]==';' || strptr[x]=='|' || strptr[x]=='&' || strptr[x]=='<' || strptr[x]=='>') keep=0; #ifdef PARANOID_CGI_INPUT else if(strptr[x]=='/' || strptr[x]=='\\') keep=0; #endif if(keep==1) strptr[y++]=strptr[x]; } strptr[y]='\x0'; } return; } /* convert encoded hex string (2 characters representing an 8-bit number) to its ASCII char equivalent */ unsigned char hex_to_char(char *input){ unsigned char outchar='\x0'; unsigned int outint; char tempbuf[3]; /* NULL or empty string */ if(input==NULL) return '\x0'; if(input[0]=='\x0') return '\x0'; tempbuf[0]=input[0]; tempbuf[1]=input[1]; tempbuf[2]='\x0'; sscanf(tempbuf,"%X",&outint); /* only convert "normal" ASCII characters - we don't want the rest. Normally you would convert all characters (i.e. for allowing users to post binary files), but since we aren't doing this, stay on the cautious side of things and reject outsiders... */ #ifdef PARANOID_CGI_INPUT if(outint<32 || outint>126) outint=0; #endif outchar=(unsigned char)outint; return outchar; } /* unescape hex characters in CGI input */ void unescape_cgi_input(char *input){ int x,y; int len; if(input==NULL) return; len=strlen(input); for(x=0,y=0;x=INT_MAX-1)){ printf("getcgivars(): Suspicious Content-Length was sent with the POST request.\n"); exit(1); } if(!(cgiinput=(char *)malloc(content_length+1))){ printf("getcgivars(): Could not allocate memory for CGI input.\n"); exit(1); } if(!fread(cgiinput,content_length,1,stdin)){ printf("getcgivars(): Could not read input from STDIN.\n"); exit(1); } cgiinput[content_length]='\0'; } else{ printf("getcgivars(): Unsupported REQUEST_METHOD -> '%s'\n",request_method); printf("\n"); printf("I'm guessing you're trying to execute the CGI from a command line.\n"); printf("In order to do that, you need to set the REQUEST_METHOD environment\n"); printf("variable to either \"GET\", \"HEAD\", or \"POST\". When using the\n"); printf("GET and HEAD methods, arguments can be passed to the CGI\n"); printf("by setting the \"QUERY_STRING\" environment variable. If you're\n"); printf("using the POST method, data is read from standard input. Also of\n"); printf("note: if you've enabled authentication in the CGIs, you must set the\n"); printf("\"REMOTE_USER\" environment variable to be the name of the user you're\n"); printf("\"authenticated\" as.\n"); printf("\n"); exit(1); } /* change all plus signs back to spaces */ for(i=0;cgiinput[i];i++){ if(cgiinput[i]=='+') cgiinput[i]=' '; } /* first, split on ampersands (&) to extract the name-value pairs into pairlist */ /* allocate memory for 256 name-value pairs at a time, increasing by same amount as necessary... */ pairlist=(char **)malloc(256*sizeof(char **)); if(pairlist==NULL){ printf("getcgivars(): Could not allocate memory for name-value pairlist.\n"); exit(1); } paircount=0; nvpair=strtok(cgiinput,"&"); while(nvpair){ pairlist[paircount++]=strdup(nvpair); if(!(paircount%256)){ pairlist=(char **)realloc(pairlist,(paircount+256)*sizeof(char **)); if(pairlist==NULL){ printf("getcgivars(): Could not re-allocate memory for name-value pairlist.\n"); exit(1); } } nvpair=strtok(NULL,"&"); } /* terminate the list */ pairlist[paircount]='\x0'; /* extract the names and values from the pairlist */ cgivars=(char **)malloc((paircount*2+1)*sizeof(char **)); if(cgivars==NULL){ printf("getcgivars(): Could not allocate memory for name-value list.\n"); exit(1); } for(i=0;i