auth.c 35.1 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 2013-2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
12 13
 */

14
/**
Michael Smith's avatar
Michael Smith committed
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>

27
#include <igloo/httpp.h>
Marvin Scholz's avatar
Marvin Scholz committed
28

Michael Smith's avatar
Michael Smith committed
29 30 31
#include "auth.h"
#include "source.h"
#include "client.h"
32
#include "errors.h"
Michael Smith's avatar
Michael Smith committed
33
#include "cfgfile.h"
34
#include "stats.h"
35
#include "fserve.h"
36
#include "admin.h"
Philipp Schafft's avatar
Philipp Schafft committed
37
#include "acl.h"
Michael Smith's avatar
Michael Smith committed
38 39 40

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

Philipp Schafft's avatar
Philipp Schafft committed
42 43 44 45
/* data structures */
struct auth_stack_tag {
    size_t refcount;
    auth_t *auth;
46
    igloo_mutex_t lock;
Philipp Schafft's avatar
Philipp Schafft committed
47 48
    auth_stack_t *next;
};
49

Philipp Schafft's avatar
Philipp Schafft committed
50
/* code */
Marvin Scholz's avatar
Marvin Scholz committed
51
static void __handle_auth_client(auth_t *auth, auth_client *auth_user);
52

53
static igloo_mutex_t _auth_lock; /* protects _current_id */
54 55 56 57 58 59 60 61 62 63 64 65
static volatile unsigned long _current_id = 0;

