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 "lib/socket/netif.h"
32 #include "lib/tls/tls.h"
34 /* don't allow connections to hang around forever */
35 #define HTTP_TIMEOUT 120
38 destroy a web connection
40 static int websrv_destructor(struct websrv_context
*web
)
42 if (web
->output
.fd
!= -1) {
43 close(web
->output
.fd
);
49 called when a connection times out. This prevents a stuck connection
50 from hanging around forever
52 static void websrv_timeout(struct event_context
*event_context
,
53 struct timed_event
*te
,
54 struct timeval t
, void *private)
56 struct websrv_context
*web
= talloc_get_type(private, struct websrv_context
);
57 struct stream_connection
*conn
= web
->conn
;
59 /* TODO: send a message to any running esp context on this connection
61 stream_terminate_connection(conn
, "websrv_timeout: timed out");
65 called when a web connection becomes readable
67 static void websrv_recv(struct stream_connection
*conn
, uint16_t flags
)
69 struct websrv_context
*web
= talloc_get_type(conn
->private,
70 struct websrv_context
);
77 /* not the most efficient http parser ever, but good enough for us */
78 status
= socket_recv(conn
->socket
, buf
, sizeof(buf
), &nread
);
79 if (NT_STATUS_IS_ERR(status
)) goto failed
;
80 if (!NT_STATUS_IS_OK(status
)) return;
82 status
= data_blob_append(web
, &web
->input
.partial
, buf
, nread
);
83 if (!NT_STATUS_IS_OK(status
)) goto failed
;
85 /* parse any lines that are available */
86 b
= web
->input
.partial
;
87 while (!web
->input
.end_of_headers
&&
88 (p
=memchr(b
.data
, '\n', b
.length
))) {
89 const char *line
= (const char *)b
.data
;
91 if (p
!= b
.data
&& p
[-1] == '\r') {
94 status
= http_parse_header(web
, line
);
95 if (!NT_STATUS_IS_OK(status
)) return;
96 b
.length
-= (p
- b
.data
) + 1;
100 /* keep any remaining bytes in web->input.partial */
104 b
= data_blob_talloc(web
, b
.data
, b
.length
);
105 data_blob_free(&web
->input
.partial
);
106 web
->input
.partial
= b
;
108 /* we finish when we have both the full headers (terminated by
109 a blank line) and any post data, as indicated by the
111 if (web
->input
.end_of_headers
&&
112 web
->input
.partial
.length
>= web
->input
.content_length
) {
113 if (web
->input
.partial
.length
> web
->input
.content_length
) {
114 web
->input
.partial
.data
[web
->input
.content_length
] = 0;
116 EVENT_FD_NOT_READABLE(web
->conn
->event
.fde
);
118 /* the reference/unlink code here is quite subtle. It
119 is needed because the rendering of the web-pages, and
120 in particular the esp/ejs backend, is semi-async. So
121 we could well end up in the connection timeout code
122 while inside http_process_input(), but we must not
123 destroy the stack variables being used by that
124 rendering process when we handle the timeout. */
125 if (!talloc_reference(web
->task
, web
)) goto failed
;
126 http_process_input(web
);
127 talloc_unlink(web
->task
, web
);
132 stream_terminate_connection(conn
, "websrv_recv: failed");
137 called when a web connection becomes writable
139 static void websrv_send(struct stream_connection
*conn
, uint16_t flags
)
141 struct websrv_context
*web
= talloc_get_type(conn
->private,
142 struct websrv_context
);
147 b
= web
->output
.content
;
148 b
.data
+= web
->output
.nsent
;
149 b
.length
-= web
->output
.nsent
;
151 status
= socket_send(conn
->socket
, &b
, &nsent
);
152 if (NT_STATUS_IS_ERR(status
)) {
153 stream_terminate_connection(web
->conn
, "socket_send: failed");
156 if (!NT_STATUS_IS_OK(status
)) {
160 web
->output
.nsent
+= nsent
;
162 /* possibly read some more raw data from a file */
163 if (web
->output
.content
.length
== web
->output
.nsent
&&
164 web
->output
.fd
!= -1) {
168 data_blob_free(&web
->output
.content
);
169 web
->output
.nsent
= 0;
171 nread
= read(web
->output
.fd
, buf
, sizeof(buf
));
172 if (nread
== -1 && errno
== EINTR
) {
176 close(web
->output
.fd
);
180 web
->output
.content
= data_blob_talloc(web
, buf
, nread
);
183 if (web
->output
.content
.length
== web
->output
.nsent
&&
184 web
->output
.fd
== -1) {
185 stream_terminate_connection(web
->conn
, "websrv_send: finished sending");
190 establish a new connection to the web server
192 static void websrv_accept(struct stream_connection
*conn
)
194 struct task_server
*task
= talloc_get_type(conn
->private, struct task_server
);
195 struct esp_data
*edata
= talloc_get_type(task
->private, struct esp_data
);
196 struct websrv_context
*web
;
197 struct socket_context
*tls_socket
;
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 tls_socket
= tls_init_server(edata
->tls_params
, conn
->socket
,
214 conn
->event
.fde
, "GPHO");
215 /* We might not have TLS, or it might not have initilised */
217 talloc_unlink(conn
, conn
->socket
);
218 talloc_steal(conn
, tls_socket
);
219 conn
->socket
= tls_socket
;
221 DEBUG(3, ("TLS not available for web_server connections\n"));
231 static const struct stream_server_ops web_stream_ops
= {
233 .accept_connection
= websrv_accept
,
234 .recv_handler
= websrv_recv
,
235 .send_handler
= websrv_send
,
239 startup the web server task
241 static void websrv_task_init(struct task_server
*task
)
244 uint16_t port
= lp_web_port();
245 const struct model_ops
*model_ops
;
247 task_server_set_title(task
, "task[websrv]");
249 /* run the web server as a single process */
250 model_ops
= process_model_byname("single");
251 if (!model_ops
) goto failed
;
253 if (lp_interfaces() && lp_bind_interfaces_only()) {
254 int num_interfaces
= iface_count();
256 for(i
= 0; i
< num_interfaces
; i
++) {
257 const char *address
= iface_n_ip(i
);
258 status
= stream_setup_socket(task
->event_ctx
, model_ops
,
262 if (!NT_STATUS_IS_OK(status
)) goto failed
;
265 status
= stream_setup_socket(task
->event_ctx
, model_ops
,
267 "ipv4", lp_socket_address(),
269 if (!NT_STATUS_IS_OK(status
)) goto failed
;
272 /* startup the esp processor - unfortunately we can't do this
273 per connection as that wouldn't allow for session variables */
274 status
= http_setup_esp(task
);
275 if (!NT_STATUS_IS_OK(status
)) goto failed
;
280 task_server_terminate(task
, "websrv_task_init: failed to startup web server task");
285 called on startup of the web server service It's job is to start
286 listening on all configured sockets
288 static NTSTATUS
websrv_init(struct event_context
*event_context
,
289 const struct model_ops
*model_ops
)
291 return task_server_startup(event_context
, model_ops
, websrv_task_init
);
294 /* called at smbd startup - register ourselves as a server service */
295 NTSTATUS
server_service_web_init(void)
297 return register_server_service("web", websrv_init
);