2010-01-29 16:50:44 +00:00
|
|
|
/*
|
|
|
|
* User authentication & authorization
|
|
|
|
*
|
|
|
|
* Copyright 2010 Krzysztof Piotr Oledzki <ole@ans.pl>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
[BUILD] fix platform-dependant build issues related to crypt()
Holger Just and Ross West reported build issues on FreeBSD and
Solaris that were initially caused by the definition of
_XOPEN_SOURCE at the top of auth.c, which was required on Linux
to avoid a build warning.
Krzysztof Oledzki found that using _GNU_SOURCE instead also worked
on Linux and did not cause any issue on several versions of FreeBSD.
Solaris still reported a warning this time, which was fixed by
including <crypt.h>, which itself is not present on FreeBSD nor on
all Linux toolchains.
So by adding a new build option (NEED_CRYPT_H), we can get Solaris
to get crypt() working and stop complaining at the same time, without
impacting other platforms.
This fix was tested at least on several linux toolchains (at least
uclibc, glibc 2.2.5, 2.3.6 and 2.7), on FreeBSD 4 to 8, Solaris 8
(which needs crypt.h), and AIX 5.3 (without crypt.h).
Every time it builds without a warning.
2010-03-04 18:10:14 +00:00
|
|
|
#ifdef CONFIG_HAP_CRYPT
|
|
|
|
/* This is to have crypt() defined on Linux */
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#ifdef NEED_CRYPT_H
|
|
|
|
/* some platforms such as Solaris need this */
|
|
|
|
#include <crypt.h>
|
|
|
|
#endif
|
|
|
|
#endif /* CONFIG_HAP_CRYPT */
|
2010-01-29 16:50:44 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <common/config.h>
|
2014-01-22 17:38:02 +00:00
|
|
|
#include <common/errors.h>
|
2010-01-29 16:50:44 +00:00
|
|
|
|
|
|
|
#include <proto/acl.h>
|
|
|
|
#include <proto/log.h>
|
|
|
|
|
|
|
|
#include <types/auth.h>
|
2013-11-28 17:22:00 +00:00
|
|
|
#include <types/pattern.h>
|
2010-01-29 16:50:44 +00:00
|
|
|
|
|
|
|
struct userlist *userlist = NULL; /* list of all existing userlists */
|
|
|
|
|
|
|
|
/* find targets for selected gropus. The function returns pointer to
|
|
|
|
* the userlist struct ot NULL if name is NULL/empty or unresolvable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct userlist *
|
|
|
|
auth_find_userlist(char *name)
|
|
|
|
{
|
|
|
|
struct userlist *l;
|
|
|
|
|
|
|
|
if (!name || !*name)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (l = userlist; l; l = l->next)
|
|
|
|
if (!strcmp(l->name, name))
|
|
|
|
return l;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:38:02 +00:00
|
|
|
int check_group(struct userlist *ul, char *name)
|
2010-01-29 16:50:44 +00:00
|
|
|
{
|
2014-01-22 17:38:02 +00:00
|
|
|
struct auth_groups *ag;
|
2010-01-29 16:50:44 +00:00
|
|
|
|
2014-01-22 17:38:02 +00:00
|
|
|
for (ag = ul->groups; ag; ag = ag->next)
|
|
|
|
if (strcmp(name, ag->name) == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2010-01-29 16:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
userlist_free(struct userlist *ul)
|
|
|
|
{
|
|
|
|
struct userlist *tul;
|
|
|
|
struct auth_users *au, *tau;
|
2014-01-22 17:38:02 +00:00
|
|
|
struct auth_groups_list *agl, *tagl;
|
|
|
|
struct auth_groups *ag, *tag;
|
2010-01-29 16:50:44 +00:00
|
|
|
|
|
|
|
while (ul) {
|
2014-01-22 17:38:02 +00:00
|
|
|
/* Free users. */
|
2010-01-29 16:50:44 +00:00
|
|
|
au = ul->users;
|
|
|
|
while (au) {
|
2014-01-22 17:38:02 +00:00
|
|
|
/* Free groups that own current user. */
|
|
|
|
agl = au->u.groups;
|
|
|
|
while (agl) {
|
|
|
|
tagl = agl;
|
|
|
|
agl = agl->next;
|
|
|
|
free(tagl);
|
|
|
|
}
|
|
|
|
|
2010-01-29 16:50:44 +00:00
|
|
|
tau = au;
|
|
|
|
au = au->next;
|
|
|
|
free(tau->user);
|
|
|
|
free(tau->pass);
|
|
|
|
free(tau);
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:38:02 +00:00
|
|
|
/* Free grouplist. */
|
|
|
|
ag = ul->groups;
|
|
|
|
while (ag) {
|
|
|
|
tag = ag;
|
|
|
|
ag = ag->next;
|
|
|
|
free(tag->name);
|
|
|
|
free(tag);
|
|
|
|
}
|
|
|
|
|
2010-01-29 16:50:44 +00:00
|
|
|
tul = ul;
|
|
|
|
ul = ul->next;
|
|
|
|
free(tul->name);
|
|
|
|
free(tul);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:38:02 +00:00
|
|
|
int userlist_postinit()
|
|
|
|
{
|
|
|
|
struct userlist *curuserlist = NULL;
|
|
|
|
|
|
|
|
/* Resolve usernames and groupnames. */
|
|
|
|
for (curuserlist = userlist; curuserlist; curuserlist = curuserlist->next) {
|
|
|
|
struct auth_groups *ag;
|
|
|
|
struct auth_users *curuser;
|
|
|
|
struct auth_groups_list *grl;
|
|
|
|
|
|
|
|
for (curuser = curuserlist->users; curuser; curuser = curuser->next) {
|
|
|
|
char *group = NULL;
|
|
|
|
struct auth_groups_list *groups = NULL;
|
|
|
|
|
|
|
|
if (!curuser->u.groups_names)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while ((group = strtok(group?NULL:curuser->u.groups_names, ","))) {
|
|
|
|
for (ag = curuserlist->groups; ag; ag = ag->next) {
|
|
|
|
if (!strcmp(ag->name, group))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ag) {
|
|
|
|
Alert("userlist '%s': no such group '%s' specified in user '%s'\n",
|
|
|
|
curuserlist->name, group, curuser->user);
|
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add this group at the group userlist. */
|
|
|
|
grl = calloc(1, sizeof(*grl));
|
|
|
|
if (!grl) {
|
|
|
|
Alert("userlist '%s': no more memory when trying to allocate the user groups.\n",
|
|
|
|
curuserlist->name);
|
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
grl->group = ag;
|
|
|
|
grl->next = groups;
|
|
|
|
groups = grl;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(curuser->u.groups);
|
|
|
|
curuser->u.groups = groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ag = curuserlist->groups; ag; ag = ag->next) {
|
|
|
|
char *user = NULL;
|
|
|
|
|
|
|
|
if (!ag->groupusers)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while ((user = strtok(user?NULL:ag->groupusers, ","))) {
|
|
|
|
for (curuser = curuserlist->users; curuser; curuser = curuser->next) {
|
|
|
|
if (!strcmp(curuser->user, user))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!curuser) {
|
|
|
|
Alert("userlist '%s': no such user '%s' specified in group '%s'\n",
|
|
|
|
curuserlist->name, user, ag->name);
|
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add this group at the group userlist. */
|
|
|
|
grl = calloc(1, sizeof(*grl));
|
|
|
|
if (!grl) {
|
|
|
|
Alert("userlist '%s': no more memory when trying to allocate the user groups.\n",
|
|
|
|
curuserlist->name);
|
|
|
|
return ERR_ALERT | ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
grl->group = ag;
|
|
|
|
grl->next = curuser->u.groups;
|
|
|
|
curuser->u.groups = grl;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ag->groupusers);
|
|
|
|
ag->groupusers = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_AUTH
|
|
|
|
for (ag = curuserlist->groups; ag; ag = ag->next) {
|
|
|
|
struct auth_groups_list *agl;
|
|
|
|
|
|
|
|
fprintf(stderr, "group %s, id %p, users:", ag->name, ag);
|
|
|
|
for (curuser = curuserlist->users; curuser; curuser = curuser->next) {
|
|
|
|
for (agl = curuser->u.groups; agl; agl = agl->next) {
|
|
|
|
if (agl->group == ag)
|
|
|
|
fprintf(stderr, " %s", curuser->user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-01-29 16:50:44 +00:00
|
|
|
/*
|
|
|
|
* Authenticate and authorize user; return 1 if OK, 0 if case of error.
|
|
|
|
*/
|
|
|
|
int
|
2014-01-22 17:38:02 +00:00
|
|
|
check_user(struct userlist *ul, const char *user, const char *pass)
|
2010-01-29 16:50:44 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
struct auth_users *u;
|
|
|
|
const char *ep;
|
|
|
|
|
|
|
|
#ifdef DEBUG_AUTH
|
2014-01-22 17:38:02 +00:00
|
|
|
fprintf(stderr, "req: userlist=%s, user=%s, pass=%s, group=%s\n",
|
|
|
|
ul->name, user, pass, group);
|
2010-01-29 16:50:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (u = ul->users; u; u = u->next)
|
|
|
|
if (!strcmp(user, u->user))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!u)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifdef DEBUG_AUTH
|
2014-01-22 17:38:02 +00:00
|
|
|
fprintf(stderr, "cfg: user=%s, pass=%s, flags=%X, groups=",
|
|
|
|
u->user, u->pass, u->flags);
|
|
|
|
for (agl = u->u.groups; agl; agl = agl->next)
|
|
|
|
fprintf(stderr, " %s", agl->group->name);
|
2010-01-29 16:50:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!(u->flags & AU_O_INSECURE)) {
|
|
|
|
#ifdef CONFIG_HAP_CRYPT
|
|
|
|
ep = crypt(pass, u->pass);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
ep = pass;
|
|
|
|
|
|
|
|
#ifdef DEBUG_AUTH
|
|
|
|
fprintf(stderr, ", crypt=%s\n", ep);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!strcmp(ep, u->pass))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-29 18:26:18 +00:00
|
|
|
|
2013-11-28 21:21:02 +00:00
|
|
|
enum pat_match_res
|
2013-11-28 17:22:00 +00:00
|
|
|
pat_match_auth(struct sample *smp, struct pattern *pattern)
|
2010-01-29 18:26:18 +00:00
|
|
|
{
|
2012-04-23 14:16:37 +00:00
|
|
|
struct userlist *ul = smp->ctx.a[0];
|
2014-01-22 17:38:02 +00:00
|
|
|
struct auth_users *u;
|
|
|
|
struct auth_groups_list *agl;
|
2010-01-29 18:26:18 +00:00
|
|
|
|
2014-01-22 17:38:02 +00:00
|
|
|
/* Check if the userlist is present in the context data. */
|
|
|
|
if (!ul)
|
|
|
|
return PAT_NOMATCH;
|
|
|
|
|
|
|
|
/* Browse the userlist for searching user. */
|
|
|
|
for (u = ul->users; u; u = u->next) {
|
|
|
|
if (strcmp(smp->data.str.str, u->user) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!u)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Browse each group for searching group name that match the pattern. */
|
|
|
|
for (agl = u->u.groups; agl; agl = agl->next) {
|
|
|
|
if (strcmp(agl->group->name, pattern->ptr.str) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!agl)
|
2013-11-28 17:22:00 +00:00
|
|
|
return PAT_NOMATCH;
|
2014-01-22 17:38:02 +00:00
|
|
|
return PAT_MATCH;
|
2010-01-29 18:26:18 +00:00
|
|
|
}
|