]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
75bd9771 | 2 | * "$Id: var.c 7460 2008-04-16 02:19:54Z mike $" |
ef416fc2 | 3 | * |
4 | * CGI form variable and array functions. | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2005 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
17 | * cgiCheckVariables() - Check for the presence of "required" variables. | |
18 | * cgiGetArray() - Get an element from a form array... | |
19 | * cgiGetFile() - Get the file (if any) that was submitted in the form. | |
20 | * cgiGetSize() - Get the size of a form array value. | |
21 | * cgiGetVariable() - Get a CGI variable from the database... | |
22 | * cgiInitialize() - Initialize the CGI variable "database"... | |
23 | * cgiIsPOST() - Determine whether this page was POSTed. | |
24 | * cgiSetArray() - Set array element N to the specified string. | |
25 | * cgiSetSize() - Set the array size. | |
26 | * cgiSetVariable() - Set a CGI variable in the database... | |
27 | * cgi_add_variable() - Add a form variable. | |
28 | * cgi_compare_variables() - Compare two variables. | |
29 | * cgi_find_variable() - Find a variable... | |
30 | * cgi_initialize_get() - Initialize form variables using the GET method. | |
31 | * cgi_initialize_multipart() - Initialize variables and file using the POST method. | |
32 | * cgi_initialize_post() - Initialize variables using the POST method. | |
33 | * cgi_initialize_string() - Initialize form variables from a string. | |
34 | * cgi_passwd() - Catch authentication requests and notify the server. | |
35 | * cgi_sort_variables() - Sort all form variables for faster lookup. | |
36 | * cgi_unlink_file() - Remove the uploaded form. | |
37 | */ | |
38 | ||
39 | /*#define DEBUG*/ | |
40 | #include "cgi-private.h" | |
41 | #include <errno.h> | |
42 | ||
43 | ||
44 | /* | |
45 | * Data structure to hold all the CGI form variables and arrays... | |
46 | */ | |
47 | ||
48 | typedef struct /**** Form variable structure ****/ | |
49 | { | |
50 | const char *name; /* Name of variable */ | |
51 | int nvalues, /* Number of values */ | |
52 | avalues; /* Number of values allocated */ | |
53 | const char **values; /* Value(s) of variable */ | |
54 | } _cgi_var_t; | |
55 | ||
56 | ||
57 | /* | |
58 | * Local globals... | |
59 | */ | |
60 | ||
61 | static int form_count = 0, /* Form variable count */ | |
62 | form_alloc = 0; /* Number of variables allocated */ | |
63 | static _cgi_var_t *form_vars = NULL; | |
64 | /* Form variables */ | |
65 | static cgi_file_t *form_file = NULL; | |
66 | /* Uploaded file */ | |
67 | ||
68 | ||
69 | /* | |
70 | * Local functions... | |
71 | */ | |
72 | ||
73 | static void cgi_add_variable(const char *name, int element, | |
74 | const char *value); | |
75 | static int cgi_compare_variables(const _cgi_var_t *v1, | |
76 | const _cgi_var_t *v2); | |
77 | static _cgi_var_t *cgi_find_variable(const char *name); | |
78 | static int cgi_initialize_get(void); | |
79 | static int cgi_initialize_multipart(const char *boundary); | |
80 | static int cgi_initialize_post(void); | |
81 | static int cgi_initialize_string(const char *data); | |
82 | static const char *cgi_passwd(const char *prompt); | |
83 | static void cgi_sort_variables(void); | |
84 | static void cgi_unlink_file(void); | |
85 | ||
86 | ||
87 | /* | |
88 | * 'cgiCheckVariables()' - Check for the presence of "required" variables. | |
89 | * | |
90 | * Names may be separated by spaces and/or commas. | |
91 | */ | |
92 | ||
93 | int /* O - 1 if all variables present, 0 otherwise */ | |
94 | cgiCheckVariables(const char *names) /* I - Variables to look for */ | |
95 | { | |
96 | char name[255], /* Current variable name */ | |
97 | *s; /* Pointer in string */ | |
98 | const char *val; /* Value of variable */ | |
99 | int element; /* Array element number */ | |
100 | ||
101 | ||
102 | if (names == NULL) | |
103 | return (1); | |
104 | ||
105 | while (*names != '\0') | |
106 | { | |
107 | while (*names == ' ' || *names == ',') | |
108 | names ++; | |
109 | ||
110 | for (s = name; *names != '\0' && *names != ' ' && *names != ','; s ++, names ++) | |
111 | *s = *names; | |
112 | ||
113 | *s = 0; | |
114 | if (name[0] == '\0') | |
115 | break; | |
116 | ||
117 | if ((s = strrchr(name, '-')) != NULL) | |
118 | { | |
119 | *s = '\0'; | |
120 | element = atoi(s + 1) - 1; | |
121 | val = cgiGetArray(name, element); | |
122 | } | |
123 | else | |
124 | val = cgiGetVariable(name); | |
125 | ||
126 | if (val == NULL) | |
127 | return (0); | |
128 | ||
129 | if (*val == '\0') | |
130 | return (0); /* Can't be blank, either! */ | |
131 | } | |
132 | ||
133 | return (1); | |
134 | } | |
135 | ||
136 | ||
137 | /* | |
138 | * 'cgiGetArray()' - Get an element from a form array... | |
139 | */ | |
140 | ||
141 | const char * /* O - Element value or NULL */ | |
142 | cgiGetArray(const char *name, /* I - Name of array variable */ | |
143 | int element) /* I - Element number (0 to N) */ | |
144 | { | |
145 | _cgi_var_t *var; /* Pointer to variable */ | |
146 | ||
147 | ||
148 | if ((var = cgi_find_variable(name)) == NULL) | |
149 | return (NULL); | |
150 | ||
151 | if (var->nvalues == 1) | |
152 | return (var->values[0]); | |
153 | ||
154 | if (element < 0 || element >= var->nvalues) | |
155 | return (NULL); | |
156 | ||
157 | return (var->values[element]); | |
158 | } | |
159 | ||
160 | ||
161 | /* | |
162 | * 'cgiGetFile()' - Get the file (if any) that was submitted in the form. | |
163 | */ | |
164 | ||
165 | const cgi_file_t * /* O - Attached file or NULL */ | |
166 | cgiGetFile(void) | |
167 | { | |
168 | return (form_file); | |
169 | } | |
170 | ||
171 | ||
172 | /* | |
173 | * 'cgiGetSize()' - Get the size of a form array value. | |
174 | */ | |
175 | ||
176 | int /* O - Number of elements */ | |
177 | cgiGetSize(const char *name) /* I - Name of variable */ | |
178 | { | |
179 | _cgi_var_t *var; /* Pointer to variable */ | |
180 | ||
181 | ||
182 | if ((var = cgi_find_variable(name)) == NULL) | |
183 | return (0); | |
184 | ||
185 | return (var->nvalues); | |
186 | } | |
187 | ||
188 | ||
189 | /* | |
190 | * 'cgiGetVariable()' - Get a CGI variable from the database... | |
191 | * | |
192 | * Returns NULL if the variable doesn't exist. If the variable is an | |
193 | * array of values, returns the last element... | |
194 | */ | |
195 | ||
196 | const char * /* O - Value of variable */ | |
197 | cgiGetVariable(const char *name) /* I - Name of variable */ | |
198 | { | |
199 | const _cgi_var_t *var; /* Returned variable */ | |
200 | ||
201 | ||
202 | var = cgi_find_variable(name); | |
203 | ||
204 | #ifdef DEBUG | |
205 | if (var == NULL) | |
ae71f5de | 206 | DEBUG_printf(("cgiGetVariable(\"%s\") is returning NULL...\n", name)); |
ef416fc2 | 207 | else |
ae71f5de MS |
208 | DEBUG_printf(("cgiGetVariable(\"%s\") is returning \"%s\"...\n", name, |
209 | var->values[var->nvalues - 1])); | |
ef416fc2 | 210 | #endif /* DEBUG */ |
211 | ||
212 | return ((var == NULL) ? NULL : var->values[var->nvalues - 1]); | |
213 | } | |
214 | ||
215 | ||
216 | /* | |
217 | * 'cgiInitialize()' - Initialize the CGI variable "database"... | |
218 | */ | |
219 | ||
220 | int /* O - Non-zero if there was form data */ | |
221 | cgiInitialize(void) | |
222 | { | |
223 | const char *method; /* Form posting method */ | |
224 | const char *content_type; /* Content-Type of post data */ | |
225 | ||
226 | ||
227 | /* | |
228 | * Setup a password callback for authentication... | |
229 | */ | |
230 | ||
231 | cupsSetPasswordCB(cgi_passwd); | |
232 | ||
80ca4592 | 233 | /* |
234 | * Set the locale so that times, etc. are formatted properly... | |
235 | */ | |
236 | ||
237 | setlocale(LC_ALL, ""); | |
238 | ||
ef416fc2 | 239 | #ifdef DEBUG |
240 | /* | |
241 | * Disable output buffering to find bugs... | |
242 | */ | |
243 | ||
244 | setbuf(stdout, NULL); | |
ef416fc2 | 245 | #endif /* DEBUG */ |
246 | ||
247 | /* | |
248 | * Get the request method (GET or POST)... | |
249 | */ | |
250 | ||
251 | method = getenv("REQUEST_METHOD"); | |
252 | content_type = getenv("CONTENT_TYPE"); | |
253 | if (!method) | |
254 | return (0); | |
255 | ||
256 | /* | |
257 | * Grab form data from the corresponding location... | |
258 | */ | |
259 | ||
260 | if (!strcasecmp(method, "GET")) | |
261 | return (cgi_initialize_get()); | |
262 | else if (!strcasecmp(method, "POST") && content_type) | |
263 | { | |
264 | const char *boundary = strstr(content_type, "boundary="); | |
265 | ||
266 | if (boundary) | |
267 | boundary += 9; | |
268 | ||
269 | if (content_type && !strncmp(content_type, "multipart/form-data; ", 21)) | |
270 | return (cgi_initialize_multipart(boundary)); | |
271 | else | |
272 | return (cgi_initialize_post()); | |
273 | } | |
274 | else | |
275 | return (0); | |
276 | } | |
277 | ||
278 | ||
279 | /* | |
280 | * 'cgiIsPOST()' - Determine whether this page was POSTed. | |
281 | */ | |
282 | ||
283 | int /* O - 1 if POST, 0 if GET */ | |
284 | cgiIsPOST(void) | |
285 | { | |
286 | const char *method; /* REQUEST_METHOD environment variable */ | |
287 | ||
288 | ||
289 | if ((method = getenv("REQUEST_METHOD")) == NULL) | |
290 | return (0); | |
291 | else | |
292 | return (!strcmp(method, "POST")); | |
293 | } | |
294 | ||
295 | ||
296 | /* | |
297 | * 'cgiSetArray()' - Set array element N to the specified string. | |
298 | * | |
299 | * If the variable array is smaller than (element + 1), the intervening | |
300 | * elements are set to NULL. | |
301 | */ | |
302 | ||
303 | void | |
304 | cgiSetArray(const char *name, /* I - Name of variable */ | |
305 | int element, /* I - Element number (0 to N) */ | |
306 | const char *value) /* I - Value of variable */ | |
307 | { | |
308 | int i; /* Looping var */ | |
309 | _cgi_var_t *var; /* Returned variable */ | |
310 | ||
311 | ||
312 | if (name == NULL || value == NULL || element < 0 || element > 100000) | |
313 | return; | |
314 | ||
315 | if ((var = cgi_find_variable(name)) == NULL) | |
316 | { | |
317 | cgi_add_variable(name, element, value); | |
318 | cgi_sort_variables(); | |
319 | } | |
320 | else | |
321 | { | |
322 | if (element >= var->avalues) | |
323 | { | |
91c84a35 MS |
324 | const char **temp; /* Temporary pointer */ |
325 | ||
326 | temp = (const char **)realloc((void *)(var->values), | |
327 | sizeof(char *) * (element + 16)); | |
328 | if (!temp) | |
329 | return; | |
330 | ||
ef416fc2 | 331 | var->avalues = element + 16; |
91c84a35 | 332 | var->values = temp; |
ef416fc2 | 333 | } |
334 | ||
335 | if (element >= var->nvalues) | |
336 | { | |
337 | for (i = var->nvalues; i < element; i ++) | |
338 | var->values[i] = NULL; | |
339 | ||
340 | var->nvalues = element + 1; | |
341 | } | |
342 | else if (var->values[element]) | |
343 | free((char *)var->values[element]); | |
344 | ||
345 | var->values[element] = strdup(value); | |
346 | } | |
347 | } | |
348 | ||
349 | ||
350 | /* | |
351 | * 'cgiSetSize()' - Set the array size. | |
352 | */ | |
353 | ||
354 | void | |
355 | cgiSetSize(const char *name, /* I - Name of variable */ | |
356 | int size) /* I - Number of elements (0 to N) */ | |
357 | { | |
358 | int i; /* Looping var */ | |
359 | _cgi_var_t *var; /* Returned variable */ | |
360 | ||
361 | ||
362 | if (name == NULL || size < 0 || size > 100000) | |
363 | return; | |
364 | ||
365 | if ((var = cgi_find_variable(name)) == NULL) | |
366 | return; | |
367 | ||
368 | if (size >= var->avalues) | |
369 | { | |
91c84a35 MS |
370 | const char **temp; /* Temporary pointer */ |
371 | ||
372 | temp = (const char **)realloc((void *)(var->values), | |
373 | sizeof(char *) * (size + 16)); | |
374 | if (!temp) | |
375 | return; | |
376 | ||
ef416fc2 | 377 | var->avalues = size + 16; |
91c84a35 | 378 | var->values = temp; |
ef416fc2 | 379 | } |
380 | ||
381 | if (size > var->nvalues) | |
382 | { | |
383 | for (i = var->nvalues; i < size; i ++) | |
384 | var->values[i] = NULL; | |
385 | } | |
386 | else if (size < var->nvalues) | |
387 | { | |
388 | for (i = size; i < var->nvalues; i ++) | |
389 | if (var->values[i]) | |
390 | free((void *)(var->values[i])); | |
391 | } | |
392 | ||
393 | var->nvalues = size; | |
394 | } | |
395 | ||
396 | ||
397 | /* | |
398 | * 'cgiSetVariable()' - Set a CGI variable in the database... | |
399 | * | |
400 | * If the variable is an array, this truncates the array to a single element. | |
401 | */ | |
402 | ||
403 | void | |
404 | cgiSetVariable(const char *name, /* I - Name of variable */ | |
405 | const char *value) /* I - Value of variable */ | |
406 | { | |
407 | int i; /* Looping var */ | |
408 | _cgi_var_t *var; /* Returned variable */ | |
409 | ||
410 | ||
411 | if (name == NULL || value == NULL) | |
412 | return; | |
413 | ||
414 | if ((var = cgi_find_variable(name)) == NULL) | |
415 | { | |
416 | cgi_add_variable(name, 0, value); | |
417 | cgi_sort_variables(); | |
418 | } | |
419 | else | |
420 | { | |
421 | for (i = 0; i < var->nvalues; i ++) | |
422 | if (var->values[i]) | |
423 | free((char *)var->values[i]); | |
424 | ||
425 | var->values[0] = strdup(value); | |
426 | var->nvalues = 1; | |
427 | } | |
428 | } | |
429 | ||
430 | ||
431 | /* | |
432 | * 'cgi_add_variable()' - Add a form variable. | |
433 | */ | |
434 | ||
435 | static void | |
436 | cgi_add_variable(const char *name, /* I - Variable name */ | |
437 | int element, /* I - Array element number */ | |
438 | const char *value) /* I - Variable value */ | |
439 | { | |
91c84a35 | 440 | _cgi_var_t *var; /* New variable */ |
ef416fc2 | 441 | |
442 | ||
443 | if (name == NULL || value == NULL || element < 0 || element > 100000) | |
444 | return; | |
445 | ||
ae71f5de MS |
446 | DEBUG_printf(("cgi_add_variable: Adding variable \'%s\' with value " |
447 | "\'%s\'...\n", name, value)); | |
ef416fc2 | 448 | |
449 | if (form_count >= form_alloc) | |
450 | { | |
91c84a35 MS |
451 | _cgi_var_t *temp_vars; /* Temporary form pointer */ |
452 | ||
453 | ||
ef416fc2 | 454 | if (form_alloc == 0) |
91c84a35 | 455 | temp_vars = malloc(sizeof(_cgi_var_t) * 16); |
ef416fc2 | 456 | else |
91c84a35 MS |
457 | temp_vars = realloc(form_vars, (form_alloc + 16) * sizeof(_cgi_var_t)); |
458 | ||
459 | if (!temp_vars) | |
460 | return; | |
ef416fc2 | 461 | |
91c84a35 | 462 | form_vars = temp_vars; |
ef416fc2 | 463 | form_alloc += 16; |
464 | } | |
465 | ||
91c84a35 MS |
466 | var = form_vars + form_count; |
467 | ||
468 | if ((var->values = calloc(element + 1, sizeof(char *))) == NULL) | |
469 | return; | |
470 | ||
ef416fc2 | 471 | var->name = strdup(name); |
472 | var->nvalues = element + 1; | |
473 | var->avalues = element + 1; | |
ef416fc2 | 474 | var->values[element] = strdup(value); |
475 | ||
476 | form_count ++; | |
477 | } | |
478 | ||
479 | ||
480 | /* | |
481 | * 'cgi_compare_variables()' - Compare two variables. | |
482 | */ | |
483 | ||
484 | static int /* O - Result of comparison */ | |
485 | cgi_compare_variables( | |
486 | const _cgi_var_t *v1, /* I - First variable */ | |
487 | const _cgi_var_t *v2) /* I - Second variable */ | |
488 | { | |
489 | return (strcasecmp(v1->name, v2->name)); | |
490 | } | |
491 | ||
492 | ||
493 | /* | |
494 | * 'cgi_find_variable()' - Find a variable... | |
495 | */ | |
496 | ||
497 | static _cgi_var_t * /* O - Variable pointer or NULL */ | |
498 | cgi_find_variable(const char *name) /* I - Name of variable */ | |
499 | { | |
500 | _cgi_var_t key; /* Search key */ | |
501 | ||
502 | ||
503 | if (form_count < 1 || name == NULL) | |
504 | return (NULL); | |
505 | ||
506 | key.name = name; | |
507 | ||
508 | return ((_cgi_var_t *)bsearch(&key, form_vars, form_count, sizeof(_cgi_var_t), | |
509 | (int (*)(const void *, const void *))cgi_compare_variables)); | |
510 | } | |
511 | ||
512 | ||
513 | /* | |
514 | * 'cgi_initialize_get()' - Initialize form variables using the GET method. | |
515 | */ | |
516 | ||
517 | static int /* O - 1 if form data read */ | |
518 | cgi_initialize_get(void) | |
519 | { | |
520 | char *data; /* Pointer to form data string */ | |
521 | ||
522 | ||
ae71f5de | 523 | DEBUG_puts("cgi_initialize_get: Initializing variables using GET method..."); |
ef416fc2 | 524 | |
525 | /* | |
526 | * Check to see if there is anything for us to read... | |
527 | */ | |
528 | ||
529 | data = getenv("QUERY_STRING"); | |
530 | if (data == NULL || strlen(data) == 0) | |
531 | return (0); | |
532 | ||
533 | /* | |
534 | * Parse it out and return... | |
535 | */ | |
536 | ||
537 | return (cgi_initialize_string(data)); | |
538 | } | |
539 | ||
540 | ||
541 | /* | |
542 | * 'cgi_initialize_multipart()' - Initialize variables and file using the POST method. | |
543 | * | |
544 | * TODO: Update to support files > 2GB. | |
545 | */ | |
546 | ||
547 | static int /* O - 1 if form data was read */ | |
548 | cgi_initialize_multipart( | |
549 | const char *boundary) /* I - Boundary string */ | |
550 | { | |
551 | char line[10240], /* MIME header line */ | |
552 | name[1024], /* Form variable name */ | |
553 | filename[1024], /* Form filename */ | |
554 | mimetype[1024], /* MIME media type */ | |
555 | bstring[256], /* Boundary string to look for */ | |
556 | *ptr, /* Pointer into name/filename */ | |
557 | *end; /* End of buffer */ | |
558 | int ch, /* Character from file */ | |
559 | fd, /* Temporary file descriptor */ | |
560 | blen; /* Length of boundary string */ | |
561 | ||
562 | ||
563 | DEBUG_printf(("cgi_initialize_multipart(boundary=\"%s\")\n", boundary)); | |
564 | ||
565 | /* | |
566 | * Read multipart form data until we run out... | |
567 | */ | |
568 | ||
569 | name[0] = '\0'; | |
570 | filename[0] = '\0'; | |
571 | mimetype[0] = '\0'; | |
572 | ||
573 | snprintf(bstring, sizeof(bstring), "\r\n--%s", boundary); | |
574 | blen = strlen(bstring); | |
575 | ||
576 | while (fgets(line, sizeof(line), stdin)) | |
577 | { | |
578 | if (!strcmp(line, "\r\n")) | |
579 | { | |
580 | /* | |
581 | * End of headers, grab value... | |
582 | */ | |
583 | ||
584 | if (filename[0]) | |
585 | { | |
586 | /* | |
587 | * Read an embedded file... | |
588 | */ | |
589 | ||
590 | if (form_file) | |
591 | { | |
592 | /* | |
593 | * Remove previous file... | |
594 | */ | |
595 | ||
596 | cgi_unlink_file(); | |
597 | } | |
598 | ||
599 | /* | |
600 | * Allocate memory for the new file... | |
601 | */ | |
602 | ||
603 | if ((form_file = calloc(1, sizeof(cgi_file_t))) == NULL) | |
604 | return (0); | |
605 | ||
606 | form_file->name = strdup(name); | |
607 | form_file->filename = strdup(filename); | |
608 | form_file->mimetype = strdup(mimetype); | |
609 | ||
610 | fd = cupsTempFd(form_file->tempfile, sizeof(form_file->tempfile)); | |
611 | ||
612 | if (fd < 0) | |
613 | return (0); | |
614 | ||
615 | atexit(cgi_unlink_file); | |
616 | ||
617 | /* | |
618 | * Copy file data to the temp file... | |
619 | */ | |
620 | ||
621 | ptr = line; | |
622 | ||
623 | while ((ch = getchar()) != EOF) | |
624 | { | |
625 | *ptr++ = ch; | |
626 | ||
627 | if ((ptr - line) >= blen && !memcmp(ptr - blen, bstring, blen)) | |
628 | { | |
629 | ptr -= blen; | |
630 | break; | |
631 | } | |
632 | ||
633 | if ((ptr - line - blen) >= 8192) | |
634 | { | |
635 | /* | |
636 | * Write out the first 8k of the buffer... | |
637 | */ | |
638 | ||
639 | write(fd, line, 8192); | |
640 | memmove(line, line + 8192, ptr - line - 8192); | |
641 | ptr -= 8192; | |
642 | } | |
643 | } | |
644 | ||
645 | /* | |
646 | * Write the rest of the data and close the temp file... | |
647 | */ | |
648 | ||
649 | if (ptr > line) | |
650 | write(fd, line, ptr - line); | |
651 | ||
652 | close(fd); | |
653 | } | |
654 | else | |
655 | { | |
656 | /* | |
657 | * Just get a form variable; the current code only handles | |
658 | * form values up to 10k in size... | |
659 | */ | |
660 | ||
661 | ptr = line; | |
662 | end = line + sizeof(line) - 1; | |
663 | ||
664 | while ((ch = getchar()) != EOF) | |
665 | { | |
666 | if (ptr < end) | |
667 | *ptr++ = ch; | |
668 | ||
669 | if ((ptr - line) >= blen && !memcmp(ptr - blen, bstring, blen)) | |
670 | { | |
671 | ptr -= blen; | |
672 | break; | |
673 | } | |
674 | } | |
675 | ||
676 | *ptr = '\0'; | |
677 | ||
678 | /* | |
679 | * Set the form variable... | |
680 | */ | |
681 | ||
682 | if ((ptr = strrchr(name, '-')) != NULL && isdigit(ptr[1] & 255)) | |
683 | { | |
684 | /* | |
685 | * Set a specific index in the array... | |
686 | */ | |
687 | ||
688 | *ptr++ = '\0'; | |
689 | if (line[0]) | |
690 | cgiSetArray(name, atoi(ptr) - 1, line); | |
691 | } | |
692 | else if (cgiGetVariable(name)) | |
693 | { | |
694 | /* | |
695 | * Add another element in the array... | |
696 | */ | |
697 | ||
698 | cgiSetArray(name, cgiGetSize(name), line); | |
699 | } | |
700 | else | |
701 | { | |
702 | /* | |
703 | * Just set the line... | |
704 | */ | |
705 | ||
706 | cgiSetVariable(name, line); | |
707 | } | |
708 | } | |
709 | ||
710 | /* | |
711 | * Read the rest of the current line... | |
712 | */ | |
713 | ||
714 | fgets(line, sizeof(line), stdin); | |
715 | ||
716 | /* | |
717 | * Clear the state vars... | |
718 | */ | |
719 | ||
720 | name[0] = '\0'; | |
721 | filename[0] = '\0'; | |
722 | mimetype[0] = '\0'; | |
723 | } | |
724 | else if (!strncasecmp(line, "Content-Disposition:", 20)) | |
725 | { | |
726 | if ((ptr = strstr(line + 20, " name=\"")) != NULL) | |
727 | { | |
728 | strlcpy(name, ptr + 7, sizeof(name)); | |
729 | ||
730 | if ((ptr = strchr(name, '\"')) != NULL) | |
731 | *ptr = '\0'; | |
732 | } | |
733 | ||
734 | if ((ptr = strstr(line + 20, " filename=\"")) != NULL) | |
735 | { | |
736 | strlcpy(filename, ptr + 11, sizeof(filename)); | |
737 | ||
738 | if ((ptr = strchr(filename, '\"')) != NULL) | |
739 | *ptr = '\0'; | |
740 | } | |
741 | } | |
742 | else if (!strncasecmp(line, "Content-Type:", 13)) | |
743 | { | |
744 | for (ptr = line + 13; isspace(*ptr & 255); ptr ++); | |
745 | ||
746 | strlcpy(mimetype, ptr, sizeof(mimetype)); | |
747 | ||
748 | for (ptr = mimetype + strlen(mimetype) - 1; | |
749 | ptr > mimetype && isspace(*ptr & 255); | |
750 | *ptr-- = '\0'); | |
751 | } | |
752 | } | |
753 | ||
754 | /* | |
755 | * Return 1 for "form data found"... | |
756 | */ | |
757 | ||
758 | return (1); | |
759 | } | |
760 | ||
761 | ||
762 | /* | |
763 | * 'cgi_initialize_post()' - Initialize variables using the POST method. | |
764 | */ | |
765 | ||
766 | static int /* O - 1 if form data was read */ | |
767 | cgi_initialize_post(void) | |
768 | { | |
769 | char *content_length, /* Length of input data (string) */ | |
770 | *data; /* Pointer to form data string */ | |
771 | int length, /* Length of input data */ | |
772 | nbytes, /* Number of bytes read this read() */ | |
773 | tbytes, /* Total number of bytes read */ | |
774 | status; /* Return status */ | |
775 | ||
776 | ||
ae71f5de | 777 | DEBUG_puts("cgi_initialize_post: Initializing variables using POST method..."); |
ef416fc2 | 778 | |
779 | /* | |
780 | * Check to see if there is anything for us to read... | |
781 | */ | |
782 | ||
783 | content_length = getenv("CONTENT_LENGTH"); | |
784 | if (content_length == NULL || atoi(content_length) <= 0) | |
785 | return (0); | |
786 | ||
787 | /* | |
788 | * Get the length of the input stream and allocate a buffer for it... | |
789 | */ | |
790 | ||
791 | length = atoi(content_length); | |
792 | data = malloc(length + 1); | |
793 | ||
794 | if (data == NULL) | |
795 | return (0); | |
796 | ||
797 | /* | |
798 | * Read the data into the buffer... | |
799 | */ | |
800 | ||
801 | for (tbytes = 0; tbytes < length; tbytes += nbytes) | |
802 | if ((nbytes = read(0, data + tbytes, length - tbytes)) < 0) | |
080811b1 | 803 | { |
ef416fc2 | 804 | if (errno != EAGAIN) |
805 | { | |
806 | free(data); | |
807 | return (0); | |
808 | } | |
080811b1 MS |
809 | else |
810 | nbytes = 0; | |
811 | } | |
ef416fc2 | 812 | |
813 | data[length] = '\0'; | |
814 | ||
815 | /* | |
816 | * Parse it out... | |
817 | */ | |
818 | ||
819 | status = cgi_initialize_string(data); | |
820 | ||
821 | /* | |
822 | * Free the data and return... | |
823 | */ | |
824 | ||
825 | free(data); | |
826 | ||
827 | return (status); | |
828 | } | |
829 | ||
830 | ||
831 | /* | |
832 | * 'cgi_initialize_string()' - Initialize form variables from a string. | |
833 | */ | |
834 | ||
835 | static int /* O - 1 if form data was processed */ | |
836 | cgi_initialize_string(const char *data) /* I - Form data string */ | |
837 | { | |
838 | int done; /* True if we're done reading a form variable */ | |
839 | char *s, /* Pointer to current form string */ | |
840 | ch, /* Temporary character */ | |
841 | name[255], /* Name of form variable */ | |
842 | value[65536]; /* Variable value... */ | |
843 | ||
844 | ||
845 | /* | |
846 | * Check input... | |
847 | */ | |
848 | ||
849 | if (data == NULL) | |
850 | return (0); | |
851 | ||
852 | /* | |
853 | * Loop until we've read all the form data... | |
854 | */ | |
855 | ||
856 | while (*data != '\0') | |
857 | { | |
858 | /* | |
859 | * Get the variable name... | |
860 | */ | |
861 | ||
862 | for (s = name; *data != '\0'; data ++) | |
863 | if (*data == '=') | |
864 | break; | |
865 | else if (*data >= ' ' && s < (name + sizeof(name) - 1)) | |
866 | *s++ = *data; | |
867 | ||
868 | *s = '\0'; | |
869 | if (*data == '=') | |
870 | data ++; | |
871 | else | |
872 | return (0); | |
873 | ||
874 | /* | |
875 | * Read the variable value... | |
876 | */ | |
877 | ||
878 | for (s = value, done = 0; !done && *data != '\0'; data ++) | |
879 | switch (*data) | |
880 | { | |
881 | case '&' : /* End of data... */ | |
882 | done = 1; | |
883 | break; | |
884 | ||
885 | case '+' : /* Escaped space character */ | |
886 | if (s < (value + sizeof(value) - 1)) | |
887 | *s++ = ' '; | |
888 | break; | |
889 | ||
890 | case '%' : /* Escaped control character */ | |
891 | /* | |
892 | * Read the hex code... | |
893 | */ | |
894 | ||
895 | if (s < (value + sizeof(value) - 1)) | |
896 | { | |
897 | data ++; | |
898 | ch = *data - '0'; | |
899 | if (ch > 9) | |
900 | ch -= 7; | |
901 | *s = ch << 4; | |
902 | ||
903 | data ++; | |
904 | ch = *data - '0'; | |
905 | if (ch > 9) | |
906 | ch -= 7; | |
907 | *s++ |= ch; | |
908 | } | |
909 | else | |
910 | data += 2; | |
911 | break; | |
912 | ||
913 | default : /* Other characters come straight through */ | |
914 | if (*data >= ' ' && s < (value + sizeof(value) - 1)) | |
915 | *s++ = *data; | |
916 | break; | |
917 | } | |
918 | ||
919 | *s = '\0'; /* nul terminate the string */ | |
920 | ||
921 | /* | |
922 | * Remove trailing whitespace... | |
923 | */ | |
924 | ||
925 | if (s > value) | |
926 | s --; | |
927 | ||
928 | while (s >= value && *s == ' ') | |
929 | *s-- = '\0'; | |
930 | ||
931 | /* | |
932 | * Add the string to the variable "database"... | |
933 | */ | |
934 | ||
935 | if ((s = strrchr(name, '-')) != NULL && isdigit(s[1] & 255)) | |
936 | { | |
937 | *s++ = '\0'; | |
938 | if (value[0]) | |
939 | cgiSetArray(name, atoi(s) - 1, value); | |
940 | } | |
941 | else if (cgiGetVariable(name) != NULL) | |
942 | cgiSetArray(name, cgiGetSize(name), value); | |
943 | else | |
944 | cgiSetVariable(name, value); | |
945 | } | |
946 | ||
947 | return (1); | |
948 | } | |
949 | ||
950 | ||
951 | /* | |
952 | * 'cgi_passwd()' - Catch authentication requests and notify the server. | |
953 | * | |
954 | * This function sends a Status header and exits, forcing authentication | |
955 | * for this request. | |
956 | */ | |
957 | ||
958 | static const char * /* O - NULL (no return) */ | |
959 | cgi_passwd(const char *prompt) /* I - Prompt (not used) */ | |
960 | { | |
961 | (void)prompt; | |
962 | ||
f301802f | 963 | fprintf(stderr, "DEBUG: cgi_passwd(prompt=\"%s\") called!\n", |
964 | prompt ? prompt : "(null)"); | |
ef416fc2 | 965 | |
966 | /* | |
967 | * Send a 401 (unauthorized) status to the server, so it can notify | |
968 | * the client that authentication is required. | |
969 | */ | |
970 | ||
971 | puts("Status: 401\n"); | |
972 | exit(0); | |
973 | ||
974 | /* | |
975 | * This code is never executed, but is present to satisfy the compiler. | |
976 | */ | |
977 | ||
978 | return (NULL); | |
979 | } | |
980 | ||
981 | ||
982 | /* | |
983 | * 'cgi_sort_variables()' - Sort all form variables for faster lookup. | |
984 | */ | |
985 | ||
986 | static void | |
987 | cgi_sort_variables(void) | |
988 | { | |
989 | #ifdef DEBUG | |
990 | int i; | |
991 | ||
992 | ||
ae71f5de | 993 | DEBUG_puts("cgi_sort_variables: Sorting variables..."); |
ef416fc2 | 994 | #endif /* DEBUG */ |
995 | ||
996 | if (form_count < 2) | |
997 | return; | |
998 | ||
999 | qsort(form_vars, form_count, sizeof(_cgi_var_t), | |
1000 | (int (*)(const void *, const void *))cgi_compare_variables); | |
1001 | ||
1002 | #ifdef DEBUG | |
ae71f5de | 1003 | DEBUG_puts("cgi_sort_variables: Sorted variable list is:"); |
ef416fc2 | 1004 | for (i = 0; i < form_count; i ++) |
ae71f5de MS |
1005 | DEBUG_printf(("cgi_sort_variables: %d: %s (%d) = \"%s\" ...\n", i, |
1006 | form_vars[i].name, form_vars[i].nvalues, | |
1007 | form_vars[i].values[0])); | |
ef416fc2 | 1008 | #endif /* DEBUG */ |
1009 | } | |
1010 | ||
1011 | ||
1012 | /* | |
1013 | * 'cgi_unlink_file()' - Remove the uploaded form. | |
1014 | */ | |
1015 | ||
1016 | static void | |
1017 | cgi_unlink_file(void) | |
1018 | { | |
1019 | if (form_file) | |
1020 | { | |
1021 | /* | |
1022 | * Remove the temporary file... | |
1023 | */ | |
1024 | ||
1025 | unlink(form_file->tempfile); | |
1026 | ||
1027 | /* | |
1028 | * Free memory used... | |
1029 | */ | |
1030 | ||
1031 | free(form_file->name); | |
1032 | free(form_file->filename); | |
1033 | free(form_file->mimetype); | |
1034 | free(form_file); | |
1035 | ||
1036 | form_file = NULL; | |
1037 | } | |
1038 | } | |
1039 | ||
1040 | ||
1041 | /* | |
75bd9771 | 1042 | * End of "$Id: var.c 7460 2008-04-16 02:19:54Z mike $". |
ef416fc2 | 1043 | */ |