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