]> granicus.if.org Git - apache/blob - server/util_script.c
Enable SetEnv to set PATH
[apache] / server / util_script.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr.h"
18 #include "apr_lib.h"
19 #include "apr_strings.h"
20
21 #define APR_WANT_STRFUNC
22 #include "apr_want.h"
23
24 #if APR_HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27
28 #include "ap_config.h"
29 #include "httpd.h"
30 #include "http_config.h"
31 #include "http_main.h"
32 #include "http_log.h"
33 #include "http_core.h"
34 #include "http_protocol.h"
35 #include "http_request.h"       /* for sub_req_lookup_uri() */
36 #include "util_script.h"
37 #include "apr_date.h"           /* For apr_date_parse_http() */
38 #include "util_ebcdic.h"
39
40 #ifdef OS2
41 #define INCL_DOS
42 #include <os2.h>
43 #endif
44
45 /*
46  * Various utility functions which are common to a whole lot of
47  * script-type extensions mechanisms, and might as well be gathered
48  * in one place (if only to avoid creating inter-module dependancies
49  * where there don't have to be).
50  */
51
52 #define MALFORMED_MESSAGE "malformed header from script. Bad header="
53 #define MALFORMED_HEADER_LENGTH_TO_SHOW 30
54
55 APLOG_USE_MODULE(core);
56
57 static char *http2env(apr_pool_t *a, const char *w)
58 {
59     char *res = (char *)apr_palloc(a, sizeof("HTTP_") + strlen(w));
60     char *cp = res;
61     char c;
62
63     *cp++ = 'H';
64     *cp++ = 'T';
65     *cp++ = 'T';
66     *cp++ = 'P';
67     *cp++ = '_';
68
69     while ((c = *w++) != 0) {
70         if (!apr_isalnum(c)) {
71             *cp++ = '_';
72         }
73         else {
74             *cp++ = apr_toupper(c);
75         }
76     }
77     *cp = 0;
78
79     return res;
80 }
81
82 AP_DECLARE(char **) ap_create_environment(apr_pool_t *p, apr_table_t *t)
83 {
84     const apr_array_header_t *env_arr = apr_table_elts(t);
85     const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
86     char **env = (char **) apr_palloc(p, (env_arr->nelts + 2) * sizeof(char *));
87     int i, j;
88     char *tz;
89     char *whack;
90
91     j = 0;
92     if (!apr_table_get(t, "TZ")) {
93         tz = getenv("TZ");
94         if (tz != NULL) {
95             env[j++] = apr_pstrcat(p, "TZ=", tz, NULL);
96         }
97     }
98     for (i = 0; i < env_arr->nelts; ++i) {
99         if (!elts[i].key) {
100             continue;
101         }
102         env[j] = apr_pstrcat(p, elts[i].key, "=", elts[i].val, NULL);
103         whack = env[j];
104         if (apr_isdigit(*whack)) {
105             *whack++ = '_';
106         }
107         while (*whack != '=') {
108             if (!apr_isalnum(*whack) && *whack != '_') {
109                 *whack = '_';
110             }
111             ++whack;
112         }
113         ++j;
114     }
115
116     env[j] = NULL;
117     return env;
118 }
119
120 AP_DECLARE(void) ap_add_common_vars(request_rec *r)
121 {
122     apr_table_t *e;
123     server_rec *s = r->server;
124     conn_rec *c = r->connection;
125     const char *rem_logname;
126     const char *env_path;
127 #if defined(WIN32) || defined(OS2)
128     const char *env_temp;
129 #endif
130     const char *host;
131     const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
132     const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
133     int i;
134     apr_port_t rport;
135
136     /* use a temporary apr_table_t which we'll overlap onto
137      * r->subprocess_env later
138      * (exception: if r->subprocess_env is empty at the start,
139      * write directly into it)
140      */
141     if (apr_is_empty_table(r->subprocess_env)) {
142         e = r->subprocess_env;
143     }
144     else {
145         e = apr_table_make(r->pool, 25 + hdrs_arr->nelts);
146     }
147
148     /* First, add environment vars from headers... this is as per
149      * CGI specs, though other sorts of scripting interfaces see
150      * the same vars...
151      */
152
153     for (i = 0; i < hdrs_arr->nelts; ++i) {
154         if (!hdrs[i].key) {
155             continue;
156         }
157
158         /* A few headers are special cased --- Authorization to prevent
159          * rogue scripts from capturing passwords; content-type and -length
160          * for no particular reason.
161          */
162
163         if (!strcasecmp(hdrs[i].key, "Content-type")) {
164             apr_table_addn(e, "CONTENT_TYPE", hdrs[i].val);
165         }
166         else if (!strcasecmp(hdrs[i].key, "Content-length")) {
167             apr_table_addn(e, "CONTENT_LENGTH", hdrs[i].val);
168         }
169         /*
170          * You really don't want to disable this check, since it leaves you
171          * wide open to CGIs stealing passwords and people viewing them
172          * in the environment with "ps -e".  But, if you must...
173          */
174 #ifndef SECURITY_HOLE_PASS_AUTHORIZATION
175         else if (!strcasecmp(hdrs[i].key, "Authorization")
176                  || !strcasecmp(hdrs[i].key, "Proxy-Authorization")) {
177             continue;
178         }
179 #endif
180         else {
181             apr_table_addn(e, http2env(r->pool, hdrs[i].key), hdrs[i].val);
182         }
183     }
184
185     env_path = apr_table_get(r->subprocess_env, "PATH");
186     if (env_path == NULL) {
187         env_path = getenv("PATH");
188     }
189     if (env_path == NULL) {
190         env_path = DEFAULT_PATH;
191     }
192     apr_table_addn(e, "PATH", apr_pstrdup(r->pool, env_path));
193
194 #ifdef WIN32
195     if (env_temp = getenv("SystemRoot")) {
196         apr_table_addn(e, "SystemRoot", env_temp);
197     }
198     if (env_temp = getenv("COMSPEC")) {
199         apr_table_addn(e, "COMSPEC", env_temp);
200     }
201     if (env_temp = getenv("PATHEXT")) {
202         apr_table_addn(e, "PATHEXT", env_temp);
203     }
204     if (env_temp = getenv("WINDIR")) {
205         apr_table_addn(e, "WINDIR", env_temp);
206     }
207 #endif
208
209 #ifdef OS2
210     if ((env_temp = getenv("COMSPEC")) != NULL) {
211         apr_table_addn(e, "COMSPEC", env_temp);
212     }
213     if ((env_temp = getenv("ETC")) != NULL) {
214         apr_table_addn(e, "ETC", env_temp);
215     }
216     if ((env_temp = getenv("DPATH")) != NULL) {
217         apr_table_addn(e, "DPATH", env_temp);
218     }
219     if ((env_temp = getenv("PERLLIB_PREFIX")) != NULL) {
220         apr_table_addn(e, "PERLLIB_PREFIX", env_temp);
221     }
222 #endif
223
224     apr_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
225     apr_table_addn(e, "SERVER_SOFTWARE", ap_get_server_banner());
226     apr_table_addn(e, "SERVER_NAME",
227                    ap_escape_html(r->pool, ap_get_server_name(r)));
228     apr_table_addn(e, "SERVER_ADDR", r->connection->local_ip);  /* Apache */
229     apr_table_addn(e, "SERVER_PORT",
230                   apr_psprintf(r->pool, "%u", ap_get_server_port(r)));
231     host = ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST, NULL);
232     if (host) {
233         apr_table_addn(e, "REMOTE_HOST", host);
234     }
235     apr_table_addn(e, "REMOTE_ADDR", c->remote_ip);
236     apr_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r));    /* Apache */
237     apr_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
238     apr_table_addn(e, "SCRIPT_FILENAME", r->filename);  /* Apache */
239
240     rport = c->remote_addr->port;
241     apr_table_addn(e, "REMOTE_PORT", apr_itoa(r->pool, rport));
242
243     if (r->user) {
244         apr_table_addn(e, "REMOTE_USER", r->user);
245     }
246     else if (r->prev) {
247         request_rec *back = r->prev;
248
249         while (back) {
250             if (back->user) {
251                 apr_table_addn(e, "REDIRECT_REMOTE_USER", back->user);
252                 break;
253             }
254             back = back->prev;
255         }
256     }
257     if (r->ap_auth_type) {
258         apr_table_addn(e, "AUTH_TYPE", r->ap_auth_type);
259     }
260     rem_logname = ap_get_remote_logname(r);
261     if (rem_logname) {
262         apr_table_addn(e, "REMOTE_IDENT", apr_pstrdup(r->pool, rem_logname));
263     }
264
265     /* Apache custom error responses. If we have redirected set two new vars */
266
267     if (r->prev) {
268         if (r->prev->args) {
269             apr_table_addn(e, "REDIRECT_QUERY_STRING", r->prev->args);
270         }
271         if (r->prev->uri) {
272             apr_table_addn(e, "REDIRECT_URL", r->prev->uri);
273         }
274     }
275
276     if (e != r->subprocess_env) {
277       apr_table_overlap(r->subprocess_env, e, APR_OVERLAP_TABLES_SET);
278     }
279 }
280
281 /* This "cute" little function comes about because the path info on
282  * filenames and URLs aren't always the same. So we take the two,
283  * and find as much of the two that match as possible.
284  */
285
286 AP_DECLARE(int) ap_find_path_info(const char *uri, const char *path_info)
287 {
288     int lu = strlen(uri);
289     int lp = strlen(path_info);
290
291     while (lu-- && lp-- && uri[lu] == path_info[lp]) {
292         if (path_info[lp] == '/') {
293             while (lu && uri[lu-1] == '/') lu--;
294         }
295     }
296
297     if (lu == -1) {
298         lu = 0;
299     }
300
301     while (uri[lu] != '\0' && uri[lu] != '/') {
302         lu++;
303     }
304     return lu;
305 }
306
307 /* Obtain the Request-URI from the original request-line, returning
308  * a new string from the request pool containing the URI or "".
309  */
310 static char *original_uri(request_rec *r)
311 {
312     char *first, *last;
313
314     if (r->the_request == NULL) {
315         return (char *) apr_pcalloc(r->pool, 1);
316     }
317
318     first = r->the_request;     /* use the request-line */
319
320     while (*first && !apr_isspace(*first)) {
321         ++first;                /* skip over the method */
322     }
323     while (apr_isspace(*first)) {
324         ++first;                /*   and the space(s)   */
325     }
326
327     last = first;
328     while (*last && !apr_isspace(*last)) {
329         ++last;                 /* end at next whitespace */
330     }
331
332     return apr_pstrmemdup(r->pool, first, last - first);
333 }
334
335 AP_DECLARE(void) ap_add_cgi_vars(request_rec *r)
336 {
337     apr_table_t *e = r->subprocess_env;
338
339     apr_table_setn(e, "GATEWAY_INTERFACE", "CGI/1.1");
340     apr_table_setn(e, "SERVER_PROTOCOL", r->protocol);
341     apr_table_setn(e, "REQUEST_METHOD", r->method);
342     apr_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
343     apr_table_setn(e, "REQUEST_URI", original_uri(r));
344
345     /* Note that the code below special-cases scripts run from includes,
346      * because it "knows" that the sub_request has been hacked to have the
347      * args and path_info of the original request, and not any that may have
348      * come with the script URI in the include command.  Ugh.
349      */
350
351     if (!strcmp(r->protocol, "INCLUDED")) {
352         apr_table_setn(e, "SCRIPT_NAME", r->uri);
353         if (r->path_info && *r->path_info) {
354             apr_table_setn(e, "PATH_INFO", r->path_info);
355         }
356     }
357     else if (!r->path_info || !*r->path_info) {
358         apr_table_setn(e, "SCRIPT_NAME", r->uri);
359     }
360     else {
361         int path_info_start = ap_find_path_info(r->uri, r->path_info);
362
363         apr_table_setn(e, "SCRIPT_NAME",
364                       apr_pstrndup(r->pool, r->uri, path_info_start));
365
366         apr_table_setn(e, "PATH_INFO", r->path_info);
367     }
368
369     if (r->path_info && r->path_info[0]) {
370         /*
371          * To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
372          * Need to re-escape it for this, since the entire URI was
373          * un-escaped before we determined where the PATH_INFO began.
374          */
375         request_rec *pa_req;
376
377         pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r,
378                                        NULL);
379
380         if (pa_req->filename) {
381             char *pt = apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
382                                   NULL);
383 #ifdef WIN32
384             /* We need to make this a real Windows path name */
385             apr_filepath_merge(&pt, "", pt, APR_FILEPATH_NATIVE, r->pool);
386 #endif
387             apr_table_setn(e, "PATH_TRANSLATED", pt);
388         }
389         ap_destroy_sub_req(pa_req);
390     }
391 }
392
393
394 static int set_cookie_doo_doo(void *v, const char *key, const char *val)
395 {
396     apr_table_addn(v, key, val);
397     return 1;
398 }
399
400 #define HTTP_UNSET (-HTTP_OK)
401
402 AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
403                                        int (*getsfunc) (char *, int, void *),
404                                        void *getsfunc_data)
405 {
406     char x[MAX_STRING_LEN];
407     char *w, *l;
408     int p;
409     int cgi_status = HTTP_UNSET;
410     apr_table_t *merge;
411     apr_table_t *cookie_table;
412
413     if (buffer) {
414         *buffer = '\0';
415     }
416     w = buffer ? buffer : x;
417
418     /* temporary place to hold headers to merge in later */
419     merge = apr_table_make(r->pool, 10);
420
421     /* The HTTP specification says that it is legal to merge duplicate
422      * headers into one.  Some browsers that support Cookies don't like
423      * merged headers and prefer that each Set-Cookie header is sent
424      * separately.  Lets humour those browsers by not merging.
425      * Oh what a pain it is.
426      */
427     cookie_table = apr_table_make(r->pool, 2);
428     apr_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out, "Set-Cookie", NULL);
429
430     while (1) {
431
432         int rv = (*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data);
433         if (rv == 0) {
434             ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
435                           "Premature end of script headers: %s",
436                           apr_filepath_name_get(r->filename));
437             return HTTP_INTERNAL_SERVER_ERROR;
438         }
439         else if (rv == -1) {
440             ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
441                           "Script timed out before returning headers: %s",
442                           apr_filepath_name_get(r->filename));
443             return HTTP_GATEWAY_TIME_OUT;
444         }
445
446         /* Delete terminal (CR?)LF */
447
448         p = strlen(w);
449              /* Indeed, the host's '\n':
450                 '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
451                  -- whatever the script generates.
452              */
453         if (p > 0 && w[p - 1] == '\n') {
454             if (p > 1 && w[p - 2] == CR) {
455                 w[p - 2] = '\0';
456             }
457             else {
458                 w[p - 1] = '\0';
459             }
460         }
461
462         /*
463          * If we've finished reading the headers, check to make sure any
464          * HTTP/1.1 conditions are met.  If so, we're done; normal processing
465          * will handle the script's output.  If not, just return the error.
466          * The appropriate thing to do would be to send the script process a
467          * SIGPIPE to let it know we're ignoring it, close the channel to the
468          * script process, and *then* return the failed-to-meet-condition
469          * error.  Otherwise we'd be waiting for the script to finish
470          * blithering before telling the client the output was no good.
471          * However, we don't have the information to do that, so we have to
472          * leave it to an upper layer.
473          */
474         if (w[0] == '\0') {
475             int cond_status = OK;
476
477             /* PR#38070: This fails because it gets confused when a
478              * CGI Status header overrides ap_meets_conditions.
479              * 
480              * We can fix that by dropping ap_meets_conditions when
481              * Status has been set.  Since this is the only place
482              * cgi_status gets used, let's test it explicitly.
483              *
484              * The alternative would be to ignore CGI Status when
485              * ap_meets_conditions returns anything interesting.
486              * That would be safer wrt HTTP, but would break CGI.
487              */
488             if ((cgi_status == HTTP_UNSET) && (r->method_number == M_GET)) {
489                 cond_status = ap_meets_conditions(r);
490             }
491             apr_table_overlap(r->err_headers_out, merge,
492                 APR_OVERLAP_TABLES_MERGE);
493             if (!apr_is_empty_table(cookie_table)) {
494                 /* the cookies have already been copied to the cookie_table */
495                 apr_table_unset(r->err_headers_out, "Set-Cookie");
496                 r->err_headers_out = apr_table_overlay(r->pool,
497                     r->err_headers_out, cookie_table);
498             }
499             return cond_status;
500         }
501
502         /* if we see a bogus header don't ignore it. Shout and scream */
503
504 #if APR_CHARSET_EBCDIC
505             /* Chances are that we received an ASCII header text instead of
506              * the expected EBCDIC header lines. Try to auto-detect:
507              */
508         if (!(l = strchr(w, ':'))) {
509             int maybeASCII = 0, maybeEBCDIC = 0;
510             unsigned char *cp, native;
511             apr_size_t inbytes_left, outbytes_left;
512
513             for (cp = w; *cp != '\0'; ++cp) {
514                 native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
515                 if (apr_isprint(*cp) && !apr_isprint(native))
516                     ++maybeEBCDIC;
517                 if (!apr_isprint(*cp) && apr_isprint(native))
518                     ++maybeASCII;
519             }
520             if (maybeASCII > maybeEBCDIC) {
521                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
522                              "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
523                              r->filename);
524                 inbytes_left = outbytes_left = cp - w;
525                 apr_xlate_conv_buffer(ap_hdrs_from_ascii,
526                                       w, &inbytes_left, w, &outbytes_left);
527             }
528         }
529 #endif /*APR_CHARSET_EBCDIC*/
530         if (!(l = strchr(w, ':'))) {
531             char malformed[(sizeof MALFORMED_MESSAGE) + 1
532                            + MALFORMED_HEADER_LENGTH_TO_SHOW];
533
534             strcpy(malformed, MALFORMED_MESSAGE);
535             strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
536
537             if (!buffer) {
538                 /* Soak up all the script output - may save an outright kill */
539                 while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) {
540                     continue;
541                 }
542             }
543
544             ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
545                           "%s: %s", malformed,
546                           apr_filepath_name_get(r->filename));
547             return HTTP_INTERNAL_SERVER_ERROR;
548         }
549
550         *l++ = '\0';
551         while (*l && apr_isspace(*l)) {
552             ++l;
553         }
554
555         if (!strcasecmp(w, "Content-type")) {
556             char *tmp;
557
558             /* Nuke trailing whitespace */
559
560             char *endp = l + strlen(l) - 1;
561             while (endp > l && apr_isspace(*endp)) {
562                 *endp-- = '\0';
563             }
564
565             tmp = apr_pstrdup(r->pool, l);
566             ap_content_type_tolower(tmp);
567             ap_set_content_type(r, tmp);
568         }
569         /*
570          * If the script returned a specific status, that's what
571          * we'll use - otherwise we assume 200 OK.
572          */
573         else if (!strcasecmp(w, "Status")) {
574             r->status = cgi_status = atoi(l);
575             r->status_line = apr_pstrdup(r->pool, l);
576         }
577         else if (!strcasecmp(w, "Location")) {
578             apr_table_set(r->headers_out, w, l);
579         }
580         else if (!strcasecmp(w, "Content-Length")) {
581             apr_table_set(r->headers_out, w, l);
582         }
583         else if (!strcasecmp(w, "Content-Range")) {
584             apr_table_set(r->headers_out, w, l);
585         }
586         else if (!strcasecmp(w, "Transfer-Encoding")) {
587             apr_table_set(r->headers_out, w, l);
588         }
589         else if (!strcasecmp(w, "ETag")) {
590             apr_table_set(r->headers_out, w, l);
591         }
592         /*
593          * If the script gave us a Last-Modified header, we can't just
594          * pass it on blindly because of restrictions on future values.
595          */
596         else if (!strcasecmp(w, "Last-Modified")) {
597             ap_update_mtime(r, apr_date_parse_http(l));
598             ap_set_last_modified(r);
599         }
600         else if (!strcasecmp(w, "Set-Cookie")) {
601             apr_table_add(cookie_table, w, l);
602         }
603         else {
604             apr_table_add(merge, w, l);
605         }
606     }
607     /* never reached - we leave this function within the while loop above */
608     return OK;
609 }
610
611 static int getsfunc_FILE(char *buf, int len, void *f)
612 {
613     return apr_file_gets(buf, len, (apr_file_t *) f) == APR_SUCCESS;
614 }
615
616 AP_DECLARE(int) ap_scan_script_header_err(request_rec *r, apr_file_t *f,
617                                           char *buffer)
618 {
619     return ap_scan_script_header_err_core(r, buffer, getsfunc_FILE, f);
620 }
621
622 static int getsfunc_BRIGADE(char *buf, int len, void *arg)
623 {
624     apr_bucket_brigade *bb = (apr_bucket_brigade *)arg;
625     const char *dst_end = buf + len - 1; /* leave room for terminating null */
626     char *dst = buf;
627     apr_bucket *e = APR_BRIGADE_FIRST(bb);
628     apr_status_t rv;
629     int done = 0;
630
631     while ((dst < dst_end) && !done && !APR_BUCKET_IS_EOS(e)) {
632         const char *bucket_data;
633         apr_size_t bucket_data_len;
634         const char *src;
635         const char *src_end;
636         apr_bucket * next;
637
638         rv = apr_bucket_read(e, &bucket_data, &bucket_data_len,
639                              APR_BLOCK_READ);
640         if (rv != APR_SUCCESS || (bucket_data_len == 0)) {
641             return APR_STATUS_IS_TIMEUP(rv) ? -1 : 0;
642         }
643         src = bucket_data;
644         src_end = bucket_data + bucket_data_len;
645         while ((src < src_end) && (dst < dst_end) && !done) {
646             if (*src == '\n') {
647                 done = 1;
648             }
649             else if (*src != '\r') {
650                 *dst++ = *src;
651             }
652             src++;
653         }
654
655         if (src < src_end) {
656             apr_bucket_split(e, src - bucket_data);
657         }
658         next = APR_BUCKET_NEXT(e);
659         APR_BUCKET_REMOVE(e);
660         apr_bucket_destroy(e);
661         e = next;
662     }
663     *dst = 0;
664     return 1;
665 }
666
667 AP_DECLARE(int) ap_scan_script_header_err_brigade(request_rec *r,
668                                                   apr_bucket_brigade *bb,
669                                                   char *buffer)
670 {
671     return ap_scan_script_header_err_core(r, buffer, getsfunc_BRIGADE, bb);
672 }
673
674 struct vastrs {
675     va_list args;
676     int arg;
677     const char *curpos;
678 };
679
680 static int getsfunc_STRING(char *w, int len, void *pvastrs)
681 {
682     struct vastrs *strs = (struct vastrs*) pvastrs;
683     const char *p;
684     int t;
685
686     if (!strs->curpos || !*strs->curpos)
687         return 0;
688     p = ap_strchr_c(strs->curpos, '\n');
689     if (p)
690         ++p;
691     else
692         p = ap_strchr_c(strs->curpos, '\0');
693     t = p - strs->curpos;
694     if (t > len)
695         t = len;
696     strncpy (w, strs->curpos, t);
697     w[t] = '\0';
698     if (!strs->curpos[t]) {
699         ++strs->arg;
700         strs->curpos = va_arg(strs->args, const char *);
701     }
702     else
703         strs->curpos += t;
704     return t;
705 }
706
707 /* ap_scan_script_header_err_strs() accepts additional const char* args...
708  * each is treated as one or more header lines, and the first non-header
709  * character is returned to **arg, **data.  (The first optional arg is
710  * counted as 0.)
711  */
712 AP_DECLARE_NONSTD(int) ap_scan_script_header_err_strs(request_rec *r,
713                                                       char *buffer,
714                                                       const char **termch,
715                                                       int *termarg, ...)
716 {
717     struct vastrs strs;
718     int res;
719
720     va_start(strs.args, termarg);
721     strs.arg = 0;
722     strs.curpos = va_arg(strs.args, char*);
723     res = ap_scan_script_header_err_core(r, buffer, getsfunc_STRING, (void *) &strs);
724     if (termch)
725         *termch = strs.curpos;
726     if (termarg)
727         *termarg = strs.arg;
728     va_end(strs.args);
729     return res;
730 }
731
732
733 static void
734 argstr_to_table(char *str, apr_table_t *parms)
735 {
736     char *key;
737     char *value;
738     char *strtok_state;
739
740     if (str == NULL) {
741         return;
742     }
743     
744     key = apr_strtok(str, "&", &strtok_state);
745     while (key) {
746         value = strchr(key, '=');
747         if (value) {
748             *value = '\0';      /* Split the string in two */
749             value++;            /* Skip passed the = */
750         }
751         else {
752             value = "1";
753         }
754         ap_unescape_url(key);
755         ap_unescape_url(value);
756         apr_table_set(parms, key, value);
757         key = apr_strtok(NULL, "&", &strtok_state);
758     }
759 }
760
761 AP_DECLARE(void) ap_args_to_table(request_rec *r, apr_table_t **table)
762 {
763     apr_table_t *t = apr_table_make(r->pool, 10);
764     argstr_to_table(apr_pstrdup(r->pool, r->args), t);
765     *table = t;
766 }