18ad9a5411ce52512fff7ad66e397b9968fc5a15
[paraslash.git] / http_send.c
1 /*
2  * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file http_send.c paraslash's http sender */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "string.h"
15 #include "server.cmdline.h"
16 #include "afh.h"
17 #include "afs.h"
18 #include "server.h"
19 #include "http.h"
20 #include "vss.h"
21 #include "send.h"
22 #include "list.h"
23 #include "close_on_fork.h"
24 #include "net.h"
25 #include "fd.h"
26 #include "chunk_queue.h"
27 #include "acl.h"
28
29 /** Message sent to clients that do not send a valid get request. */
30 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
31
32
33 /** The possible states of a client from the server's POV. */
34 enum http_status {
35         /** We accepted the connection on the tcp socket. */
36         HTTP_CONNECTED,
37         /** Successfully received the get request. */
38         HTTP_GOT_GET_REQUEST,
39         /** Connection is ready for sending audio data. */
40         HTTP_STREAMING,
41         /** We didn't receive a valid get request. */
42         HTTP_INVALID_GET_REQUEST
43 };
44
45 /** Clients will be kicked if there are more than that many bytes pending. */
46 #define MAX_BACKLOG 400000
47 /** The list of connected clients. */
48 static struct list_head clients;
49 /** The whitelist/blacklist. */
50 static struct list_head http_acl;
51
52 /** Describes one client that connected the tcp port of the http sender. */
53 struct http_client {
54         /** The file descriptor of the client. */
55         int fd;
56         /** The socket `name' of the client. */
57         char *name;
58         /** The client's current status. */
59         enum http_status status;
60         /** Non-zero if we included \a fd in the read set.*/
61         int check_r;
62         /** Non-zero if we included \a fd in the write set. */
63         int check_w;
64         /** The position of this client in the client list. */
65         struct list_head node;
66         /** non-zero if audio file header has been sent */
67         int header_sent;
68         /** The list of pending chunks for this client. */
69         struct chunk_queue *cq;
70 };
71
72 static int listen_fd = -1, numclients;
73
74
75 static void http_shutdown_client(struct http_client *hc, const char *msg)
76 {
77         PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", hc->name, hc->fd,
78                 msg);
79         numclients--;
80         free(hc->name);
81         close(hc->fd);
82         del_close_on_fork_list(hc->fd);
83         cq_destroy(hc->cq);
84         list_del(&hc->node);
85         free(hc);
86 }
87
88 static void http_shutdown_clients(void)
89 {
90         struct http_client *hc, *tmp;
91         list_for_each_entry_safe(hc, tmp, &clients, node)
92                 http_shutdown_client(hc, "vss request");
93 }
94
95 static int http_send_msg(struct http_client *hc, const char *msg)
96 {
97         int ret = send_buffer(hc->fd, msg);
98
99         if (ret < 0)
100                 http_shutdown_client(hc, "send msg failed");
101         return ret;
102 }
103
104 static void http_send_ok_msg(struct http_client *hc)
105 {
106         PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
107         http_send_msg(hc, HTTP_OK_MSG);
108 }
109
110 static int http_send_err_msg(struct http_client *hc)
111 {
112         PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
113         return http_send_msg(hc, HTTP_ERR_MSG);
114 }
115
116 /*
117  * ret: Negative on errors, zero if nothing was written and write returned
118  * EAGAIN, number of bytes written else.
119  */
120 static int http_write(int fd, const char *buf, size_t len)
121 {
122         size_t written = 0;
123
124         while (written < len) {
125                 int ret = write(fd, buf + written, len - written);
126                 if (ret < 0 && errno == EAGAIN)
127                         return written;
128                 if (ret < 0)
129                         return -ERRNO_TO_PARA_ERROR(errno);
130                 written += ret;
131         }
132         return written;
133 }
134
135
136 static int send_queued_chunks(struct http_client *hc)
137 {
138         struct queued_chunk *qc;
139         while ((qc = cq_peek(hc->cq))) {
140                 char *buf;
141                 size_t len;
142                 int ret;
143                 cq_get(qc, &buf, &len);
144                 ret = http_write(hc->fd, buf, len);
145                 if (ret < 0)
146                         return ret;
147                 cq_update(hc->cq, ret);
148                 if (ret != len)
149                         return 1;
150                 cq_dequeue(hc->cq);
151         }
152         return 1;
153 }
154
155 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
156         size_t sent)
157 {
158         int ret = cq_enqueue(hc->cq, chunk_num, sent);
159         if (ret < 0)
160                 http_shutdown_client(hc, "queue error");
161         return ret;
162 }
163
164 static void http_send(long unsigned current_chunk,
165         __a_unused long unsigned chunks_sent, const char *buf, size_t len)
166 {
167         struct http_client *hc, *tmp;
168         int ret;
169
170         list_for_each_entry_safe(hc, tmp, &clients, node) {
171                 if (hc->status != HTTP_STREAMING)
172                         continue;
173                 if (!hc->header_sent && current_chunk) {
174                         size_t hlen;
175                         char *hbuf = vss_get_header(&hlen);
176                         if (hbuf && hlen > 0) { /* need to send header */
177                                 PARA_INFO_LOG("queueing header: %zu\n", hlen);
178                                 if (queue_chunk_or_shutdown(hc, -1U, 0) < 0)
179                                         continue;
180                         } else
181                                 PARA_INFO_LOG("no need to queue header\n");
182                         hc->header_sent = 1;
183                 }
184                 ret = send_queued_chunks(hc);
185                 if (ret < 0) {
186                         http_shutdown_client(hc, "queue send error");
187                         continue;
188                 }
189                 if (!len)
190                         continue;
191                 ret = http_write(hc->fd, buf, len);
192                 if (ret < 0) {
193                         http_shutdown_client(hc, "send error");
194                         continue;
195                 }
196                 if (ret != len)
197                         queue_chunk_or_shutdown(hc, current_chunk, ret);
198         }
199 }
200
201 static void http_post_select(fd_set *rfds, fd_set *wfds)
202 {
203         int i = -1, match;
204         struct http_client *hc, *tmp;
205         const char *err_msg;
206
207         if (listen_fd < 0)
208                 return;
209         list_for_each_entry_safe(hc, tmp, &clients, node) {
210                 i++;
211 //              PARA_DEBUG_LOG("handling client %d: %s\n", i, remote_name(hc->fd));
212                 switch (hc->status) {
213                 case HTTP_STREAMING: /* nothing to do */
214                         break;
215                 case HTTP_CONNECTED: /* need to recv get request */
216                         if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
217                                 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
218                                                 < 0) {
219                                         hc->status = HTTP_INVALID_GET_REQUEST;
220                                 } else {
221                                         hc->status = HTTP_GOT_GET_REQUEST;
222                                         PARA_INFO_LOG("%s",
223                                                 "received get request\n");
224                                 }
225                         }
226                         break;
227                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
228                         if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
229                                 hc->status = HTTP_STREAMING;
230                                 http_send_ok_msg(hc);
231                         }
232                         break;
233                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
234                         if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
235                                 if (http_send_err_msg(hc) >= 0)
236                                         http_shutdown_client(hc,
237                                                 "invalid get request");
238                         }
239                         break;
240                 }
241         }
242         if (!FD_ISSET(listen_fd, rfds))
243                 return;
244         hc = para_calloc(sizeof(struct http_client));
245         err_msg = "accept error";
246         hc->fd = para_accept(listen_fd, NULL, 0);
247         if (hc->fd <= 0)
248                 goto err_out;
249         hc->name = make_message("%s", remote_name(hc->fd));
250         PARA_NOTICE_LOG("connection from %s (fd %d)\n", hc->name, hc->fd);
251         if (conf.http_max_clients_arg > 0 && numclients >=
252                         conf.http_max_clients_arg) {
253                 err_msg = "server full";
254                 goto err_out;
255         }
256         match = acl_lookup(hc->fd, &http_acl);
257         PARA_DEBUG_LOG("acl lookup returned %d\n", match);
258         if ((match && !conf.http_default_deny_given) ||
259                         (!match && conf.http_default_deny_given)) {
260                 err_msg = "permission denied";
261                 goto err_out;
262         }
263         err_msg = "failed to mark fd non-blocking";
264         if (mark_fd_nonblocking(hc->fd) < 0)
265                 goto err_out;
266         hc->status = HTTP_CONNECTED;
267         hc->cq = cq_new(MAX_BACKLOG);
268         numclients++;
269         PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
270                 hc->name, hc->fd);
271         para_list_add(&hc->node, &clients);
272         add_close_on_fork_list(hc->fd);
273         return;
274 err_out:
275         PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
276                 hc->name, err_msg);
277         if (hc->fd > 0)
278                 close(hc->fd);
279         free(hc);
280 }
281
282 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
283 {
284         struct http_client *hc, *tmp;
285
286         if (listen_fd < 0)
287                 return;
288         para_fd_set(listen_fd, rfds, max_fileno);
289         list_for_each_entry_safe(hc, tmp, &clients, node) {
290                 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
291                 hc->check_r = 0;
292                 hc->check_w = 0;
293                 switch (hc->status) {
294                 case HTTP_STREAMING:
295                         break;
296                 case HTTP_CONNECTED: /* need to recv get request */
297                         para_fd_set(hc->fd, rfds, max_fileno);
298                         hc->check_r = 1;
299                         break;
300                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
301                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
302                         para_fd_set(hc->fd, wfds, max_fileno);
303                         hc->check_w = 1;
304                         break;
305                 }
306         }
307 }
308
309 static int http_open(void)
310 {
311         int ret;
312
313         listen_fd = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.http_port_arg);
314         if (listen_fd < 0)
315                 return listen_fd;
316         ret = mark_fd_nonblocking(listen_fd);
317         if (ret < 0) {
318                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
319                 exit(EXIT_FAILURE);
320         }
321         add_close_on_fork_list(listen_fd);
322         return 1;
323 }
324
325 static int http_com_on(__a_unused struct sender_command_data *scd)
326 {
327         if (listen_fd >= 0)
328                 return 1;
329         return http_open();
330 }
331
332 static int http_com_off(__a_unused struct sender_command_data *scd)
333 {
334         if (listen_fd < 0)
335                 return 1;
336         PARA_NOTICE_LOG("closing http port %d\n", conf.http_port_arg);
337         close(listen_fd);
338         del_close_on_fork_list(listen_fd);
339         http_shutdown_clients();
340         listen_fd = -1;
341         return 1;
342 }
343
344 static int http_com_deny(struct sender_command_data *scd)
345 {
346         if (conf.http_default_deny_given)
347                 acl_del_entry(&http_acl, scd->addr, scd->netmask);
348         else
349                 acl_add_entry(&http_acl, scd->addr, scd->netmask);
350         return 1;
351 }
352
353 static int http_com_allow(struct sender_command_data *scd)
354 {
355         if (conf.http_default_deny_given)
356                 acl_add_entry(&http_acl, scd->addr, scd->netmask);
357         else
358                 acl_del_entry(&http_acl, scd->addr, scd->netmask);
359         return 1;
360 }
361
362 static char *http_info(void)
363 {
364         char *clnts = NULL, *ret;
365         struct http_client *hc, *tmp_hc;
366
367         char *acl_contents = acl_get_contents(&http_acl);
368         list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
369                 char *tmp = make_message("%s%s ", clnts? clnts : "", hc->name);
370                 free(clnts);
371                 clnts = tmp;
372         }
373         ret = make_message(
374                 "http status: %s\n"
375                 "http tcp port: %d\n"
376                 "http clients: %d\n"
377                 "http maximal number of clients: %d%s\n"
378                 "http connected clients: %s\n"
379                 "http access %s list: %s\n",
380                 (listen_fd >= 0)? "on" : "off",
381                 conf.http_port_arg,
382                 numclients,
383                 conf.http_max_clients_arg,
384                 conf.http_max_clients_arg > 0? "" : " (unlimited)",
385                 clnts? clnts : "(none)",
386                 conf.http_default_deny_given? "allow" : "deny",
387                 acl_contents? acl_contents : "(none)"
388         );
389         free(acl_contents);
390         free(clnts);
391         return ret;
392 }
393
394 static char *http_help(void)
395 {
396         return make_message(
397                 "usage: {on|off}\n"
398                 "usage: {allow|deny} IP mask\n"
399                 "example: allow 127.0.0.1 32\n"
400         );
401 }
402
403 /**
404  * The init function of the http sender.
405  *
406  * \param s Pointer to the http sender struct.
407  *
408  * It initializes all function pointers of \a s, the client list and the access
409  * control list. If the autostart option was given, the tcp port is opened.
410  */
411 void http_send_init(struct sender *s)
412 {
413         INIT_LIST_HEAD(&clients);
414         s->info = http_info;
415         s->send = http_send;
416         s->pre_select = http_pre_select;
417         s->post_select = http_post_select;
418         s->shutdown_clients = http_shutdown_clients;
419         s->help = http_help;
420         s->client_cmds[SENDER_ON] = http_com_on;
421         s->client_cmds[SENDER_OFF] = http_com_off;
422         s->client_cmds[SENDER_DENY] = http_com_deny;
423         s->client_cmds[SENDER_ALLOW] = http_com_allow;
424         s->client_cmds[SENDER_ADD] = NULL;
425         s->client_cmds[SENDER_DELETE] = NULL;
426         acl_init(&http_acl, conf.http_access_arg, conf.http_access_given);
427         if (!conf.http_no_autostart_given)
428                 http_open(); /* ignore errors */
429         PARA_DEBUG_LOG("%s", "http sender init complete\n");
430 }