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