auth.c 19 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
    /* 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;
198 199
        auth_release (client->auth);
        client->auth = NULL;
200 201
        return;
    }
202
    if (auth->authenticate)
203
    {
204
        if (auth->authenticate (auth_user) != AUTH_OK)
205 206 207
        {
            auth_release (client->auth);
            client->auth = NULL;
208
            return;
209
        }
Michael Smith's avatar
Michael Smith committed
210
    }
211
    if (auth_postprocess_listener (auth_user) < 0)
212 213 214
    {
        auth_release (client->auth);
        client->auth = NULL;
215
        INFO1 ("client %lu failed", client->con->id);
216
    }
Michael Smith's avatar
Michael Smith committed
217 218
}

219

220
/* wrapper function for auth thread to drop listener connections
221
 */
222
static void auth_remove_listener (auth_t *auth, auth_client *auth_user)
Michael Smith's avatar
Michael Smith committed
223
{
224 225
    client_t *client = auth_user->client;

226 227
    if (client->auth->release_listener)
        client->auth->release_listener (auth_user);
228 229
    auth_release (client->auth);
    client->auth = NULL;
230 231 232 233 234 235 236 237
    /* 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.
 */
238
static void stream_start_callback (auth_t *auth, auth_client *auth_user)
239 240 241
{
    if (auth->stream_start)
        auth->stream_start (auth_user);
242
    auth_release (auth);
243 244 245 246 247 248
}


/* Callback from auth thread to handle a stream start event, this applies
 * to both source clients and relays.
 */
249
static void stream_end_callback (auth_t *auth, auth_client *auth_user)
250 251 252
{
    if (auth->stream_end)
        auth->stream_end (auth_user);
253
    auth_release (auth);
254
}
Michael Smith's avatar
Michael Smith committed
255 256


257 258 259
/* The auth thread main loop. */
static void *auth_run_thread (void *arg)
{
260 261
    auth_t *auth = arg;

262
    INFO0 ("Authentication thread started");
263
    while (auth->running)
264
    {
265 266
        /* usually no clients are waiting, so don't bother taking locks */
        if (auth->head)
267 268
        {
            auth_client *auth_user;
Michael Smith's avatar
Michael Smith committed
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283
            /* 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);
284
            auth_user->next = NULL;
Michael Smith's avatar
Michael Smith committed
285

286
            if (auth_user->process)
287
                auth_user->process (auth, auth_user);
288 289
            else
                ERROR0 ("client auth process not set");
Michael Smith's avatar
Michael Smith committed
290

291
            auth_client_free (auth_user);
Michael Smith's avatar
Michael Smith committed
292

293
            continue;
294
        }
295
        thread_sleep (150000);
296
    }
297 298 299
    INFO0 ("Authenication thread shutting down");
    return NULL;
}
Michael Smith's avatar
Michael Smith committed
300 301


302 303 304 305
/* 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
 */
306
static int check_duplicate_logins (source_t *source, client_t *client, auth_t *auth)
307 308 309 310
{
    /* allow multiple authenticated relays */
    if (client->username == NULL)
        return 1;
Michael Smith's avatar
Michael Smith committed
311

312 313 314 315 316 317 318 319
    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)
        {   
320 321 322
            client_t *existing_client = (client_t *)node->key;
            if (existing_client->username && 
                    strcmp (existing_client->username, client->username) == 0)
323 324 325 326 327 328 329 330 331 332 333 334
            {
                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)
        {
335 336 337
            client_t *existing_client = (client_t *)node->key;
            if (existing_client->username && 
                    strcmp (existing_client->username, client->username) == 0)
338 339 340
            {
                avl_tree_unlock (source->pending_tree);
                return 0;
Michael Smith's avatar
Michael Smith committed
341
            }
342
            node = avl_get_next (node);
Michael Smith's avatar
Michael Smith committed
343
        }
344
        avl_tree_unlock (source->pending_tree);
Michael Smith's avatar
Michael Smith committed
345
    }
346
    return 1;
Michael Smith's avatar
Michael Smith committed
347 348
}

349 350 351 352

/* 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
 */
353
static int add_listener_to_source (source_t *source, client_t *client)
Michael Smith's avatar
Michael Smith committed
354
{
355
    int loop = 10;
356 357 358 359 360 361 362 363
    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
364

365 366 367
        if (loop && source->fallback_when_full && source->fallback_mount)
        {
            source_t *next = source_find_mount (source->fallback_mount);
368
            if (!next) {
Michael Smith's avatar
Michael Smith committed
369
                ERROR2("Fallback '%s' for full source '%s' not found", 
370 371 372 373
                        source->mount, source->fallback_mount);
                return -1;
            }

374 375 376 377 378
            INFO1 ("stream full trying %s", next->mount);
            source = next;
            loop--;
            continue;
        }
379 380
        /* now we fail the client */
        return -1;
Michael Smith's avatar
Michael Smith committed
381

382
    } while (1);
Michael Smith's avatar
Michael Smith committed
383

384 385 386 387
    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
388

389 390 391 392 393
    /* 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);

394 395 396 397 398
    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
399
    }
400 401 402
    DEBUG1 ("Added client to %s", source->mount);
    return 0;
}
Michael Smith's avatar
Michael Smith committed
403 404


405 406 407
/* Add listener to the pending lists of either the  source or fserve thread.
 * This can be run from the connection or auth thread context
 */
408
static int add_authenticated_listener (const char *mount, mount_proxy *mountinfo, client_t *client)
409 410 411
{
    int ret = 0;
    source_t *source = NULL;
412

413 414
    client->authenticated = 1;

415 416 417 418 419 420 421 422 423 424 425
    /* 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;
    }

426 427
    avl_tree_rlock (global.source_tree);
    source = source_find_mount (mount);
Michael Smith's avatar
Michael Smith committed
428

429 430
    if (source)
    {
431 432
        if (mountinfo)
        {
Karl Heyes's avatar
Karl Heyes committed
433 434 435 436 437 438
            if (check_duplicate_logins (source, client, mountinfo->auth) == 0)
            {
                avl_tree_unlock (global.source_tree);
                return -1;
            }

439 440 441 442
            /* 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;
        }
443

444
        ret = add_listener_to_source (source, client);
445 446 447
        avl_tree_unlock (global.source_tree);
        if (ret == 0)
            DEBUG0 ("client authenticated, passed to source");
448
    }
449 450 451 452 453
    else
    {
        avl_tree_unlock (global.source_tree);
        fserve_client_create (client, mount);
    }
454 455
    return ret;
}
456 457


458
int auth_postprocess_listener (auth_client *auth_user)
459
{
460
    int ret;
461
    client_t *client = auth_user->client;
462
    ice_config_t *config = config_get_config();
463

464
    mount_proxy *mountinfo = config_find_mount (config, auth_user->mount);
465

466
    ret = add_authenticated_listener (auth_user->mount, mountinfo, client);
467
    config_release_config();
468

469 470 471
    if (ret < 0)
        client_send_401 (auth_user->client);
    auth_user->client = NULL;
472

473
    return ret;
474 475
}

476 477 478 479

/* Add a listener. Check for any mount information that states any
 * authentication to be used.
 */
480
void auth_add_listener (const char *mount, client_t *client)
481
{
482 483 484 485 486 487 488
    mount_proxy *mountinfo; 
    ice_config_t *config = config_get_config();

    mountinfo = config_find_mount (config, mount);
    if (mountinfo && mountinfo->no_mount)
    {
        config_release_config ();
489
        client_send_403 (client, "mountpoint unavailable");
490 491
        return;
    }
492
    if (mountinfo && mountinfo->auth)
493 494 495
    {
        auth_client *auth_user;

496
        if (mountinfo->auth->pending_count > 100)
497 498 499
        {
            config_release_config ();
            WARN0 ("too many clients awaiting authentication");
500
            client_send_403 (client, "busy, please try again later");
501 502
            return;
        }
503
        auth_user = auth_client_setup (mount, client);
504 505
        auth_user->process = auth_new_listener;
        INFO0 ("adding client for authentication");
506 507
        queue_auth_client (auth_user, mountinfo);
        config_release_config ();
508 509 510
    }
    else
    {
511
        int ret = add_authenticated_listener (mount, mountinfo, client);
512 513
        config_release_config ();
        if (ret < 0)
514
            client_send_403 (client, "max listeners reached");
515 516 517
    }
}

518 519 520 521

/* determine whether we need to process this client further. This
 * involves any auth exit, typically for external auth servers.
 */
522
int auth_release_listener (client_t *client)
523
{
524
    if (client->authenticated)
525
    {
526
        const char *mount = httpp_getvar (client->parser, HTTPP_VAR_URI);
527

528 529 530
        /* 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);
531

532 533 534 535 536 537 538 539
        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;
540 541 542
    }
    return 0;
}
543 544


Karl Heyes's avatar
Karl Heyes committed
545
static int get_authenticator (auth_t *auth, config_options_t *options)
546
{
547 548 549 550 551
    if (auth->type == NULL)
    {
        WARN0 ("no authentication type defined");
        return -1;
    }
552 553 554
    do
    {
        DEBUG1 ("type is %s", auth->type);
555

Karl Heyes's avatar
Karl Heyes committed
556 557
        if (strcmp (auth->type, "url") == 0)
        {
558
#ifdef HAVE_AUTH_URL
Karl Heyes's avatar
Karl Heyes committed
559 560
            if (auth_get_url_auth (auth, options) < 0)
                return -1;
561
            break;
562 563
#else
            ERROR0 ("Auth URL disabled");
Karl Heyes's avatar
Karl Heyes committed
564
            return -1;
565
#endif
Karl Heyes's avatar
Karl Heyes committed
566
        }
567 568
        if (strcmp (auth->type, "htpasswd") == 0)
        {
Karl Heyes's avatar
Karl Heyes committed
569 570
            if (auth_get_htpasswd_auth (auth, options) < 0)
                return -1;
571
            break;
572
        }
Karl Heyes's avatar
Karl Heyes committed
573

574
        ERROR1("Unrecognised authenticator type: \"%s\"", auth->type);
Karl Heyes's avatar
Karl Heyes committed
575
        return -1;
576 577 578 579
    } while (0);

    while (options)
    {
580 581
        if (strcmp (options->name, "allow_duplicate_users") == 0)
            auth->allow_duplicate_users = atoi ((char*)options->value);
582
        options = options->next;
583
    }
Karl Heyes's avatar
Karl Heyes committed
584
    return 0;
585
}
586 587


588 589 590 591 592 593 594 595 596 597 598 599 600 601
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;
602
        if (xmlStrcmp (current->name, XMLSTR("option")) == 0)
603 604
        {
            config_options_t *opt = calloc (1, sizeof (config_options_t));
605
            opt->name = (char *)xmlGetProp (current, XMLSTR("name"));
606 607 608 609 610
            if (opt->name == NULL)
            {
                free(opt);
                continue;
            }
611
            opt->value = (char *)xmlGetProp (current, XMLSTR("value"));
612 613 614 615 616 617 618 619 620 621
            if (opt->value == NULL)
            {
                xmlFree (opt->name);
                free (opt);
                continue;
            }
            *next_option = opt;
            next_option = &opt->next;
        }
        else
622
            if (xmlStrcmp (current->name, XMLSTR("text")) != 0)
623
                WARN1 ("unknown auth setting (%s)", current->name);
624
    }
625
    auth->type = (char*)xmlGetProp (node, XMLSTR("type"));
Karl Heyes's avatar
Karl Heyes committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639
    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);
    }
640

641 642 643 644 645 646 647
    while (options)
    {
        config_options_t *opt = options;
        options = opt->next;
        xmlFree (opt->name);
        xmlFree (opt->value);
        free (opt);
648
    }
649 650
    return auth;
}
651 652


653 654 655 656
/* called when the stream starts, so that authentication engine can do any
 * cleanup/initialisation.
 */
void auth_stream_start (mount_proxy *mountinfo, const char *mount)
657
{
658 659 660 661 662 663
    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);
664
            auth_user->process = stream_start_callback;
665

666
            queue_auth_client (auth_user, mountinfo);
667 668 669 670 671
        }
    }
}


672 673 674 675 676 677 678 679 680 681 682
/* 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);
683
            auth_user->process = stream_end_callback;
684

685
            queue_auth_client (auth_user, mountinfo);
686
        }
687
    }
688
}
689 690


691
/* these are called at server start and termination */
692

693
void auth_initialise (void)
694 695
{
    thread_mutex_create (&auth_lock);
696 697
}

698
void auth_shutdown (void)
699
{
700 701
    thread_mutex_destroy (&auth_lock);
    INFO0 ("Auth shutdown");
702
}
703