client.c 17.5 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).
11
 * Copyright 2011-2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
12 13
 */

Jack Moffitt's avatar
Jack Moffitt committed
14
/* client.c
15 16 17 18
 **
 ** client interface implementation
 **
 */
Jack Moffitt's avatar
Jack Moffitt committed
19

20 21 22 23
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
24 25 26
#include <stdlib.h>
#include <string.h>

Marvin Scholz's avatar
Marvin Scholz committed
27 28 29
#include "common/thread/thread.h"
#include "common/avl/avl.h"
#include "common/httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
30

31
#include "global.h"
32
#include "refobject.h"
33
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
34
#include "connection.h"
35
#include "tls.h"
Jack Moffitt's avatar
Jack Moffitt committed
36
#include "refbuf.h"
Karl Heyes's avatar
Karl Heyes committed
37
#include "format.h"
38
#include "stats.h"
39
#include "fserve.h"
40
#include "errors.h"
Jack Moffitt's avatar
Jack Moffitt committed
41 42

#include "client.h"
Philipp Schafft's avatar
Philipp Schafft committed
43
#include "auth.h"
Jack Moffitt's avatar
Jack Moffitt committed
44
#include "logging.h"
45

46
#include "util.h"
47
#include "acl.h"
48

Philipp Schafft's avatar
Philipp Schafft committed
49 50 51
/* for ADMIN_COMMAND_ERROR */
#include "admin.h"

52 53 54
#ifdef _WIN32
#define snprintf _snprintf
#endif
Jack Moffitt's avatar
Jack Moffitt committed
55

56 57 58
#undef CATMODULE
#define CATMODULE "client"

59 60
static inline void client_send_500(client_t *client, const char *message);

61 62 63 64 65
/* create a client_t with the provided connection and parser details. Return
 * 0 on success, -1 if server limit has been reached.  In either case a
 * client_t is returned just in case a message needs to be returned. Should
 * be called with global lock held.
 */
66
int client_create(client_t **c_ptr, connection_t *con, http_parser_t *parser)
Jack Moffitt's avatar
Jack Moffitt committed
67
{
Marvin Scholz's avatar
Marvin Scholz committed
68 69 70
    ice_config_t    *config;
    client_t        *client = (client_t *) calloc(1, sizeof(client_t));
    int              ret    = -1;
71 72

    if (client == NULL)
73
        abort();
74

75
    config = config_get_config();
76 77

    global.clients++;
Marvin Scholz's avatar
Marvin Scholz committed
78
    if (config->client_limit < global.clients) {
79
        ICECAST_LOG_WARN("server client limit reached (%d/%d)", config->client_limit, global.clients);
Marvin Scholz's avatar
Marvin Scholz committed
80
    } else {
81
        ret = 0;
Marvin Scholz's avatar
Marvin Scholz committed
82
    }
Jack Moffitt's avatar
Jack Moffitt committed
83

84 85 86
    config_release_config ();

    stats_event_args (NULL, "clients", "%d", global.clients);
87 88
    client->con = con;
    client->parser = parser;
Philipp Schafft's avatar
Philipp Schafft committed
89
    client->protocol = ICECAST_PROTOCOL_HTTP;
90 91
    client->request_body_length = 0;
    client->request_body_read = 0;
Philipp Schafft's avatar
Philipp Schafft committed
92
    client->admin_command = ADMIN_COMMAND_ERROR;
93 94
    client->refbuf = refbuf_new (PER_CLIENT_REFBUF_SIZE);
    client->refbuf->len = 0; /* force reader code to ignore buffer contents */
95
    client->pos = 0;
96
    client->write_to_client = format_generic_write_to_client;
97
    *c_ptr = client;
Jack Moffitt's avatar
Jack Moffitt committed
98

99
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
100 101
}

