blob: 3cfc445263c3f0a38be35f72eb4b7ec1bced81cf [file] [log] [blame]
willy tarreau9e138862006-05-14 23:06:28 +02001/*
2 * URI-based user authentication using the HTTP basic method.
3 *
Willy Tarreaubbd42122007-07-25 07:26:38 +02004 * Copyright 2006-2007 Willy Tarreau <w@1wt.eu>
willy tarreau9e138862006-05-14 23:06:28 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdlib.h>
14#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015
Willy Tarreau2dd0d472006-06-29 17:53:05 +020016#include <common/base64.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020017#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020018#include <common/uri_auth.h>
willy tarreau9e138862006-05-14 23:06:28 +020019
20
21/*
22 * Initializes a basic uri_auth structure header and returns a pointer to it.
23 * Uses the pointer provided if not NULL and not initialized.
24 */
25struct uri_auth *stats_check_init_uri_auth(struct uri_auth **root)
26{
27 struct uri_auth *u;
28
29 if (!root || !*root) {
30 if ((u = (struct uri_auth *)calloc(1, sizeof (*u))) == NULL)
31 goto out_u;
32 } else
33 u = *root;
34
35 if (!u->uri_prefix) {
36 u->uri_len = strlen(STATS_DEFAULT_URI);
37 if ((u->uri_prefix = strdup(STATS_DEFAULT_URI)) == NULL)
38 goto out_uri;
39 }
40
41 if (!u->auth_realm)
42 if ((u->auth_realm = strdup(STATS_DEFAULT_REALM)) == NULL)
43 goto out_realm;
44
45 if (root && !*root)
46 *root = u;
47
48 return u;
49
50 out_realm:
51 free(u->uri_prefix);
52 out_uri:
53 if (!root || !*root)
54 free(u);
55 out_u:
56 return NULL;
57}
58
59/*
60 * Returns a default uri_auth with <uri> set as the uri_prefix.
61 * Uses the pointer provided if not NULL and not initialized.
62 */
63struct uri_auth *stats_set_uri(struct uri_auth **root, char *uri)
64{
65 struct uri_auth *u;
66 char *uri_copy;
67 int uri_len;
68
69 uri_len = strlen(uri);
70 if ((uri_copy = strdup(uri)) == NULL)
71 goto out_uri;
72
73 if ((u = stats_check_init_uri_auth(root)) == NULL)
74 goto out_u;
75
Willy Tarreaua534fea2008-08-03 12:19:50 +020076 free(u->uri_prefix);
willy tarreau9e138862006-05-14 23:06:28 +020077 u->uri_prefix = uri_copy;
Willy Tarreaua534fea2008-08-03 12:19:50 +020078 u->uri_len = uri_len;
willy tarreau9e138862006-05-14 23:06:28 +020079 return u;
80
81 out_u:
82 free(uri_copy);
83 out_uri:
84 return NULL;
85}
86
87/*
88 * Returns a default uri_auth with <realm> set as the realm.
89 * Uses the pointer provided if not NULL and not initialized.
90 */
91struct uri_auth *stats_set_realm(struct uri_auth **root, char *realm)
92{
93 struct uri_auth *u;
94 char *realm_copy;
95
96 if ((realm_copy = strdup(realm)) == NULL)
97 goto out_realm;
98
99 if ((u = stats_check_init_uri_auth(root)) == NULL)
100 goto out_u;
101
Willy Tarreaua534fea2008-08-03 12:19:50 +0200102 free(u->auth_realm);
willy tarreau9e138862006-05-14 23:06:28 +0200103 u->auth_realm = realm_copy;
104 return u;
105
106 out_u:
107 free(realm_copy);
108 out_realm:
109 return NULL;
110}
111
112/*
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200113 * Returns a default uri_auth with ST_SHNODE flag enabled and
114 * <node> set as the name if it is not empty.
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200115 * Uses the pointer provided if not NULL and not initialized.
116 */
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200117struct uri_auth *stats_set_node(struct uri_auth **root, char *name)
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200118{
119 struct uri_auth *u;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200120 char *node_copy = NULL;
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200121
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200122 if (name && *name) {
123 node_copy = strdup(name);
124 if (node_copy == NULL)
125 goto out_realm;
126 }
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200127
128 if ((u = stats_check_init_uri_auth(root)) == NULL)
129 goto out_u;
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200130
131 if (!stats_set_flag(root, ST_SHNODE))
132 goto out_u;
133
134 if (node_copy) {
135 free(u->node);
136 u->node = node_copy;
137 }
138
139 return u;
140
141 out_u:
142 free(node_copy);
143 out_realm:
144 return NULL;
145}
146
147/*
148 * Returns a default uri_auth with ST_SHDESC flag enabled and
149 * <description> set as the desc if it is not empty.
150 * Uses the pointer provided if not NULL and not initialized.
151 */
152struct uri_auth *stats_set_desc(struct uri_auth **root, char *desc)
153{
154 struct uri_auth *u;
155 char *desc_copy = NULL;
156
157 if (desc && *desc) {
158 desc_copy = strdup(desc);
159 if (desc_copy == NULL)
160 goto out_realm;
161 }
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200162
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200163 if ((u = stats_check_init_uri_auth(root)) == NULL)
164 goto out_u;
165
166 if (!stats_set_flag(root, ST_SHDESC))
167 goto out_u;
168
169 if (desc_copy) {
170 free(u->desc);
171 u->desc = desc_copy;
172 }
173
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200174 return u;
175
176 out_u:
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200177 free(desc_copy);
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200178 out_realm:
179 return NULL;
180}
181
182/*
Willy Tarreaubbd42122007-07-25 07:26:38 +0200183 * Returns a default uri_auth with the <refresh> refresh interval.
184 * Uses the pointer provided if not NULL and not initialized.
185 */
186struct uri_auth *stats_set_refresh(struct uri_auth **root, int interval)
187{
188 struct uri_auth *u;
189
190 if ((u = stats_check_init_uri_auth(root)) != NULL)
191 u->refresh = interval;
192 return u;
193}
194
195/*
Krzysztof Oledzkid9db9272007-10-15 10:05:11 +0200196 * Returns a default uri_auth with the <flag> set.
197 * Uses the pointer provided if not NULL and not initialized.
198 */
199struct uri_auth *stats_set_flag(struct uri_auth **root, int flag)
200{
201 struct uri_auth *u;
202
203 if ((u = stats_check_init_uri_auth(root)) != NULL)
204 u->flags |= flag;
205 return u;
206}
207
208/*
willy tarreau9e138862006-05-14 23:06:28 +0200209 * Returns a default uri_auth with a <user:passwd> entry added to the list of
210 * authorized users. If a matching entry is found, no update will be performed.
211 * Uses the pointer provided if not NULL and not initialized.
212 */
213struct uri_auth *stats_add_auth(struct uri_auth **root, char *auth)
214{
215 struct uri_auth *u;
216 char *auth_base64;
217 int alen, blen;
218 struct user_auth *users, **ulist;
219
220 alen = strlen(auth);
221 blen = ((alen + 2) / 3) * 4;
222
223 if ((auth_base64 = (char *)calloc(1, blen + 1)) == NULL)
224 goto out_ubase64;
225
226 /* convert user:passwd to base64. It should return exactly blen */
227 if (a2base64(auth, alen, auth_base64, blen + 1) != blen)
228 goto out_base64;
229
230 if ((u = stats_check_init_uri_auth(root)) == NULL)
231 goto out_base64;
232
233 ulist = &u->users;
234 while ((users = *ulist)) {
235 if (!strcmp(users->user_pwd, auth_base64))
236 break;
237 ulist = &users->next;
238 }
239
240 if (!users) {
241 if ((users = (struct user_auth *)calloc(1, sizeof(*users))) == NULL)
242 goto out_u;
243 *ulist = users;
244 users->user_pwd = auth_base64;
245 users->user_len = blen;
246 }
247 return u;
248
249 out_u:
250 free(u);
251 out_base64:
252 free(auth_base64);
253 out_ubase64:
254 return NULL;
255}
256
willy tarreau1f431b52006-05-21 14:46:15 +0200257/*
258 * Returns a default uri_auth with a <scope> entry added to the list of
259 * allowed scopes. If a matching entry is found, no update will be performed.
260 * Uses the pointer provided if not NULL and not initialized.
261 */
262struct uri_auth *stats_add_scope(struct uri_auth **root, char *scope)
263{
264 struct uri_auth *u;
265 char *new_name;
266 struct stat_scope *old_scope, **scope_list;
267
268 if ((u = stats_check_init_uri_auth(root)) == NULL)
269 goto out;
270
271 scope_list = &u->scope;
272 while ((old_scope = *scope_list)) {
273 if (!strcmp(old_scope->px_id, scope))
274 break;
275 scope_list = &old_scope->next;
276 }
277
278 if (!old_scope) {
279 if ((new_name = strdup(scope)) == NULL)
280 goto out_u;
281
282 if ((old_scope = (struct stat_scope *)calloc(1, sizeof(*old_scope))) == NULL)
283 goto out_name;
284
285 old_scope->px_id = new_name;
286 old_scope->px_len = strlen(new_name);
287 *scope_list = old_scope;
288 }
289 return u;
290
291 out_name:
292 free(new_name);
293 out_u:
294 free(u);
295 out:
296 return NULL;
297}
298
Willy Tarreaubbd42122007-07-25 07:26:38 +0200299/*
300 * Local variables:
301 * c-indent-level: 8
302 * c-basic-offset: 8
303 * End:
304 */