2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-31 22:35:10 +00:00

Rewrite compat/getprogname.c and add setprogname().

The progname is now passed to the plugin via the settings array.
This commit is contained in:
Todd C. Miller
2010-03-19 07:52:31 -04:00
parent 0dbf4362d9
commit eec5235d19
6 changed files with 42 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003-2005 Todd C. Miller <Todd.Miller@courtesan.com>
* Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -12,10 +12,6 @@
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Sponsored in part by the Defense Advanced Research Projects
* Agency (DARPA) and Air Force Research Laboratory, Air Force
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
*/
#include <stdio.h>
@@ -24,24 +20,24 @@
#include <config.h>
#include <compat.h>
static char *progname = "sudo";
void
setprogname(const char *name)
{
const char *base;
if ((base = strrchr(name, '/')) != NULL) {
base++;
} else {
base = name;
}
if (strcmp(progname, base) != 0)
progname = base;
}
const char *
getprogname(void)
{
#ifdef PIC
return("sudo");
#else
static const char *progname;
extern int Argc;
extern char **Argv;
if (progname == NULL) {
if (Argc < 0)
progname = "sudo";
else if ((progname = strrchr(Argv[0], '/')) != NULL)
progname++;
else
progname = Argv[0];
}
return(progname);
#endif
return progname;
}

View File

@@ -248,6 +248,7 @@ extern const char *__progname;
# define getprogname() (__progname)
# else
const char *getprogname(void);
void setprogname(const char *);
#endif /* HAVE___PROGNAME */
#endif /* !HAVE_GETPROGNAME */

View File

@@ -163,6 +163,11 @@ policy_open(unsigned int version, sudo_conv_t conversation,
if (strncmp(*ui, "runas_group=", sizeof("runas_group=") - 1) == 0) {
runas_group = *ui + sizeof("runas_group=") - 1;
}
#if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
if (strncmp(*ui, "progname=", sizeof("progname=") - 1) == 0) {
setprogname(*ui + sizeof("progname=") - 1);
}
#endif
}
if (runas_user != NULL) {
if ((pw = getpwnam(runas_user)) == NULL) {

View File

@@ -162,8 +162,6 @@ static int NewArgc;
static char **NewArgv;
/* XXX */
extern int Argc;
extern char **Argv;
extern char **environ;
/* error.c */
@@ -1228,6 +1226,12 @@ deserialize_info(char * const settings[], char * const user_info[])
continue;
}
#endif /* HAVE_BSD_AUTH_H */
#if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
if (MATCHES(*cur, "progname=")) {
setprogname(*cur + sizeof("progname=") - 1);
continue;
}
#endif
}
for (cur = user_info; *cur != NULL; cur++) {

View File

@@ -100,7 +100,9 @@ static struct sudo_settings {
{ "selinux_type" },
#define ARG_RUNAS_USER 11
{ "runas_user" },
#define NUM_SETTINGS 12
#define ARG_PROGNAME 12
{ "progname" },
#define NUM_SETTINGS 13
{ NULL }
};
@@ -125,6 +127,9 @@ parse_args(int argc, char **argv, int *nargc, char ***nargv, char ***settingsp,
env_add = emalloc2(env_size, sizeof(char *));
env_add[0] = NULL;
/* Pass progname to plugin so it can call setprogname() */
sudo_settings[ARG_PROGNAME].value = getprogname();
/* First, check to see if we were invoked as "sudoedit". */
if (strcmp(getprogname(), "sudoedit") == 0)
mode = MODE_EDIT;

View File

@@ -99,10 +99,6 @@ static int run_command(struct command_details *details, char *argv[],
/* XXX - header file */
extern const char *list_user, *runas_user, *runas_group;
/* Used by getprogname() unless crt0 supports getting program name. */
int Argc;
char **Argv;
/* Needed by tgetpass when executing askpass helper */
struct user_details user_details;
@@ -126,13 +122,15 @@ main(int argc, char *argv[], char *envp[])
malloc_options = "AFGJPR";
#endif
Argc = argc;
Argv = argv;
#ifdef HAVE_SETLOCALE
setlocale(LC_ALL, "");
#endif
#if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
if (argc > 0)
setprogname(argv[0]);
#endif
if (geteuid() != 0)
errorx(1, "must be setuid root");
@@ -159,7 +157,7 @@ main(int argc, char *argv[], char *envp[])
user_info = get_user_info(&user_details);
/* Parse command line arguments. */
sudo_mode = parse_args(Argc, Argv, &nargc, &nargv, &settings, &env_add);
sudo_mode = parse_args(argc, argv, &nargc, &nargv, &settings, &env_add);
/* Read sudo.conf and load plugins. */
sudo_load_plugins(_PATH_SUDO_CONF, &policy_plugin, &io_plugins);