blob: 8ac618cc373a2fcad43a56610668cda0d2fb8c88 [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
76 if (u->uri_prefix)
77 free(u->uri_prefix);
78
79 u->uri_len = uri_len;
80 u->uri_prefix = uri_copy;
81 return u;
82
83 out_u:
84 free(uri_copy);
85 out_uri:
86 return NULL;
87}
88
89/*
90 * Returns a default uri_auth with <realm> set as the realm.
91 * Uses the pointer provided if not NULL and not initialized.
92 */
93struct uri_auth *stats_set_realm(struct uri_auth **root, char *realm)
94{
95 struct uri_auth *u;
96 char *realm_copy;
97
98 if ((realm_copy = strdup(realm)) == NULL)
99 goto out_realm;
100
101 if ((u = stats_check_init_uri_auth(root)) == NULL)
102 goto out_u;
103
104 if (u->auth_realm)
105 free(u->auth_realm);
106
107 u->auth_realm = realm_copy;
108 return u;
109
110 out_u:
111 free(realm_copy);
112 out_realm:
113 return NULL;
114}
115
116/*
Willy Tarreaubbd42122007-07-25 07:26:38 +0200117 * Returns a default uri_auth with the <refresh> refresh interval.
118 * Uses the pointer provided if not NULL and not initialized.
119 */
120struct uri_auth *stats_set_refresh(struct uri_auth **root, int interval)
121{
122 struct uri_auth *u;
123
124 if ((u = stats_check_init_uri_auth(root)) != NULL)
125 u->refresh = interval;
126 return u;
127}
128
129/*
Krzysztof Oledzkid9db9272007-10-15 10:05:11 +0200130 * Returns a default uri_auth with the <flag> set.
131 * Uses the pointer provided if not NULL and not initialized.
132 */
133struct uri_auth *stats_set_flag(struct uri_auth **root, int flag)
134{
135 struct uri_auth *u;
136
137 if ((u = stats_check_init_uri_auth(root)) != NULL)
138 u->flags |= flag;
139 return u;
140}
141
142/*
willy tarreau9e138862006-05-14 23:06:28 +0200143 * Returns a default uri_auth with a <user:passwd> entry added to the list of
144 * authorized users. If a matching entry is found, no update will be performed.
145 * Uses the pointer provided if not NULL and not initialized.
146 */
147struct uri_auth *stats_add_auth(struct uri_auth **root, char *auth)
148{
149 struct uri_auth *u;
150 char *auth_base64;
151 int alen, blen;
152 struct user_auth *users, **ulist;
153
154 alen = strlen(auth);
155 blen = ((alen + 2) / 3) * 4;
156
157 if ((auth_base64 = (char *)calloc(1, blen + 1)) == NULL)
158 goto out_ubase64;
159
160 /* convert user:passwd to base64. It should return exactly blen */
161 if (a2base64(auth, alen, auth_base64, blen + 1) != blen)
162 goto out_base64;
163
164 if ((u = stats_check_init_uri_auth(root)) == NULL)
165 goto out_base64;
166
167 ulist = &u->users;
168 while ((users = *ulist)) {
169 if (!strcmp(users->user_pwd, auth_base64))
170 break;
171 ulist = &users->next;
172 }
173
174 if (!users) {
175 if ((users = (struct user_auth *)calloc(1, sizeof(*users))) == NULL)
176 goto out_u;
177 *ulist = users;
178 users->user_pwd = auth_base64;
179 users->user_len = blen;
180 }
181 return u;
182
183 out_u:
184 free(u);
185 out_base64:
186 free(auth_base64);
187 out_ubase64:
188 return NULL;
189}
190
willy tarreau1f431b52006-05-21 14:46:15 +0200191/*
192 * Returns a default uri_auth with a <scope> entry added to the list of
193 * allowed scopes. If a matching entry is found, no update will be performed.
194 * Uses the pointer provided if not NULL and not initialized.
195 */
196struct uri_auth *stats_add_scope(struct uri_auth **root, char *scope)
197{
198 struct uri_auth *u;
199 char *new_name;
200 struct stat_scope *old_scope, **scope_list;
201
202 if ((u = stats_check_init_uri_auth(root)) == NULL)
203 goto out;
204
205 scope_list = &u->scope;
206 while ((old_scope = *scope_list)) {
207 if (!strcmp(old_scope->px_id, scope))
208 break;
209 scope_list = &old_scope->next;
210 }
211
212 if (!old_scope) {
213 if ((new_name = strdup(scope)) == NULL)
214 goto out_u;
215
216 if ((old_scope = (struct stat_scope *)calloc(1, sizeof(*old_scope))) == NULL)
217 goto out_name;
218
219 old_scope->px_id = new_name;
220 old_scope->px_len = strlen(new_name);
221 *scope_list = old_scope;
222 }
223 return u;
224
225 out_name:
226 free(new_name);
227 out_u:
228 free(u);
229 out:
230 return NULL;
231}
232
Willy Tarreaubbd42122007-07-25 07:26:38 +0200233/*
234 * Local variables:
235 * c-indent-level: 8
236 * c-basic-offset: 8
237 * End:
238 */