static unsigned long _next_auth_id(void) {
    unsigned long id;

    thread_mutex_lock(&_auth_lock);
    id = _current_id++;
    thread_mutex_unlock(&_auth_lock);

    return id;
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
static const struct {
    auth_result result;
    const char *string;
} __auth_results[] = {
    {.result = AUTH_UNDEFINED,      .string = "undefined"},
    {.result = AUTH_OK,             .string = "ok"},
    {.result = AUTH_FAILED,         .string = "failed"},
    {.result = AUTH_RELEASED,       .string = "released"},
    {.result = AUTH_FORBIDDEN,      .string = "forbidden"},
    {.result = AUTH_NOMATCH,        .string = "no match"},
    {.result = AUTH_USERADDED,      .string = "user added"},
    {.result = AUTH_USEREXISTS,     .string = "user exists"},
    {.result = AUTH_USERDELETED,    .string = "user deleted"}
};

81 82
static const char *auth_result2str(auth_result res)
{
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    size_t i;

    for (i = 0; i < (sizeof(__auth_results)/sizeof(*__auth_results)); i++) {
        if (__auth_results[i].result == res)
            return __auth_results[i].string;
    }

    return "(unknown)";
}

auth_result auth_str2result(const char *str)
{
    size_t i;

    for (i = 0; i < (sizeof(__auth_results)/sizeof(*__auth_results)); i++) {
        if (strcasecmp(__auth_results[i].string, str) == 0)
            return __auth_results[i].result;
100
    }
101 102

    return AUTH_FAILED;
103 104
}

Philipp Schafft's avatar
Philipp Schafft committed
105
static auth_client *auth_client_setup (client_t *client)
106
{
107
    /* This will look something like "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" */
108
    auth_client *auth_user;
109

Philipp Schafft's avatar
Philipp Schafft committed
110 111 112 113 114 115 116 117 118
    do {
        const char *header;
        char *userpass, *tmp;
        char *username, *password;

        /* check if we already have auth infos */
        if (client->username || client->password)
            break;

119
        header = igloo_httpp_getvar(client->parser, "authorization");
Philipp Schafft's avatar
Philipp Schafft committed
120

121 122 123
        if (header == NULL)
            break;

Philipp Schafft's avatar
Philipp Schafft committed
124
        if (strncmp(header, "Basic ", 6) == 0) {
125
            userpass = util_base64_decode (header+6);
Philipp Schafft's avatar
Philipp Schafft committed
126
            if (userpass == NULL) {
127
                ICECAST_LOG_WARN("Base64 decode of Authorization header \"%s\" failed",
128 129 130 131 132
                        header+6);
                break;
            }

            tmp = strchr(userpass, ':');
133
            if (tmp == NULL) {
Philipp Schafft's avatar
Philipp Schafft committed
134
                free(userpass);
135
                break;
136
            }
137 138 139 140

            *tmp = 0;
            username = userpass;
            password = tmp+1;
Philipp Schafft's avatar
Philipp Schafft committed
141 142
            client->username = strdup(username);
            client->password = strdup(password);
143 144
            free (userpass);
            break;
145
        }
146
        ICECAST_LOG_INFO("unhandled authorization header: %s", header);
147
    } while (0);
148

Philipp Schafft's avatar
Philipp Schafft committed
149
    auth_user = calloc(1, sizeof(auth_client));
150 151
    auth_user->client = client;
    return auth_user;
152 153
}

Michael Smith's avatar
Michael Smith committed
154

Philipp Schafft's avatar
Philipp Schafft committed
155
static void queue_auth_client (auth_client *auth_user)
156
{
157 158
    auth_t *auth;

159
    if (auth_user == NULL)
160 161
        return;
    auth_user->next = NULL;
Philipp Schafft's avatar
Philipp Schafft committed
162
    if (auth_user->client == NULL || auth_user->client->auth == NULL)
163
    {
Philipp Schafft's avatar
Philipp Schafft committed
164 165
        ICECAST_LOG_WARN("internal state is incorrect for %p", auth_user->client);
        return;
166
    }
Philipp Schafft's avatar
Philipp Schafft committed
167 168
    auth = auth_user->client->auth;
    ICECAST_LOG_DEBUG("...refcount on auth_t %s is now %d", auth->mount, (int)auth->refcount);
169 170 171 172 173 174 175 176 177 178
    if (auth->immediate) {
        __handle_auth_client(auth, auth_user);
    } else {
        thread_mutex_lock (&auth->lock);
        *auth->tailp = auth_user;
        auth->tailp = &auth_user->next;
        auth->pending_count++;
        ICECAST_LOG_INFO("auth on %s has %d pending", auth->mount, auth->pending_count);
        thread_mutex_unlock (&auth->lock);
    }
179
}
Michael Smith's avatar
Michael Smith committed
180 181


182 183 184
/* release the auth. It is referred to by multiple structures so this is
 * refcounted and only actual freed after the last use
 */
Philipp Schafft's avatar
Philipp Schafft committed
185
void auth_release (auth_t *authenticator) {
186 187
    size_t i;

188 189
    if (authenticator == NULL)
        return;
Michael Smith's avatar
Michael Smith committed
190

Philipp Schafft's avatar
Philipp Schafft committed
191
    thread_mutex_lock(&authenticator->lock);
192
    authenticator->refcount--;
Philipp Schafft's avatar
Philipp Schafft committed
193
    ICECAST_LOG_DEBUG("...refcount on auth_t %s is now %d", authenticator->mount, (int)authenticator->refcount);
194
    if (authenticator->refcount)
195
    {
Philipp Schafft's avatar
Philipp Schafft committed
196
        thread_mutex_unlock(&authenticator->lock);
197
        return;
198
    }
Michael Smith's avatar
Michael Smith committed
199

200
    /* cleanup auth thread attached to this auth */
Philipp Schafft's avatar
Philipp Schafft committed
201 202
    if (authenticator->running) {
        authenticator->running = 0;
203
        thread_mutex_unlock(&authenticator->lock);
204
        igloo_thread_join(authenticator->thread);
205
        thread_mutex_lock(&authenticator->lock);
Philipp Schafft's avatar
Philipp Schafft committed
206
    }
207

208
    if (authenticator->free)
Philipp Schafft's avatar
Philipp Schafft committed
209 210 211 212 213
        authenticator->free(authenticator);
    if (authenticator->type)
        xmlFree (authenticator->type);
    if (authenticator->role)
        xmlFree (authenticator->role);
214 215
    if (authenticator->management_url)
        xmlFree (authenticator->management_url);
Philipp Schafft's avatar
Philipp Schafft committed
216
    thread_mutex_unlock(&authenticator->lock);
217
    igloo_thread_mutex_destroy(&authenticator->lock);
218
    if (authenticator->mount)
Philipp Schafft's avatar
Philipp Schafft committed
219 220
        free(authenticator->mount);
    acl_release(authenticator->acl);
221
    config_clear_http_header(authenticator->http_headers);
222 223 224 225 226 227

    for (i = 0; i < authenticator->filter_origin_len; i++) {
        free(authenticator->filter_origin[i].origin);
    }
    free(authenticator->filter_origin);

Philipp Schafft's avatar
Philipp Schafft committed
228
    free(authenticator);
Michael Smith's avatar
Michael Smith committed
229 230
}

Philipp Schafft's avatar
Philipp Schafft committed
231 232 233 234 235 236 237 238 239 240 241
/* increment refcount on the auth.
 */
void    auth_addref (auth_t *authenticator) {
    if (authenticator == NULL)
        return;

    thread_mutex_lock (&authenticator->lock);
    authenticator->refcount++;
    ICECAST_LOG_DEBUG("...refcount on auth_t %s is now %d", authenticator->mount, (int)authenticator->refcount);
    thread_mutex_unlock (&authenticator->lock);
}
Michael Smith's avatar
Michael Smith committed
242

243
static void auth_client_free (auth_client *auth_user)
Michael Smith's avatar
Michael Smith committed
244
{
245
    if (!auth_user)
246
        return;
247 248 249

    free(auth_user->alter_client_arg);
    free(auth_user);
Michael Smith's avatar
Michael Smith committed
250 251 252
}


253
/* verify that the client is still connected. */
254
static int is_client_connected (client_t *client) {
255
/* As long as igloo_sock_active() is broken we need to disable this:
256

257 258
    int ret = 1;
    if (client)
259
        if (igloo_sock_active(client->con->sock) == 0)
260 261
            ret = 0;
    return ret;
262 263
*/
    return 1;
264 265
}

Philipp Schafft's avatar
Philipp Schafft committed
266
static auth_result auth_new_client (auth_t *auth, auth_client *auth_user) {
267
    client_t *client = auth_user->client;
Philipp Schafft's avatar
Philipp Schafft committed
268
    auth_result ret = AUTH_FAILED;
269

270 271
    /* make sure there is still a client at this point, a slow backend request
     * can be avoided if client has disconnected */
Philipp Schafft's avatar
Philipp Schafft committed
272
    if (is_client_connected(client) == 0) {
273
        ICECAST_LOG_DEBUG("client is no longer connected");
274
        client->respcode = 400;
275 276
        auth_release (client->auth);
        client->auth = NULL;
Philipp Schafft's avatar
Philipp Schafft committed
277
        return AUTH_FAILED;
278
    }
Philipp Schafft's avatar
Philipp Schafft committed
279 280 281 282

    if (auth->authenticate_client) {
        ret = auth->authenticate_client(auth_user);
        if (ret != AUTH_OK)
283 284 285
        {
            auth_release (client->auth);
            client->auth = NULL;
Philipp Schafft's avatar
Philipp Schafft committed
286
            return ret;
287
        }
Michael Smith's avatar
Michael Smith committed
288
    }
Philipp Schafft's avatar
Philipp Schafft committed
289
    return ret;
Michael Smith's avatar
Michael Smith committed
290 291
}

292

293
/* wrapper function for auth thread to drop client connections
294
 */
Philipp Schafft's avatar
Philipp Schafft committed
295
static auth_result auth_remove_client(auth_t *auth, auth_client *auth_user)
Michael Smith's avatar
Michael Smith committed
296
{
297
    client_t *client = auth_user->client;
Philipp Schafft's avatar
Philipp Schafft committed
298
    auth_result ret = AUTH_RELEASED;
299

300 301
    (void)auth;

Philipp Schafft's avatar
Philipp Schafft committed
302 303
    if (client->auth->release_client)
        ret = client->auth->release_client(auth_user);
304

Philipp Schafft's avatar
Philipp Schafft committed
305
    auth_release(client->auth);
306
    client->auth = NULL;
307

Philipp Schafft's avatar
Philipp Schafft committed
308 309 310
    /* client is going, so auth is not an issue at this point */
    acl_release(client->acl);
    client->acl = NULL;
311

Philipp Schafft's avatar
Philipp Schafft committed
312
    return ret;
313
}
Michael Smith's avatar
Michael Smith committed
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
static inline int __handle_auth_client_alter(auth_t *auth, auth_client *auth_user)
{
    client_t *client = auth_user->client;
    const char *uuid = NULL;
    const char *location = NULL;
    int http_status = 0;

    void client_send_redirect(client_t *client, const char *uuid, int status, const char *location);

    switch (auth_user->alter_client_action) {
        case AUTH_ALTER_NOOP:
            return 0;
        break;
        case AUTH_ALTER_REWRITE:
            free(client->uri);
            client->uri = auth_user->alter_client_arg;
            auth_user->alter_client_arg = NULL;
            return 0;
        break;
        case AUTH_ALTER_REDIRECT:
        /* fall through */
        case AUTH_ALTER_REDIRECT_SEE_OTHER:
            uuid = "be7fac90-54fb-4673-9e0d-d15d6a4963a2";
            http_status = 303;
            location = auth_user->alter_client_arg;
        break;
        case AUTH_ALTER_REDIRECT_TEMPORARY:
            uuid = "4b08a03a-ecce-4981-badf-26b0bb6c9d9c";
            http_status = 307;
            location = auth_user->alter_client_arg;
        break;
        case AUTH_ALTER_REDIRECT_PERMANENT:
            uuid = "36bf6815-95cb-4cc8-a7b0-6b4b0c82ac5d";
            http_status = 308;
            location = auth_user->alter_client_arg;
        break;
        case AUTH_ALTER_SEND_ERROR:
            client_send_error_by_uuid(client, auth_user->alter_client_arg);
            return 1;
        break;
    }

    if (uuid && location && http_status) {
        client_send_redirect(client, uuid, http_status, location);
        return 1;
    }

    return -1;
}
364 365 366 367 368 369 370 371 372 373
static void __handle_auth_client (auth_t *auth, auth_client *auth_user) {
    auth_result result;

    if (auth_user->process) {
        result = auth_user->process(auth, auth_user);
    } else {
        ICECAST_LOG_ERROR("client auth process not set");
        result = AUTH_FAILED;
    }

374 375
    ICECAST_LOG_DEBUG("client %p on auth %p role %s processed: %s", auth_user->client, auth, auth->role, auth_result2str(result));

376 377 378 379
    if (result == AUTH_OK) {
        if (auth_user->client->acl)
            acl_release(auth_user->client->acl);
        acl_addref(auth_user->client->acl = auth->acl);
380
        if (auth->role && !auth_user->client->role) /* TODO: Handle errors here */
381 382 383
            auth_user->client->role = strdup(auth->role);
    }

384 385 386 387 388
    if (result != AUTH_NOMATCH) {
        if (__handle_auth_client_alter(auth, auth_user) == 1)
            return;
    }

389 390 391 392 393 394 395 396
    if (result == AUTH_NOMATCH && auth_user->on_no_match) {
        auth_user->on_no_match(auth_user->client, auth_user->on_result, auth_user->userdata);
    } else if (auth_user->on_result) {
        auth_user->on_result(auth_user->client, auth_user->userdata, result);
    }

    auth_client_free (auth_user);
}
Michael Smith's avatar
Michael Smith committed
397

398 399 400
/* The auth thread main loop. */
static void *auth_run_thread (void *arg)
{
401 402
    auth_t *auth = arg;

403
    ICECAST_LOG_INFO("Authentication thread started");
404 405 406 407 408 409 410 411 412
    while (1) {
        thread_mutex_lock(&auth->lock);

        if (!auth->running) {
            thread_mutex_unlock(&auth->lock);
            break;
        }

        if (auth->head) {
413
            auth_client *auth_user;
Michael Smith's avatar
Michael Smith committed
414

415 416 417 418 419 420 421
            /* may become NULL before lock taken */
            auth_user = (auth_client*)auth->head;
            if (auth_user == NULL)
            {
                thread_mutex_unlock (&auth->lock);
                continue;
            }
422
            ICECAST_LOG_DEBUG("%d client(s) pending on %s (role %s)", auth->pending_count, auth->mount, auth->role);
423 424 425 426
            auth->head = auth_user->next;
            if (auth->head == NULL)
                auth->tailp = &auth->head;
            auth->pending_count--;
427
            thread_mutex_unlock(&auth->lock);
428
            auth_user->next = NULL;
Michael Smith's avatar
Michael Smith committed
429

430
            __handle_auth_client(auth, auth_user);
Philipp Schafft's avatar
Philipp Schafft committed
431

432
            continue;
433 434
        } else {
            thread_mutex_unlock(&auth->lock);
435
        }
436
        igloo_thread_sleep (150000);
Michael Smith's avatar
Michael Smith committed
437
    }
438
    ICECAST_LOG_INFO("Authentication thread shutting down");
Philipp Schafft's avatar
Philipp Schafft committed
439
    return NULL;
440
}
Michael Smith's avatar
Michael Smith committed
441 442


Philipp Schafft's avatar
Philipp Schafft committed
443
/* Add a client.
444
 */
Philipp Schafft's avatar
Philipp Schafft committed
445 446
static void auth_add_client(auth_t *auth, client_t *client, void (*on_no_match)(client_t *client, void (*on_result)(client_t *client, void *userdata, auth_result result), void *userdata), void (*on_result)(client_t *client, void *userdata, auth_result result), void *userdata) {
    auth_client *auth_user;
447
    auth_matchtype_t matchtype;
448 449
    const char *origin;
    size_t i;
450

451 452
    ICECAST_LOG_DEBUG("Trying to add client %p to auth %p's (role %s) queue.", client, auth, auth->role);

Philipp Schafft's avatar
Philipp Schafft committed
453 454
    /* TODO: replace that magic number */
    if (auth->pending_count > 100) {
455
        ICECAST_LOG_WARN("too many clients awaiting authentication on auth %p", auth);
456
        client_send_error_by_id(client, ICECAST_ERROR_AUTH_BUSY);
Philipp Schafft's avatar
Philipp Schafft committed
457
        return;
458 459
    }

460
    if (auth->filter_method[client->parser->req_type] == AUTH_MATCHTYPE_NOMATCH) {
Philipp Schafft's avatar
Philipp Schafft committed
461 462 463 464
        if (on_no_match) {
           on_no_match(client, on_result, userdata);
        } else if (on_result) {
           on_result(client, userdata, AUTH_NOMATCH);
465
        }
466 467 468
        return;
    }

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    /* Filter for CORS "Origin".
     *
     * CORS actually knows about non-CORS requests and assigned the "null"
     * location to them.
     */
    origin = httpp_getvar(client->parser, "origin");
    if (!origin)
        origin = "null";

    matchtype = auth->filter_origin_policy;
    for (i = 0; i < auth->filter_origin_len; i++) {
        if (strcmp(auth->filter_origin[i].origin, origin) == 0) {
            matchtype = auth->filter_origin[i].type;
            break;
        }
    }
    if (matchtype == AUTH_MATCHTYPE_NOMATCH) {
        if (on_no_match) {
            on_no_match(client, on_result, userdata);
        } else if (on_result) {
            on_result(client, userdata, AUTH_NOMATCH);
        }
        return;
    }

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
    if (client->admin_command == ADMIN_COMMAND_ERROR) {
        /* this is a web/ client */
        matchtype = auth->filter_web_policy;
    } else {
        /* this is a admin/ client */
        size_t i;

        matchtype = AUTH_MATCHTYPE_UNUSED;

        for (i = 0; i < (sizeof(auth->filter_admin)/sizeof(*(auth->filter_admin))); i++) {
            if (auth->filter_admin[i].type != AUTH_MATCHTYPE_UNUSED && auth->filter_admin[i].command == client->admin_command) {
                matchtype = auth->filter_admin[i].type;
                break;
            }
        }

        if (matchtype == AUTH_MATCHTYPE_UNUSED)
            matchtype = auth->filter_admin_policy;
    }
    if (matchtype == AUTH_MATCHTYPE_NOMATCH) {
        if (on_no_match) {
           on_no_match(client, on_result, userdata);
        } else if (on_result) {
           on_result(client, userdata, AUTH_NOMATCH);
        }
        return;
    }

Philipp Schafft's avatar
Philipp Schafft committed
522 523 524 525 526 527 528
    auth_release(client->auth);
    auth_addref(client->auth = auth);
    auth_user = auth_client_setup(client);
    auth_user->process = auth_new_client;
    auth_user->on_no_match = on_no_match;
    auth_user->on_result = on_result;
    auth_user->userdata = userdata;
529
    ICECAST_LOG_DEBUG("adding client %p for authentication on %p", client, auth);
Philipp Schafft's avatar
Philipp Schafft committed
530
    queue_auth_client(auth_user);
531 532
}

533 534 535 536 537 538 539
static void __auth_on_result_destroy_client(client_t *client, void *userdata, auth_result result)
{
    (void)userdata, (void)result;

    client_destroy(client);
}

540 541 542
/* determine whether we need to process this client further. This
 * involves any auth exit, typically for external auth servers.
 */
Philipp Schafft's avatar
Philipp Schafft committed
543 544 545
int auth_release_client (client_t *client) {
    if (!client->acl)
        return 0;
546

Philipp Schafft's avatar
Philipp Schafft committed
547 548 549
    if (client->auth && client->auth->release_client) {
        auth_client *auth_user = auth_client_setup(client);
        auth_user->process = auth_remove_client;
550
        auth_user->on_result = __auth_on_result_destroy_client;
Philipp Schafft's avatar
Philipp Schafft committed
551 552 553 554 555
        queue_auth_client(auth_user);
        return 1;
    } else if (client->auth) {
        auth_release(client->auth);
        client->auth = NULL;
556
    }
Philipp Schafft's avatar
Philipp Schafft committed
557 558 559

    acl_release(client->acl);
    client->acl = NULL;
560 561
    return 0;
}
562 563


Karl Heyes's avatar
Karl Heyes committed
564
static int get_authenticator (auth_t *auth, config_options_t *options)
565
{
566 567
    if (auth->type == NULL)
    {
568
        ICECAST_LOG_WARN("no authentication type defined");
569 570
        return -1;
    }
571 572
    do
    {
573
        ICECAST_LOG_DEBUG("type is %s", auth->type);
574

Philipp Schafft's avatar
Philipp Schafft committed
575
        if (strcmp(auth->type, AUTH_TYPE_URL) == 0) {
576
#ifdef HAVE_CURL
Philipp Schafft's avatar
Philipp Schafft committed
577
            if (auth_get_url_auth(auth, options) < 0)
Karl Heyes's avatar
Karl Heyes committed
578
                return -1;
579
            break;
580
#else
581
            ICECAST_LOG_ERROR("Auth URL disabled");
Karl Heyes's avatar
Karl Heyes committed
582
            return -1;
583
#endif
Philipp Schafft's avatar
Philipp Schafft committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597
        } else if (strcmp(auth->type, AUTH_TYPE_HTPASSWD) == 0) {
            if (auth_get_htpasswd_auth(auth, options) < 0)
                return -1;
            break;
        } else if (strcmp(auth->type, AUTH_TYPE_ANONYMOUS) == 0) {
            if (auth_get_anonymous_auth(auth, options) < 0)
                return -1;
            break;
        } else if (strcmp(auth->type, AUTH_TYPE_STATIC) == 0) {
            if (auth_get_static_auth(auth, options) < 0)
                return -1;
            break;
        } else if (strcmp(auth->type, AUTH_TYPE_LEGACY_PASSWORD) == 0) {
            if (auth_get_static_auth(auth, options) < 0)
Karl Heyes's avatar
Karl Heyes committed
598
                return -1;
599
            break;
600
        }
Karl Heyes's avatar
Karl Heyes committed
601

602
        ICECAST_LOG_ERROR("Unrecognised authenticator type: \"%s\"", auth->type);
Karl Heyes's avatar
Karl Heyes committed
603
        return -1;
604 605
    } while (0);

Karl Heyes's avatar
Karl Heyes committed
606
    return 0;
607
}
608 609


610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
static inline void auth_get_authenticator__filter_admin(auth_t *auth, xmlNodePtr node, size_t *filter_admin_index, const char *name, auth_matchtype_t matchtype)
{
    char * tmp = (char*)xmlGetProp(node, XMLSTR(name));

    if (tmp) {
        char *cur = tmp;
        char *next;

        while (cur) {
            next = strstr(cur, ",");
            if (next) {
                *next = 0;
                next++;
                for (; *next == ' '; next++);
            }

            if (*filter_admin_index < (sizeof(auth->filter_admin)/sizeof(*(auth->filter_admin)))) {
                auth->filter_admin[*filter_admin_index].command = admin_get_command(cur);
                switch (auth->filter_admin[*filter_admin_index].command) {
                    case ADMIN_COMMAND_ERROR:
                        ICECAST_LOG_ERROR("Can not add unknown %s command to role.", name);
                    break;
                    case ADMIN_COMMAND_ANY:
                        auth->filter_admin_policy = matchtype;
                    break;
                    default:
                        auth->filter_admin[*filter_admin_index].type = matchtype;
                        (*filter_admin_index)++;
                    break;
                }
            } else {
                ICECAST_LOG_ERROR("Can not add more %s commands to role.", name);
            }

            cur = next;
        }

        free(tmp);
    }
}

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
static inline void auth_get_authenticator__filter_origin(auth_t *auth, xmlNodePtr node, const char *name, auth_matchtype_t matchtype)
{
    char * tmp = (char*)xmlGetProp(node, XMLSTR(name));

    if (tmp) {
        char *cur = tmp;
        char *next;

        while (cur) {
            next = strstr(cur, ",");
            if (next) {
                *next = 0;
                next++;
                for (; *next == ' '; next++);
            }

            if (strcmp(cur, "*") == 0) {
                auth->filter_origin_policy = matchtype;
            } else {
                void *n = realloc(auth->filter_origin, (auth->filter_origin_len + 1)*sizeof(*auth->filter_origin));
                if (!n) {
                    ICECAST_LOG_ERROR("Can not allocate memory. BAD.");
                    break;
                }

                auth->filter_origin = n;
                auth->filter_origin[auth->filter_origin_len].type = matchtype;
                auth->filter_origin[auth->filter_origin_len].origin = strdup(cur);
                if (auth->filter_origin[auth->filter_origin_len].origin) {
                    auth->filter_origin_len++;
                } else {
                    ICECAST_LOG_ERROR("Can not allocate memory. BAD.");
                    break;
                }
            }

            cur = next;
        }

        free(tmp);
    }
}

694 695 696 697 698 699 700 701 702 703 704 705 706 707
static inline void auth_get_authenticator__init_method(auth_t *auth, int *method_inited, auth_matchtype_t init_with)
{
    size_t i;

    if (*method_inited)
        return;

    for (i = 0; i < (sizeof(auth->filter_method)/sizeof(*auth->filter_method)); i++)
        auth->filter_method[i] = init_with;

    *method_inited = 1;
}

static inline int auth_get_authenticator__filter_method(auth_t *auth, xmlNodePtr node, const char *name, auth_matchtype_t matchtype, int *method_inited, auth_matchtype_t init_with)
708 709 710 711 712 713
{
    char * tmp = (char*)xmlGetProp(node, XMLSTR(name));

    if (tmp) {
        char *cur = tmp;

714 715
        auth_get_authenticator__init_method(auth, method_inited, init_with);

716 717
        while (cur) {
            char *next = strstr(cur, ",");
718
            igloo_httpp_request_type_e idx;
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733

            if (next) {
                *next = 0;
                next++;
                for (; *next == ' '; next++);
            }

            if (strcmp(cur, "*") == 0) {
                size_t i;

                for (i = 0; i < (sizeof(auth->filter_method)/sizeof(*(auth->filter_method))); i++)
                    auth->filter_method[i] = matchtype;
                break;
            }

734
            idx = igloo_httpp_str_to_method(cur);
735 736 737 738 739 740 741 742 743 744 745 746 747 748
            if (idx == httpp_req_unknown) {
                ICECAST_LOG_ERROR("Can not add known method \"%H\" to role's %s", cur, name);
                return -1;
            }
            auth->filter_method[idx] = matchtype;
            cur = next;
        }

        free(tmp);
    }

    return 0;
}

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
static inline int auth_get_authenticator__permission_alter(auth_t *auth, xmlNodePtr node, const char *name, auth_matchtype_t matchtype)
{
    char * tmp = (char*)xmlGetProp(node, XMLSTR(name));

    if (tmp) {
        char *cur = tmp;

        while (cur) {
            char *next = strstr(cur, ",");
            auth_alter_t idx;

            if (next) {
                *next = 0;
                next++;
                for (; *next == ' '; next++);
            }

            if (strcmp(cur, "*") == 0) {
                size_t i;

                for (i = 0; i < (sizeof(auth->permission_alter)/sizeof(*(auth->permission_alter))); i++)
                    auth->permission_alter[i] = matchtype;
                break;
            }

            idx = auth_str2alter(cur);
            if (idx == AUTH_ALTER_NOOP) {
                ICECAST_LOG_ERROR("Can not add unknown alter action \"%H\" to role's %s", cur, name);
                return -1;
            } else if (idx == AUTH_ALTER_REDIRECT) {
                auth->permission_alter[AUTH_ALTER_REDIRECT] = matchtype;
                auth->permission_alter[AUTH_ALTER_REDIRECT_SEE_OTHER] = matchtype;
                auth->permission_alter[AUTH_ALTER_REDIRECT_TEMPORARY] = matchtype;
                auth->permission_alter[AUTH_ALTER_REDIRECT_PERMANENT] = matchtype;
            } else {
                auth->permission_alter[idx] = matchtype;
            }

            cur = next;
        }

        free(tmp);
    }

    return 0;
}
795
auth_t *auth_get_authenticator(xmlNodePtr node)
796
{
797
    auth_t *auth = calloc(1, sizeof(auth_t));
798
    config_options_t *options = NULL, **next_option = &options;
799
    xmlNodePtr child;
Philipp Schafft's avatar
Philipp Schafft committed
800
    char *method;
801
    char *tmp;
Philipp Schafft's avatar
Philipp Schafft committed
802
    size_t i;
803
    size_t filter_admin_index = 0;
804
    int method_inited = 0;
805 806 807 808

    if (auth == NULL)
        return NULL;

Philipp Schafft's avatar
Philipp Schafft committed
809 810
    thread_mutex_create(&auth->lock);
    auth->refcount = 1;
811
    auth->id = _next_auth_id();
Philipp Schafft's avatar
Philipp Schafft committed
812 813
    auth->type = (char*)xmlGetProp(node, XMLSTR("type"));
    auth->role = (char*)xmlGetProp(node, XMLSTR("name"));
814
    auth->management_url = (char*)xmlGetProp(node, XMLSTR("management-url"));
815 816 817 818 819 820 821
    auth->filter_web_policy = AUTH_MATCHTYPE_MATCH;
    auth->filter_admin_policy = AUTH_MATCHTYPE_MATCH;

    for (i = 0; i < (sizeof(auth->filter_admin)/sizeof(*(auth->filter_admin))); i++) {
        auth->filter_admin[i].type = AUTH_MATCHTYPE_UNUSED;
        auth->filter_admin[i].command = ADMIN_COMMAND_ERROR;
    }
Philipp Schafft's avatar
Philipp Schafft committed
822

823 824 825
    for (i = 0; i < (sizeof(auth->permission_alter)/sizeof(*(auth->permission_alter))); i++)
        auth->permission_alter[i] = AUTH_MATCHTYPE_NOMATCH;

Philipp Schafft's avatar
Philipp Schafft committed
826 827 828 829 830 831 832 833 834 835
    if (!auth->type) {
        auth_release(auth);
        return NULL;
    }

    method = (char*)xmlGetProp(node, XMLSTR("method"));
    if (method) {
        char *cur = method;
        char *next;

836 837
        for (i = 0; i < (sizeof(auth->filter_method)/sizeof(*auth->filter_method)); i++)
            auth->filter_method[i] = AUTH_MATCHTYPE_NOMATCH;
838
	method_inited = 1;
Philipp Schafft's avatar
Philipp Schafft committed
839 840

        while (cur) {
841
            igloo_httpp_request_type_e idx;
Philipp Schafft's avatar
Philipp Schafft committed
842 843 844 845 846 847 848 849 850

            next = strstr(cur, ",");
            if (next) {
                *next = 0;
                next++;
                for (; *next == ' '; next++);
            }

            if (strcmp(cur, "*") == 0) {
851 852
                for (i = 0; i < (sizeof(auth->filter_method)/sizeof(*auth->filter_method)); i++)
                    auth->filter_method[i] = AUTH_MATCHTYPE_MATCH;
Philipp Schafft's avatar
Philipp Schafft committed
853 854 855
                break;
            }

856
            idx = igloo_httpp_str_to_method(cur);
Philipp Schafft's avatar
Philipp Schafft committed
857 858 859 860
            if (idx == httpp_req_unknown) {
                auth_release(auth);
                return NULL;
            }
861
            auth->filter_method[idx] = AUTH_MATCHTYPE_MATCH;
Philipp Schafft's avatar
Philipp Schafft committed
862 863 864 865 866 867 868

            cur = next;
        }

        xmlFree(method);
    }

869 870 871 872
    auth_get_authenticator__filter_method(auth, node, "match-method", AUTH_MATCHTYPE_MATCH, &method_inited, AUTH_MATCHTYPE_NOMATCH);
    auth_get_authenticator__filter_method(auth, node, "nomatch-method", AUTH_MATCHTYPE_NOMATCH, &method_inited, AUTH_MATCHTYPE_MATCH);

    auth_get_authenticator__init_method(auth, &method_inited, AUTH_MATCHTYPE_MATCH);
873

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
    tmp = (char*)xmlGetProp(node, XMLSTR("match-web"));
    if (tmp) {
        if (strcmp(tmp, "*") == 0) {
            auth->filter_web_policy = AUTH_MATCHTYPE_MATCH;
        } else {
            auth->filter_web_policy = AUTH_MATCHTYPE_NOMATCH;
        }
        free(tmp);
    }

    tmp = (char*)xmlGetProp(node, XMLSTR("nomatch-web"));
    if (tmp) {
        if (strcmp(tmp, "*") == 0) {
            auth->filter_web_policy = AUTH_MATCHTYPE_NOMATCH;
        } else {
            auth->filter_web_policy = AUTH_MATCHTYPE_MATCH;
        }
        free(tmp);
    }

    auth_get_authenticator__filter_admin(auth, node, &filter_admin_index, "match-admin", AUTH_MATCHTYPE_MATCH);
    auth_get_authenticator__filter_admin(auth, node, &filter_admin_index, "nomatch-admin", AUTH_MATCHTYPE_NOMATCH);

897 898 899 900
    auth->filter_origin_policy = AUTH_MATCHTYPE_MATCH;
    auth_get_authenticator__filter_origin(auth, node, "match-origin", AUTH_MATCHTYPE_MATCH);
    auth_get_authenticator__filter_origin(auth, node, "nomatch-origin", AUTH_MATCHTYPE_NOMATCH);

901 902 903
    auth_get_authenticator__permission_alter(auth, node, "may-alter", AUTH_MATCHTYPE_MATCH);
    auth_get_authenticator__permission_alter(auth, node, "may-not-alter", AUTH_MATCHTYPE_NOMATCH);

904 905 906 907 908 909 910 911 912 913
    /* sub node parsing */
    child = node->xmlChildrenNode;
    do {
        if (child == NULL)
            break;

        if (xmlIsBlankNode(child))
            continue;

        if (xmlStrcmp (child->name, XMLSTR("option")) == 0) {
Philipp Schafft's avatar
Philipp Schafft committed
914
            config_options_t *opt = calloc(1, sizeof (config_options_t));
915 916
            opt->name = (char *)xmlGetProp(child, XMLSTR("name"));
            if (opt->name == NULL) {
917 918 919
                free(opt);
                continue;
            }
920 921
            opt->value = (char *)xmlGetProp(child, XMLSTR("value"));
            if (opt->value == NULL) {
Philipp Schafft's avatar
Philipp Schafft committed
922 923
                xmlFree(opt->name);
                free(opt);
924 925 926 927
                continue;
            }
            *next_option = opt;
            next_option = &opt->next;
928 929
        } else if (xmlStrcmp (child->name, XMLSTR("http-headers")) == 0) {
            config_parse_http_headers(child->xmlChildrenNode, &(auth->http_headers));
930 931 932 933 934 935 936 937
        } else if (xmlStrcmp (child->name, XMLSTR("acl")) == 0) {
            if (!auth->acl) {
                auth->acl  = acl_new_from_xml_node(child);
            } else {
                ICECAST_LOG_ERROR("More than one ACL defined in role! Not supported (yet).");
            }
        } else {
            ICECAST_LOG_WARN("unknown auth setting (%H)", child->name);
938
        }
939 940 941 942 943
    } while ((child = child->next));

    if (!auth->acl) {
        /* If we did not get a <acl> try ACL as part of <role> (old style). */
        auth->acl  = acl_new_from_xml_node(node);
944
    }
Philipp Schafft's avatar
Philipp Schafft committed
945 946
    if (!auth->acl) {
        auth_release(auth);
Karl Heyes's avatar
Karl Heyes committed
947
        auth = NULL;
Philipp Schafft's avatar
Philipp Schafft committed
948 949 950 951 952 953
    } else {
        if (get_authenticator (auth, options) < 0) {
            auth_release(auth);
            auth = NULL;
        } else {
            auth->tailp = &auth->head;
954 955
            if (!auth->immediate) {
                auth->running = 1;
956
                auth->thread = thread_create("auth thread", auth_run_thread, auth, igloo_THREAD_ATTACHED);
957
            }
Philipp Schafft's avatar
Philipp Schafft committed
958
        }
Karl Heyes's avatar
Karl Heyes committed
959
    }
960

Philipp Schafft's avatar
Philipp Schafft committed
961
    while (options) {
962 963
        config_options_t *opt = options;
        options = opt->next;
Philipp Schafft's avatar
Philipp Schafft committed
964 965
        xmlFree(opt->name);
        xmlFree(opt->value);
966
        free (opt);
967
    }
Philipp Schafft's avatar
Philipp Schafft committed
968

969
    if (auth && !auth->management_url && (auth->adduser || auth->deleteuser || auth->listuser)) {
970 971 972 973 974
        char url[128];
        snprintf(url, sizeof(url), "/admin/manageauth.xsl?id=%lu", auth->id);
        auth->management_url = (char*)xmlCharStrdup(url);
    }

975 976
    return auth;
}
977

978 979 980 981 982
int auth_alter_client(auth_t *auth, auth_client *auth_user, auth_alter_t action, const char *arg)
{
    if (!auth || !auth_user || !arg)
        return -1;

983 984 985 986 987
    if (action < 0 || action >= (sizeof(auth->permission_alter)/sizeof(*(auth->permission_alter))))
        return -1;

    if (auth->permission_alter[action] != AUTH_MATCHTYPE_MATCH)
        return -1;
988 989 990 991 992 993 994 995

    if (replace_string(&(auth_user->alter_client_arg), arg) != 0)
        return -1;

    auth_user->alter_client_action = action;

    return 0;
}
996

997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
auth_alter_t auth_str2alter(const char *str)
{
    if (!str)
        return AUTH_ALTER_NOOP;

    if (strcasecmp(str, "noop") == 0) {
        return AUTH_ALTER_NOOP;
    } else if (strcasecmp(str, "rewrite") == 0) {
        return AUTH_ALTER_REWRITE;
    } else if (strcasecmp(str, "redirect") == 0) {
        return AUTH_ALTER_REDIRECT;
    } else if (strcasecmp(str, "redirect_see_other") == 0) {
        return AUTH_ALTER_REDIRECT_SEE_OTHER;
    } else if (strcasecmp(str, "redirect_temporary") == 0) {
        return AUTH_ALTER_REDIRECT_TEMPORARY;
    } else if (strcasecmp(str, "redirect_permanent") == 0) {
        return AUTH_ALTER_REDIRECT_PERMANENT;
    } else if (strcasecmp(str, "send_error") == 0) {
        return AUTH_ALTER_SEND_ERROR;
    } else {
        return AUTH_ALTER_NOOP;
    }
}

Philipp Schafft's avatar
Philipp Schafft committed
1021 1022 1023
/* these are called at server start and termination */

void auth_initialise (void)
1024
{
1025
    thread_mutex_create(&_auth_lock);
Philipp Schafft's avatar
Philipp Schafft committed
1026
}
1027

Philipp Schafft's avatar
Philipp Schafft committed
1028 1029 1030
void auth_shutdown (void)
{
    ICECAST_LOG_INFO("Auth shutdown");
1031
    igloo_thread_mutex_destroy(&_auth_lock);
Philipp Schafft's avatar
Philipp Schafft committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
}

/* authstack functions */

static void __move_client_forward_in_auth_stack(client_t *client, void (*on_result)(client_t *client, void *userdata, auth_result result), void *userdata) {
    auth_stack_next(&client->authstack);
    if (client->authstack) {
        auth_stack_add_client(client->authstack, client, on_result, userdata);
    } else {
        if (on_result)
            on_result(client, userdata, AUTH_NOMATCH);
1043
    }
Philipp Schafft's avatar
Philipp Schafft committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
}

void          auth_stack_add_client(auth_stack_t *stack, client_t *client, void (*on_result)(client_t *client, void *userdata, auth_result result), void *userdata) {
    auth_t *auth;

    if (!stack || !client || (client->authstack