auth.c 18.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org, 
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
 */

Michael Smith's avatar
Michael Smith committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26
/** 
 * Client authentication functions
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

#include "auth.h"
27
#include "auth_htpasswd.h"
Karl Heyes's avatar
Karl Heyes committed
28
#include "auth_url.h"
Michael Smith's avatar
Michael Smith committed
29 30 31
#include "source.h"
#include "client.h"
#include "cfgfile.h"
32
#include "stats.h"
Michael Smith's avatar
Michael Smith committed
33
#include "httpp/httpp.h"
34
#include "fserve.h"
Michael Smith's avatar
Michael Smith committed
35 36 37

#include "logging.h"
#define CATMODULE "auth"
38

39 40 41 42

static mutex_t auth_lock;


43
static auth_client *auth_client_setup (const char *mount, client_t *client)
44
{
45
    /* This will look something like "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" */
46
    const char *header = httpp_getvar(client->parser, "authorization");
47 48
    char *userpass, *tmp;
    char *username, *password;
49
    auth_client *auth_user;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    do
    {
        if (header == NULL)
            break;

        if (strncmp(header, "Basic ", 6) == 0)
        {
            userpass = util_base64_decode (header+6);
            if (userpass == NULL)
            {
                WARN1("Base64 decode of Authorization header \"%s\" failed",
                        header+6);
                break;
            }

            tmp = strchr(userpass, ':');
            if (tmp == NULL)
            { 
                free (userpass);
                break;
71
            }
72 73 74 75 76 77 78 79

            *tmp = 0;
            username = userpass;
            password = tmp+1;
            client->username = strdup (username);
            client->password = strdup (password);
            free (userpass);
            break;
80
        }
81
        INFO1 ("unhandled authorization header: %s", header);
82

83
    } while (0);
84

85 86 87 88
    auth_user = calloc (1, sizeof(auth_client));
    auth_user->mount = strdup (mount);
    auth_user->client = client;
    return auth_user;
89 90
}

Michael Smith's avatar
Michael Smith committed
91

92
static void queue_auth_client (auth_client *auth_user, mount_proxy *mountinfo)
93
{
94 95
    auth_t *auth;

96 97
    if (auth_user == NULL || (mountinfo == NULL && auth_user->client
                && auth_user->client->auth == NULL))
98 99
        return;
    auth_user->next = NULL;
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    if (mountinfo)
    {
        auth = mountinfo->auth;
        thread_mutex_lock (&auth->lock);
        if (auth_user->client)
            auth_user->client->auth = auth;
        auth->refcount++;
    }
    else
    {
        auth = auth_user->client->auth;
        thread_mutex_lock (&auth->lock);
    }
    DEBUG2 ("...refcount on auth_t %s is now %d", auth->mount, auth->refcount);
114 115 116 117 118
    *auth->tailp = auth_user;
    auth->tailp = &auth_user->next;
    auth->pending_count++;
    INFO2 ("auth on %s has %d pending", auth->mount, auth->pending_count);
    thread_mutex_unlock (&auth->lock);
119
}
Michael Smith's avatar
Michael Smith committed
120 121


122 123 124 125 126 127 128
/* release the auth. It is referred to by multiple structures so this is
 * refcounted and only actual freed after the last use
 */
void auth_release (auth_t *authenticator)
{
    if (authenticator == NULL)
        return;
Michael Smith's avatar
Michael Smith committed
129

130
    thread_mutex_lock (&authenticator->lock);
131
    authenticator->refcount--;
132
    DEBUG2 ("...refcount on auth_t %s is now %d", authenticator->mount, authenticator->refcount);
133
    if (authenticator->refcount)
134 135
    {
        thread_mutex_unlock (&authenticator->lock);
136
        return;
137
    }
Michael Smith's avatar
Michael Smith committed
138

139 140 141 142
    /* cleanup auth thread attached to this auth */
    authenticator->running = 0;
    thread_join (authenticator->thread);

143 144
    if (authenticator->free)
        authenticator->free (authenticator);
145
    xmlFree (authenticator->type);
146 147
    thread_mutex_unlock (&authenticator->lock);
    thread_mutex_destroy (&authenticator->lock);
148
    free (authenticator->mount);
149
    free (authenticator);
Michael Smith's avatar
Michael Smith committed
150 151 152
}


