add login_access

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@6252 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
1999-05-17 22:40:05 +00:00
parent b53166b10e
commit 7ccd04a7e2
4 changed files with 297 additions and 16 deletions

View File

@@ -6,7 +6,7 @@ INCLUDES += $(INCLUDE_krb4)
bin_PROGRAMS = login bin_PROGRAMS = login
login_SOURCES = login.c osfc2.c read_string.c utmp_login.c utmpx_login.c \ login_SOURCES = login.c osfc2.c read_string.c utmp_login.c utmpx_login.c \
tty.c stty_default.c login_locl.h tty.c stty_default.c login_access.c login_locl.h
LDADD = $(LIB_kafs) \ LDADD = $(LIB_kafs) \
$(top_builddir)/lib/krb5/libkrb5.la \ $(top_builddir)/lib/krb5/libkrb5.la \

View File

@@ -289,19 +289,9 @@ struct getargs args[] = {
int nargs = sizeof(args) / sizeof(args[0]); int nargs = sizeof(args) / sizeof(args[0]);
static void static void
update_utmp(const char *username, const char *hostname) update_utmp(const char *username, const char *hostname,
char *tty, char *ttyn)
{ {
char *tty, *ttyn, ttname[32];
ttyn = ttyname(STDIN_FILENO);
if(ttyn == NULL){
snprintf(ttname, sizeof(ttname), "%s??", _PATH_TTY);
ttyn = ttname;
}
if((tty = strrchr(ttyn, '/')))
tty++;
else
tty = ttyn;
/* /*
* Update the utmp files, both BSD and SYSV style. * Update the utmp files, both BSD and SYSV style.
*/ */
@@ -330,14 +320,15 @@ checknologin(void)
static void static void
do_login(struct passwd *pwd) do_login(struct passwd *pwd, char *tty, char *ttyn)
{ {
int rootlogin = (pwd->pw_uid == 0); int rootlogin = (pwd->pw_uid == 0);
if(pwd->pw_uid != 0) if(pwd->pw_uid != 0)
checknologin(); checknologin();
update_utmp(pwd->pw_name, remote_host ? remote_host : ""); update_utmp(pwd->pw_name, remote_host ? remote_host : "",
tty, ttyn);
#ifdef HAVE_SETLOGIN #ifdef HAVE_SETLOGIN
if(setlogin(pwd->pw_name)){ if(setlogin(pwd->pw_name)){
warn("setlogin(%s)", pwd->pw_name); warn("setlogin(%s)", pwd->pw_name);
@@ -506,6 +497,9 @@ main(int argc, char **argv)
struct passwd *pwd; struct passwd *pwd;
char password[128]; char password[128];
int ret; int ret;
char ttname[32];
char *tty, *ttyn;
if(ask){ if(ask){
f_flag = r_flag = 0; f_flag = r_flag = 0;
ret = read_string("login: ", username, sizeof(username), 1); ret = read_string("login: ", username, sizeof(username), 1);
@@ -530,7 +524,27 @@ main(int argc, char **argv)
fprintf(stderr, "Login incorrect.\n"); fprintf(stderr, "Login incorrect.\n");
continue; continue;
} }
do_login(pwd); ttyn = ttyname(STDIN_FILENO);
if(ttyn == NULL){
snprintf(ttname, sizeof(ttname), "%s??", _PATH_TTY);
ttyn = ttname;
}
if((tty = strrchr(ttyn, '/')))
tty++;
else
tty = ttyn;
if (login_access (pwd, remote_host ? remote_host : tty)) {
fprintf(stderr, "Permission denied\n");
if (remote_host)
syslog(LOG_NOTICE, "%s LOGIN REFUSED FROM %s",
pwd->pw_name, remote_host);
else
syslog(LOG_NOTICE, "%s LOGIN REFUSED ON %s",
pwd->pw_name, tty);
exit (1);
}
do_login(pwd, tty, ttyn);
} }
exit(1); exit(1);
} }

261
appl/login/login_access.c Normal file
View File

@@ -0,0 +1,261 @@
/*
* This module implements a simple but effective form of login access
* control based on login names and on host (or domain) names, internet
* addresses (or network numbers), or on terminal line names in case of
* non-networked logins. Diagnostics are reported through syslog(3).
*
* Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
*/
#include "login_locl.h"
RCSID("$Id$");
/* Delimiters for fields and for lists of users, ttys or hosts. */
static char fs[] = ":"; /* field separator */
static char sep[] = ", \t"; /* list-element separator */
/* Constants to be used in assignments only, not in comparisons... */
#define YES 1
#define NO 0
/*
* A structure to bundle up all login-related information to keep the
* functional interfaces as generic as possible.
*/
struct login_info {
struct passwd *user;
char *from;
};
static int list_match(char *list, struct login_info *item,
int (*match_fn)(char *, struct login_info *));
static int user_match(char *tok, struct login_info *item);
static int from_match(char *tok, struct login_info *item);
static int string_match(char *tok, char *string);
/* login_access - match username/group and host/tty with access control file */
int login_access(struct passwd *user, char *from)
{
struct login_info item;
FILE *fp;
char line[BUFSIZ];
char *perm; /* becomes permission field */
char *users; /* becomes list of login names */
char *froms; /* becomes list of terminals or hosts */
int match = NO;
int end;
int lineno = 0; /* for diagnostics */
char *foo;
/*
* Bundle up the arguments to avoid unnecessary clumsiness lateron.
*/
item.user = user;
item.from = from;
/*
* Process the table one line at a time and stop at the first match.
* Blank lines and lines that begin with a '#' character are ignored.
* Non-comment lines are broken at the ':' character. All fields are
* mandatory. The first field should be a "+" or "-" character. A
* non-existing table means no access control.
*/
if ((fp = fopen(_PATH_LOGACCESS, "r")) != 0) {
while (!match && fgets(line, sizeof(line), fp)) {
lineno++;
if (line[end = strlen(line) - 1] != '\n') {
syslog(LOG_ERR, "%s: line %d: missing newline or line too long",
_PATH_LOGACCESS, lineno);
continue;
}
if (line[0] == '#')
continue; /* comment line */
while (end > 0 && isspace((unsigned char)line[end - 1]))
end--;
line[end] = 0; /* strip trailing whitespace */
if (line[0] == 0) /* skip blank lines */
continue;
foo = NULL;
if (!(perm = strtok_r(line, fs, &foo))
|| !(users = strtok_r(NULL, fs, &foo))
|| !(froms = strtok_r(NULL, fs, &foo))
|| strtok_r(NULL, fs, &foo)) {
syslog(LOG_ERR, "%s: line %d: bad field count",
_PATH_LOGACCESS,
lineno);
continue;
}
if (perm[0] != '+' && perm[0] != '-') {
syslog(LOG_ERR, "%s: line %d: bad first field",
_PATH_LOGACCESS,
lineno);
continue;
}
match = (list_match(froms, &item, from_match)
&& list_match(users, &item, user_match));
}
fclose(fp);
} else if (errno != ENOENT) {
syslog(LOG_ERR, "cannot open %s: %m", _PATH_LOGACCESS);
}
return (match == 0 || (line[0] == '+'));
}
/* list_match - match an item against a list of tokens with exceptions */
static int
list_match(char *list,
struct login_info *item,
int (*match_fn)(char *, struct login_info *))
{
char *tok;
int match = NO;
char *foo = NULL;
/*
* Process tokens one at a time. We have exhausted all possible matches
* when we reach an "EXCEPT" token or the end of the list. If we do find
* a match, look for an "EXCEPT" list and recurse to determine whether
* the match is affected by any exceptions.
*/
for (tok = strtok_r(list, sep, &foo);
tok != NULL;
tok = strtok_r(NULL, sep, &foo)) {
if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
break;
if ((match = (*match_fn) (tok, item)) != 0) /* YES */
break;
}
/* Process exceptions to matches. */
if (match != NO) {
while ((tok = strtok_r(NULL, sep, &foo)) && strcasecmp(tok, "EXCEPT"))
/* VOID */ ;
if (tok == 0 || list_match(NULL, item, match_fn) == NO)
return (match);
}
return (NO);
}
/* myhostname - figure out local machine name */
static char *myhostname(void)
{
static char name[MAXHOSTNAMELEN + 1] = "";
if (name[0] == 0) {
gethostname(name, sizeof(name));
name[MAXHOSTNAMELEN] = 0;
}
return (name);
}
/* netgroup_match - match group against machine or user */
static int netgroup_match(char *group, char *machine, char *user)
{
#ifdef HAVE_YP_GET_DEFAULT_DOMAIN
static char *mydomain = 0;
if (mydomain == 0)
yp_get_default_domain(&mydomain);
return (innetgr(group, machine, user, mydomain));
#else
syslog(LOG_ERR, "NIS netgroup support not configured");
return 0;
#endif
}
/* user_match - match a username against one token */
static int user_match(char *tok, struct login_info *item)
{
char *string = item->user->pw_name;
struct login_info fake_item;
struct group *group;
int i;
char *at;
/*
* If a token has the magic value "ALL" the match always succeeds.
* Otherwise, return YES if the token fully matches the username, if the
* token is a group that contains the username, or if the token is the
* name of the user's primary group.
*/
if ((at = strchr(tok + 1, '@')) != 0) { /* split user@host pattern */
*at = 0;
fake_item.from = myhostname();
return (user_match(tok, item) && from_match(at + 1, &fake_item));
} else if (tok[0] == '@') { /* netgroup */
return (netgroup_match(tok + 1, (char *) 0, string));
} else if (string_match(tok, string)) { /* ALL or exact match */
return (YES);
} else if ((group = getgrnam(tok)) != 0) { /* try group membership */
if (item->user->pw_gid == group->gr_gid)
return (YES);
for (i = 0; group->gr_mem[i]; i++)
if (strcasecmp(string, group->gr_mem[i]) == 0)
return (YES);
}
return (NO);
}
/* from_match - match a host or tty against a list of tokens */
static int from_match(char *tok, struct login_info *item)
{
char *string = item->from;
int tok_len;
int str_len;
/*
* If a token has the magic value "ALL" the match always succeeds. Return
* YES if the token fully matches the string. If the token is a domain
* name, return YES if it matches the last fields of the string. If the
* token has the magic value "LOCAL", return YES if the string does not
* contain a "." character. If the token is a network number, return YES
* if it matches the head of the string.
*/
if (tok[0] == '@') { /* netgroup */
return (netgroup_match(tok + 1, string, (char *) 0));
} else if (string_match(tok, string)) { /* ALL or exact match */
return (YES);
} else if (tok[0] == '.') { /* domain: match last fields */
if ((str_len = strlen(string)) > (tok_len = strlen(tok))
&& strcasecmp(tok, string + str_len - tok_len) == 0)
return (YES);
} else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
if (strchr(string, '.') == 0)
return (YES);
} else if (tok[(tok_len = strlen(tok)) - 1] == '.' /* network */
&& strncmp(tok, string, tok_len) == 0) {
return (YES);
}
return (NO);
}
/* string_match - match a string against one token */
static int string_match(char *tok, char *string)
{
/*
* If the token has the magic value "ALL" the match always succeeds.
* Otherwise, return YES if the token fully matches the string.
*/
if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */
return (YES);
} else if (strcasecmp(tok, string) == 0) { /* try exact match */
return (YES);
}
return (NO);
}

View File

@@ -111,6 +111,10 @@
#endif #endif
#endif #endif
#ifndef _PATH_LOGACCESS
#define _PATH_LOGACCESS "/etc/login.access"
#endif /* _PATH_LOGACCESS */
int do_osfc2_magic(uid_t); int do_osfc2_magic(uid_t);
char *clean_ttyname (char*); char *clean_ttyname (char*);
char *make_id (char*); char *make_id (char*);
@@ -120,4 +124,6 @@ void stty_default (void);
void utmp_login (char*, const char*, const char*); void utmp_login (char*, const char*, const char*);
int utmpx_login (char*, const char*, const char*); int utmpx_login (char*, const char*, const char*);
int login_access(struct passwd *user, char *from);
#endif /* __LOGIN_LOCL_H__ */ #endif /* __LOGIN_LOCL_H__ */