xref: /freebsd/crypto/heimdal/appl/rsh/login_access.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1c19800e8SDoug Rabson /************************************************************************
2c19800e8SDoug Rabson * Copyright 1995 by Wietse Venema.  All rights reserved.  Some individual
3c19800e8SDoug Rabson * files may be covered by other copyrights.
4c19800e8SDoug Rabson *
5c19800e8SDoug Rabson * This material was originally written and compiled by Wietse Venema at
6c19800e8SDoug Rabson * Eindhoven University of Technology, The Netherlands, in 1990, 1991,
7c19800e8SDoug Rabson * 1992, 1993, 1994 and 1995.
8c19800e8SDoug Rabson *
9c19800e8SDoug Rabson * Redistribution and use in source and binary forms, with or without
10c19800e8SDoug Rabson * modification, are permitted provided that this entire copyright notice
11c19800e8SDoug Rabson * is duplicated in all such copies.
12c19800e8SDoug Rabson *
13c19800e8SDoug Rabson * This software is provided "as is" and without any expressed or implied
14c19800e8SDoug Rabson * warranties, including, without limitation, the implied warranties of
15c19800e8SDoug Rabson * merchantibility and fitness for any particular purpose.
16c19800e8SDoug Rabson ************************************************************************/
17c19800e8SDoug Rabson  /*
18c19800e8SDoug Rabson   * This module implements a simple but effective form of login access
19c19800e8SDoug Rabson   * control based on login names and on host (or domain) names, internet
20c19800e8SDoug Rabson   * addresses (or network numbers), or on terminal line names in case of
21c19800e8SDoug Rabson   * non-networked logins. Diagnostics are reported through syslog(3).
22c19800e8SDoug Rabson   *
23c19800e8SDoug Rabson   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
24c19800e8SDoug Rabson   */
25c19800e8SDoug Rabson 
26c19800e8SDoug Rabson #include "login_locl.h"
27c19800e8SDoug Rabson 
28*ae771770SStanislav Sedov RCSID("$Id$");
29c19800e8SDoug Rabson 
30c19800e8SDoug Rabson  /* Delimiters for fields and for lists of users, ttys or hosts. */
31c19800e8SDoug Rabson 
32c19800e8SDoug Rabson static char fs[] = ":";			/* field separator */
33c19800e8SDoug Rabson static char sep[] = ", \t";		/* list-element separator */
34c19800e8SDoug Rabson 
35c19800e8SDoug Rabson  /* Constants to be used in assignments only, not in comparisons... */
36c19800e8SDoug Rabson 
37c19800e8SDoug Rabson #define YES             1
38c19800e8SDoug Rabson #define NO              0
39c19800e8SDoug Rabson 
40c19800e8SDoug Rabson  /*
41c19800e8SDoug Rabson   * A structure to bundle up all login-related information to keep the
42c19800e8SDoug Rabson   * functional interfaces as generic as possible.
43c19800e8SDoug Rabson   */
44c19800e8SDoug Rabson struct login_info {
45c19800e8SDoug Rabson     struct passwd *user;
46c19800e8SDoug Rabson     char   *from;
47c19800e8SDoug Rabson };
48c19800e8SDoug Rabson 
49c19800e8SDoug Rabson static int list_match(char *list, struct login_info *item,
50c19800e8SDoug Rabson 		      int (*match_fn)(char *, struct login_info *));
51c19800e8SDoug Rabson static int user_match(char *tok, struct login_info *item);
52c19800e8SDoug Rabson static int from_match(char *tok, struct login_info *item);
53c19800e8SDoug Rabson static int string_match(char *tok, char *string);
54c19800e8SDoug Rabson 
55c19800e8SDoug Rabson /* login_access - match username/group and host/tty with access control file */
56c19800e8SDoug Rabson 
login_access(struct passwd * user,char * from)57c19800e8SDoug Rabson int login_access(struct passwd *user, char *from)
58c19800e8SDoug Rabson {
59c19800e8SDoug Rabson     struct login_info item;
60c19800e8SDoug Rabson     FILE   *fp;
61c19800e8SDoug Rabson     char    line[BUFSIZ];
62c19800e8SDoug Rabson     char   *perm;			/* becomes permission field */
63c19800e8SDoug Rabson     char   *users;			/* becomes list of login names */
64c19800e8SDoug Rabson     char   *froms;			/* becomes list of terminals or hosts */
65c19800e8SDoug Rabson     int     match = NO;
66c19800e8SDoug Rabson     int     end;
67c19800e8SDoug Rabson     int     lineno = 0;			/* for diagnostics */
68c19800e8SDoug Rabson     char   *foo;
69c19800e8SDoug Rabson 
70c19800e8SDoug Rabson     /*
71c19800e8SDoug Rabson      * Bundle up the arguments to avoid unnecessary clumsiness lateron.
72c19800e8SDoug Rabson      */
73c19800e8SDoug Rabson     item.user = user;
74c19800e8SDoug Rabson     item.from = from;
75c19800e8SDoug Rabson 
76c19800e8SDoug Rabson     /*
77c19800e8SDoug Rabson      * Process the table one line at a time and stop at the first match.
78c19800e8SDoug Rabson      * Blank lines and lines that begin with a '#' character are ignored.
79c19800e8SDoug Rabson      * Non-comment lines are broken at the ':' character. All fields are
80c19800e8SDoug Rabson      * mandatory. The first field should be a "+" or "-" character. A
81c19800e8SDoug Rabson      * non-existing table means no access control.
82c19800e8SDoug Rabson      */
83c19800e8SDoug Rabson 
84c19800e8SDoug Rabson     if ((fp = fopen(_PATH_LOGACCESS, "r")) != 0) {
85c19800e8SDoug Rabson 	while (!match && fgets(line, sizeof(line), fp)) {
86c19800e8SDoug Rabson 	    lineno++;
87c19800e8SDoug Rabson 	    if (line[end = strlen(line) - 1] != '\n') {
88c19800e8SDoug Rabson 		syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
89c19800e8SDoug Rabson 		       _PATH_LOGACCESS, lineno);
90c19800e8SDoug Rabson 		continue;
91c19800e8SDoug Rabson 	    }
92c19800e8SDoug Rabson 	    if (line[0] == '#')
93c19800e8SDoug Rabson 		continue;			/* comment line */
94c19800e8SDoug Rabson 	    while (end > 0 && isspace((unsigned char)line[end - 1]))
95c19800e8SDoug Rabson 		end--;
96c19800e8SDoug Rabson 	    line[end] = 0;			/* strip trailing whitespace */
97c19800e8SDoug Rabson 	    if (line[0] == 0)			/* skip blank lines */
98c19800e8SDoug Rabson 		continue;
99c19800e8SDoug Rabson 	    foo = NULL;
100c19800e8SDoug Rabson 	    if (!(perm = strtok_r(line, fs, &foo))
101c19800e8SDoug Rabson 		|| !(users = strtok_r(NULL, fs, &foo))
102c19800e8SDoug Rabson 		|| !(froms = strtok_r(NULL, fs, &foo))
103c19800e8SDoug Rabson 		|| strtok_r(NULL, fs, &foo)) {
104c19800e8SDoug Rabson 		syslog(LOG_ERR, "%s: line %d: bad field count",
105c19800e8SDoug Rabson 		       _PATH_LOGACCESS,
106c19800e8SDoug Rabson 		       lineno);
107c19800e8SDoug Rabson 		continue;
108c19800e8SDoug Rabson 	    }
109c19800e8SDoug Rabson 	    if (perm[0] != '+' && perm[0] != '-') {
110c19800e8SDoug Rabson 		syslog(LOG_ERR, "%s: line %d: bad first field",
111c19800e8SDoug Rabson 		       _PATH_LOGACCESS,
112c19800e8SDoug Rabson 		       lineno);
113c19800e8SDoug Rabson 		continue;
114c19800e8SDoug Rabson 	    }
115c19800e8SDoug Rabson 	    match = (list_match(froms, &item, from_match)
116c19800e8SDoug Rabson 		     && list_match(users, &item, user_match));
117c19800e8SDoug Rabson 	}
118c19800e8SDoug Rabson 	fclose(fp);
119c19800e8SDoug Rabson     } else if (errno != ENOENT) {
120c19800e8SDoug Rabson 	syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS);
121c19800e8SDoug Rabson     }
122c19800e8SDoug Rabson     return (match == 0 || (line[0] == '+'));
123c19800e8SDoug Rabson }
124c19800e8SDoug Rabson 
125c19800e8SDoug Rabson /* list_match - match an item against a list of tokens with exceptions */
126c19800e8SDoug Rabson 
127c19800e8SDoug Rabson static int
list_match(char * list,struct login_info * item,int (* match_fn)(char *,struct login_info *))128c19800e8SDoug Rabson list_match(char *list,
129c19800e8SDoug Rabson 	   struct login_info *item,
130c19800e8SDoug Rabson 	   int (*match_fn)(char *, struct login_info *))
131c19800e8SDoug Rabson {
132c19800e8SDoug Rabson     char   *tok;
133c19800e8SDoug Rabson     int     match = NO;
134c19800e8SDoug Rabson     char   *foo = NULL;
135c19800e8SDoug Rabson 
136c19800e8SDoug Rabson     /*
137c19800e8SDoug Rabson      * Process tokens one at a time. We have exhausted all possible matches
138c19800e8SDoug Rabson      * when we reach an "EXCEPT" token or the end of the list. If we do find
139c19800e8SDoug Rabson      * a match, look for an "EXCEPT" list and recurse to determine whether
140c19800e8SDoug Rabson      * the match is affected by any exceptions.
141c19800e8SDoug Rabson      */
142c19800e8SDoug Rabson 
143c19800e8SDoug Rabson     for (tok = strtok_r(list, sep, &foo);
144c19800e8SDoug Rabson 	 tok != NULL;
145c19800e8SDoug Rabson 	 tok = strtok_r(NULL, sep, &foo)) {
146c19800e8SDoug Rabson 	if (strcasecmp(tok, "EXCEPT") == 0)	/* EXCEPT: give up */
147c19800e8SDoug Rabson 	    break;
148c19800e8SDoug Rabson 	if ((match = (*match_fn) (tok, item)) != 0)	/* YES */
149c19800e8SDoug Rabson 	    break;
150c19800e8SDoug Rabson     }
151c19800e8SDoug Rabson     /* Process exceptions to matches. */
152c19800e8SDoug Rabson 
153c19800e8SDoug Rabson     if (match != NO) {
154c19800e8SDoug Rabson 	while ((tok = strtok_r(NULL, sep, &foo)) && strcasecmp(tok, "EXCEPT"))
155c19800e8SDoug Rabson 	     /* VOID */ ;
156c19800e8SDoug Rabson 	if (tok == 0 || list_match(NULL, item, match_fn) == NO)
157c19800e8SDoug Rabson 	    return (match);
158c19800e8SDoug Rabson     }
159c19800e8SDoug Rabson     return (NO);
160c19800e8SDoug Rabson }
161c19800e8SDoug Rabson 
162c19800e8SDoug Rabson /* myhostname - figure out local machine name */
163c19800e8SDoug Rabson 
myhostname(void)164c19800e8SDoug Rabson static char *myhostname(void)
165c19800e8SDoug Rabson {
166c19800e8SDoug Rabson     static char name[MAXHOSTNAMELEN + 1] = "";
167c19800e8SDoug Rabson 
168c19800e8SDoug Rabson     if (name[0] == 0) {
169c19800e8SDoug Rabson 	gethostname(name, sizeof(name));
170c19800e8SDoug Rabson 	name[MAXHOSTNAMELEN] = 0;
171c19800e8SDoug Rabson     }
172c19800e8SDoug Rabson     return (name);
173c19800e8SDoug Rabson }
174c19800e8SDoug Rabson 
175c19800e8SDoug Rabson /* netgroup_match - match group against machine or user */
176c19800e8SDoug Rabson 
netgroup_match(char * group,char * machine,char * user)177c19800e8SDoug Rabson static int netgroup_match(char *group, char *machine, char *user)
178c19800e8SDoug Rabson {
179c19800e8SDoug Rabson #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
180c19800e8SDoug Rabson     static char *mydomain = 0;
181c19800e8SDoug Rabson 
182c19800e8SDoug Rabson     if (mydomain == 0)
183c19800e8SDoug Rabson 	yp_get_default_domain(&mydomain);
184c19800e8SDoug Rabson     return (innetgr(group, machine, user, mydomain));
185c19800e8SDoug Rabson #else
186c19800e8SDoug Rabson     syslog(LOG_ERR, "NIS netgroup support not configured");
187c19800e8SDoug Rabson     return 0;
188c19800e8SDoug Rabson #endif
189c19800e8SDoug Rabson }
190c19800e8SDoug Rabson 
191c19800e8SDoug Rabson /* user_match - match a username against one token */
192c19800e8SDoug Rabson 
user_match(char * tok,struct login_info * item)193c19800e8SDoug Rabson static int user_match(char *tok, struct login_info *item)
194c19800e8SDoug Rabson {
195c19800e8SDoug Rabson     char   *string = item->user->pw_name;
196c19800e8SDoug Rabson     struct login_info fake_item;
197c19800e8SDoug Rabson     struct group *group;
198c19800e8SDoug Rabson     int     i;
199c19800e8SDoug Rabson     char   *at;
200c19800e8SDoug Rabson 
201c19800e8SDoug Rabson     /*
202c19800e8SDoug Rabson      * If a token has the magic value "ALL" the match always succeeds.
203c19800e8SDoug Rabson      * Otherwise, return YES if the token fully matches the username, if the
204c19800e8SDoug Rabson      * token is a group that contains the username, or if the token is the
205c19800e8SDoug Rabson      * name of the user's primary group.
206c19800e8SDoug Rabson      */
207c19800e8SDoug Rabson 
208c19800e8SDoug Rabson     if ((at = strchr(tok + 1, '@')) != 0) {	/* split user@host pattern */
209c19800e8SDoug Rabson 	*at = 0;
210c19800e8SDoug Rabson 	fake_item.from = myhostname();
211c19800e8SDoug Rabson 	return (user_match(tok, item) && from_match(at + 1, &fake_item));
212c19800e8SDoug Rabson     } else if (tok[0] == '@') {			/* netgroup */
213c19800e8SDoug Rabson 	return (netgroup_match(tok + 1, (char *) 0, string));
214c19800e8SDoug Rabson     } else if (string_match(tok, string)) {	/* ALL or exact match */
215c19800e8SDoug Rabson 	return (YES);
216c19800e8SDoug Rabson     } else if ((group = getgrnam(tok)) != 0) { /* try group membership */
217c19800e8SDoug Rabson 	if (item->user->pw_gid == group->gr_gid)
218c19800e8SDoug Rabson 	    return (YES);
219c19800e8SDoug Rabson 	for (i = 0; group->gr_mem[i]; i++)
220c19800e8SDoug Rabson 	    if (strcasecmp(string, group->gr_mem[i]) == 0)
221c19800e8SDoug Rabson 		return (YES);
222c19800e8SDoug Rabson     }
223c19800e8SDoug Rabson     return (NO);
224c19800e8SDoug Rabson }
225c19800e8SDoug Rabson 
226c19800e8SDoug Rabson /* from_match - match a host or tty against a list of tokens */
227c19800e8SDoug Rabson 
from_match(char * tok,struct login_info * item)228c19800e8SDoug Rabson static int from_match(char *tok, struct login_info *item)
229c19800e8SDoug Rabson {
230c19800e8SDoug Rabson     char   *string = item->from;
231c19800e8SDoug Rabson     int     tok_len;
232c19800e8SDoug Rabson     int     str_len;
233c19800e8SDoug Rabson 
234c19800e8SDoug Rabson     /*
235c19800e8SDoug Rabson      * If a token has the magic value "ALL" the match always succeeds. Return
236c19800e8SDoug Rabson      * YES if the token fully matches the string. If the token is a domain
237c19800e8SDoug Rabson      * name, return YES if it matches the last fields of the string. If the
238c19800e8SDoug Rabson      * token has the magic value "LOCAL", return YES if the string does not
239c19800e8SDoug Rabson      * contain a "." character. If the token is a network number, return YES
240c19800e8SDoug Rabson      * if it matches the head of the string.
241c19800e8SDoug Rabson      */
242c19800e8SDoug Rabson 
243c19800e8SDoug Rabson     if (tok[0] == '@') {			/* netgroup */
244c19800e8SDoug Rabson 	return (netgroup_match(tok + 1, string, (char *) 0));
245c19800e8SDoug Rabson     } else if (string_match(tok, string)) {	/* ALL or exact match */
246c19800e8SDoug Rabson 	return (YES);
247c19800e8SDoug Rabson     } else if (tok[0] == '.') {			/* domain: match last fields */
248c19800e8SDoug Rabson 	if ((str_len = strlen(string)) > (tok_len = strlen(tok))
249c19800e8SDoug Rabson 	    && strcasecmp(tok, string + str_len - tok_len) == 0)
250c19800e8SDoug Rabson 	    return (YES);
251c19800e8SDoug Rabson     } else if (strcasecmp(tok, "LOCAL") == 0) {	/* local: no dots */
252c19800e8SDoug Rabson 	if (strchr(string, '.') == 0)
253c19800e8SDoug Rabson 	    return (YES);
254c19800e8SDoug Rabson     } else if (tok[(tok_len = strlen(tok)) - 1] == '.'	/* network */
255c19800e8SDoug Rabson 	       && strncmp(tok, string, tok_len) == 0) {
256c19800e8SDoug Rabson 	return (YES);
257c19800e8SDoug Rabson     }
258c19800e8SDoug Rabson     return (NO);
259c19800e8SDoug Rabson }
260c19800e8SDoug Rabson 
261c19800e8SDoug Rabson /* string_match - match a string against one token */
262c19800e8SDoug Rabson 
string_match(char * tok,char * string)263c19800e8SDoug Rabson static int string_match(char *tok, char *string)
264c19800e8SDoug Rabson {
265c19800e8SDoug Rabson 
266c19800e8SDoug Rabson     /*
267c19800e8SDoug Rabson      * If the token has the magic value "ALL" the match always succeeds.
268c19800e8SDoug Rabson      * Otherwise, return YES if the token fully matches the string.
269c19800e8SDoug Rabson      */
270c19800e8SDoug Rabson 
271c19800e8SDoug Rabson     if (strcasecmp(tok, "ALL") == 0) {		/* all: always matches */
272c19800e8SDoug Rabson 	return (YES);
273c19800e8SDoug Rabson     } else if (strcasecmp(tok, string) == 0) {	/* try exact match */
274c19800e8SDoug Rabson 	return (YES);
275c19800e8SDoug Rabson     }
276c19800e8SDoug Rabson     return (NO);
277c19800e8SDoug Rabson }
278