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 RCSID("$Id: login_access.c,v 1.19 1999/05/14 22:02:14 assar Exp $");
16 /* Delimiters for fields and for lists of users, ttys or hosts. */
18 static char fs[] = ":"; /* field separator */
19 static char sep[] = ", \t"; /* list-element separator */
21 /* Constants to be used in assignments only, not in comparisons... */
27 * A structure to bundle up all login-related information to keep the
28 * functional interfaces as generic as possible.
35 static int list_match(char *list, struct login_info *item,
36 int (*match_fn)(char *, struct login_info *));
37 static int user_match(char *tok, struct login_info *item);
38 static int from_match(char *tok, struct login_info *item);
39 static int string_match(char *tok, char *string);
41 /* login_access - match username/group and host/tty with access control file */
43 int login_access(struct passwd *user, char *from)
45 struct login_info item;
48 char *perm; /* becomes permission field */
49 char *users; /* becomes list of login names */
50 char *froms; /* becomes list of terminals or hosts */
53 int lineno = 0; /* for diagnostics */
57 * Bundle up the arguments to avoid unnecessary clumsiness lateron.
63 * Process the table one line at a time and stop at the first match.
64 * Blank lines and lines that begin with a '#' character are ignored.
65 * Non-comment lines are broken at the ':' character. All fields are
66 * mandatory. The first field should be a "+" or "-" character. A
67 * non-existing table means no access control.
70 if ((fp = fopen(_PATH_LOGACCESS, "r")) != 0) {
71 while (!match && fgets(line, sizeof(line), fp)) {
73 if (line[end = strlen(line) - 1] != '\n') {
74 syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
75 _PATH_LOGACCESS, lineno);
79 continue; /* comment line */
80 while (end > 0 && isspace((unsigned char)line[end - 1]))
82 line[end] = 0; /* strip trailing whitespace */
83 if (line[0] == 0) /* skip blank lines */
86 if (!(perm = strtok_r(line, fs, &foo))
87 || !(users = strtok_r(NULL, fs, &foo))
88 || !(froms = strtok_r(NULL, fs, &foo))
89 || strtok_r(NULL, fs, &foo)) {
90 syslog(LOG_ERR, "%s: line %d: bad field count",
95 if (perm[0] != '+' && perm[0] != '-') {
96 syslog(LOG_ERR, "%s: line %d: bad first field",
101 match = (list_match(froms, &item, from_match)
102 && list_match(users, &item, 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 */
114 list_match(char *list,
115 struct login_info *item,
116 int (*match_fn)(char *, struct login_info *))
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_r(list, sep, &foo);
131 tok = strtok_r(NULL, sep, &foo)) {
132 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
134 if ((match = (*match_fn) (tok, item)) != 0) /* YES */
137 /* Process exceptions to matches. */
140 while ((tok = strtok_r(NULL, sep, &foo)) && strcasecmp(tok, "EXCEPT"))
142 if (tok == 0 || list_match(NULL, item, match_fn) == NO)
148 /* myhostname - figure out local machine name */
150 static char *myhostname(void)
152 static char name[MAXHOSTNAMELEN + 1] = "";
155 gethostname(name, sizeof(name));
156 name[MAXHOSTNAMELEN] = 0;
161 /* netgroup_match - match group against machine or user */
163 static int netgroup_match(char *group, char *machine, char *user)
165 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
166 static char *mydomain = 0;
169 yp_get_default_domain(&mydomain);
170 return (innetgr(group, machine, user, mydomain));
172 syslog(LOG_ERR, "NIS netgroup support not configured");
177 /* user_match - match a username against one token */
179 static int user_match(char *tok, struct login_info *item)
181 char *string = item->user->pw_name;
182 struct login_info fake_item;
188 * If a token has the magic value "ALL" the match always succeeds.
189 * Otherwise, return YES if the token fully matches the username, if the
190 * token is a group that contains the username, or if the token is the
191 * name of the user's primary group.
194 if ((at = strchr(tok + 1, '@')) != 0) { /* split user@host pattern */
196 fake_item.from = myhostname();
197 return (user_match(tok, item) && from_match(at + 1, &fake_item));
198 } else if (tok[0] == '@') { /* netgroup */
199 return (netgroup_match(tok + 1, (char *) 0, string));
200 } else if (string_match(tok, string)) { /* ALL or exact match */
202 } else if ((group = getgrnam(tok)) != 0) { /* try group membership */
203 if (item->user->pw_gid == group->gr_gid)
205 for (i = 0; group->gr_mem[i]; i++)
206 if (strcasecmp(string, group->gr_mem[i]) == 0)
212 /* from_match - match a host or tty against a list of tokens */
214 static int from_match(char *tok, struct login_info *item)
216 char *string = item->from;
221 * If a token has the magic value "ALL" the match always succeeds. Return
222 * YES if the token fully matches the string. If the token is a domain
223 * name, return YES if it matches the last fields of the string. If the
224 * token has the magic value "LOCAL", return YES if the string does not
225 * contain a "." character. If the token is a network number, return YES
226 * if it matches the head of the string.
229 if (tok[0] == '@') { /* netgroup */
230 return (netgroup_match(tok + 1, string, (char *) 0));
231 } else if (string_match(tok, string)) { /* ALL or exact match */
233 } else if (tok[0] == '.') { /* domain: match last fields */
234 if ((str_len = strlen(string)) > (tok_len = strlen(tok))
235 && strcasecmp(tok, string + str_len - tok_len) == 0)
237 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
238 if (strchr(string, '.') == 0)
240 } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */
241 && strncmp(tok, string, tok_len) == 0) {
247 /* string_match - match a string against one token */
249 static int string_match(char *tok, char *string)
253 * If the token has the magic value "ALL" the match always succeeds.
254 * Otherwise, return YES if the token fully matches the string.
257 if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */
259 } else if (strcasecmp(tok, string) == 0) { /* try exact match */
264 #endif /* LOGIN_ACCES */