Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * User authentication & authorization |
| 3 | * |
| 4 | * Copyright 2010 Krzysztof Piotr Oledzki <ole@ans.pl> |
| 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 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 13 | #ifdef USE_LIBCRYPT |
Willy Tarreau | 890a33e | 2010-03-04 19:10:14 +0100 | [diff] [blame] | 14 | /* This is to have crypt() defined on Linux */ |
| 15 | #define _GNU_SOURCE |
| 16 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 17 | #ifdef USE_CRYPT_H |
Willy Tarreau | 890a33e | 2010-03-04 19:10:14 +0100 | [diff] [blame] | 18 | /* some platforms such as Solaris need this */ |
| 19 | #include <crypt.h> |
| 20 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 21 | #endif /* USE_LIBCRYPT */ |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | |
Willy Tarreau | ac13aea | 2020-06-04 10:36:03 +0200 | [diff] [blame] | 28 | #include <haproxy/auth-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 29 | #include <haproxy/api.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 30 | #include <haproxy/global.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 31 | #include <haproxy/errors.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 32 | #include <haproxy/log.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 33 | #include <haproxy/pattern-t.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 34 | #include <haproxy/thread.h> |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 35 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 36 | struct userlist *userlist = NULL; /* list of all existing userlists */ |
| 37 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 38 | #ifdef USE_LIBCRYPT |
Willy Tarreau | 943e7ec | 2018-10-29 19:16:27 +0100 | [diff] [blame] | 39 | #ifdef HA_HAVE_CRYPT_R |
| 40 | /* context for crypt_r() */ |
| 41 | static THREAD_LOCAL struct crypt_data crypt_data = { .initialized = 0 }; |
| 42 | #else |
| 43 | /* lock for crypt() */ |
Willy Tarreau | af613e8 | 2020-06-05 08:40:51 +0200 | [diff] [blame] | 44 | __decl_thread(static HA_SPINLOCK_T auth_lock); |
Willy Tarreau | 34d4b52 | 2018-10-29 18:02:54 +0100 | [diff] [blame] | 45 | #endif |
Willy Tarreau | 943e7ec | 2018-10-29 19:16:27 +0100 | [diff] [blame] | 46 | #endif |
Willy Tarreau | 34d4b52 | 2018-10-29 18:02:54 +0100 | [diff] [blame] | 47 | |
David Carlier | 6c00eba | 2019-09-01 14:59:10 +0100 | [diff] [blame] | 48 | /* find targets for selected groups. The function returns pointer to |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 49 | * the userlist struct ot NULL if name is NULL/empty or unresolvable. |
| 50 | */ |
| 51 | |
| 52 | struct userlist * |
| 53 | auth_find_userlist(char *name) |
| 54 | { |
| 55 | struct userlist *l; |
| 56 | |
| 57 | if (!name || !*name) |
| 58 | return NULL; |
| 59 | |
| 60 | for (l = userlist; l; l = l->next) |
| 61 | if (!strcmp(l->name, name)) |
| 62 | return l; |
| 63 | |
| 64 | return NULL; |
| 65 | } |
| 66 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 67 | int check_group(struct userlist *ul, char *name) |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 68 | { |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 69 | struct auth_groups *ag; |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 70 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 71 | for (ag = ul->groups; ag; ag = ag->next) |
| 72 | if (strcmp(name, ag->name) == 0) |
| 73 | return 1; |
| 74 | return 0; |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 77 | void |
| 78 | userlist_free(struct userlist *ul) |
| 79 | { |
| 80 | struct userlist *tul; |
| 81 | struct auth_users *au, *tau; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 82 | struct auth_groups_list *agl, *tagl; |
| 83 | struct auth_groups *ag, *tag; |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 84 | |
| 85 | while (ul) { |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 86 | /* Free users. */ |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 87 | au = ul->users; |
| 88 | while (au) { |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 89 | /* Free groups that own current user. */ |
| 90 | agl = au->u.groups; |
| 91 | while (agl) { |
| 92 | tagl = agl; |
| 93 | agl = agl->next; |
| 94 | free(tagl); |
| 95 | } |
| 96 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 97 | tau = au; |
| 98 | au = au->next; |
| 99 | free(tau->user); |
| 100 | free(tau->pass); |
| 101 | free(tau); |
| 102 | } |
| 103 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 104 | /* Free grouplist. */ |
| 105 | ag = ul->groups; |
| 106 | while (ag) { |
| 107 | tag = ag; |
| 108 | ag = ag->next; |
| 109 | free(tag->name); |
| 110 | free(tag); |
| 111 | } |
| 112 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 113 | tul = ul; |
| 114 | ul = ul->next; |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 115 | free(tul->name); |
| 116 | free(tul); |
| 117 | }; |
| 118 | } |
| 119 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 120 | int userlist_postinit() |
| 121 | { |
| 122 | struct userlist *curuserlist = NULL; |
| 123 | |
| 124 | /* Resolve usernames and groupnames. */ |
| 125 | for (curuserlist = userlist; curuserlist; curuserlist = curuserlist->next) { |
| 126 | struct auth_groups *ag; |
| 127 | struct auth_users *curuser; |
| 128 | struct auth_groups_list *grl; |
| 129 | |
| 130 | for (curuser = curuserlist->users; curuser; curuser = curuser->next) { |
| 131 | char *group = NULL; |
| 132 | struct auth_groups_list *groups = NULL; |
| 133 | |
| 134 | if (!curuser->u.groups_names) |
| 135 | continue; |
| 136 | |
| 137 | while ((group = strtok(group?NULL:curuser->u.groups_names, ","))) { |
| 138 | for (ag = curuserlist->groups; ag; ag = ag->next) { |
| 139 | if (!strcmp(ag->name, group)) |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | if (!ag) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 144 | ha_alert("userlist '%s': no such group '%s' specified in user '%s'\n", |
| 145 | curuserlist->name, group, curuser->user); |
Dirkjan Bussink | 07fcaaa | 2014-04-28 22:57:16 +0000 | [diff] [blame] | 146 | free(groups); |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 147 | return ERR_ALERT | ERR_FATAL; |
| 148 | } |
| 149 | |
| 150 | /* Add this group at the group userlist. */ |
| 151 | grl = calloc(1, sizeof(*grl)); |
| 152 | if (!grl) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 153 | ha_alert("userlist '%s': no more memory when trying to allocate the user groups.\n", |
| 154 | curuserlist->name); |
Dirkjan Bussink | 07fcaaa | 2014-04-28 22:57:16 +0000 | [diff] [blame] | 155 | free(groups); |
| 156 | return ERR_ALERT | ERR_FATAL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | grl->group = ag; |
| 160 | grl->next = groups; |
| 161 | groups = grl; |
| 162 | } |
| 163 | |
| 164 | free(curuser->u.groups); |
| 165 | curuser->u.groups = groups; |
| 166 | } |
| 167 | |
| 168 | for (ag = curuserlist->groups; ag; ag = ag->next) { |
| 169 | char *user = NULL; |
| 170 | |
| 171 | if (!ag->groupusers) |
| 172 | continue; |
| 173 | |
| 174 | while ((user = strtok(user?NULL:ag->groupusers, ","))) { |
| 175 | for (curuser = curuserlist->users; curuser; curuser = curuser->next) { |
| 176 | if (!strcmp(curuser->user, user)) |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | if (!curuser) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 181 | ha_alert("userlist '%s': no such user '%s' specified in group '%s'\n", |
| 182 | curuserlist->name, user, ag->name); |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 183 | return ERR_ALERT | ERR_FATAL; |
| 184 | } |
| 185 | |
| 186 | /* Add this group at the group userlist. */ |
| 187 | grl = calloc(1, sizeof(*grl)); |
| 188 | if (!grl) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 189 | ha_alert("userlist '%s': no more memory when trying to allocate the user groups.\n", |
| 190 | curuserlist->name); |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 191 | return ERR_ALERT | ERR_FATAL; |
| 192 | } |
| 193 | |
| 194 | grl->group = ag; |
| 195 | grl->next = curuser->u.groups; |
| 196 | curuser->u.groups = grl; |
| 197 | } |
| 198 | |
| 199 | free(ag->groupusers); |
| 200 | ag->groupusers = NULL; |
| 201 | } |
| 202 | |
| 203 | #ifdef DEBUG_AUTH |
| 204 | for (ag = curuserlist->groups; ag; ag = ag->next) { |
| 205 | struct auth_groups_list *agl; |
| 206 | |
| 207 | fprintf(stderr, "group %s, id %p, users:", ag->name, ag); |
| 208 | for (curuser = curuserlist->users; curuser; curuser = curuser->next) { |
| 209 | for (agl = curuser->u.groups; agl; agl = agl->next) { |
| 210 | if (agl->group == ag) |
| 211 | fprintf(stderr, " %s", curuser->user); |
| 212 | } |
| 213 | } |
| 214 | fprintf(stderr, "\n"); |
| 215 | } |
| 216 | #endif |
| 217 | } |
| 218 | |
| 219 | return ERR_NONE; |
| 220 | } |
| 221 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 222 | /* |
| 223 | * Authenticate and authorize user; return 1 if OK, 0 if case of error. |
| 224 | */ |
| 225 | int |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 226 | check_user(struct userlist *ul, const char *user, const char *pass) |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 227 | { |
| 228 | |
| 229 | struct auth_users *u; |
CJ Ess | 6eac32e | 2015-05-02 16:35:24 -0400 | [diff] [blame] | 230 | #ifdef DEBUG_AUTH |
| 231 | struct auth_groups_list *agl; |
| 232 | #endif |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 233 | const char *ep; |
| 234 | |
| 235 | #ifdef DEBUG_AUTH |
CJ Ess | 6eac32e | 2015-05-02 16:35:24 -0400 | [diff] [blame] | 236 | fprintf(stderr, "req: userlist=%s, user=%s, pass=%s\n", |
| 237 | ul->name, user, pass); |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 238 | #endif |
| 239 | |
| 240 | for (u = ul->users; u; u = u->next) |
| 241 | if (!strcmp(user, u->user)) |
| 242 | break; |
| 243 | |
| 244 | if (!u) |
| 245 | return 0; |
| 246 | |
| 247 | #ifdef DEBUG_AUTH |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 248 | fprintf(stderr, "cfg: user=%s, pass=%s, flags=%X, groups=", |
| 249 | u->user, u->pass, u->flags); |
| 250 | for (agl = u->u.groups; agl; agl = agl->next) |
| 251 | fprintf(stderr, " %s", agl->group->name); |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 252 | #endif |
| 253 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 254 | if (!(u->flags & AU_O_INSECURE)) { |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 255 | #ifdef USE_LIBCRYPT |
Willy Tarreau | 943e7ec | 2018-10-29 19:16:27 +0100 | [diff] [blame] | 256 | #ifdef HA_HAVE_CRYPT_R |
| 257 | ep = crypt_r(pass, u->pass, &crypt_data); |
| 258 | #else |
Willy Tarreau | 34d4b52 | 2018-10-29 18:02:54 +0100 | [diff] [blame] | 259 | HA_SPIN_LOCK(AUTH_LOCK, &auth_lock); |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 260 | ep = crypt(pass, u->pass); |
Willy Tarreau | 34d4b52 | 2018-10-29 18:02:54 +0100 | [diff] [blame] | 261 | HA_SPIN_UNLOCK(AUTH_LOCK, &auth_lock); |
Willy Tarreau | 943e7ec | 2018-10-29 19:16:27 +0100 | [diff] [blame] | 262 | #endif |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 263 | #else |
| 264 | return 0; |
| 265 | #endif |
| 266 | } else |
| 267 | ep = pass; |
| 268 | |
| 269 | #ifdef DEBUG_AUTH |
| 270 | fprintf(stderr, ", crypt=%s\n", ep); |
| 271 | #endif |
| 272 | |
Cyril Bonté | c82279c | 2014-08-29 20:20:01 +0200 | [diff] [blame] | 273 | if (ep && strcmp(ep, u->pass) == 0) |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 274 | return 1; |
| 275 | else |
| 276 | return 0; |
| 277 | } |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 278 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 279 | struct pattern * |
| 280 | pat_match_auth(struct sample *smp, struct pattern_expr *expr, int fill) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 281 | { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 282 | struct userlist *ul = smp->ctx.a[0]; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 283 | struct pattern_list *lst; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 284 | struct auth_users *u; |
| 285 | struct auth_groups_list *agl; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 286 | struct pattern *pattern; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 287 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 288 | /* Check if the userlist is present in the context data. */ |
| 289 | if (!ul) |
Willy Tarreau | 86e0fc1 | 2014-04-29 19:52:16 +0200 | [diff] [blame] | 290 | return NULL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 291 | |
| 292 | /* Browse the userlist for searching user. */ |
| 293 | for (u = ul->users; u; u = u->next) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 294 | if (strcmp(smp->data.u.str.area, u->user) == 0) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | if (!u) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 298 | return NULL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 299 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 300 | /* Browse each pattern. */ |
| 301 | list_for_each_entry(lst, &expr->patterns, list) { |
| 302 | pattern = &lst->pat; |
| 303 | |
| 304 | /* Browse each group for searching group name that match the pattern. */ |
| 305 | for (agl = u->u.groups; agl; agl = agl->next) { |
| 306 | if (strcmp(agl->group->name, pattern->ptr.str) == 0) |
| 307 | return pattern; |
| 308 | } |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 309 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 310 | return NULL; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 311 | } |
Willy Tarreau | e8692b4 | 2016-12-21 19:36:25 +0100 | [diff] [blame] | 312 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 313 | REGISTER_BUILD_OPTS("Encrypted password support via crypt(3): yes"); |