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.
13 static char sccsid[] = "%Z% %M% %I% %E% %U%";
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
20 #include <sys/types.h>
31 #include "pathnames.h"
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 __P((char *, char *));
44 static int list_match __P((char *, char *, int (*)(char *, char *)));
45 static int netgroup_match __P((char *, char *, char *));
46 static int string_match __P((char *, char *));
47 static int user_match __P((char *, char *));
49 /* login_access - match username/group and host/tty with access control file */
52 login_access(user, from)
58 char *perm; /* becomes permission field */
59 char *users; /* becomes list of login names */
60 char *froms; /* becomes list of terminals or hosts */
63 int lineno = 0; /* for diagnostics */
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(_PATH_LOGACCESS, "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 _PATH_LOGACCESS, 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, fs))
89 || !(users = strtok((char *) 0, fs))
90 || !(froms = strtok((char *) 0, fs))
91 || strtok((char *) 0, fs)) {
92 syslog(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS,
96 if (perm[0] != '+' && perm[0] != '-') {
97 syslog(LOG_ERR, "%s: line %d: bad first field", _PATH_LOGACCESS,
101 match = (list_match(froms, from, from_match)
102 && list_match(users, user, user_match));
105 } else if (errno != ENOENT) {
106 syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS);
108 return (match == 0 || (line[0] == '+'));
111 /* list_match - match an item against a list of tokens with exceptions */
113 static int list_match(list, item, match_fn)
116 int (*match_fn) __P((char *, char *));
122 * Process tokens one at a time. We have exhausted all possible matches
123 * when we reach an "EXCEPT" token or the end of the list. If we do find
124 * a match, look for an "EXCEPT" list and recurse to determine whether
125 * the match is affected by any exceptions.
128 for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
129 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
131 if ((match = (*match_fn)(tok, item)) != NULL) /* YES */
134 /* Process exceptions to matches. */
137 while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
139 if (tok == 0 || list_match((char *) 0, item, match_fn) == NO)
145 /* netgroup_match - match group against machine or user */
147 static int netgroup_match(group, machine, user)
148 char *group __unused;
149 char *machine __unused;
152 syslog(LOG_ERR, "NIS netgroup support not configured");
156 /* user_match - match a username against one token */
158 static int user_match(tok, string)
166 * If a token has the magic value "ALL" the match always succeeds.
167 * Otherwise, return YES if the token fully matches the username, or if
168 * the token is a group that contains the username.
171 if (tok[0] == '@') { /* netgroup */
172 return (netgroup_match(tok + 1, (char *) 0, string));
173 } else if (string_match(tok, string)) { /* ALL or exact match */
175 } else if ((group = getgrnam(tok)) != NULL) {/* try group membership */
176 for (i = 0; group->gr_mem[i]; i++)
177 if (strcasecmp(string, group->gr_mem[i]) == 0)
183 /* from_match - match a host or tty against a list of tokens */
185 static int from_match(tok, string)
193 * If a token has the magic value "ALL" the match always succeeds. Return
194 * YES if the token fully matches the string. If the token is a domain
195 * name, return YES if it matches the last fields of the string. If the
196 * token has the magic value "LOCAL", return YES if the string does not
197 * contain a "." character. If the token is a network number, return YES
198 * if it matches the head of the string.
201 if (tok[0] == '@') { /* netgroup */
202 return (netgroup_match(tok + 1, string, (char *) 0));
203 } else if (string_match(tok, string)) { /* ALL or exact match */
205 } else if (tok[0] == '.') { /* domain: match last fields */
206 if ((str_len = strlen(string)) > (tok_len = strlen(tok))
207 && strcasecmp(tok, string + str_len - tok_len) == 0)
209 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
210 if (strchr(string, '.') == 0)
212 } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */
213 && strncmp(tok, string, tok_len) == 0) {
219 /* string_match - match a string against one token */
221 static int string_match(tok, string)
227 * If the token has the magic value "ALL" the match always succeeds.
228 * Otherwise, return YES if the token fully matches the string.
231 if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */
233 } else if (strcasecmp(tok, string) == 0) { /* try exact match */
238 #endif /* LOGIN_ACCES */