#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <grp.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.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 *);
int
login_access(const char *user, const char *from)
{
FILE *fp;
char line[BUFSIZ];
char *perm;
char *users;
char *froms;
int match = NO;
int end;
int lineno = 0;
if ((fp = fopen(_PATH_LOGACCESS, "r")) != NULL) {
while (!match && fgets(line, sizeof(line), fp)) {
lineno++;
if (line[end = strlen(line) - 1] != '\n') {
syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
_PATH_LOGACCESS, lineno);
continue;
}
if (line[0] == '#')
continue;
while (end > 0 && isspace(line[end - 1]))
end--;
line[end] = 0;
if (line[0] == 0)
continue;
if (!(perm = strtok(line, fs))
|| !(users = strtok(NULL, fs))
|| !(froms = strtok(NULL, fs))
|| strtok(NULL, fs)) {
syslog(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS,
lineno);
continue;
}
if (perm[0] != '+' && perm[0] != '-') {
syslog(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));
}
fclose(fp);
} else if (errno != ENOENT) {
syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS);
}
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 != NULL; tok = strtok(NULL, sep)) {
if (strcasecmp(tok, "EXCEPT") == 0)
break;
if ((match = (*match_fn)(tok, item)) != 0)
break;
}
if (match != NO) {
while ((tok = strtok(NULL, sep)) && strcasecmp(tok, "EXCEPT"))
;
if (tok == NULL || list_match(NULL, item, match_fn) == NO)
return (match);
}
return (NO);
}
static int
netgroup_match(const char *group, const char *machine, const char *user)
{
char domain[1024];
unsigned int i;
if (getdomainname(domain, sizeof(domain)) != 0 || *domain == '\0') {
syslog(LOG_ERR, "NIS netgroup support disabled: no NIS domain");
return (NO);
}
for (i = 0; i < sizeof(domain); ++i)
if (domain[i] == '\0')
break;
if (i == sizeof(domain)) {
syslog(LOG_ERR, "NIS netgroup support disabled: invalid NIS domain");
return (NO);
}
if (innetgr(group, machine, user, domain) == 1)
return (YES);
return (NO);
}
static int
user_match(const char *tok, const char *string)
{
struct group *group;
int i;
if (tok[0] == '@') {
return (netgroup_match(tok + 1, NULL, string));
} else if (string_match(tok, string)) {
return (YES);
} else if ((group = getgrnam(tok)) != 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)
{
int tok_len;
int str_len;
if (tok[0] == '@') {
return (netgroup_match(tok + 1, string, NULL));
} 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);
}