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