Home | History | Annotate | Line # | Download | only in pam_login_access
login_access.c revision 1.5.40.2
      1  1.5.40.2      yamt /*	$NetBSD: login_access.c,v 1.5.40.2 2014/05/22 11:36:58 yamt Exp $	*/
      2       1.2  christos 
      3       1.2  christos /*
      4       1.2  christos  * This module implements a simple but effective form of login access
      5       1.2  christos  * control based on login names and on host (or domain) names, internet
      6       1.2  christos  * addresses (or network numbers), or on terminal line names in case of
      7       1.2  christos  * non-networked logins. Diagnostics are reported through syslog(3).
      8       1.2  christos  *
      9       1.2  christos  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
     10       1.2  christos  */
     11       1.1  christos 
     12       1.1  christos #if 0
     13       1.1  christos #ifndef lint
     14       1.1  christos static char sccsid[] = "%Z% %M% %I% %E% %U%";
     15       1.1  christos #endif
     16       1.1  christos #endif
     17       1.1  christos 
     18       1.1  christos #include <sys/cdefs.h>
     19       1.2  christos #ifdef __FreeBSD__
     20       1.1  christos __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_login_access/login_access.c,v 1.12 2004/03/05 08:10:18 markm Exp $");
     21       1.2  christos #else
     22  1.5.40.2      yamt __RCSID("$NetBSD: login_access.c,v 1.5.40.2 2014/05/22 11:36:58 yamt Exp $");
     23       1.2  christos #endif
     24       1.1  christos 
     25       1.1  christos #include <sys/types.h>
     26       1.1  christos #include <ctype.h>
     27       1.1  christos #include <errno.h>
     28       1.1  christos #include <grp.h>
     29       1.1  christos #include <stdio.h>
     30       1.1  christos #include <stdlib.h>
     31       1.1  christos #include <string.h>
     32       1.1  christos #include <syslog.h>
     33       1.1  christos #include <unistd.h>
     34       1.4  christos #include <stdarg.h>
     35       1.1  christos 
     36       1.1  christos #include "pam_login_access.h"
     37       1.1  christos 
     38       1.1  christos #define _PATH_LOGACCESS		"/etc/login.access"
     39       1.1  christos 
     40       1.1  christos  /* Delimiters for fields and for lists of users, ttys or hosts. */
     41       1.1  christos 
     42       1.1  christos static char fs[] = ":";			/* field separator */
     43       1.1  christos static char sep[] = ", \t";		/* list-element separator */
     44       1.1  christos 
     45       1.1  christos  /* Constants to be used in assignments only, not in comparisons... */
     46       1.1  christos 
     47       1.1  christos #define YES             1
     48       1.1  christos #define NO              0
     49       1.1  christos 
     50       1.1  christos static int	from_match(const char *, const char *);
     51       1.1  christos static int	list_match(char *, const char *,
     52       1.1  christos 				int (*)(const char *, const char *));
     53       1.1  christos static int	netgroup_match(const char *, const char *, const char *);
     54       1.1  christos static int	string_match(const char *, const char *);
     55       1.1  christos static int	user_match(const char *, const char *);
     56       1.1  christos 
     57       1.1  christos /* login_access - match username/group and host/tty with access control file */
     58       1.1  christos 
     59  1.5.40.2      yamt __printflike(2, 3)
     60       1.4  christos static void
     61       1.4  christos logit(int level, const char *fmt, ...)
     62       1.4  christos {
     63       1.4  christos 	va_list ap;
     64       1.5  christos 	struct syslog_data data = SYSLOG_DATA_INIT;
     65       1.4  christos 
     66       1.4  christos 	openlog_r("pam_login_access", LOG_PID, LOG_AUTHPRIV, &data);
     67       1.4  christos 	va_start(ap, fmt);
     68       1.4  christos 	vsyslog_r(level, &data, fmt, ap);
     69       1.4  christos 	va_end(ap);
     70       1.4  christos 	closelog_r(&data);
     71       1.4  christos }
     72       1.4  christos 
     73       1.1  christos int
     74       1.1  christos login_access(const char *user, const char *from)
     75       1.1  christos {
     76       1.1  christos     FILE   *fp;
     77       1.1  christos     char    line[BUFSIZ];
     78       1.1  christos     char   *perm;			/* becomes permission field */
     79       1.1  christos     char   *users;			/* becomes list of login names */
     80       1.1  christos     char   *froms;			/* becomes list of terminals or hosts */
     81       1.1  christos     int     match = NO;
     82  1.5.40.2      yamt     size_t  end;
     83       1.1  christos     int     lineno = 0;			/* for diagnostics */
     84       1.1  christos 
     85       1.1  christos     /*
     86       1.1  christos      * Process the table one line at a time and stop at the first match.
     87       1.1  christos      * Blank lines and lines that begin with a '#' character are ignored.
     88       1.1  christos      * Non-comment lines are broken at the ':' character. All fields are
     89       1.1  christos      * mandatory. The first field should be a "+" or "-" character. A
     90       1.1  christos      * non-existing table means no access control.
     91       1.1  christos      */
     92       1.1  christos 
     93       1.1  christos     if ((fp = fopen(_PATH_LOGACCESS, "r")) != NULL) {
     94       1.1  christos 	while (!match && fgets(line, sizeof(line), fp)) {
     95       1.1  christos 	    lineno++;
     96  1.5.40.2      yamt 	    if ((end = strlen(line)) == 0 || line[end - 1] != '\n') {
     97       1.4  christos 		logit(LOG_ERR, "%s: line %d: missing newline or line too long",
     98       1.1  christos 		       _PATH_LOGACCESS, lineno);
     99       1.1  christos 		continue;
    100       1.1  christos 	    }
    101       1.1  christos 	    if (line[0] == '#')
    102       1.1  christos 		continue;			/* comment line */
    103       1.2  christos 	    while (end > 0 && isspace((unsigned char)line[end - 1]))
    104       1.1  christos 		end--;
    105       1.1  christos 	    line[end] = 0;			/* strip trailing whitespace */
    106       1.1  christos 	    if (line[0] == 0)			/* skip blank lines */
    107       1.1  christos 		continue;
    108       1.1  christos 	    if (!(perm = strtok(line, fs))
    109       1.1  christos 		|| !(users = strtok((char *) 0, fs))
    110       1.1  christos 		|| !(froms = strtok((char *) 0, fs))
    111       1.1  christos 		|| strtok((char *) 0, fs)) {
    112       1.4  christos 		logit(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS,
    113       1.1  christos 		       lineno);
    114       1.1  christos 		continue;
    115       1.1  christos 	    }
    116       1.1  christos 	    if (perm[0] != '+' && perm[0] != '-') {
    117       1.4  christos 		logit(LOG_ERR, "%s: line %d: bad first field", _PATH_LOGACCESS,
    118       1.1  christos 		       lineno);
    119       1.1  christos 		continue;
    120       1.1  christos 	    }
    121       1.1  christos 	    match = (list_match(froms, from, from_match)
    122       1.1  christos 		     && list_match(users, user, user_match));
    123       1.1  christos 	}
    124       1.1  christos 	(void) fclose(fp);
    125       1.1  christos     } else if (errno != ENOENT) {
    126  1.5.40.1      yamt 	logit(LOG_ERR, "cannot open %s: %s", _PATH_LOGACCESS, strerror(errno));
    127       1.1  christos     }
    128       1.1  christos     return (match == 0 || (line[0] == '+'));
    129       1.1  christos }
    130       1.1  christos 
    131       1.1  christos /* list_match - match an item against a list of tokens with exceptions */
    132       1.1  christos 
    133       1.1  christos static int
    134       1.1  christos list_match(char *list, const char *item,
    135       1.1  christos     int (*match_fn)(const char *, const char *))
    136       1.1  christos {
    137       1.1  christos     char   *tok;
    138       1.1  christos     int     match = NO;
    139       1.1  christos 
    140       1.1  christos     /*
    141       1.1  christos      * Process tokens one at a time. We have exhausted all possible matches
    142       1.1  christos      * when we reach an "EXCEPT" token or the end of the list. If we do find
    143       1.1  christos      * a match, look for an "EXCEPT" list and recurse to determine whether
    144       1.1  christos      * the match is affected by any exceptions.
    145       1.1  christos      */
    146       1.1  christos 
    147       1.1  christos     for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
    148       1.1  christos 	if (strcasecmp(tok, "EXCEPT") == 0)	/* EXCEPT: give up */
    149       1.1  christos 	    break;
    150       1.1  christos 	if ((match = (*match_fn)(tok, item)) != 0)	/* YES */
    151       1.1  christos 	    break;
    152       1.1  christos     }
    153       1.1  christos     /* Process exceptions to matches. */
    154       1.1  christos 
    155       1.1  christos     if (match != NO) {
    156       1.1  christos 	while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
    157       1.1  christos 	     /* VOID */ ;
    158       1.1  christos 	if (tok == 0 || list_match((char *) 0, item, match_fn) == NO)
    159       1.1  christos 	    return (match);
    160       1.1  christos     }
    161       1.1  christos     return (NO);
    162       1.1  christos }
    163       1.1  christos 
    164       1.1  christos /* netgroup_match - match group against machine or user */
    165       1.1  christos 
    166       1.1  christos static int
    167       1.1  christos netgroup_match(const char *group __unused,
    168       1.1  christos     const char *machine __unused, const char *user __unused)
    169       1.1  christos {
    170       1.4  christos     logit(LOG_ERR, "NIS netgroup support not configured");
    171       1.1  christos     return 0;
    172       1.1  christos }
    173       1.1  christos 
    174       1.1  christos /* user_match - match a username against one token */
    175       1.1  christos 
    176       1.1  christos static int
    177       1.1  christos user_match(const char *tok, const char *string)
    178       1.1  christos {
    179       1.3  christos     struct group grres, *group;
    180       1.3  christos     char grbuf[1024];
    181       1.1  christos     int     i;
    182       1.1  christos 
    183       1.1  christos     /*
    184       1.1  christos      * If a token has the magic value "ALL" the match always succeeds.
    185       1.1  christos      * Otherwise, return YES if the token fully matches the username, or if
    186       1.1  christos      * the token is a group that contains the username.
    187       1.1  christos      */
    188       1.1  christos 
    189       1.1  christos     if (tok[0] == '@') {			/* netgroup */
    190       1.1  christos 	return (netgroup_match(tok + 1, (char *) 0, string));
    191       1.1  christos     } else if (string_match(tok, string)) {	/* ALL or exact match */
    192       1.1  christos 	return (YES);
    193       1.3  christos     } else if (getgrnam_r(tok, &grres, grbuf, sizeof(grbuf), &group) == 0 &&
    194       1.3  christos 	group != NULL) {/* try group membership */
    195       1.1  christos 	for (i = 0; group->gr_mem[i]; i++)
    196       1.1  christos 	    if (strcasecmp(string, group->gr_mem[i]) == 0)
    197       1.1  christos 		return (YES);
    198       1.1  christos     }
    199       1.1  christos     return (NO);
    200       1.1  christos }
    201       1.1  christos 
    202       1.1  christos /* from_match - match a host or tty against a list of tokens */
    203       1.1  christos 
    204       1.1  christos static int
    205       1.1  christos from_match(const char *tok, const char *string)
    206       1.1  christos {
    207  1.5.40.2      yamt     size_t     tok_len;
    208  1.5.40.2      yamt     size_t     str_len;
    209       1.1  christos 
    210       1.1  christos     /*
    211       1.1  christos      * If a token has the magic value "ALL" the match always succeeds. Return
    212       1.1  christos      * YES if the token fully matches the string. If the token is a domain
    213       1.1  christos      * name, return YES if it matches the last fields of the string. If the
    214       1.1  christos      * token has the magic value "LOCAL", return YES if the string does not
    215       1.1  christos      * contain a "." character. If the token is a network number, return YES
    216       1.1  christos      * if it matches the head of the string.
    217       1.1  christos      */
    218       1.1  christos 
    219       1.1  christos     if (tok[0] == '@') {			/* netgroup */
    220       1.1  christos 	return (netgroup_match(tok + 1, string, (char *) 0));
    221       1.1  christos     } else if (string_match(tok, string)) {	/* ALL or exact match */
    222       1.1  christos 	return (YES);
    223       1.1  christos     } else if (tok[0] == '.') {			/* domain: match last fields */
    224       1.1  christos 	if ((str_len = strlen(string)) > (tok_len = strlen(tok))
    225       1.1  christos 	    && strcasecmp(tok, string + str_len - tok_len) == 0)
    226       1.1  christos 	    return (YES);
    227       1.1  christos     } else if (strcasecmp(tok, "LOCAL") == 0) {	/* local: no dots */
    228       1.1  christos 	if (strchr(string, '.') == 0)
    229       1.1  christos 	    return (YES);
    230       1.1  christos     } else if (tok[(tok_len = strlen(tok)) - 1] == '.'	/* network */
    231       1.1  christos 	       && strncmp(tok, string, tok_len) == 0) {
    232       1.1  christos 	return (YES);
    233       1.1  christos     }
    234       1.1  christos     return (NO);
    235       1.1  christos }
    236       1.1  christos 
    237       1.1  christos /* string_match - match a string against one token */
    238       1.1  christos 
    239       1.1  christos static int
    240       1.1  christos string_match(const char *tok, const char *string)
    241       1.1  christos {
    242       1.1  christos 
    243       1.1  christos     /*
    244       1.1  christos      * If the token has the magic value "ALL" the match always succeeds.
    245       1.1  christos      * Otherwise, return YES if the token fully matches the string.
    246       1.1  christos      */
    247       1.1  christos 
    248       1.1  christos     if (strcasecmp(tok, "ALL") == 0) {		/* all: always matches */
    249       1.1  christos 	return (YES);
    250       1.1  christos     } else if (strcasecmp(tok, string) == 0) {	/* try exact match */
    251       1.1  christos 	return (YES);
    252       1.1  christos     }
    253       1.1  christos     return (NO);
    254       1.1  christos }
    255