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