102 103 104 105 106 107 108 109 110 111 112
static inline void client_reuseconnection(client_t *client) {
    connection_t *con;
    reuse_t reuse;

    if (!client)
        return;

    con = client->con;
    con = connection_create(con->sock, con->serversock, strdup(con->ip));
    reuse = client->reuse;
    client->con->sock = -1; /* TODO: do not use magic */
113 114

    /* handle to keep the TLS connection */
115
    if (client->con->tls) {
116
        /* AHhhggrr.. That pain....
117
         * stealing TLS state...
118
         */
119
        con->tls  = client->con->tls;
120 121
        con->read = client->con->read;
        con->send = client->con->send;
122
        client->con->tls  = NULL;
123 124 125 126
        client->con->read = NULL;
        client->con->send = NULL;
    }

127 128 129 130 131 132 133 134 135 136
    if (client->con->readbufferlen) {
        /* Aend... moorre paaiin.
         * stealing putback buffer.
         */
        con->readbuffer = client->con->readbuffer;
        con->readbufferlen = client->con->readbufferlen;
        client->con->readbuffer = NULL;
        client->con->readbufferlen = 0;
    }

137 138 139 140
    client->reuse = ICECAST_REUSE_CLOSE;

    client_destroy(client);

141
    if (reuse == ICECAST_REUSE_UPGRADETLS)
142
        connection_uses_tls(con);
143 144 145
    connection_queue(con);
}

Jack Moffitt's avatar
Jack Moffitt committed
146 147
void client_destroy(client_t *client)
{
Philipp Schafft's avatar
Philipp Schafft committed
148
    ICECAST_LOG_DEBUG("Called to destory client %p", client);
Karl Heyes's avatar
Karl Heyes committed
149 150
    if (client == NULL)
        return;
151

152
    if (client->reuse != ICECAST_REUSE_CLOSE) {
153 154 155 156 157
        /* only reuse the client if we reached the body's EOF. */
        if (client_body_eof(client) == 1) {
            client_reuseconnection(client);
            return;
        }
158 159
    }

160 161
    /* release the buffer now, as the buffer could be on the source queue
     * and may of disappeared after auth completes */
Marvin Scholz's avatar
Marvin Scholz committed
162
    if (client->refbuf) {
163 164 165 166
        refbuf_release (client->refbuf);
        client->refbuf = NULL;
    }

Philipp Schafft's avatar
Philipp Schafft committed
167
    if (auth_release_client(client))
168 169
        return;

170
    /* write log entry if ip is set (some things don't set it, like outgoing
171 172
     * slave requests
     */
Karl Heyes's avatar
Karl Heyes committed
173
    if (client->respcode && client->parser)
174
        logging_access(client);
175 176
    if (client->con)
        connection_close(client->con);
Karl Heyes's avatar
Karl Heyes committed
177 178
    if (client->parser)
        httpp_destroy(client->parser);
179 180
    if (client->encoding)
        httpp_encoding_release(client->encoding);
Jack Moffitt's avatar
Jack Moffitt committed
181

182
    global_lock();
183
    global.clients--;
184 185
    stats_event_args(NULL, "clients", "%d", global.clients);
    global_unlock();
186

187 188
    /* we need to free client specific format data (if any) */
    if (client->free_client_data)
189
        client->free_client_data(client);
190

191 192
    refobject_unref(client->handler_module);
    free(client->handler_function);
Michael Smith's avatar
Michael Smith committed
193
    free(client->username);
Karl Heyes's avatar
Karl Heyes committed
194
    free(client->password);
Philipp Schafft's avatar
Philipp Schafft committed
195 196
    free(client->role);
    acl_release(client->acl);
Michael Smith's avatar
Michael Smith committed
197

198
    free(client);
Jack Moffitt's avatar
Jack Moffitt committed
199
}
200

201
/* helper function for reading data from a client */
202 203 204 205 206 207 208 209 210 211 212 213 214 215
static ssize_t __client_read_bytes_real(client_t *client, void *buf, size_t len)
{
    /* we have data to read from a refbuf first */
    if (client->refbuf->len < len)
        len = client->refbuf->len;
    memcpy (buf, client->refbuf->data, len);
    if (len < client->refbuf->len) {
        char *ptr = client->refbuf->data;
        memmove (ptr, ptr+len, client->refbuf->len - len);
    }
    client->refbuf->len -= len;
    return len;
}

216
int client_read_bytes(client_t *client, void *buf, unsigned len)
217
{
218 219
    ssize_t (*reader)(void*, void*, size_t) = (ssize_t(*)(void*,void*,size_t))__client_read_bytes_real;
    void *userdata = client;
220
    int bytes;
221

222 223 224 225 226 227 228 229 230
    if (!(client->refbuf && client->refbuf->len)) {
        reader = (ssize_t(*)(void*,void*,size_t))connection_read_bytes;
        userdata = client->con;
    }

    if (client->encoding) {
        bytes = httpp_encoding_read(client->encoding, buf, len, reader, userdata);
    } else {
        bytes = reader(userdata, buf, len);
231
    }
232

233
    if (bytes == -1 && client->con->error)
234
        ICECAST_LOG_DEBUG("reading from connection has failed");
235 236

    return bytes;
237 238
}

