blob: ba029a6ba020e4a00c6420dea4ab9d77ff17af8f [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/*
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200113 * Returns a default uri_auth with <node-name> set as the node name.
114 * Uses the pointer provided if not NULL and not initialized.
115 */
116struct uri_auth *stats_set_node_name(struct uri_auth **root, char *name)
117{
118 struct uri_auth *u;
119 char *name_copy;
120
121 if ((name_copy = strdup(name)) == NULL)
122 goto out_realm;
123
124 if ((u = stats_check_init_uri_auth(root)) == NULL)
125 goto out_u;
126
127 free(u->node_name);
128 u->node_name = name_copy;
129 return u;
130
131 out_u:
132 free(name_copy);
133 out_realm:
134 return NULL;
135}
136
137/*
Willy Tarreaubbd42122007-07-25 07:26:38 +0200138 * Returns a default uri_auth with the <refresh> refresh interval.
139 * Uses the pointer provided if not NULL and not initialized.
140 */
141struct uri_auth *stats_set_refresh(struct uri_auth **root, int interval)
142{
143 struct uri_auth *u;
144
145 if ((u = stats_check_init_uri_auth(root)) != NULL)
146 u->refresh = interval;
147 return u;
148}
149
150/*
Krzysztof Oledzkid9db9272007-10-15 10:05:11 +0200151 * Returns a default uri_auth with the <flag> set.
152 * Uses the pointer provided if not NULL and not initialized.
153 */
154struct uri_auth *stats_set_flag(struct uri_auth **root, int flag)
155{
156 struct uri_auth *u;
157
158 if ((u = stats_check_init_uri_auth(root)) != NULL)
159 u->flags |= flag;
160 return u;
161}
162
163/*
willy tarreau9e138862006-05-14 23:06:28 +0200164 * Returns a default uri_auth with a <user:passwd> entry added to the list of
165 * authorized users. If a matching entry is found, no update will be performed.
166 * Uses the pointer provided if not NULL and not initialized.
167 */
168struct uri_auth *stats_add_auth(struct uri_auth **root, char *auth)
169{
170 struct uri_auth *u;
171 char *auth_base64;
172 int alen, blen;
173 struct user_auth *users, **ulist;
174
175 alen = strlen(auth);
176 blen = ((alen + 2) / 3) * 4;
177
178 if ((auth_base64 = (char *)calloc(1, blen + 1)) == NULL)
179 goto out_ubase64;
180
181 /* convert user:passwd to base64. It should return exactly blen */
182 if (a2base64(auth, alen, auth_base64, blen + 1) != blen)
183 goto out_base64;
184
185 if ((u = stats_check_init_uri_auth(root)) == NULL)
186 goto out_base64;
187
188 ulist = &u->users;
189 while ((users = *ulist)) {
190 if (!strcmp(users->user_pwd, auth_base64))
191 break;
192 ulist = &users->next;
193 }
194
195 if (!users) {
196 if ((users = (struct user_auth *)calloc(1, sizeof(*users))) == NULL)
197 goto out_u;
198 *ulist = users;
199 users->user_pwd = auth_base64;
200 users->user_len = blen;
201 }
202 return u;
203
204 out_u:
205 free(u);
206 out_base64:
207 free(auth_base64);
208 out_ubase64:
209 return NULL;
210}
211
willy tarreau1f431b52006-05-21 14:46:15 +0200212/*
213 * Returns a default uri_auth with a <scope> entry added to the list of
214 * allowed scopes. If a matching entry is found, no update will be performed.
215 * Uses the pointer provided if not NULL and not initialized.
216 */
217struct uri_auth *stats_add_scope(struct uri_auth **root, char *scope)
218{
219 struct uri_auth *u;
220 char *new_name;
221 struct stat_scope *old_scope, **scope_list;
222
223 if ((u = stats_check_init_uri_auth(root)) == NULL)
224 goto out;
225
226 scope_list = &u->scope;
227 while ((old_scope = *scope_list)) {
228 if (!strcmp(old_scope->px_id, scope))
229 break;
230 scope_list = &old_scope->next;
231 }
232
233 if (!old_scope) {
234 if ((new_name = strdup(scope)) == NULL)
235 goto out_u;
236
237 if ((old_scope = (struct stat_scope *)calloc(1, sizeof(*old_scope))) == NULL)
238 goto out_name;
239
240 old_scope->px_id = new_name;
241 old_scope->px_len = strlen(new_name);
242 *scope_list = old_scope;
243 }
244 return u;
245
246 out_name:
247 free(new_name);
248 out_u:
249 free(u);
250 out:
251 return NULL;
252}
253
Willy Tarreaubbd42122007-07-25 07:26:38 +0200254/*
255 * Local variables:
256 * c-indent-level: 8
257 * c-basic-offset: 8
258 * End:
259 */