connection.c 53.9 KB
Newer Older
1 2 3 4 5
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
6
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
7 8 9 10
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
Philipp Schafft's avatar
Philipp Schafft committed
11
 * Copyright 2011,      Dave 'justdave' Miller <justdave@mozilla.com>,
12
 * Copyright 2011-2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
13 14
 */

15
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
16 17 18 19
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
20 21
#include <stdio.h>
#include <stdlib.h>
22
#include <errno.h>
Jack Moffitt's avatar
Jack Moffitt committed
23
#include <string.h>
24 25 26
#ifdef HAVE_POLL
#include <sys/poll.h>
#endif
27
#include <sys/types.h>
28 29

#ifndef _WIN32
Jack Moffitt's avatar
Jack Moffitt committed
30 31
#include <sys/socket.h>
#include <netinet/in.h>
32
#else
33
#include <winsock2.h>
34
#endif
Jack Moffitt's avatar
Jack Moffitt committed
35

Marvin Scholz's avatar
Marvin Scholz committed
36 37 38 39
#include "common/thread/thread.h"
#include "common/avl/avl.h"
#include "common/net/sock.h"
#include "common/httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
40

41 42
#include "compat.h"
#include "connection.h"
43
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
44 45
#include "global.h"
#include "util.h"
46
#include "refobject.h"
Jack Moffitt's avatar
Jack Moffitt committed
47 48
#include "refbuf.h"
#include "client.h"
49
#include "errors.h"
Jack Moffitt's avatar
Jack Moffitt committed
50 51
#include "stats.h"
#include "logging.h"
52
#include "fserve.h"
53
#include "slave.h"
54

Jack Moffitt's avatar
Jack Moffitt committed
55
#include "source.h"
56
#include "admin.h"
Michael Smith's avatar
Michael Smith committed
57
#include "auth.h"
58
#include "matchfile.h"
59
#include "tls.h"
60
#include "acl.h"
61 62
#include "refobject.h"
#include "listensocket.h"
Jack Moffitt's avatar
Jack Moffitt committed
63 64 65

#define CATMODULE "connection"

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/* Two different major types of source authentication.
   Shoutcast style is used only by the Shoutcast DSP
   and is a crazy version of HTTP.  It looks like :
     Source Client -> Connects to port + 1
     Source Client -> sends encoder password (plaintext)\r\n
     Icecast -> reads encoder password, if ok, sends OK2\r\n, else disconnects
     Source Client -> reads OK2\r\n, then sends http-type request headers
                      that contain the stream details (icy-name, etc..)
     Icecast -> reads headers, stores them
     Source Client -> starts sending MP3 data
     Source Client -> periodically updates metadata via admin.cgi call

   Icecast auth style uses HTTP and Basic Authorization.
*/

81 82 83 84
typedef struct client_queue_tag {
    client_t *client;
    int offset;
    int shoutcast;
85
    char *shoutcast_mount;
86 87
    char *bodybuffer;
    size_t bodybufferlen;
88
    int tried_body;
89 90
    struct client_queue_tag *next;
} client_queue_t;
Jack Moffitt's avatar
Jack Moffitt committed
91

92
static spin_t _connection_lock; // protects _current_id, _con_queue, _con_queue_tail
93
static volatile unsigned long _current_id = 0;
Jack Moffitt's avatar
Jack Moffitt committed
94 95
static int _initialized = 0;

96 97
static volatile client_queue_t *_req_queue = NULL, **_req_queue_tail = &_req_queue;
static volatile client_queue_t *_con_queue = NULL, **_con_queue_tail = &_con_queue;
98
static volatile client_queue_t *_body_queue = NULL, **_body_queue_tail = &_body_queue;
99
static int tls_ok;
100
static tls_ctx_t *tls_ctx;
101

102
/* filtering client connection based on IP */
103
static matchfile_t *banned_ip, *allowed_ip;
104

105
rwlock_t _source_shutdown_rwlock;
Jack Moffitt's avatar
Jack Moffitt committed
106

107
static void _handle_connection(void);
108
static void get_tls_certificate(ice_config_t *config);
Jack Moffitt's avatar
Jack Moffitt committed
109 110 111

void connection_initialize(void)
{
Marvin Scholz's avatar
Marvin Scholz committed
112 113
    if (_initialized)
        return;
114

115
    thread_spin_create (&_connection_lock);
116
    thread_mutex_create(&move_clients_mutex);
117
    thread_rwlock_create(&_source_shutdown_rwlock);
118
    thread_cond_create(&global.shutdown_cond);
119 120 121 122
    _req_queue = NULL;
    _req_queue_tail = &_req_queue;
    _con_queue = NULL;
    _con_queue_tail = &_con_queue;
123 124
    _body_queue = NULL;
    _body_queue_tail = &_body_queue;
Jack Moffitt's avatar
Jack Moffitt committed
125

126
    _initialized = 1;
Jack Moffitt's avatar
Jack Moffitt committed
127 128 129 130
}