239
static inline void _client_send_error(client_t *client, int plain, const icecast_error_t *error)
240
{
241
    ssize_t ret;
242
    refbuf_t *data;
243

244 245
    if (error->http_status == 500) {
         client_send_500(client, error->message);
246 247 248
         return;
    }

249 250
    data = refbuf_new(PER_CLIENT_REFBUF_SIZE);
    if (!data) {
251
         client_send_500(client, error->message);
252 253 254
         return;
    }

255 256
    client->reuse = ICECAST_REUSE_KEEPALIVE;

257
    ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
258
                                 0, error->http_status, NULL,
259
                                 plain ? "text/plain" : "text/html", "utf-8",
260
                                 NULL, NULL, client);
261

262 263 264 265 266 267
    if (ret == -1 || ret >= PER_CLIENT_REFBUF_SIZE) {
        ICECAST_LOG_ERROR("Dropping client as we can not build response headers.");
        client_send_500(client, "Header generation failed.");
        return;
    }

268
    if (plain) {
269 270 271
        snprintf(data->data, data->len, "Error %i\r\n---------\r\n\r\nMessage: %s\r\n\r\nError code: %s\r\n",
                 error->http_status, error->message, error->uuid
                );
272 273
    } else {
        snprintf(data->data, data->len,
274 275
                 "<html><head><title>Error %i</title></head><body><h1>Error %i</h1><hr><p><b>%s</b></p><p>Error code: %s</p></body></html>\r\n",
                 error->http_status, error->http_status, error->message, error->uuid);
276 277 278 279
    }
    data->len = strlen(data->data);

    snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
280
             "Content-Length: %llu\r\n\r\n",
281
             (long long unsigned int)data->len);
282

283
    client->respcode = error->http_status;
284
    client->refbuf->len = strlen (client->refbuf->data);
285 286
    client->refbuf->next = data;

287
    fserve_add_client (client, NULL);
288 289
}

290
void client_send_error_by_id(client_t *client, icecast_error_id_t id)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
{
    const icecast_error_t *error = error_get_by_id(id);
    const char *pref;
    int plain;

    if (!error) {
         client_send_500(client, "Unknown error ID");
         return;
    }

    pref = util_http_select_best(httpp_getvar(client->parser, "accept"), "text/plain", "text/html", (const char*)NULL);

    if (strcmp(pref, "text/plain") == 0) {
        plain = 1;
    } else if (strcmp(pref, "text/html") == 0) {
        plain = 0;
    } else {
        plain = 1;
    }

311
    _client_send_error(client, plain, error);
312 313
}

314 315 316 317 318 319 320 321 322 323 324 325
void client_send_101(client_t *client, reuse_t reuse)
{
    ssize_t ret;

    if (!client)
        return;

    if (reuse != ICECAST_REUSE_UPGRADETLS) {
        client_send_500(client, "Bad reuse parameter");
        return;
    }

326 327
    client->reuse = reuse;

328 329 330 331 332 333
    ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
                                 0, 101, NULL,
                                 "text/plain", "utf-8",
                                 NULL, NULL, client);

    snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
334
             "Content-Length: 0\r\nUpgrade: TLS/1.0, HTTP/1.0\r\n\r\n");
335 336 337 338 339 340 341

    client->respcode = 101;
    client->refbuf->len = strlen(client->refbuf->data);

    fserve_add_client(client, NULL);
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
void client_send_204(client_t *client)
{
    ssize_t ret;

    if (!client)
        return;

    client->reuse = ICECAST_REUSE_KEEPALIVE;

    ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
                                 0, 204, NULL,
                                 NULL, NULL,
                                 NULL, NULL, client);

    snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
             "Content-Length: 0\r\n\r\n");

    client->respcode = 204;
    client->refbuf->len = strlen(client->refbuf->data);

    fserve_add_client(client, NULL);
}

