blob: ad606afb8c128a85e4d173c08d2d6e28a3912630 [file] [log] [blame]
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001/*
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 Tarreau890a33e2010-03-04 19:10:14 +010013#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 Oledzki96105042010-01-29 17:50:44 +010022
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <common/config.h>
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010029#include <common/errors.h>
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +010030
31#include <proto/acl.h>
32#include <proto/log.h>
33
34#include <types/auth.h>
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010035#include <types/pattern.h>
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +010036
37struct 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
43struct userlist *
44auth_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 FOURNIER9eec0a62014-01-22 18:38:02 +010058int check_group(struct userlist *ul, char *name)
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +010059{
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010060 struct auth_groups *ag;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +010061
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010062 for (ag = ul->groups; ag; ag = ag->next)
63 if (strcmp(name, ag->name) == 0)
64 return 1;
65 return 0;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +010066}
67
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +010068void
69userlist_free(struct userlist *ul)
70{
71 struct userlist *tul;
72 struct auth_users *au, *tau;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010073 struct auth_groups_list *agl, *tagl;
74 struct auth_groups *ag, *tag;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +010075
76 while (ul) {
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010077 /* Free users. */
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +010078 au = ul->users;
79 while (au) {
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010080 /* 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 Oledzki96105042010-01-29 17:50:44 +010088 tau = au;
89 au = au->next;
90 free(tau->user);
91 free(tau->pass);
92 free(tau);
93 }
94
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010095 /* 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 Oledzki96105042010-01-29 17:50:44 +0100104 tul = ul;
105 ul = ul->next;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100106 free(tul->name);
107 free(tul);
108 };
109}
110
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +0100111int 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 Oledzki96105042010-01-29 17:50:44 +0100211/*
212 * Authenticate and authorize user; return 1 if OK, 0 if case of error.
213 */
214int
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +0100215check_user(struct userlist *ul, const char *user, const char *pass)
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100216{
217
218 struct auth_users *u;
219 const char *ep;
220
221#ifdef DEBUG_AUTH
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +0100222 fprintf(stderr, "req: userlist=%s, user=%s, pass=%s, group=%s\n",
223 ul->name, user, pass, group);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100224#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 FOURNIER9eec0a62014-01-22 18:38:02 +0100234 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 Oledzki96105042010-01-29 17:50:44 +0100238#endif
239
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +0100240 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 Oledzkif9423ae2010-01-29 19:26:18 +0100258
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100259struct pattern *
260pat_match_auth(struct sample *smp, struct pattern_expr *expr, int fill)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100261{
Willy Tarreau37406352012-04-23 16:16:37 +0200262 struct userlist *ul = smp->ctx.a[0];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100263 struct pattern_list *lst;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +0100264 struct auth_users *u;
265 struct auth_groups_list *agl;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100266 struct pattern *pattern;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100267
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +0100268 /* Check if the userlist is present in the context data. */
269 if (!ul)
270 return PAT_NOMATCH;
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 FOURNIER5338eea2013-12-16 14:22:13 +0100278 return NULL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +0100279
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100280 /* 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 FOURNIER9eec0a62014-01-22 18:38:02 +0100289 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100290 return NULL;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100291}