client.c 10.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).
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 "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
33 34
#include "connection.h"
#include "refbuf.h"
Karl Heyes's avatar
Karl Heyes committed
35
#include "format.h"
36
#include "stats.h"
37
#include "fserve.h"
38
#include "errors.h"
Jack Moffitt's avatar
Jack Moffitt committed
39 40

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

44 45
#include "util.h"

Philipp Schafft's avatar
Philipp Schafft committed
46 47 48
/* for ADMIN_COMMAND_ERROR */
#include "admin.h"

49 50 51
#ifdef _WIN32
#define snprintf _snprintf
#endif
Jack Moffitt's avatar
Jack Moffitt committed
52

53 54 55
#undef CATMODULE
#define CATMODULE "client"

56 57
static inline void client_send_500(client_t *client, const char *message);

58 59 60 61 62
/* 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.
 */
63
int client_create(client_t **c_ptr, connection_t *con, http_parser_t *parser)
Jack Moffitt's avatar
Jack Moffitt committed
64
{
Marvin Scholz's avatar
Marvin Scholz committed
65 66 67
    ice_config_t    *config;
    client_t        *client = (client_t *) calloc(1, sizeof(client_t));
    int              ret    = -1;
68 69

    if (client == NULL)
70
        abort();
71

72
    config = config_get_config();
73 74

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

81 82 83
    config_release_config ();

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

94
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
95 96
}

97 98 99 100 101 102 103 104 105 106 107
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 */
108 109

    /* handle to keep the TLS connection */
110
    if (client->con->tls) {
111
        /* AHhhggrr.. That pain....
112
         * stealing TLS state...
113
         */
114
        con->tls  = client->con->tls;
115 116
        con->read = client->con->read;
        con->send = client->con->send;
117
        client->con->tls  = NULL;
118 119 120 121
        client->con->read = NULL;
        client->con->send = NULL;
    }

122 123 124 125
    client->reuse = ICECAST_REUSE_CLOSE;

    client_destroy(client);

126
    if (reuse == ICECAST_REUSE_UPGRADETLS)
127
        connection_uses_tls(con);
128 129 130
    connection_queue(con);
}

Jack Moffitt's avatar
Jack Moffitt committed
131 132
void client_destroy(client_t *client)
{
Philipp Schafft's avatar
Philipp Schafft committed
133
    ICECAST_LOG_DEBUG("Called to destory client %p", client);
Karl Heyes's avatar
Karl Heyes committed
134 135
    if (client == NULL)
        return;
136

137 138 139 140 141
    if (client->reuse != ICECAST_REUSE_CLOSE) {
        client_reuseconnection(client);
        return;
    }

142 143
    /* 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
144
    if (client->refbuf) {
145 146 147 148
        refbuf_release (client->refbuf);
        client->refbuf = NULL;
    }

Philipp Schafft's avatar
Philipp Schafft committed
149
    if (auth_release_client(client))
150 151
        return;

152
    /* write log entry if ip is set (some things don't set it, like outgoing
153 154
     * slave requests
     */
Karl Heyes's avatar
Karl Heyes committed
155
    if (client->respcode && client->parser)
156
        logging_access(client);
157 158
    if (client->con)
        connection_close(client->con);
Karl Heyes's avatar
Karl Heyes committed
159 160
    if (client->parser)
        httpp_destroy(client->parser);
161 162
    if (client->encoding)
        httpp_encoding_release(client->encoding);
Jack Moffitt's avatar
Jack Moffitt committed
163

164
    global_lock();
165
    global.clients--;
166 167
    stats_event_args(NULL, "clients", "%d", global.clients);
    global_unlock();
168

169 170
    /* we need to free client specific format data (if any) */
    if (client->free_client_data)
171
        client->free_client_data(client);
172

Michael Smith's avatar
Michael Smith committed
173
    free(client->username);
Karl Heyes's avatar
Karl Heyes committed
174
    free(client->password);
Philipp Schafft's avatar
Philipp Schafft committed
175 176
    free(client->role);
    acl_release(client->acl);
Michael Smith's avatar
Michael Smith committed
177

178
    free(client);
Jack Moffitt's avatar
Jack Moffitt committed
179
}
180

