#include <sys/cdefs.h>
#ifdef __FreeBSD__
__FBSDID("$FreeBSD: src/lib/libpam/modules/pam_ftpusers/pam_ftpusers.c,v 1.1 2002/05/08 00:30:10 des Exp $");
#else
__RCSID("$NetBSD: pam_ftpusers.c,v 1.6 2012/01/03 19:02:55 christos Exp $");
#endif
#include <ctype.h>
#include <grp.h>
#include <paths.h>
#include <pwd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define PAM_SM_ACCOUNT
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/pam_mod_misc.h>
#include <security/openpam.h>
PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
int argc __unused, const char *argv[] __unused)
{
struct passwd *pwd, pwres;
struct group *grp, grres;
const char *user;
int pam_err, found, allow;
char *line, *name, **mem;
size_t len, ulen;
FILE *f;
char pwbuf[1024], grbuf[1024];
pam_err = pam_get_user(pamh, &user, NULL);
if (pam_err != PAM_SUCCESS)
return (pam_err);
if (user == NULL ||
getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
pwd == NULL)
return (PAM_SERVICE_ERR);
found = 0;
ulen = strlen(user);
if ((f = fopen(_PATH_FTPUSERS, "r")) == NULL) {
PAM_LOG("%s: %s", _PATH_FTPUSERS, strerror(errno));
goto done;
}
while (!found && (line = fgetln(f, &len)) != NULL) {
if (*line == '#')
continue;
while (len > 0 && isspace((unsigned char)line[len - 1]))
--len;
if (len == 0)
continue;
if (*line != '@') {
if (len == ulen && strncmp(user, line, len) == 0)
found = 1;
continue;
}
asprintf(&name, "%.*s", (int)len - 1, line + 1);
if (name == NULL) {
fclose(f);
return (PAM_BUF_ERR);
}
(void)getgrnam_r(name, &grres, grbuf, sizeof(grbuf), &grp);
free(name);
if (grp == NULL)
continue;
for (mem = grp->gr_mem; mem && *mem && !found; ++mem)
if (strcmp(user, *mem) == 0)
found = 1;
}
done:
allow = (openpam_get_option(pamh, "disallow") == NULL);
if (found)
pam_err = allow ? PAM_SUCCESS : PAM_AUTH_ERR;
else
pam_err = allow ? PAM_AUTH_ERR : PAM_SUCCESS;
if (f != NULL)
fclose(f);
return (pam_err);
}
PAM_MODULE_ENTRY("pam_ftpusers");