r7744: converted the web server to use the lib/tls/ generic tls code
[bbaumbach/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 "web_server/web_server.h"
27 #include "lib/events/events.h"
28 #include "system/filesys.h"
29 #include "lib/tls/tls.h"
30
31 /* don't allow connections to hang around forever */
32 #define HTTP_TIMEOUT 30
33
34 /*
35   destroy a web connection
36 */
37 static int websrv_destructor(void *ptr)
38 {
39         struct websrv_context *web = talloc_get_type(ptr, struct websrv_context);
40         if (web->output.fd != -1) {
41                 close(web->output.fd);
42         }
43         return 0;
44 }
45
46 /*
47   called when a connection times out. This prevents a stuck connection
48   from hanging around forever
49 */
50 static void websrv_timeout(struct event_context *event_context, 
51                            struct timed_event *te, 
52                            struct timeval t, void *private)
53 {
54         struct websrv_context *web = talloc_get_type(private, struct websrv_context);
55         stream_terminate_connection(web->conn, "websrv_context: timeout");
56 }
57
58 /*
59   called when a web connection becomes readable
60 */
61 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
62 {
63         struct websrv_context *web = talloc_get_type(conn->private, 
64                                                      struct websrv_context);
65         NTSTATUS status;
66         uint8_t buf[1024];
67         size_t nread;
68         uint8_t *p;
69         DATA_BLOB b;
70
71         /* not the most efficient http parser ever, but good enough for us */
72         status = tls_socket_recv(web->tls, buf, sizeof(buf), &nread);
73         if (NT_STATUS_IS_ERR(status)) goto failed;
74         if (!NT_STATUS_IS_OK(status)) return;
75
76         status = data_blob_append(web, &web->input.partial, buf, nread);
77         if (!NT_STATUS_IS_OK(status)) goto failed;
78
79         /* parse any lines that are available */
80         b = web->input.partial;
81         while (!web->input.end_of_headers &&
82                (p=memchr(b.data, '\n', b.length))) {
83                 const char *line = b.data;
84                 *p = 0;
85                 if (p != b.data && p[-1] == '\r') {
86                         p[-1] = 0;
87                 }
88                 status = http_parse_header(web, line);
89                 if (!NT_STATUS_IS_OK(status)) return;
90                 b.length -= (p - b.data) + 1;
91                 b.data = p+1;
92         }
93
94         /* keep any remaining bytes in web->input.partial */
95         if (b.length == 0) {
96                 b.data = NULL;
97         }
98         b = data_blob_talloc(web, b.data, b.length);
99         data_blob_free(&web->input.partial);
100         web->input.partial = b;
101
102         /* we finish when we have both the full headers (terminated by
103            a blank line) and any post data, as indicated by the
104            content_length */
105         if (web->input.end_of_headers &&
106             web->input.partial.length == web->input.content_length) {
107                 EVENT_FD_NOT_READABLE(web->conn->event.fde);
108                 http_process_input(web);
109         }
110         return;
111
112 failed:
113         stream_terminate_connection(conn, "websrv_recv: failed\n");
114 }
115
116
117 /*
118   called when a web connection becomes writable
119 */
120 static void websrv_send(struct stream_connection *conn, uint16_t flags)
121 {
122         struct websrv_context *web = talloc_get_type(conn->private, 
123                                                      struct websrv_context);
124         NTSTATUS status;
125         size_t nsent;
126         DATA_BLOB b;
127
128         b = web->output.content;
129         b.data += web->output.nsent;
130         b.length -= web->output.nsent;
131
132         status = tls_socket_send(web->tls, &b, &nsent);
133         if (NT_STATUS_IS_ERR(status)) {
134                 stream_terminate_connection(web->conn, "socket_send: failed");
135                 return;
136         }
137         if (!NT_STATUS_IS_OK(status)) {
138                 return;
139         }
140
141         web->output.nsent += nsent;
142
143         /* possibly read some more raw data from a file */
144         if (web->output.content.length == web->output.nsent && 
145             web->output.fd != -1) {
146                 uint8_t buf[2048];
147                 ssize_t nread;
148
149                 data_blob_free(&web->output.content);
150                 web->output.nsent = 0;
151
152                 nread = read(web->output.fd, buf, sizeof(buf));
153                 if (nread == 0) {
154                         close(web->output.fd);
155                         web->output.fd = -1;
156                 }
157                 if (nread == -1 && errno == EINTR) {
158                         return;
159                 }
160                 web->output.content = data_blob_talloc(web, buf, nread);
161         }
162
163         if (web->output.content.length == web->output.nsent && 
164             web->output.fd == -1) {
165                 talloc_free(web->tls);
166                 web->tls = NULL;
167                 stream_terminate_connection(web->conn, NULL);
168         }
169 }
170
171 /*
172   establish a new connection to the web server
173 */
174 static void websrv_accept(struct stream_connection *conn)
175 {
176         struct task_server *task = talloc_get_type(conn->private, struct task_server);
177         struct esp_data *edata = talloc_get_type(task->private, struct esp_data);
178         struct websrv_context *web;
179
180         web = talloc_zero(conn, struct websrv_context);
181         if (web == NULL) goto failed;
182
183         web->task = task;
184         web->conn = conn;
185         conn->private = web;
186         web->output.fd = -1;
187         talloc_set_destructor(web, websrv_destructor);
188
189         event_add_timed(conn->event.ctx, web, 
190                         timeval_current_ofs(HTTP_TIMEOUT, 0),
191                         websrv_timeout, web);
192
193         web->tls = tls_init_server(edata->tls_params, conn->socket, 
194                                    conn->event.fde, "GPHO");
195         if (web->tls == NULL) goto failed;
196
197         return;
198
199 failed:
200         talloc_free(conn);
201 }
202
203
204 static const struct stream_server_ops web_stream_ops = {
205         .name                   = "web",
206         .accept_connection      = websrv_accept,
207         .recv_handler           = websrv_recv,
208         .send_handler           = websrv_send,
209 };
210
211 /*
212   startup the web server task
213 */
214 static void websrv_task_init(struct task_server *task)
215 {
216         NTSTATUS status;
217         uint16_t port = lp_web_port();
218         const struct model_ops *model_ops;
219
220         /* run the web server as a single process */
221         model_ops = process_model_byname("single");
222         if (!model_ops) goto failed;
223
224         if (lp_interfaces() && lp_bind_interfaces_only()) {
225                 int num_interfaces = iface_count();
226                 int i;
227                 for(i = 0; i < num_interfaces; i++) {
228                         const char *address = iface_n_ip(i);
229                         status = stream_setup_socket(task->event_ctx, model_ops, 
230                                                      &web_stream_ops, 
231                                                      "ipv4", address, 
232                                                      &port, task);
233                         if (!NT_STATUS_IS_OK(status)) goto failed;
234                 }
235         } else {
236                 status = stream_setup_socket(task->event_ctx, model_ops, 
237                                              &web_stream_ops, 
238                                              "ipv4", lp_socket_address(), 
239                                              &port, task);
240                 if (!NT_STATUS_IS_OK(status)) goto failed;
241         }
242
243         /* startup the esp processor - unfortunately we can't do this
244            per connection as that wouldn't allow for session variables */
245         status = http_setup_esp(task);
246         if (!NT_STATUS_IS_OK(status)) goto failed;
247
248         return;
249
250 failed:
251         task_terminate(task, "Failed to startup web server task");
252 }
253
254
255 /*
256   called on startup of the web server service It's job is to start
257   listening on all configured sockets
258 */
259 static NTSTATUS websrv_init(struct event_context *event_context, 
260                             const struct model_ops *model_ops)
261 {       
262         return task_server_startup(event_context, model_ops, websrv_task_init);
263 }
264
265 /* called at smbd startup - register ourselves as a server service */
266 NTSTATUS server_service_web_init(void)
267 {
268         return register_server_service("web", websrv_init);
269 }