181
/* helper function for reading data from a client */
182 183 184 185 186 187 188 189 190 191 192 193 194 195
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;
}

196
int client_read_bytes(client_t *client, void *buf, unsigned len)
197
{
198 199
    ssize_t (*reader)(void*, void*, size_t) = (ssize_t(*)(void*,void*,size_t))__client_read_bytes_real;
    void *userdata = client;
200
    int bytes;
201

202 203 204 205 206 207 208 209 210
    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);
211
    }
212

213
    if (bytes == -1 && client->con->error)
214
        ICECAST_LOG_DEBUG("reading from connection has failed");
215 216

    return bytes;
217 218
}

219
static inline void _client_send_error(client_t *client, int plain, const icecast_error_t *error)
220
{
221
    ssize_t ret;
222
    refbuf_t *data;
223

224 225
    if (error->http_status == 500) {
         client_send_500(client, error->message);
226 227 228
         return;
    }

229 230
    data = refbuf_new(PER_CLIENT_REFBUF_SIZE);
    if (!data) {
231
         client_send_500(client, error->message);
232 233 234
         return;
    }

235 236
    client->reuse = ICECAST_REUSE_KEEPALIVE;

237
    ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
238
                                 0, error->http_status, NULL,
239
                                 plain ? "text/plain" : "text/html", "utf-8",
240
                                 NULL, NULL, client);
241

242 243 244 245 246 247
    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;
    }

248
    if (plain) {
249 250 251
        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
                );
252 253
    } else {
        snprintf(data->data, data->len,
254 255
                 "<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);
256 257 258 259
    }
    data->len = strlen(data->data);

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

263
    client->respcode = error->http_status;
264
    client->refbuf->len = strlen (client->refbuf->data);
265 266
    client->refbuf->next = data;

267
    fserve_add_client (client, NULL);
268 269
}

270
void client_send_error_by_id(client_t *client, icecast_error_id_t id)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
{
    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;
    }

291
    _client_send_error(client, plain, error);
292 293
}

294 295 296 297 298 299 300 301 302 303 304 305
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;
    }

306 307
    client->reuse = reuse;

308 309 310 311 312 313
    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,
314
             "Content-Length: 0\r\nUpgrade: TLS/1.0, HTTP/1.0\r\n\r\n");
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

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

    fserve_add_client(client, NULL);
}

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;
    }

334 335
    client->reuse = reuse;

336 337 338 339 340 341
    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,
342
             "Content-Length: 0\r\nUpgrade: TLS/1.0, HTTP/1.0\r\n\r\n");
343 344 345 346 347 348 349 350 351

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

    client->reuse = ICECAST_REUSE_KEEPALIVE;

    fserve_add_client(client, NULL);
}

352
/* this function is designed to work even if client is in bad state */
Marvin Scholz's avatar
Marvin Scholz committed
353 354
static inline void client_send_500(client_t *client, const char *message)
{
355 356
    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
357 358
    const ssize_t header_len = sizeof(header) - 1;
    ssize_t ret;
359 360 361 362 363 364 365 366 367

    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);
}
368 369

/* helper function for sending the data to a client */
370
int client_send_bytes(client_t *client, const void *buf, unsigned len)
371
{
372
    int ret = client->con->send(client->con, buf, len);
373 374

    if (client->con->error)
375
        ICECAST_LOG_DEBUG("Client connection died");
376

377 378 379
    return ret;
}

380
void client_set_queue(client_t *client, refbuf_t *refbuf)
Karl Heyes's avatar
Karl Heyes committed
381 382 383 384
{
    refbuf_t *to_release = client->refbuf;

    client->refbuf = refbuf;
Karl Heyes's avatar
Karl Heyes committed
385
    if (refbuf)
386
        refbuf_addref(client->refbuf);
Karl Heyes's avatar
Karl Heyes committed
387 388
    client->pos = 0;
    if (to_release)
389
        refbuf_release(to_release);
Karl Heyes's avatar
Karl Heyes committed
390
}