login_access.c revision 1.6 1 1.6 christos /* $NetBSD: login_access.c,v 1.6 2012/01/03 19:02:55 christos Exp $ */
2 1.2 christos
3 1.2 christos /*
4 1.2 christos * This module implements a simple but effective form of login access
5 1.2 christos * control based on login names and on host (or domain) names, internet
6 1.2 christos * addresses (or network numbers), or on terminal line names in case of
7 1.2 christos * non-networked logins. Diagnostics are reported through syslog(3).
8 1.2 christos *
9 1.2 christos * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
10 1.2 christos */
11 1.1 christos
12 1.1 christos #if 0
13 1.1 christos #ifndef lint
14 1.1 christos static char sccsid[] = "%Z% %M% %I% %E% %U%";
15 1.1 christos #endif
16 1.1 christos #endif
17 1.1 christos
18 1.1 christos #include <sys/cdefs.h>
19 1.2 christos #ifdef __FreeBSD__
20 1.1 christos __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_login_access/login_access.c,v 1.12 2004/03/05 08:10:18 markm Exp $");
21 1.2 christos #else
22 1.6 christos __RCSID("$NetBSD: login_access.c,v 1.6 2012/01/03 19:02:55 christos Exp $");
23 1.2 christos #endif
24 1.1 christos
25 1.1 christos #include <sys/types.h>
26 1.1 christos #include <ctype.h>
27 1.1 christos #include <errno.h>
28 1.1 christos #include <grp.h>
29 1.1 christos #include <stdio.h>
30 1.1 christos #include <stdlib.h>
31 1.1 christos #include <string.h>
32 1.1 christos #include <syslog.h>
33 1.1 christos #include <unistd.h>
34 1.4 christos #include <stdarg.h>
35 1.1 christos
36 1.1 christos #include "pam_login_access.h"
37 1.1 christos
38 1.1 christos #define _PATH_LOGACCESS "/etc/login.access"
39 1.1 christos
40 1.1 christos /* Delimiters for fields and for lists of users, ttys or hosts. */
41 1.1 christos
42 1.1 christos static char fs[] = ":"; /* field separator */
43 1.1 christos static char sep[] = ", \t"; /* list-element separator */
44 1.1 christos
45 1.1 christos /* Constants to be used in assignments only, not in comparisons... */
46 1.1 christos
47 1.1 christos #define YES 1
48 1.1 christos #define NO 0
49 1.1 christos
50 1.1 christos static int from_match(const char *, const char *);
51 1.1 christos static int list_match(char *, const char *,
52 1.1 christos int (*)(const char *, const char *));
53 1.1 christos static int netgroup_match(const char *, const char *, const char *);
54 1.1 christos static int string_match(const char *, const char *);
55 1.1 christos static int user_match(const char *, const char *);
56 1.1 christos
57 1.1 christos /* login_access - match username/group and host/tty with access control file */
58 1.1 christos
59 1.4 christos static void
60 1.4 christos logit(int level, const char *fmt, ...)
61 1.4 christos {
62 1.4 christos va_list ap;
63 1.5 christos struct syslog_data data = SYSLOG_DATA_INIT;
64 1.4 christos
65 1.4 christos openlog_r("pam_login_access", LOG_PID, LOG_AUTHPRIV, &data);
66 1.4 christos va_start(ap, fmt);
67 1.4 christos vsyslog_r(level, &data, fmt, ap);
68 1.4 christos va_end(ap);
69 1.4 christos closelog_r(&data);
70 1.4 christos }
71 1.4 christos
72 1.1 christos int
73 1.1 christos login_access(const char *user, const char *from)
74 1.1 christos {
75 1.1 christos FILE *fp;
76 1.1 christos char line[BUFSIZ];
77 1.1 christos char *perm; /* becomes permission field */
78 1.1 christos char *users; /* becomes list of login names */
79 1.1 christos char *froms; /* becomes list of terminals or hosts */
80 1.1 christos int match = NO;
81 1.1 christos int end;
82 1.1 christos int lineno = 0; /* for diagnostics */
83 1.1 christos
84 1.1 christos /*
85 1.1 christos * Process the table one line at a time and stop at the first match.
86 1.1 christos * Blank lines and lines that begin with a '#' character are ignored.
87 1.1 christos * Non-comment lines are broken at the ':' character. All fields are
88 1.1 christos * mandatory. The first field should be a "+" or "-" character. A
89 1.1 christos * non-existing table means no access control.
90 1.1 christos */
91 1.1 christos
92 1.1 christos if ((fp = fopen(_PATH_LOGACCESS, "r")) != NULL) {
93 1.1 christos while (!match && fgets(line, sizeof(line), fp)) {
94 1.1 christos lineno++;
95 1.1 christos if (line[end = strlen(line) - 1] != '\n') {
96 1.4 christos logit(LOG_ERR, "%s: line %d: missing newline or line too long",
97 1.1 christos _PATH_LOGACCESS, lineno);
98 1.1 christos continue;
99 1.1 christos }
100 1.1 christos if (line[0] == '#')
101 1.1 christos continue; /* comment line */
102 1.2 christos while (end > 0 && isspace((unsigned char)line[end - 1]))
103 1.1 christos end--;
104 1.1 christos line[end] = 0; /* strip trailing whitespace */
105 1.1 christos if (line[0] == 0) /* skip blank lines */
106 1.1 christos continue;
107 1.1 christos if (!(perm = strtok(line, fs))
108 1.1 christos || !(users = strtok((char *) 0, fs))
109 1.1 christos || !(froms = strtok((char *) 0, fs))
110 1.1 christos || strtok((char *) 0, fs)) {
111 1.4 christos logit(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS,
112 1.1 christos lineno);
113 1.1 christos continue;
114 1.1 christos }
115 1.1 christos if (perm[0] != '+' && perm[0] != '-') {
116 1.4 christos logit(LOG_ERR, "%s: line %d: bad first field", _PATH_LOGACCESS,
117 1.1 christos lineno);
118 1.1 christos continue;
119 1.1 christos }
120 1.1 christos match = (list_match(froms, from, from_match)
121 1.1 christos && list_match(users, user, user_match));
122 1.1 christos }
123 1.1 christos (void) fclose(fp);
124 1.1 christos } else if (errno != ENOENT) {
125 1.6 christos logit(LOG_ERR, "cannot open %s: %s", _PATH_LOGACCESS, strerror(errno));
126 1.1 christos }
127 1.1 christos return (match == 0 || (line[0] == '+'));
128 1.1 christos }
129 1.1 christos
130 1.1 christos /* list_match - match an item against a list of tokens with exceptions */
131 1.1 christos
132 1.1 christos static int
133 1.1 christos list_match(char *list, const char *item,
134 1.1 christos int (*match_fn)(const char *, const char *))
135 1.1 christos {
136 1.1 christos char *tok;
137 1.1 christos int match = NO;
138 1.1 christos
139 1.1 christos /*
140 1.1 christos * Process tokens one at a time. We have exhausted all possible matches
141 1.1 christos * when we reach an "EXCEPT" token or the end of the list. If we do find
142 1.1 christos * a match, look for an "EXCEPT" list and recurse to determine whether
143 1.1 christos * the match is affected by any exceptions.
144 1.1 christos */
145 1.1 christos
146 1.1 christos for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
147 1.1 christos if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
148 1.1 christos break;
149 1.1 christos if ((match = (*match_fn)(tok, item)) != 0) /* YES */
150 1.1 christos break;
151 1.1 christos }
152 1.1 christos /* Process exceptions to matches. */
153 1.1 christos
154 1.1 christos if (match != NO) {
155 1.1 christos while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
156 1.1 christos /* VOID */ ;
157 1.1 christos if (tok == 0 || list_match((char *) 0, item, match_fn) == NO)
158 1.1 christos return (match);
159 1.1 christos }
160 1.1 christos return (NO);
161 1.1 christos }
162 1.1 christos
163 1.1 christos /* netgroup_match - match group against machine or user */
164 1.1 christos
165 1.1 christos static int
166 1.1 christos netgroup_match(const char *group __unused,
167 1.1 christos const char *machine __unused, const char *user __unused)
168 1.1 christos {
169 1.4 christos logit(LOG_ERR, "NIS netgroup support not configured");
170 1.1 christos return 0;
171 1.1 christos }
172 1.1 christos
173 1.1 christos /* user_match - match a username against one token */
174 1.1 christos
175 1.1 christos static int
176 1.1 christos user_match(const char *tok, const char *string)
177 1.1 christos {
178 1.3 christos struct group grres, *group;
179 1.3 christos char grbuf[1024];
180 1.1 christos int i;
181 1.1 christos
182 1.1 christos /*
183 1.1 christos * If a token has the magic value "ALL" the match always succeeds.
184 1.1 christos * Otherwise, return YES if the token fully matches the username, or if
185 1.1 christos * the token is a group that contains the username.
186 1.1 christos */
187 1.1 christos
188 1.1 christos if (tok[0] == '@') { /* netgroup */
189 1.1 christos return (netgroup_match(tok + 1, (char *) 0, string));
190 1.1 christos } else if (string_match(tok, string)) { /* ALL or exact match */
191 1.1 christos return (YES);
192 1.3 christos } else if (getgrnam_r(tok, &grres, grbuf, sizeof(grbuf), &group) == 0 &&
193 1.3 christos group != NULL) {/* try group membership */
194 1.1 christos for (i = 0; group->gr_mem[i]; i++)
195 1.1 christos if (strcasecmp(string, group->gr_mem[i]) == 0)
196 1.1 christos return (YES);
197 1.1 christos }
198 1.1 christos return (NO);
199 1.1 christos }
200 1.1 christos
201 1.1 christos /* from_match - match a host or tty against a list of tokens */
202 1.1 christos
203 1.1 christos static int
204 1.1 christos from_match(const char *tok, const char *string)
205 1.1 christos {
206 1.1 christos int tok_len;
207 1.1 christos int str_len;
208 1.1 christos
209 1.1 christos /*
210 1.1 christos * If a token has the magic value "ALL" the match always succeeds. Return
211 1.1 christos * YES if the token fully matches the string. If the token is a domain
212 1.1 christos * name, return YES if it matches the last fields of the string. If the
213 1.1 christos * token has the magic value "LOCAL", return YES if the string does not
214 1.1 christos * contain a "." character. If the token is a network number, return YES
215 1.1 christos * if it matches the head of the string.
216 1.1 christos */
217 1.1 christos
218 1.1 christos if (tok[0] == '@') { /* netgroup */
219 1.1 christos return (netgroup_match(tok + 1, string, (char *) 0));
220 1.1 christos } else if (string_match(tok, string)) { /* ALL or exact match */
221 1.1 christos return (YES);
222 1.1 christos } else if (tok[0] == '.') { /* domain: match last fields */
223 1.1 christos if ((str_len = strlen(string)) > (tok_len = strlen(tok))
224 1.1 christos && strcasecmp(tok, string + str_len - tok_len) == 0)
225 1.1 christos return (YES);
226 1.1 christos } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
227 1.1 christos if (strchr(string, '.') == 0)
228 1.1 christos return (YES);
229 1.1 christos } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */
230 1.1 christos && strncmp(tok, string, tok_len) == 0) {
231 1.1 christos return (YES);
232 1.1 christos }
233 1.1 christos return (NO);
234 1.1 christos }
235 1.1 christos
236 1.1 christos /* string_match - match a string against one token */
237 1.1 christos
238 1.1 christos static int
239 1.1 christos string_match(const char *tok, const char *string)
240 1.1 christos {
241 1.1 christos
242 1.1 christos /*
243 1.1 christos * If the token has the magic value "ALL" the match always succeeds.
244 1.1 christos * Otherwise, return YES if the token fully matches the string.
245 1.1 christos */
246 1.1 christos
247 1.1 christos if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */
248 1.1 christos return (YES);
249 1.1 christos } else if (strcasecmp(tok, string) == 0) { /* try exact match */
250 1.1 christos return (YES);
251 1.1 christos }
252 1.1 christos return (NO);
253 1.1 christos }
254