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