login_access.c revision 126643
12198Sguido /* 22198Sguido * This module implements a simple but effective form of login access 32198Sguido * control based on login names and on host (or domain) names, internet 42198Sguido * addresses (or network numbers), or on terminal line names in case of 52198Sguido * non-networked logins. Diagnostics are reported through syslog(3). 68874Srgrimes * 72198Sguido * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. 82198Sguido */ 92198Sguido 1087628Sdwmalone#if 0 112198Sguido#ifndef lint 1287628Sdwmalonestatic char sccsid[] = "%Z% %M% %I% %E% %U%"; 132198Sguido#endif 1487628Sdwmalone#endif 152198Sguido 1687628Sdwmalone#include <sys/cdefs.h> 1787628Sdwmalone__FBSDID("$FreeBSD: head/lib/libpam/modules/pam_login_access/login_access.c 126643 2004-03-05 08:10:19Z markm $"); 1887628Sdwmalone 1987177Smarkm#include <sys/types.h> 202198Sguido#include <ctype.h> 2187177Smarkm#include <errno.h> 222198Sguido#include <grp.h> 2387177Smarkm#include <stdio.h> 2487177Smarkm#include <stdlib.h> 252198Sguido#include <string.h> 2687177Smarkm#include <syslog.h> 272198Sguido#include <unistd.h> 282198Sguido 2990145Smarkm#include "pam_login_access.h" 3090145Smarkm 3190093Sdes#define _PATH_LOGACCESS "/etc/login.access" 322198Sguido 332198Sguido /* Delimiters for fields and for lists of users, ttys or hosts. */ 342198Sguido 352198Sguidostatic char fs[] = ":"; /* field separator */ 362198Sguidostatic char sep[] = ", \t"; /* list-element separator */ 372198Sguido 382198Sguido /* Constants to be used in assignments only, not in comparisons... */ 392198Sguido 402198Sguido#define YES 1 412198Sguido#define NO 0 422198Sguido 4390145Smarkmstatic int from_match(const char *, const char *); 4490145Smarkmstatic int list_match(char *, const char *, 4590145Smarkm int (*)(const char *, const char *)); 4690145Smarkmstatic int netgroup_match(const char *, const char *, const char *); 4790145Smarkmstatic int string_match(const char *, const char *); 4890145Smarkmstatic int user_match(const char *, const char *); 492198Sguido 502198Sguido/* login_access - match username/group and host/tty with access control file */ 512198Sguido 5222230Spstint 5390145Smarkmlogin_access(const char *user, const char *from) 542198Sguido{ 552198Sguido FILE *fp; 562198Sguido char line[BUFSIZ]; 572198Sguido char *perm; /* becomes permission field */ 582198Sguido char *users; /* becomes list of login names */ 592198Sguido char *froms; /* becomes list of terminals or hosts */ 602198Sguido int match = NO; 612198Sguido int end; 622198Sguido int lineno = 0; /* for diagnostics */ 632198Sguido 642198Sguido /* 652198Sguido * Process the table one line at a time and stop at the first match. 662198Sguido * Blank lines and lines that begin with a '#' character are ignored. 672198Sguido * Non-comment lines are broken at the ':' character. All fields are 682198Sguido * mandatory. The first field should be a "+" or "-" character. A 692198Sguido * non-existing table means no access control. 702198Sguido */ 712198Sguido 7222230Spst if ((fp = fopen(_PATH_LOGACCESS, "r")) != NULL) { 732198Sguido while (!match && fgets(line, sizeof(line), fp)) { 742198Sguido lineno++; 752198Sguido if (line[end = strlen(line) - 1] != '\n') { 762198Sguido syslog(LOG_ERR, "%s: line %d: missing newline or line too long", 772198Sguido _PATH_LOGACCESS, lineno); 782198Sguido continue; 792198Sguido } 802198Sguido if (line[0] == '#') 812198Sguido continue; /* comment line */ 822198Sguido while (end > 0 && isspace(line[end - 1])) 832198Sguido end--; 842198Sguido line[end] = 0; /* strip trailing whitespace */ 852198Sguido if (line[0] == 0) /* skip blank lines */ 862198Sguido continue; 872198Sguido if (!(perm = strtok(line, fs)) 882198Sguido || !(users = strtok((char *) 0, fs)) 892198Sguido || !(froms = strtok((char *) 0, fs)) 902198Sguido || strtok((char *) 0, fs)) { 912198Sguido syslog(LOG_ERR, "%s: line %d: bad field count", _PATH_LOGACCESS, 922198Sguido lineno); 932198Sguido continue; 942198Sguido } 952198Sguido if (perm[0] != '+' && perm[0] != '-') { 962198Sguido syslog(LOG_ERR, "%s: line %d: bad first field", _PATH_LOGACCESS, 972198Sguido lineno); 982198Sguido continue; 992198Sguido } 1002198Sguido match = (list_match(froms, from, from_match) 1012198Sguido && list_match(users, user, user_match)); 1022198Sguido } 1032198Sguido (void) fclose(fp); 1042198Sguido } else if (errno != ENOENT) { 1052198Sguido syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS); 1062198Sguido } 1072198Sguido return (match == 0 || (line[0] == '+')); 1082198Sguido} 1092198Sguido 1102198Sguido/* list_match - match an item against a list of tokens with exceptions */ 1112198Sguido 11290145Smarkmstatic int 11390145Smarkmlist_match(char *list, const char *item, 11490145Smarkm int (*match_fn)(const char *, const char *)) 1152198Sguido{ 1162198Sguido char *tok; 1172198Sguido int match = NO; 1182198Sguido 1192198Sguido /* 1202198Sguido * Process tokens one at a time. We have exhausted all possible matches 1212198Sguido * when we reach an "EXCEPT" token or the end of the list. If we do find 1222198Sguido * a match, look for an "EXCEPT" list and recurse to determine whether 1232198Sguido * the match is affected by any exceptions. 1242198Sguido */ 1252198Sguido 1262198Sguido for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) { 1272198Sguido if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */ 1282198Sguido break; 129126643Smarkm if ((match = (*match_fn)(tok, item)) != 0) /* YES */ 1302198Sguido break; 1312198Sguido } 1322198Sguido /* Process exceptions to matches. */ 1332198Sguido 1342198Sguido if (match != NO) { 1352198Sguido while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT")) 1362198Sguido /* VOID */ ; 1372198Sguido if (tok == 0 || list_match((char *) 0, item, match_fn) == NO) 1382198Sguido return (match); 1392198Sguido } 1402198Sguido return (NO); 1412198Sguido} 1422198Sguido 1432198Sguido/* netgroup_match - match group against machine or user */ 1442198Sguido 14590145Smarkmstatic int 14690145Smarkmnetgroup_match(const char *group __unused, 14790145Smarkm const char *machine __unused, const char *user __unused) 1482198Sguido{ 1492198Sguido syslog(LOG_ERR, "NIS netgroup support not configured"); 15022230Spst return 0; 1512198Sguido} 1522198Sguido 1532198Sguido/* user_match - match a username against one token */ 1542198Sguido 15590145Smarkmstatic int 15690145Smarkmuser_match(const char *tok, const char *string) 1572198Sguido{ 1582198Sguido struct group *group; 1592198Sguido int i; 1602198Sguido 1612198Sguido /* 1622198Sguido * If a token has the magic value "ALL" the match always succeeds. 1632198Sguido * Otherwise, return YES if the token fully matches the username, or if 1642198Sguido * the token is a group that contains the username. 1652198Sguido */ 1662198Sguido 1672198Sguido if (tok[0] == '@') { /* netgroup */ 1682198Sguido return (netgroup_match(tok + 1, (char *) 0, string)); 1692198Sguido } else if (string_match(tok, string)) { /* ALL or exact match */ 1702198Sguido return (YES); 17122230Spst } else if ((group = getgrnam(tok)) != NULL) {/* try group membership */ 1722198Sguido for (i = 0; group->gr_mem[i]; i++) 1732198Sguido if (strcasecmp(string, group->gr_mem[i]) == 0) 1742198Sguido return (YES); 1752198Sguido } 1762198Sguido return (NO); 1772198Sguido} 1782198Sguido 1792198Sguido/* from_match - match a host or tty against a list of tokens */ 1802198Sguido 18190145Smarkmstatic int 18290145Smarkmfrom_match(const char *tok, const char *string) 1832198Sguido{ 1842198Sguido int tok_len; 1852198Sguido int str_len; 1862198Sguido 1872198Sguido /* 1882198Sguido * If a token has the magic value "ALL" the match always succeeds. Return 1892198Sguido * YES if the token fully matches the string. If the token is a domain 1902198Sguido * name, return YES if it matches the last fields of the string. If the 1912198Sguido * token has the magic value "LOCAL", return YES if the string does not 1922198Sguido * contain a "." character. If the token is a network number, return YES 1932198Sguido * if it matches the head of the string. 1942198Sguido */ 1952198Sguido 1962198Sguido if (tok[0] == '@') { /* netgroup */ 1972198Sguido return (netgroup_match(tok + 1, string, (char *) 0)); 1982198Sguido } else if (string_match(tok, string)) { /* ALL or exact match */ 1992198Sguido return (YES); 2002198Sguido } else if (tok[0] == '.') { /* domain: match last fields */ 2012198Sguido if ((str_len = strlen(string)) > (tok_len = strlen(tok)) 2022198Sguido && strcasecmp(tok, string + str_len - tok_len) == 0) 2032198Sguido return (YES); 2042198Sguido } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */ 2052198Sguido if (strchr(string, '.') == 0) 2062198Sguido return (YES); 2072198Sguido } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */ 2082198Sguido && strncmp(tok, string, tok_len) == 0) { 2092198Sguido return (YES); 2102198Sguido } 2112198Sguido return (NO); 2122198Sguido} 2132198Sguido 2142198Sguido/* string_match - match a string against one token */ 2152198Sguido 21690145Smarkmstatic int 21790145Smarkmstring_match(const char *tok, const char *string) 2182198Sguido{ 2192198Sguido 2202198Sguido /* 2212198Sguido * If the token has the magic value "ALL" the match always succeeds. 2222198Sguido * Otherwise, return YES if the token fully matches the string. 2232198Sguido */ 2242198Sguido 2252198Sguido if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */ 2262198Sguido return (YES); 2272198Sguido } else if (strcasecmp(tok, string) == 0) { /* try exact match */ 2282198Sguido return (YES); 2292198Sguido } 2302198Sguido return (NO); 2312198Sguido} 232