blob: f666c0dfa0d5dd19035c8236b903b437c2356261 [file] [log] [blame]
willy tarreau9e138862006-05-14 23:06:28 +02001/*
2 * URI-based user authentication using the HTTP basic method.
3 *
Willy Tarreaubaaee002006-06-26 02:48:02 +02004 * Copyright 2006 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/*
117 * Returns a default uri_auth with a <user:passwd> entry added to the list of
118 * authorized users. If a matching entry is found, no update will be performed.
119 * Uses the pointer provided if not NULL and not initialized.
120 */
121struct uri_auth *stats_add_auth(struct uri_auth **root, char *auth)
122{
123 struct uri_auth *u;
124 char *auth_base64;
125 int alen, blen;
126 struct user_auth *users, **ulist;
127
128 alen = strlen(auth);
129 blen = ((alen + 2) / 3) * 4;
130
131 if ((auth_base64 = (char *)calloc(1, blen + 1)) == NULL)
132 goto out_ubase64;
133
134 /* convert user:passwd to base64. It should return exactly blen */
135 if (a2base64(auth, alen, auth_base64, blen + 1) != blen)
136 goto out_base64;
137
138 if ((u = stats_check_init_uri_auth(root)) == NULL)
139 goto out_base64;
140
141 ulist = &u->users;
142 while ((users = *ulist)) {
143 if (!strcmp(users->user_pwd, auth_base64))
144 break;
145 ulist = &users->next;
146 }
147
148 if (!users) {
149 if ((users = (struct user_auth *)calloc(1, sizeof(*users))) == NULL)
150 goto out_u;
151 *ulist = users;
152 users->user_pwd = auth_base64;
153 users->user_len = blen;
154 }
155 return u;
156
157 out_u:
158 free(u);
159 out_base64:
160 free(auth_base64);
161 out_ubase64:
162 return NULL;
163}
164
willy tarreau1f431b52006-05-21 14:46:15 +0200165/*
166 * Returns a default uri_auth with a <scope> entry added to the list of
167 * allowed scopes. If a matching entry is found, no update will be performed.
168 * Uses the pointer provided if not NULL and not initialized.
169 */
170struct uri_auth *stats_add_scope(struct uri_auth **root, char *scope)
171{
172 struct uri_auth *u;
173 char *new_name;
174 struct stat_scope *old_scope, **scope_list;
175
176 if ((u = stats_check_init_uri_auth(root)) == NULL)
177 goto out;
178
179 scope_list = &u->scope;
180 while ((old_scope = *scope_list)) {
181 if (!strcmp(old_scope->px_id, scope))
182 break;
183 scope_list = &old_scope->next;
184 }
185
186 if (!old_scope) {
187 if ((new_name = strdup(scope)) == NULL)
188 goto out_u;
189
190 if ((old_scope = (struct stat_scope *)calloc(1, sizeof(*old_scope))) == NULL)
191 goto out_name;
192
193 old_scope->px_id = new_name;
194 old_scope->px_len = strlen(new_name);
195 *scope_list = old_scope;
196 }
197 return u;
198
199 out_name:
200 free(new_name);
201 out_u:
202 free(u);
203 out:
204 return NULL;
205}
206