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