#if 0
#ifndef lint
static char sccsid[] = "%Z% %M% %I% %E% %U%";
#endif
#endif
#include <sys/cdefs.h>
#ifdef __FreeBSD__
__FBSDID("$FreeBSD: src/lib/libpam/modules/pam_login_access/login_access.c,v 1.12 2004/03/05 08:10:18 markm Exp $");
#else
__RCSID("$NetBSD: login_access.c,v 1.8 2014/01/07 02:07:43 joerg Exp $");
#endif
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <stdarg.h>
#include "pam_login_access.h"
#define _PATH_LOGACCESS "/etc/login.access"
static char fs[] = ":";
static char sep[] = ", \t";
#define YES 1
#define NO 0
static int from_match(const char *, const char *);
static int list_match(char *, const char *,
int (*)(const char *, const char *));
static int netgroup_match(const char *, const char *, const char *);
static int string_match(const char *, const char *);
static int user_match(const char *, const char *);
__printflike(2, 3)
static void
logit(int level, const char *fmt, ...)
{
va_list ap;
struct syslog_data data = SYSLOG_DATA_INIT;
openlog_r("pam_login_access", LOG_PID, LOG_AUTHPRIV, &data);
va_start(ap, fmt);
vsyslog_r(level, &data, fmt, ap);
va_end(ap);
closelog_r(&data);
}
int
login_access(const char *user, const char *from)
{
FILE *fp;
char line[BUFSIZ];
char *perm;
char *users;
char *froms;
int match = NO;
size_t end;
int lineno = 0;
if ((fp = fopen(_PATH_LOGACCESS, "r")) != NULL) {
while (!match && fgets(line, sizeof(line), fp)) {
lineno++;
if ((end = strlen(line)) == 0 || line[end - 1] != '\n') {
logit(LOG_ERR, "%s: line %d: missing newline or line too long",
_PATH_LOGACCESS, lineno);
continue;
}
if (line[0] == '#')
continue;
while (end > 0 && isspace((unsigned char)line[end - 1]))
end--;
line[end] = 0;
if (line[0] == 0)
continue;
if (!(perm = strtok(line, fs))
|| !(users = strtok((char *) 0, fs))
|| !(froms = strtok((char *) 0, fs))
|| strtok((char *) 0, fs)) {
logit(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS,
lineno);
continue;
}
if (perm[0] != '+' && perm[0] != '-') {
logit(LOG_ERR, "%s: line %d: bad first field", _PATH_LOGACCESS,
lineno);
continue;
}
match = (list_match(froms, from, from_match)
&& list_match(users, user, user_match));
}
(void) fclose(fp);
} else if (errno != ENOENT) {
logit(LOG_ERR, "cannot open %s: %s", _PATH_LOGACCESS, strerror(errno));
}
return (match == 0 || (line[0] == '+'));
}
static int
list_match(char *list, const char *item,
int (*match_fn)(const char *, const char *))
{
char *tok;
int match = NO;
for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
if (strcasecmp(tok, "EXCEPT") == 0)
break;
if ((match = (*match_fn)(tok, item)) != 0)
break;
}
if (match != NO) {
while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
;
if (tok == 0 || list_match((char *) 0, item, match_fn) == NO)
return (match);
}
return (NO);
}
static int
netgroup_match(const char *group __unused,
const char *machine __unused, const char *user __unused)
{
logit(LOG_ERR, "NIS netgroup support not configured");
return 0;
}
static int
user_match(const char *tok, const char *string)
{
struct group grres, *group;
char grbuf[1024];
int i;
if (tok[0] == '@') {
return (netgroup_match(tok + 1, (char *) 0, string));
} else if (string_match(tok, string)) {
return (YES);
} else if (getgrnam_r(tok, &grres, grbuf, sizeof(grbuf), &group) == 0 &&
group != NULL) {
for (i = 0; group->gr_mem[i]; i++)
if (strcasecmp(string, group->gr_mem[i]) == 0)
return (YES);
}
return (NO);
}
static int
from_match(const char *tok, const char *string)
{
size_t tok_len;
size_t str_len;
if (tok[0] == '@') {
return (netgroup_match(tok + 1, string, (char *) 0));
} else if (string_match(tok, string)) {
return (YES);
} else if (tok[0] == '.') {
if ((str_len = strlen(string)) > (tok_len = strlen(tok))
&& strcasecmp(tok, string + str_len - tok_len) == 0)
return (YES);
} else if (strcasecmp(tok, "LOCAL") == 0) {
if (strchr(string, '.') == 0)
return (YES);
} else if (tok[(tok_len = strlen(tok)) - 1] == '.'
&& strncmp(tok, string, tok_len) == 0) {
return (YES);
}
return (NO);
}
static int
string_match(const char *tok, const char *string)
{
if (strcasecmp(tok, "ALL") == 0) {
return (YES);
} else if (strcasecmp(tok, string) == 0) {
return (YES);
}
return (NO);
}