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 const char sccsid[] = "%Z% %M% %I% %E% %U%";
18 #include <sys/types.h>
25 #include "pathnames.h"
27 /* Delimiters for fields and for lists of users, ttys or hosts. */
29 static char fs[] = ":"; /* field separator */
30 static char sep[] = ", \t"; /* list-element separator */
32 /* Constants to be used in assignments only, not in comparisons... */
37 static int list_match();
38 static int user_match();
39 static int from_match();
40 static int string_match();
42 /* login_access - match username/group and host/tty with access control file */
45 login_access(user, from)
51 char *perm; /* becomes permission field */
52 char *users; /* becomes list of login names */
53 char *froms; /* becomes list of terminals or hosts */
56 int lineno = 0; /* for diagnostics */
59 * Process the table one line at a time and stop at the first match.
60 * Blank lines and lines that begin with a '#' character are ignored.
61 * Non-comment lines are broken at the ':' character. All fields are
62 * mandatory. The first field should be a "+" or "-" character. A
63 * non-existing table means no access control.
66 if ((fp = fopen(_PATH_LOGACCESS, "r")) != NULL) {
67 while (!match && fgets(line, sizeof(line), fp)) {
69 if (line[end = strlen(line) - 1] != '\n') {
70 syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
71 _PATH_LOGACCESS, lineno);
75 continue; /* comment line */
76 while (end > 0 && isspace(line[end - 1]))
78 line[end] = 0; /* strip trailing whitespace */
79 if (line[0] == 0) /* skip blank lines */
81 if (!(perm = strtok(line, fs))
82 || !(users = strtok((char *) 0, fs))
83 || !(froms = strtok((char *) 0, fs))
84 || strtok((char *) 0, fs)) {
85 syslog(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS,
89 if (perm[0] != '+' && perm[0] != '-') {
90 syslog(LOG_ERR, "%s: line %d: bad first field", _PATH_LOGACCESS,
94 match = (list_match(froms, from, from_match)
95 && list_match(users, user, user_match));
98 } else if (errno != ENOENT) {
99 syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS);
101 return (match == 0 || (line[0] == '+'));
104 /* list_match - match an item against a list of tokens with exceptions */
106 static int list_match(list, item, match_fn)
115 * Process tokens one at a time. We have exhausted all possible matches
116 * when we reach an "EXCEPT" token or the end of the list. If we do find
117 * a match, look for an "EXCEPT" list and recurse to determine whether
118 * the match is affected by any exceptions.
121 for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
122 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
124 if ((match = (*match_fn)(tok, item)) != NULL) /* YES */
127 /* Process exceptions to matches. */
130 while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
132 if (tok == 0 || list_match((char *) 0, item, match_fn) == NO)
138 /* netgroup_match - match group against machine or user */
140 static int netgroup_match(group, machine, user)
146 static char *mydomain = 0;
149 yp_get_default_domain(&mydomain);
150 return (innetgr(group, machine, user, mydomain));
152 syslog(LOG_ERR, "NIS netgroup support not configured");
157 /* user_match - match a username against one token */
159 static int user_match(tok, string)
167 * If a token has the magic value "ALL" the match always succeeds.
168 * Otherwise, return YES if the token fully matches the username, or if
169 * the token is a group that contains the username.
172 if (tok[0] == '@') { /* netgroup */
173 return (netgroup_match(tok + 1, (char *) 0, string));
174 } else if (string_match(tok, string)) { /* ALL or exact match */
176 } else if ((group = getgrnam(tok)) != NULL) {/* try group membership */
177 for (i = 0; group->gr_mem[i]; i++)
178 if (strcasecmp(string, group->gr_mem[i]) == 0)
184 /* from_match - match a host or tty against a list of tokens */
186 static int from_match(tok, string)
194 * If a token has the magic value "ALL" the match always succeeds. Return
195 * YES if the token fully matches the string. If the token is a domain
196 * name, return YES if it matches the last fields of the string. If the
197 * token has the magic value "LOCAL", return YES if the string does not
198 * contain a "." character. If the token is a network number, return YES
199 * if it matches the head of the string.
202 if (tok[0] == '@') { /* netgroup */
203 return (netgroup_match(tok + 1, string, (char *) 0));
204 } else if (string_match(tok, string)) { /* ALL or exact match */
206 } else if (tok[0] == '.') { /* domain: match last fields */
207 if ((str_len = strlen(string)) > (tok_len = strlen(tok))
208 && strcasecmp(tok, string + str_len - tok_len) == 0)
210 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
211 if (strchr(string, '.') == 0)
213 } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */
214 && strncmp(tok, string, tok_len) == 0) {
220 /* string_match - match a string against one token */
222 static int string_match(tok, string)
228 * If the token has the magic value "ALL" the match always succeeds.
229 * Otherwise, return YES if the token fully matches the string.
232 if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */
234 } else if (strcasecmp(tok, string) == 0) { /* try exact match */
239 #endif /* LOGIN_ACCES */