153
static void auth_client_free (auth_client *auth_user)
Michael Smith's avatar
Michael Smith committed
154
{
155 156 157 158 159
    if (auth_user == NULL)
        return;
    if (auth_user->client)
    {
        client_t *client = auth_user->client;
Michael Smith's avatar
Michael Smith committed
160

161 162 163 164 165 166 167 168
        if (client->respcode)
            client_destroy (client);
        else
            client_send_401 (client);
        auth_user->client = NULL;
    }
    free (auth_user->mount);
    free (auth_user);
Michael Smith's avatar
Michael Smith committed
169 170 171
}


172 173 174 175 176 177 178 179 180 181 182 183 184
/* verify that the listener is still connected. */
static int is_listener_connected (client_t *client)
{
    int ret = 1;
    if (client)
    {
        if (sock_active (client->con->sock) == 0)
            ret = 0;
    }
    return ret;
}


185 186 187
/* wrapper function for auth thread to authenticate new listener
 * connection details
 */
188
static void auth_new_listener (auth_t *auth, auth_client *auth_user)
Michael Smith's avatar
Michael Smith committed
189
{
190 191
    client_t *client = auth_user->client;

192 193 194 195 196 197 198 199
    /* make sure there is still a client at this point, a slow backend request
     * can be avoided if client has disconnected */
    if (is_listener_connected (client) == 0)
    {
        DEBUG0 ("listener is no longer connected");
        client->respcode = 400;
        return;
    }
200
    if (auth->authenticate)
201
    {
202
        if (auth->authenticate (auth_user) != AUTH_OK)
203
            return;
Michael Smith's avatar
Michael Smith committed
204
    }
205
    if (auth_postprocess_listener (auth_user) < 0)
206
        INFO1 ("client %lu failed", client->con->id);
Michael Smith's avatar
Michael Smith committed
207 208
}

209

210
/* wrapper function for auth thread to drop listener connections
211
 */
212
static void auth_remove_listener (auth_t *auth, auth_client *auth_user)
Michael Smith's avatar
Michael Smith committed
213
{
214 215
    client_t *client = auth_user->client;

216 217
    if (client->auth->release_listener)
        client->auth->release_listener (auth_user);
218 219
    auth_release (client->auth);
    client->auth = NULL;
220 221 222 223 224 225 226 227
    /* client is going, so auth is not an issue at this point */
    client->authenticated = 0;
}


/* Callback from auth thread to handle a stream start event, this applies
 * to both source clients and relays.
 */
228
static void stream_start_callback (auth_t *auth, auth_client *auth_user)
229 230 231
{
    if (auth->stream_start)
        auth->stream_start (auth_user);
232
    auth_release (auth);
233 234 235 236 237 238
}


/* Callback from auth thread to handle a stream start event, this applies
 * to both source clients and relays.
 */
239
static void stream_end_callback (auth_t *auth, auth_client *auth_user)
240 241 242
{
    if (auth->stream_end)
        auth->stream_end (auth_user);
243
    auth_release (auth);
244
}
Michael Smith's avatar
Michael Smith committed
245 246


247 248 249
/* The auth thread main loop. */
static void *auth_run_thread (void *arg)
{
250 251
    auth_t *auth = arg;

252
    INFO0 ("Authentication thread started");
253
    while (auth->running)
254
    {
255 256
        /* usually no clients are waiting, so don't bother taking locks */
        if (auth->head)
257 258
        {
            auth_client *auth_user;
Michael Smith's avatar
Michael Smith committed
259

260 261 262 263 264 265 266 267 268 269 270 271 272 273
            /* may become NULL before lock taken */
            thread_mutex_lock (&auth->lock);
            auth_user = (auth_client*)auth->head;
            if (auth_user == NULL)
            {
                thread_mutex_unlock (&auth->lock);
                continue;
            }
            DEBUG2 ("%d client(s) pending on %s", auth->pending_count, auth->mount);
            auth->head = auth_user->next;
            if (auth->head == NULL)
                auth->tailp = &auth->head;
            auth->pending_count--;
            thread_mutex_unlock (&auth->lock);
274
            auth_user->next = NULL;
Michael Smith's avatar
Michael Smith committed
275

276
            if (auth_user->process)
277
                auth_user->process (auth, auth_user);
278 279
            else
                ERROR0 ("client auth process not set");
Michael Smith's avatar
Michael Smith committed
280

281
            auth_client_free (auth_user);
Michael Smith's avatar
Michael Smith committed
282

283
            continue;
284
        }
285
        thread_sleep (150000);
286
    }
287 288 289
    INFO0 ("Authenication thread shutting down");
    return NULL;
}
Michael Smith's avatar
Michael Smith committed
290 291


292 293 294 295
/* Check whether this client is currently on this mount, the client may be
 * on either the active or pending lists.
 * return 1 if ok to add or 0 to prevent
 */
