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>
44 #include <rpcsvc/ypclnt.h>
46 #ifndef BROKEN_NETWORK_MATCH
48 # include <sys/socket.h>
52 * here, we make definitions for the externally accessible functions
53 * in this file (these definitions are required for static modules
54 * but strongly encouraged generally) they are used to instruct the
55 * modules include file to define their prototypes.
58 #define PAM_SM_ACCOUNT
60 #include <security/_pam_macros.h>
61 #include <security/pam_modules.h>
62 #include <security/_pam_modutil.h>
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 (pam_handle_t *, char *, struct login_info *);
154 static int list_match (pam_handle_t *, char *, struct login_info *,
156 static int user_match (pam_handle_t *, char *, struct login_info *);
157 static int from_match (pam_handle_t *, char *, struct login_info *);
158 static int string_match (pam_handle_t *, char *, char *);
160 /* login_access - match username/group and host/tty with access control file */
163 login_access (pam_handle_t *pamh, struct login_info *item)
167 char *perm; /* becomes permission field */
168 char *users; /* becomes list of login names */
169 char *froms; /* becomes list of terminals or hosts */
172 int lineno = 0; /* for diagnostics */
175 * Process the table one line at a time and stop at the first match.
176 * Blank lines and lines that begin with a '#' character are ignored.
177 * Non-comment lines are broken at the ':' character. All fields are
178 * mandatory. The first field should be a "+" or "-" character. A
179 * non-existing table means no access control.
182 if ((fp = fopen(item->config_file, "r"))!=NULL) {
183 while (!match && fgets(line, sizeof(line), fp)) {
185 if (line[end = strlen(line) - 1] != '\n') {
186 _log_err("%s: line %d: missing newline or line too long",
187 item->config_file, lineno);
191 continue; /* comment line */
192 while (end > 0 && isspace(line[end - 1]))
194 line[end] = 0; /* strip trailing whitespace */
195 if (line[0] == 0) /* skip blank lines */
197 if (!(perm = strtok(line, fs))
198 || !(users = strtok((char *) 0, fs))
199 || !(froms = strtok((char *) 0, fs))
200 || strtok((char *) 0, fs)) {
201 _log_err("%s: line %d: bad field count",
202 item->config_file, lineno);
205 if (perm[0] != '+' && perm[0] != '-') {
206 _log_err("%s: line %d: bad first field",
207 item->config_file, lineno);
210 match = (list_match(pamh, froms, item, from_match)
211 && list_match(pamh, users, item, user_match));
214 } else if (errno != ENOENT) {
215 _log_err("cannot open %s: %m", item->config_file);
218 return (match == 0 || (line[0] == '+'));
221 /* list_match - match an item against a list of tokens with exceptions */
223 static int list_match(pam_handle_t *pamh,
224 char *list, struct login_info *item, match_func *match_fn)
230 * Process tokens one at a time. We have exhausted all possible matches
231 * when we reach an "EXCEPT" token or the end of the list. If we do find
232 * a match, look for an "EXCEPT" list and recurse to determine whether
233 * the match is affected by any exceptions.
236 for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
237 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
239 if ((match = (*match_fn) (pamh, tok, item))) /* YES */
242 /* Process exceptions to matches. */
245 while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
247 if (tok == 0 || list_match(pamh, (char *) 0, item, match_fn) == NO)
253 /* myhostname - figure out local machine name */
255 static char * myhostname(void)
257 static char name[MAXHOSTNAMELEN + 1];
259 if (gethostname(name, MAXHOSTNAMELEN) == 0) {
260 name[MAXHOSTNAMELEN] = 0;
266 /* netgroup_match - match group against machine or user */
268 static int netgroup_match(char *group, char *machine, char *user)
270 static char *mydomain = NULL;
273 yp_get_default_domain(&mydomain);
274 return (innetgr(group, machine, user, mydomain));
277 /* user_match - match a username against one token */
279 static int user_match(pam_handle_t *pamh, char *tok, struct login_info *item)
281 char *string = item->user->pw_name;
282 struct login_info fake_item;
286 * If a token has the magic value "ALL" the match always succeeds.
287 * Otherwise, return YES if the token fully matches the username, if the
288 * token is a group that contains the username, or if the token is the
289 * name of the user's primary group.
292 if ((at = strchr(tok + 1, '@')) != 0) { /* split user@host pattern */
294 fake_item.from = myhostname();
295 if (fake_item.from == NULL)
297 return (user_match (pamh, tok, item) && from_match (pamh, at + 1, &fake_item));
298 } else if (tok[0] == '@') /* netgroup */
299 return (netgroup_match(tok + 1, (char *) 0, string));
300 else if (string_match (pamh, tok, string)) /* ALL or exact match */
302 else if (_pammodutil_user_in_group_nam_nam (pamh, item->user->pw_name, tok))
303 /* try group membership */
309 /* from_match - match a host or tty against a list of tokens */
312 from_match (pam_handle_t *pamh, char *tok, struct login_info *item)
314 char *string = item->from;
319 * If a token has the magic value "ALL" the match always succeeds. Return
320 * YES if the token fully matches the string. If the token is a domain
321 * name, return YES if it matches the last fields of the string. If the
322 * token has the magic value "LOCAL", return YES if the string does not
323 * contain a "." character. If the token is a network number, return YES
324 * if it matches the head of the string.
327 if (tok[0] == '@') { /* netgroup */
328 return (netgroup_match(tok + 1, string, (char *) 0));
329 } else if (string_match (pamh, tok, string)) /* ALL or exact match */
331 else if (tok[0] == '.') { /* domain: match last fields */
332 if ((str_len = strlen(string)) > (tok_len = strlen(tok))
333 && strcasecmp(tok, string + str_len - tok_len) == 0)
335 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
336 if (strchr(string, '.') == 0)
338 #ifdef BROKEN_NETWORK_MATCH
339 } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */
340 && strncmp(tok, string, tok_len) == 0) {
342 #else /* BROKEN_NETWORK_MATCH */
343 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') {
345 The code below does a more correct check if the address specified
346 by "string" starts from "tok".
347 1998/01/27 Andrey V. Savochkin <saw@msu.ru>
351 char hn[3+1+3+1+3+1+3+1+1];
354 h = gethostbyname(string);
357 if (h->h_addrtype != AF_INET)
359 if (h->h_length != 4)
360 return (NO); /* only IPv4 addresses (SAW) */
361 r = snprintf(hn, sizeof(hn), "%u.%u.%u.%u.",
362 (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1],
363 (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
364 if (r < 0 || r >= sizeof(hn))
366 if (!strncmp(tok, hn, tok_len))
368 #endif /* BROKEN_NETWORK_MATCH */
373 /* string_match - match a string against one token */
376 string_match (pam_handle_t *pamh, char *tok, char *string)
380 * If the token has the magic value "ALL" the match always succeeds.
381 * Otherwise, return YES if the token fully matches the string.
384 if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */
386 } else if (strcasecmp(tok, string) == 0) { /* try exact match */
392 /* --- public account management functions --- */
394 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh,int flags,int argc
397 struct login_info loginfo;
398 const char *user=NULL, *service=NULL;
400 struct passwd *user_pw;
402 if ((pam_get_item(pamh, PAM_SERVICE, (const void **)&service)
403 != PAM_SUCCESS) || (service == NULL) || (*service == ' ')) {
404 _log_err("cannot find the service name");
410 if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS || user == NULL
412 _log_err("cannot determine the user's name");
413 return PAM_USER_UNKNOWN;
416 /* remote host name */
418 if (pam_get_item(pamh, PAM_RHOST, (const void **)&from)
420 _log_err("cannot find the remote host name");
424 if ((from==NULL) || (*from=='\0')) {
426 /* local login, set tty name */
428 if (pam_get_item(pamh, PAM_TTY, (const void **)&from) != PAM_SUCCESS
430 D(("PAM_TTY not set, probing stdin"));
431 from = ttyname(STDIN_FILENO);
433 _log_err("couldn't get the tty name");
436 if (pam_set_item(pamh, PAM_TTY, from) != PAM_SUCCESS) {
437 _log_err("couldn't set tty name");
441 if (strncmp("/dev/",from,5) == 0) { /* strip leading /dev/ */
447 if ((user_pw=_pammodutil_getpwnam(pamh, user))==NULL) return (PAM_USER_UNKNOWN);
450 * Bundle up the arguments to avoid unnecessary clumsiness later on.
452 loginfo.user = user_pw;
454 loginfo.service = service;
455 loginfo.config_file = PAM_ACCESS_CONFIG;
457 /* parse the argument list */
459 if (!parse_args(&loginfo, argc, argv)) {
460 _log_err("failed to parse the module arguments");
464 if (login_access(pamh, &loginfo)) {
465 return (PAM_SUCCESS);
467 _log_err("access denied for user `%s' from `%s'",user,from);
468 return (PAM_PERM_DENIED);
472 /* end of module definition */
476 /* static module data */
478 struct pam_module _pam_access_modstruct = {