365 366 367 368 369 370 371 372 373 374 375 376
void client_send_426(client_t *client, reuse_t reuse)
{
    ssize_t ret;

    if (!client)
        return;

    if (reuse != ICECAST_REUSE_UPGRADETLS) {
        client_send_500(client, "Bad reuse parameter");
        return;
    }

377 378
    client->reuse = reuse;

379 380 381 382 383 384
    ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
                                 0, 426, NULL,
                                 "text/plain", "utf-8",
                                 NULL, NULL, client);

    snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
385
             "Content-Length: 0\r\nUpgrade: TLS/1.0, HTTP/1.0\r\n\r\n");
386 387 388 389 390 391 392 393 394

    client->respcode = 426;
    client->refbuf->len = strlen(client->refbuf->data);

    client->reuse = ICECAST_REUSE_KEEPALIVE;

    fserve_add_client(client, NULL);
}

395
/* this function is designed to work even if client is in bad state */
Marvin Scholz's avatar
Marvin Scholz committed
396 397
static inline void client_send_500(client_t *client, const char *message)
{
398 399
    const char header[] = "HTTP/1.0 500 Internal Server Error\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n"
                          "500 - Internal Server Error\n---------------------------\n";
Philipp Schafft's avatar
Philipp Schafft committed
400 401
    const ssize_t header_len = sizeof(header) - 1;
    ssize_t ret;
402 403 404 405 406 407 408 409 410

    ret = client_send_bytes(client, header, header_len);

    /* only send message if we have one AND if header could have transmitted completly */
    if (message && ret == header_len)
        client_send_bytes(client, message, strlen(message));

    client_destroy(client);
}
411

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
admin_format_t client_get_admin_format_by_content_negotiation(client_t *client)
{
    const char *pref;

    if (!client || !client->parser)
        return CLIENT_DEFAULT_ADMIN_FORMAT;

    pref = util_http_select_best(httpp_getvar(client->parser, "accept"), "text/xml", "text/html", "text/plain", (const char*)NULL);

    if (strcmp(pref, "text/xml") == 0) {
        return ADMIN_FORMAT_RAW;
    } else if (strcmp(pref, "text/html") == 0) {
        return ADMIN_FORMAT_TRANSFORMED;
    } else if (strcmp(pref, "text/plain") == 0) {
        return ADMIN_FORMAT_PLAINTEXT;
    } else {
        return CLIENT_DEFAULT_ADMIN_FORMAT;
    }
}

432
/* helper function for sending the data to a client */
433
int client_send_bytes(client_t *client, const void *buf, unsigned len)
434
{
435
    int ret = client->con->send(client->con, buf, len);
436 437

    if (client->con->error)
438
        ICECAST_LOG_DEBUG("Client connection died");
439

440 441 442
    return ret;
}

443
void client_set_queue(client_t *client, refbuf_t *refbuf)
Karl Heyes's avatar
Karl Heyes committed
444 445 446 447
{
    refbuf_t *to_release = client->refbuf;

    client->refbuf = refbuf;
Karl Heyes's avatar
Karl Heyes committed
448
    if (refbuf)
449
        refbuf_addref(client->refbuf);
Karl Heyes's avatar
Karl Heyes committed
450 451
    client->pos = 0;
    if (to_release)
452
        refbuf_release(to_release);
Karl Heyes's avatar
Karl Heyes committed
453
}
454 455 456

ssize_t client_body_read(client_t *client, void *buf, size_t len)
{
457 458
    ssize_t ret;

459
    ICECAST_LOG_DEBUG("Reading from body (client=%p)", client);
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

    if (client->request_body_length != -1) {
        size_t left = (size_t)client->request_body_length - client->request_body_read;
        if (len > left) {
            ICECAST_LOG_DEBUG("Limiting read request to left over body size: left %zu byte, requested %zu byte", left, len);
            len = left;
        }
    }

    ret = client_read_bytes(client, buf, len);

    if (ret > 0) {
        client->request_body_read += ret;
    }

    return ret;
476 477
}

478 479
/* we might un-static this if needed at some time in distant future. -- ph3-der-loewe, 2018-04-17 */
static int client_eof(client_t *client)
480
{
481 482 483
    if (!client)
        return -1;

484 485 486 487 488 489 490 491 492 493 494
    if (!client->con)
        return 0;

    if (client->con->tls && tls_got_shutdown(client->con->tls) > 1)
        client->con->error = 1;

    if (client->con->error)
        return 1;

    return 0;
}
495 496 497 498 499 500 501 502

