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>
29 #include "pam_login_access.h"
31 #define _PATH_LOGACCESS "/etc/login.access"
33 /* Delimiters for fields and for lists of users, ttys or hosts. */
35 static char fs[] = ":"; /* field separator */
36 static char sep[] = ", \t"; /* list-element separator */
38 /* Constants to be used in assignments only, not in comparisons... */
43 static int from_match(const char *, const char *);
44 static int list_match(char *, const char *,
45 int (*)(const char *, const char *));
46 static int netgroup_match(const char *, const char *, const char *);
47 static int string_match(const char *, const char *);
48 static int user_match(const char *, const char *);
50 /* login_access - match username/group and host/tty with access control file */
53 login_access(const char *user, const char *from)
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 */
65 * Process the table one line at a time and stop at the first match.
66 * Blank lines and lines that begin with a '#' character are ignored.
67 * Non-comment lines are broken at the ':' character. All fields are
68 * mandatory. The first field should be a "+" or "-" character. A
69 * non-existing table means no access control.
72 if ((fp = fopen(_PATH_LOGACCESS, "r")) != NULL) {
73 while (!match && fgets(line, sizeof(line), fp)) {
75 if (line[end = strlen(line) - 1] != '\n') {
76 syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
77 _PATH_LOGACCESS, lineno);
81 continue; /* comment line */
82 while (end > 0 && isspace(line[end - 1]))
84 line[end] = 0; /* strip trailing whitespace */
85 if (line[0] == 0) /* skip blank lines */
87 if (!(perm = strtok(line, fs))
88 || !(users = strtok((char *) 0, fs))
89 || !(froms = strtok((char *) 0, fs))
90 || strtok((char *) 0, fs)) {
91 syslog(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS,
95 if (perm[0] != '+' && perm[0] != '-') {
96 syslog(LOG_ERR, "%s: line %d: bad first field", _PATH_LOGACCESS,
100 match = (list_match(froms, from, from_match)
101 && list_match(users, user, user_match));
104 } else if (errno != ENOENT) {
105 syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS);
107 return (match == 0 || (line[0] == '+'));
110 /* list_match - match an item against a list of tokens with exceptions */
113 list_match(char *list, const char *item,
114 int (*match_fn)(const char *, const char *))
120 * Process tokens one at a time. We have exhausted all possible matches
121 * when we reach an "EXCEPT" token or the end of the list. If we do find
122 * a match, look for an "EXCEPT" list and recurse to determine whether
123 * the match is affected by any exceptions.
126 for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
127 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
129 if ((match = (*match_fn)(tok, item)) != 0) /* YES */
132 /* Process exceptions to matches. */
135 while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
137 if (tok == 0 || list_match((char *) 0, item, match_fn) == NO)
143 /* netgroup_match - match group against machine or user */
146 netgroup_match(const char *group __unused,
147 const char *machine __unused, const char *user __unused)
149 syslog(LOG_ERR, "NIS netgroup support not configured");
153 /* user_match - match a username against one token */
156 user_match(const char *tok, const char *string)
162 * If a token has the magic value "ALL" the match always succeeds.
163 * Otherwise, return YES if the token fully matches the username, or if
164 * the token is a group that contains the username.
167 if (tok[0] == '@') { /* netgroup */
168 return (netgroup_match(tok + 1, (char *) 0, string));
169 } else if (string_match(tok, string)) { /* ALL or exact match */
171 } else if ((group = getgrnam(tok)) != NULL) {/* try group membership */
172 for (i = 0; group->gr_mem[i]; i++)
173 if (strcasecmp(string, group->gr_mem[i]) == 0)
179 /* from_match - match a host or tty against a list of tokens */
182 from_match(const char *tok, const char *string)
188 * If a token has the magic value "ALL" the match always succeeds. Return
189 * YES if the token fully matches the string. If the token is a domain
190 * name, return YES if it matches the last fields of the string. If the
191 * token has the magic value "LOCAL", return YES if the string does not
192 * contain a "." character. If the token is a network number, return YES
193 * if it matches the head of the string.
196 if (tok[0] == '@') { /* netgroup */
197 return (netgroup_match(tok + 1, string, (char *) 0));
198 } else if (string_match(tok, string)) { /* ALL or exact match */
200 } else if (tok[0] == '.') { /* domain: match last fields */
201 if ((str_len = strlen(string)) > (tok_len = strlen(tok))
202 && strcasecmp(tok, string + str_len - tok_len) == 0)
204 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
205 if (strchr(string, '.') == 0)
207 } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */
208 && strncmp(tok, string, tok_len) == 0) {
214 /* string_match - match a string against one token */
217 string_match(const char *tok, const char *string)
221 * If the token has the magic value "ALL" the match always succeeds.
222 * Otherwise, return YES if the token fully matches the string.
225 if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */
227 } else if (strcasecmp(tok, string) == 0) { /* try exact match */