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 "smbd/service_task.h"
25 #include "smbd/service_stream.h"
26 #include "smbd/service.h"
27 #include "web_server/web_server.h"
28 #include "lib/events/events.h"
29 #include "system/filesys.h"
30 #include "system/network.h"
31 #include "netif/netif.h"
32 #include "lib/tls/tls.h"
34 /* don't allow connections to hang around forever */
35 #define HTTP_TIMEOUT 30
38 destroy a web connection
40 static int websrv_destructor(void *ptr)
42 struct websrv_context *web = talloc_get_type(ptr, struct websrv_context);
43 if (web->output.fd != -1) {
44 close(web->output.fd);
50 called when a connection times out. This prevents a stuck connection
51 from hanging around forever
53 static void websrv_timeout(struct event_context *event_context,
54 struct timed_event *te,
55 struct timeval t, void *private)
57 struct websrv_context *web = talloc_get_type(private, struct websrv_context);
58 struct stream_connection *conn = web->conn;
60 /* TODO: send a message to any running esp context on this connection
62 stream_terminate_connection(conn, "websrv_timeout: timed out");
66 called when a web connection becomes readable
68 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
70 struct websrv_context *web = talloc_get_type(conn->private,
71 struct websrv_context);
78 /* not the most efficient http parser ever, but good enough for us */
79 status = socket_recv(conn->socket, buf, sizeof(buf), &nread);
80 if (NT_STATUS_IS_ERR(status)) goto failed;
81 if (!NT_STATUS_IS_OK(status)) return;
83 status = data_blob_append(web, &web->input.partial, buf, nread);
84 if (!NT_STATUS_IS_OK(status)) goto failed;
86 /* parse any lines that are available */
87 b = web->input.partial;
88 while (!web->input.end_of_headers &&
89 (p=memchr(b.data, '\n', b.length))) {
90 const char *line = (const char *)b.data;
92 if (p != b.data && p[-1] == '\r') {
95 status = http_parse_header(web, line);
96 if (!NT_STATUS_IS_OK(status)) return;
97 b.length -= (p - b.data) + 1;
101 /* keep any remaining bytes in web->input.partial */
105 b = data_blob_talloc(web, b.data, b.length);
106 data_blob_free(&web->input.partial);
107 web->input.partial = b;
109 /* we finish when we have both the full headers (terminated by
110 a blank line) and any post data, as indicated by the
112 if (web->input.end_of_headers &&
113 web->input.partial.length >= web->input.content_length) {
114 if (web->input.partial.length > web->input.content_length) {
115 web->input.partial.data[web->input.content_length] = 0;
117 EVENT_FD_NOT_READABLE(web->conn->event.fde);
119 /* the reference/unlink code here is quite subtle. It
120 is needed because the rendering of the web-pages, and
121 in particular the esp/ejs backend, is semi-async. So
122 we could well end up in the connection timeout code
123 while inside http_process_input(), but we must not
124 destroy the stack variables being used by that
125 rendering process when we handle the timeout. */
126 talloc_reference(web->task, web);
127 http_process_input(web);
128 talloc_unlink(web->task, web);
133 stream_terminate_connection(conn, "websrv_recv: failed");
138 called when a web connection becomes writable
140 static void websrv_send(struct stream_connection *conn, uint16_t flags)
142 struct websrv_context *web = talloc_get_type(conn->private,
143 struct websrv_context);
148 b = web->output.content;
149 b.data += web->output.nsent;
150 b.length -= web->output.nsent;
152 status = socket_send(conn->socket, &b, &nsent);
153 if (NT_STATUS_IS_ERR(status)) {
154 stream_terminate_connection(web->conn, "socket_send: failed");
157 if (!NT_STATUS_IS_OK(status)) {
161 web->output.nsent += nsent;
163 /* possibly read some more raw data from a file */
164 if (web->output.content.length == web->output.nsent &&
165 web->output.fd != -1) {
169 data_blob_free(&web->output.content);
170 web->output.nsent = 0;
172 nread = read(web->output.fd, buf, sizeof(buf));
173 if (nread == -1 && errno == EINTR) {
177 close(web->output.fd);
181 web->output.content = data_blob_talloc(web, buf, nread);
184 if (web->output.content.length == web->output.nsent &&
185 web->output.fd == -1) {
186 stream_terminate_connection(web->conn, "websrv_send: finished sending");
191 establish a new connection to the web server
193 static void websrv_accept(struct stream_connection *conn)
195 struct task_server *task = talloc_get_type(conn->private, struct task_server);
196 struct esp_data *edata = talloc_get_type(task->private, struct esp_data);
197 struct websrv_context *web;
199 web = talloc_zero(conn, struct websrv_context);
200 if (web == NULL) goto failed;
206 talloc_set_destructor(web, websrv_destructor);
208 event_add_timed(conn->event.ctx, web,
209 timeval_current_ofs(HTTP_TIMEOUT, 0),
210 websrv_timeout, web);
212 /* Overwrite the socket with a (possibly) TLS socket */
213 conn->socket = tls_init_server(edata->tls_params, conn->socket,
214 conn->event.fde, "GPHO");
215 if (conn->socket == NULL) goto failed;
224 static const struct stream_server_ops web_stream_ops = {
226 .accept_connection = websrv_accept,
227 .recv_handler = websrv_recv,
228 .send_handler = websrv_send,
232 startup the web server task
234 static void websrv_task_init(struct task_server *task)
237 uint16_t port = lp_web_port();
238 const struct model_ops *model_ops;
240 task_server_set_title(task, "task[websrv]");
242 /* run the web server as a single process */
243 model_ops = process_model_byname("single");
244 if (!model_ops) goto failed;
246 if (lp_interfaces() && lp_bind_interfaces_only()) {
247 int num_interfaces = iface_count();
249 for(i = 0; i < num_interfaces; i++) {
250 const char *address = iface_n_ip(i);
251 status = stream_setup_socket(task->event_ctx, model_ops,
255 if (!NT_STATUS_IS_OK(status)) goto failed;
258 status = stream_setup_socket(task->event_ctx, model_ops,
260 "ipv4", lp_socket_address(),
262 if (!NT_STATUS_IS_OK(status)) goto failed;
265 /* startup the esp processor - unfortunately we can't do this
266 per connection as that wouldn't allow for session variables */
267 status = http_setup_esp(task);
268 if (!NT_STATUS_IS_OK(status)) goto failed;
273 task_server_terminate(task, "websrv_task_init: failed to startup web server task");
278 called on startup of the web server service It's job is to start
279 listening on all configured sockets
281 static NTSTATUS websrv_init(struct event_context *event_context,
282 const struct model_ops *model_ops)
284 return task_server_startup(event_context, model_ops, websrv_task_init);
287 /* called at smbd startup - register ourselves as a server service */
288 NTSTATUS server_service_web_init(void)
290 return register_server_service("web", websrv_init);