int client_body_eof(client_t *client)
{
    int ret = -1;

    if (!client)
        return -1;

503 504 505 506
    if (client->request_body_length != -1 && client->request_body_read == (size_t)client->request_body_length) {
        ICECAST_LOG_DEBUG("Reached given body length (client=%p)", client);
        ret = 1;
    } else if (client->encoding) {
507 508 509 510 511 512 513 514 515 516
        ICECAST_LOG_DEBUG("Looking for body EOF with encoding (client=%p)", client);
        ret = httpp_encoding_eof(client->encoding, (int(*)(void*))client_eof, client);
    } else {
        ICECAST_LOG_DEBUG("Looking for body EOF without encoding (client=%p)", client);
        ret = client_eof(client);
    }

    ICECAST_LOG_DEBUG("... result is: %i (client=%p)", ret, client);
    return ret;
}
517 518 519 520 521 522 523 524 525 526 527 528 529

client_slurp_result_t client_body_slurp(client_t *client, void *buf, size_t *len)
{
    if (!client || !buf || !len)
        return CLIENT_SLURP_ERROR;

    if (client->request_body_length != -1) {
        /* non-streaming mode */
        size_t left = (size_t)client->request_body_length - client->request_body_read;

        if (!left)
            return CLIENT_SLURP_SUCCESS;

530
        if (*len < (size_t)client->request_body_length)
531 532 533 534 535 536 537
            return CLIENT_SLURP_BUFFER_TO_SMALL;

        if (left > 2048)
            left = 2048;

        client_body_read(client, buf + client->request_body_read, left);

538
        if ((size_t)client->request_body_length == client->request_body_read) {
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
            *len = client->request_body_read;

            return CLIENT_SLURP_SUCCESS;
        } else {
            return CLIENT_SLURP_NEEDS_MORE_DATA;
        }
    } else {
        /* streaming mode */
        size_t left = *len - client->request_body_read;
        int ret;

        if (left) {
            if (left > 2048)
                left = 2048;

            client_body_read(client, buf + client->request_body_read, left);
        }

        ret = client_body_eof(client);
        switch (ret) {
            case 0:
                if (*len == client->request_body_read) {
                    return CLIENT_SLURP_BUFFER_TO_SMALL;
                }
                return CLIENT_SLURP_NEEDS_MORE_DATA;
            break;
            case 1:
                return CLIENT_SLURP_SUCCESS;
            break;
            default:
                return CLIENT_SLURP_ERROR;
            break;
        }
    }
}

client_slurp_result_t client_body_skip(client_t *client)
{
    char buf[2048];
    int ret;

580 581 582 583
    ICECAST_LOG_DEBUG("Slurping client %p");

    if (!client) {
        ICECAST_LOG_DEBUG("Slurping client %p ... failed");
584
        return CLIENT_SLURP_ERROR;
585
    }
586 587 588 589

    if (client->request_body_length != -1) {
        size_t left = (size_t)client->request_body_length - client->request_body_read;

590 591
        if (!left) {
            ICECAST_LOG_DEBUG("Slurping client %p ... was a success");
592
            return CLIENT_SLURP_SUCCESS;
593
        }
594 595 596 597 598 599

        if (left > sizeof(buf))
            left = sizeof(buf);

        client_body_read(client, buf, left);

600
        if ((size_t)client->request_body_length == client->request_body_read) {
601
            ICECAST_LOG_DEBUG("Slurping client %p ... was a success");
602 603
            return CLIENT_SLURP_SUCCESS;
        } else {
604
            ICECAST_LOG_DEBUG("Slurping client %p ... needs more data");
605 606 607 608 609 610 611 612 613
            return CLIENT_SLURP_NEEDS_MORE_DATA;
        }
    } else {
        client_body_read(client, buf, sizeof(buf));
    }

    ret = client_body_eof(client);
    switch (ret) {
        case 0:
614
            ICECAST_LOG_DEBUG("Slurping client %p ... needs more data");
615 616 617
            return CLIENT_SLURP_NEEDS_MORE_DATA;
        break;
        case 1:
618
            ICECAST_LOG_DEBUG("Slurping client %p ... was a success");
619 620 621
            return CLIENT_SLURP_SUCCESS;
        break;
        default:
622
            ICECAST_LOG_DEBUG("Slurping client %p ... failed");
623 624 625 626
            return CLIENT_SLURP_ERROR;
        break;
    }
}