296
static int check_duplicate_logins (source_t *source, client_t *client, auth_t *auth)
297 298 299 300
{
    /* allow multiple authenticated relays */
    if (client->username == NULL)
        return 1;
Michael Smith's avatar
Michael Smith committed
301

302 303 304 305 306 307 308 309
    if (auth && auth->allow_duplicate_users == 0)
    {
        avl_node *node;

        avl_tree_rlock (source->client_tree);
        node = avl_get_first (source->client_tree);
        while (node)
        {   
310 311 312
            client_t *existing_client = (client_t *)node->key;
            if (existing_client->username && 
                    strcmp (existing_client->username, client->username) == 0)
313 314 315 316 317 318 319 320 321 322 323 324
            {
                avl_tree_unlock (source->client_tree);
                return 0;
            }
            node = avl_get_next (node);
        }       
        avl_tree_unlock (source->client_tree);

        avl_tree_rlock (source->pending_tree);
        node = avl_get_first (source->pending_tree);
        while (node)
        {
325 326 327
            client_t *existing_client = (client_t *)node->key;
            if (existing_client->username && 
                    strcmp (existing_client->username, client->username) == 0)
328 329 330
            {
                avl_tree_unlock (source->pending_tree);
                return 0;
Michael Smith's avatar
Michael Smith committed
331
            }
332
            node = avl_get_next (node);
Michael Smith's avatar
Michael Smith committed
333
        }
334
        avl_tree_unlock (source->pending_tree);
Michael Smith's avatar
Michael Smith committed
335
    }
336
    return 1;
Michael Smith's avatar
Michael Smith committed
337 338
}

339 340 341 342

/* 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
 */
343
static int add_listener_to_source (source_t *source, client_t *client)
Michael Smith's avatar
Michael Smith committed
344
{
345
    int loop = 10;
346 347 348 349 350 351 352 353
    do
    {
        DEBUG3 ("max on %s is %ld (cur %lu)", source->mount,
                source->max_listeners, source->listeners);
        if (source->max_listeners == -1)
            break;
        if (source->listeners < (unsigned long)source->max_listeners)
            break;
Michael Smith's avatar
Michael Smith committed
354

355 356 357
        if (loop && source->fallback_when_full && source->fallback_mount)
        {
            source_t *next = source_find_mount (source->fallback_mount);
358
            if (!next) {
Michael Smith's avatar
Michael Smith committed
359
                ERROR2("Fallback '%s' for full source '%s' not found", 
360 361 362 363
                        source->mount, source->fallback_mount);
                return -1;
            }

364 365 366 367 368
            INFO1 ("stream full trying %s", next->mount);
            source = next;
            loop--;
            continue;
        }
369 370
        /* now we fail the client */
        return -1;
Michael Smith's avatar
Michael Smith committed
371

372
    } while (1);
Michael Smith's avatar
Michael Smith committed
373

374 375 376 377
    client->write_to_client = format_generic_write_to_client;
    client->check_buffer = format_check_http_buffer;
    client->refbuf->len = PER_CLIENT_REFBUF_SIZE;
    memset (client->refbuf->data, 0, PER_CLIENT_REFBUF_SIZE);
Michael Smith's avatar
Michael Smith committed
378

379 380 381 382 383
    /* lets add the client to the active list */
    avl_tree_wlock (source->pending_tree);
    avl_insert (source->pending_tree, client);
    avl_tree_unlock (source->pending_tree);

384 385 386 387 388
    if (source->running == 0 && source->on_demand)
    {
        /* enable on-demand relay to start, wake up the slave thread */
        DEBUG0("kicking off on-demand relay");
        source->on_demand_req = 1;
Michael Smith's avatar
Michael Smith committed
389
    }
390 391 392
    DEBUG1 ("Added client to %s", source->mount);
    return 0;
}
Michael Smith's avatar
Michael Smith committed
393 394


395 396 397
/* Add listener to the pending lists of either the  source or fserve thread.
 * This can be run from the connection or auth thread context
 */
