Allow getprogname() to succeed as long as __progname is present.

Also simplify the progname code so we only need a single implementation.
This commit is contained in:
Todd C. Miller
2021-02-02 11:20:53 -07:00
parent 0d34fa4285
commit eec4f42366

View File

@@ -1,7 +1,7 @@
/* /*
* SPDX-License-Identifier: ISC * SPDX-License-Identifier: ISC
* *
* Copyright (c) 2013-2015 Todd C. Miller <Todd.Miller@sudo.ws> * Copyright (c) 2013-2015, 2020-2021 Todd C. Miller <Todd.Miller@sudo.ws>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -29,12 +29,27 @@
#include "sudo_compat.h" #include "sudo_compat.h"
#include "sudo_util.h" #include "sudo_util.h"
#ifdef HAVE_GETPROGNAME /*
* Declare/define __progname[] if necessary.
* Assumes __progname[] is present if we have getprogname(3).
*/
#ifndef HAVE_SETPROGNAME
# if defined(HAVE_GETPROGNAME) || defined(HAVE___PROGNAME)
extern const char *__progname;
# else
static const char *__progname = "";
# endif /* HAVE_GETPROGNAME || HAVE___PROGNAME */
#endif /* HAVE_SETPROGNAME */
#ifndef HAVE_GETPROGNAME
const char *
sudo_getprogname(void)
{
return __progname;
}
#endif
#ifndef HAVE_SETPROGNAME #ifndef HAVE_SETPROGNAME
/* Assume __progname if have getprogname(3) but not setprogname(3). */
extern const char *__progname;
void void
sudo_setprogname(const char *name) sudo_setprogname(const char *name)
{ {
@@ -50,8 +65,14 @@ initprogname2(const char *name, const char * const * allowed)
int i; int i;
/* Fall back on "name" if getprogname() returns an empty string. */ /* Fall back on "name" if getprogname() returns an empty string. */
if ((progname = getprogname()) != NULL && *progname != '\0') if ((progname = getprogname()) != NULL && *progname != '\0') {
name = progname; name = progname;
} else {
/* Make sure user-specified name is relative. */
const char *slash = strrchr(name, '/');
if (slash != NULL)
name = slash + 1;
}
/* Check for libtool prefix and strip it if present. */ /* Check for libtool prefix and strip it if present. */
if (name[0] == 'l' && name[1] == 't' && name[2] == '-' && name[3] != '\0') if (name[0] == 'l' && name[1] == 't' && name[2] == '-' && name[3] != '\0')
@@ -75,59 +96,6 @@ initprogname2(const char *name, const char * const * allowed)
return; return;
} }
#else /* !HAVE_GETPROGNAME */
static const char *progname = "";
void
initprogname2(const char *name, const char * const * allowed)
{
int i;
# ifdef HAVE___PROGNAME
extern const char *__progname;
if (__progname != NULL && *__progname != '\0')
progname = __progname;
else
# endif
if ((progname = strrchr(name, '/')) != NULL) {
progname++;
} else {
progname = name;
}
/* Check for libtool prefix and strip it if present. */
if (progname[0] == 'l' && progname[1] == 't' && progname[2] == '-' &&
progname[3] != '\0')
progname += 3;
/* Check allow list if present (first element is the default). */
if (allowed != NULL) {
for (i = 0; ; i++) {
if (allowed[i] == NULL) {
progname = allowed[0];
break;
}
if (strcmp(allowed[i], progname) == 0)
break;
}
}
}
const char *
sudo_getprogname(void)
{
return progname;
}
void
sudo_setprogname(const char *name)
{
const char *slash = strrchr(name, '/');
progname = slash ? slash + 1 : name;
}
#endif /* !HAVE_GETPROGNAME */
void void
initprogname(const char *name) initprogname(const char *name)
{ {