2013-12-01 19:12:21 -07:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2017-12-03 17:53:40 -07:00
|
|
|
* Copyright (c) 2013-2015 Todd C. Miller <Todd.Miller@sudo.ws>
|
2013-12-01 19:12:21 -07:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-10-26 08:39:09 -06:00
|
|
|
/*
|
|
|
|
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
|
|
|
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
|
|
*/
|
2018-10-21 08:46:05 -06:00
|
|
|
|
2013-12-01 19:12:21 -07:00
|
|
|
#include <config.h>
|
|
|
|
|
2015-06-19 14:29:27 -06:00
|
|
|
#include <stdlib.h>
|
2020-05-18 07:59:24 -06:00
|
|
|
#include <string.h>
|
2013-12-01 19:12:21 -07:00
|
|
|
|
2014-07-22 14:25:16 -06:00
|
|
|
#include "sudo_compat.h"
|
2013-12-12 18:29:07 -07:00
|
|
|
#include "sudo_util.h"
|
2013-12-01 19:12:21 -07:00
|
|
|
|
2014-07-25 14:08:59 -06:00
|
|
|
#ifdef HAVE_GETPROGNAME
|
2013-12-01 19:12:21 -07:00
|
|
|
|
|
|
|
void
|
2021-01-06 10:16:00 -07:00
|
|
|
initprogname2(const char *name, const char * const * allowed)
|
2013-12-01 19:12:21 -07:00
|
|
|
{
|
2014-07-14 09:08:50 -06:00
|
|
|
# ifdef HAVE_SETPROGNAME
|
2015-02-05 11:17:24 -07:00
|
|
|
const char *progname;
|
2021-01-06 10:16:00 -07:00
|
|
|
int i;
|
2015-02-05 11:17:24 -07:00
|
|
|
|
|
|
|
/* Fall back on "name" if getprogname() returns an empty string. */
|
|
|
|
if ((progname = getprogname()) != NULL && *progname != '\0')
|
|
|
|
name = progname;
|
|
|
|
|
2014-07-14 09:08:50 -06:00
|
|
|
/* Check for libtool prefix and strip it if present. */
|
|
|
|
if (name[0] == 'l' && name[1] == 't' && name[2] == '-' && name[3] != '\0')
|
2015-02-05 11:17:24 -07:00
|
|
|
name += 3;
|
|
|
|
|
2021-01-06 10:16:00 -07:00
|
|
|
/* Check allow list if present (first element is the default). */
|
|
|
|
if (allowed != NULL) {
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
if (allowed[i] == NULL) {
|
|
|
|
name = allowed[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strcmp(allowed[i], name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 11:17:24 -07:00
|
|
|
/* Update internal progname if needed. */
|
|
|
|
if (name != progname)
|
|
|
|
setprogname(name);
|
2014-07-14 09:08:50 -06:00
|
|
|
# endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-25 14:08:59 -06:00
|
|
|
#else /* !HAVE_GETPROGNAME */
|
2013-12-01 19:12:21 -07:00
|
|
|
|
|
|
|
static const char *progname = "";
|
|
|
|
|
|
|
|
void
|
2021-01-06 10:16:00 -07:00
|
|
|
initprogname2(const char *name, const char * const * allowed)
|
2013-12-01 19:12:21 -07:00
|
|
|
{
|
2021-01-06 10:16:00 -07:00
|
|
|
int i;
|
2014-07-25 14:08:59 -06:00
|
|
|
# ifdef HAVE___PROGNAME
|
|
|
|
extern const char *__progname;
|
2013-12-01 19:12:21 -07:00
|
|
|
|
2015-02-05 11:17:24 -07:00
|
|
|
if (__progname != NULL && *__progname != '\0')
|
|
|
|
progname = __progname;
|
|
|
|
else
|
|
|
|
# endif
|
2014-07-25 14:08:59 -06:00
|
|
|
if ((progname = strrchr(name, '/')) != NULL) {
|
|
|
|
progname++;
|
2013-12-01 19:12:21 -07:00
|
|
|
} else {
|
2014-07-25 14:08:59 -06:00
|
|
|
progname = name;
|
2013-12-01 19:12:21 -07:00
|
|
|
}
|
2014-07-14 09:08:50 -06:00
|
|
|
|
2015-02-05 11:17:24 -07:00
|
|
|
/* Check for libtool prefix and strip it if present. */
|
2014-07-14 09:08:50 -06:00
|
|
|
if (progname[0] == 'l' && progname[1] == 't' && progname[2] == '-' &&
|
|
|
|
progname[3] != '\0')
|
|
|
|
progname += 3;
|
2021-01-06 10:16:00 -07:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
2013-12-01 19:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2014-07-14 09:08:50 -06:00
|
|
|
sudo_getprogname(void)
|
2013-12-01 19:12:21 -07:00
|
|
|
{
|
|
|
|
return progname;
|
|
|
|
}
|
2014-07-14 09:08:50 -06:00
|
|
|
#endif /* !HAVE_GETPROGNAME */
|
2021-01-06 10:16:00 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
initprogname(const char *name)
|
|
|
|
{
|
|
|
|
initprogname2(name, NULL);
|
|
|
|
}
|