Hold serializer of the correct interface when calling if_output
[dragonfly.git] / usr.bin / login / login_access.c
blob575f551ac850391a73301290f3d993406198d20d
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).
7 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
9 * $DragonFly: src/usr.bin/login/login_access.c,v 1.3 2004/06/19 17:28:28 cpressey Exp $
12 #ifdef LOGIN_ACCESS
13 #ifndef lint
14 static const char sccsid[] = "%Z% %M% %I% %E% %U%";
15 #endif
17 #include <stdio.h>
18 #include <syslog.h>
19 #include <ctype.h>
20 #include <sys/types.h>
21 #include <grp.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <stdlib.h>
27 #include "pathnames.h"
29 /* Delimiters for fields and for lists of users, ttys or hosts. */
31 static char fs[] = ":"; /* field separator */
32 static char sep[] = ", \t"; /* list-element separator */
34 /* Constants to be used in assignments only, not in comparisons... */
36 #define YES 1
37 #define NO 0
39 static int list_match();
40 static int user_match();
41 static int from_match();
42 static int string_match();
44 /* login_access - match username/group and host/tty with access control file */
46 int
47 login_access(char *user, char *from)
49 FILE *fp;
50 char line[BUFSIZ];
51 char *perm; /* becomes permission field */
52 char *users; /* becomes list of login names */
53 char *froms; /* becomes list of terminals or hosts */
54 int match = NO;
55 int end;
56 int lineno = 0; /* for diagnostics */
59 * Process the table one line at a time and stop at the first match.
60 * Blank lines and lines that begin with a '#' character are ignored.
61 * Non-comment lines are broken at the ':' character. All fields are
62 * mandatory. The first field should be a "+" or "-" character. A
63 * non-existing table means no access control.
66 if ((fp = fopen(_PATH_LOGACCESS, "r")) != NULL) {
67 while (!match && fgets(line, sizeof(line), fp)) {
68 lineno++;
69 if (line[end = strlen(line) - 1] != '\n') {
70 syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
71 _PATH_LOGACCESS, lineno);
72 continue;
74 if (line[0] == '#')
75 continue; /* comment line */
76 while (end > 0 && isspace(line[end - 1]))
77 end--;
78 line[end] = 0; /* strip trailing whitespace */
79 if (line[0] == 0) /* skip blank lines */
80 continue;
81 if (!(perm = strtok(line, fs))
82 || !(users = strtok((char *) 0, fs))
83 || !(froms = strtok((char *) 0, fs))
84 || strtok((char *) 0, fs)) {
85 syslog(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS,
86 lineno);
87 continue;
89 if (perm[0] != '+' && perm[0] != '-') {
90 syslog(LOG_ERR, "%s: line %d: bad first field", _PATH_LOGACCESS,
91 lineno);
92 continue;
94 match = (list_match(froms, from, from_match)
95 && list_match(users, user, user_match));
97 (void) fclose(fp);
98 } else if (errno != ENOENT) {
99 syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS);
101 return (match == 0 || (line[0] == '+'));
104 /* list_match - match an item against a list of tokens with exceptions */
106 static int list_match(char *list, char *item, int (*match_fn)())
108 char *tok;
109 int match = NO;
112 * Process tokens one at a time. We have exhausted all possible matches
113 * when we reach an "EXCEPT" token or the end of the list. If we do find
114 * a match, look for an "EXCEPT" list and recurse to determine whether
115 * the match is affected by any exceptions.
118 for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
119 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
120 break;
121 if ((match = (*match_fn)(tok, item)) != NULL) /* YES */
122 break;
124 /* Process exceptions to matches. */
126 if (match != NO) {
127 while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
128 /* VOID */ ;
129 if (tok == 0 || list_match((char *) 0, item, match_fn) == NO)
130 return (match);
132 return (NO);
135 /* netgroup_match - match group against machine or user */
137 static int netgroup_match(char *group, char *machine, char *user)
139 #ifdef NIS
140 static char *mydomain = 0;
142 if (mydomain == 0)
143 yp_get_default_domain(&mydomain);
144 return (innetgr(group, machine, user, mydomain));
145 #else
146 syslog(LOG_ERR, "NIS netgroup support not configured");
147 return 0;
148 #endif
151 /* user_match - match a username against one token */
153 static int user_match(char *tok, char *string)
155 struct group *group;
156 int i;
159 * If a token has the magic value "ALL" the match always succeeds.
160 * Otherwise, return YES if the token fully matches the username, or if
161 * the token is a group that contains the username.
164 if (tok[0] == '@') { /* netgroup */
165 return (netgroup_match(tok + 1, (char *) 0, string));
166 } else if (string_match(tok, string)) { /* ALL or exact match */
167 return (YES);
168 } else if ((group = getgrnam(tok)) != NULL) {/* try group membership */
169 for (i = 0; group->gr_mem[i]; i++)
170 if (strcasecmp(string, group->gr_mem[i]) == 0)
171 return (YES);
173 return (NO);
176 /* from_match - match a host or tty against a list of tokens */
178 static int from_match(char *tok, char *string)
180 int tok_len;
181 int str_len;
184 * If a token has the magic value "ALL" the match always succeeds. Return
185 * YES if the token fully matches the string. If the token is a domain
186 * name, return YES if it matches the last fields of the string. If the
187 * token has the magic value "LOCAL", return YES if the string does not
188 * contain a "." character. If the token is a network number, return YES
189 * if it matches the head of the string.
192 if (tok[0] == '@') { /* netgroup */
193 return (netgroup_match(tok + 1, string, (char *) 0));
194 } else if (string_match(tok, string)) { /* ALL or exact match */
195 return (YES);
196 } else if (tok[0] == '.') { /* domain: match last fields */
197 if ((str_len = strlen(string)) > (tok_len = strlen(tok))
198 && strcasecmp(tok, string + str_len - tok_len) == 0)
199 return (YES);
200 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
201 if (strchr(string, '.') == 0)
202 return (YES);
203 } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */
204 && strncmp(tok, string, tok_len) == 0) {
205 return (YES);
207 return (NO);
210 /* string_match - match a string against one token */
212 static int string_match(char *tok, char *string)
216 * If the token has the magic value "ALL" the match always succeeds.
217 * Otherwise, return YES if the token fully matches the string.
220 if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */
221 return (YES);
222 } else if (strcasecmp(tok, string) == 0) { /* try exact match */
223 return (YES);
225 return (NO);
227 #endif /* LOGIN_ACCES */