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 | 890a33e | 2010-03-04 19:10:14 +0100 | [diff] [blame] | 13 | #ifdef CONFIG_HAP_CRYPT |
| 14 | /* This is to have crypt() defined on Linux */ |
| 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #ifdef NEED_CRYPT_H |
| 18 | /* some platforms such as Solaris need this */ |
| 19 | #include <crypt.h> |
| 20 | #endif |
| 21 | #endif /* CONFIG_HAP_CRYPT */ |
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 | |
| 28 | #include <common/config.h> |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 29 | #include <common/errors.h> |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 30 | |
| 31 | #include <proto/acl.h> |
| 32 | #include <proto/log.h> |
| 33 | |
| 34 | #include <types/auth.h> |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 35 | #include <types/pattern.h> |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 36 | |
| 37 | struct userlist *userlist = NULL; /* list of all existing userlists */ |
| 38 | |
| 39 | /* find targets for selected gropus. The function returns pointer to |
| 40 | * the userlist struct ot NULL if name is NULL/empty or unresolvable. |
| 41 | */ |
| 42 | |
| 43 | struct userlist * |
| 44 | auth_find_userlist(char *name) |
| 45 | { |
| 46 | struct userlist *l; |
| 47 | |
| 48 | if (!name || !*name) |
| 49 | return NULL; |
| 50 | |
| 51 | for (l = userlist; l; l = l->next) |
| 52 | if (!strcmp(l->name, name)) |
| 53 | return l; |
| 54 | |
| 55 | return NULL; |
| 56 | } |
| 57 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 58 | int check_group(struct userlist *ul, char *name) |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 59 | { |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 60 | struct auth_groups *ag; |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 61 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 62 | for (ag = ul->groups; ag; ag = ag->next) |
| 63 | if (strcmp(name, ag->name) == 0) |
| 64 | return 1; |
| 65 | return 0; |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 68 | void |
| 69 | userlist_free(struct userlist *ul) |
| 70 | { |
| 71 | struct userlist *tul; |
| 72 | struct auth_users *au, *tau; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 73 | struct auth_groups_list *agl, *tagl; |
| 74 | struct auth_groups *ag, *tag; |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 75 | |
| 76 | while (ul) { |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 77 | /* Free users. */ |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 78 | au = ul->users; |
| 79 | while (au) { |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 80 | /* Free groups that own current user. */ |
| 81 | agl = au->u.groups; |
| 82 | while (agl) { |
| 83 | tagl = agl; |
| 84 | agl = agl->next; |
| 85 | free(tagl); |
| 86 | } |
| 87 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 88 | tau = au; |
| 89 | au = au->next; |
| 90 | free(tau->user); |
| 91 | free(tau->pass); |
| 92 | free(tau); |
| 93 | } |
| 94 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 95 | /* Free grouplist. */ |
| 96 | ag = ul->groups; |
| 97 | while (ag) { |
| 98 | tag = ag; |
| 99 | ag = ag->next; |
| 100 | free(tag->name); |
| 101 | free(tag); |
| 102 | } |
| 103 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 104 | tul = ul; |
| 105 | ul = ul->next; |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 106 | free(tul->name); |
| 107 | free(tul); |
| 108 | }; |
| 109 | } |
| 110 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 111 | int userlist_postinit() |
| 112 | { |
| 113 | struct userlist *curuserlist = NULL; |
| 114 | |
| 115 | /* Resolve usernames and groupnames. */ |
| 116 | for (curuserlist = userlist; curuserlist; curuserlist = curuserlist->next) { |
| 117 | struct auth_groups *ag; |
| 118 | struct auth_users *curuser; |
| 119 | struct auth_groups_list *grl; |
| 120 | |
| 121 | for (curuser = curuserlist->users; curuser; curuser = curuser->next) { |
| 122 | char *group = NULL; |
| 123 | struct auth_groups_list *groups = NULL; |
| 124 | |
| 125 | if (!curuser->u.groups_names) |
| 126 | continue; |
| 127 | |
| 128 | while ((group = strtok(group?NULL:curuser->u.groups_names, ","))) { |
| 129 | for (ag = curuserlist->groups; ag; ag = ag->next) { |
| 130 | if (!strcmp(ag->name, group)) |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | if (!ag) { |
| 135 | Alert("userlist '%s': no such group '%s' specified in user '%s'\n", |
| 136 | curuserlist->name, group, curuser->user); |
| 137 | return ERR_ALERT | ERR_FATAL; |
| 138 | } |
| 139 | |
| 140 | /* Add this group at the group userlist. */ |
| 141 | grl = calloc(1, sizeof(*grl)); |
| 142 | if (!grl) { |
| 143 | Alert("userlist '%s': no more memory when trying to allocate the user groups.\n", |
| 144 | curuserlist->name); |
| 145 | return ERR_ALERT | ERR_FATAL; |
| 146 | } |
| 147 | |
| 148 | grl->group = ag; |
| 149 | grl->next = groups; |
| 150 | groups = grl; |
| 151 | } |
| 152 | |
| 153 | free(curuser->u.groups); |
| 154 | curuser->u.groups = groups; |
| 155 | } |
| 156 | |
| 157 | for (ag = curuserlist->groups; ag; ag = ag->next) { |
| 158 | char *user = NULL; |
| 159 | |
| 160 | if (!ag->groupusers) |
| 161 | continue; |
| 162 | |
| 163 | while ((user = strtok(user?NULL:ag->groupusers, ","))) { |
| 164 | for (curuser = curuserlist->users; curuser; curuser = curuser->next) { |
| 165 | if (!strcmp(curuser->user, user)) |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | if (!curuser) { |
| 170 | Alert("userlist '%s': no such user '%s' specified in group '%s'\n", |
| 171 | curuserlist->name, user, ag->name); |
| 172 | return ERR_ALERT | ERR_FATAL; |
| 173 | } |
| 174 | |
| 175 | /* Add this group at the group userlist. */ |
| 176 | grl = calloc(1, sizeof(*grl)); |
| 177 | if (!grl) { |
| 178 | Alert("userlist '%s': no more memory when trying to allocate the user groups.\n", |
| 179 | curuserlist->name); |
| 180 | return ERR_ALERT | ERR_FATAL; |
| 181 | } |
| 182 | |
| 183 | grl->group = ag; |
| 184 | grl->next = curuser->u.groups; |
| 185 | curuser->u.groups = grl; |
| 186 | } |
| 187 | |
| 188 | free(ag->groupusers); |
| 189 | ag->groupusers = NULL; |
| 190 | } |
| 191 | |
| 192 | #ifdef DEBUG_AUTH |
| 193 | for (ag = curuserlist->groups; ag; ag = ag->next) { |
| 194 | struct auth_groups_list *agl; |
| 195 | |
| 196 | fprintf(stderr, "group %s, id %p, users:", ag->name, ag); |
| 197 | for (curuser = curuserlist->users; curuser; curuser = curuser->next) { |
| 198 | for (agl = curuser->u.groups; agl; agl = agl->next) { |
| 199 | if (agl->group == ag) |
| 200 | fprintf(stderr, " %s", curuser->user); |
| 201 | } |
| 202 | } |
| 203 | fprintf(stderr, "\n"); |
| 204 | } |
| 205 | #endif |
| 206 | } |
| 207 | |
| 208 | return ERR_NONE; |
| 209 | } |
| 210 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 211 | /* |
| 212 | * Authenticate and authorize user; return 1 if OK, 0 if case of error. |
| 213 | */ |
| 214 | int |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 215 | check_user(struct userlist *ul, const char *user, const char *pass) |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 216 | { |
| 217 | |
| 218 | struct auth_users *u; |
| 219 | const char *ep; |
| 220 | |
| 221 | #ifdef DEBUG_AUTH |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 222 | fprintf(stderr, "req: userlist=%s, user=%s, pass=%s, group=%s\n", |
| 223 | ul->name, user, pass, group); |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 224 | #endif |
| 225 | |
| 226 | for (u = ul->users; u; u = u->next) |
| 227 | if (!strcmp(user, u->user)) |
| 228 | break; |
| 229 | |
| 230 | if (!u) |
| 231 | return 0; |
| 232 | |
| 233 | #ifdef DEBUG_AUTH |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 234 | fprintf(stderr, "cfg: user=%s, pass=%s, flags=%X, groups=", |
| 235 | u->user, u->pass, u->flags); |
| 236 | for (agl = u->u.groups; agl; agl = agl->next) |
| 237 | fprintf(stderr, " %s", agl->group->name); |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 238 | #endif |
| 239 | |
Krzysztof Piotr Oledzki | 9610504 | 2010-01-29 17:50:44 +0100 | [diff] [blame] | 240 | if (!(u->flags & AU_O_INSECURE)) { |
| 241 | #ifdef CONFIG_HAP_CRYPT |
| 242 | ep = crypt(pass, u->pass); |
| 243 | #else |
| 244 | return 0; |
| 245 | #endif |
| 246 | } else |
| 247 | ep = pass; |
| 248 | |
| 249 | #ifdef DEBUG_AUTH |
| 250 | fprintf(stderr, ", crypt=%s\n", ep); |
| 251 | #endif |
| 252 | |
| 253 | if (!strcmp(ep, u->pass)) |
| 254 | return 1; |
| 255 | else |
| 256 | return 0; |
| 257 | } |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 258 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 259 | struct pattern * |
| 260 | 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] | 261 | { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 262 | struct userlist *ul = smp->ctx.a[0]; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 263 | struct pattern_list *lst; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 264 | struct auth_users *u; |
| 265 | struct auth_groups_list *agl; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 266 | struct pattern *pattern; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 267 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 268 | /* Check if the userlist is present in the context data. */ |
| 269 | if (!ul) |
Willy Tarreau | 86e0fc1 | 2014-04-29 19:52:16 +0200 | [diff] [blame] | 270 | return NULL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 271 | |
| 272 | /* Browse the userlist for searching user. */ |
| 273 | for (u = ul->users; u; u = u->next) { |
| 274 | if (strcmp(smp->data.str.str, u->user) == 0) |
| 275 | break; |
| 276 | } |
| 277 | if (!u) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 278 | return NULL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 279 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 280 | /* Browse each pattern. */ |
| 281 | list_for_each_entry(lst, &expr->patterns, list) { |
| 282 | pattern = &lst->pat; |
| 283 | |
| 284 | /* Browse each group for searching group name that match the pattern. */ |
| 285 | for (agl = u->u.groups; agl; agl = agl->next) { |
| 286 | if (strcmp(agl->group->name, pattern->ptr.str) == 0) |
| 287 | return pattern; |
| 288 | } |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 289 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 290 | return NULL; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 291 | } |