1 /************************************************************************
2 * Copyright 1995 by Wietse Venema. All rights reserved. Some individual
3 * files may be covered by other copyrights.
5 * This material was originally written and compiled by Wietse Venema at
6 * Eindhoven University of Technology, The Netherlands, in 1990, 1991,
7 * 1992, 1993, 1994 and 1995.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that this entire copyright notice
11 * is duplicated in all such copies.
13 * This software is provided "as is" and without any expressed or implied
14 * warranties, including, without limitation, the implied warranties of
15 * merchantibility and fitness for any particular purpose.
16 ************************************************************************/
18 * This module implements a simple but effective form of login access
19 * control based on login names and on host (or domain) names, internet
20 * addresses (or network numbers), or on terminal line names in case of
21 * non-networked logins. Diagnostics are reported through syslog(3).
23 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
26 #include "login_locl.h"
28 __RCSID("$Heimdal: login_access.c 10020 2001-06-04 14:10:19Z assar $"
31 /* Delimiters for fields and for lists of users, ttys or hosts. */
33 static char fs
[] = ":"; /* field separator */
34 static char sep
[] = ", \t"; /* list-element separator */
36 /* Constants to be used in assignments only, not in comparisons... */
42 * A structure to bundle up all login-related information to keep the
43 * functional interfaces as generic as possible.
50 static int list_match(char *list
, struct login_info
*item
,
51 int (*match_fn
)(char *, struct login_info
*));
52 static int user_match(char *tok
, struct login_info
*item
);
53 static int from_match(char *tok
, struct login_info
*item
);
54 static int string_match(char *tok
, char *string
);
56 /* login_access - match username/group and host/tty with access control file */
58 int login_access(struct passwd
*user
, char *from
)
60 struct login_info item
;
63 char *perm
; /* becomes permission field */
64 char *users
; /* becomes list of login names */
65 char *froms
; /* becomes list of terminals or hosts */
68 int lineno
= 0; /* for diagnostics */
72 * Bundle up the arguments to avoid unnecessary clumsiness lateron.
78 * Process the table one line at a time and stop at the first match.
79 * Blank lines and lines that begin with a '#' character are ignored.
80 * Non-comment lines are broken at the ':' character. All fields are
81 * mandatory. The first field should be a "+" or "-" character. A
82 * non-existing table means no access control.
85 if ((fp
= fopen(_PATH_LOGACCESS
, "r")) != 0) {
86 while (!match
&& fgets(line
, sizeof(line
), fp
)) {
88 if (line
[end
= strlen(line
) - 1] != '\n') {
89 syslog(LOG_ERR
, "%s: line %d: missing newline or line too long",
90 _PATH_LOGACCESS
, lineno
);
94 continue; /* comment line */
95 while (end
> 0 && isspace((unsigned char)line
[end
- 1]))
97 line
[end
] = 0; /* strip trailing whitespace */
98 if (line
[0] == 0) /* skip blank lines */
101 if (!(perm
= strtok_r(line
, fs
, &foo
))
102 || !(users
= strtok_r(NULL
, fs
, &foo
))
103 || !(froms
= strtok_r(NULL
, fs
, &foo
))
104 || strtok_r(NULL
, fs
, &foo
)) {
105 syslog(LOG_ERR
, "%s: line %d: bad field count",
110 if (perm
[0] != '+' && perm
[0] != '-') {
111 syslog(LOG_ERR
, "%s: line %d: bad first field",
116 match
= (list_match(froms
, &item
, from_match
)
117 && list_match(users
, &item
, user_match
));
120 } else if (errno
!= ENOENT
) {
121 syslog(LOG_ERR
, "cannot open %s: %m", _PATH_LOGACCESS
);
123 return (match
== 0 || (line
[0] == '+'));
126 /* list_match - match an item against a list of tokens with exceptions */
129 list_match(char *list
,
130 struct login_info
*item
,
131 int (*match_fn
)(char *, struct login_info
*))
138 * Process tokens one at a time. We have exhausted all possible matches
139 * when we reach an "EXCEPT" token or the end of the list. If we do find
140 * a match, look for an "EXCEPT" list and recurse to determine whether
141 * the match is affected by any exceptions.
144 for (tok
= strtok_r(list
, sep
, &foo
);
146 tok
= strtok_r(NULL
, sep
, &foo
)) {
147 if (strcasecmp(tok
, "EXCEPT") == 0) /* EXCEPT: give up */
149 if ((match
= (*match_fn
) (tok
, item
)) != 0) /* YES */
152 /* Process exceptions to matches. */
155 while ((tok
= strtok_r(NULL
, sep
, &foo
)) && strcasecmp(tok
, "EXCEPT"))
157 if (tok
== 0 || list_match(NULL
, item
, match_fn
) == NO
)
163 /* myhostname - figure out local machine name */
165 static char *myhostname(void)
167 static char name
[MAXHOSTNAMELEN
+ 1] = "";
170 gethostname(name
, sizeof(name
));
171 name
[MAXHOSTNAMELEN
] = 0;
176 /* netgroup_match - match group against machine or user */
178 static int netgroup_match(char *group
, char *machine
, char *user
)
180 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
181 static char *mydomain
= 0;
184 yp_get_default_domain(&mydomain
);
185 return (innetgr(group
, machine
, user
, mydomain
));
187 syslog(LOG_ERR
, "NIS netgroup support not configured");
192 /* user_match - match a username against one token */
194 static int user_match(char *tok
, struct login_info
*item
)
196 char *string
= item
->user
->pw_name
;
197 struct login_info fake_item
;
203 * If a token has the magic value "ALL" the match always succeeds.
204 * Otherwise, return YES if the token fully matches the username, if the
205 * token is a group that contains the username, or if the token is the
206 * name of the user's primary group.
209 if ((at
= strchr(tok
+ 1, '@')) != 0) { /* split user@host pattern */
211 fake_item
.from
= myhostname();
212 return (user_match(tok
, item
) && from_match(at
+ 1, &fake_item
));
213 } else if (tok
[0] == '@') { /* netgroup */
214 return (netgroup_match(tok
+ 1, (char *) 0, string
));
215 } else if (string_match(tok
, string
)) { /* ALL or exact match */
217 } else if ((group
= getgrnam(tok
)) != 0) { /* try group membership */
218 if (item
->user
->pw_gid
== group
->gr_gid
)
220 for (i
= 0; group
->gr_mem
[i
]; i
++)
221 if (strcasecmp(string
, group
->gr_mem
[i
]) == 0)
227 /* from_match - match a host or tty against a list of tokens */
229 static int from_match(char *tok
, struct login_info
*item
)
231 char *string
= item
->from
;
236 * If a token has the magic value "ALL" the match always succeeds. Return
237 * YES if the token fully matches the string. If the token is a domain
238 * name, return YES if it matches the last fields of the string. If the
239 * token has the magic value "LOCAL", return YES if the string does not
240 * contain a "." character. If the token is a network number, return YES
241 * if it matches the head of the string.
244 if (tok
[0] == '@') { /* netgroup */
245 return (netgroup_match(tok
+ 1, string
, (char *) 0));
246 } else if (string_match(tok
, string
)) { /* ALL or exact match */
248 } else if (tok
[0] == '.') { /* domain: match last fields */
249 if ((str_len
= strlen(string
)) > (tok_len
= strlen(tok
))
250 && strcasecmp(tok
, string
+ str_len
- tok_len
) == 0)
252 } else if (strcasecmp(tok
, "LOCAL") == 0) { /* local: no dots */
253 if (strchr(string
, '.') == 0)
255 } else if (tok
[(tok_len
= strlen(tok
)) - 1] == '.' /* network */
256 && strncmp(tok
, string
, tok_len
) == 0) {
262 /* string_match - match a string against one token */
264 static int string_match(char *tok
, char *string
)
268 * If the token has the magic value "ALL" the match always succeeds.
269 * Otherwise, return YES if the token fully matches the string.
272 if (strcasecmp(tok
, "ALL") == 0) { /* all: always matches */
274 } else if (strcasecmp(tok
, string
) == 0) { /* try exact match */