Path: blob/main/crypto/heimdal/appl/rsh/login_access.c
96309 views
/************************************************************************1* Copyright 1995 by Wietse Venema. All rights reserved. Some individual2* files may be covered by other copyrights.3*4* This material was originally written and compiled by Wietse Venema at5* Eindhoven University of Technology, The Netherlands, in 1990, 1991,6* 1992, 1993, 1994 and 1995.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that this entire copyright notice10* is duplicated in all such copies.11*12* This software is provided "as is" and without any expressed or implied13* warranties, including, without limitation, the implied warranties of14* merchantibility and fitness for any particular purpose.15************************************************************************/16/*17* This module implements a simple but effective form of login access18* control based on login names and on host (or domain) names, internet19* addresses (or network numbers), or on terminal line names in case of20* non-networked logins. Diagnostics are reported through syslog(3).21*22* Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.23*/2425#include "login_locl.h"2627RCSID("$Id$");2829/* Delimiters for fields and for lists of users, ttys or hosts. */3031static char fs[] = ":"; /* field separator */32static char sep[] = ", \t"; /* list-element separator */3334/* Constants to be used in assignments only, not in comparisons... */3536#define YES 137#define NO 03839/*40* A structure to bundle up all login-related information to keep the41* functional interfaces as generic as possible.42*/43struct login_info {44struct passwd *user;45char *from;46};4748static int list_match(char *list, struct login_info *item,49int (*match_fn)(char *, struct login_info *));50static int user_match(char *tok, struct login_info *item);51static int from_match(char *tok, struct login_info *item);52static int string_match(char *tok, char *string);5354/* login_access - match username/group and host/tty with access control file */5556int login_access(struct passwd *user, char *from)57{58struct login_info item;59FILE *fp;60char line[BUFSIZ];61char *perm; /* becomes permission field */62char *users; /* becomes list of login names */63char *froms; /* becomes list of terminals or hosts */64int match = NO;65int end;66int lineno = 0; /* for diagnostics */67char *foo;6869/*70* Bundle up the arguments to avoid unnecessary clumsiness lateron.71*/72item.user = user;73item.from = from;7475/*76* Process the table one line at a time and stop at the first match.77* Blank lines and lines that begin with a '#' character are ignored.78* Non-comment lines are broken at the ':' character. All fields are79* mandatory. The first field should be a "+" or "-" character. A80* non-existing table means no access control.81*/8283if ((fp = fopen(_PATH_LOGACCESS, "r")) != 0) {84while (!match && fgets(line, sizeof(line), fp)) {85lineno++;86if (line[end = strlen(line) - 1] != '\n') {87syslog(LOG_ERR, "%s: line %d: missing newline or line too long",88_PATH_LOGACCESS, lineno);89continue;90}91if (line[0] == '#')92continue; /* comment line */93while (end > 0 && isspace((unsigned char)line[end - 1]))94end--;95line[end] = 0; /* strip trailing whitespace */96if (line[0] == 0) /* skip blank lines */97continue;98foo = NULL;99if (!(perm = strtok_r(line, fs, &foo))100|| !(users = strtok_r(NULL, fs, &foo))101|| !(froms = strtok_r(NULL, fs, &foo))102|| strtok_r(NULL, fs, &foo)) {103syslog(LOG_ERR, "%s: line %d: bad field count",104_PATH_LOGACCESS,105lineno);106continue;107}108if (perm[0] != '+' && perm[0] != '-') {109syslog(LOG_ERR, "%s: line %d: bad first field",110_PATH_LOGACCESS,111lineno);112continue;113}114match = (list_match(froms, &item, from_match)115&& list_match(users, &item, user_match));116}117fclose(fp);118} else if (errno != ENOENT) {119syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS);120}121return (match == 0 || (line[0] == '+'));122}123124/* list_match - match an item against a list of tokens with exceptions */125126static int127list_match(char *list,128struct login_info *item,129int (*match_fn)(char *, struct login_info *))130{131char *tok;132int match = NO;133char *foo = NULL;134135/*136* Process tokens one at a time. We have exhausted all possible matches137* when we reach an "EXCEPT" token or the end of the list. If we do find138* a match, look for an "EXCEPT" list and recurse to determine whether139* the match is affected by any exceptions.140*/141142for (tok = strtok_r(list, sep, &foo);143tok != NULL;144tok = strtok_r(NULL, sep, &foo)) {145if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */146break;147if ((match = (*match_fn) (tok, item)) != 0) /* YES */148break;149}150/* Process exceptions to matches. */151152if (match != NO) {153while ((tok = strtok_r(NULL, sep, &foo)) && strcasecmp(tok, "EXCEPT"))154/* VOID */ ;155if (tok == 0 || list_match(NULL, item, match_fn) == NO)156return (match);157}158return (NO);159}160161/* myhostname - figure out local machine name */162163static char *myhostname(void)164{165static char name[MAXHOSTNAMELEN + 1] = "";166167if (name[0] == 0) {168gethostname(name, sizeof(name));169name[MAXHOSTNAMELEN] = 0;170}171return (name);172}173174/* netgroup_match - match group against machine or user */175176static int netgroup_match(char *group, char *machine, char *user)177{178#ifdef HAVE_YP_GET_DEFAULT_DOMAIN179static char *mydomain = 0;180181if (mydomain == 0)182yp_get_default_domain(&mydomain);183return (innetgr(group, machine, user, mydomain));184#else185syslog(LOG_ERR, "NIS netgroup support not configured");186return 0;187#endif188}189190/* user_match - match a username against one token */191192static int user_match(char *tok, struct login_info *item)193{194char *string = item->user->pw_name;195struct login_info fake_item;196struct group *group;197int i;198char *at;199200/*201* If a token has the magic value "ALL" the match always succeeds.202* Otherwise, return YES if the token fully matches the username, if the203* token is a group that contains the username, or if the token is the204* name of the user's primary group.205*/206207if ((at = strchr(tok + 1, '@')) != 0) { /* split user@host pattern */208*at = 0;209fake_item.from = myhostname();210return (user_match(tok, item) && from_match(at + 1, &fake_item));211} else if (tok[0] == '@') { /* netgroup */212return (netgroup_match(tok + 1, (char *) 0, string));213} else if (string_match(tok, string)) { /* ALL or exact match */214return (YES);215} else if ((group = getgrnam(tok)) != 0) { /* try group membership */216if (item->user->pw_gid == group->gr_gid)217return (YES);218for (i = 0; group->gr_mem[i]; i++)219if (strcasecmp(string, group->gr_mem[i]) == 0)220return (YES);221}222return (NO);223}224225/* from_match - match a host or tty against a list of tokens */226227static int from_match(char *tok, struct login_info *item)228{229char *string = item->from;230int tok_len;231int str_len;232233/*234* If a token has the magic value "ALL" the match always succeeds. Return235* YES if the token fully matches the string. If the token is a domain236* name, return YES if it matches the last fields of the string. If the237* token has the magic value "LOCAL", return YES if the string does not238* contain a "." character. If the token is a network number, return YES239* if it matches the head of the string.240*/241242if (tok[0] == '@') { /* netgroup */243return (netgroup_match(tok + 1, string, (char *) 0));244} else if (string_match(tok, string)) { /* ALL or exact match */245return (YES);246} else if (tok[0] == '.') { /* domain: match last fields */247if ((str_len = strlen(string)) > (tok_len = strlen(tok))248&& strcasecmp(tok, string + str_len - tok_len) == 0)249return (YES);250} else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */251if (strchr(string, '.') == 0)252return (YES);253} else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */254&& strncmp(tok, string, tok_len) == 0) {255return (YES);256}257return (NO);258}259260/* string_match - match a string against one token */261262static int string_match(char *tok, char *string)263{264265/*266* If the token has the magic value "ALL" the match always succeeds.267* Otherwise, return YES if the token fully matches the string.268*/269270if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */271return (YES);272} else if (strcasecmp(tok, string) == 0) { /* try exact match */273return (YES);274}275return (NO);276}277278279