void connection_shutdown(void)
{
Marvin Scholz's avatar
Marvin Scholz committed
131 132
    if (!_initialized)
        return;
133

134
    tls_ctx_unref(tls_ctx);
135 136 137
    matchfile_release(banned_ip);
    matchfile_release(allowed_ip);
 
138
    thread_cond_destroy(&global.shutdown_cond);
139
    thread_rwlock_destroy(&_source_shutdown_rwlock);
140
    thread_spin_destroy (&_connection_lock);
141
    thread_mutex_destroy(&move_clients_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
142

143
    _initialized = 0;
Jack Moffitt's avatar
Jack Moffitt committed
144 145
}

146
void connection_reread_config(ice_config_t *config)
147
{
148
    get_tls_certificate(config);
149
    listensocket_container_configure_and_setup(global.listensockets, config);
150 151
}

Jack Moffitt's avatar
Jack Moffitt committed
152 153
static unsigned long _next_connection_id(void)
{
154
    unsigned long id;
Jack Moffitt's avatar
Jack Moffitt committed
155

156
    thread_spin_lock(&_connection_lock);
157
    id = _current_id++;
158
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
159

160
    return id;
Jack Moffitt's avatar
Jack Moffitt committed
161 162
}

163

164
#ifdef ICECAST_CAP_TLS
165
static void get_tls_certificate(ice_config_t *config)
166
{
167 168
    const char *keyfile;

169
    config->tls_ok = tls_ok = 0;
170

171 172 173 174
    keyfile = config->tls_context.key_file;
    if (!keyfile)
        keyfile = config->tls_context.cert_file;

175
    tls_ctx_unref(tls_ctx);
176
    tls_ctx = tls_ctx_new(config->tls_context.cert_file, keyfile, config->tls_context.cipher_list);
177 178
    if (!tls_ctx) {
        ICECAST_LOG_INFO("No TLS capability on any configured ports");
179
        return;
180 181
    }

182
    config->tls_ok = tls_ok = 1;
183 184 185
}


186
/* handlers for reading and writing a connection_t when there is TLS
187 188
 * configured on the listening port
 */
189
static int connection_read_tls(connection_t *con, void *buf, size_t len)
190
{
191
    ssize_t bytes = tls_read(con->tls, buf, len);
192

Marvin Scholz's avatar
Marvin Scholz committed
193
    if (bytes < 0) {
194
        if (tls_want_io(con->tls) > 0)
195
            return -1;
196 197 198 199 200
        con->error = 1;
    }
    return bytes;
}

201
static int connection_send_tls(connection_t *con, const void *buf, size_t len)
202
{
203
    ssize_t bytes = tls_write(con->tls, buf, len);
204

Marvin Scholz's avatar
Marvin Scholz committed
205
    if (bytes < 0) {
206 207
        if (tls_want_io(con->tls) > 0)
            return -1;
208
        con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
209
    } else {
210
        con->sent_bytes += bytes;
Marvin Scholz's avatar
Marvin Scholz committed
211
    }
212 213 214 215
    return bytes;
}
#else

216 217
/* TLS not compiled in, so at least log it */
static void get_tls_certificate(ice_config_t *config)
218
{
219
    tls_ok = 0;
220
    ICECAST_LOG_INFO("No TLS capability. "
Philipp Schafft's avatar
Philipp Schafft committed
221
                     "Rebuild Icecast with OpenSSL support to enable this.");
222
}
223
#endif /* ICECAST_CAP_TLS */
224 225 226 227 228


/* handlers (default) for reading and writing a connection_t, no encrpytion
 * used just straight access to the socket
 */
Marvin Scholz's avatar
Marvin Scholz committed
229
static int connection_read(connection_t *con, void *buf, size_t len)
230
{
Marvin Scholz's avatar
Marvin Scholz committed
231
    int bytes = sock_read_bytes(con->sock, buf, len);
232 233
    if (bytes == 0)
        con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
234
    if (bytes == -1 && !sock_recoverable(sock_error()))
235 236 237 238
        con->error = 1;
    return bytes;
}

Marvin Scholz's avatar
Marvin Scholz committed
239
static int connection_send(connection_t *con, const void *buf, size_t len)
240
{
Marvin Scholz's avatar
Marvin Scholz committed
241 242 243
    int bytes = sock_write_bytes(con->sock, buf, len);
    if (bytes < 0) {
        if (!sock_recoverable(sock_error()))
244
            con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
245
    } else {
246
        con->sent_bytes += bytes;
Marvin Scholz's avatar
Marvin Scholz committed
247
    }
248 249 250
    return bytes;
}

251
connection_t *connection_create(sock_t sock, listensocket_t *listensocket_real, listensocket_t* listensocket_effective, char *ip)
252
{
253
    connection_t *con;
254 255 256 257

    if (!matchfile_match_allow_deny(allowed_ip, banned_ip, ip))
        return NULL;

258
    con = (connection_t *)calloc(1, sizeof(connection_t));
Marvin Scholz's avatar
Marvin Scholz committed
259
    if (con) {
260 261 262
        refobject_ref(listensocket_real);
        refobject_ref(listensocket_effective);

Marvin Scholz's avatar
Marvin Scholz committed
263
        con->sock       = sock;
264 265
        con->listensocket_real = listensocket_real;
        con->listensocket_effective = listensocket_effective;
Marvin Scholz's avatar
Marvin Scholz committed
266 267 268
        con->con_time   = time(NULL);
        con->id         = _next_connection_id();
        con->ip         = ip;
269
        con->tlsmode    = ICECAST_TLSMODE_AUTO;
Marvin Scholz's avatar
Marvin Scholz committed
270 271
        con->read       = connection_read;
        con->send       = connection_send;
272
    }
273

274
    return con;
275 276
}

277
/* prepare connection for interacting over a TLS connection
278
 */
279
void connection_uses_tls(connection_t *con)
280
{
281
#ifdef ICECAST_CAP_TLS
282
    if (con->tls)
283 284
        return;

285 286 287 288 289 290
    if (con->readbufferlen) {
        ICECAST_LOG_ERROR("Connection is now using TLS but has data put back. BAD. Discarding putback data.");
        free(con->readbuffer);
        con->readbufferlen = 0;
    }

291
    con->tlsmode = ICECAST_TLSMODE_RFC2818;
292 293
    con->read = connection_read_tls;
    con->send = connection_send_tls;
294 295 296
    con->tls = tls_new(tls_ctx);
    tls_set_incoming(con->tls);
    tls_set_socket(con->tls, con->sock);
297 298 299
#endif
}

300 301
ssize_t connection_read_bytes(connection_t *con, void *buf, size_t len)
{
302 303 304 305 306 307 308 309
    ssize_t done = 0;
    ssize_t ret;

    if (con->readbufferlen) {
        ICECAST_LOG_DEBUG("On connection %p we read from putback buffer, filled with %zu bytes, requested are %zu bytes", con, con->readbufferlen, len);
        if (len >= con->readbufferlen) {
            memcpy(buf, con->readbuffer, con->readbufferlen);
            free(con->readbuffer);
310
            ICECAST_LOG_DEBUG("New fill in buffer=<empty>");
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
            if (len == con->readbufferlen) {
                con->readbufferlen = 0;
                return len;
            } else {
                len -= con->readbufferlen;
                buf += con->readbufferlen;
                done = con->readbufferlen;
                con->readbufferlen = 0;
            }
        } else {
            memcpy(buf, con->readbuffer, len);
            memmove(con->readbuffer, con->readbuffer+len, con->readbufferlen-len);
            con->readbufferlen -= len;
            return len;
        }
    }

    ret = con->read(con, buf, len);

    if (ret < 0) {
        if (done == 0) {
            return ret;
        } else {
            return done;
        }
    }

    return done + ret;
}

int connection_read_put_back(connection_t *con, const void *buf, size_t len)
{
    void *n;

    if (con->readbufferlen) {
        n = realloc(con->readbuffer, con->readbufferlen + len);
        if (!n)
            return -1;

        memcpy(n + con->readbufferlen, buf, len);
        con->readbuffer = n;
        con->readbufferlen += len;

        ICECAST_LOG_DEBUG("On connection %p %zu bytes have been put back.", con, len);
        return 0;
    } else {
        n = malloc(len);
        if (!n)
            return -1;

        memcpy(n, buf, len);
        con->readbuffer = n;
        con->readbufferlen = len;
        ICECAST_LOG_DEBUG("On connection %p %zu bytes have been put back.", con, len);
        return 0;
    }
367 368
}

369 370 371 372
/* add client to connection queue. At this point some header information
 * has been collected, so we now pass it onto the connection thread for
 * further processing
 */
373
static void _add_connection(client_queue_t *node)
Jack Moffitt's avatar
Jack Moffitt committed
374
{
375
    thread_spin_lock(&_connection_lock);
376
    *_con_queue_tail = node;
377 378
    _con_queue_tail = (volatile client_queue_t **) &node->next;
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
379 380 381
}


382 383 384 385 386 387
/* this returns queued clients for the connection thread. headers are
 * already provided, but need to be parsed.
 */
static client_queue_t *_get_connection(void)
{
    client_queue_t *node = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
388

Marvin Scholz's avatar
Marvin Scholz committed
389
    thread_spin_lock(&_connection_lock);
390

Marvin Scholz's avatar
Marvin Scholz committed
391
    if (_con_queue){
392 393 394 395
        node = (client_queue_t *)_con_queue;
        _con_queue = node->next;
        if (_con_queue == NULL)
            _con_queue_tail = &_con_queue;
396
        node->next = NULL;
397
    }
398

Marvin Scholz's avatar
Marvin Scholz committed
399
    thread_spin_unlock(&_connection_lock);
400 401
    return node;
}
Jack Moffitt's avatar
Jack Moffitt committed
402 403


404
/* run along queue checking for any data that has come in or a timeout */
405
static void process_request_queue (void)
406 407
{
    client_queue_t **node_ref = (client_queue_t **)&_req_queue;
408 409 410 411 412 413
    ice_config_t *config;
    int timeout;
    char peak;

    config = config_get_config();
    timeout = config->header_timeout;
414
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
415

Marvin Scholz's avatar
Marvin Scholz committed
416
    while (*node_ref) {
417 418 419 420
        client_queue_t *node = *node_ref;
        client_t *client = node->client;
        int len = PER_CLIENT_REFBUF_SIZE - 1 - node->offset;
        char *buf = client->refbuf->data + node->offset;
Jack Moffitt's avatar
Jack Moffitt committed
421

422
        if (client->con->tlsmode == ICECAST_TLSMODE_AUTO || client->con->tlsmode == ICECAST_TLSMODE_AUTO_NO_PLAIN) {
423 424
            if (recv(client->con->sock, &peak, 1, MSG_PEEK) == 1) {
                if (peak == 0x16) { /* TLS Record Protocol Content type 0x16 == Handshake */
425
                    connection_uses_tls(client->con);
426 427 428 429
                }
            }
        }

Marvin Scholz's avatar
Marvin Scholz committed
430 431
        if (len > 0) {
            if (client->con->con_time + timeout <= time(NULL)) {
432
                len = 0;
Marvin Scholz's avatar
Marvin Scholz committed
433 434 435
            } else {
                len = client_read_bytes(client, buf, len);
            }
436
        }
Jack Moffitt's avatar
Jack Moffitt committed
437

Marvin Scholz's avatar
Marvin Scholz committed
438
        if (len > 0) {
439
            ssize_t stream_offset = -1;
440 441 442
            int pass_it = 1;
            char *ptr;

443 444
            /* handle \n, \r\n and nsvcap which for some strange reason has
             * EOL as \r\r\n */
445
            node->offset += len;
Marvin Scholz's avatar
Marvin Scholz committed
446 447 448
            client->refbuf->data[node->offset] = '\000';
            do {
                if (node->shoutcast == 1) {
449
                    /* password line */
450 451
                    if (strstr (client->refbuf->data, "\r\r\n") != NULL)
                        break;
452 453 454 455 456 457 458
                    if (strstr (client->refbuf->data, "\r\n") != NULL)
                        break;
                    if (strstr (client->refbuf->data, "\n") != NULL)
                        break;
                }
                /* stream_offset refers to the start of any data sent after the
                 * http style headers, we don't want to lose those */
Marvin Scholz's avatar
Marvin Scholz committed
459 460
                ptr = strstr(client->refbuf->data, "\r\r\n\r\r\n");
                if (ptr) {
461
                    stream_offset = (ptr+6) - client->refbuf->data;
462 463
                    break;
                }
Marvin Scholz's avatar
Marvin Scholz committed
464 465
                ptr = strstr(client->refbuf->data, "\r\n\r\n");
                if (ptr) {
466
                    stream_offset = (ptr+4) - client->refbuf->data;
467 468
                    break;
                }
Marvin Scholz's avatar
Marvin Scholz committed
469 470
                ptr = strstr(client->refbuf->data, "\n\n");
                if (ptr) {
471
                    stream_offset = (ptr+2) - client->refbuf->data;
472 473 474 475
                    break;
                }
                pass_it = 0;
            } while (0);
Jack Moffitt's avatar
Jack Moffitt committed
476

Marvin Scholz's avatar
Marvin Scholz committed
477
            if (pass_it) {
478 479 480 481
                if (stream_offset != -1) {
                    connection_read_put_back(client->con, client->refbuf->data + stream_offset, node->offset - stream_offset);
                    node->offset = stream_offset;
                }
482 483 484 485
                if ((client_queue_t **)_req_queue_tail == &(node->next))
                    _req_queue_tail = (volatile client_queue_t **)node_ref;
                *node_ref = node->next;
                node->next = NULL;
Marvin Scholz's avatar
Marvin Scholz committed
486
                _add_connection(node);
487
                continue;
488
            }
Marvin Scholz's avatar
Marvin Scholz committed
489 490
        } else {
            if (len == 0 || client->con->error) {
491 492 493
                if ((client_queue_t **)_req_queue_tail == &node->next)
                    _req_queue_tail = (volatile client_queue_t **)node_ref;
                *node_ref = node->next;
Marvin Scholz's avatar
Marvin Scholz committed
494 495
                client_destroy(client);
                free(node);
496 497 498 499
                continue;
            }
        }
        node_ref = &node->next;
500
    }
501
    _handle_connection();
Jack Moffitt's avatar
Jack Moffitt committed
502 503
}

504 505 506 507 508 509 510 511 512 513 514 515
/* add client to body queue.
 */
static void _add_body_client(client_queue_t *node)
{
    ICECAST_LOG_DEBUG("Putting client %p in body queue.", node->client);

    thread_spin_lock(&_connection_lock);
    *_body_queue_tail = node;
    _body_queue_tail = (volatile client_queue_t **) &node->next;
    thread_spin_unlock(&_connection_lock);
}

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
static client_slurp_result_t process_request_body_queue_one(client_queue_t *node, time_t timeout, size_t body_size_limit)
{
        client_t *client = node->client;
        client_slurp_result_t res;

        if (client->parser->req_type == httpp_req_post) {
            if (node->bodybuffer == NULL && client->request_body_read == 0) {
                if (client->request_body_length < 0) {
                    node->bodybufferlen = body_size_limit;
                    node->bodybuffer = malloc(node->bodybufferlen);
                } else if (client->request_body_length <= (ssize_t)body_size_limit) {
                    node->bodybufferlen = client->request_body_length;
                    node->bodybuffer = malloc(node->bodybufferlen);
                }
            }
        }

        if (node->bodybuffer) {
            res = client_body_slurp(client, node->bodybuffer, &(node->bodybufferlen));
            if (res == CLIENT_SLURP_SUCCESS) {
                httpp_parse_postdata(client->parser, node->bodybuffer, node->bodybufferlen);
                free(node->bodybuffer);
                node->bodybuffer = NULL;
            }
        } else {
            res = client_body_skip(client);
        }

        if (res != CLIENT_SLURP_SUCCESS) {
            if (client->con->con_time <= timeout || client->request_body_read >= body_size_limit) {
                return CLIENT_SLURP_ERROR;
            }
        }

        return res;
}
552 553 554 555 556

/* This queue reads data from the body of clients. */
static void process_request_body_queue (void)
{
    client_queue_t **node_ref = (client_queue_t **)&_body_queue;
557 558 559
    ice_config_t *config;
    time_t timeout;
    size_t body_size_limit;
560 561 562 563 564

    ICECAST_LOG_DEBUG("Processing body queue.");

    ICECAST_LOG_DEBUG("_body_queue=%p, &_body_queue=%p, _body_queue_tail=%p", _body_queue, &_body_queue, _body_queue_tail);

565 566 567 568 569
    config = config_get_config();
    timeout = time(NULL) - config->body_timeout;
    body_size_limit = config->body_size_limit;
    config_release_config();

570 571 572 573 574
    while (*node_ref) {
        client_queue_t *node = *node_ref;
        client_t *client = node->client;
        client_slurp_result_t res;

575 576
        node->tried_body = 1;

577 578
        ICECAST_LOG_DEBUG("Got client %p in body queue.", client);

579
        res = process_request_body_queue_one(node, timeout, body_size_limit);
580

581
        if (res != CLIENT_SLURP_NEEDS_MORE_DATA) {
582 583 584 585 586 587 588 589 590 591 592 593
            ICECAST_LOG_DEBUG("Putting client %p back in connection queue.", client);

            if ((client_queue_t **)_body_queue_tail == &(node->next))
                _body_queue_tail = (volatile client_queue_t **)node_ref;
            *node_ref = node->next;
            node->next = NULL;
            _add_connection(node);
            continue;
        }
        node_ref = &node->next;
    }
}
594

595 596 597
/* add node to the queue of requests. This is where the clients are when
 * initial http details are read.
 */
Marvin Scholz's avatar
Marvin Scholz committed
598
static void _add_request_queue(client_queue_t *node)
599 600 601
{
    *_req_queue_tail = node;
    _req_queue_tail = (volatile client_queue_t **)&node->next;
Jack Moffitt's avatar
Jack Moffitt committed
602 603
}

604 605 606
static client_queue_t *create_client_node(client_t *client)
{
    client_queue_t *node = calloc (1, sizeof (client_queue_t));
607
    const listener_t *listener;
608 609 610 611 612 613

    if (!node)
        return NULL;

    node->client = client;

614
    listener = listensocket_get_listener(client->con->listensocket_effective);
615 616 617 618

    if (listener) {
        if (listener->shoutcast_compat)
            node->shoutcast = 1;
619 620
        client->con->tlsmode = listener->tls;
        if (listener->tls == ICECAST_TLSMODE_RFC2818 && tls_ok)
621
            connection_uses_tls(client->con);
622 623 624 625 626 627
        if (listener->shoutcast_mount)
            node->shoutcast_mount = strdup(listener->shoutcast_mount);
    }

    return node;
}
628

629 630 631 632 633 634 635 636
void connection_queue(connection_t *con)
{
    client_queue_t *node;
    client_t *client = NULL;

    global_lock();
    if (client_create(&client, con, NULL) < 0) {
        global_unlock();
637
        client_send_error_by_id(client, ICECAST_ERROR_GEN_CLIENT_LIMIT);
638 639 640 641 642 643 644 645 646 647
        /* don't be too eager as this is an imposed hard limit */
        thread_sleep(400000);
        return;
    }

    /* setup client for reading incoming http */
    client->refbuf->data[PER_CLIENT_REFBUF_SIZE-1] = '\000';

    if (sock_set_blocking(client->con->sock, 0) || sock_set_nodelay(client->con->sock)) {
        global_unlock();
648
        ICECAST_LOG_WARN("Failed to set tcp options on client connection, dropping");
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
        client_destroy(client);
        return;
    }
    node = create_client_node(client);
    global_unlock();

    if (node == NULL) {
        client_destroy(client);
        return;
    }

    _add_request_queue(node);
    stats_event_inc(NULL, "connections");
}

Marvin Scholz's avatar
Marvin Scholz committed
664
void connection_accept_loop(void)
Jack Moffitt's avatar
Jack Moffitt committed
665
{
666
    connection_t *con;
667
    ice_config_t *config;
668
    int duration = 300;
669

Marvin Scholz's avatar
Marvin Scholz committed
670
    config = config_get_config();
671
    get_tls_certificate(config);
Marvin Scholz's avatar
Marvin Scholz committed
672
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
673

Marvin Scholz's avatar
Marvin Scholz committed
674
    while (global.running == ICECAST_RUNNING) {
675
        con = listensocket_container_accept(global.listensockets, duration);
676

Marvin Scholz's avatar
Marvin Scholz committed
677
        if (con) {
678
            connection_queue(con);
679
            duration = 5;
Marvin Scholz's avatar
Marvin Scholz committed
680
        } else {
681 682
            if (_req_queue == NULL)
                duration = 300; /* use longer timeouts when nothing waiting */
683
        }
Marvin Scholz's avatar
Marvin Scholz committed
684
        process_request_queue();
685
        process_request_body_queue();
686
    }
Jack Moffitt's avatar
Jack Moffitt committed
687

688 689 690
    /* Give all the other threads notification to shut down */
    thread_cond_broadcast(&global.shutdown_cond);

691 692 693
    /* wait for all the sources to shutdown */
    thread_rwlock_wlock(&_source_shutdown_rwlock);
    thread_rwlock_unlock(&_source_shutdown_rwlock);
Jack Moffitt's avatar
Jack Moffitt committed
694 695
}

696 697 698

/* Called when activating a source. Verifies that the source count is not
 * exceeded and applies any initial parameters.
699
 */
Marvin Scholz's avatar
Marvin Scholz committed
700
int connection_complete_source(source_t *source, int response)
701
{
702
    ice_config_t *config;
703

Marvin Scholz's avatar
Marvin Scholz committed
704
    global_lock();
705
    ICECAST_LOG_DEBUG("sources count is %d", global.sources);
706

707
    config = config_get_config();
Marvin Scholz's avatar
Marvin Scholz committed
708
    if (global.sources < config->source_limit) {
709
        const char *contenttype;
710
        mount_proxy *mountinfo;
711 712 713 714
        format_type_t format_type;

        /* setup format handler */
        contenttype = httpp_getvar (source->parser, "content-type");
Marvin Scholz's avatar
Marvin Scholz committed
715 716
        if (contenttype != NULL) {
            format_type = format_get_type(contenttype);
717

Marvin Scholz's avatar
Marvin Scholz committed
718
            if (format_type == FORMAT_ERROR) {
719
                config_release_config();
720
                global_unlock();
721
                if (response) {
722
                    client_send_error_by_id(source->client, ICECAST_ERROR_CON_CONTENT_TYPE_NOSYS);
723 724
                    source->client = NULL;
                }
725
                ICECAST_LOG_WARN("Content-type \"%s\" not supported, dropping source", contenttype);
726 727
                return -1;
            }
728 729 730 731
        } else if (source->parser->req_type == httpp_req_put) {
            config_release_config();
            global_unlock();
            if (response) {
732
                client_send_error_by_id(source->client, ICECAST_ERROR_CON_NO_CONTENT_TYPE_GIVEN);
733 734 735 736 737 738 739 740
                source->client = NULL;
            }
            ICECAST_LOG_ERROR("Content-type not given in PUT request, dropping source");
            return -1;
        } else {
            ICECAST_LOG_ERROR("No content-type header, falling back to backwards compatibility mode "
                    "for icecast 1.x relays. Assuming content is mp3. This behaviour is deprecated "
                    "and the source client will NOT work with future Icecast versions!");
741
            format_type = FORMAT_TYPE_GENERIC;
742 743
        }

Marvin Scholz's avatar
Marvin Scholz committed
744
        if (format_get_plugin (format_type, source) < 0) {
745 746
            global_unlock();
            config_release_config();
Marvin Scholz's avatar
Marvin Scholz committed
747
            if (response) {
748
                client_send_error_by_id(source->client, ICECAST_ERROR_CON_INTERNAL_FORMAT_ALLOC_ERROR);
749 750
                source->client = NULL;
            }
751
            ICECAST_LOG_WARN("plugin format failed for \"%s\"", source->mount);
752 753 754
            return -1;
        }

755
        global.sources++;
Marvin Scholz's avatar
Marvin Scholz committed
756
        stats_event_args(NULL, "sources", "%d", global.sources);
757
        global_unlock();
758

759
        source->running = 1;
Marvin Scholz's avatar
Marvin Scholz committed
760 761
        mountinfo = config_find_mount(config, source->mount, MOUNT_TYPE_NORMAL);
        source_update_settings(config, source, mountinfo);
762
        config_release_config();
763
        slave_rebuild_mounts();
764 765

        source->shutdown_rwlock = &_source_shutdown_rwlock;
766
        ICECAST_LOG_DEBUG("source is ready to start");
767 768 769

        return 0;
    }
770
    ICECAST_LOG_WARN("Request to add source when maximum source limit "
Marvin Scholz's avatar
Marvin Scholz committed
771
        "reached %d", global.sources);
772 773 774 775

    global_unlock();
    config_release_config();

Marvin Scholz's avatar
Marvin Scholz committed
776
    if (response) {
777
        client_send_error_by_id(source->client, ICECAST_ERROR_CON_SOURCE_CLIENT_LIMIT);
778 779
        source->client = NULL;
    }
780 781 782 783

    return -1;
}

Marvin Scholz's avatar
Marvin Scholz committed
784
static inline void source_startup(client_t *client, const char *uri)
785 786
{
    source_t *source;
Marvin Scholz's avatar
Marvin Scholz committed
787
    source = source_reserve(uri);
788

Marvin Scholz's avatar
Marvin Scholz committed
789
    if (source) {
790
        source->client = client;
791 792
        source->parser = client->parser;
        source->con = client->con;
Marvin Scholz's avatar
Marvin Scholz committed
793 794 795
        if (connection_complete_source(source, 1) < 0) {
            source_clear_source(source);
            source_free_source(source);
796 797 798
            return;
        }
        client->respcode = 200;
Philipp Schafft's avatar
Philipp Schafft committed
799 800 801 802
        if (client->protocol == ICECAST_PROTOCOL_SHOUTCAST) {
            client->respcode = 200;
            /* send this non-blocking but if there is only a partial write
             * then leave to header timeout */
803
            client_send_bytes(client, "OK2\r\nicy-caps:11\r\n\r\n", 20); /* TODO: Replace Magic Number! */
804
            source->shoutcast_compat = 1;
Marvin Scholz's avatar
Marvin Scholz committed
805
            source_client_callback(client, source);
Philipp Schafft's avatar
Philipp Schafft committed
806
        } else {
Marvin Scholz's avatar
Marvin Scholz committed
807
            refbuf_t *ok = refbuf_new(PER_CLIENT_REFBUF_SIZE);
808
            const char *expectcontinue;
809
            const char *transfer_encoding;
810
            int status_to_send = 200;
811
            ssize_t ret;
812

813 814 815 816
            transfer_encoding = httpp_getvar(source->parser, "transfer-encoding");
            if (transfer_encoding && strcasecmp(transfer_encoding, HTTPP_ENCODING_IDENTITY) != 0) {
                client->encoding = httpp_encoding_new(transfer_encoding);
                if (!client->encoding) {
817
                    client_send_error_by_id(client, ICECAST_ERROR_CON_UNIMPLEMENTED);
818 819 820 821
                    return;
                }
            }

822 823 824 825 826 827 828
            /* For PUT support we check for 100-continue and send back a 100 to stay in spec */
            expectcontinue = httpp_getvar (source->parser, "expect");

            if (expectcontinue != NULL) {
#ifdef HAVE_STRCASESTR
                if (strcasestr (expectcontinue, "100-continue") != NULL)
#else
829
                ICECAST_LOG_WARN("OS doesn't support case insensitive substring checks...");
830 831 832 833 834 835 836
                if (strstr (expectcontinue, "100-continue") != NULL)
#endif
                {
                    status_to_send = 100;
                }
            }

837
            client->respcode = 200;
838 839
            ret = util_http_build_header(ok->data, PER_CLIENT_REFBUF_SIZE, 0, 0, status_to_send, NULL, NULL, NULL, NULL, NULL, client);
            snprintf(ok->data + ret, PER_CLIENT_REFBUF_SIZE - ret, "Content-Length: 0\r\n\r\n");
Marvin Scholz's avatar
Marvin Scholz committed
840
            ok->len = strlen(ok->data);
841
            refbuf_release(client->refbuf);
842
            client->refbuf = ok;
Marvin Scholz's avatar
Marvin Scholz committed
843
            fserve_add_client_callback(client, source_client_callback, source);
844
        }
Marvin Scholz's avatar
Marvin Scholz committed
845
    } else {
846
        client_send_error_by_id(client, ICECAST_ERROR_CON_MOUNT_IN_USE);
847
        ICECAST_LOG_WARN("Mountpoint %s in use", uri);
848
    }
849 850
}

Philipp Schafft's avatar
Philipp Schafft committed
851
/* only called for native icecast source clients */
Marvin Scholz's avatar
Marvin Scholz committed
852
static void _handle_source_request(client_t *client, const char *uri)
Jack Moffitt's avatar
Jack Moffitt committed
853
{
854 855
    ICECAST_LOG_INFO("Source logging in at mountpoint \"%s\" from %s as role %s",
        uri, client->con->ip, client->role);
856

Marvin Scholz's avatar
Marvin Scholz committed
857
    if (uri[0] != '/') {
Philipp Schafft's avatar
Philipp Schafft committed
858
        ICECAST_LOG_WARN("source mountpoint not starting with /");
859
        client_send_error_by_id(client, ICECAST_ERROR_CON_MOUNTPOINT_NOT_STARTING_WITH_SLASH);
860
        return;
861
    }
862

Philipp Schafft's avatar
Philipp Schafft committed
863 864 865 866
    source_startup(client, uri);
}


Marvin Scholz's avatar
Marvin Scholz committed
867
static void _handle_stats_request(client_t *client, char *uri)
Philipp Schafft's avatar
Philipp Schafft committed
868 869 870
{
    stats_event_inc(NULL, "stats_connections");

871
    client->respcode = 200;
872
    snprintf (client->refbuf->data, PER_CLIENT_REFBUF_SIZE,
Marvin Scholz's avatar
Marvin Scholz committed
873 874 875
        "HTTP/1.0 200 OK\r\n\r\n");
    client->refbuf->len = strlen(client->refbuf->data);
    fserve_add_client_callback(client, stats_callback, NULL);
876 877
}

Philipp Schafft's avatar
Philipp Schafft committed
878 879 880
/* if 0 is returned then the client should not be touched, however if -1
 * is returned then the caller is responsible for handling the client
 */
Marvin Scholz's avatar
Marvin Scholz committed
881
static int __add_listener_to_source(source_t *source, client_t *client)
882
{
Philipp Schafft's avatar
Philipp Schafft committed
883
    size_t loop = 10;
884

Marvin Scholz's avatar
Marvin Scholz committed
885
    do {
Philipp Schafft's avatar
Philipp Schafft committed
886
        ICECAST_LOG_DEBUG("max on %s is %ld (cur %lu)", source->mount,
Marvin Scholz's avatar
Marvin Scholz committed
887
            source->max_listeners, source->listeners);
Philipp Schafft's avatar
Philipp Schafft committed
888 889 890 891
        if (source->max_listeners == -1)
            break;
        if (source->listeners < (unsigned long)source->max_listeners)
            break;
892

Marvin Scholz's avatar
Marvin Scholz committed
893
        if (loop && source->fallback_when_full && source->fallback_mount) {
Philipp Schafft's avatar
Philipp Schafft committed
894 895 896
            source_t *next = source_find_mount (source->fallback_mount);
            if (!next) {
                ICECAST_LOG_ERROR("Fallback '%s' for full source '%s' not found",
Marvin Scholz's avatar
Marvin Scholz committed
897
                    source->mount, source->fallback_mount);
Philipp Schafft's avatar
Philipp Schafft committed
898 899
                return -1;
            }
900
            ICECAST_LOG_INFO("stream full, trying %s", next->mount);
Philipp Schafft's avatar
Philipp Schafft committed
901 902 903 904 905 906 907 908 909 910 911
            source = next;
            loop--;
            continue;
        }
        /* now we fail the client */
        return -1;
    } while (1);

    client->write_to_client = format_generic_write_to_client;
    client->check_buffer = format_check_http_buffer;
    client->refbuf->len = PER_CLIENT_REFBUF_SIZE;
Marvin Scholz's avatar
Marvin Scholz committed
912
    memset(client->refbuf->data, 0, PER_CLIENT_REFBUF_SIZE);
Philipp Schafft's avatar
Philipp Schafft committed
913 914

    /* lets add the client to the active list */
Marvin Scholz's avatar
Marvin Scholz committed
915 916 917
    avl_tree_wlock(source->pending_tree);
    avl_insert(source->pending_tree, client);
    avl_tree_unlock(source->pending_tree);
Philipp Schafft's avatar
Philipp Schafft committed
918

Marvin Scholz's avatar
Marvin Scholz committed
919
    if (source->running == 0 && source->on_demand) {
Philipp Schafft's avatar
Philipp Schafft committed
920 921 922
        /* enable on-demand relay to start, wake up the slave thread */
        ICECAST_LOG_DEBUG("kicking off on-demand relay");
        source->on_demand_req = 1;
923
    }
Philipp Schafft's avatar
Philipp Schafft committed
924 925 926
    ICECAST_LOG_DEBUG("Added client to %s", source->mount);
    return 0;
}
927

Philipp Schafft's avatar
Philipp Schafft committed
928 929 930 931 932 933 934 935 936 937 938 939
/* count the number of clients on a mount with same username and same role as the given one */
static inline ssize_t __count_user_role_on_mount (source_t *source, client_t *client) {
    ssize_t ret = 0;
    avl_node *node;

    avl_tree_rlock(source->client_tree);
    node = avl_get_first(source->client_tree);
    while (node) {
        client_t *existing_client = (client_t *)node->key;
        if (existing_client->username && client->username &&
            strcmp(existing_client->username, client->username) == 0 &&
            existing_client->role && client->role &&
Marvin Scholz's avatar
Marvin Scholz committed
940
            strcmp(existing_client->role, client->role) == 0) {
Philipp Schafft's avatar
Philipp Schafft committed
941
            ret++;
Marvin Scholz's avatar
Marvin Scholz committed
942
        }
Philipp Schafft's avatar
Philipp Schafft committed
943
        node = avl_get_next(node);
944
    }
Philipp Schafft's avatar
Philipp Schafft committed
945 946 947 948
    avl_tree_unlock(source->client_tree);

    avl_tree_rlock(source->pending_tree);
    node = avl_get_first(source->pending_tree);
Marvin Scholz's avatar
Marvin Scholz committed
949
    while (node) {
Philipp Schafft's avatar
Philipp Schafft committed
950 951 952 953
        client_t *existing_client = (client_t *)node->key;
        if (existing_client->username && client->username &&
            strcmp(existing_client->username, client->username) == 0 &&
            existing_client->role && client->role &&
Marvin Scholz's avatar
Marvin Scholz committed
954
            strcmp(existing_client->role, client->role) == 0){
Philipp Schafft's avatar
Philipp Schafft committed
955
            ret++;
Marvin Scholz's avatar
Marvin Scholz committed
956
        }
Philipp Schafft's avatar
Philipp Schafft committed
957
        node = avl_get_next(node);
958
    }
Philipp Schafft's avatar
Philipp Schafft committed
959 960 961 962
    avl_tree_unlock(source->pending_tree);
    return ret;
}

Marvin Scholz's avatar
Marvin Scholz committed
963
static void _handle_get_request(client_t *client, char *uri) {
Philipp Schafft's avatar
Philipp Schafft committed
964 965 966 967 968 969 970 971 972
    source_t *source = NULL;

    ICECAST_LOG_DEBUG("Got client %p with URI %H", client, uri);

    /* there are several types of HTTP GET clients
     * media clients, which are looking for a source (eg, URI = /stream.ogg),
     * stats clients, which are looking for /admin/stats.xml and
     * fserve clients, which are looking for static files.
     */
973 974

    stats_event_inc(NULL, "client_connections");
975

Philipp Schafft's avatar
Philipp Schafft committed
976 977 978 979 980 981
    /* this is a web/ request. let's check if we are allowed to do that. */
    if (acl_test_web(client->acl) != ACL_POLICY_ALLOW) {
        /* doesn't seem so, sad client :( */
        if (client->protocol == ICECAST_PROTOCOL_SHOUTCAST) {
            client_destroy(client);
        } else {
982
            client_send_error_by_id(client, ICECAST_ERROR_GEN_CLIENT_NEEDS_TO_AUTHENTICATE);
Philipp Schafft's avatar
Philipp Schafft committed
983 984 985 986
        }
        return;
    }

987 988 989 990 991
    if (client->parser->req_type == httpp_req_options) {
        client_send_204(client);
        return;
    }

Philipp Schafft's avatar
Philipp Schafft committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
    if (util_check_valid_extension(uri) == XSLT_CONTENT) {
        /* If the file exists, then transform it, otherwise, write a 404 */
        ICECAST_LOG_DEBUG("Stats request, sending XSL transformed stats");
        stats_transform_xslt(client, uri);
        return;
    }

    avl_tree_rlock(global.source_tree);
    /* let's see if this is a source or just a random fserve file */
    source = source_find_mount(uri);
    if (source) {
        /* true mount */
        int in_error = 0;
        ssize_t max_connections_per_user = acl_get_max_connections_per_user(client->acl);
        /* check for duplicate_logins */
        if (max_connections_per_user > 0) { /* -1 = not set (-> default=unlimited), 0 = unlimited */
            if (max_connections_per_user <= __count_user_role_on_mount(source, client)) {
1009
                client_send_error_by_id(client, ICECAST_ERROR_CON_PER_CRED_CLIENT_LIMIT);
Philipp Schafft's avatar
Philipp Schafft committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
                in_error = 1;
            }
        }


        /* Set max listening duration in case not already set. */
        if (!in_error && client->con->discon_time == 0) {
            time_t connection_duration = acl_get_max_connection_duration(client->acl);
            if (connection_duration == -1) {
                ice_config_t *config = config_get_config();
                mount_proxy *mount = config_find_mount(config, source->mount, MOUNT_TYPE_NORMAL);
                if (mount && mount->max_listener_duration)
                    connection_duration = mount->max_listener_duration;
                config_release_config();
            }

            if (connection_duration > 0) /* -1 = not set (-> default=unlimited), 0 = unlimited */
                client->con->discon_time = connection_duration + time(NULL);
        }
        if (!in_error && __add_listener_to_source(source, client) == -1) {
1030
            client_send_error_by_id(client, ICECAST_ERROR_CON_rejecting_client_for_whatever_reason);
Philipp Schafft's avatar
Philipp Schafft committed
1031 1032 1033 1034 1035 1036 1037
        }
        avl_tree_unlock(global.source_tree);
    } else {
        /* file */
        avl_tree_unlock(global.source_tree);
        fserve_client_create(client, uri);
    }
1038 1039
}