auth.h 5.12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* 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).
11
 * Copyright 2014-2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
12
13
 */

Michael Smith's avatar
Michael Smith committed
14
15
16
#ifndef __AUTH_H__
#define __AUTH_H__

Karl Heyes's avatar
Karl Heyes committed
17
18
19
20
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

21
22
23
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
24

Marvin Scholz's avatar
Marvin Scholz committed
25
#include "common/thread/thread.h"
26
27
28
#include "common/httpp/httpp.h"

#include "icecasttypes.h"
Michael Smith's avatar
Michael Smith committed
29

Philipp Schafft's avatar
Philipp Schafft committed
30
31
32
33
34
35
36
/* implemented */
#define AUTH_TYPE_ANONYMOUS       "anonymous"
#define AUTH_TYPE_STATIC          "static"
#define AUTH_TYPE_LEGACY_PASSWORD "legacy-password"
#define AUTH_TYPE_URL             "url"
#define AUTH_TYPE_HTPASSWD        "htpasswd"

37
38
#define MAX_ADMIN_COMMANDS 32

Michael Smith's avatar
Michael Smith committed
39
40
typedef enum
{
41
    /* XXX: ??? */
42
    AUTH_UNDEFINED,
43
    /* user authed successfull */
Michael Smith's avatar
Michael Smith committed
44
    AUTH_OK,
45
    /* user authed failed */
Michael Smith's avatar
Michael Smith committed
46
    AUTH_FAILED,
Philipp Schafft's avatar
Philipp Schafft committed
47
48
    /* session got terminated */
    AUTH_RELEASED,
49
    /* XXX: ??? */
50
    AUTH_FORBIDDEN,
51
52
53
    /* No match for given username or other identifier found */
    AUTH_NOMATCH,
    /* status codes for database changes */
54
55
    AUTH_USERADDED,
    AUTH_USEREXISTS,
56
    AUTH_USERDELETED
Michael Smith's avatar
Michael Smith committed
57
58
} auth_result;

59
60
61
62
63
64
65
66
67
typedef enum {
    /* The slot is not used */
    AUTH_MATCHTYPE_UNUSED,
    /* Match on this slot */
    AUTH_MATCHTYPE_MATCH,
    /* Do not match on this slot */
    AUTH_MATCHTYPE_NOMATCH
} auth_matchtype_t;

Philipp Schafft's avatar
Philipp Schafft committed
68
69
typedef struct auth_client_tag auth_client;
struct auth_client_tag {
Philipp Schafft's avatar
Philipp Schafft committed
70
    client_t     *client;
Philipp Schafft's avatar
Philipp Schafft committed
71
    auth_result (*process)(auth_t *auth, auth_client *auth_user);
Philipp Schafft's avatar
Philipp Schafft committed
72
73
74
    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;
Philipp Schafft's avatar
Philipp Schafft committed
75
76
    auth_client  *next;
};
77
78


79
struct auth_tag
Michael Smith's avatar
Michael Smith committed
80
{
81
82
83
    /* unique ID */
    unsigned long id;

84
85
86
    /* URL for any kind of UI used to configure this or NULL. */
    char *management_url;

87
88
    char *mount;

Philipp Schafft's avatar
Philipp Schafft committed
89
    /* filters */
90
    auth_matchtype_t filter_method[httpp_req_unknown+1];
91
92
93
94
95
96
    auth_matchtype_t filter_web_policy;
    auth_matchtype_t filter_admin_policy;
    struct {
        auth_matchtype_t type;
        admin_command_id_t command;
    } filter_admin[MAX_ADMIN_COMMANDS];
97

98
99
100
101
102
    /* whether authenticate_client() and release_client() will return immediate.
     * Setting this will result in no thread being started for this.
     */
    int immediate;

Philipp Schafft's avatar
Philipp Schafft committed
103
104
105
    /* Authenticate using the given username and password */
    auth_result (*authenticate_client)(auth_client *aclient);
    auth_result (*release_client)(auth_client *auth_user);
106

107
    /* auth state-specific free call */
108
    void (*free)(auth_t *self);
109

110
111
112
    auth_result (*adduser)(auth_t *auth, const char *username, const char *password);
    auth_result (*deleteuser)(auth_t *auth, const char *username);
    auth_result (*listuser)(auth_t *auth, xmlNodePtr srcnode);
113

114
    mutex_t lock;
115
    int running;
Philipp Schafft's avatar
Philipp Schafft committed
116
    size_t refcount;
117

118
119
120
121
122
123
    thread_type *thread;

    /* per-auth queue for clients */
    auth_client *head, **tailp;
    int pending_count;

Michael Smith's avatar
Michael Smith committed
124
    void *state;
125
    char *type;
Philipp Schafft's avatar
Philipp Schafft committed
126
127
128
129
130
131
    char *unique_tag;

    /* acl to set on succsessful auth */
    acl_t *acl;
    /* role name for later matching, may be NULL if no role name was given in config */
    char  *role;
132
};
Michael Smith's avatar
Michael Smith committed
133

134
/* prototypes for auths that do not need own header file */
Marvin Scholz's avatar
Marvin Scholz committed
135
136
137
int auth_get_anonymous_auth(auth_t *auth, config_options_t *options);
int auth_get_static_auth(auth_t *auth, config_options_t *options);
int auth_get_url_auth(auth_t *authenticator, config_options_t *options);
138
139
140
int auth_get_htpasswd_auth(auth_t *auth, config_options_t *options);

/* prototypes for auth.c */
Marvin Scholz's avatar
Marvin Scholz committed
141
142
void auth_initialise(void);
void auth_shutdown(void);
143

Marvin Scholz's avatar
Marvin Scholz committed
144
145
146
auth_t  *auth_get_authenticator(xmlNodePtr node);
void    auth_release(auth_t *authenticator);
void    auth_addref(auth_t *authenticator);
147

Marvin Scholz's avatar
Marvin Scholz committed
148
int auth_release_client(client_t *client);
Michael Smith's avatar
Michael Smith committed
149

Marvin Scholz's avatar
Marvin Scholz committed
150
151
152
153
154
155
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);
156

Philipp Schafft's avatar
Philipp Schafft committed
157
158
159
160
161
162
void          auth_stack_release(auth_stack_t *stack);
void          auth_stack_addref(auth_stack_t *stack);
int           auth_stack_next(auth_stack_t **stack); /* returns -1 on error, 0 on success, +1 if no next element is present */
int           auth_stack_push(auth_stack_t **stack, auth_t *auth);
int           auth_stack_append(auth_stack_t *stack, auth_stack_t *tail);
auth_t       *auth_stack_get(auth_stack_t *stack);
163
auth_t       *auth_stack_getbyid(auth_stack_t *stack, unsigned long id);
164
acl_t        *auth_stack_get_anonymous_acl(auth_stack_t *stack, httpp_request_type_e method);
Michael Smith's avatar
Michael Smith committed
165
166

#endif