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