398
static int add_authenticated_listener (const char *mount, mount_proxy *mountinfo, client_t *client)
399 400 401
{
    int ret = 0;
    source_t *source = NULL;
402

403 404
    client->authenticated = 1;

405 406 407 408 409 410 411 412 413 414 415
    /* Here we are parsing the URI request to see if the extension is .xsl, if
     * so, then process this request as an XSLT request
     */
    if (util_check_valid_extension (mount) == XSLT_CONTENT)
    {
        /* If the file exists, then transform it, otherwise, write a 404 */
        DEBUG0("Stats request, sending XSL transformed stats");
        stats_transform_xslt (client, mount);
        return 0;
    }

416 417
    avl_tree_rlock (global.source_tree);
    source = source_find_mount (mount);
Michael Smith's avatar
Michael Smith committed
418

419 420
    if (source)
    {
421
        if (check_duplicate_logins (source, client, mountinfo->auth) == 0)
422 423 424
        {
            avl_tree_unlock (global.source_tree);
            return -1;
425
        }
426 427 428 429 430 431
        if (mountinfo)
        {
            /* set a per-mount disconnect time if auth hasn't set one already */
            if (mountinfo->max_listener_duration && client->con->discon_time == 0)
                client->con->discon_time = time(NULL) + mountinfo->max_listener_duration;
        }
432

433
        ret = add_listener_to_source (source, client);
434 435 436
        avl_tree_unlock (global.source_tree);
        if (ret == 0)
            DEBUG0 ("client authenticated, passed to source");
437
    }
438 439 440 441 442
    else
    {
        avl_tree_unlock (global.source_tree);
        fserve_client_create (client, mount);
    }
443 444
    return ret;
}
445 446


447
int auth_postprocess_listener (auth_client *auth_user)
448
{
449
    int ret;
450
    client_t *client = auth_user->client;
451
    ice_config_t *config = config_get_config();
452

453
    mount_proxy *mountinfo = config_find_mount (config, auth_user->mount);
454

455
    ret = add_authenticated_listener (auth_user->mount, mountinfo, client);
456
    config_release_config();
457

458 459 460
    if (ret < 0)
        client_send_401 (auth_user->client);
    auth_user->client = NULL;
461

462
    return ret;
463 464
}

465 466 467 468

/* Add a listener. Check for any mount information that states any
 * authentication to be used.
 */
469
void auth_add_listener (const char *mount, client_t *client)
470
{
471 472 473 474 475 476 477
    mount_proxy *mountinfo; 
    ice_config_t *config = config_get_config();

    mountinfo = config_find_mount (config, mount);
    if (mountinfo && mountinfo->no_mount)
    {
        config_release_config ();
478
        client_send_403 (client, "mountpoint unavailable");
479 480
        return;
    }
481
    if (mountinfo && mountinfo->auth && mountinfo->auth->authenticate)
482 483 484
    {
        auth_client *auth_user;

485
        if (mountinfo->auth->pending_count > 100)
486 487 488
        {
            config_release_config ();
            WARN0 ("too many clients awaiting authentication");
489
            client_send_403 (client, "busy, please try again later");
490 491
            return;
        }
492
        auth_user = auth_client_setup (mount, client);
493 494
        auth_user->process = auth_new_listener;
        INFO0 ("adding client for authentication");
495 496
        queue_auth_client (auth_user, mountinfo);
        config_release_config ();
497 498 499
    }
    else
    {
500
        int ret = add_authenticated_listener (mount, mountinfo, client);
501 502
        config_release_config ();
        if (ret < 0)
503
            client_send_403 (client, "max listeners reached");
504 505 506
    }
}

507 508 509 510

/* determine whether we need to process this client further. This
 * involves any auth exit, typically for external auth servers.
 */
511
int auth_release_listener (client_t *client)
512
{
513
    if (client->authenticated)
514
    {
515
        const char *mount = httpp_getvar (client->parser, HTTPP_VAR_URI);
516

517 518 519
        /* drop any queue reference here, we do not want a race between the source thread
         * and the auth/fserve thread */
        client_set_queue (client, NULL);
520

521 522 523 524 525 526 527 528
        if (mount && client->auth && client->auth->release_listener)
        {
            auth_client *auth_user = auth_client_setup (mount, client);
            auth_user->process = auth_remove_listener;
            queue_auth_client (auth_user, NULL);
            return 1;
        }
        client->authenticated = 0;
529 530 531
    }
    return 0;
}
532 533


