util_script.c revision 7184de27ec1d62a83c41cdeac0953ca9fd661e8c
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* Licensed to the Apache Software Foundation (ASF) under one or more
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * contributor license agreements. See the NOTICE file distributed with
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * this work for additional information regarding copyright ownership.
0a05fab9aadd37834734ffe106fc8ad4488fb3e3rbowen * The ASF licenses this file to You under the Apache License, Version 2.0
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * (the "License"); you may not use this file except in compliance with
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * the License. You may obtain a copy of the License at
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Unless required by applicable law or agreed to in writing, software
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * distributed under the License is distributed on an "AS IS" BASIS,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * See the License for the specific language governing permissions and
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * limitations under the License.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "http_request.h" /* for sub_req_lookup_uri() */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Various utility functions which are common to a whole lot of
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * script-type extensions mechanisms, and might as well be gathered
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * in one place (if only to avoid creating inter-module dependancies
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * where there don't have to be).
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#define MALFORMED_MESSAGE "malformed header from script. Bad header="
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis/* we know core's module_index is 0 */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *res = (char *)apr_palloc(r->pool, sizeof("HTTP_") + strlen(w));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while ((c = *w++) != 0) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (c == '-') {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor "Not exporting header with invalid name as envvar: %s",
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorstatic void add_unless_null(apr_table_t *table, const char *name, const char *val)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorstatic void env2env(apr_table_t *table, const char *name)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorAP_DECLARE(char **) ap_create_environment(apr_pool_t *p, apr_table_t *t)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const apr_array_header_t *env_arr = apr_table_elts(t);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **env = (char **) apr_palloc(p, (env_arr->nelts + 2) * sizeof(char *));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor env[j] = apr_pstrcat(p, elts[i].key, "=", elts[i].val, NULL);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const char *env_temp;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis /* use a temporary apr_table_t which we'll overlap onto
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * r->subprocess_env later
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * (exception: if r->subprocess_env is empty at the start,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * write directly into it)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* First, add environment vars from headers... this is as per
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * CGI specs, though other sorts of scripting interfaces see
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * the same vars...
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* A few headers are special cased --- Authorization to prevent
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * rogue scripts from capturing passwords; content-type and -length
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * for no particular reason.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (!strcasecmp(hdrs[i].key, "Content-length")) {
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis * You really don't want to disable this check, since it leaves you
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis * wide open to CGIs stealing passwords and people viewing them
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * in the environment with "ps -e". But, if you must...
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor || !strcasecmp(hdrs[i].key, "Proxy-Authorization")) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor add_unless_null(e, http2env(r, hdrs[i].key), hdrs[i].val);
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis apr_table_addn(e, "PATH", apr_pstrdup(r->pool, env_temp));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#if defined(WIN32)
fdd456e363b82501c228e3ca0f74750d9ca1472flgentis /* HPUX PARISC 2.0W knows both, otherwise redundancy is harmless */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#else /* Some Unix */
5ed11195e199bfaef4e520a412510b5ad0c158c5lgentis apr_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis apr_table_addn(e, "SERVER_SOFTWARE", ap_get_server_banner());
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "SERVER_ADDR", r->connection->local_ip); /* Apache */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST, NULL));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r)); /* Apache */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "REQUEST_SCHEME", ap_http_scheme(r));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "CONTEXT_PREFIX", ap_context_prefix(r));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "CONTEXT_DOCUMENT_ROOT", ap_context_document_root(r));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "SCRIPT_FILENAME", r->filename); /* Apache */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "REMOTE_PORT", apr_itoa(r->pool, rport));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (r->prev) {
b2d402bf3e0826117941602dd3c1c5b41a7ed630lgentis apr_table_addn(e, "REDIRECT_REMOTE_USER", back->user);
b2d402bf3e0826117941602dd3c1c5b41a7ed630lgentis apr_table_addn(e, "REMOTE_IDENT", apr_pstrdup(r->pool, env_temp));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Apache custom error responses. If we have redirected set two new vars */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor add_unless_null(e, "REDIRECT_QUERY_STRING", r->prev->args);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (e != r->subprocess_env) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_overlap(r->subprocess_env, e, APR_OVERLAP_TABLES_SET);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* This "cute" little function comes about because the path info on
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * filenames and URLs aren't always the same. So we take the two,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * and find as much of the two that match as possible.
9a2c99c15b21987ac37de808c8ff49f2d02a9c9flgentisAP_DECLARE(int) ap_find_path_info(const char *uri, const char *path_info)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* Obtain the Request-URI from the original request-line, returning
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * a new string from the request pool containing the URI or "".
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
3169b556e342e88cdd0719be473eaf27d3084340lgentis /* Note that the code below special-cases scripts run from includes,
fdd456e363b82501c228e3ca0f74750d9ca1472flgentis * because it "knows" that the sub_request has been hacked to have the
fdd456e363b82501c228e3ca0f74750d9ca1472flgentis * args and path_info of the original request, and not any that may have
fdd456e363b82501c228e3ca0f74750d9ca1472flgentis * come with the script URI in the include command. Ugh.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int path_info_start = ap_find_path_info(r->uri, r->path_info);
1828bbc20654649b007eec284bb0b751fed08b91lgentis * To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
1828bbc20654649b007eec284bb0b751fed08b91lgentis * Need to re-escape it for this, since the entire URI was
1828bbc20654649b007eec284bb0b751fed08b91lgentis * un-escaped before we determined where the PATH_INFO began.
1828bbc20654649b007eec284bb0b751fed08b91lgentis pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r,
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis char *pt = apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis /* We need to make this a real Windows path name */
1828bbc20654649b007eec284bb0b751fed08b91lgentis apr_filepath_merge(&pt, "", pt, APR_FILEPATH_NATIVE, r->pool);
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentisstatic int set_cookie_doo_doo(void *v, const char *key, const char *val)
1828bbc20654649b007eec284bb0b751fed08b91lgentisAP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
1828bbc20654649b007eec284bb0b751fed08b91lgentis int (*getsfunc) (char *, int, void *),
020366f830905b6b5dfccfa03373379ae6a13e7blgentis char *w, *l;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* temporary place to hold headers to merge in later */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* The HTTP specification says that it is legal to merge duplicate
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * headers into one. Some browsers that support Cookies don't like
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * merged headers and prefer that each Set-Cookie header is sent
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * separately. Lets humour those browsers by not merging.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Oh what a pain it is.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out, "Set-Cookie", NULL);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (1) {
7ddfde8cc1587a70759084db65c83a02e51380d1lgentis int rv = (*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (rv == 0) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor "Premature end of script headers: %s",
7ddfde8cc1587a70759084db65c83a02e51380d1lgentis ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
7ddfde8cc1587a70759084db65c83a02e51380d1lgentis "Script timed out before returning headers: %s",
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Delete terminal (CR?)LF */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Indeed, the host's '\n':
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor -- whatever the script generates.
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis * If we've finished reading the headers, check to make sure any
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis * HTTP/1.1 conditions are met. If so, we're done; normal processing
9a2c99c15b21987ac37de808c8ff49f2d02a9c9flgentis * will handle the script's output. If not, just return the error.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * The appropriate thing to do would be to send the script process a
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * SIGPIPE to let it know we're ignoring it, close the channel to the
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * script process, and *then* return the failed-to-meet-condition
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * error. Otherwise we'd be waiting for the script to finish
c9cea69ad0824357e3a28411a30f966c7d299116lgentis * blithering before telling the client the output was no good.
c9cea69ad0824357e3a28411a30f966c7d299116lgentis * However, we don't have the information to do that, so we have to
c9cea69ad0824357e3a28411a30f966c7d299116lgentis * leave it to an upper layer.
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis if (w[0] == '\0') {
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis /* PR#38070: This fails because it gets confused when a
c9cea69ad0824357e3a28411a30f966c7d299116lgentis * CGI Status header overrides ap_meets_conditions.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * We can fix that by dropping ap_meets_conditions when
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Status has been set. Since this is the only place
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * cgi_status gets used, let's test it explicitly.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * The alternative would be to ignore CGI Status when
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * ap_meets_conditions returns anything interesting.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * That would be safer wrt HTTP, but would break CGI.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if ((cgi_status == HTTP_UNSET) && (r->method_number == M_GET)) {
2352e688b8a17f1d6cfb3558bcb95ae9145b4075lgentis /* the cookies have already been copied to the cookie_table */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* if we see a bogus header don't ignore it. Shout and scream */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Chances are that we received an ASCII header text instead of
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * the expected EBCDIC header lines. Try to auto-detect:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis#endif /*APR_CHARSET_EBCDIC*/
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
3169b556e342e88cdd0719be473eaf27d3084340lgentis /* Soak up all the script output - may save an outright kill */
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *l++ = '\0';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (*l && apr_isspace(*l)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Nuke trailing whitespace */
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis * If the script returned a specific status, that's what
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis * we'll use - otherwise we assume 200 OK.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * If the script gave us a Last-Modified header, we can't just
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * pass it on blindly because of restrictions on future values.
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis /* never reached - we leave this function within the while loop above */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return apr_file_gets(buf, len, (apr_file_t *) f) == APR_SUCCESS;
3169b556e342e88cdd0719be473eaf27d3084340lgentisAP_DECLARE(int) ap_scan_script_header_err(request_rec *r, apr_file_t *f,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return ap_scan_script_header_err_core(r, buffer, getsfunc_FILE, f);
int done = 0;
const char *bucket_data;
const char *src;
const char *src_end;
src++;
e = next;
*dst = 0;
char *buffer)
struct vastrs {
int arg;
const char *curpos;
if (t > len)
t = len;
char *buffer,
const char **termch,
int *termarg, ...)
int res;
if (termch)
if (termarg)
return res;
char *key;
char *value;
char *strtok_state;
while (key) {
if (value) {
*table = t;