willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * URI-based user authentication using the HTTP basic method. |
| 3 | * |
Willy Tarreau | bbd4212 | 2007-07-25 07:26:38 +0200 | [diff] [blame] | 4 | * Copyright 2006-2007 Willy Tarreau <w@1wt.eu> |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 15 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 16 | #include <common/base64.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 17 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 18 | #include <common/uri_auth.h> |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 19 | |
| 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 | */ |
| 25 | struct 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 | */ |
| 63 | struct 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 Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 76 | free(u->uri_prefix); |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 77 | u->uri_prefix = uri_copy; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 78 | u->uri_len = uri_len; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 79 | 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 | */ |
| 91 | struct 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 Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 102 | free(u->auth_realm); |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 103 | 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 | /* |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 113 | * Returns a default uri_auth with ST_SHNODE flag enabled and |
| 114 | * <node> set as the name if it is not empty. |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 115 | * Uses the pointer provided if not NULL and not initialized. |
| 116 | */ |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 117 | struct uri_auth *stats_set_node(struct uri_auth **root, char *name) |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 118 | { |
| 119 | struct uri_auth *u; |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 120 | char *node_copy = NULL; |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 121 | |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 122 | if (name && *name) { |
| 123 | node_copy = strdup(name); |
| 124 | if (node_copy == NULL) |
| 125 | goto out_realm; |
| 126 | } |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 127 | |
| 128 | if ((u = stats_check_init_uri_auth(root)) == NULL) |
| 129 | goto out_u; |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 130 | |
| 131 | if (!stats_set_flag(root, ST_SHNODE)) |
| 132 | goto out_u; |
| 133 | |
| 134 | if (node_copy) { |
| 135 | free(u->node); |
| 136 | u->node = node_copy; |
| 137 | } |
| 138 | |
| 139 | return u; |
| 140 | |
| 141 | out_u: |
| 142 | free(node_copy); |
| 143 | out_realm: |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Returns a default uri_auth with ST_SHDESC flag enabled and |
| 149 | * <description> set as the desc if it is not empty. |
| 150 | * Uses the pointer provided if not NULL and not initialized. |
| 151 | */ |
| 152 | struct uri_auth *stats_set_desc(struct uri_auth **root, char *desc) |
| 153 | { |
| 154 | struct uri_auth *u; |
| 155 | char *desc_copy = NULL; |
| 156 | |
| 157 | if (desc && *desc) { |
| 158 | desc_copy = strdup(desc); |
| 159 | if (desc_copy == NULL) |
| 160 | goto out_realm; |
| 161 | } |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 162 | |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 163 | if ((u = stats_check_init_uri_auth(root)) == NULL) |
| 164 | goto out_u; |
| 165 | |
| 166 | if (!stats_set_flag(root, ST_SHDESC)) |
| 167 | goto out_u; |
| 168 | |
| 169 | if (desc_copy) { |
| 170 | free(u->desc); |
| 171 | u->desc = desc_copy; |
| 172 | } |
| 173 | |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 174 | return u; |
| 175 | |
| 176 | out_u: |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 177 | free(desc_copy); |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 178 | out_realm: |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | /* |
Willy Tarreau | bbd4212 | 2007-07-25 07:26:38 +0200 | [diff] [blame] | 183 | * Returns a default uri_auth with the <refresh> refresh interval. |
| 184 | * Uses the pointer provided if not NULL and not initialized. |
| 185 | */ |
| 186 | struct uri_auth *stats_set_refresh(struct uri_auth **root, int interval) |
| 187 | { |
| 188 | struct uri_auth *u; |
| 189 | |
| 190 | if ((u = stats_check_init_uri_auth(root)) != NULL) |
| 191 | u->refresh = interval; |
| 192 | return u; |
| 193 | } |
| 194 | |
| 195 | /* |
Krzysztof Oledzki | d9db927 | 2007-10-15 10:05:11 +0200 | [diff] [blame] | 196 | * Returns a default uri_auth with the <flag> set. |
| 197 | * Uses the pointer provided if not NULL and not initialized. |
| 198 | */ |
| 199 | struct uri_auth *stats_set_flag(struct uri_auth **root, int flag) |
| 200 | { |
| 201 | struct uri_auth *u; |
| 202 | |
| 203 | if ((u = stats_check_init_uri_auth(root)) != NULL) |
| 204 | u->flags |= flag; |
| 205 | return u; |
| 206 | } |
| 207 | |
| 208 | /* |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 209 | * Returns a default uri_auth with a <user:passwd> entry added to the list of |
| 210 | * authorized users. If a matching entry is found, no update will be performed. |
| 211 | * Uses the pointer provided if not NULL and not initialized. |
| 212 | */ |
| 213 | struct uri_auth *stats_add_auth(struct uri_auth **root, char *auth) |
| 214 | { |
| 215 | struct uri_auth *u; |
| 216 | char *auth_base64; |
| 217 | int alen, blen; |
| 218 | struct user_auth *users, **ulist; |
| 219 | |
| 220 | alen = strlen(auth); |
| 221 | blen = ((alen + 2) / 3) * 4; |
| 222 | |
| 223 | if ((auth_base64 = (char *)calloc(1, blen + 1)) == NULL) |
| 224 | goto out_ubase64; |
| 225 | |
| 226 | /* convert user:passwd to base64. It should return exactly blen */ |
| 227 | if (a2base64(auth, alen, auth_base64, blen + 1) != blen) |
| 228 | goto out_base64; |
| 229 | |
| 230 | if ((u = stats_check_init_uri_auth(root)) == NULL) |
| 231 | goto out_base64; |
| 232 | |
| 233 | ulist = &u->users; |
| 234 | while ((users = *ulist)) { |
| 235 | if (!strcmp(users->user_pwd, auth_base64)) |
| 236 | break; |
| 237 | ulist = &users->next; |
| 238 | } |
| 239 | |
| 240 | if (!users) { |
| 241 | if ((users = (struct user_auth *)calloc(1, sizeof(*users))) == NULL) |
| 242 | goto out_u; |
| 243 | *ulist = users; |
| 244 | users->user_pwd = auth_base64; |
| 245 | users->user_len = blen; |
| 246 | } |
| 247 | return u; |
| 248 | |
| 249 | out_u: |
| 250 | free(u); |
| 251 | out_base64: |
| 252 | free(auth_base64); |
| 253 | out_ubase64: |
| 254 | return NULL; |
| 255 | } |
| 256 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 257 | /* |
| 258 | * Returns a default uri_auth with a <scope> entry added to the list of |
| 259 | * allowed scopes. If a matching entry is found, no update will be performed. |
| 260 | * Uses the pointer provided if not NULL and not initialized. |
| 261 | */ |
| 262 | struct uri_auth *stats_add_scope(struct uri_auth **root, char *scope) |
| 263 | { |
| 264 | struct uri_auth *u; |
| 265 | char *new_name; |
| 266 | struct stat_scope *old_scope, **scope_list; |
| 267 | |
| 268 | if ((u = stats_check_init_uri_auth(root)) == NULL) |
| 269 | goto out; |
| 270 | |
| 271 | scope_list = &u->scope; |
| 272 | while ((old_scope = *scope_list)) { |
| 273 | if (!strcmp(old_scope->px_id, scope)) |
| 274 | break; |
| 275 | scope_list = &old_scope->next; |
| 276 | } |
| 277 | |
| 278 | if (!old_scope) { |
| 279 | if ((new_name = strdup(scope)) == NULL) |
| 280 | goto out_u; |
| 281 | |
| 282 | if ((old_scope = (struct stat_scope *)calloc(1, sizeof(*old_scope))) == NULL) |
| 283 | goto out_name; |
| 284 | |
| 285 | old_scope->px_id = new_name; |
| 286 | old_scope->px_len = strlen(new_name); |
| 287 | *scope_list = old_scope; |
| 288 | } |
| 289 | return u; |
| 290 | |
| 291 | out_name: |
| 292 | free(new_name); |
| 293 | out_u: |
| 294 | free(u); |
| 295 | out: |
| 296 | return NULL; |
| 297 | } |
| 298 | |
Willy Tarreau | bbd4212 | 2007-07-25 07:26:38 +0200 | [diff] [blame] | 299 | /* |
| 300 | * Local variables: |
| 301 | * c-indent-level: 8 |
| 302 | * c-basic-offset: 8 |
| 303 | * End: |
| 304 | */ |