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