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: stable/11/lib/libpam/modules/pam_login_access/login_access.c 369345 2021-02-24 01:39:53Z markj $"); 1887628Sdwmalone 1987177Smarkm#include <sys/types.h> 20359116Scy#include <sys/param.h> 212198Sguido#include <ctype.h> 2287177Smarkm#include <errno.h> 232198Sguido#include <grp.h> 24169976Sdes#include <netdb.h> 25359116Scy#include <pwd.h> 2687177Smarkm#include <stdio.h> 2787177Smarkm#include <stdlib.h> 282198Sguido#include <string.h> 2987177Smarkm#include <syslog.h> 302198Sguido#include <unistd.h> 312198Sguido 3290145Smarkm#include "pam_login_access.h" 3390145Smarkm 342198Sguido /* Constants to be used in assignments only, not in comparisons... */ 352198Sguido 362198Sguido#define YES 1 372198Sguido#define NO 0 382198Sguido 39359117Scystatic int from_match(const char *, const char *, struct pam_login_access_options *); 4090145Smarkmstatic int list_match(char *, const char *, 41359117Scy int (*)(const char *, const char *, 42359117Scy struct pam_login_access_options *), 43359117Scy struct pam_login_access_options *); 4490145Smarkmstatic int netgroup_match(const char *, const char *, const char *); 4590145Smarkmstatic int string_match(const char *, const char *); 46359117Scystatic int user_match(const char *, const char *, struct pam_login_access_options *); 47359117Scystatic int group_match(const char *, const char *); 482198Sguido 492198Sguido/* login_access - match username/group and host/tty with access control file */ 502198Sguido 5122230Spstint 52359117Scylogin_access(const char *user, const char *from, 53359117Scy struct pam_login_access_options *login_access_opts) 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 */ 63359117Scy const char *fieldsep = login_access_opts->fieldsep; 642198Sguido 652198Sguido /* 662198Sguido * Process the table one line at a time and stop at the first match. 672198Sguido * Blank lines and lines that begin with a '#' character are ignored. 682198Sguido * Non-comment lines are broken at the ':' character. All fields are 692198Sguido * mandatory. The first field should be a "+" or "-" character. A 702198Sguido * non-existing table means no access control. 712198Sguido */ 722198Sguido 73359117Scy if ((fp = fopen(login_access_opts->accessfile, "r")) != NULL) { 742198Sguido while (!match && fgets(line, sizeof(line), fp)) { 752198Sguido lineno++; 762198Sguido if (line[end = strlen(line) - 1] != '\n') { 772198Sguido syslog(LOG_ERR, "%s: line %d: missing newline or line too long", 78359117Scy login_access_opts->accessfile, lineno); 792198Sguido continue; 802198Sguido } 812198Sguido if (line[0] == '#') 822198Sguido continue; /* comment line */ 832198Sguido while (end > 0 && isspace(line[end - 1])) 842198Sguido end--; 852198Sguido line[end] = 0; /* strip trailing whitespace */ 862198Sguido if (line[0] == 0) /* skip blank lines */ 872198Sguido continue; 88359117Scy if (!(perm = strtok(line, fieldsep)) 89359117Scy || !(users = strtok((char *) 0, fieldsep)) 90359117Scy || !(froms = strtok((char *) 0, fieldsep)) 91359117Scy || strtok((char *) 0, fieldsep)) { 92359117Scy syslog(LOG_ERR, "%s: line %d: bad field count", login_access_opts->accessfile, 932198Sguido lineno); 942198Sguido continue; 952198Sguido } 962198Sguido if (perm[0] != '+' && perm[0] != '-') { 97359117Scy syslog(LOG_ERR, "%s: line %d: bad first field", login_access_opts->accessfile, 982198Sguido lineno); 992198Sguido continue; 1002198Sguido } 101359117Scy match = (list_match(froms, from, from_match, login_access_opts) 102359117Scy && list_match(users, user, user_match, login_access_opts)); 1032198Sguido } 1042198Sguido (void) fclose(fp); 1052198Sguido } else if (errno != ENOENT) { 106359117Scy syslog(LOG_ERR, "cannot open %s: %m", login_access_opts->accessfile); 1072198Sguido } 1082198Sguido return (match == 0 || (line[0] == '+')); 1092198Sguido} 1102198Sguido 1112198Sguido/* list_match - match an item against a list of tokens with exceptions */ 1122198Sguido 11390145Smarkmstatic int 11490145Smarkmlist_match(char *list, const char *item, 115359117Scy int (*match_fn)(const char *, const char *, struct pam_login_access_options *), 116359117Scy struct pam_login_access_options *login_access_opts) 1172198Sguido{ 1182198Sguido char *tok; 1192198Sguido int match = NO; 120359117Scy const char *listsep = login_access_opts->listsep; 1212198Sguido 1222198Sguido /* 1232198Sguido * Process tokens one at a time. We have exhausted all possible matches 1242198Sguido * when we reach an "EXCEPT" token or the end of the list. If we do find 1252198Sguido * a match, look for an "EXCEPT" list and recurse to determine whether 1262198Sguido * the match is affected by any exceptions. 1272198Sguido */ 1282198Sguido 129359117Scy for (tok = strtok(list, listsep); tok != NULL; tok = strtok((char *) 0, listsep)) { 130359115Scy if (strcmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */ 1312198Sguido break; 132359117Scy if ((match = (*match_fn)(tok, item, login_access_opts)) != 0) /* YES */ 1332198Sguido break; 1342198Sguido } 1352198Sguido /* Process exceptions to matches. */ 1362198Sguido 1372198Sguido if (match != NO) { 138359117Scy while ((tok = strtok((char *) 0, listsep)) && strcmp(tok, "EXCEPT")) { 1392198Sguido /* VOID */ ; 140369345Smarkj } 141369345Smarkj if (tok == NULL || 142369345Smarkj list_match((char *) 0, item, match_fn, login_access_opts) == NO) { 143359117Scy return (match); 144359117Scy } 1452198Sguido } 1462198Sguido return (NO); 1472198Sguido} 1482198Sguido 1492198Sguido/* netgroup_match - match group against machine or user */ 1502198Sguido 15190145Smarkmstatic int 152169976Sdesnetgroup_match(const char *group, const char *machine, const char *user) 1532198Sguido{ 154169976Sdes char domain[1024]; 155169976Sdes unsigned int i; 156169976Sdes 157169976Sdes if (getdomainname(domain, sizeof(domain)) != 0 || *domain == '\0') { 158169976Sdes syslog(LOG_ERR, "NIS netgroup support disabled: no NIS domain"); 159169976Sdes return (NO); 160169976Sdes } 161169976Sdes 162169976Sdes /* getdomainname() does not reliably terminate the string */ 163169976Sdes for (i = 0; i < sizeof(domain); ++i) 164169976Sdes if (domain[i] == '\0') 165169976Sdes break; 166169976Sdes if (i == sizeof(domain)) { 167169976Sdes syslog(LOG_ERR, "NIS netgroup support disabled: invalid NIS domain"); 168169976Sdes return (NO); 169169976Sdes } 170169976Sdes 171169976Sdes if (innetgr(group, machine, user, domain) == 1) 172169976Sdes return (YES); 173169976Sdes return (NO); 1742198Sguido} 1752198Sguido 176359117Scy/* group_match - match a group against one token */ 1772198Sguido 178359117Scyint 179359117Scygroup_match(const char *tok, const char *username) 1802198Sguido{ 1812198Sguido struct group *group; 182359116Scy struct passwd *passwd; 183359116Scy gid_t *grouplist; 184359117Scy int i, ret, ngroups = NGROUPS; 1852198Sguido 186359117Scy if ((passwd = getpwnam(username)) == NULL) 187359117Scy return (NO); 188359117Scy errno = 0; 189359117Scy if ((group = getgrnam(tok)) == NULL) { 190359117Scy if (errno != 0) 191359117Scy syslog(LOG_ERR, "getgrnam() failed for %s: %s", username, strerror(errno)); 192359117Scy else 193359117Scy syslog(LOG_NOTICE, "group not found: %s", username); 194359117Scy return (NO); 195359117Scy } 196359117Scy if ((grouplist = calloc(ngroups, sizeof(gid_t))) == NULL) { 197359117Scy syslog(LOG_ERR, "cannot allocate memory for grouplist: %s", username); 198359117Scy return (NO); 199359117Scy } 200359117Scy ret = NO; 201359117Scy if (getgrouplist(username, passwd->pw_gid, grouplist, &ngroups) != 0) 202359117Scy syslog(LOG_ERR, "getgrouplist() failed for %s", username); 203359117Scy for (i = 0; i < ngroups; i++) 204359117Scy if (grouplist[i] == group->gr_gid) 205359117Scy ret = YES; 206359117Scy free(grouplist); 207359117Scy return (ret); 208359117Scy} 209359117Scy 210359117Scy/* user_match - match a username against one token */ 211359117Scy 212359117Scystatic int 213359117Scyuser_match(const char *tok, const char *string, 214359117Scy struct pam_login_access_options *login_access_opts) 215359117Scy{ 216359117Scy size_t stringlen; 217359117Scy char *grpstr; 218359117Scy int rc; 219359117Scy 2202198Sguido /* 2212198Sguido * If a token has the magic value "ALL" the match always succeeds. 2222198Sguido * Otherwise, return YES if the token fully matches the username, or if 2232198Sguido * the token is a group that contains the username. 2242198Sguido */ 2252198Sguido 2262198Sguido if (tok[0] == '@') { /* netgroup */ 2272198Sguido return (netgroup_match(tok + 1, (char *) 0, string)); 228359117Scy } else if (tok[0] == '(' && tok[(stringlen = strlen(&tok[1]))] == ')') { /* group */ 229359117Scy if ((grpstr = strndup(&tok[1], stringlen - 1)) == NULL) { 230359117Scy syslog(LOG_ERR, "cannot allocate memory for %s", string); 231359117Scy return (NO); 232359117Scy } 233359117Scy rc = group_match(grpstr, string); 234359117Scy free(grpstr); 235359117Scy return (rc); 2362198Sguido } else if (string_match(tok, string)) { /* ALL or exact match */ 2372198Sguido return (YES); 238359117Scy } else if (login_access_opts->defgroup == true) {/* try group membership */ 239359117Scy return (group_match(tok, string)); 2402198Sguido } 2412198Sguido return (NO); 2422198Sguido} 2432198Sguido 2442198Sguido/* from_match - match a host or tty against a list of tokens */ 2452198Sguido 24690145Smarkmstatic int 247359117Scyfrom_match(const char *tok, const char *string, 248359117Scy struct pam_login_access_options *login_access_opts __unused) 2492198Sguido{ 2502198Sguido int tok_len; 2512198Sguido int str_len; 2522198Sguido 2532198Sguido /* 2542198Sguido * If a token has the magic value "ALL" the match always succeeds. Return 2552198Sguido * YES if the token fully matches the string. If the token is a domain 2562198Sguido * name, return YES if it matches the last fields of the string. If the 2572198Sguido * token has the magic value "LOCAL", return YES if the string does not 2582198Sguido * contain a "." character. If the token is a network number, return YES 2592198Sguido * if it matches the head of the string. 2602198Sguido */ 2612198Sguido 2622198Sguido if (tok[0] == '@') { /* netgroup */ 2632198Sguido return (netgroup_match(tok + 1, string, (char *) 0)); 2642198Sguido } else if (string_match(tok, string)) { /* ALL or exact match */ 2652198Sguido return (YES); 2662198Sguido } else if (tok[0] == '.') { /* domain: match last fields */ 2672198Sguido if ((str_len = strlen(string)) > (tok_len = strlen(tok)) 2682198Sguido && strcasecmp(tok, string + str_len - tok_len) == 0) 2692198Sguido return (YES); 270359115Scy } else if (strcmp(tok, "LOCAL") == 0) { /* local: no dots */ 271358197Scy if (strchr(string, '.') == NULL) 2722198Sguido return (YES); 2732198Sguido } else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */ 2742198Sguido && strncmp(tok, string, tok_len) == 0) { 2752198Sguido return (YES); 2762198Sguido } 2772198Sguido return (NO); 2782198Sguido} 2792198Sguido 2802198Sguido/* string_match - match a string against one token */ 2812198Sguido 28290145Smarkmstatic int 28390145Smarkmstring_match(const char *tok, const char *string) 2842198Sguido{ 2852198Sguido 2862198Sguido /* 2872198Sguido * If the token has the magic value "ALL" the match always succeeds. 2882198Sguido * Otherwise, return YES if the token fully matches the string. 2892198Sguido */ 2902198Sguido 291359115Scy if (strcmp(tok, "ALL") == 0) { /* all: always matches */ 2922198Sguido return (YES); 2932198Sguido } else if (strcasecmp(tok, string) == 0) { /* try exact match */ 2942198Sguido return (YES); 2952198Sguido } 2962198Sguido return (NO); 2972198Sguido} 298