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"
29 #include "lib/tls/tls.h"
31 /* don't allow connections to hang around forever */
32 #define HTTP_TIMEOUT 30
35 destroy a web connection
37 static int websrv_destructor(void *ptr)
39 struct websrv_context *web = talloc_get_type(ptr, struct websrv_context);
40 if (web->output.fd != -1) {
41 close(web->output.fd);
47 called when a connection times out. This prevents a stuck connection
48 from hanging around forever
50 static void websrv_timeout(struct event_context *event_context,
51 struct timed_event *te,
52 struct timeval t, void *private)
54 struct websrv_context *web = talloc_get_type(private, struct websrv_context);
55 stream_terminate_connection(web->conn, "websrv_context: timeout");
59 called when a web connection becomes readable
61 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
63 struct websrv_context *web = talloc_get_type(conn->private,
64 struct websrv_context);
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;
76 status = data_blob_append(web, &web->input.partial, buf, nread);
77 if (!NT_STATUS_IS_OK(status)) goto failed;
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;
85 if (p != b.data && p[-1] == '\r') {
88 status = http_parse_header(web, line);
89 if (!NT_STATUS_IS_OK(status)) return;
90 b.length -= (p - b.data) + 1;
94 /* keep any remaining bytes in web->input.partial */
98 b = data_blob_talloc(web, b.data, b.length);
99 data_blob_free(&web->input.partial);
100 web->input.partial = b;
102 /* we finish when we have both the full headers (terminated by
103 a blank line) and any post data, as indicated by the
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);
113 stream_terminate_connection(conn, "websrv_recv: failed\n");
118 called when a web connection becomes writable
120 static void websrv_send(struct stream_connection *conn, uint16_t flags)
122 struct websrv_context *web = talloc_get_type(conn->private,
123 struct websrv_context);
128 b = web->output.content;
129 b.data += web->output.nsent;
130 b.length -= web->output.nsent;
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");
137 if (!NT_STATUS_IS_OK(status)) {
141 web->output.nsent += nsent;
143 /* possibly read some more raw data from a file */
144 if (web->output.content.length == web->output.nsent &&
145 web->output.fd != -1) {
149 data_blob_free(&web->output.content);
150 web->output.nsent = 0;
152 nread = read(web->output.fd, buf, sizeof(buf));
154 close(web->output.fd);
157 if (nread == -1 && errno == EINTR) {
160 web->output.content = data_blob_talloc(web, buf, nread);
163 if (web->output.content.length == web->output.nsent &&
164 web->output.fd == -1) {
165 talloc_free(web->tls);
167 stream_terminate_connection(web->conn, NULL);
172 establish a new connection to the web server
174 static void websrv_accept(struct stream_connection *conn)
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;
180 web = talloc_zero(conn, struct websrv_context);
181 if (web == NULL) goto failed;
187 talloc_set_destructor(web, websrv_destructor);
189 event_add_timed(conn->event.ctx, web,
190 timeval_current_ofs(HTTP_TIMEOUT, 0),
191 websrv_timeout, web);
193 web->tls = tls_init_server(edata->tls_params, conn->socket,
194 conn->event.fde, "GPHO");
195 if (web->tls == NULL) goto failed;
204 static const struct stream_server_ops web_stream_ops = {
206 .accept_connection = websrv_accept,
207 .recv_handler = websrv_recv,
208 .send_handler = websrv_send,
212 startup the web server task
214 static void websrv_task_init(struct task_server *task)
217 uint16_t port = lp_web_port();
218 const struct model_ops *model_ops;
220 /* run the web server as a single process */
221 model_ops = process_model_byname("single");
222 if (!model_ops) goto failed;
224 if (lp_interfaces() && lp_bind_interfaces_only()) {
225 int num_interfaces = iface_count();
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,
233 if (!NT_STATUS_IS_OK(status)) goto failed;
236 status = stream_setup_socket(task->event_ctx, model_ops,
238 "ipv4", lp_socket_address(),
240 if (!NT_STATUS_IS_OK(status)) goto failed;
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;
251 task_terminate(task, "Failed to startup web server task");
256 called on startup of the web server service It's job is to start
257 listening on all configured sockets
259 static NTSTATUS websrv_init(struct event_context *event_context,
260 const struct model_ops *model_ops)
262 return task_server_startup(event_context, model_ops, websrv_task_init);
265 /* called at smbd startup - register ourselves as a server service */
266 NTSTATUS server_service_web_init(void)
268 return register_server_service("web", websrv_init);