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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
19
#include "apr_strings.h"
21
#define APR_WANT_STRFUNC
29
#include "ap_config.h"
31
#include "http_config.h"
32
#include "http_main.h"
34
#include "http_core.h"
35
#include "http_protocol.h"
36
#include "http_request.h" /* for sub_req_lookup_uri() */
37
#include "util_script.h"
38
#include "apr_date.h" /* For apr_date_parse_http() */
39
#include "util_ebcdic.h"
47
* Various utility functions which are common to a whole lot of
48
* script-type extensions mechanisms, and might as well be gathered
49
* in one place (if only to avoid creating inter-module dependancies
50
* where there don't have to be).
53
#define MALFORMED_MESSAGE "malformed header from script. Bad header="
54
#define MALFORMED_HEADER_LENGTH_TO_SHOW 30
56
static char *http2env(apr_pool_t *a, const char *w)
58
char *res = (char *)apr_palloc(a, sizeof("HTTP_") + strlen(w));
68
while ((c = *w++) != 0) {
69
if (!apr_isalnum(c)) {
73
*cp++ = apr_toupper(c);
81
AP_DECLARE(char **) ap_create_environment(apr_pool_t *p, apr_table_t *t)
83
const apr_array_header_t *env_arr = apr_table_elts(t);
84
const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
85
char **env = (char **) apr_palloc(p, (env_arr->nelts + 2) * sizeof(char *));
91
if (!apr_table_get(t, "TZ")) {
94
env[j++] = apr_pstrcat(p, "TZ=", tz, NULL);
97
for (i = 0; i < env_arr->nelts; ++i) {
101
env[j] = apr_pstrcat(p, elts[i].key, "=", elts[i].val, NULL);
103
if (apr_isdigit(*whack)) {
106
while (*whack != '=') {
107
if (!apr_isalnum(*whack) && *whack != '_') {
119
AP_DECLARE(void) ap_add_common_vars(request_rec *r)
122
server_rec *s = r->server;
123
conn_rec *c = r->connection;
124
const char *rem_logname;
126
#if defined(WIN32) || defined(OS2) || defined(BEOS)
130
const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
131
const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
135
/* use a temporary apr_table_t which we'll overlap onto
136
* r->subprocess_env later
137
* (exception: if r->subprocess_env is empty at the start,
138
* write directly into it)
140
if (apr_is_empty_table(r->subprocess_env)) {
141
e = r->subprocess_env;
144
e = apr_table_make(r->pool, 25 + hdrs_arr->nelts);
147
/* First, add environment vars from headers... this is as per
148
* CGI specs, though other sorts of scripting interfaces see
152
for (i = 0; i < hdrs_arr->nelts; ++i) {
157
/* A few headers are special cased --- Authorization to prevent
158
* rogue scripts from capturing passwords; content-type and -length
159
* for no particular reason.
162
if (!strcasecmp(hdrs[i].key, "Content-type")) {
163
apr_table_addn(e, "CONTENT_TYPE", hdrs[i].val);
165
else if (!strcasecmp(hdrs[i].key, "Content-length")) {
166
apr_table_addn(e, "CONTENT_LENGTH", hdrs[i].val);
169
* You really don't want to disable this check, since it leaves you
170
* wide open to CGIs stealing passwords and people viewing them
171
* in the environment with "ps -e". But, if you must...
173
#ifndef SECURITY_HOLE_PASS_AUTHORIZATION
174
else if (!strcasecmp(hdrs[i].key, "Authorization")
175
|| !strcasecmp(hdrs[i].key, "Proxy-Authorization")) {
180
apr_table_addn(e, http2env(r->pool, hdrs[i].key), hdrs[i].val);
184
if (!(env_path = getenv("PATH"))) {
185
env_path = DEFAULT_PATH;
187
apr_table_addn(e, "PATH", apr_pstrdup(r->pool, env_path));
190
if (env_temp = getenv("SystemRoot")) {
191
apr_table_addn(e, "SystemRoot", env_temp);
193
if (env_temp = getenv("COMSPEC")) {
194
apr_table_addn(e, "COMSPEC", env_temp);
196
if (env_temp = getenv("PATHEXT")) {
197
apr_table_addn(e, "PATHEXT", env_temp);
199
if (env_temp = getenv("WINDIR")) {
200
apr_table_addn(e, "WINDIR", env_temp);
205
if ((env_temp = getenv("COMSPEC")) != NULL) {
206
apr_table_addn(e, "COMSPEC", env_temp);
208
if ((env_temp = getenv("ETC")) != NULL) {
209
apr_table_addn(e, "ETC", env_temp);
211
if ((env_temp = getenv("DPATH")) != NULL) {
212
apr_table_addn(e, "DPATH", env_temp);
214
if ((env_temp = getenv("PERLLIB_PREFIX")) != NULL) {
215
apr_table_addn(e, "PERLLIB_PREFIX", env_temp);
220
if ((env_temp = getenv("LIBRARY_PATH")) != NULL) {
221
apr_table_addn(e, "LIBRARY_PATH", env_temp);
225
apr_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
226
apr_table_addn(e, "SERVER_SOFTWARE", ap_get_server_version());
227
apr_table_addn(e, "SERVER_NAME",
228
ap_escape_html(r->pool, ap_get_server_name(r)));
229
apr_table_addn(e, "SERVER_ADDR", r->connection->local_ip); /* Apache */
230
apr_table_addn(e, "SERVER_PORT",
231
apr_psprintf(r->pool, "%u", ap_get_server_port(r)));
232
host = ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST, NULL);
234
apr_table_addn(e, "REMOTE_HOST", host);
236
apr_table_addn(e, "REMOTE_ADDR", c->remote_ip);
237
apr_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r)); /* Apache */
238
apr_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
239
apr_table_addn(e, "SCRIPT_FILENAME", r->filename); /* Apache */
241
rport = c->remote_addr->port;
242
apr_table_addn(e, "REMOTE_PORT", apr_itoa(r->pool, rport));
245
apr_table_addn(e, "REMOTE_USER", r->user);
248
request_rec *back = r->prev;
252
apr_table_addn(e, "REDIRECT_REMOTE_USER", back->user);
258
if (r->ap_auth_type) {
259
apr_table_addn(e, "AUTH_TYPE", r->ap_auth_type);
261
rem_logname = ap_get_remote_logname(r);
263
apr_table_addn(e, "REMOTE_IDENT", apr_pstrdup(r->pool, rem_logname));
266
/* Apache custom error responses. If we have redirected set two new vars */
270
apr_table_addn(e, "REDIRECT_QUERY_STRING", r->prev->args);
273
apr_table_addn(e, "REDIRECT_URL", r->prev->uri);
277
if (e != r->subprocess_env) {
278
apr_table_overlap(r->subprocess_env, e, APR_OVERLAP_TABLES_SET);
282
/* This "cute" little function comes about because the path info on
283
* filenames and URLs aren't always the same. So we take the two,
284
* and find as much of the two that match as possible.
287
AP_DECLARE(int) ap_find_path_info(const char *uri, const char *path_info)
289
int lu = strlen(uri);
290
int lp = strlen(path_info);
292
while (lu-- && lp-- && uri[lu] == path_info[lp]) {
293
if (path_info[lp] == '/') {
294
while (lu && uri[lu-1] == '/') lu--;
302
while (uri[lu] != '\0' && uri[lu] != '/') {
308
/* Obtain the Request-URI from the original request-line, returning
309
* a new string from the request pool containing the URI or "".
311
static char *original_uri(request_rec *r)
315
if (r->the_request == NULL) {
316
return (char *) apr_pcalloc(r->pool, 1);
319
first = r->the_request; /* use the request-line */
321
while (*first && !apr_isspace(*first)) {
322
++first; /* skip over the method */
324
while (apr_isspace(*first)) {
325
++first; /* and the space(s) */
329
while (*last && !apr_isspace(*last)) {
330
++last; /* end at next whitespace */
333
return apr_pstrmemdup(r->pool, first, last - first);
336
AP_DECLARE(void) ap_add_cgi_vars(request_rec *r)
338
apr_table_t *e = r->subprocess_env;
340
apr_table_setn(e, "GATEWAY_INTERFACE", "CGI/1.1");
341
apr_table_setn(e, "SERVER_PROTOCOL", r->protocol);
342
apr_table_setn(e, "REQUEST_METHOD", r->method);
343
apr_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
344
apr_table_setn(e, "REQUEST_URI", original_uri(r));
346
/* Note that the code below special-cases scripts run from includes,
347
* because it "knows" that the sub_request has been hacked to have the
348
* args and path_info of the original request, and not any that may have
349
* come with the script URI in the include command. Ugh.
352
if (!strcmp(r->protocol, "INCLUDED")) {
353
apr_table_setn(e, "SCRIPT_NAME", r->uri);
354
if (r->path_info && *r->path_info) {
355
apr_table_setn(e, "PATH_INFO", r->path_info);
358
else if (!r->path_info || !*r->path_info) {
359
apr_table_setn(e, "SCRIPT_NAME", r->uri);
362
int path_info_start = ap_find_path_info(r->uri, r->path_info);
364
apr_table_setn(e, "SCRIPT_NAME",
365
apr_pstrndup(r->pool, r->uri, path_info_start));
367
apr_table_setn(e, "PATH_INFO", r->path_info);
370
if (r->path_info && r->path_info[0]) {
372
* To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
373
* Need to re-escape it for this, since the entire URI was
374
* un-escaped before we determined where the PATH_INFO began.
378
pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r,
381
if (pa_req->filename) {
382
char *pt = apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
385
/* We need to make this a real Windows path name */
386
apr_filepath_merge(&pt, "", pt, APR_FILEPATH_NATIVE, r->pool);
388
apr_table_setn(e, "PATH_TRANSLATED", pt);
390
ap_destroy_sub_req(pa_req);
395
static int set_cookie_doo_doo(void *v, const char *key, const char *val)
397
apr_table_addn(v, key, val);
401
#define HTTP_UNSET (-HTTP_OK)
403
AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
404
int (*getsfunc) (char *, int, void *),
407
char x[MAX_STRING_LEN];
410
int cgi_status = HTTP_UNSET;
412
apr_table_t *cookie_table;
417
w = buffer ? buffer : x;
419
/* temporary place to hold headers to merge in later */
420
merge = apr_table_make(r->pool, 10);
422
/* The HTTP specification says that it is legal to merge duplicate
423
* headers into one. Some browsers that support Cookies don't like
424
* merged headers and prefer that each Set-Cookie header is sent
425
* separately. Lets humour those browsers by not merging.
426
* Oh what a pain it is.
428
cookie_table = apr_table_make(r->pool, 2);
429
apr_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out, "Set-Cookie", NULL);
433
if ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data) == 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;
440
/* Delete terminal (CR?)LF */
443
/* Indeed, the host's '\n':
444
'\012' for UNIX; '\015' for MacOS; '\025' for OS/390
445
-- whatever the script generates.
447
if (p > 0 && w[p - 1] == '\n') {
448
if (p > 1 && w[p - 2] == CR) {
457
* If we've finished reading the headers, check to make sure any
458
* HTTP/1.1 conditions are met. If so, we're done; normal processing
459
* will handle the script's output. If not, just return the error.
460
* The appropriate thing to do would be to send the script process a
461
* SIGPIPE to let it know we're ignoring it, close the channel to the
462
* script process, and *then* return the failed-to-meet-condition
463
* error. Otherwise we'd be waiting for the script to finish
464
* blithering before telling the client the output was no good.
465
* However, we don't have the information to do that, so we have to
466
* leave it to an upper layer.
469
int cond_status = OK;
471
/* PR#38070: This fails because it gets confused when a
472
* CGI Status header overrides ap_meets_conditions.
474
* We can fix that by dropping ap_meets_conditions when
475
* Status has been set. Since this is the only place
476
* cgi_status gets used, let's test it explicitly.
478
* The alternative would be to ignore CGI Status when
479
* ap_meets_conditions returns anything interesting.
480
* That would be safer wrt HTTP, but would break CGI.
482
if ((cgi_status == HTTP_UNSET) && (r->method_number == M_GET)) {
483
cond_status = ap_meets_conditions(r);
485
apr_table_overlap(r->err_headers_out, merge,
486
APR_OVERLAP_TABLES_MERGE);
487
if (!apr_is_empty_table(cookie_table)) {
488
/* the cookies have already been copied to the cookie_table */
489
apr_table_unset(r->err_headers_out, "Set-Cookie");
490
r->err_headers_out = apr_table_overlay(r->pool,
491
r->err_headers_out, cookie_table);
496
/* if we see a bogus header don't ignore it. Shout and scream */
498
#if APR_CHARSET_EBCDIC
499
/* Chances are that we received an ASCII header text instead of
500
* the expected EBCDIC header lines. Try to auto-detect:
502
if (!(l = strchr(w, ':'))) {
503
int maybeASCII = 0, maybeEBCDIC = 0;
504
unsigned char *cp, native;
505
apr_size_t inbytes_left, outbytes_left;
507
for (cp = w; *cp != '\0'; ++cp) {
508
native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
509
if (apr_isprint(*cp) && !apr_isprint(native))
511
if (!apr_isprint(*cp) && apr_isprint(native))
514
if (maybeASCII > maybeEBCDIC) {
515
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
516
"CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
518
inbytes_left = outbytes_left = cp - w;
519
apr_xlate_conv_buffer(ap_hdrs_from_ascii,
520
w, &inbytes_left, w, &outbytes_left);
523
#endif /*APR_CHARSET_EBCDIC*/
524
if (!(l = strchr(w, ':'))) {
525
char malformed[(sizeof MALFORMED_MESSAGE) + 1
526
+ MALFORMED_HEADER_LENGTH_TO_SHOW];
528
strcpy(malformed, MALFORMED_MESSAGE);
529
strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
532
/* Soak up all the script output - may save an outright kill */
533
while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) {
538
ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
540
apr_filepath_name_get(r->filename));
541
return HTTP_INTERNAL_SERVER_ERROR;
545
while (*l && apr_isspace(*l)) {
549
if (!strcasecmp(w, "Content-type")) {
552
/* Nuke trailing whitespace */
554
char *endp = l + strlen(l) - 1;
555
while (endp > l && apr_isspace(*endp)) {
559
tmp = apr_pstrdup(r->pool, l);
560
ap_content_type_tolower(tmp);
561
ap_set_content_type(r, tmp);
564
* If the script returned a specific status, that's what
565
* we'll use - otherwise we assume 200 OK.
567
else if (!strcasecmp(w, "Status")) {
568
r->status = cgi_status = atoi(l);
569
r->status_line = apr_pstrdup(r->pool, l);
571
else if (!strcasecmp(w, "Location")) {
572
apr_table_set(r->headers_out, w, l);
574
else if (!strcasecmp(w, "Content-Length")) {
575
apr_table_set(r->headers_out, w, l);
577
else if (!strcasecmp(w, "Content-Range")) {
578
apr_table_set(r->headers_out, w, l);
580
else if (!strcasecmp(w, "Transfer-Encoding")) {
581
apr_table_set(r->headers_out, w, l);
584
* If the script gave us a Last-Modified header, we can't just
585
* pass it on blindly because of restrictions on future values.
587
else if (!strcasecmp(w, "Last-Modified")) {
588
ap_update_mtime(r, apr_date_parse_http(l));
589
ap_set_last_modified(r);
591
else if (!strcasecmp(w, "Set-Cookie")) {
592
apr_table_add(cookie_table, w, l);
595
apr_table_add(merge, w, l);
602
static int getsfunc_FILE(char *buf, int len, void *f)
604
return apr_file_gets(buf, len, (apr_file_t *) f) == APR_SUCCESS;
607
AP_DECLARE(int) ap_scan_script_header_err(request_rec *r, apr_file_t *f,
610
return ap_scan_script_header_err_core(r, buffer, getsfunc_FILE, f);
613
static int getsfunc_BRIGADE(char *buf, int len, void *arg)
615
apr_bucket_brigade *bb = (apr_bucket_brigade *)arg;
616
const char *dst_end = buf + len - 1; /* leave room for terminating null */
618
apr_bucket *e = APR_BRIGADE_FIRST(bb);
622
while ((dst < dst_end) && !done && !APR_BUCKET_IS_EOS(e)) {
623
const char *bucket_data;
624
apr_size_t bucket_data_len;
629
rv = apr_bucket_read(e, &bucket_data, &bucket_data_len,
631
if (rv != APR_SUCCESS || (bucket_data_len == 0)) {
635
src_end = bucket_data + bucket_data_len;
636
while ((src < src_end) && (dst < dst_end) && !done) {
640
else if (*src != '\r') {
647
apr_bucket_split(e, src - bucket_data);
649
next = APR_BUCKET_NEXT(e);
650
APR_BUCKET_REMOVE(e);
651
apr_bucket_destroy(e);
658
AP_DECLARE(int) ap_scan_script_header_err_brigade(request_rec *r,
659
apr_bucket_brigade *bb,
662
return ap_scan_script_header_err_core(r, buffer, getsfunc_BRIGADE, bb);
671
static int getsfunc_STRING(char *w, int len, void *pvastrs)
673
struct vastrs *strs = (struct vastrs*) pvastrs;
677
if (!strs->curpos || !*strs->curpos)
679
p = ap_strchr_c(strs->curpos, '\n');
683
p = ap_strchr_c(strs->curpos, '\0');
684
t = p - strs->curpos;
687
strncpy (w, strs->curpos, t);
689
if (!strs->curpos[t]) {
691
strs->curpos = va_arg(strs->args, const char *);
698
/* ap_scan_script_header_err_strs() accepts additional const char* args...
699
* each is treated as one or more header lines, and the first non-header
700
* character is returned to **arg, **data. (The first optional arg is
703
AP_DECLARE_NONSTD(int) ap_scan_script_header_err_strs(request_rec *r,
711
va_start(strs.args, termarg);
713
strs.curpos = va_arg(strs.args, char*);
714
res = ap_scan_script_header_err_core(r, buffer, getsfunc_STRING, (void *) &strs);
716
*termch = strs.curpos;