Karl Heyes's avatar
Karl Heyes committed
534
static int get_authenticator (auth_t *auth, config_options_t *options)
535
{
536 537 538 539 540
    if (auth->type == NULL)
    {
        WARN0 ("no authentication type defined");
        return -1;
    }
541 542 543
    do
    {
        DEBUG1 ("type is %s", auth->type);
544

Karl Heyes's avatar
Karl Heyes committed
545 546
        if (strcmp (auth->type, "url") == 0)
        {
547
#ifdef HAVE_AUTH_URL
Karl Heyes's avatar
Karl Heyes committed
548 549
            if (auth_get_url_auth (auth, options) < 0)
                return -1;
550
            break;
551 552
#else
            ERROR0 ("Auth URL disabled");
Karl Heyes's avatar
Karl Heyes committed
553
            return -1;
554
#endif
Karl Heyes's avatar
Karl Heyes committed
555
        }
556 557
        if (strcmp (auth->type, "htpasswd") == 0)
        {
Karl Heyes's avatar
Karl Heyes committed
558 559
            if (auth_get_htpasswd_auth (auth, options) < 0)
                return -1;
560
            break;
561
        }
Karl Heyes's avatar
Karl Heyes committed
562

563
        ERROR1("Unrecognised authenticator type: \"%s\"", auth->type);
Karl Heyes's avatar
Karl Heyes committed
564
        return -1;
565 566 567 568
    } while (0);

    while (options)
    {
569 570
        if (strcmp (options->name, "allow_duplicate_users") == 0)
            auth->allow_duplicate_users = atoi ((char*)options->value);
571
        options = options->next;
572
    }
Karl Heyes's avatar
Karl Heyes committed
573
    return 0;
574
}
575 576


577 578 579 580 581 582 583 584 585 586 587 588 589 590
auth_t *auth_get_authenticator (xmlNodePtr node)
{
    auth_t *auth = calloc (1, sizeof (auth_t));
    config_options_t *options = NULL, **next_option = &options;
    xmlNodePtr option;

    if (auth == NULL)
        return NULL;

    option = node->xmlChildrenNode;
    while (option)
    {
        xmlNodePtr current = option;
        option = option->next;
591
        if (xmlStrcmp (current->name, XMLSTR("option")) == 0)
592 593
        {
            config_options_t *opt = calloc (1, sizeof (config_options_t));
594
            opt->name = (char *)xmlGetProp (current, XMLSTR("name"));
595 596 597 598 599
            if (opt->name == NULL)
            {
                free(opt);
                continue;
            }
600
            opt->value = (char *)xmlGetProp (current, XMLSTR("value"));
601 602 603 604 605 606 607 608 609 610
            if (opt->value == NULL)
            {
                xmlFree (opt->name);
                free (opt);
                continue;
            }
            *next_option = opt;
            next_option = &opt->next;
        }
        else
611
            if (xmlStrcmp (current->name, XMLSTR("text")) != 0)
612
                WARN1 ("unknown auth setting (%s)", current->name);
613
    }
614
    auth->type = (char*)xmlGetProp (node, XMLSTR("type"));
Karl Heyes's avatar
Karl Heyes committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628
    if (get_authenticator (auth, options) < 0)
    {
        xmlFree (auth->type);
        free (auth);
        auth = NULL;
    }
    else
    {
        auth->tailp = &auth->head;
        thread_mutex_create (&auth->lock);
        auth->refcount = 1;
        auth->running = 1;
        auth->thread = thread_create ("auth thread", auth_run_thread, auth, THREAD_ATTACHED);
    }
629

630 631 632 633 634 635 636
    while (options)
    {
        config_options_t *opt = options;
        options = opt->next;
        xmlFree (opt->name);
        xmlFree (opt->value);
        free (opt);
637
    }
638 639
    return auth;
}
640 641


642 643 644 645
/* called when the stream starts, so that authentication engine can do any
 * cleanup/initialisation.
 */
void auth_stream_start (mount_proxy *mountinfo, const char *mount)
646
{
647 648 649 650 651 652
    if (mountinfo && mountinfo->auth && mountinfo->auth->stream_start)
    {
        auth_client *auth_user = calloc (1, sizeof (auth_client));
        if (auth_user)
        {
            auth_user->mount = strdup (mount);
653
            auth_user->process = stream_start_callback;
654

655
            queue_auth_client (auth_user, mountinfo);
656 657 658 659 660
        }
    }
}


661 662 663 664 665 666 667 668 669 670 671
/* Called when the stream ends so that the authentication engine can do
 * any authentication cleanup
 */
void auth_stream_end (mount_proxy *mountinfo, const char *mount)
{
    if (mountinfo && mountinfo->auth && mountinfo->auth->stream_end)
    {
        auth_client *auth_user = calloc (1, sizeof (auth_client));
        if (auth_user)
        {
            auth_user->mount = strdup (mount);
672
            auth_user->process = stream_end_callback;
673

674
            queue_auth_client (auth_user, mountinfo);
675
        }
676
    }
677
}
678 679


680
/* these are called at server start and termination */
681

682
void auth_initialise (void)
683 684
{
    thread_mutex_create (&auth_lock);
685 686
}

687
void auth_shutdown (void)
688
{
689 690
    thread_mutex_destroy (&auth_lock);
    INFO0 ("Auth shutdown");
691
}
692