1  /*
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).
6   *
7   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
8   */
9 
10 #if 0
11 #ifndef lint
12 static char sccsid[] = "%Z% %M% %I% %E% %U%";
13 #endif
14 #endif
15 
16 #include <sys/types.h>
17 #include <sys/param.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include <grp.h>
21 #include <netdb.h>
22 #include <pwd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <syslog.h>
27 #include <unistd.h>
28 
29 #include "pam_login_access.h"
30 
31  /* Constants to be used in assignments only, not in comparisons... */
32 
33 #define YES             1
34 #define NO              0
35 
36 static int	from_match(const char *, const char *, struct pam_login_access_options *);
37 static int	list_match(char *, const char *,
38 				int (*)(const char *, const char *,
39 				struct pam_login_access_options *),
40 				struct pam_login_access_options *);
41 static int	netgroup_match(const char *, const char *, const char *);
42 static int	string_match(const char *, const char *);
43 static int	user_match(const char *, const char *, struct pam_login_access_options *);
44 static int	group_match(const char *, const char *);
45 
46 /* login_access - match username/group and host/tty with access control file */
47 
48 int
49 login_access(const char *user, const char *from,
50 	struct pam_login_access_options *login_access_opts)
51 {
52     FILE   *fp;
53     char    line[BUFSIZ];
54     char   *perm;			/* becomes permission field */
55     char   *users;			/* becomes list of login names */
56     char   *froms;			/* becomes list of terminals or hosts */
57     int     match = NO;
58     int     end;
59     int     lineno = 0;			/* for diagnostics */
60     const char *fieldsep = login_access_opts->fieldsep;
61 
62     /*
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.
68      */
69 
70     if ((fp = fopen(login_access_opts->accessfile, "r")) != NULL) {
71 	while (!match && fgets(line, sizeof(line), fp)) {
72 	    lineno++;
73 	    if (line[end = strlen(line) - 1] != '\n') {
74 		syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
75 		       login_access_opts->accessfile, lineno);
76 		continue;
77 	    }
78 	    if (line[0] == '#')
79 		continue;			/* comment line */
80 	    while (end > 0 && isspace(line[end - 1]))
81 		end--;
82 	    line[end] = 0;			/* strip trailing whitespace */
83 	    if (line[0] == 0)			/* skip blank lines */
84 		continue;
85 	    if (!(perm = strtok(line, fieldsep))
86 		|| !(users = strtok((char *) 0, fieldsep))
87 		|| !(froms = strtok((char *) 0, fieldsep))
88 		|| strtok((char *) 0, fieldsep)) {
89 		syslog(LOG_ERR, "%s: line %d: bad field count", login_access_opts->accessfile,
90 		       lineno);
91 		continue;
92 	    }
93 	    if (perm[0] != '+' && perm[0] != '-') {
94 		syslog(LOG_ERR, "%s: line %d: bad first field", login_access_opts->accessfile,
95 		       lineno);
96 		continue;
97 	    }
98 	    match = (list_match(froms, from, from_match, login_access_opts)
99 		     && list_match(users, user, user_match, login_access_opts));
100 	}
101 	(void) fclose(fp);
102     } else if (errno != ENOENT) {
103 	syslog(LOG_ERR, "cannot open %s: %m", login_access_opts->accessfile);
104     }
105     return (match == 0 || (line[0] == '+'));
106 }
107 
108 /* list_match - match an item against a list of tokens with exceptions */
109 
110 static int
111 list_match(char *list, const char *item,
112     int (*match_fn)(const char *, const char *, struct pam_login_access_options *),
113     struct pam_login_access_options *login_access_opts)
114 {
115     char   *tok;
116     int     match = NO;
117     const char *listsep = login_access_opts->listsep;
118 
119     /*
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.
124      */
125 
126     for (tok = strtok(list, listsep); tok != NULL; tok = strtok((char *) 0, listsep)) {
127 	if (strcmp(tok, "EXCEPT") == 0)	/* EXCEPT: give up */
128 	    break;
129 	if ((match = (*match_fn)(tok, item, login_access_opts)) != 0)	/* YES */
130 	    break;
131     }
132     /* Process exceptions to matches. */
133 
134     if (match != NO) {
135 	while ((tok = strtok((char *) 0, listsep)) && strcmp(tok, "EXCEPT")) {
136 	     /* VOID */ ;
137 	}
138 	if (tok == NULL ||
139 	    list_match((char *) 0, item, match_fn, login_access_opts) == NO) {
140 		return (match);
141 	}
142     }
143     return (NO);
144 }
145 
146 /* netgroup_match - match group against machine or user */
147 
148 static int
149 netgroup_match(const char *group, const char *machine, const char *user)
150 {
151     char domain[1024];
152     unsigned int i;
153 
154     if (getdomainname(domain, sizeof(domain)) != 0 || *domain == '\0') {
155 	syslog(LOG_ERR, "NIS netgroup support disabled: no NIS domain");
156 	return (NO);
157     }
158 
159     /* getdomainname() does not reliably terminate the string */
160     for (i = 0; i < sizeof(domain); ++i)
161 	if (domain[i] == '\0')
162 	    break;
163     if (i == sizeof(domain)) {
164 	syslog(LOG_ERR, "NIS netgroup support disabled: invalid NIS domain");
165 	return (NO);
166     }
167 
168     if (innetgr(group, machine, user, domain) == 1)
169 	return (YES);
170     return (NO);
171 }
172 
173 /* group_match - match a group against one token */
174 
175 int
176 group_match(const char *tok, const char *username)
177 {
178     struct group *group;
179     struct passwd *passwd;
180     gid_t *grouplist;
181     int i, ret, ngroups = NGROUPS;
182 
183     if ((passwd = getpwnam(username)) == NULL)
184 	return (NO);
185     errno = 0;
186     if ((group = getgrnam(tok)) == NULL) {
187 	if (errno != 0)
188 	    syslog(LOG_ERR, "getgrnam() failed for %s: %s", username, strerror(errno));
189 	else
190 	    syslog(LOG_NOTICE, "group not found: %s", username);
191 	return (NO);
192     }
193     if ((grouplist = calloc(ngroups, sizeof(gid_t))) == NULL) {
194 	syslog(LOG_ERR, "cannot allocate memory for grouplist: %s", username);
195 	return (NO);
196     }
197     ret = NO;
198     if (getgrouplist(username, passwd->pw_gid, grouplist, &ngroups) != 0)
199 	syslog(LOG_ERR, "getgrouplist() failed for %s", username);
200     for (i = 0; i < ngroups; i++)
201 	if (grouplist[i] == group->gr_gid)
202 	    ret = YES;
203     free(grouplist);
204     return (ret);
205 }
206 
207 /* user_match - match a username against one token */
208 
209 static int
210 user_match(const char *tok, const char *string,
211 	struct pam_login_access_options *login_access_opts)
212 {
213     size_t stringlen;
214     char *grpstr;
215     int rc;
216 
217     /*
218      * If a token has the magic value "ALL" the match always succeeds.
219      * Otherwise, return YES if the token fully matches the username, or if
220      * the token is a group that contains the username.
221      */
222 
223     if (tok[0] == '@') {			/* netgroup */
224 	return (netgroup_match(tok + 1, (char *) 0, string));
225     } else if (tok[0] == '(' && tok[(stringlen = strlen(&tok[1]))] == ')') {		/* group */
226 	if ((grpstr = strndup(&tok[1], stringlen - 1)) == NULL) {
227 	    syslog(LOG_ERR, "cannot allocate memory for %s", string);
228 	    return (NO);
229 	}
230 	rc = group_match(grpstr, string);
231 	free(grpstr);
232 	return (rc);
233     } else if (string_match(tok, string)) {	/* ALL or exact match */
234 	return (YES);
235     } else if (login_access_opts->defgroup == true) {/* try group membership */
236 	return (group_match(tok, string));
237     }
238     return (NO);
239 }
240 
241 /* from_match - match a host or tty against a list of tokens */
242 
243 static int
244 from_match(const char *tok, const char *string,
245 	struct pam_login_access_options *login_access_opts __unused)
246 {
247     int     tok_len;
248     int     str_len;
249 
250     /*
251      * If a token has the magic value "ALL" the match always succeeds. Return
252      * YES if the token fully matches the string. If the token is a domain
253      * name, return YES if it matches the last fields of the string. If the
254      * token has the magic value "LOCAL", return YES if the string does not
255      * contain a "." character. If the token is a network number, return YES
256      * if it matches the head of the string.
257      */
258 
259     if (tok[0] == '@') {			/* netgroup */
260 	return (netgroup_match(tok + 1, string, (char *) 0));
261     } else if (string_match(tok, string)) {	/* ALL or exact match */
262 	return (YES);
263     } else if (tok[0] == '.') {			/* domain: match last fields */
264 	if ((str_len = strlen(string)) > (tok_len = strlen(tok))
265 	    && strcasecmp(tok, string + str_len - tok_len) == 0)
266 	    return (YES);
267     } else if (strcmp(tok, "LOCAL") == 0) {	/* local: no dots */
268 	if (strchr(string, '.') == NULL)
269 	    return (YES);
270     } else if (tok[(tok_len = strlen(tok)) - 1] == '.'	/* network */
271 	       && strncmp(tok, string, tok_len) == 0) {
272 	return (YES);
273     }
274     return (NO);
275 }
276 
277 /* string_match - match a string against one token */
278 
279 static int
280 string_match(const char *tok, const char *string)
281 {
282 
283     /*
284      * If the token has the magic value "ALL" the match always succeeds.
285      * Otherwise, return YES if the token fully matches the string.
286      */
287 
288     if (strcmp(tok, "ALL") == 0) {		/* all: always matches */
289 	return (YES);
290     } else if (strcasecmp(tok, string) == 0) {	/* try exact match */
291 	return (YES);
292     }
293     return (NO);
294 }
295