Changeset View
Standalone View
lib/libpam/modules/pam_login_access/login_access.c
Show All 11 Lines | |||||
static char sccsid[] = "%Z% %M% %I% %E% %U%"; | static char sccsid[] = "%Z% %M% %I% %E% %U%"; | ||||
#endif | #endif | ||||
#endif | #endif | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <sys/param.h> | |||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <grp.h> | #include <grp.h> | ||||
#include <netdb.h> | #include <netdb.h> | ||||
#include <pwd.h> | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <syslog.h> | #include <syslog.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include "pam_login_access.h" | #include "pam_login_access.h" | ||||
#define _PATH_LOGACCESS "/etc/login.access" | |||||
/* Delimiters for fields and for lists of users, ttys or hosts. */ | |||||
static char fs[] = ":"; /* field separator */ | |||||
static char sep[] = ", \t"; /* list-element separator */ | |||||
/* Constants to be used in assignments only, not in comparisons... */ | /* Constants to be used in assignments only, not in comparisons... */ | ||||
#define YES 1 | #define YES 1 | ||||
#define NO 0 | #define NO 0 | ||||
static int from_match(const char *, const char *); | static int from_match(const char *, const char *, struct pam_login_access_options *); | ||||
static int list_match(char *, const char *, | static int list_match(char *, const char *, | ||||
int (*)(const char *, const char *)); | int (*)(const char *, const char *, | ||||
struct pam_login_access_options *), | |||||
struct pam_login_access_options *); | |||||
static int netgroup_match(const char *, const char *, const char *); | static int netgroup_match(const char *, const char *, const char *); | ||||
static int string_match(const char *, const char *); | static int string_match(const char *, const char *); | ||||
static int user_match(const char *, const char *); | static int user_match(const char *, const char *, struct pam_login_access_options *); | ||||
static int group_match(const char *, const char *); | |||||
/* login_access - match username/group and host/tty with access control file */ | /* login_access - match username/group and host/tty with access control file */ | ||||
int | int | ||||
login_access(const char *user, const char *from) | login_access(const char *user, const char *from, | ||||
struct pam_login_access_options *login_access_opts) | |||||
bjk: It looks like this line is 99 columns? | |||||
{ | { | ||||
FILE *fp; | FILE *fp; | ||||
char line[BUFSIZ]; | char line[BUFSIZ]; | ||||
char *perm; /* becomes permission field */ | char *perm; /* becomes permission field */ | ||||
char *users; /* becomes list of login names */ | char *users; /* becomes list of login names */ | ||||
char *froms; /* becomes list of terminals or hosts */ | char *froms; /* becomes list of terminals or hosts */ | ||||
int match = NO; | int match = NO; | ||||
int end; | int end; | ||||
int lineno = 0; /* for diagnostics */ | int lineno = 0; /* for diagnostics */ | ||||
const char *fieldsep = login_access_opts->fieldsep; | |||||
/* | /* | ||||
* Process the table one line at a time and stop at the first match. | * Process the table one line at a time and stop at the first match. | ||||
* Blank lines and lines that begin with a '#' character are ignored. | * Blank lines and lines that begin with a '#' character are ignored. | ||||
* Non-comment lines are broken at the ':' character. All fields are | * Non-comment lines are broken at the ':' character. All fields are | ||||
* mandatory. The first field should be a "+" or "-" character. A | * mandatory. The first field should be a "+" or "-" character. A | ||||
* non-existing table means no access control. | * non-existing table means no access control. | ||||
*/ | */ | ||||
if ((fp = fopen(_PATH_LOGACCESS, "r")) != NULL) { | if ((fp = fopen(login_access_opts->accessfile, "r")) != NULL) { | ||||
while (!match && fgets(line, sizeof(line), fp)) { | while (!match && fgets(line, sizeof(line), fp)) { | ||||
lineno++; | lineno++; | ||||
if (line[end = strlen(line) - 1] != '\n') { | if (line[end = strlen(line) - 1] != '\n') { | ||||
syslog(LOG_ERR, "%s: line %d: missing newline or line too long", | syslog(LOG_ERR, "%s: line %d: missing newline or line too long", | ||||
_PATH_LOGACCESS, lineno); | login_access_opts->accessfile, lineno); | ||||
continue; | continue; | ||||
} | } | ||||
if (line[0] == '#') | if (line[0] == '#') | ||||
continue; /* comment line */ | continue; /* comment line */ | ||||
while (end > 0 && isspace(line[end - 1])) | while (end > 0 && isspace(line[end - 1])) | ||||
end--; | end--; | ||||
line[end] = 0; /* strip trailing whitespace */ | line[end] = 0; /* strip trailing whitespace */ | ||||
if (line[0] == 0) /* skip blank lines */ | if (line[0] == 0) /* skip blank lines */ | ||||
continue; | continue; | ||||
if (!(perm = strtok(line, fs)) | if (!(perm = strtok(line, fieldsep)) | ||||
|| !(users = strtok((char *) 0, fs)) | || !(users = strtok((char *) 0, fieldsep)) | ||||
Not Done Inline ActionsWould it be worth caching these in locals to avoid repeated dereferences (and make the code easier to read)? bjk: Would it be worth caching these in locals to avoid repeated dereferences (and make the code… | |||||
Done Inline ActionsThat makes sense. cy: That makes sense. | |||||
|| !(froms = strtok((char *) 0, fs)) | || !(froms = strtok((char *) 0, fieldsep)) | ||||
|| strtok((char *) 0, fs)) { | || strtok((char *) 0, fieldsep)) { | ||||
syslog(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS, | syslog(LOG_ERR, "%s: line %d: bad field count", login_access_opts->accessfile, | ||||
lineno); | lineno); | ||||
continue; | continue; | ||||
} | } | ||||
if (perm[0] != '+' && perm[0] != '-') { | if (perm[0] != '+' && perm[0] != '-') { | ||||
syslog(LOG_ERR, "%s: line %d: bad first field", _PATH_LOGACCESS, | syslog(LOG_ERR, "%s: line %d: bad first field", login_access_opts->accessfile, | ||||
lineno); | lineno); | ||||
continue; | continue; | ||||
} | } | ||||
match = (list_match(froms, from, from_match) | match = (list_match(froms, from, from_match, login_access_opts) | ||||
&& list_match(users, user, user_match)); | && list_match(users, user, user_match, login_access_opts)); | ||||
} | } | ||||
(void) fclose(fp); | (void) fclose(fp); | ||||
} else if (errno != ENOENT) { | } else if (errno != ENOENT) { | ||||
syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS); | syslog(LOG_ERR, "cannot open %s: %m", login_access_opts->accessfile); | ||||
} | } | ||||
return (match == 0 || (line[0] == '+')); | return (match == 0 || (line[0] == '+')); | ||||
} | } | ||||
/* list_match - match an item against a list of tokens with exceptions */ | /* list_match - match an item against a list of tokens with exceptions */ | ||||
static int | static int | ||||
list_match(char *list, const char *item, | list_match(char *list, const char *item, | ||||
int (*match_fn)(const char *, const char *)) | int (*match_fn)(const char *, const char *, struct pam_login_access_options *), | ||||
struct pam_login_access_options *login_access_opts) | |||||
{ | { | ||||
char *tok; | char *tok; | ||||
int match = NO; | int match = NO; | ||||
const char *listsep = login_access_opts->listsep; | |||||
/* | /* | ||||
* Process tokens one at a time. We have exhausted all possible matches | * Process tokens one at a time. We have exhausted all possible matches | ||||
* when we reach an "EXCEPT" token or the end of the list. If we do find | * when we reach an "EXCEPT" token or the end of the list. If we do find | ||||
* a match, look for an "EXCEPT" list and recurse to determine whether | * a match, look for an "EXCEPT" list and recurse to determine whether | ||||
* the match is affected by any exceptions. | * the match is affected by any exceptions. | ||||
*/ | */ | ||||
for (tok = strtok(list, sep); tok != NULL; tok = strtok((char *) 0, sep)) { | for (tok = strtok(list, listsep); tok != NULL; tok = strtok((char *) 0, listsep)) { | ||||
if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */ | if (strcmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */ | ||||
Not Done Inline ActionsThinking about strtok_r() is clearly a matter for a different change. bjk: Thinking about strtok_r() is clearly a matter for a different change. | |||||
Done Inline Actionsstrtok_r() isn't needed in this module. It's only used during account management. Only one will run. Ideally this should be replaced with strsep(), but that's for another project. cy: strtok_r() isn't needed in this module. It's only used during account management. Only one will… | |||||
break; | break; | ||||
if ((match = (*match_fn)(tok, item)) != 0) /* YES */ | if ((match = (*match_fn)(tok, item, login_access_opts)) != 0) /* YES */ | ||||
break; | break; | ||||
} | } | ||||
/* Process exceptions to matches. */ | /* Process exceptions to matches. */ | ||||
if (match != NO) { | if (match != NO) { | ||||
while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT")) | while ((tok = strtok((char *) 0, listsep)) && strcmp(tok, "EXCEPT")) | ||||
Done Inline ActionsThe indentation is off here. bjk: The indentation is off here. | |||||
Done Inline ActionsThanks. Good catch. cy: Thanks. Good catch. | |||||
/* VOID */ ; | /* VOID */ ; | ||||
if (tok == NULL || list_match((char *) 0, item, match_fn) == NO) | if (tok == NULL || list_match((char *) 0, item, match_fn, | ||||
login_access_opts) == NO) | |||||
return (match); | return (match); | ||||
} | } | ||||
return (NO); | return (NO); | ||||
} | } | ||||
/* netgroup_match - match group against machine or user */ | /* netgroup_match - match group against machine or user */ | ||||
static int | static int | ||||
netgroup_match(const char *group, const char *machine, const char *user) | netgroup_match(const char *group, const char *machine, const char *user) | ||||
Show All 15 Lines | netgroup_match(const char *group, const char *machine, const char *user) | ||||
return (NO); | return (NO); | ||||
} | } | ||||
if (innetgr(group, machine, user, domain) == 1) | if (innetgr(group, machine, user, domain) == 1) | ||||
return (YES); | return (YES); | ||||
return (NO); | return (NO); | ||||
} | } | ||||
/* group_match - match a group against one token */ | |||||
int | |||||
group_match(const char *tok, const char *username) | |||||
{ | |||||
struct group *group; | |||||
struct passwd *passwd; | |||||
gid_t *grouplist; | |||||
int i, ret, ngroups = NGROUPS; | |||||
if ((passwd = getpwnam(username)) == NULL) | |||||
Done Inline ActionsIt feels weird to call getpwnam() with a "groupname" (vs. getgrnam()), though IIUC the second parameter here is the username we're trying to determine access for so the code is okay. bjk: It feels weird to call getpwnam() with a "groupname" (vs. getgrnam()), though IIUC the second… | |||||
Done Inline ActionsThis string because we don't know if this is a user or group. In this context we consider it user so renaming string to groupname was wrong. Username is correct. Thank you for pointing this out. cy: This string because we don't know if this is a user or group. In this context we consider it… | |||||
return (NO); | |||||
errno = 0; | |||||
if ((group = getgrnam(tok)) == NULL) { | |||||
if (errno != 0) | |||||
syslog(LOG_ERR, "getgrnam() failed for %s: %s", username, strerror(errno)); | |||||
else | |||||
syslog(LOG_NOTICE, "group not found: %s", username); | |||||
return (NO); | |||||
} | |||||
if ((grouplist = calloc(ngroups, sizeof(gid_t))) == NULL) { | |||||
Done Inline ActionsI'm not sure I understand why "string" is the right thing after "grouplist:" here; "grouplist of" would make more sense, as would "grouplist for user" or "getgrouplist" bjk: I'm not sure I understand why "string" is the right thing after "grouplist:" here; "grouplist… | |||||
Done Inline Actions"string" came from its sister function user_match() which doesn't know if "string" is a user or a group. group_match() does. I'll change this to "groupname". cy: "string" came from its sister function user_match() which doesn't know if "string" is a user or… | |||||
syslog(LOG_ERR, "cannot allocate memory for grouplist: %s", username); | |||||
return (NO); | |||||
} | |||||
ret = NO; | |||||
if (getgrouplist(username, passwd->pw_gid, grouplist, &ngroups) != 0) | |||||
syslog(LOG_ERR, "getgrouplist() failed for %s", username); | |||||
for (i = 0; i < ngroups; i++) | |||||
if (grouplist[i] == group->gr_gid) | |||||
ret = YES; | |||||
free(grouplist); | |||||
return (ret); | |||||
} | |||||
/* user_match - match a username against one token */ | /* user_match - match a username against one token */ | ||||
Done Inline ActionsThree instances of free(grouplist) is perhaps on the boundary of moving to a cleanup handler. bjk: Three instances of free(grouplist) is perhaps on the boundary of moving to a cleanup handler. | |||||
static int | static int | ||||
user_match(const char *tok, const char *string) | user_match(const char *tok, const char *string, | ||||
struct pam_login_access_options *login_access_opts) | |||||
{ | { | ||||
struct group *group; | size_t stringlen; | ||||
int i; | char *grpstr; | ||||
int rc; | |||||
/* | /* | ||||
* If a token has the magic value "ALL" the match always succeeds. | * If a token has the magic value "ALL" the match always succeeds. | ||||
* Otherwise, return YES if the token fully matches the username, or if | * Otherwise, return YES if the token fully matches the username, or if | ||||
* the token is a group that contains the username. | * the token is a group that contains the username. | ||||
*/ | */ | ||||
if (tok[0] == '@') { /* netgroup */ | if (tok[0] == '@') { /* netgroup */ | ||||
return (netgroup_match(tok + 1, (char *) 0, string)); | return (netgroup_match(tok + 1, (char *) 0, string)); | ||||
} else if (tok[0] == '(' && tok[(stringlen = strlen(&tok[1]))] == ')') { /* group */ | |||||
Not Done Inline ActionsHow long is this line? bjk: How long is this line? | |||||
Done Inline Actions76. cy: 76. | |||||
if ((grpstr = strndup(&tok[1], stringlen - 1)) == NULL) { | |||||
syslog(LOG_ERR, "cannot allocate memory for %s", string); | |||||
return (NO); | |||||
} | |||||
Not Done Inline ActionsI'm not super-keen on this construction but can proffer no alternative. bjk: I'm not super-keen on this construction but can proffer no alternative. | |||||
Done Inline ActionsIt needs to be enclosed in parentheses. The only alternative would be nested if statements. I'm not enamored with this: } else if (tok[0] == '(') { /* group */ stringlen = strlen(&tok[1]; if (tok[stringlen] == ')') { if ((grpstr = strndup(&tok[1], stringlen - 1)) == NULL) { syslog(LOG_ERR, "cannot allocate memory for %s", string); return (NO); } } cy: It needs to be enclosed in parentheses. The only alternative would be nested if statements. I'm… | |||||
rc = group_match(grpstr, string); | |||||
free(grpstr); | |||||
Done Inline ActionsDo we need to check errno to know that strndup failed to allocate memory? bjk: Do we need to check errno to know that strndup failed to allocate memory? | |||||
Done Inline ActionsProbably not. cy: Probably not. | |||||
return (rc); | |||||
} else if (string_match(tok, string)) { /* ALL or exact match */ | } else if (string_match(tok, string)) { /* ALL or exact match */ | ||||
return (YES); | return (YES); | ||||
} else if ((group = getgrnam(tok)) != NULL) {/* try group membership */ | } else if (login_access_opts->defgroup == true) {/* try group membership */ | ||||
for (i = 0; group->gr_mem[i]; i++) | return (group_match(tok, string)); | ||||
Done Inline ActionsShouldn't we just return the result of group_match(), which is already YES or NO? bjk: Shouldn't we just return the result of group_match(), which is already YES or NO? | |||||
if (strcasecmp(string, group->gr_mem[i]) == 0) | |||||
return (YES); | |||||
} | } | ||||
return (NO); | return (NO); | ||||
} | } | ||||
/* from_match - match a host or tty against a list of tokens */ | /* from_match - match a host or tty against a list of tokens */ | ||||
static int | static int | ||||
from_match(const char *tok, const char *string) | from_match(const char *tok, const char *string, | ||||
struct pam_login_access_options *login_access_opts __unused) | |||||
{ | { | ||||
int tok_len; | int tok_len; | ||||
int str_len; | int str_len; | ||||
/* | /* | ||||
* If a token has the magic value "ALL" the match always succeeds. Return | * If a token has the magic value "ALL" the match always succeeds. Return | ||||
* YES if the token fully matches the string. If the token is a domain | * YES if the token fully matches the string. If the token is a domain | ||||
* name, return YES if it matches the last fields of the string. If the | * name, return YES if it matches the last fields of the string. If the | ||||
* token has the magic value "LOCAL", return YES if the string does not | * token has the magic value "LOCAL", return YES if the string does not | ||||
* contain a "." character. If the token is a network number, return YES | * contain a "." character. If the token is a network number, return YES | ||||
* if it matches the head of the string. | * if it matches the head of the string. | ||||
*/ | */ | ||||
if (tok[0] == '@') { /* netgroup */ | if (tok[0] == '@') { /* netgroup */ | ||||
return (netgroup_match(tok + 1, string, (char *) 0)); | return (netgroup_match(tok + 1, string, (char *) 0)); | ||||
} else if (string_match(tok, string)) { /* ALL or exact match */ | } else if (string_match(tok, string)) { /* ALL or exact match */ | ||||
return (YES); | return (YES); | ||||
} else if (tok[0] == '.') { /* domain: match last fields */ | } else if (tok[0] == '.') { /* domain: match last fields */ | ||||
if ((str_len = strlen(string)) > (tok_len = strlen(tok)) | if ((str_len = strlen(string)) > (tok_len = strlen(tok)) | ||||
&& strcasecmp(tok, string + str_len - tok_len) == 0) | && strcasecmp(tok, string + str_len - tok_len) == 0) | ||||
return (YES); | return (YES); | ||||
} else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */ | } else if (strcmp(tok, "LOCAL") == 0) { /* local: no dots */ | ||||
if (strchr(string, '.') == 0) | if (strchr(string, '.') == NULL) | ||||
return (YES); | return (YES); | ||||
} else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */ | } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */ | ||||
&& strncmp(tok, string, tok_len) == 0) { | && strncmp(tok, string, tok_len) == 0) { | ||||
return (YES); | return (YES); | ||||
} | } | ||||
return (NO); | return (NO); | ||||
Done Inline ActionsNot quite already being touched, but strchr returns a pointer not an integer. bjk: Not quite already being touched, but strchr returns a pointer not an integer. | |||||
Done Inline ActionsThis is original code. I'll fix this in another commit. strchr() returns a pointer not an int. Reported by: bjk cy: This is original code. I'll fix this in another commit.
strchr() returns a pointer not an int. | |||||
} | } | ||||
/* string_match - match a string against one token */ | /* string_match - match a string against one token */ | ||||
static int | static int | ||||
string_match(const char *tok, const char *string) | string_match(const char *tok, const char *string) | ||||
{ | { | ||||
/* | /* | ||||
* If the token has the magic value "ALL" the match always succeeds. | * If the token has the magic value "ALL" the match always succeeds. | ||||
* Otherwise, return YES if the token fully matches the string. | * Otherwise, return YES if the token fully matches the string. | ||||
*/ | */ | ||||
if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */ | if (strcmp(tok, "ALL") == 0) { /* all: always matches */ | ||||
return (YES); | return (YES); | ||||
} else if (strcasecmp(tok, string) == 0) { /* try exact match */ | } else if (strcasecmp(tok, string) == 0) { /* try exact match */ | ||||
return (YES); | return (YES); | ||||
} | } | ||||
return (NO); | return (NO); | ||||
} | } |
It looks like this line is 99 columns?