1 /* 2 * http_cgi - Common Gateway Interface (CGI) interfaces (RFC 3875) 3 * 4 * Copyright(c) 2016-2017 Glenn Strauss gstrauss()gluelogic.com All rights reserved 5 * License: BSD 3-clause (same as lighttpd) 6 */ 7 #include "first.h" 8 9 #include "http_cgi.h" 10 11 #include "sys-socket.h" 12 #include <string.h> 13 14 #include "base.h" 15 #include "array.h" 16 #include "buffer.h" 17 #include "chunk.h" 18 #include "log.h" 19 #include "http_header.h" 20 #include "sock_addr.h" 21 22 handler_t 23 http_cgi_local_redir (request_st * const r) 24 { 25 /* [RFC3875] The Common Gateway Interface (CGI) Version 1.1 26 * [RFC3875] 6.2.2 Local Redirect Response 27 * 28 * The CGI script can return a URI path and query-string 29 * ('local-pathquery') for a local resource in a Location header field. 30 * This indicates to the server that it should reprocess the request 31 * using the path specified. 32 * 33 * local-redir-response = local-Location NL 34 * 35 * The script MUST NOT return any other header fields or a message-body, 36 * and the server MUST generate the response that it would have produced 37 * in response to a request containing the URL 38 * 39 * scheme "://" server-name ":" server-port local-pathquery 40 * 41 * (While not required by the RFC, do not send local-redir back to same URL 42 * since CGI should have handled it internally if it really wanted to do 43 * that internally) 44 */ 45 46 size_t ulen = buffer_string_length(&r->uri.path); 47 const buffer *vb = http_header_response_get(r, HTTP_HEADER_LOCATION, 48 CONST_STR_LEN("Location")); 49 if (NULL != vb 50 && vb->ptr[0] == '/' 51 && (0 != strncmp(vb->ptr, r->uri.path.ptr, ulen) 52 || ( vb->ptr[ulen] != '\0' 53 && vb->ptr[ulen] != '/' 54 && vb->ptr[ulen] != '?')) 55 && !light_btst(r->resp_htags, HTTP_HEADER_STATUS) 56 && 1 == r->resp_headers.used /*"Location"; no "Status" or NPH response*/ 57 && r->http_status >= 300 && r->http_status < 400) { 58 if (++r->loops_per_request > 5) { 59 log_error(r->conf.errh, __FILE__, __LINE__, 60 "too many internal loops while processing request: %s", 61 r->target_orig.ptr); 62 r->http_status = 500; /* Internal Server Error */ 63 r->resp_body_started = 0; 64 r->handler_module = NULL; 65 return HANDLER_FINISHED; 66 } 67 68 buffer_copy_buffer(&r->target, vb); 69 70 if (r->reqbody_length) { 71 if (r->reqbody_length != r->reqbody_queue.bytes_in) 72 r->keep_alive = 0; 73 r->reqbody_length = 0; 74 chunkqueue_reset(&r->reqbody_queue); 75 } 76 77 if (r->http_status != 307 && r->http_status != 308) { 78 /* Note: request body (if any) sent to initial dynamic handler 79 * and is not available to the internal redirect */ 80 r->http_method = HTTP_METHOD_GET; 81 } 82 83 /*(caller must reset request as follows)*/ 84 /*http_response_reset(r);*/ /*(sets r->http_status = 0)*/ 85 /*plugins_call_handle_request_reset(r);*/ 86 87 return HANDLER_COMEBACK; 88 } 89 90 return HANDLER_GO_ON; 91 } 92 93 94 static void 95 http_cgi_encode_varname (buffer * const b, const char * const restrict s, const size_t len, const int is_http_header) 96 { 97 char * const restrict p = buffer_string_prepare_copy(b, len + 5); 98 size_t i, j = 0; 99 100 if (is_http_header) { 101 if (len == 12 && buffer_eq_icase_ssn(s, "Content-Type", 12)) { 102 buffer_copy_string_len(b, CONST_STR_LEN("CONTENT_TYPE")); 103 return; 104 } 105 memcpy(p, "HTTP_", 5); 106 j = 5; /* "HTTP_" */ 107 } 108 109 for (i = 0; i < len; ++i) {/* uppercase alpha, pass numeric, map rest '_' */ 110 const unsigned char c = s[i]; 111 p[j++] = light_isalpha(c) ? c & ~0x20 : light_isdigit(c) ? c : '_'; 112 } 113 buffer_string_set_length(b, j); 114 } 115 116 117 int 118 http_cgi_headers (request_st * const r, http_cgi_opts * const opts, http_cgi_header_append_cb cb, void *vdata) 119 { 120 /* CGI-SPEC 6.1.2, FastCGI spec 6.3 and SCGI spec */ 121 122 /* note: string ptrs passed to cb() func must not be NULL */ 123 124 int rc = 0; 125 buffer * const tb = r->tmp_buf; 126 const char *s; 127 size_t n; 128 char buf[INET6_ADDRSTRLEN + 1]; /*(also larger than LI_ITOSTRING_LENGTH)*/ 129 130 /* (CONTENT_LENGTH must be first for SCGI) */ 131 if (!opts->authorizer) 132 rc |= cb(vdata, CONST_STR_LEN("CONTENT_LENGTH"), 133 buf, li_itostrn(buf,sizeof(buf),r->reqbody_length)); 134 135 if (!buffer_string_is_empty(&r->uri.query)) 136 rc |= cb(vdata, CONST_STR_LEN("QUERY_STRING"), 137 CONST_BUF_LEN(&r->uri.query)); 138 else 139 rc |= cb(vdata, CONST_STR_LEN("QUERY_STRING"), 140 CONST_STR_LEN("")); 141 142 if (!buffer_string_is_empty(opts->strip_request_uri)) { 143 /** 144 * /app1/index/list 145 * 146 * stripping /app1 or /app1/ should lead to 147 * 148 * /index/list 149 * 150 */ 151 size_t len = buffer_string_length(opts->strip_request_uri); 152 if ('/' == opts->strip_request_uri->ptr[len-1]) 153 --len; 154 155 if (buffer_string_length(&r->target_orig) >= len 156 && 0 == memcmp(r->target_orig.ptr, 157 opts->strip_request_uri->ptr, len) 158 && r->target_orig.ptr[len] == '/') { 159 rc |= cb(vdata, CONST_STR_LEN("REQUEST_URI"), 160 r->target_orig.ptr+len, 161 buffer_string_length(&r->target_orig)-len); 162 } 163 else 164 rc |= cb(vdata, CONST_STR_LEN("REQUEST_URI"), 165 CONST_BUF_LEN(&r->target_orig)); 166 } 167 else 168 rc |= cb(vdata, CONST_STR_LEN("REQUEST_URI"), 169 CONST_BUF_LEN(&r->target_orig)); 170 171 if (!buffer_is_equal(&r->target, &r->target_orig)) 172 rc |= cb(vdata, CONST_STR_LEN("REDIRECT_URI"), 173 CONST_BUF_LEN(&r->target)); 174 175 /* set REDIRECT_STATUS for php compiled with --force-redirect 176 * (if REDIRECT_STATUS has not already been set by error handler) */ 177 if (0 == r->error_handler_saved_status) 178 rc |= cb(vdata, CONST_STR_LEN("REDIRECT_STATUS"), 179 CONST_STR_LEN("200")); 180 181 /* 182 * SCRIPT_NAME, PATH_INFO and PATH_TRANSLATED according to 183 * http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html 184 * (6.1.14, 6.1.6, 6.1.7) 185 */ 186 if (!opts->authorizer) { 187 rc |= cb(vdata, CONST_STR_LEN("SCRIPT_NAME"), 188 CONST_BUF_LEN(&r->uri.path)); 189 if (!buffer_string_is_empty(&r->pathinfo)) { 190 rc |= cb(vdata, CONST_STR_LEN("PATH_INFO"), 191 CONST_BUF_LEN(&r->pathinfo)); 192 /* PATH_TRANSLATED is only defined if PATH_INFO is set 193 * Note: not implemented: re-url-encode '?' '=' ';' for 194 * (RFC 3875 4.1.6) */ 195 if (!buffer_string_is_empty(opts->docroot)) 196 buffer_copy_buffer(tb, opts->docroot); 197 else 198 buffer_copy_buffer(tb, &r->physical.basedir); 199 buffer_append_path_len(tb, CONST_BUF_LEN(&r->pathinfo)); 200 rc |= cb(vdata, CONST_STR_LEN("PATH_TRANSLATED"), 201 CONST_BUF_LEN(tb)); 202 } 203 } 204 205 /* 206 * SCRIPT_FILENAME and DOCUMENT_ROOT for php 207 * The PHP manual http://www.php.net/manual/en/reserved.variables.php 208 * treatment of PATH_TRANSLATED is different from the one of CGI specs. 209 * (see php.ini cgi.fix_pathinfo = 1 config parameter) 210 */ 211 212 if (!buffer_string_is_empty(opts->docroot)) { 213 /* alternate docroot, e.g. for remote FastCGI or SCGI server */ 214 buffer_copy_buffer(tb, opts->docroot); 215 buffer_append_path_len(tb, CONST_BUF_LEN(&r->uri.path)); 216 rc |= cb(vdata, CONST_STR_LEN("SCRIPT_FILENAME"), 217 CONST_BUF_LEN(tb)); 218 rc |= cb(vdata, CONST_STR_LEN("DOCUMENT_ROOT"), 219 CONST_BUF_LEN(opts->docroot)); 220 } 221 else { 222 if (opts->break_scriptfilename_for_php) { 223 /* php.ini config cgi.fix_pathinfo = 1 need a broken SCRIPT_FILENAME 224 * to find out what PATH_INFO is itself 225 * 226 * see src/sapi/cgi_main.c, init_request_info() 227 */ 228 buffer_copy_buffer(tb, &r->physical.path); 229 buffer_append_path_len(tb, CONST_BUF_LEN(&r->pathinfo)); 230 rc |= cb(vdata, CONST_STR_LEN("SCRIPT_FILENAME"), 231 CONST_BUF_LEN(tb)); 232 } 233 else 234 rc |= cb(vdata, CONST_STR_LEN("SCRIPT_FILENAME"), 235 CONST_BUF_LEN(&r->physical.path)); 236 rc |= cb(vdata, CONST_STR_LEN("DOCUMENT_ROOT"), 237 CONST_BUF_LEN(&r->physical.basedir)); 238 } 239 240 s = get_http_method_name(r->http_method); 241 force_assert(s); 242 rc |= cb(vdata, CONST_STR_LEN("REQUEST_METHOD"), s, strlen(s)); 243 244 s = get_http_version_name(r->http_version); 245 force_assert(s); 246 rc |= cb(vdata, CONST_STR_LEN("SERVER_PROTOCOL"), s, strlen(s)); 247 248 rc |= cb(vdata, CONST_STR_LEN("SERVER_SOFTWARE"), 249 CONST_BUF_LEN(r->conf.server_tag)); 250 251 rc |= cb(vdata, CONST_STR_LEN("GATEWAY_INTERFACE"), 252 CONST_STR_LEN("CGI/1.1")); 253 254 rc |= cb(vdata, CONST_STR_LEN("REQUEST_SCHEME"), 255 CONST_BUF_LEN(&r->uri.scheme)); 256 257 if (buffer_is_equal_string(&r->uri.scheme, CONST_STR_LEN("https"))) 258 rc |= cb(vdata, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on")); 259 260 const connection * const con = r->con; 261 const server_socket * const srv_sock = con->srv_socket; 262 const size_t tlen = buffer_string_length(srv_sock->srv_token); 263 n = srv_sock->srv_token_colon; 264 if (n < tlen) { /*(n != tlen)*/ 265 s = srv_sock->srv_token->ptr+n+1; 266 n = tlen - (n+1); 267 } 268 else { 269 s = "0"; 270 n = 1; 271 } 272 rc |= cb(vdata, CONST_STR_LEN("SERVER_PORT"), s, n); 273 274 n = 0; 275 switch (sock_addr_get_family(&srv_sock->addr)) { 276 case AF_INET: 277 case AF_INET6: 278 if (sock_addr_is_addr_wildcard(&srv_sock->addr)) { 279 sock_addr addrbuf; 280 socklen_t addrlen = sizeof(addrbuf); 281 if (0 == getsockname(con->fd,(struct sockaddr *)&addrbuf,&addrlen)){ 282 /* future: might add a one- or two- element cache 283 * or use sock_addr_cache_inet_ntop_copy_buffer() into tb */ 284 s = sock_addr_inet_ntop(&addrbuf, buf, sizeof(buf)); 285 if (s) 286 n = strlen(s); 287 else 288 s = ""; 289 } 290 else 291 s = ""; 292 } 293 else { 294 s = srv_sock->srv_token->ptr; 295 n = srv_sock->srv_token_colon; 296 } 297 break; 298 default: 299 s = ""; 300 break; 301 } 302 rc |= cb(vdata, CONST_STR_LEN("SERVER_ADDR"), s, n); 303 304 if (!buffer_string_is_empty(r->server_name)) { 305 n = buffer_string_length(r->server_name); 306 s = r->server_name->ptr; 307 if (s[0] == '[') { 308 const char *colon = strstr(s, "]:"); 309 if (colon) n = (colon + 1) - s; 310 } 311 else { 312 const char *colon = strchr(s, ':'); 313 if (colon) n = colon - s; 314 } 315 } /* else set to be same as SERVER_ADDR (above) */ 316 rc |= cb(vdata, CONST_STR_LEN("SERVER_NAME"), s, n); 317 318 rc |= cb(vdata, CONST_STR_LEN("REMOTE_ADDR"), 319 CONST_BUF_LEN(con->dst_addr_buf)); 320 321 rc |= cb(vdata, CONST_STR_LEN("REMOTE_PORT"), buf, 322 li_utostrn(buf,sizeof(buf),sock_addr_get_port(&con->dst_addr))); 323 324 for (n = 0; n < r->rqst_headers.used; n++) { 325 data_string *ds = (data_string *)r->rqst_headers.data[n]; 326 if (!buffer_string_is_empty(&ds->value) && !buffer_is_empty(&ds->key)) { 327 /* Security: Do not emit HTTP_PROXY in environment. 328 * Some executables use HTTP_PROXY to configure 329 * outgoing proxy. See also https://httpoxy.org/ */ 330 if (ds->ext == HTTP_HEADER_OTHER 331 && buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Proxy"))) { 332 continue; 333 } 334 http_cgi_encode_varname(tb, CONST_BUF_LEN(&ds->key), 1); 335 rc |= cb(vdata, CONST_BUF_LEN(tb), 336 CONST_BUF_LEN(&ds->value)); 337 } 338 } 339 340 con->srv->request_env(r); 341 342 for (n = 0; n < r->env.used; n++) { 343 data_string *ds = (data_string *)r->env.data[n]; 344 if (!buffer_is_empty(&ds->value) && !buffer_is_empty(&ds->key)) { 345 http_cgi_encode_varname(tb, CONST_BUF_LEN(&ds->key), 0); 346 rc |= cb(vdata, CONST_BUF_LEN(tb), 347 CONST_BUF_LEN(&ds->value)); 348 } 349 } 350 351 return rc; 352 } 353