93d59cc47a19dff9de830fd427f1d1e1fa51c699
[kai/samba-autobuild/.git] / source4 / web_server / web_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    web server startup
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
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"
33
34 /* don't allow connections to hang around forever */
35 #define HTTP_TIMEOUT 30
36
37 /*
38   destroy a web connection
39 */
40 static int websrv_destructor(void *ptr)
41 {
42         struct websrv_context *web = talloc_get_type(ptr, struct websrv_context);
43         if (web->output.fd != -1) {
44                 close(web->output.fd);
45         }
46         return 0;
47 }
48
49 /*
50   called when a connection times out. This prevents a stuck connection
51   from hanging around forever
52 */
53 static void websrv_timeout(struct event_context *event_context, 
54                            struct timed_event *te, 
55                            struct timeval t, void *private)
56 {
57         struct websrv_context *web = talloc_get_type(private, struct websrv_context);
58         struct stream_connection *conn = web->conn;
59         web->conn = NULL;
60         /* TODO: send a message to any running esp context on this connection
61            to stop running */
62         stream_terminate_connection(conn, "websrv_timeout: timed out"); 
63 }
64
65 /*
66   called when a web connection becomes readable
67 */
68 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
69 {
70         struct websrv_context *web = talloc_get_type(conn->private, 
71                                                      struct websrv_context);
72         NTSTATUS status;
73         uint8_t buf[1024];
74         size_t nread;
75         uint8_t *p;
76         DATA_BLOB b;
77
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;
82
83         status = data_blob_append(web, &web->input.partial, buf, nread);
84         if (!NT_STATUS_IS_OK(status)) goto failed;
85
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;
91                 *p = 0;
92                 if (p != b.data && p[-1] == '\r') {
93                         p[-1] = 0;
94                 }
95                 status = http_parse_header(web, line);
96                 if (!NT_STATUS_IS_OK(status)) return;
97                 b.length -= (p - b.data) + 1;
98                 b.data = p+1;
99         }
100
101         /* keep any remaining bytes in web->input.partial */
102         if (b.length == 0) {
103                 b.data = NULL;
104         }
105         b = data_blob_talloc(web, b.data, b.length);
106         data_blob_free(&web->input.partial);
107         web->input.partial = b;
108
109         /* we finish when we have both the full headers (terminated by
110            a blank line) and any post data, as indicated by the
111            content_length */
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;
116                 }
117                 EVENT_FD_NOT_READABLE(web->conn->event.fde);
118
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);
129         }
130         return;
131
132 failed:
133         stream_terminate_connection(conn, "websrv_recv: failed");
134 }
135
136
137 /*
138   called when a web connection becomes writable
139 */
140 static void websrv_send(struct stream_connection *conn, uint16_t flags)
141 {
142         struct websrv_context *web = talloc_get_type(conn->private, 
143                                                      struct websrv_context);
144         NTSTATUS status;
145         size_t nsent;
146         DATA_BLOB b;
147
148         b = web->output.content;
149         b.data += web->output.nsent;
150         b.length -= web->output.nsent;
151
152         status = socket_send(conn->socket, &b, &nsent);
153         if (NT_STATUS_IS_ERR(status)) {
154                 stream_terminate_connection(web->conn, "socket_send: failed");
155                 return;
156         }
157         if (!NT_STATUS_IS_OK(status)) {
158                 return;
159         }
160
161         web->output.nsent += nsent;
162
163         /* possibly read some more raw data from a file */
164         if (web->output.content.length == web->output.nsent && 
165             web->output.fd != -1) {
166                 uint8_t buf[2048];
167                 ssize_t nread;
168
169                 data_blob_free(&web->output.content);
170                 web->output.nsent = 0;
171
172                 nread = read(web->output.fd, buf, sizeof(buf));
173                 if (nread == -1 && errno == EINTR) {
174                         return;
175                 }
176                 if (nread <= 0) {
177                         close(web->output.fd);
178                         web->output.fd = -1;
179                         nread = 0;
180                 }
181                 web->output.content = data_blob_talloc(web, buf, nread);
182         }
183
184         if (web->output.content.length == web->output.nsent && 
185             web->output.fd == -1) {
186                 stream_terminate_connection(web->conn, "websrv_send: finished sending");
187         }
188 }
189
190 /*
191   establish a new connection to the web server
192 */
193 static void websrv_accept(struct stream_connection *conn)
194 {
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;
198
199         web = talloc_zero(conn, struct websrv_context);
200         if (web == NULL) goto failed;
201
202         web->task = task;
203         web->conn = conn;
204         conn->private = web;
205         web->output.fd = -1;
206         talloc_set_destructor(web, websrv_destructor);
207
208         event_add_timed(conn->event.ctx, web, 
209                         timeval_current_ofs(HTTP_TIMEOUT, 0),
210                         websrv_timeout, web);
211
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;
216
217         return;
218
219 failed:
220         talloc_free(conn);
221 }
222
223
224 static const struct stream_server_ops web_stream_ops = {
225         .name                   = "web",
226         .accept_connection      = websrv_accept,
227         .recv_handler           = websrv_recv,
228         .send_handler           = websrv_send,
229 };
230
231 /*
232   startup the web server task
233 */
234 static void websrv_task_init(struct task_server *task)
235 {
236         NTSTATUS status;
237         uint16_t port = lp_web_port();
238         const struct model_ops *model_ops;
239
240         task_server_set_title(task, "task[websrv]");
241
242         /* run the web server as a single process */
243         model_ops = process_model_byname("single");
244         if (!model_ops) goto failed;
245
246         if (lp_interfaces() && lp_bind_interfaces_only()) {
247                 int num_interfaces = iface_count();
248                 int i;
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, 
252                                                      &web_stream_ops, 
253                                                      "ipv4", address, 
254                                                      &port, task);
255                         if (!NT_STATUS_IS_OK(status)) goto failed;
256                 }
257         } else {
258                 status = stream_setup_socket(task->event_ctx, model_ops, 
259                                              &web_stream_ops, 
260                                              "ipv4", lp_socket_address(), 
261                                              &port, task);
262                 if (!NT_STATUS_IS_OK(status)) goto failed;
263         }
264
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;
269
270         return;
271
272 failed:
273         task_server_terminate(task, "websrv_task_init: failed to startup web server task");
274 }
275
276
277 /*
278   called on startup of the web server service It's job is to start
279   listening on all configured sockets
280 */
281 static NTSTATUS websrv_init(struct event_context *event_context, 
282                             const struct model_ops *model_ops)
283 {       
284         return task_server_startup(event_context, model_ops, websrv_task_init);
285 }
286
287 /* called at smbd startup - register ourselves as a server service */
288 NTSTATUS server_service_web_init(void)
289 {
290         return register_server_service("web", websrv_init);
291 }