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 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 20 | #include <proto/log.h> |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Initializes a basic uri_auth structure header and returns a pointer to it. |
| 24 | * Uses the pointer provided if not NULL and not initialized. |
| 25 | */ |
| 26 | struct uri_auth *stats_check_init_uri_auth(struct uri_auth **root) |
| 27 | { |
| 28 | struct uri_auth *u; |
| 29 | |
| 30 | if (!root || !*root) { |
| 31 | if ((u = (struct uri_auth *)calloc(1, sizeof (*u))) == NULL) |
| 32 | goto out_u; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 33 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 34 | LIST_INIT(&u->http_req_rules); |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 35 | LIST_INIT(&u->admin_rules); |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 36 | } else |
| 37 | u = *root; |
| 38 | |
| 39 | if (!u->uri_prefix) { |
| 40 | u->uri_len = strlen(STATS_DEFAULT_URI); |
| 41 | if ((u->uri_prefix = strdup(STATS_DEFAULT_URI)) == NULL) |
| 42 | goto out_uri; |
| 43 | } |
| 44 | |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 45 | if (root && !*root) |
| 46 | *root = u; |
| 47 | |
| 48 | return u; |
| 49 | |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 50 | out_uri: |
| 51 | if (!root || !*root) |
| 52 | free(u); |
| 53 | out_u: |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Returns a default uri_auth with <uri> set as the uri_prefix. |
| 59 | * Uses the pointer provided if not NULL and not initialized. |
| 60 | */ |
| 61 | struct uri_auth *stats_set_uri(struct uri_auth **root, char *uri) |
| 62 | { |
| 63 | struct uri_auth *u; |
| 64 | char *uri_copy; |
| 65 | int uri_len; |
| 66 | |
| 67 | uri_len = strlen(uri); |
| 68 | if ((uri_copy = strdup(uri)) == NULL) |
| 69 | goto out_uri; |
| 70 | |
| 71 | if ((u = stats_check_init_uri_auth(root)) == NULL) |
| 72 | goto out_u; |
| 73 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 74 | free(u->uri_prefix); |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 75 | u->uri_prefix = uri_copy; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 76 | u->uri_len = uri_len; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 77 | return u; |
| 78 | |
| 79 | out_u: |
| 80 | free(uri_copy); |
| 81 | out_uri: |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Returns a default uri_auth with <realm> set as the realm. |
| 87 | * Uses the pointer provided if not NULL and not initialized. |
| 88 | */ |
| 89 | struct uri_auth *stats_set_realm(struct uri_auth **root, char *realm) |
| 90 | { |
| 91 | struct uri_auth *u; |
| 92 | char *realm_copy; |
| 93 | |
| 94 | if ((realm_copy = strdup(realm)) == NULL) |
| 95 | goto out_realm; |
| 96 | |
| 97 | if ((u = stats_check_init_uri_auth(root)) == NULL) |
| 98 | goto out_u; |
| 99 | |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 100 | free(u->auth_realm); |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 101 | u->auth_realm = realm_copy; |
| 102 | return u; |
| 103 | |
| 104 | out_u: |
| 105 | free(realm_copy); |
| 106 | out_realm: |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | /* |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 111 | * Returns a default uri_auth with ST_SHNODE flag enabled and |
| 112 | * <node> set as the name if it is not empty. |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 113 | * Uses the pointer provided if not NULL and not initialized. |
| 114 | */ |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 115 | struct uri_auth *stats_set_node(struct uri_auth **root, char *name) |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 116 | { |
| 117 | struct uri_auth *u; |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 118 | char *node_copy = NULL; |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 119 | |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 120 | if (name && *name) { |
| 121 | node_copy = strdup(name); |
| 122 | if (node_copy == NULL) |
| 123 | goto out_realm; |
| 124 | } |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 125 | |
| 126 | if ((u = stats_check_init_uri_auth(root)) == NULL) |
| 127 | goto out_u; |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 128 | |
| 129 | if (!stats_set_flag(root, ST_SHNODE)) |
| 130 | goto out_u; |
| 131 | |
| 132 | if (node_copy) { |
| 133 | free(u->node); |
| 134 | u->node = node_copy; |
| 135 | } |
| 136 | |
| 137 | return u; |
| 138 | |
| 139 | out_u: |
| 140 | free(node_copy); |
| 141 | out_realm: |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Returns a default uri_auth with ST_SHDESC flag enabled and |
| 147 | * <description> set as the desc if it is not empty. |
| 148 | * Uses the pointer provided if not NULL and not initialized. |
| 149 | */ |
| 150 | struct uri_auth *stats_set_desc(struct uri_auth **root, char *desc) |
| 151 | { |
| 152 | struct uri_auth *u; |
| 153 | char *desc_copy = NULL; |
| 154 | |
| 155 | if (desc && *desc) { |
| 156 | desc_copy = strdup(desc); |
| 157 | if (desc_copy == NULL) |
| 158 | goto out_realm; |
| 159 | } |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 160 | |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 161 | if ((u = stats_check_init_uri_auth(root)) == NULL) |
| 162 | goto out_u; |
| 163 | |
| 164 | if (!stats_set_flag(root, ST_SHDESC)) |
| 165 | goto out_u; |
| 166 | |
| 167 | if (desc_copy) { |
| 168 | free(u->desc); |
| 169 | u->desc = desc_copy; |
| 170 | } |
| 171 | |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 172 | return u; |
| 173 | |
| 174 | out_u: |
Krzysztof Piotr Oledzki | 48cb2ae | 2009-10-02 22:51:14 +0200 | [diff] [blame] | 175 | free(desc_copy); |
Willy Tarreau | 1d45b7c | 2009-08-16 10:29:18 +0200 | [diff] [blame] | 176 | out_realm: |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | /* |
Willy Tarreau | bbd4212 | 2007-07-25 07:26:38 +0200 | [diff] [blame] | 181 | * Returns a default uri_auth with the <refresh> refresh interval. |
| 182 | * Uses the pointer provided if not NULL and not initialized. |
| 183 | */ |
| 184 | struct uri_auth *stats_set_refresh(struct uri_auth **root, int interval) |
| 185 | { |
| 186 | struct uri_auth *u; |
| 187 | |
| 188 | if ((u = stats_check_init_uri_auth(root)) != NULL) |
| 189 | u->refresh = interval; |
| 190 | return u; |
| 191 | } |
| 192 | |
| 193 | /* |
Krzysztof Oledzki | d9db927 | 2007-10-15 10:05:11 +0200 | [diff] [blame] | 194 | * Returns a default uri_auth with the <flag> set. |
| 195 | * Uses the pointer provided if not NULL and not initialized. |
| 196 | */ |
| 197 | struct uri_auth *stats_set_flag(struct uri_auth **root, int flag) |
| 198 | { |
| 199 | struct uri_auth *u; |
| 200 | |
| 201 | if ((u = stats_check_init_uri_auth(root)) != NULL) |
| 202 | u->flags |= flag; |
| 203 | return u; |
| 204 | } |
| 205 | |
| 206 | /* |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 207 | * Returns a default uri_auth with a <user:passwd> entry added to the list of |
| 208 | * authorized users. If a matching entry is found, no update will be performed. |
| 209 | * Uses the pointer provided if not NULL and not initialized. |
| 210 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 211 | struct uri_auth *stats_add_auth(struct uri_auth **root, char *user) |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 212 | { |
| 213 | struct uri_auth *u; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 214 | struct auth_users *newuser; |
| 215 | char *pass; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 216 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 217 | pass = strchr(user, ':'); |
| 218 | if (pass) |
| 219 | *pass++ = '\0'; |
| 220 | else |
| 221 | pass = ""; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 222 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 223 | if ((u = stats_check_init_uri_auth(root)) == NULL) |
| 224 | return NULL; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 225 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 226 | if (!u->userlist) |
| 227 | u->userlist = (struct userlist *)calloc(1, sizeof(struct userlist)); |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 228 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 229 | if (!u->userlist) |
| 230 | return NULL; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 231 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 232 | if (!u->userlist->name) |
| 233 | u->userlist->name = strdup(".internal-stats-userlist"); |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 234 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 235 | if (!u->userlist->name) |
| 236 | return NULL; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 237 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 238 | for (newuser = u->userlist->users; newuser; newuser = newuser->next) |
| 239 | if (!strcmp(newuser->user, user)) { |
| 240 | Warning("uri auth: ignoring duplicated user '%s'.\n", |
| 241 | user); |
| 242 | return u; |
| 243 | } |
| 244 | |
| 245 | newuser = (struct auth_users *)calloc(1, sizeof(struct auth_users)); |
| 246 | if (!newuser) |
| 247 | return NULL; |
| 248 | |
| 249 | newuser->user = strdup(user); |
Willy Tarreau | 0b291bd | 2013-01-24 02:26:43 +0100 | [diff] [blame] | 250 | if (!newuser->user) { |
| 251 | free(newuser); |
| 252 | return NULL; |
| 253 | } |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 254 | |
Willy Tarreau | 0b291bd | 2013-01-24 02:26:43 +0100 | [diff] [blame] | 255 | newuser->pass = strdup(pass); |
| 256 | if (!newuser->pass) { |
| 257 | free(newuser->user); |
| 258 | free(newuser); |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 259 | return NULL; |
Willy Tarreau | 0b291bd | 2013-01-24 02:26:43 +0100 | [diff] [blame] | 260 | } |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 261 | |
Willy Tarreau | 0b291bd | 2013-01-24 02:26:43 +0100 | [diff] [blame] | 262 | newuser->flags |= AU_O_INSECURE; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 263 | newuser->next = u->userlist->users; |
| 264 | u->userlist->users = newuser; |
| 265 | |
| 266 | return u; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 267 | } |
| 268 | |
willy tarreau | 1f431b5 | 2006-05-21 14:46:15 +0200 | [diff] [blame] | 269 | /* |
| 270 | * Returns a default uri_auth with a <scope> entry added to the list of |
| 271 | * allowed scopes. If a matching entry is found, no update will be performed. |
| 272 | * Uses the pointer provided if not NULL and not initialized. |
| 273 | */ |
| 274 | struct uri_auth *stats_add_scope(struct uri_auth **root, char *scope) |
| 275 | { |
| 276 | struct uri_auth *u; |
| 277 | char *new_name; |
| 278 | struct stat_scope *old_scope, **scope_list; |
| 279 | |
| 280 | if ((u = stats_check_init_uri_auth(root)) == NULL) |
| 281 | goto out; |
| 282 | |
| 283 | scope_list = &u->scope; |
| 284 | while ((old_scope = *scope_list)) { |
| 285 | if (!strcmp(old_scope->px_id, scope)) |
| 286 | break; |
| 287 | scope_list = &old_scope->next; |
| 288 | } |
| 289 | |
| 290 | if (!old_scope) { |
| 291 | if ((new_name = strdup(scope)) == NULL) |
| 292 | goto out_u; |
| 293 | |
| 294 | if ((old_scope = (struct stat_scope *)calloc(1, sizeof(*old_scope))) == NULL) |
| 295 | goto out_name; |
| 296 | |
| 297 | old_scope->px_id = new_name; |
| 298 | old_scope->px_len = strlen(new_name); |
| 299 | *scope_list = old_scope; |
| 300 | } |
| 301 | return u; |
| 302 | |
| 303 | out_name: |
| 304 | free(new_name); |
| 305 | out_u: |
| 306 | free(u); |
| 307 | out: |
| 308 | return NULL; |
| 309 | } |
| 310 | |
Willy Tarreau | bbd4212 | 2007-07-25 07:26:38 +0200 | [diff] [blame] | 311 | /* |
| 312 | * Local variables: |
| 313 | * c-indent-level: 8 |
| 314 | * c-basic-offset: 8 |
| 315 | * End: |
| 316 | */ |