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 int 95 http_cgi_headers (request_st * const r, http_cgi_opts * const opts, http_cgi_header_append_cb cb, void *vdata) 96 { 97 /* CGI-SPEC 6.1.2, FastCGI spec 6.3 and SCGI spec */ 98 99 /* note: string ptrs passed to cb() func must not be NULL */ 100 101 int rc = 0; 102 buffer * const tb = r->tmp_buf; 103 const char *s; 104 size_t n; 105 char buf[INET6_ADDRSTRLEN + 1]; /*(also larger than LI_ITOSTRING_LENGTH)*/ 106 107 /* (CONTENT_LENGTH must be first for SCGI) */ 108 if (!opts->authorizer) 109 rc |= cb(vdata, CONST_STR_LEN("CONTENT_LENGTH"), 110 buf, li_itostrn(buf,sizeof(buf),r->reqbody_length)); 111 112 if (!buffer_string_is_empty(&r->uri.query)) 113 rc |= cb(vdata, CONST_STR_LEN("QUERY_STRING"), 114 CONST_BUF_LEN(&r->uri.query)); 115 else 116 rc |= cb(vdata, CONST_STR_LEN("QUERY_STRING"), 117 CONST_STR_LEN("")); 118 119 if (!buffer_string_is_empty(opts->strip_request_uri)) { 120 /** 121 * /app1/index/list 122 * 123 * stripping /app1 or /app1/ should lead to 124 * 125 * /index/list 126 * 127 */ 128 size_t len = buffer_string_length(opts->strip_request_uri); 129 if ('/' == opts->strip_request_uri->ptr[len-1]) 130 --len; 131 132 if (buffer_string_length(&r->target_orig) >= len 133 && 0 == memcmp(r->target_orig.ptr, 134 opts->strip_request_uri->ptr, len) 135 && r->target_orig.ptr[len] == '/') { 136 rc |= cb(vdata, CONST_STR_LEN("REQUEST_URI"), 137 r->target_orig.ptr+len, 138 buffer_string_length(&r->target_orig)-len); 139 } 140 else 141 rc |= cb(vdata, CONST_STR_LEN("REQUEST_URI"), 142 CONST_BUF_LEN(&r->target_orig)); 143 } 144 else 145 rc |= cb(vdata, CONST_STR_LEN("REQUEST_URI"), 146 CONST_BUF_LEN(&r->target_orig)); 147 148 if (!buffer_is_equal(&r->target, &r->target_orig)) 149 rc |= cb(vdata, CONST_STR_LEN("REDIRECT_URI"), 150 CONST_BUF_LEN(&r->target)); 151 152 /* set REDIRECT_STATUS for php compiled with --force-redirect 153 * (if REDIRECT_STATUS has not already been set by error handler) */ 154 if (0 == r->error_handler_saved_status) 155 rc |= cb(vdata, CONST_STR_LEN("REDIRECT_STATUS"), 156 CONST_STR_LEN("200")); 157 158 /* 159 * SCRIPT_NAME, PATH_INFO and PATH_TRANSLATED according to 160 * http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html 161 * (6.1.14, 6.1.6, 6.1.7) 162 */ 163 if (!opts->authorizer) { 164 rc |= cb(vdata, CONST_STR_LEN("SCRIPT_NAME"), 165 CONST_BUF_LEN(&r->uri.path)); 166 if (!buffer_string_is_empty(&r->pathinfo)) { 167 rc |= cb(vdata, CONST_STR_LEN("PATH_INFO"), 168 CONST_BUF_LEN(&r->pathinfo)); 169 /* PATH_TRANSLATED is only defined if PATH_INFO is set 170 * Note: not implemented: re-url-encode '?' '=' ';' for 171 * (RFC 3875 4.1.6) */ 172 if (!buffer_string_is_empty(opts->docroot)) 173 buffer_copy_buffer(tb, opts->docroot); 174 else 175 buffer_copy_buffer(tb, &r->physical.basedir); 176 buffer_append_path_len(tb, CONST_BUF_LEN(&r->pathinfo)); 177 rc |= cb(vdata, CONST_STR_LEN("PATH_TRANSLATED"), 178 CONST_BUF_LEN(tb)); 179 } 180 } 181 182 /* 183 * SCRIPT_FILENAME and DOCUMENT_ROOT for php 184 * The PHP manual http://www.php.net/manual/en/reserved.variables.php 185 * treatment of PATH_TRANSLATED is different from the one of CGI specs. 186 * (see php.ini cgi.fix_pathinfo = 1 config parameter) 187 */ 188 189 if (!buffer_string_is_empty(opts->docroot)) { 190 /* alternate docroot, e.g. for remote FastCGI or SCGI server */ 191 buffer_copy_buffer(tb, opts->docroot); 192 buffer_append_path_len(tb, CONST_BUF_LEN(&r->uri.path)); 193 rc |= cb(vdata, CONST_STR_LEN("SCRIPT_FILENAME"), 194 CONST_BUF_LEN(tb)); 195 rc |= cb(vdata, CONST_STR_LEN("DOCUMENT_ROOT"), 196 CONST_BUF_LEN(opts->docroot)); 197 } 198 else { 199 if (opts->break_scriptfilename_for_php) { 200 /* php.ini config cgi.fix_pathinfo = 1 need a broken SCRIPT_FILENAME 201 * to find out what PATH_INFO is itself 202 * 203 * see src/sapi/cgi_main.c, init_request_info() 204 */ 205 buffer_copy_buffer(tb, &r->physical.path); 206 buffer_append_path_len(tb, CONST_BUF_LEN(&r->pathinfo)); 207 rc |= cb(vdata, CONST_STR_LEN("SCRIPT_FILENAME"), 208 CONST_BUF_LEN(tb)); 209 } 210 else 211 rc |= cb(vdata, CONST_STR_LEN("SCRIPT_FILENAME"), 212 CONST_BUF_LEN(&r->physical.path)); 213 rc |= cb(vdata, CONST_STR_LEN("DOCUMENT_ROOT"), 214 CONST_BUF_LEN(&r->physical.basedir)); 215 } 216 217 s = get_http_method_name(r->http_method); 218 force_assert(s); 219 rc |= cb(vdata, CONST_STR_LEN("REQUEST_METHOD"), s, strlen(s)); 220 221 s = get_http_version_name(r->http_version); 222 force_assert(s); 223 rc |= cb(vdata, CONST_STR_LEN("SERVER_PROTOCOL"), s, strlen(s)); 224 225 rc |= cb(vdata, CONST_STR_LEN("SERVER_SOFTWARE"), 226 CONST_BUF_LEN(r->conf.server_tag)); 227 228 rc |= cb(vdata, CONST_STR_LEN("GATEWAY_INTERFACE"), 229 CONST_STR_LEN("CGI/1.1")); 230 231 rc |= cb(vdata, CONST_STR_LEN("REQUEST_SCHEME"), 232 CONST_BUF_LEN(&r->uri.scheme)); 233 234 if (buffer_is_equal_string(&r->uri.scheme, CONST_STR_LEN("https"))) 235 rc |= cb(vdata, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on")); 236 237 const connection * const con = r->con; 238 const server_socket * const srv_sock = con->srv_socket; 239 const size_t tlen = buffer_string_length(srv_sock->srv_token); 240 n = srv_sock->srv_token_colon; 241 if (n < tlen) { /*(n != tlen)*/ 242 s = srv_sock->srv_token->ptr+n+1; 243 n = tlen - (n+1); 244 } 245 else { 246 s = "0"; 247 n = 1; 248 } 249 rc |= cb(vdata, CONST_STR_LEN("SERVER_PORT"), s, n); 250 251 n = 0; 252 switch (sock_addr_get_family(&srv_sock->addr)) { 253 case AF_INET: 254 case AF_INET6: 255 if (sock_addr_is_addr_wildcard(&srv_sock->addr)) { 256 sock_addr addrbuf; 257 socklen_t addrlen = sizeof(addrbuf); 258 if (0 == getsockname(con->fd,(struct sockaddr *)&addrbuf,&addrlen)){ 259 /* future: might add a one- or two- element cache 260 * or use sock_addr_cache_inet_ntop_copy_buffer() into tb */ 261 s = sock_addr_inet_ntop(&addrbuf, buf, sizeof(buf)); 262 if (s) 263 n = strlen(s); 264 else 265 s = ""; 266 } 267 else 268 s = ""; 269 } 270 else { 271 s = srv_sock->srv_token->ptr; 272 n = srv_sock->srv_token_colon; 273 } 274 break; 275 default: 276 s = ""; 277 break; 278 } 279 rc |= cb(vdata, CONST_STR_LEN("SERVER_ADDR"), s, n); 280 281 if (!buffer_string_is_empty(r->server_name)) { 282 n = buffer_string_length(r->server_name); 283 s = r->server_name->ptr; 284 if (s[0] == '[') { 285 const char *colon = strstr(s, "]:"); 286 if (colon) n = (colon + 1) - s; 287 } 288 else { 289 const char *colon = strchr(s, ':'); 290 if (colon) n = colon - s; 291 } 292 } /* else set to be same as SERVER_ADDR (above) */ 293 rc |= cb(vdata, CONST_STR_LEN("SERVER_NAME"), s, n); 294 295 rc |= cb(vdata, CONST_STR_LEN("REMOTE_ADDR"), 296 CONST_BUF_LEN(con->dst_addr_buf)); 297 298 rc |= cb(vdata, CONST_STR_LEN("REMOTE_PORT"), buf, 299 li_utostrn(buf,sizeof(buf),sock_addr_get_port(&con->dst_addr))); 300 301 for (n = 0; n < r->rqst_headers.used; n++) { 302 data_string *ds = (data_string *)r->rqst_headers.data[n]; 303 if (!buffer_string_is_empty(&ds->value) && !buffer_is_empty(&ds->key)) { 304 /* Security: Do not emit HTTP_PROXY in environment. 305 * Some executables use HTTP_PROXY to configure 306 * outgoing proxy. See also https://httpoxy.org/ */ 307 if (ds->ext == HTTP_HEADER_OTHER 308 && buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Proxy"))) { 309 continue; 310 } 311 buffer_copy_string_encoded_cgi_varnames(tb, 312 CONST_BUF_LEN(&ds->key), 1); 313 rc |= cb(vdata, CONST_BUF_LEN(tb), 314 CONST_BUF_LEN(&ds->value)); 315 } 316 } 317 318 con->srv->request_env(r); 319 320 for (n = 0; n < r->env.used; n++) { 321 data_string *ds = (data_string *)r->env.data[n]; 322 if (!buffer_is_empty(&ds->value) && !buffer_is_empty(&ds->key)) { 323 buffer_copy_string_encoded_cgi_varnames(tb, 324 CONST_BUF_LEN(&ds->key), 0); 325 rc |= cb(vdata, CONST_BUF_LEN(tb), 326 CONST_BUF_LEN(&ds->value)); 327 } 328 } 329 330 return rc; 331 } 332