Home | History | Annotate | Line # | Download | only in pam_login_access
login_access.c revision 1.2.2.1
      1      1.2  christos /*	$NetBSD: login_access.c,v 1.2.2.1 2005/07/11 11:31:12 tron 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.2  christos __RCSID("$NetBSD: login_access.c,v 1.2.2.1 2005/07/11 11:31:12 tron 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.1  christos 
     35      1.1  christos #include "pam_login_access.h"
     36      1.1  christos 
     37      1.1  christos #define _PATH_LOGACCESS		"/etc/login.access"
     38      1.1  christos 
     39      1.1  christos  /* Delimiters for fields and for lists of users, ttys or hosts. */
     40      1.1  christos 
     41      1.1  christos static char fs[] = ":";			/* field separator */
     42      1.1  christos static char sep[] = ", \t";		/* list-element separator */
     43      1.1  christos 
     44      1.1  christos  /* Constants to be used in assignments only, not in comparisons... */
     45      1.1  christos 
     46      1.1  christos #define YES             1
     47      1.1  christos #define NO              0
     48      1.1  christos 
     49      1.1  christos static int	from_match(const char *, const char *);
     50      1.1  christos static int	list_match(char *, const char *,
     51      1.1  christos 				int (*)(const char *, const char *));
     52      1.1  christos static int	netgroup_match(const char *, const char *, const char *);
     53      1.1  christos static int	string_match(const char *, const char *);
     54      1.1  christos static int	user_match(const char *, const char *);
     55      1.1  christos 
     56      1.1  christos /* login_access - match username/group and host/tty with access control file */
     57      1.1  christos 
     58      1.1  christos int
     59      1.1  christos login_access(const char *user, const char *from)
     60      1.1  christos {
     61      1.1  christos     FILE   *fp;
     62      1.1  christos     char    line[BUFSIZ];
     63      1.1  christos     char   *perm;			/* becomes permission field */
     64      1.1  christos     char   *users;			/* becomes list of login names */
     65      1.1  christos     char   *froms;			/* becomes list of terminals or hosts */
     66      1.1  christos     int     match = NO;
     67      1.1  christos     int     end;
     68      1.1  christos     int     lineno = 0;			/* for diagnostics */
     69      1.1  christos 
     70      1.1  christos     /*
     71      1.1  christos      * Process the table one line at a time and stop at the first match.
     72      1.1  christos      * Blank lines and lines that begin with a '#' character are ignored.
     73      1.1  christos      * Non-comment lines are broken at the ':' character. All fields are
     74      1.1  christos      * mandatory. The first field should be a "+" or "-" character. A
     75      1.1  christos      * non-existing table means no access control.
     76      1.1  christos      */
     77      1.1  christos 
     78      1.1  christos     if ((fp = fopen(_PATH_LOGACCESS, "r")) != NULL) {
     79      1.1  christos 	while (!match && fgets(line, sizeof(line), fp)) {
     80      1.1  christos 	    lineno++;
     81      1.1  christos 	    if (line[end = strlen(line) - 1] != '\n') {
     82      1.1  christos 		syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
     83      1.1  christos 		       _PATH_LOGACCESS, lineno);
     84      1.1  christos 		continue;
     85      1.1  christos 	    }
     86      1.1  christos 	    if (line[0] == '#')
     87      1.1  christos 		continue;			/* comment line */
     88      1.2  christos 	    while (end > 0 && isspace((unsigned char)line[end - 1]))
     89      1.1  christos 		end--;
     90      1.1  christos 	    line[end] = 0;			/* strip trailing whitespace */
     91      1.1  christos 	    if (line[0] == 0)			/* skip blank lines */
     92      1.1  christos 		continue;
     93      1.1  christos 	    if (!(perm = strtok(line, fs))
     94      1.1  christos 		|| !(users = strtok((char *) 0, fs))
     95      1.1  christos 		|| !(froms = strtok((char *) 0, fs))
     96      1.1  christos 		|| strtok((char *) 0, fs)) {
     97      1.1  christos 		syslog(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS,
     98      1.1  christos 		       lineno);
     99      1.1  christos 		continue;
    100      1.1  christos 	    }
    101      1.1  christos 	    if (perm[0] != '+' && perm[0] != '-') {
    102      1.1  christos 		syslog(LOG_ERR, "%s: line %d: bad first field", _PATH_LOGACCESS,
    103      1.1  christos 		       lineno);
    104      1.1  christos 		continue;
    105      1.1  christos 	    }
    106      1.1  christos 	    match = (list_match(froms, from, from_match)
    107      1.1  christos 		     && list_match(users, user, user_match));
    108      1.1  christos 	}
    109      1.1  christos 	(void) fclose(fp);
    110      1.1  christos     } else if (errno != ENOENT) {
    111      1.1  christos 	syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS);
    112      1.1  christos     }
    113      1.1  christos     return (match == 0 || (line[0] == '+'));
    114      1.1  christos }
    115      1.1  christos 
    116      1.1  christos /* list_match - match an item against a list of tokens with exceptions */
    117      1.1  christos 
    118      1.1  christos static int
    119      1.1  christos list_match(char *list, const char *item,
    120      1.1  christos     int (*match_fn)(const char *, const char *))
    121      1.1  christos {
    122      1.1  christos     char   *tok;
    123      1.1  christos     int     match = NO;
    124      1.1  christos 
    125      1.1  christos     /*
    126      1.1  christos      * Process tokens one at a time. We have exhausted all possible matches
    127      1.1  christos      * when we reach an "EXCEPT" token or the end of the list. If we do find
    128      1.1  christos      * a match, look for an "EXCEPT" list and recurse to determine whether
    129      1.1  christos      * the match is affected by any exceptions.
    130      1.1  christos      */
    131      1.1  christos 
    132      1.1  christos     for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
    133      1.1  christos 	if (strcasecmp(tok, "EXCEPT") == 0)	/* EXCEPT: give up */
    134      1.1  christos 	    break;
    135      1.1  christos 	if ((match = (*match_fn)(tok, item)) != 0)	/* YES */
    136      1.1  christos 	    break;
    137      1.1  christos     }
    138      1.1  christos     /* Process exceptions to matches. */
    139      1.1  christos 
    140      1.1  christos     if (match != NO) {
    141      1.1  christos 	while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
    142      1.1  christos 	     /* VOID */ ;
    143      1.1  christos 	if (tok == 0 || list_match((char *) 0, item, match_fn) == NO)
    144      1.1  christos 	    return (match);
    145      1.1  christos     }
    146      1.1  christos     return (NO);
    147      1.1  christos }
    148      1.1  christos 
    149      1.1  christos /* netgroup_match - match group against machine or user */
    150      1.1  christos 
    151      1.1  christos static int
    152      1.1  christos netgroup_match(const char *group __unused,
    153      1.1  christos     const char *machine __unused, const char *user __unused)
    154      1.1  christos {
    155      1.1  christos     syslog(LOG_ERR, "NIS netgroup support not configured");
    156      1.1  christos     return 0;
    157      1.1  christos }
    158      1.1  christos 
    159      1.1  christos /* user_match - match a username against one token */
    160      1.1  christos 
    161      1.1  christos static int
    162      1.1  christos user_match(const char *tok, const char *string)
    163      1.1  christos {
    164  1.2.2.1      tron     struct group grres, *group;
    165  1.2.2.1      tron     char grbuf[1024];
    166      1.1  christos     int     i;
    167      1.1  christos 
    168      1.1  christos     /*
    169      1.1  christos      * If a token has the magic value "ALL" the match always succeeds.
    170      1.1  christos      * Otherwise, return YES if the token fully matches the username, or if
    171      1.1  christos      * the token is a group that contains the username.
    172      1.1  christos      */
    173      1.1  christos 
    174      1.1  christos     if (tok[0] == '@') {			/* netgroup */
    175      1.1  christos 	return (netgroup_match(tok + 1, (char *) 0, string));
    176      1.1  christos     } else if (string_match(tok, string)) {	/* ALL or exact match */
    177      1.1  christos 	return (YES);
    178  1.2.2.1      tron     } else if (getgrnam_r(tok, &grres, grbuf, sizeof(grbuf), &group) == 0 &&
    179  1.2.2.1      tron 	group != NULL) {/* try group membership */
    180      1.1  christos 	for (i = 0; group->gr_mem[i]; i++)
    181      1.1  christos 	    if (strcasecmp(string, group->gr_mem[i]) == 0)
    182      1.1  christos 		return (YES);
    183      1.1  christos     }
    184      1.1  christos     return (NO);
    185      1.1  christos }
    186      1.1  christos 
    187      1.1  christos /* from_match - match a host or tty against a list of tokens */
    188      1.1  christos 
    189      1.1  christos static int
    190      1.1  christos from_match(const char *tok, const char *string)
    191      1.1  christos {
    192      1.1  christos     int     tok_len;
    193      1.1  christos     int     str_len;
    194      1.1  christos 
    195      1.1  christos     /*
    196      1.1  christos      * If a token has the magic value "ALL" the match always succeeds. Return
    197      1.1  christos      * YES if the token fully matches the string. If the token is a domain
    198      1.1  christos      * name, return YES if it matches the last fields of the string. If the
    199      1.1  christos      * token has the magic value "LOCAL", return YES if the string does not
    200      1.1  christos      * contain a "." character. If the token is a network number, return YES
    201      1.1  christos      * if it matches the head of the string.
    202      1.1  christos      */
    203      1.1  christos 
    204      1.1  christos     if (tok[0] == '@') {			/* netgroup */
    205      1.1  christos 	return (netgroup_match(tok + 1, string, (char *) 0));
    206      1.1  christos     } else if (string_match(tok, string)) {	/* ALL or exact match */
    207      1.1  christos 	return (YES);
    208      1.1  christos     } else if (tok[0] == '.') {			/* domain: match last fields */
    209      1.1  christos 	if ((str_len = strlen(string)) > (tok_len = strlen(tok))
    210      1.1  christos 	    && strcasecmp(tok, string + str_len - tok_len) == 0)
    211      1.1  christos 	    return (YES);
    212      1.1  christos     } else if (strcasecmp(tok, "LOCAL") == 0) {	/* local: no dots */
    213      1.1  christos 	if (strchr(string, '.') == 0)
    214      1.1  christos 	    return (YES);
    215      1.1  christos     } else if (tok[(tok_len = strlen(tok)) - 1] == '.'	/* network */
    216      1.1  christos 	       && strncmp(tok, string, tok_len) == 0) {
    217      1.1  christos 	return (YES);
    218      1.1  christos     }
    219      1.1  christos     return (NO);
    220      1.1  christos }
    221      1.1  christos 
    222      1.1  christos /* string_match - match a string against one token */
    223      1.1  christos 
    224      1.1  christos static int
    225      1.1  christos string_match(const char *tok, const char *string)
    226      1.1  christos {
    227      1.1  christos 
    228      1.1  christos     /*
    229      1.1  christos      * If the token has the magic value "ALL" the match always succeeds.
    230      1.1  christos      * Otherwise, return YES if the token fully matches the string.
    231      1.1  christos      */
    232      1.1  christos 
    233      1.1  christos     if (strcasecmp(tok, "ALL") == 0) {		/* all: always matches */
    234      1.1  christos 	return (YES);
    235      1.1  christos     } else if (strcasecmp(tok, string) == 0) {	/* try exact match */
    236      1.1  christos 	return (YES);
    237      1.1  christos     }
    238      1.1  christos     return (NO);
    239      1.1  christos }
    240