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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "smbd/service_task.h"
24 #include "smbd/service_stream.h"
25 #include "smbd/service.h"
26 #include "web_server/web_server.h"
27 #include "lib/events/events.h"
28 #include "system/filesys.h"
29 #include "system/network.h"
30 #include "lib/socket/netif.h"
31 #include "lib/tls/tls.h"
33 /* don't allow connections to hang around forever */
34 #define HTTP_TIMEOUT 120
37 destroy a web connection
39 static int websrv_destructor(struct websrv_context *web)
41 if (web->output.fd != -1) {
42 close(web->output.fd);
48 called when a connection times out. This prevents a stuck connection
49 from hanging around forever
51 static void websrv_timeout(struct event_context *event_context,
52 struct timed_event *te,
53 struct timeval t, void *private)
55 struct websrv_context *web = talloc_get_type(private, struct websrv_context);
56 struct stream_connection *conn = web->conn;
58 /* TODO: send a message to any running esp context on this connection
60 stream_terminate_connection(conn, "websrv_timeout: timed out");
64 called when a web connection becomes readable
66 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
68 struct websrv_context *web = talloc_get_type(conn->private,
69 struct websrv_context);
76 /* not the most efficient http parser ever, but good enough for us */
77 status = socket_recv(conn->socket, buf, sizeof(buf), &nread);
78 if (NT_STATUS_IS_ERR(status)) goto failed;
79 if (!NT_STATUS_IS_OK(status)) return;
81 if (!data_blob_append(web, &web->input.partial, buf, nread))
84 /* parse any lines that are available */
85 b = web->input.partial;
86 while (!web->input.end_of_headers &&
87 (p=memchr(b.data, '\n', b.length))) {
88 const char *line = (const char *)b.data;
90 if (p != b.data && p[-1] == '\r') {
93 status = http_parse_header(web, line);
94 if (!NT_STATUS_IS_OK(status)) return;
95 b.length -= (p - b.data) + 1;
99 /* keep any remaining bytes in web->input.partial */
103 b = data_blob_talloc(web, b.data, b.length);
104 data_blob_free(&web->input.partial);
105 web->input.partial = b;
107 /* we finish when we have both the full headers (terminated by
108 a blank line) and any post data, as indicated by the
110 if (web->input.end_of_headers &&
111 web->input.partial.length >= web->input.content_length) {
112 if (web->input.partial.length > web->input.content_length) {
113 web->input.partial.data[web->input.content_length] = 0;
115 EVENT_FD_NOT_READABLE(web->conn->event.fde);
117 /* the reference/unlink code here is quite subtle. It
118 is needed because the rendering of the web-pages, and
119 in particular the esp/ejs backend, is semi-async. So
120 we could well end up in the connection timeout code
121 while inside http_process_input(), but we must not
122 destroy the stack variables being used by that
123 rendering process when we handle the timeout. */
124 if (!talloc_reference(web->task, web)) goto failed;
125 http_process_input(web);
126 talloc_unlink(web->task, web);
131 stream_terminate_connection(conn, "websrv_recv: failed");
136 called when a web connection becomes writable
138 static void websrv_send(struct stream_connection *conn, uint16_t flags)
140 struct websrv_context *web = talloc_get_type(conn->private,
141 struct websrv_context);
146 b = web->output.content;
147 b.data += web->output.nsent;
148 b.length -= web->output.nsent;
150 status = socket_send(conn->socket, &b, &nsent);
151 if (NT_STATUS_IS_ERR(status)) {
152 stream_terminate_connection(web->conn, "socket_send: failed");
155 if (!NT_STATUS_IS_OK(status)) {
159 web->output.nsent += nsent;
161 /* possibly read some more raw data from a file */
162 if (web->output.content.length == web->output.nsent &&
163 web->output.fd != -1) {
167 data_blob_free(&web->output.content);
168 web->output.nsent = 0;
170 nread = read(web->output.fd, buf, sizeof(buf));
171 if (nread == -1 && errno == EINTR) {
175 close(web->output.fd);
179 web->output.content = data_blob_talloc(web, buf, nread);
182 if (web->output.content.length == web->output.nsent &&
183 web->output.fd == -1) {
184 stream_terminate_connection(web->conn, "websrv_send: finished sending");
189 establish a new connection to the web server
191 static void websrv_accept(struct stream_connection *conn)
193 struct task_server *task = talloc_get_type(conn->private, struct task_server);
194 struct esp_data *edata = talloc_get_type(task->private, struct esp_data);
195 struct websrv_context *web;
196 struct socket_context *tls_socket;
198 web = talloc_zero(conn, struct websrv_context);
199 if (web == NULL) goto failed;
205 talloc_set_destructor(web, websrv_destructor);
207 event_add_timed(conn->event.ctx, web,
208 timeval_current_ofs(HTTP_TIMEOUT, 0),
209 websrv_timeout, web);
211 /* Overwrite the socket with a (possibly) TLS socket */
212 tls_socket = tls_init_server(edata->tls_params, conn->socket,
213 conn->event.fde, "GPHO");
214 /* We might not have TLS, or it might not have initilised */
216 talloc_unlink(conn, conn->socket);
217 talloc_steal(conn, tls_socket);
218 conn->socket = tls_socket;
220 DEBUG(3, ("TLS not available for web_server connections\n"));
230 static const struct stream_server_ops web_stream_ops = {
232 .accept_connection = websrv_accept,
233 .recv_handler = websrv_recv,
234 .send_handler = websrv_send,
238 startup the web server task
240 static void websrv_task_init(struct task_server *task)
243 uint16_t port = lp_web_port();
244 const struct model_ops *model_ops;
246 task_server_set_title(task, "task[websrv]");
248 /* run the web server as a single process */
249 model_ops = process_model_byname("single");
250 if (!model_ops) goto failed;
252 if (lp_interfaces() && lp_bind_interfaces_only()) {
253 int num_interfaces = iface_count();
255 for(i = 0; i < num_interfaces; i++) {
256 const char *address = iface_n_ip(i);
257 status = stream_setup_socket(task->event_ctx, model_ops,
261 if (!NT_STATUS_IS_OK(status)) goto failed;
264 status = stream_setup_socket(task->event_ctx, model_ops,
266 "ipv4", lp_socket_address(),
268 if (!NT_STATUS_IS_OK(status)) goto failed;
271 /* startup the esp processor - unfortunately we can't do this
272 per connection as that wouldn't allow for session variables */
273 status = http_setup_esp(task);
274 if (!NT_STATUS_IS_OK(status)) goto failed;
279 task_server_terminate(task, "websrv_task_init: failed to startup web server task");
284 called on startup of the web server service It's job is to start
285 listening on all configured sockets
287 static NTSTATUS websrv_init(struct event_context *event_context,
288 const struct model_ops *model_ops)
290 return task_server_startup(event_context, model_ops, websrv_task_init);
293 /* called at smbd startup - register ourselves as a server service */
294 NTSTATUS server_service_web_init(void)
296 return register_server_service("web", websrv_init);