1 /* pam_access module */
4 * Written by Alexei Nogin <alexei@nogin.dnttm.ru> 1997/06/15
5 * (I took login_access from logdaemon-5.6 and converted it to PAM
6 * using parts of pam_time code.)
8 ************************************************************************
9 * Copyright message from logdaemon-5.6 (original file name DISCLAIMER)
10 ************************************************************************
11 * Copyright 1995 by Wietse Venema. All rights reserved. Individual files
12 * may be covered by other copyrights (as noted in the file itself.)
14 * This material was originally written and compiled by Wietse Venema at
15 * Eindhoven University of Technology, The Netherlands, in 1990, 1991,
16 * 1992, 1993, 1994 and 1995.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this entire copyright notice is duplicated in all such
22 * This software is provided "as is" and without any expressed or implied
23 * warranties, including, without limitation, the implied warranties of
24 * merchantibility and fitness for any particular purpose.
25 *************************************************************************
28 #include <security/_pam_aconf.h>
37 #include <sys/types.h>
43 #include <sys/utsname.h>
45 #ifndef BROKEN_NETWORK_MATCH
47 # include <sys/socket.h>
51 * here, we make definitions for the externally accessible functions
52 * in this file (these definitions are required for static modules
53 * but strongly encouraged generally) they are used to instruct the
54 * modules include file to define their prototypes.
57 #define PAM_SM_ACCOUNT
59 #include <security/_pam_macros.h>
60 #include <security/pam_modules.h>
62 int strcasecmp(const char *s1, const char *s2);
64 /* login_access.c from logdaemon-5.6 with several changes by A.Nogin: */
67 * This module implements a simple but effective form of login access
68 * control based on login names and on host (or domain) names, internet
69 * addresses (or network numbers), or on terminal line names in case of
70 * non-networked logins. Diagnostics are reported through syslog(3).
72 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
75 #if !defined(MAXHOSTNAMELEN) || (MAXHOSTNAMELEN < 64)
77 #define MAXHOSTNAMELEN 256
80 #ifdef DEFAULT_CONF_FILE
81 # define PAM_ACCESS_CONFIG DEFAULT_CONF_FILE
83 # define PAM_ACCESS_CONFIG "/etc/security/access.conf"
86 /* Delimiters for fields and for lists of users, ttys or hosts. */
88 static const char *fs = ":"; /* field separator */
89 static const char sep[] = ", \t"; /* list-element separator */
91 /* Constants to be used in assignments only, not in comparisons... */
97 * A structure to bundle up all login-related information to keep the
98 * functional interfaces as generic as possible.
103 const char *config_file;
107 /* --- static functions for checking whether the user should be let in --- */
109 static void _log_err(const char *format, ... )
113 va_start(args, format);
114 openlog("pam_access", LOG_CONS|LOG_PID, LOG_AUTH);
115 vsyslog(LOG_ERR, format, args);
120 /* Parse module config arguments */
122 static int parse_args(struct login_info *loginfo, int argc, const char **argv)
126 for (i=0; i<argc; ++i) {
127 if (!strncmp("fieldsep=", argv[i], 9)) {
129 /* the admin wants to override the default field separators */
132 } else if (!strncmp("accessfile=", argv[i], 11)) {
133 FILE *fp = fopen(11 + argv[i], "r");
136 loginfo->config_file = 11 + argv[i];
139 _log_err("for service [%s] failed to open accessfile=[%s]"
140 , loginfo->service, 11 + argv[i]);
145 _log_err("unrecognized option [%s]", argv[i]);
152 typedef int match_func (char *, struct login_info *);
154 static int list_match (char *, struct login_info *,
156 static int user_match (char *, struct login_info *);
157 static int from_match (char *, struct login_info *);
158 static int string_match (char *, char *);
160 /* login_access - match username/group and host/tty with access control file */
162 static int login_access(struct login_info *item)
166 char *perm; /* becomes permission field */
167 char *users; /* becomes list of login names */
168 char *froms; /* becomes list of terminals or hosts */
171 int lineno = 0; /* for diagnostics */
174 * Process the table one line at a time and stop at the first match.
175 * Blank lines and lines that begin with a '#' character are ignored.
176 * Non-comment lines are broken at the ':' character. All fields are
177 * mandatory. The first field should be a "+" or "-" character. A
178 * non-existing table means no access control.
181 if ((fp = fopen(item->config_file, "r"))!=NULL) {
182 while (!match && fgets(line, sizeof(line), fp)) {
184 if (line[end = strlen(line) - 1] != '\n') {
185 _log_err("%s: line %d: missing newline or line too long",
186 item->config_file, lineno);
190 continue; /* comment line */
191 while (end > 0 && isspace(line[end - 1]))
193 line[end] = 0; /* strip trailing whitespace */
194 if (line[0] == 0) /* skip blank lines */
196 if (!(perm = strtok(line, fs))
197 || !(users = strtok((char *) 0, fs))
198 || !(froms = strtok((char *) 0, fs))
199 || strtok((char *) 0, fs)) {
200 _log_err("%s: line %d: bad field count",
201 item->config_file, lineno);
204 if (perm[0] != '+' && perm[0] != '-') {
205 _log_err("%s: line %d: bad first field",
206 item->config_file, lineno);
209 match = (list_match(froms, item, from_match)
210 && list_match(users, item, user_match));
213 } else if (errno != ENOENT) {
214 _log_err("cannot open %s: %m", item->config_file);
216 return (match == 0 || (line[0] == '+'));
219 /* list_match - match an item against a list of tokens with exceptions */
221 static int list_match(char *list, struct login_info *item, match_func *match_fn)
227 * Process tokens one at a time. We have exhausted all possible matches
228 * when we reach an "EXCEPT" token or the end of the list. If we do find
229 * a match, look for an "EXCEPT" list and recurse to determine whether
230 * the match is affected by any exceptions.
233 for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
234 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
236 if ((match = (*match_fn) (tok, item))) /* YES */
239 /* Process exceptions to matches. */
242 while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
244 if (tok == 0 || list_match((char *) 0, item, match_fn) == NO)
250 /* myhostname - figure out local machine name */
252 static char * myhostname(void)
254 static char name[MAXHOSTNAMELEN + 1];
256 gethostname(name, MAXHOSTNAMELEN);
257 name[MAXHOSTNAMELEN] = 0;
261 /* netgroup_match - match group against machine or user */
263 static int netgroup_match(char *group, char *machine, char *user)
266 static char *mydomain = 0;
269 yp_get_default_domain(&mydomain);
270 return (innetgr(group, machine, user, mydomain));
272 _log_err("NIS netgroup support not configured");
277 /* user_match - match a username against one token */
279 static int user_match(char *tok, struct login_info *item)
281 char *string = item->user->pw_name;
282 struct login_info fake_item;
288 * If a token has the magic value "ALL" the match always succeeds.
289 * Otherwise, return YES if the token fully matches the username, if the
290 * token is a group that contains the username, or if the token is the
291 * name of the user's primary group.
294 if ((at = strchr(tok + 1, '@')) != 0) { /* split user@host pattern */
296 fake_item.from = myhostname();
297 return (user_match(tok, item) && from_match(at + 1, &fake_item));
298 } else if (tok[0] == '@') { /* netgroup */
299 return (netgroup_match(tok + 1, (char *) 0, string));
300 } else if (string_match(tok, string)) { /* ALL or exact match */
302 } else if ((group = getgrnam(tok))) { /* try group membership */
303 if (item->user->pw_gid == group->gr_gid)
305 for (i = 0; group->gr_mem[i]; i++)
306 if (strcasecmp(string, group->gr_mem[i]) == 0)
312 /* from_match - match a host or tty against a list of tokens */
314 static int from_match(char *tok, struct login_info *item)
316 char *string = item->from;
321 * If a token has the magic value "ALL" the match always succeeds. Return
322 * YES if the token fully matches the string. If the token is a domain
323 * name, return YES if it matches the last fields of the string. If the
324 * token has the magic value "LOCAL", return YES if the string does not
325 * contain a "." character. If the token is a network number, return YES
326 * if it matches the head of the string.
329 if (tok[0] == '@') { /* netgroup */
330 return (netgroup_match(tok + 1, string, (char *) 0));
331 } else if (string_match(tok, string)) { /* ALL or exact match */
333 } else if (tok[0] == '.') { /* domain: match last fields */
334 if ((str_len = strlen(string)) > (tok_len = strlen(tok))
335 && strcasecmp(tok, string + str_len - tok_len) == 0)
337 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
338 if (strchr(string, '.') == 0)
340 #ifdef BROKEN_NETWORK_MATCH
341 } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */
342 && strncmp(tok, string, tok_len) == 0) {
344 #else /* BROKEN_NETWORK_MATCH */
345 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') {
347 The code below does a more correct check if the address specified
348 by "string" starts from "tok".
349 1998/01/27 Andrey V. Savochkin <saw@msu.ru>
353 char hn[3+1+3+1+3+1+3+1+1];
356 h = gethostbyname(string);
359 if (h->h_addrtype != AF_INET)
361 if (h->h_length != 4)
362 return (NO); /* only IPv4 addresses (SAW) */
363 r = snprintf(hn, sizeof(hn), "%u.%u.%u.%u.",
364 (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1],
365 (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
366 if (r < 0 || r >= sizeof(hn))
368 if (!strncmp(tok, hn, tok_len))
370 #endif /* BROKEN_NETWORK_MATCH */
375 /* string_match - match a string against one token */
377 static int string_match(char *tok, char *string)
381 * If the token has the magic value "ALL" the match always succeeds.
382 * Otherwise, return YES if the token fully matches the string.
385 if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */
387 } else if (strcasecmp(tok, string) == 0) { /* try exact match */
393 /* end of login_access.c */
395 int strcasecmp(const char *s1, const char *s2)
397 while ((toupper(*s1)==toupper(*s2)) && (*s1) && (*s2)) {s1++; s2++;}
398 return(toupper(*s1)-toupper(*s2));
401 /* --- public account management functions --- */
403 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh,int flags,int argc
406 struct login_info loginfo;
407 const char *user=NULL, *service=NULL;
409 struct passwd *user_pw;
411 if ((pam_get_item(pamh, PAM_SERVICE, (const void **)&service)
412 != PAM_SUCCESS) || (service == NULL) || (*service == ' ')) {
413 _log_err("cannot find the service name");
419 if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS || user == NULL
421 _log_err("cannot determine the user's name");
422 return PAM_USER_UNKNOWN;
425 /* remote host name */
427 if (pam_get_item(pamh, PAM_RHOST, (const void **)&from)
429 _log_err("cannot find the remote host name");
433 if ((from==NULL) || (*from=='\0')) {
435 /* local login, set tty name */
437 if (pam_get_item(pamh, PAM_TTY, (const void **)&from) != PAM_SUCCESS
439 D(("PAM_TTY not set, probing stdin"));
440 from = ttyname(STDIN_FILENO);
442 _log_err("couldn't get the tty name");
445 if (pam_set_item(pamh, PAM_TTY, from) != PAM_SUCCESS) {
446 _log_err("couldn't set tty name");
450 if (strncmp("/dev/",from,5) == 0) { /* strip leading /dev/ */
456 if ((user_pw=getpwnam(user))==NULL) return (PAM_USER_UNKNOWN);
459 * Bundle up the arguments to avoid unnecessary clumsiness later on.
461 loginfo.user = user_pw;
463 loginfo.service = service;
464 loginfo.config_file = PAM_ACCESS_CONFIG;
466 /* parse the argument list */
468 if (!parse_args(&loginfo, argc, argv)) {
469 _log_err("failed to parse the module arguments");
473 if (login_access(&loginfo)) {
474 return (PAM_SUCCESS);
476 _log_err("access denied for user `%s' from `%s'",user,from);
477 return (PAM_PERM_DENIED);
481 /* end of module definition */
485 /* static module data */
487 struct pam_module _pam_access_modstruct = {