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