2 * This module implements a simple but effective form of login access
3 * control based on login names and on host (or domain) names, internet
4 * addresses (or network numbers), or on terminal line names in case of
5 * non-networked logins. Diagnostics are reported through syslog(3).
7 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
12 static char sccsid[] = "%Z% %M% %I% %E% %U%";
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
19 #include <sys/types.h>
20 #include <sys/param.h>
32 #include "pam_login_access.h"
34 /* Constants to be used in assignments only, not in comparisons... */
39 static int from_match(const char *, const char *, struct pam_login_access_options *);
40 static int list_match(char *, const char *,
41 int (*)(const char *, const char *,
42 struct pam_login_access_options *),
43 struct pam_login_access_options *);
44 static int netgroup_match(const char *, const char *, const char *);
45 static int string_match(const char *, const char *);
46 static int user_match(const char *, const char *, struct pam_login_access_options *);
47 static int group_match(const char *, const char *);
49 /* login_access - match username/group and host/tty with access control file */
52 login_access(const char *user, const char *from,
53 struct pam_login_access_options *login_access_opts)
57 char *perm; /* becomes permission field */
58 char *users; /* becomes list of login names */
59 char *froms; /* becomes list of terminals or hosts */
62 int lineno = 0; /* for diagnostics */
63 const char *fieldsep = login_access_opts->fieldsep;
66 * Process the table one line at a time and stop at the first match.
67 * Blank lines and lines that begin with a '#' character are ignored.
68 * Non-comment lines are broken at the ':' character. All fields are
69 * mandatory. The first field should be a "+" or "-" character. A
70 * non-existing table means no access control.
73 if ((fp = fopen(login_access_opts->accessfile, "r")) != NULL) {
74 while (!match && fgets(line, sizeof(line), fp)) {
76 if (line[end = strlen(line) - 1] != '\n') {
77 syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
78 login_access_opts->accessfile, lineno);
82 continue; /* comment line */
83 while (end > 0 && isspace(line[end - 1]))
85 line[end] = 0; /* strip trailing whitespace */
86 if (line[0] == 0) /* skip blank lines */
88 if (!(perm = strtok(line, fieldsep))
89 || !(users = strtok((char *) 0, fieldsep))
90 || !(froms = strtok((char *) 0, fieldsep))
91 || strtok((char *) 0, fieldsep)) {
92 syslog(LOG_ERR, "%s: line %d: bad field count", login_access_opts->accessfile,
96 if (perm[0] != '+' && perm[0] != '-') {
97 syslog(LOG_ERR, "%s: line %d: bad first field", login_access_opts->accessfile,
101 match = (list_match(froms, from, from_match, login_access_opts)
102 && list_match(users, user, user_match, login_access_opts));
105 } else if (errno != ENOENT) {
106 syslog(LOG_ERR, "cannot open %s: %m", login_access_opts->accessfile);
108 return (match == 0 || (line[0] == '+'));
111 /* list_match - match an item against a list of tokens with exceptions */
114 list_match(char *list, const char *item,
115 int (*match_fn)(const char *, const char *, struct pam_login_access_options *),
116 struct pam_login_access_options *login_access_opts)
120 const char *listsep = login_access_opts->listsep;
123 * Process tokens one at a time. We have exhausted all possible matches
124 * when we reach an "EXCEPT" token or the end of the list. If we do find
125 * a match, look for an "EXCEPT" list and recurse to determine whether
126 * the match is affected by any exceptions.
129 for (tok = strtok(list, listsep); tok != NULL; tok = strtok((char *) 0, listsep)) {
130 if (strcmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
132 if ((match = (*match_fn)(tok, item, login_access_opts)) != 0) /* YES */
135 /* Process exceptions to matches. */
138 while ((tok = strtok((char *) 0, listsep)) && strcmp(tok, "EXCEPT")) {
140 if (tok == NULL || list_match((char *) 0, item, match_fn,
141 login_access_opts) == NO) {
149 /* netgroup_match - match group against machine or user */
152 netgroup_match(const char *group, const char *machine, const char *user)
157 if (getdomainname(domain, sizeof(domain)) != 0 || *domain == '\0') {
158 syslog(LOG_ERR, "NIS netgroup support disabled: no NIS domain");
162 /* getdomainname() does not reliably terminate the string */
163 for (i = 0; i < sizeof(domain); ++i)
164 if (domain[i] == '\0')
166 if (i == sizeof(domain)) {
167 syslog(LOG_ERR, "NIS netgroup support disabled: invalid NIS domain");
171 if (innetgr(group, machine, user, domain) == 1)
176 /* group_match - match a group against one token */
179 group_match(const char *tok, const char *username)
182 struct passwd *passwd;
184 int i, ret, ngroups = NGROUPS;
186 if ((passwd = getpwnam(username)) == NULL)
189 if ((group = getgrnam(tok)) == NULL) {
191 syslog(LOG_ERR, "getgrnam() failed for %s: %s", username, strerror(errno));
193 syslog(LOG_NOTICE, "group not found: %s", username);
196 if ((grouplist = calloc(ngroups, sizeof(gid_t))) == NULL) {
197 syslog(LOG_ERR, "cannot allocate memory for grouplist: %s", username);
201 if (getgrouplist(username, passwd->pw_gid, grouplist, &ngroups) != 0)
202 syslog(LOG_ERR, "getgrouplist() failed for %s", username);
203 for (i = 0; i < ngroups; i++)
204 if (grouplist[i] == group->gr_gid)
210 /* user_match - match a username against one token */
213 user_match(const char *tok, const char *string,
214 struct pam_login_access_options *login_access_opts)
221 * If a token has the magic value "ALL" the match always succeeds.
222 * Otherwise, return YES if the token fully matches the username, or if
223 * the token is a group that contains the username.
226 if (tok[0] == '@') { /* netgroup */
227 return (netgroup_match(tok + 1, (char *) 0, string));
228 } else if (tok[0] == '(' && tok[(stringlen = strlen(&tok[1]))] == ')') { /* group */
229 if ((grpstr = strndup(&tok[1], stringlen - 1)) == NULL) {
230 syslog(LOG_ERR, "cannot allocate memory for %s", string);
233 rc = group_match(grpstr, string);
236 } else if (string_match(tok, string)) { /* ALL or exact match */
238 } else if (login_access_opts->defgroup == true) {/* try group membership */
239 return (group_match(tok, string));
244 /* from_match - match a host or tty against a list of tokens */
247 from_match(const char *tok, const char *string,
248 struct pam_login_access_options *login_access_opts __unused)
254 * If a token has the magic value "ALL" the match always succeeds. Return
255 * YES if the token fully matches the string. If the token is a domain
256 * name, return YES if it matches the last fields of the string. If the
257 * token has the magic value "LOCAL", return YES if the string does not
258 * contain a "." character. If the token is a network number, return YES
259 * if it matches the head of the string.
262 if (tok[0] == '@') { /* netgroup */
263 return (netgroup_match(tok + 1, string, (char *) 0));
264 } else if (string_match(tok, string)) { /* ALL or exact match */
266 } else if (tok[0] == '.') { /* domain: match last fields */
267 if ((str_len = strlen(string)) > (tok_len = strlen(tok))
268 && strcasecmp(tok, string + str_len - tok_len) == 0)
270 } else if (strcmp(tok, "LOCAL") == 0) { /* local: no dots */
271 if (strchr(string, '.') == NULL)
273 } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */
274 && strncmp(tok, string, tok_len) == 0) {
280 /* string_match - match a string against one token */
283 string_match(const char *tok, const char *string)
287 * If the token has the magic value "ALL" the match always succeeds.
288 * Otherwise, return YES if the token fully matches the string.
291 if (strcmp(tok, "ALL") == 0) { /* all: always matches */
293 } else if (strcasecmp(tok, string) == 0) { /* try exact match */