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