xref: /freebsd/lib/libpam/modules/pam_login_access/login_access.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
17c4c6e58SGuido van Rooij  /*
27c4c6e58SGuido van Rooij   * This module implements a simple but effective form of login access
37c4c6e58SGuido van Rooij   * control based on login names and on host (or domain) names, internet
47c4c6e58SGuido van Rooij   * addresses (or network numbers), or on terminal line names in case of
57c4c6e58SGuido van Rooij   * non-networked logins. Diagnostics are reported through syslog(3).
67c4c6e58SGuido van Rooij   *
77c4c6e58SGuido van Rooij   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
87c4c6e58SGuido van Rooij   */
97c4c6e58SGuido van Rooij 
109f5b04e9SDavid Malone #if 0
117c4c6e58SGuido van Rooij #ifndef lint
129f5b04e9SDavid Malone static char sccsid[] = "%Z% %M% %I% %E% %U%";
137c4c6e58SGuido van Rooij #endif
149f5b04e9SDavid Malone #endif
159f5b04e9SDavid Malone 
167c4c6e58SGuido van Rooij #include <sys/types.h>
170d960f73SCy Schubert #include <sys/param.h>
18d2f6cd8fSMark Murray #include <ctype.h>
197c4c6e58SGuido van Rooij #include <errno.h>
20d2f6cd8fSMark Murray #include <grp.h>
21ca2ddac3SDag-Erling Smørgrav #include <netdb.h>
220d960f73SCy Schubert #include <pwd.h>
23d2f6cd8fSMark Murray #include <stdio.h>
247c4c6e58SGuido van Rooij #include <stdlib.h>
25d2f6cd8fSMark Murray #include <string.h>
26d2f6cd8fSMark Murray #include <syslog.h>
27d2f6cd8fSMark Murray #include <unistd.h>
287c4c6e58SGuido van Rooij 
29ac569969SMark Murray #include "pam_login_access.h"
30ac569969SMark Murray 
317c4c6e58SGuido van Rooij  /* Constants to be used in assignments only, not in comparisons... */
327c4c6e58SGuido van Rooij 
337c4c6e58SGuido van Rooij #define YES             1
347c4c6e58SGuido van Rooij #define NO              0
357c4c6e58SGuido van Rooij 
36e8c4b9d4SCy Schubert static int	from_match(const char *, const char *, struct pam_login_access_options *);
37ac569969SMark Murray static int	list_match(char *, const char *,
38e8c4b9d4SCy Schubert 				int (*)(const char *, const char *,
39e8c4b9d4SCy Schubert 				struct pam_login_access_options *),
40e8c4b9d4SCy Schubert 				struct pam_login_access_options *);
41ac569969SMark Murray static int	netgroup_match(const char *, const char *, const char *);
42ac569969SMark Murray static int	string_match(const char *, const char *);
43e8c4b9d4SCy Schubert static int	user_match(const char *, const char *, struct pam_login_access_options *);
44e8c4b9d4SCy Schubert static int	group_match(const char *, const char *);
457c4c6e58SGuido van Rooij 
467c4c6e58SGuido van Rooij /* login_access - match username/group and host/tty with access control file */
477c4c6e58SGuido van Rooij 
482ed98aa0SPaul Traina int
login_access(const char * user,const char * from,struct pam_login_access_options * login_access_opts)49e8c4b9d4SCy Schubert login_access(const char *user, const char *from,
50e8c4b9d4SCy Schubert 	struct pam_login_access_options *login_access_opts)
517c4c6e58SGuido van Rooij {
527c4c6e58SGuido van Rooij     FILE   *fp;
537c4c6e58SGuido van Rooij     char    line[BUFSIZ];
547c4c6e58SGuido van Rooij     char   *perm;			/* becomes permission field */
557c4c6e58SGuido van Rooij     char   *users;			/* becomes list of login names */
567c4c6e58SGuido van Rooij     char   *froms;			/* becomes list of terminals or hosts */
577c4c6e58SGuido van Rooij     int     match = NO;
587c4c6e58SGuido van Rooij     int     end;
597c4c6e58SGuido van Rooij     int     lineno = 0;			/* for diagnostics */
60e8c4b9d4SCy Schubert     const char *fieldsep = login_access_opts->fieldsep;
617c4c6e58SGuido van Rooij 
627c4c6e58SGuido van Rooij     /*
637c4c6e58SGuido van Rooij      * Process the table one line at a time and stop at the first match.
647c4c6e58SGuido van Rooij      * Blank lines and lines that begin with a '#' character are ignored.
657c4c6e58SGuido van Rooij      * Non-comment lines are broken at the ':' character. All fields are
667c4c6e58SGuido van Rooij      * mandatory. The first field should be a "+" or "-" character. A
677c4c6e58SGuido van Rooij      * non-existing table means no access control.
687c4c6e58SGuido van Rooij      */
697c4c6e58SGuido van Rooij 
70e8c4b9d4SCy Schubert     if ((fp = fopen(login_access_opts->accessfile, "r")) != NULL) {
717c4c6e58SGuido van Rooij 	while (!match && fgets(line, sizeof(line), fp)) {
727c4c6e58SGuido van Rooij 	    lineno++;
737c4c6e58SGuido van Rooij 	    if (line[end = strlen(line) - 1] != '\n') {
747c4c6e58SGuido van Rooij 		syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
75e8c4b9d4SCy Schubert 		       login_access_opts->accessfile, lineno);
767c4c6e58SGuido van Rooij 		continue;
777c4c6e58SGuido van Rooij 	    }
787c4c6e58SGuido van Rooij 	    if (line[0] == '#')
797c4c6e58SGuido van Rooij 		continue;			/* comment line */
807c4c6e58SGuido van Rooij 	    while (end > 0 && isspace(line[end - 1]))
817c4c6e58SGuido van Rooij 		end--;
827c4c6e58SGuido van Rooij 	    line[end] = 0;			/* strip trailing whitespace */
837c4c6e58SGuido van Rooij 	    if (line[0] == 0)			/* skip blank lines */
847c4c6e58SGuido van Rooij 		continue;
85e8c4b9d4SCy Schubert 	    if (!(perm = strtok(line, fieldsep))
86e8c4b9d4SCy Schubert 		|| !(users = strtok((char *) 0, fieldsep))
87e8c4b9d4SCy Schubert 		|| !(froms = strtok((char *) 0, fieldsep))
88e8c4b9d4SCy Schubert 		|| strtok((char *) 0, fieldsep)) {
89e8c4b9d4SCy Schubert 		syslog(LOG_ERR, "%s: line %d: bad field count", login_access_opts->accessfile,
907c4c6e58SGuido van Rooij 		       lineno);
917c4c6e58SGuido van Rooij 		continue;
927c4c6e58SGuido van Rooij 	    }
937c4c6e58SGuido van Rooij 	    if (perm[0] != '+' && perm[0] != '-') {
94e8c4b9d4SCy Schubert 		syslog(LOG_ERR, "%s: line %d: bad first field", login_access_opts->accessfile,
957c4c6e58SGuido van Rooij 		       lineno);
967c4c6e58SGuido van Rooij 		continue;
977c4c6e58SGuido van Rooij 	    }
98e8c4b9d4SCy Schubert 	    match = (list_match(froms, from, from_match, login_access_opts)
99e8c4b9d4SCy Schubert 		     && list_match(users, user, user_match, login_access_opts));
1007c4c6e58SGuido van Rooij 	}
1017c4c6e58SGuido van Rooij 	(void) fclose(fp);
1027c4c6e58SGuido van Rooij     } else if (errno != ENOENT) {
103e8c4b9d4SCy Schubert 	syslog(LOG_ERR, "cannot open %s: %m", login_access_opts->accessfile);
1047c4c6e58SGuido van Rooij     }
1057c4c6e58SGuido van Rooij     return (match == 0 || (line[0] == '+'));
1067c4c6e58SGuido van Rooij }
1077c4c6e58SGuido van Rooij 
1087c4c6e58SGuido van Rooij /* list_match - match an item against a list of tokens with exceptions */
1097c4c6e58SGuido van Rooij 
110ac569969SMark Murray static int
list_match(char * list,const char * item,int (* match_fn)(const char *,const char *,struct pam_login_access_options *),struct pam_login_access_options * login_access_opts)111ac569969SMark Murray list_match(char *list, const char *item,
112e8c4b9d4SCy Schubert     int (*match_fn)(const char *, const char *, struct pam_login_access_options *),
113e8c4b9d4SCy Schubert     struct pam_login_access_options *login_access_opts)
1147c4c6e58SGuido van Rooij {
1157c4c6e58SGuido van Rooij     char   *tok;
1167c4c6e58SGuido van Rooij     int     match = NO;
117e8c4b9d4SCy Schubert     const char *listsep = login_access_opts->listsep;
1187c4c6e58SGuido van Rooij 
1197c4c6e58SGuido van Rooij     /*
1207c4c6e58SGuido van Rooij      * Process tokens one at a time. We have exhausted all possible matches
1217c4c6e58SGuido van Rooij      * when we reach an "EXCEPT" token or the end of the list. If we do find
1227c4c6e58SGuido van Rooij      * a match, look for an "EXCEPT" list and recurse to determine whether
1237c4c6e58SGuido van Rooij      * the match is affected by any exceptions.
1247c4c6e58SGuido van Rooij      */
1257c4c6e58SGuido van Rooij 
126e8c4b9d4SCy Schubert     for (tok = strtok(list, listsep); tok != NULL; tok = strtok((char *) 0, listsep)) {
127f47effabSCy Schubert 	if (strcmp(tok, "EXCEPT") == 0)	/* EXCEPT: give up */
1287c4c6e58SGuido van Rooij 	    break;
129e8c4b9d4SCy Schubert 	if ((match = (*match_fn)(tok, item, login_access_opts)) != 0)	/* YES */
1307c4c6e58SGuido van Rooij 	    break;
1317c4c6e58SGuido van Rooij     }
1327c4c6e58SGuido van Rooij     /* Process exceptions to matches. */
1337c4c6e58SGuido van Rooij 
1347c4c6e58SGuido van Rooij     if (match != NO) {
135e8c4b9d4SCy Schubert 	while ((tok = strtok((char *) 0, listsep)) && strcmp(tok, "EXCEPT")) {
1367c4c6e58SGuido van Rooij 	     /* VOID */ ;
1377c4c6e58SGuido van Rooij 	}
138*6ab923cbSMark Johnston 	if (tok == NULL ||
139*6ab923cbSMark Johnston 	    list_match((char *) 0, item, match_fn, login_access_opts) == NO) {
140*6ab923cbSMark Johnston 		return (match);
141e8c4b9d4SCy Schubert 	}
142e8c4b9d4SCy Schubert     }
1437c4c6e58SGuido van Rooij     return (NO);
1447c4c6e58SGuido van Rooij }
1457c4c6e58SGuido van Rooij 
1467c4c6e58SGuido van Rooij /* netgroup_match - match group against machine or user */
1477c4c6e58SGuido van Rooij 
148ac569969SMark Murray static int
netgroup_match(const char * group,const char * machine,const char * user)149ca2ddac3SDag-Erling Smørgrav netgroup_match(const char *group, const char *machine, const char *user)
1507c4c6e58SGuido van Rooij {
151ca2ddac3SDag-Erling Smørgrav     char domain[1024];
152ca2ddac3SDag-Erling Smørgrav     unsigned int i;
153ca2ddac3SDag-Erling Smørgrav 
154ca2ddac3SDag-Erling Smørgrav     if (getdomainname(domain, sizeof(domain)) != 0 || *domain == '\0') {
155ca2ddac3SDag-Erling Smørgrav 	syslog(LOG_ERR, "NIS netgroup support disabled: no NIS domain");
156ca2ddac3SDag-Erling Smørgrav 	return (NO);
157ca2ddac3SDag-Erling Smørgrav     }
158ca2ddac3SDag-Erling Smørgrav 
159ca2ddac3SDag-Erling Smørgrav     /* getdomainname() does not reliably terminate the string */
160ca2ddac3SDag-Erling Smørgrav     for (i = 0; i < sizeof(domain); ++i)
161ca2ddac3SDag-Erling Smørgrav 	if (domain[i] == '\0')
162ca2ddac3SDag-Erling Smørgrav 	    break;
163ca2ddac3SDag-Erling Smørgrav     if (i == sizeof(domain)) {
164ca2ddac3SDag-Erling Smørgrav 	syslog(LOG_ERR, "NIS netgroup support disabled: invalid NIS domain");
165ca2ddac3SDag-Erling Smørgrav 	return (NO);
166ca2ddac3SDag-Erling Smørgrav     }
167ca2ddac3SDag-Erling Smørgrav 
168ca2ddac3SDag-Erling Smørgrav     if (innetgr(group, machine, user, domain) == 1)
169ca2ddac3SDag-Erling Smørgrav 	return (YES);
170ca2ddac3SDag-Erling Smørgrav     return (NO);
1717c4c6e58SGuido van Rooij }
1727c4c6e58SGuido van Rooij 
173e8c4b9d4SCy Schubert /* group_match - match a group against one token */
1747c4c6e58SGuido van Rooij 
175e8c4b9d4SCy Schubert int
group_match(const char * tok,const char * username)176e8c4b9d4SCy Schubert group_match(const char *tok, const char *username)
1777c4c6e58SGuido van Rooij {
1787c4c6e58SGuido van Rooij     struct group *group;
1790d960f73SCy Schubert     struct passwd *passwd;
1800d960f73SCy Schubert     gid_t *grouplist;
181e8c4b9d4SCy Schubert     int i, ret, ngroups = NGROUPS;
182e8c4b9d4SCy Schubert 
183e8c4b9d4SCy Schubert     if ((passwd = getpwnam(username)) == NULL)
184e8c4b9d4SCy Schubert 	return (NO);
185e8c4b9d4SCy Schubert     errno = 0;
186e8c4b9d4SCy Schubert     if ((group = getgrnam(tok)) == NULL) {
187e8c4b9d4SCy Schubert 	if (errno != 0)
188e8c4b9d4SCy Schubert 	    syslog(LOG_ERR, "getgrnam() failed for %s: %s", username, strerror(errno));
189e8c4b9d4SCy Schubert 	else
190e8c4b9d4SCy Schubert 	    syslog(LOG_NOTICE, "group not found: %s", username);
191e8c4b9d4SCy Schubert 	return (NO);
192e8c4b9d4SCy Schubert     }
193e8c4b9d4SCy Schubert     if ((grouplist = calloc(ngroups, sizeof(gid_t))) == NULL) {
194e8c4b9d4SCy Schubert 	syslog(LOG_ERR, "cannot allocate memory for grouplist: %s", username);
195e8c4b9d4SCy Schubert 	return (NO);
196e8c4b9d4SCy Schubert     }
197e8c4b9d4SCy Schubert     ret = NO;
198e8c4b9d4SCy Schubert     if (getgrouplist(username, passwd->pw_gid, grouplist, &ngroups) != 0)
199e8c4b9d4SCy Schubert 	syslog(LOG_ERR, "getgrouplist() failed for %s", username);
200e8c4b9d4SCy Schubert     for (i = 0; i < ngroups; i++)
201e8c4b9d4SCy Schubert 	if (grouplist[i] == group->gr_gid)
202e8c4b9d4SCy Schubert 	    ret = YES;
203e8c4b9d4SCy Schubert     free(grouplist);
204e8c4b9d4SCy Schubert     return (ret);
205e8c4b9d4SCy Schubert }
206e8c4b9d4SCy Schubert 
207e8c4b9d4SCy Schubert /* user_match - match a username against one token */
208e8c4b9d4SCy Schubert 
209e8c4b9d4SCy Schubert static int
user_match(const char * tok,const char * string,struct pam_login_access_options * login_access_opts)210e8c4b9d4SCy Schubert user_match(const char *tok, const char *string,
211e8c4b9d4SCy Schubert 	struct pam_login_access_options *login_access_opts)
212e8c4b9d4SCy Schubert {
213e8c4b9d4SCy Schubert     size_t stringlen;
214e8c4b9d4SCy Schubert     char *grpstr;
215e8c4b9d4SCy Schubert     int rc;
2167c4c6e58SGuido van Rooij 
2177c4c6e58SGuido van Rooij     /*
2187c4c6e58SGuido van Rooij      * If a token has the magic value "ALL" the match always succeeds.
2197c4c6e58SGuido van Rooij      * Otherwise, return YES if the token fully matches the username, or if
2207c4c6e58SGuido van Rooij      * the token is a group that contains the username.
2217c4c6e58SGuido van Rooij      */
2227c4c6e58SGuido van Rooij 
2237c4c6e58SGuido van Rooij     if (tok[0] == '@') {			/* netgroup */
2247c4c6e58SGuido van Rooij 	return (netgroup_match(tok + 1, (char *) 0, string));
225e8c4b9d4SCy Schubert     } else if (tok[0] == '(' && tok[(stringlen = strlen(&tok[1]))] == ')') {		/* group */
226e8c4b9d4SCy Schubert 	if ((grpstr = strndup(&tok[1], stringlen - 1)) == NULL) {
227e8c4b9d4SCy Schubert 	    syslog(LOG_ERR, "cannot allocate memory for %s", string);
228e8c4b9d4SCy Schubert 	    return (NO);
229e8c4b9d4SCy Schubert 	}
230e8c4b9d4SCy Schubert 	rc = group_match(grpstr, string);
231e8c4b9d4SCy Schubert 	free(grpstr);
232e8c4b9d4SCy Schubert 	return (rc);
2337c4c6e58SGuido van Rooij     } else if (string_match(tok, string)) {	/* ALL or exact match */
2347c4c6e58SGuido van Rooij 	return (YES);
235e8c4b9d4SCy Schubert     } else if (login_access_opts->defgroup == true) {/* try group membership */
236e8c4b9d4SCy Schubert 	return (group_match(tok, string));
2370d960f73SCy Schubert     }
2387c4c6e58SGuido van Rooij     return (NO);
2397c4c6e58SGuido van Rooij }
2407c4c6e58SGuido van Rooij 
2417c4c6e58SGuido van Rooij /* from_match - match a host or tty against a list of tokens */
2427c4c6e58SGuido van Rooij 
243ac569969SMark Murray static int
from_match(const char * tok,const char * string,struct pam_login_access_options * login_access_opts __unused)244e8c4b9d4SCy Schubert from_match(const char *tok, const char *string,
245e8c4b9d4SCy Schubert 	struct pam_login_access_options *login_access_opts __unused)
2467c4c6e58SGuido van Rooij {
2477c4c6e58SGuido van Rooij     int     tok_len;
2487c4c6e58SGuido van Rooij     int     str_len;
2497c4c6e58SGuido van Rooij 
2507c4c6e58SGuido van Rooij     /*
2517c4c6e58SGuido van Rooij      * If a token has the magic value "ALL" the match always succeeds. Return
2527c4c6e58SGuido van Rooij      * YES if the token fully matches the string. If the token is a domain
2537c4c6e58SGuido van Rooij      * name, return YES if it matches the last fields of the string. If the
2547c4c6e58SGuido van Rooij      * token has the magic value "LOCAL", return YES if the string does not
2557c4c6e58SGuido van Rooij      * contain a "." character. If the token is a network number, return YES
2567c4c6e58SGuido van Rooij      * if it matches the head of the string.
2577c4c6e58SGuido van Rooij      */
2587c4c6e58SGuido van Rooij 
2597c4c6e58SGuido van Rooij     if (tok[0] == '@') {			/* netgroup */
2607c4c6e58SGuido van Rooij 	return (netgroup_match(tok + 1, string, (char *) 0));
2617c4c6e58SGuido van Rooij     } else if (string_match(tok, string)) {	/* ALL or exact match */
2627c4c6e58SGuido van Rooij 	return (YES);
2637c4c6e58SGuido van Rooij     } else if (tok[0] == '.') {			/* domain: match last fields */
2647c4c6e58SGuido van Rooij 	if ((str_len = strlen(string)) > (tok_len = strlen(tok))
2657c4c6e58SGuido van Rooij 	    && strcasecmp(tok, string + str_len - tok_len) == 0)
2667c4c6e58SGuido van Rooij 	    return (YES);
267f47effabSCy Schubert     } else if (strcmp(tok, "LOCAL") == 0) {	/* local: no dots */
2686f1ed8aeSCy Schubert 	if (strchr(string, '.') == NULL)
2697c4c6e58SGuido van Rooij 	    return (YES);
2707c4c6e58SGuido van Rooij     } else if (tok[(tok_len = strlen(tok)) - 1] == '.'	/* network */
2717c4c6e58SGuido van Rooij 	       && strncmp(tok, string, tok_len) == 0) {
2727c4c6e58SGuido van Rooij 	return (YES);
2737c4c6e58SGuido van Rooij     }
2747c4c6e58SGuido van Rooij     return (NO);
2757c4c6e58SGuido van Rooij }
2767c4c6e58SGuido van Rooij 
2777c4c6e58SGuido van Rooij /* string_match - match a string against one token */
2787c4c6e58SGuido van Rooij 
279ac569969SMark Murray static int
string_match(const char * tok,const char * string)280ac569969SMark Murray string_match(const char *tok, const char *string)
2817c4c6e58SGuido van Rooij {
2827c4c6e58SGuido van Rooij 
2837c4c6e58SGuido van Rooij     /*
2847c4c6e58SGuido van Rooij      * If the token has the magic value "ALL" the match always succeeds.
2857c4c6e58SGuido van Rooij      * Otherwise, return YES if the token fully matches the string.
2867c4c6e58SGuido van Rooij      */
2877c4c6e58SGuido van Rooij 
288f47effabSCy Schubert     if (strcmp(tok, "ALL") == 0) {		/* all: always matches */
2897c4c6e58SGuido van Rooij 	return (YES);
2907c4c6e58SGuido van Rooij     } else if (strcasecmp(tok, string) == 0) {	/* try exact match */
2917c4c6e58SGuido van Rooij 	return (YES);
2927c4c6e58SGuido van Rooij     }
2937c4c6e58SGuido van Rooij     return (NO);
2947c4c6e58SGuido van Rooij }
295