auth_static.c 3.32 KB
Newer Older
Philipp Schafft's avatar
Philipp Schafft committed
1 2 3 4 5 6 7 8
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2014,      Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
 */

9
/**
Philipp Schafft's avatar
Philipp Schafft committed
10 11 12 13 14 15 16
 * Client authentication functions
 */

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

Philipp Schafft's avatar
Philipp Schafft committed
17 18 19
/* for strcmp() and strdup() */
#include <string.h>

Philipp Schafft's avatar
Philipp Schafft committed
20 21 22 23 24 25 26 27 28 29 30
#include "auth.h"
#include "client.h"

#include "logging.h"
#define CATMODULE "auth_static"

typedef struct auth_static {
    char *username;
    char *password;
} auth_static_t;

Marvin Scholz's avatar
Marvin Scholz committed
31 32 33 34 35
static auth_result static_auth(auth_client *auth_user)
{
    client_t        *client     = auth_user->client;
    auth_t          *auth       = client->auth;
    auth_static_t   *auth_info  = auth->state;
Philipp Schafft's avatar
Philipp Schafft committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    if (auth_info->username) {
        if (!client->username)
            return AUTH_NOMATCH;
        if (strcmp(auth_info->username, client->username) != 0)
            return AUTH_NOMATCH;
    }

    if (!client->password)
        return AUTH_NOMATCH;

    if (strcmp(auth_info->password, client->password) == 0)
        return AUTH_OK;

    return AUTH_FAILED;
}

Marvin Scholz's avatar
Marvin Scholz committed
53 54
static void clear_auth (auth_t *auth)
{
Philipp Schafft's avatar
Philipp Schafft committed
55
    auth_static_t *auth_info = auth->state;
Marvin Scholz's avatar
Marvin Scholz committed
56 57 58 59
    if (auth_info->username)
        free(auth_info->username);
    if (auth_info->password)
        free(auth_info->password);
Philipp Schafft's avatar
Philipp Schafft committed
60 61 62 63
    free(auth_info);
    auth->state = NULL;
}

Marvin Scholz's avatar
Marvin Scholz committed
64 65
static auth_result static_userlist(auth_t *auth, xmlNodePtr srcnode)
{
66
    auth_static_t *auth_info = auth->state;
Marvin Scholz's avatar
Marvin Scholz committed
67
    xmlNodePtr    newnode;
68

69
    newnode = xmlNewChild(srcnode, NULL, XMLSTR("user"), NULL);
70 71
    xmlNewTextChild(newnode, NULL, XMLSTR("username"), XMLSTR(auth_info->username));
    xmlNewTextChild(newnode, NULL, XMLSTR("password"), XMLSTR(auth_info->password));
72 73 74 75

    return AUTH_OK;
}

Marvin Scholz's avatar
Marvin Scholz committed
76 77
int  auth_get_static_auth (auth_t *authenticator, config_options_t *options)
{
Philipp Schafft's avatar
Philipp Schafft committed
78
    auth_static_t *auth_info;
Marvin Scholz's avatar
Marvin Scholz committed
79
    int           need_user;
Philipp Schafft's avatar
Philipp Schafft committed
80 81 82 83 84 85

    if (strcmp(authenticator->type, AUTH_TYPE_STATIC) == 0) {
        need_user = 1;
    } else if (strcmp(authenticator->type, AUTH_TYPE_LEGACY_PASSWORD) == 0) {
        need_user = 0;
    } else {
86
        ICECAST_LOG_ERROR("Unknown type.");
Philipp Schafft's avatar
Philipp Schafft committed
87 88 89 90 91 92 93 94
        return -1;
    }

    auth_info = calloc(1, sizeof(auth_static_t));
    if (!auth_info)
        return -1;

    authenticator->authenticate_client = static_auth;
95
    authenticator->listuser = static_userlist;
Philipp Schafft's avatar
Philipp Schafft committed
96 97
    authenticator->free = clear_auth;
    authenticator->state = auth_info;
98
    authenticator->immediate = 1;
Philipp Schafft's avatar
Philipp Schafft committed
99 100 101

    while (options) {
        if (strcmp(options->name, "username") == 0) {
Marvin Scholz's avatar
Marvin Scholz committed
102 103
            if (auth_info->username)
                free(auth_info->username);
Philipp Schafft's avatar
Philipp Schafft committed
104 105
            auth_info->username = strdup(options->value);
        } else if (strcmp(options->name, "password") == 0) {
Marvin Scholz's avatar
Marvin Scholz committed
106 107
            if (auth_info->password)
                free(auth_info->password);
Philipp Schafft's avatar
Philipp Schafft committed
108 109 110 111 112 113 114 115
            auth_info->password = strdup(options->value);
        } else {
            ICECAST_LOG_ERROR("Unknown option: %s", options->name);
        }
        options = options->next;
    }

    if (need_user && !auth_info->username) {
116
        ICECAST_LOG_ERROR("Username required, but not given.");
Philipp Schafft's avatar
Philipp Schafft committed
117 118 119
        clear_auth(authenticator);
        return -1;
    } else if (!auth_info->password) {
120
        ICECAST_LOG_ERROR("Password required, but not given.");
Philipp Schafft's avatar
Philipp Schafft committed
121 122 123 124 125 126
        clear_auth(authenticator);
        return -1;
    }

    return 0;
}