2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 09:57:41 +00:00
sudo/testsudoers.c

438 lines
9.8 KiB
C
Raw Normal View History

1995-04-13 16:34:54 +00:00
/*
* Copyright (c) 1996, 1998-2001 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
1995-04-13 16:34:54 +00:00
*
* This code is derived from software contributed by Chris Jepeway.
1995-04-13 16:34:54 +00:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
1995-04-13 16:34:54 +00:00
*
* 4. Products derived from this software may not be called "Sudo" nor
* may "Sudo" appear in their names without specific prior written
* permission from the author.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1995-04-13 16:34:54 +00:00
*/
#include "config.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
1995-04-13 16:34:54 +00:00
#include <stdio.h>
1998-06-24 16:22:26 +00:00
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
1998-06-24 16:22:26 +00:00
#endif /* STDC_HEADERS */
1995-04-13 16:34:54 +00:00
#ifdef HAVE_STRING_H
# include <string.h>
#else
# ifdef HAVE_STRINGS_H
# include <strings.h>
# endif
1995-04-13 16:34:54 +00:00
#endif /* HAVE_STRING_H */
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_FNMATCH
# include <fnmatch.h>
1996-05-28 00:00:17 +00:00
#endif /* HAVE_FNMATCH_H */
1996-06-20 05:16:36 +00:00
#ifdef HAVE_NETGROUP_H
# include <netgroup.h>
1996-06-20 05:16:36 +00:00
#endif /* HAVE_NETGROUP_H */
1995-04-13 16:34:54 +00:00
#include <ctype.h>
1995-11-21 02:50:43 +00:00
#include <pwd.h>
1995-12-18 03:04:55 +00:00
#include <grp.h>
1995-04-13 16:34:54 +00:00
#include <netinet/in.h>
1999-04-07 06:59:59 +00:00
#include <arpa/inet.h>
1995-04-13 16:34:54 +00:00
#include <netdb.h>
#include <dirent.h>
#include "sudo.h"
#include "parse.h"
#include "interfaces.h"
1995-04-13 16:34:54 +00:00
#ifndef HAVE_FNMATCH
# include "emul/fnmatch.h"
#endif /* HAVE_FNMATCH */
1998-11-18 04:16:13 +00:00
#ifndef lint
1999-01-17 23:16:20 +00:00
static const char rcsid[] = "$Sudo$";
1998-11-18 04:16:13 +00:00
#endif /* lint */
/*
* Prototypes
*/
void init_parser __P((void));
void dumpaliases __P((void));
void set_perms_dummy __P((int));
1995-04-13 16:34:54 +00:00
/*
* Globals
*/
char **Argv, **NewArgv;
int Argc, NewArgc;
1995-04-13 16:34:54 +00:00
int parse_error = FALSE;
int num_interfaces;
struct interface *interfaces;
struct sudo_user sudo_user;
void (*set_perms) __P((int)) = set_perms_dummy;
extern int clearaliases;
extern int pedantic;
1995-04-13 16:34:54 +00:00
1999-04-07 06:59:59 +00:00
/*
* Returns TRUE if "s" has shell meta characters in it,
* else returns FALSE.
*/
int
has_meta(s)
1999-04-07 06:59:59 +00:00
char *s;
{
2000-03-22 15:53:09 +00:00
char *t;
1999-04-07 06:59:59 +00:00
for (t = s; *t; t++) {
if (*t == '\\' || *t == '?' || *t == '*' || *t == '[' || *t == ']')
return(TRUE);
}
return(FALSE);
}
1995-04-13 16:34:54 +00:00
/*
* Returns TRUE if cmnd matches, in the sudo sense,
1995-04-13 16:34:54 +00:00
* the pathname in path; otherwise, return FALSE
*/
int
command_matches(cmnd, cmnd_args, path, sudoers_args)
1996-02-05 03:48:34 +00:00
char *cmnd;
char *cmnd_args;
1996-02-05 03:48:34 +00:00
char *path;
char *sudoers_args;
1995-04-13 16:34:54 +00:00
{
int clen, plen;
char *args;
1995-04-13 16:34:54 +00:00
if (cmnd == NULL)
return(FALSE);
if ((args = strchr(path, ' ')))
*args++ = '\0';
1995-04-13 16:34:54 +00:00
1996-05-28 00:00:17 +00:00
if (has_meta(path)) {
if (fnmatch(path, cmnd, FNM_PATHNAME))
return(FALSE);
if (!sudoers_args)
return(TRUE);
else if (!cmnd_args && sudoers_args && !strcmp("\"\"", sudoers_args))
1996-05-28 00:00:17 +00:00
return(TRUE);
else if (sudoers_args)
return((fnmatch(sudoers_args, cmnd_args ? cmnd_args : "", 0) == 0));
1996-05-28 00:00:17 +00:00
else
return(FALSE);
} else {
plen = strlen(path);
if (path[plen - 1] != '/') {
if (strcmp(cmnd, path))
return(FALSE);
if (!sudoers_args)
return(TRUE);
else if (!cmnd_args && sudoers_args && !strcmp("\"\"", sudoers_args))
1996-05-28 00:00:17 +00:00
return(TRUE);
else if (sudoers_args)
return((fnmatch(sudoers_args, cmnd_args ? cmnd_args : "", 0) == 0));
1996-05-28 00:00:17 +00:00
else
return(FALSE);
1996-05-28 00:00:17 +00:00
}
1995-04-13 16:34:54 +00:00
1996-05-28 00:00:17 +00:00
clen = strlen(cmnd);
if (clen < plen + 1)
/* path cannot be the parent dir of cmnd */
return(FALSE);
1995-04-13 16:34:54 +00:00
1996-05-28 00:00:17 +00:00
if (strchr(cmnd + plen + 1, '/') != NULL)
/* path could only be an anscestor of cmnd -- */
/* ignoring, of course, things like // & /./ */
return(FALSE);
1995-04-13 16:34:54 +00:00
1996-05-28 00:00:17 +00:00
/* see whether path is the prefix of cmnd */
return((strncmp(cmnd, path, plen) == 0));
}
1995-04-13 16:34:54 +00:00
}
int
addr_matches(n)
char *n;
1995-04-13 16:34:54 +00:00
{
int i;
1996-08-17 23:17:23 +00:00
char *m;
struct in_addr addr, mask;
1999-08-24 16:42:28 +00:00
/* If there's an explicit netmask, use it. */
1996-08-17 23:17:23 +00:00
if ((m = strchr(n, '/'))) {
*m++ = '\0';
addr.s_addr = inet_addr(n);
1999-08-24 16:42:28 +00:00
if (strchr(m, '.'))
mask.s_addr = inet_addr(m);
2001-12-15 02:27:17 +00:00
else {
i = 32 - atoi(m);
mask.s_addr = 0xffffffff;
mask.s_addr >>= i;
mask.s_addr <<= i;
mask.s_addr = htonl(mask.s_addr);
}
1996-08-17 23:17:23 +00:00
*(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);
1995-04-13 16:34:54 +00:00
1996-08-17 23:17:23 +00:00
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);
}
1995-04-13 16:34:54 +00:00
return(FALSE);
1995-04-13 16:34:54 +00:00
}
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)
1995-12-18 03:04:55 +00:00
char *group;
char *user;
{
struct group *grp;
1995-12-18 03:04:55 +00:00
char **cur;
/* Make sure we have a valid usergroup, sudo style. */
1995-12-18 03:04:55 +00:00
if (*group++ != '%')
return(FALSE);
if ((grp = getgrnam(group)) == NULL)
1995-12-18 03:04:55 +00:00
return(FALSE);
/*
* Check against user's real gid as well as group's user list
*/
if (getgid() == grp->gr_gid)
1995-12-18 03:04:55 +00:00
return(TRUE);
for (cur=grp->gr_mem; *cur; cur++) {
1995-12-18 03:04:55 +00:00
if (strcmp(*cur, user) == 0)
return(TRUE);
}
return(FALSE);
}
int
netgr_matches(netgr, host, shost, user)
1995-07-27 02:00:21 +00:00
char *netgr;
char *host;
char *shost;
1995-07-27 02:00:21 +00:00
char *user;
{
#ifdef HAVE_GETDOMAINNAME
static char *domain = (char *) -1;
#else
static char *domain = NULL;
#endif /* HAVE_GETDOMAINNAME */
/* Make sure we have a valid netgroup, sudo style. */
1995-12-18 03:04:55 +00:00
if (*netgr++ != '+')
1995-07-27 02:00:21 +00:00
return(FALSE);
#ifdef HAVE_GETDOMAINNAME
/* Get the domain name (if any). */
1995-07-27 02:00:21 +00:00
if (domain == (char *) -1) {
1999-03-29 02:59:34 +00:00
domain = (char *) emalloc(MAXHOSTNAMELEN);
1995-07-27 02:00:21 +00:00
1998-04-06 03:15:39 +00:00
if (getdomainname(domain, MAXHOSTNAMELEN) != 0 || *domain == '\0') {
free(domain);
1995-07-27 02:00:21 +00:00
domain = NULL;
}
}
#endif /* HAVE_GETDOMAINNAME */
#ifdef HAVE_INNETGR
if (innetgr(netgr, host, user, domain))
return(TRUE);
else if (host != shost && innetgr(netgr, shost, user, domain))
return(TRUE);
1995-07-27 02:00:21 +00:00
#endif /* HAVE_INNETGR */
return(FALSE);
1995-07-27 02:00:21 +00:00
}
void
set_perms_dummy(i)
int i;
1995-04-13 16:34:54 +00:00
{
return;
}
1999-12-05 02:18:17 +00:00
void
set_fqdn()
{
return;
}
2001-12-11 22:55:23 +00:00
void
init_envtables()
{
return;
}
int
main(argc, argv)
int argc;
char **argv;
1995-04-13 16:34:54 +00:00
{
struct passwd pw;
1996-03-17 20:08:56 +00:00
char *p;
1995-04-13 16:34:54 +00:00
#ifdef YYDEBUG
extern int yydebug;
yydebug = 1;
#endif
Argv = argv;
Argc = argc;
if (Argc >= 6 && strcmp(Argv[1], "-u") == 0) {
user_runas = &Argv[2];
pw.pw_name = Argv[3];
user_host = Argv[4];
user_cmnd = Argv[5];
NewArgv = &Argv[5];
NewArgc = Argc - 5;
} else if (Argc >= 4) {
pw.pw_name = Argv[1];
user_host = Argv[2];
user_cmnd = Argv[3];
NewArgv = &Argv[3];
NewArgc = Argc - 3;
1996-07-08 18:16:45 +00:00
} else {
(void) fprintf(stderr,
"usage: %s [-u user] <user> <host> <command> [args]\n", Argv[0]);
exit(1);
1996-07-08 18:16:45 +00:00
}
sudo_user.pw = &pw; /* user_name needs to be defined */
if ((p = strchr(user_host, '.'))) {
1996-03-17 20:08:56 +00:00
*p = '\0';
user_shost = estrdup(user_host);
1996-03-17 20:08:56 +00:00
*p = '.';
} else {
user_shost = user_host;
1996-03-17 20:08:56 +00:00
}
1995-04-13 16:34:54 +00:00
/* Fill in cmnd_args from NewArgv. */
if (NewArgc > 1) {
char *to, **from;
size_t size, n;
size = (size_t) (NewArgv[NewArgc-1] - NewArgv[1]) +
strlen(NewArgv[NewArgc-1]) + 1;
user_args = (char *) emalloc(size);
for (to = user_args, from = NewArgv + 1; *from; from++) {
n = strlcpy(to, *from, size - (to - user_args));
if (n >= size) {
(void) fprintf(stderr,
"%s: internal error, init_vars() overflow\n", Argv[0]);
exit(1);
}
to += n;
*to++ = ' ';
}
*--to = '\0';
}
/* Initialize default values. */
init_defaults();
/* Warn about aliases that are used before being defined. */
pedantic = TRUE;
/* Need to keep aliases around for dumpaliases(). */
clearaliases = FALSE;
/* Load ip addr/mask for each interface. */
load_interfaces();
/* Allocate space for data structures in the parser. */
1996-08-17 23:17:23 +00:00
init_parser();
if (yyparse() || parse_error) {
1995-06-15 03:41:25 +00:00
(void) printf("doesn't parse.\n");
} else {
1995-06-15 03:41:25 +00:00
(void) printf("parses OK.\n\n");
1995-04-13 16:34:54 +00:00
if (top == 0)
(void) printf("User %s not found\n", pw.pw_name);
1995-04-13 16:34:54 +00:00
else while (top) {
1995-06-15 03:41:25 +00:00
(void) printf("[%d]\n", top-1);
(void) printf("user_match : %d\n", user_matches);
(void) printf("host_match : %d\n", host_matches);
(void) printf("cmnd_match : %d\n", cmnd_matches);
(void) printf("no_passwd : %d\n", no_passwd);
(void) printf("runas_match: %d\n", runas_matches);
(void) printf("runas : %s\n", *user_runas);
1995-04-13 16:34:54 +00:00
top--;
}
}
/* Dump aliases. */
1995-06-15 03:41:25 +00:00
(void) printf("Matching Aliases --\n");
1995-04-13 16:34:54 +00:00
dumpaliases();
1996-08-17 23:17:23 +00:00
exit(0);
1995-04-13 16:34:54 +00:00
}