Only seed database for Debian install if we're using default config
[onak.git] / getcgi.c
1 /*
2  * getcgivars.c - routine to read CGI input variables into an array.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * The x2c() and unescape_url() routines were lifted directly
7  * from NCSA's sample program util.c, packaged with their HTTPD.
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <stdlib.h>
14
15 #include "getcgi.h"
16
17 /**
18  *      txt2html - Takes a string and converts it to HTML.
19  *      @string: The string to HTMLize.
20  *
21  *      Takes a string and escapes any HTML entities.
22  */
23 char *txt2html(const char *string)
24 {
25         static char buf[1024];
26         char *ptr = NULL;
27         char *nextptr = NULL;
28
29         memset(buf, 0, 1024);
30
31         ptr = strchr(string, '<');
32         if (ptr != NULL) {
33                 nextptr = ptr + 1;
34                 *ptr = 0;
35                 strncpy(buf, string, 1023);
36                 strncat(buf, "&lt;", 1023 - strlen(buf));
37                 string = nextptr;
38         }
39
40         ptr = strchr(string, '>');
41         if (ptr != NULL) {
42                 nextptr = ptr + 1;
43                 *ptr = 0;
44                 strncat(buf, string, 1023 - strlen(buf));
45                 strncat(buf, "&gt;", 1023 - strlen(buf));
46                 string = nextptr;
47         }
48         
49         /*
50          * TODO: We need to while() this really as each entity may appear more
51          * than once. We need to start with & and ; as we replace with those
52          * throughout. Fuck it for the moment though; it's Easter and < & > are
53          * the most common and tend to only appear once.
54          */
55
56         strncat(buf, string, 1023 - strlen(buf));
57
58         return buf;
59 }
60
61 /*
62  *      start_html - Start HTML output.
63  *      @title: The title for the HTML.
64  *
65  *      Takes a title string and starts HTML output, including the
66  *      Content-Type header all the way up to <BODY>.
67  */
68 void start_html(const char *title)
69 {
70         puts("Content-Type: text/html; charset=UTF-8\n");
71         puts("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>");
72         puts("<HTML>");
73         puts("<HEAD>");
74         printf("<TITLE>%s</TITLE>\n", title);
75         puts("</HEAD>");
76         puts("<BODY>");
77
78         return;
79 }
80
81 /*
82  *      end_html - End HTML output.
83  *
84  *      Ends HTML output - closes the BODY and HTML tags.
85  */
86 void end_html(void)
87 {
88         puts("</BODY>");
89         puts("</HTML>");
90
91         return;
92 }
93
94
95 /* Convert a two-char hex string into the char it represents */
96 char x2c(const char *what) 
97 {
98         register char digit;
99
100         digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
101                                         (what[0] - '0'));
102         digit *= 16;
103         digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
104                                         (what[1] - '0'));
105         
106         return(digit);
107 }
108
109 /* Reduce any %xx escape sequences to the characters they represent */
110 void unescape_url(char *url) 
111 {
112         register int i,j;
113
114         for(i=0,j=0; url[j]; ++i,++j) {
115                 if((url[i] = url[j]) == '%') {
116                         url[i]=x2c(&url[j+1]);
117                         j+=2;
118                 }
119         }
120         
121         url[i] = '\0';
122 }
123
124
125 /* Read the CGI input and place all name/val pairs into list.        */
126 /* Returns list containing name1, value1, name2, value2, ... , NULL  */
127 char **getcgivars(int argc, char *argv[]) 
128 {
129         int i;
130         char *request_method;
131         int content_length, paircount;
132         char *cgiinput = NULL;
133         char **cgivars = NULL;
134         char **pairlist = NULL;
135         char *nvpair,*eqpos;
136
137         /* Depending on the request method, read all CGI input into cgiinput */
138         /* (really should produce HTML error messages, instead of exit()ing) */
139
140         request_method = getenv("REQUEST_METHOD");
141         
142         if (request_method == NULL) {
143                 if (argc > 1) {
144                         cgiinput = strdup(argv[1]);
145                 } else {
146                         return NULL;
147                 }
148         } else if (strlen(request_method)==0) {
149                 return NULL;
150         } else if (!strcmp(request_method, "GET") ||
151                         !strcmp(request_method, "HEAD")) {
152                 cgiinput=strdup(getenv("QUERY_STRING"));
153         } else if (!strcmp(request_method, "POST")) {
154                 if (getenv("CONTENT_TYPE") != NULL &&
155                                 strcasecmp(getenv("CONTENT_TYPE"),
156                                         "application/x-www-form-urlencoded")) {
157                         printf("getcgivars(): Unsupported Content-Type.\n");
158                         exit(1);
159                 }
160                 
161                 if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
162                         printf("getcgivars(): No Content-Length was sent with"
163                                         " the POST request.\n");
164                         exit(1);
165                 }
166                 
167                 if (!(cgiinput= (char *) malloc(content_length+1))) {
168                         printf("getcgivars(): Could not malloc for "
169                                         "cgiinput.\n");
170                         exit(1);
171                 }
172                 
173                 if (!fread(cgiinput, content_length, 1, stdin)) {
174                         printf("Couldn't read CGI input from STDIN.\n");
175                         exit(1);
176                 }
177                 
178                 cgiinput[content_length]='\0';
179                 
180         } else {
181                 printf("getcgivars(): unsupported REQUEST_METHOD\n");
182                 exit(1);
183         }
184
185         /* Change all plusses back to spaces */
186
187         for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
188
189         /* First, split on "&" to extract the name-value pairs into pairlist */
190         pairlist=(char **) malloc(256*sizeof(char **));
191         paircount=0;
192         nvpair=strtok(cgiinput, "&");
193         while (nvpair) {
194                 pairlist[paircount++]= strdup(nvpair) ;
195                 if (!(paircount%256)) {
196                         pairlist=(char **) realloc(pairlist,
197                                         (paircount+256)*sizeof(char **));
198                 }
199                 nvpair=strtok(NULL, "&") ;
200         }
201
202         pairlist[paircount]=0;          /* terminate the list with NULL */
203
204         /* Then, from the list of pairs, extract the names and values */
205         
206         cgivars=(char **) malloc((paircount*2+1)*sizeof(char **));
207         
208         for (i=0; i<paircount; i++) {
209                 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
210                         *eqpos='\0';
211                         unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
212                 } else {
213                         unescape_url(cgivars[i*2+1]=strdup(""));
214                 }
215                 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
216         }
217
218         cgivars[paircount*2]=NULL;      /* terminate the list with NULL */
219     
220         /* Free anything that needs to be freed */
221         free(cgiinput);
222         for (i=0; pairlist[i]; i++) free(pairlist[i]);
223         free(pairlist);
224
225         /* Return the list of name-value strings */
226         return cgivars;
227 }
228
229
230 /**
231  *      cleanupcgi - free the memory allocated for our CGI parameters.
232  *      @cgivars: The CGI parameter list to free.
233  *
234  *      Frees up the elements of the CGI parameter array and then frees the
235  *      array.
236  */
237 void cleanupcgi(char **cgivars)
238 {
239         int i;
240
241         if (cgivars != NULL) {
242                 for (i = 0; cgivars[i] != NULL; i++) {
243                         free(cgivars[i]);
244                         cgivars[i] = NULL;
245                 }
246                 free(cgivars);
247                 cgivars = NULL;
248         }
249
250         return;
251 }