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 "web_server/web_server.h"
27 #include "lib/events/events.h"
28 #include "system/filesys.h"
30 /* don't allow connections to hang around forever */
31 #define HTTP_TIMEOUT 30
34 destroy a web connection
36 static int websrv_destructor(void *ptr)
38 struct websrv_context *web = talloc_get_type(ptr, struct websrv_context);
39 if (web->output.fd != -1) {
40 close(web->output.fd);
46 called when a connection times out. This prevents a stuck connection
47 from hanging around forever
49 static void websrv_timeout(struct event_context *event_context,
50 struct timed_event *te,
51 struct timeval t, void *private)
53 struct websrv_context *web = talloc_get_type(private, struct websrv_context);
54 stream_terminate_connection(web->conn, "websrv_context: timeout");
58 called when a web connection becomes readable
60 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
62 struct websrv_context *web = talloc_get_type(conn->private,
63 struct websrv_context);
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;
75 status = data_blob_append(web, &web->input.partial, buf, nread);
76 if (!NT_STATUS_IS_OK(status)) goto failed;
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;
84 if (p != b.data && p[-1] == '\r') {
87 status = http_parse_header(web, line);
88 if (!NT_STATUS_IS_OK(status)) return;
89 b.length -= (p - b.data) + 1;
93 /* keep any remaining bytes in web->input.partial */
97 b = data_blob_talloc(web, b.data, b.length);
98 data_blob_free(&web->input.partial);
99 web->input.partial = b;
101 /* we finish when we have both the full headers (terminated by
102 a blank line) and any post data, as indicated by the
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);
112 stream_terminate_connection(conn, "websrv_recv: failed\n");
117 called when a web connection becomes writable
119 static void websrv_send(struct stream_connection *conn, uint16_t flags)
121 struct websrv_context *web = talloc_get_type(conn->private,
122 struct websrv_context);
127 b = web->output.content;
128 b.data += web->output.nsent;
129 b.length -= web->output.nsent;
131 status = tls_socket_send(web, &b, &nsent);
132 if (NT_STATUS_IS_ERR(status)) {
133 stream_terminate_connection(web->conn, "socket_send: failed");
136 if (!NT_STATUS_IS_OK(status)) {
140 web->output.nsent += nsent;
142 /* possibly read some more raw data from a file */
143 if (web->output.content.length == web->output.nsent &&
144 web->output.fd != -1) {
148 data_blob_free(&web->output.content);
149 web->output.nsent = 0;
151 nread = read(web->output.fd, buf, sizeof(buf));
153 close(web->output.fd);
156 if (nread == -1 && errno == EINTR) {
159 web->output.content = data_blob_talloc(web, buf, nread);
162 if (web->output.content.length == web->output.nsent &&
163 web->output.fd == -1) {
164 stream_terminate_connection(web->conn, NULL);
169 establish a new connection to the web server
171 static void websrv_accept(struct stream_connection *conn)
173 struct task_server *task = talloc_get_type(conn->private, struct task_server);
174 struct websrv_context *web;
177 web = talloc_zero(conn, struct websrv_context);
178 if (web == NULL) goto failed;
184 talloc_set_destructor(web, websrv_destructor);
186 event_add_timed(conn->event.ctx, web,
187 timeval_current_ofs(HTTP_TIMEOUT, 0),
188 websrv_timeout, web);
190 status = tls_init_connection(web);
191 if (!NT_STATUS_IS_OK(status)) goto failed;
200 static const struct stream_server_ops web_stream_ops = {
202 .accept_connection = websrv_accept,
203 .recv_handler = websrv_recv,
204 .send_handler = websrv_send,
208 startup the web server task
210 static void websrv_task_init(struct task_server *task)
213 uint16_t port = lp_web_port();
214 const struct model_ops *model_ops;
216 /* run the web server as a single process */
217 model_ops = process_model_byname("single");
218 if (!model_ops) goto failed;
220 if (lp_interfaces() && lp_bind_interfaces_only()) {
221 int num_interfaces = iface_count();
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,
229 if (!NT_STATUS_IS_OK(status)) goto failed;
232 status = stream_setup_socket(task->event_ctx, model_ops,
234 "ipv4", lp_socket_address(),
236 if (!NT_STATUS_IS_OK(status)) goto failed;
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;
244 tls_initialise(task);
249 task_terminate(task, "Failed to startup web server task");
254 called on startup of the web server service It's job is to start
255 listening on all configured sockets
257 static NTSTATUS websrv_init(struct event_context *event_context,
258 const struct model_ops *model_ops)
260 return task_server_startup(event_context, model_ops, websrv_task_init);
263 /* called at smbd startup - register ourselves as a server service */
264 NTSTATUS server_service_web_init(void)
266 return register_server_service("web", websrv_init);