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