2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "web_server/web_server.h"
25 #include "smbd/service_stream.h"
26 #include "lib/events/events.h"
27 #include "system/filesys.h"
28 #include "system/iconv.h"
29 #include "system/time.h"
30 #include "web_server/esp/esp.h"
32 /* state of the esp subsystem */
34 struct websrv_context *web;
35 struct MprVar variables[ESP_OBJ_MAX];
36 struct EspRequest *req;
39 /* destroy a esp session */
40 static int esp_destructor(void *ptr)
42 struct esp_state *esp = talloc_get_type(ptr, struct esp_state);
44 espDestroyRequest(esp->req);
52 output the http headers
54 static void http_output_headers(struct websrv_context *web)
59 const char *response_string = "Unknown Code";
62 const char *response_string;
68 { 304, "Not Modified" },
69 { 400, "Bad request" },
70 { 401, "Unauthorized" },
73 { 500, "Internal Server Error" },
74 { 501, "Not implemented" }
76 for (i=0;i<ARRAY_SIZE(codes);i++) {
77 if (codes[i].code == web->output.response_code) {
78 response_string = codes[i].response_string;
82 if (web->output.headers == NULL) return;
83 s = talloc_asprintf(web, "HTTP/1.0 %u %s\r\n",
84 web->output.response_code, response_string);
85 if (s == NULL) return;
86 for (i=0;web->output.headers[i];i++) {
87 s = talloc_asprintf_append(s, "%s\r\n", web->output.headers[i]);
89 s = talloc_asprintf_append(s, "\r\n");
90 if (s == NULL) return;
92 b = web->output.content;
93 web->output.content.data = s;
94 web->output.content.length = strlen(s);
95 data_blob_append(web, &web->output.content, b.data, b.length);
100 called when esp wants to output something
102 static int http_writeBlock(EspHandle handle, char *buf, int size)
104 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
106 status = data_blob_append(web, &web->output.content, buf, size);
107 if (!NT_STATUS_IS_OK(status)) return -1;
115 static void http_setHeader(EspHandle handle, const char *value, bool allowMultiple)
117 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
118 char *p = strchr(value, ':');
120 if (p && !allowMultiple && web->output.headers) {
122 for (i=0;web->output.headers[i];i++) {
123 if (strncmp(web->output.headers[i], value, (p+1)-value) == 0) {
124 web->output.headers[i] = talloc_strdup(web, value);
130 web->output.headers = str_list_add(web->output.headers, value);
134 set a http response code
136 static void http_setResponseCode(EspHandle handle, int code)
138 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
139 web->output.response_code = code;
143 redirect to another web page
145 static void http_redirect(EspHandle handle, int code, char *url)
147 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
148 const char *host = web->input.host;
150 /* form the full url, unless it already looks like a url */
151 if (strchr(url, ':') == NULL) {
153 host = talloc_asprintf(web, "%s:%u",
154 socket_get_my_addr(web->conn->socket, web),
155 socket_get_my_port(web->conn->socket));
157 if (host == NULL) goto internal_error;
159 char *p = strrchr(web->input.url, '/');
160 if (p == web->input.url) {
161 url = talloc_asprintf(web, "http://%s/%s", host, url);
163 int dirlen = p - web->input.url;
164 url = talloc_asprintf(web, "http://%s%*.*s/%s",
166 dirlen, dirlen, web->input.url,
169 if (url == NULL) goto internal_error;
173 http_setHeader(handle, talloc_asprintf(web, "Location: %s", url), 0);
175 /* make sure we give a valid redirect code */
176 if (code >= 300 && code < 400) {
177 http_setResponseCode(handle, code);
179 http_setResponseCode(handle, 302);
184 http_error(web, 500, "Internal server error");
188 /* callbacks for esp processing */
189 static const struct Esp esp_control = {
190 .maxScriptSize = 60000,
191 .writeBlock = http_writeBlock,
192 .setHeader = http_setHeader,
193 .redirect = http_redirect,
194 .setResponseCode = http_setResponseCode
199 setup for a raw http level error
201 void http_error(struct websrv_context *web, int code, const char *info)
204 s = talloc_asprintf(web,"<HTML><HEAD><TITLE>Error %u</TITLE></HEAD><BODY><H1>Error %u</H1>%s<p></BODY></HTML>\r\n\r\n",
207 stream_terminate_connection(web->conn, "http_error: out of memory");
210 http_writeBlock(web, s, strlen(s));
211 http_setResponseCode(web, code);
212 http_output_headers(web);
213 EVENT_FD_NOT_READABLE(web->conn->event.fde);
214 EVENT_FD_WRITEABLE(web->conn->event.fde);
218 map a unix error code to a http error
220 void http_error_unix(struct websrv_context *web, const char *info)
232 http_error(web, code, info);
236 return the local path for a URL
238 static const char *http_local_path(struct websrv_context *web, const char *url)
243 /* check that the url is OK */
244 if (url[0] != '/') return NULL;
246 for (i=0;url[i];i++) {
247 if ((!isalnum(url[i]) && !strchr("./", url[i])) ||
248 (url[i] == '.' && strchr("/.", url[i+1]))) {
253 path = talloc_asprintf(web, "%s/%s", lp_swat_directory(), url+1);
254 if (path == NULL) return NULL;
256 if (directory_exist(path)) {
257 path = talloc_asprintf_append(path, "/index.html");
264 a simple file request
266 static void http_simple_request(struct websrv_context *web)
268 const char *url = web->input.url;
272 path = http_local_path(web, url);
273 if (path == NULL) goto invalid;
276 web->output.fd = open(path, O_RDONLY);
277 if (web->output.fd == -1) {
278 http_error_unix(web, url);
282 if (fstat(web->output.fd, &st) != 0 || !S_ISREG(st.st_mode)) {
283 close(web->output.fd);
287 http_output_headers(web);
288 EVENT_FD_WRITEABLE(web->conn->event.fde);
292 http_error(web, 400, "Malformed URL");
296 setup the standard ESP arrays
298 static void http_setup_arrays(struct esp_state *esp)
300 struct websrv_context *web = esp->web;
301 struct EspRequest *req = esp->req;
304 espSetStringVar(req, ESP_REQUEST_OBJ, "CONTENT_LENGTH",
305 talloc_asprintf(esp, "%u", web->input.content_length));
306 if (web->input.query_string) {
307 espSetStringVar(req, ESP_REQUEST_OBJ, "QUERY_STRING",
308 web->input.query_string);
310 espSetStringVar(req, ESP_REQUEST_OBJ, "REQUEST_METHOD",
311 web->input.post_request?"POST":"GET");
312 espSetStringVar(req, ESP_REQUEST_OBJ, "REQUEST_URI", web->input.url);
313 p = strrchr(web->input.url, '/');
314 espSetStringVar(req, ESP_REQUEST_OBJ, "SCRIPT_NAME", p+1);
316 if (web->input.referer) {
317 espSetStringVar(req, ESP_HEADERS_OBJ, "HTT_REFERER", web->input.referer);
319 if (web->input.user_agent) {
320 espSetStringVar(req, ESP_HEADERS_OBJ, "USER_AGENT", web->input.user_agent);
323 espSetStringVar(req, ESP_SERVER_OBJ, "SERVER_ADDR",
324 socket_get_my_addr(web->conn->socket, esp));
325 espSetStringVar(req, ESP_SERVER_OBJ, "SERVER_PORT",
326 talloc_asprintf(esp, "%u", socket_get_my_port(web->conn->socket)));
327 espSetStringVar(req, ESP_SERVER_OBJ, "SERVER_PROTOCOL", "http");
335 process a esp request
337 static void esp_request(struct esp_state *esp)
339 struct websrv_context *web = esp->web;
340 const char *url = web->input.url;
347 http_setup_arrays(esp);
349 path = http_local_path(web, url);
350 if (path == NULL) goto invalid;
352 espSetStringVar(esp->req, ESP_REQUEST_OBJ, "SCRIPT_FILENAME", path);
355 fd = open(path, O_RDONLY);
357 http_error_unix(web, url);
361 if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) {
366 buf = talloc_size(esp, st.st_size+1);
367 if (buf == NULL) goto invalid;
369 if (read(fd, buf, st.st_size) != st.st_size) {
375 res = espProcessRequest(esp->req, path, buf, &emsg);
376 if (res != 0 && emsg) {
377 http_writeBlock(esp, emsg, strlen(emsg));
379 http_output_headers(web);
380 EVENT_FD_WRITEABLE(web->conn->event.fde);
384 http_error(web, 400, "Malformed URL");
389 handling of + and % escapes in http variables
391 static const char *http_unescape(TALLOC_CTX *mem_ctx, const char *p)
393 char *s0 = talloc_strdup(mem_ctx, p);
395 if (s == NULL) return NULL;
399 if (*s == '+') *s = ' ';
400 if (*s == '%' && sscanf(s+1, "%02x", &v) == 1) {
402 memmove(s+1, s+3, strlen(s+3)+1);
411 set a form or GET variable
413 static void esp_putvar(struct esp_state *esp, const char *var, const char *value)
415 espSetStringVar(esp->req, ESP_FORM_OBJ,
416 http_unescape(esp, var),
417 http_unescape(esp, value));
422 parse the variables in a POST style request
424 static NTSTATUS http_parse_post(struct esp_state *esp)
426 DATA_BLOB b = esp->web->input.partial;
432 p = memchr(b.data, '&', b.length);
436 len = p - (char *)b.data;
438 line = talloc_strndup(esp, b.data, len);
439 NT_STATUS_HAVE_NO_MEMORY(line);
441 p = strchr(line,'=');
444 esp_putvar(esp, line, p+1);
459 parse the variables in a GET style request
461 static NTSTATUS http_parse_get(struct esp_state *esp)
463 struct websrv_context *web = esp->web;
467 p = strchr(web->input.url, '?');
468 web->input.query_string = p+1;
471 s = talloc_strdup(esp, esp->web->input.query_string);
472 NT_STATUS_HAVE_NO_MEMORY(s);
474 for (tok=strtok_r(s,"&;", &pp);tok;tok=strtok_r(NULL,"&;", &pp)) {
478 esp_putvar(esp, tok, p+1);
485 setup some standard variables
487 static void http_setup_vars(struct esp_state *esp)
491 for (i = 0; i < ESP_OBJ_MAX; i++) {
492 esp->variables[i] = mprCreateUndefinedVar();
494 esp->variables[ESP_HEADERS_OBJ] = mprCreateObjVar("headers", ESP_HASH_SIZE);
495 esp->variables[ESP_FORM_OBJ] = mprCreateObjVar("form", ESP_HASH_SIZE);
496 esp->variables[ESP_APPLICATION_OBJ] = mprCreateObjVar("application", ESP_HASH_SIZE);
497 esp->variables[ESP_COOKIES_OBJ] = mprCreateObjVar("cookies", ESP_HASH_SIZE);
498 esp->variables[ESP_FILES_OBJ] = mprCreateObjVar("files", ESP_HASH_SIZE);
499 esp->variables[ESP_REQUEST_OBJ] = mprCreateObjVar("request", ESP_HASH_SIZE);
500 esp->variables[ESP_SERVER_OBJ] = mprCreateObjVar("server", ESP_HASH_SIZE);
501 esp->variables[ESP_SESSION_OBJ] = mprCreateObjVar("session", ESP_HASH_SIZE);
505 process a complete http request
507 void http_process_input(struct websrv_context *web)
510 struct esp_state *esp;
513 const char *file_type = NULL;
515 const char *extension;
516 const char *mime_type;
518 {"gif", "image/gif"},
519 {"png", "image/png"},
520 {"jpg", "image/jpeg"},
521 {"txt", "text/plain"}
524 esp = talloc_zero(web, struct esp_state);
525 if (esp == NULL) goto internal_error;
531 talloc_set_destructor(esp, esp_destructor);
533 if (espOpen(&esp_control) != 0) goto internal_error;
535 http_setup_vars(esp);
537 esp->req = espCreateRequest(web, web->input.url, esp->variables);
538 if (esp->req == NULL) goto internal_error;
540 if (web->input.url == NULL) {
541 http_error(web, 400, "You must specify a GET or POST request");
545 if (web->input.post_request) {
546 status = http_parse_post(esp);
547 if (!NT_STATUS_IS_OK(status)) {
548 http_error(web, 400, "Malformed POST data");
551 } else if (strchr(web->input.url, '?')) {
552 status = http_parse_get(esp);
553 if (!NT_STATUS_IS_OK(status)) {
554 http_error(web, 400, "Malformed GET data");
559 /* process all html files as ESP */
560 p = strrchr(web->input.url, '.');
561 for (i=0;p && i<ARRAY_SIZE(mime_types);i++) {
562 if (strcmp(mime_types[i].extension, p+1) == 0) {
563 file_type = mime_types[i].mime_type;
566 if (file_type == NULL) {
567 file_type = "text/html";
570 /* setup basic headers */
571 http_setResponseCode(web, 200);
572 http_setHeader(web, talloc_asprintf(esp, "Date: %s",
573 http_timestring(esp, time(NULL))), 0);
574 http_setHeader(web, "Server: Samba", 0);
575 http_setHeader(web, "Connection: close", 0);
576 http_setHeader(web, talloc_asprintf(esp, "Content-Type: %s", file_type), 0);
578 if (strcmp(file_type, "text/html") == 0) {
581 http_simple_request(web);
588 http_error(web, 500, "Internal server error");
593 parse one line of header input
595 NTSTATUS http_parse_header(struct websrv_context *web, const char *line)
598 web->input.end_of_headers = True;
599 } else if (strncasecmp(line,"GET ", 4)==0) {
600 web->input.url = talloc_strndup(web, &line[4], strcspn(&line[4], " \t"));
601 } else if (strncasecmp(line,"POST ", 5)==0) {
602 web->input.post_request = True;
603 web->input.url = talloc_strndup(web, &line[5], strcspn(&line[5], " \t"));
604 } else if (strchr(line, ':') == NULL) {
605 http_error(web, 400, "This server only accepts GET and POST requests");
606 return NT_STATUS_INVALID_PARAMETER;
607 } else if (strncasecmp(line,"Content-Length: ", 16)==0) {
608 web->input.content_length = strtoul(&line[16], NULL, 10);
610 #define PULL_HEADER(v, s) do { \
611 if (strncmp(line, s, strlen(s)) == 0) { \
612 web->input.v = talloc_strdup(web, &line[strlen(s)]); \
613 return NT_STATUS_OK; \
616 PULL_HEADER(content_type, "Content-Type: ");
617 PULL_HEADER(user_agent, "User-Agent: ");
618 PULL_HEADER(referer, "Referer: ");
619 PULL_HEADER(host, "Host: ");
620 PULL_HEADER(accept_encoding, "Accept-Encoding: ");
623 /* ignore all other headers for now */