]> granicus.if.org Git - sudo/commitdiff
Rewrite compat/getprogname.c and add setprogname().
authorTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 19 Mar 2010 11:52:31 +0000 (07:52 -0400)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 19 Mar 2010 11:52:31 +0000 (07:52 -0400)
The progname is now passed to the plugin via the settings array.

compat/getprogname.c
include/compat.h
plugins/sample/sample_plugin.c
plugins/sudoers/sudoers.c
src/parse_args.c
src/sudo.c

index ec3b964bcf57554c6bae74da2bcb7376f3a4d2a4..96c2615d2e7793ed07e8b59dc459983ea9bd1024 100644 (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
  * 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>
 #include <config.h>
 #include <compat.h>
 
-const char *
-getprogname(void)
+static char *progname = "sudo";
+
+void
+setprogname(const char *name)
 {
-#ifdef PIC
-    return("sudo");
-#else
-    static const char *progname;
-    extern int Argc;
-    extern char **Argv;
+    const char *base;
 
-    if (progname == NULL) {
-       if (Argc < 0)
-           progname = "sudo";
-       else if ((progname = strrchr(Argv[0], '/')) != NULL)
-           progname++;
-       else
-           progname = Argv[0];
+    if ((base = strrchr(name, '/')) != NULL) {
+       base++;
+    } else {
+       base = name;
     }
-    return(progname);
-#endif
+    if (strcmp(progname, base) != 0)
+       progname = base;
+}
+
+const char *
+getprogname(void)
+{
+    return progname;
 }
index 1d00fa6e1edbd75f6ab99c0c7dae75120c98eb47..f622dbd0c01a694e0f6e545dc2973961a2cc01e4 100644 (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 */
 
index 33ef428913cf21aaf450bece81f6aba614d6b03d..f60214b142625c316de3afcc047e1c59698420ed 100644 (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) {
index 986cef6cd97c432db6b361c1cf20ecffec12306a..469c5f1ff62ebe408a9aead5ffa57f4803896b46 100644 (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++) {
index e970a9f041bdc88ccf7a1fed31e071819fa31e29..222a190574ccefa97dda3e2afbe513c00c2e70c8 100644 (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;
index 86b0eac2d009b8c1ec3967d09b115763d968d8b3..a595b0aaa251cdaa74a556c2e8aac8885f410525 100644 (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);