#include "config.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#else
# ifdef HAVE_STRINGS_H
# include <strings.h>
# endif
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_FNMATCH
# include <fnmatch.h>
#endif
#ifdef HAVE_NETGROUP_H
# include <netgroup.h>
#endif
#include <ctype.h>
#include <pwd.h>
#include <grp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif
#endif
#include "sudo.h"
#include "parse.h"
#include "interfaces.h"
#ifndef HAVE_FNMATCH
# include "emul/fnmatch.h"
#endif
#ifndef lint
static const char rcsid[] = "$Sudo: parse.c,v 1.137 2002/03/16 00:44:47 millert Exp $";
#endif
int parse_error = FALSE;
extern int keepall;
extern FILE *yyin, *yyout;
static int has_meta __P((char *));
void init_parser __P((void));
int
sudoers_lookup(pwflag)
int pwflag;
{
int error;
int pwcheck;
int nopass;
set_perms(PERM_SUDOERS, 0);
rewind(sudoers_fp);
yyin = sudoers_fp;
yyout = stdout;
init_parser();
if (pwflag > 0)
keepall = TRUE;
set_perms(PERM_ROOT, 0);
error = yyparse();
(void) fclose(sudoers_fp);
sudoers_fp = NULL;
if (error || parse_error)
return(VALIDATE_ERROR);
if (pwflag)
pwcheck = (pwflag == -1) ? PWCHECK_NEVER : def_ival(pwflag);
else
pwcheck = 0;
if (def_flag(I_AUTHENTICATE))
error = VALIDATE_NOT_OK;
else
error = VALIDATE_NOT_OK | FLAG_NOPASS;
if (pwcheck) {
error |= FLAG_NO_CHECK;
} else {
error |= FLAG_NO_HOST;
if (!top)
error |= FLAG_NO_USER;
}
nopass = -1;
if (pwcheck) {
int found;
if (pwcheck == PWCHECK_NEVER || !def_flag(I_AUTHENTICATE))
nopass = FLAG_NOPASS;
found = 0;
while (top) {
if (host_matches == TRUE) {
found = 1;
if (pwcheck == PWCHECK_ANY && no_passwd == TRUE)
nopass = FLAG_NOPASS;
else if (pwcheck == PWCHECK_ALL && nopass != 0)
nopass = (no_passwd == TRUE) ? FLAG_NOPASS : 0;
}
top--;
}
if (found) {
if (nopass == -1)
nopass = 0;
return(VALIDATE_OK | nopass);
}
} else {
while (top) {
if (host_matches == TRUE) {
error &= ~FLAG_NO_HOST;
if (runas_matches == TRUE) {
if (cmnd_matches == TRUE) {
if (no_passwd == TRUE)
return(VALIDATE_OK | FLAG_NOPASS);
else
return(VALIDATE_OK);
} else if (cmnd_matches == FALSE) {
if (no_passwd == TRUE)
return(VALIDATE_NOT_OK | FLAG_NOPASS);
else
return(VALIDATE_NOT_OK);
}
}
}
top--;
}
}
if (nopass == -1)
nopass = 0;
return(error | nopass);
}
int
command_matches(cmnd, cmnd_args, path, sudoers_args)
char *cmnd;
char *cmnd_args;
char *path;
char *sudoers_args;
{
int plen;
static struct stat cst;
struct stat pst;
DIR *dirp;
struct dirent *dent;
char buf[MAXPATHLEN];
static char *cmnd_base;
if (strchr(cmnd, '/') == NULL)
return(FALSE);
plen = strlen(path);
if (cst.st_dev == 0) {
if (stat(cmnd, &cst) == -1)
return(FALSE);
if ((cmnd_base = strrchr(cmnd, '/')) == NULL)
cmnd_base = cmnd;
else
cmnd_base++;
}
if (has_meta(path)) {
if (fnmatch(path, cmnd, FNM_PATHNAME) != 0)
return(FALSE);
if (!sudoers_args ||
(!cmnd_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
(sudoers_args && fnmatch(sudoers_args, cmnd_args ? cmnd_args : "",
0) == 0)) {
if (safe_cmnd)
free(safe_cmnd);
safe_cmnd = estrdup(user_cmnd);
return(TRUE);
} else
return(FALSE);
} else {
if (path[plen - 1] != '/') {
char *p;
if ((p = strrchr(path, '/')) == NULL)
p = path;
else
p++;
if (strcmp(cmnd_base, p) != 0 || stat(path, &pst) == -1)
return(FALSE);
if (cst.st_dev != pst.st_dev || cst.st_ino != pst.st_ino)
return(FALSE);
if (!sudoers_args ||
(!cmnd_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
(sudoers_args &&
fnmatch(sudoers_args, cmnd_args ? cmnd_args : "", 0) == 0)) {
if (safe_cmnd)
free(safe_cmnd);
safe_cmnd = estrdup(path);
return(TRUE);
} else
return(FALSE);
}
dirp = opendir(path);
if (dirp == NULL)
return(FALSE);
while ((dent = readdir(dirp)) != NULL) {
if (plen + NAMLEN(dent) >= sizeof(buf))
continue;
strcpy(buf, path);
strcat(buf, dent->d_name);
if (strcmp(cmnd_base, dent->d_name) != 0 || stat(buf, &pst) == -1)
continue;
if (cst.st_dev == pst.st_dev && cst.st_ino == pst.st_ino) {
if (safe_cmnd)
free(safe_cmnd);
safe_cmnd = estrdup(buf);
break;
}
}
closedir(dirp);
return(dent != NULL);
}
}
int
addr_matches(n)
char *n;
{
int i;
char *m;
struct in_addr addr, mask;
if ((m = strchr(n, '/'))) {
*m++ = '\0';
addr.s_addr = inet_addr(n);
if (strchr(m, '.'))
mask.s_addr = inet_addr(m);
else {
i = 32 - atoi(m);
mask.s_addr = 0xffffffff;
mask.s_addr >>= i;
mask.s_addr <<= i;
mask.s_addr = htonl(mask.s_addr);
}
*(m - 1) = '/';
for (i = 0; i < num_interfaces; i++)
if ((interfaces[i].addr.s_addr & mask.s_addr) == addr.s_addr)
return(TRUE);
} else {
addr.s_addr = inet_addr(n);
for (i = 0; i < num_interfaces; i++)
if (interfaces[i].addr.s_addr == addr.s_addr ||
(interfaces[i].addr.s_addr & interfaces[i].netmask.s_addr)
== addr.s_addr)
return(TRUE);
}
return(FALSE);
}
int
hostname_matches(shost, lhost, pattern)
char *shost;
char *lhost;
char *pattern;
{
if (has_meta(pattern)) {
if (strchr(pattern, '.'))
return(fnmatch(pattern, lhost, FNM_CASEFOLD));
else
return(fnmatch(pattern, shost, FNM_CASEFOLD));
} else {
if (strchr(pattern, '.'))
return(strcasecmp(lhost, pattern));
else
return(strcasecmp(shost, pattern));
}
}
int
usergr_matches(group, user)
char *group;
char *user;
{
struct group *grp;
struct passwd *pw;
char **cur;
if (*group++ != '%')
return(FALSE);
if ((grp = getgrnam(group)) == NULL)
return(FALSE);
if ((pw = getpwnam(user)) == NULL)
return(FALSE);
if (grp->gr_gid == pw->pw_gid)
return(TRUE);
for (cur=grp->gr_mem; *cur; cur++) {
if (strcmp(*cur, user) == 0)
return(TRUE);
}
return(FALSE);
}
int
netgr_matches(netgr, host, shost, user)
char *netgr;
char *host;
char *shost;
char *user;
{
#ifdef HAVE_GETDOMAINNAME
static char *domain = (char *) -1;
#else
static char *domain = NULL;
#endif
if (*netgr++ != '+')
return(FALSE);
#ifdef HAVE_GETDOMAINNAME
if (domain == (char *) -1) {
domain = (char *) emalloc(MAXHOSTNAMELEN);
if (getdomainname(domain, MAXHOSTNAMELEN) == -1 || *domain == '\0') {
free(domain);
domain = NULL;
}
}
#endif
#ifdef HAVE_INNETGR
if (innetgr(netgr, host, user, domain))
return(TRUE);
else if (host != shost && innetgr(netgr, shost, user, domain))
return(TRUE);
#endif
return(FALSE);
}
static int
has_meta(s)
char *s;
{
char *t;
for (t = s; *t; t++) {
if (*t == '\\' || *t == '?' || *t == '*' || *t == '[' || *t == ']')
return(TRUE);
}
return(FALSE);
}