/src/sudo/lib/util/progname.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2013-2015, 2020-2021 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <config.h> |
20 | | |
21 | | #include <stdlib.h> |
22 | | #include <string.h> |
23 | | |
24 | | #include <sudo_compat.h> |
25 | | #include <sudo_util.h> |
26 | | |
27 | | /* |
28 | | * Declare/define __progname[] if necessary. |
29 | | * Assumes __progname[] is present if we have getprogname(3). |
30 | | */ |
31 | | #ifndef HAVE_SETPROGNAME |
32 | | # if defined(HAVE_GETPROGNAME) || defined(HAVE___PROGNAME) |
33 | | extern const char *__progname; |
34 | | # else |
35 | | static const char *__progname = ""; |
36 | | # endif /* HAVE_GETPROGNAME || HAVE___PROGNAME */ |
37 | | #endif /* HAVE_SETPROGNAME */ |
38 | | |
39 | | #ifndef HAVE_GETPROGNAME |
40 | | const char * |
41 | | sudo_getprogname(void) |
42 | 701 | { |
43 | 701 | return __progname; |
44 | 701 | } |
45 | | #endif |
46 | | |
47 | | #ifndef HAVE_SETPROGNAME |
48 | | void |
49 | | sudo_setprogname(const char *name) |
50 | 0 | { |
51 | 0 | __progname = sudo_basename(name); |
52 | 0 | } |
53 | | #endif |
54 | | |
55 | | void |
56 | | initprogname2(const char *name, const char * const * allowed) |
57 | 392 | { |
58 | 392 | const char *progname; |
59 | 392 | size_t i; |
60 | | |
61 | | /* Fall back on "name" if getprogname() returns an empty string. */ |
62 | 392 | if ((progname = getprogname()) != NULL && *progname != '\0') { |
63 | 392 | name = progname; |
64 | 392 | } else { |
65 | | /* Make sure user-specified name is relative. */ |
66 | 0 | name = sudo_basename(name); |
67 | 0 | } |
68 | | |
69 | | /* Check for libtool prefix and strip it if present. */ |
70 | 392 | if (name[0] == 'l' && name[1] == 't' && name[2] == '-' && name[3] != '\0') |
71 | 0 | name += 3; |
72 | | |
73 | | /* Check allow list if present (first element is the default). */ |
74 | 392 | if (allowed != NULL) { |
75 | 0 | for (i = 0; ; i++) { |
76 | 0 | if (allowed[i] == NULL) { |
77 | 0 | name = allowed[0]; |
78 | 0 | break; |
79 | 0 | } |
80 | 0 | if (strcmp(allowed[i], name) == 0) |
81 | 0 | break; |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | /* Update internal progname if needed. */ |
86 | 392 | if (name != progname) |
87 | 0 | setprogname(name); |
88 | 392 | return; |
89 | 392 | } |
90 | | |
91 | | void |
92 | | initprogname(const char *name) |
93 | 392 | { |
94 | | initprogname2(name, NULL); |
95 | 392 | } |