/src/sudo/lib/util/progname.c
Line | Count | Source (jump to first uncovered line) |
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 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <stdlib.h> |
27 | | #include <string.h> |
28 | | |
29 | | #include <sudo_compat.h> |
30 | | #include <sudo_util.h> |
31 | | |
32 | | /* |
33 | | * Declare/define __progname[] if necessary. |
34 | | * Assumes __progname[] is present if we have getprogname(3). |
35 | | */ |
36 | | #ifndef HAVE_SETPROGNAME |
37 | | # if defined(HAVE_GETPROGNAME) || defined(HAVE___PROGNAME) |
38 | | extern const char *__progname; |
39 | | # else |
40 | | static const char *__progname = ""; |
41 | | # endif /* HAVE_GETPROGNAME || HAVE___PROGNAME */ |
42 | | #endif /* HAVE_SETPROGNAME */ |
43 | | |
44 | | #ifndef HAVE_GETPROGNAME |
45 | | const char * |
46 | | sudo_getprogname(void) |
47 | 706 | { |
48 | 706 | return __progname; |
49 | 706 | } |
50 | | #endif |
51 | | |
52 | | #ifndef HAVE_SETPROGNAME |
53 | | void |
54 | | sudo_setprogname(const char *name) |
55 | 0 | { |
56 | 0 | __progname = sudo_basename(name); |
57 | 0 | } |
58 | | #endif |
59 | | |
60 | | void |
61 | | initprogname2(const char *name, const char * const * allowed) |
62 | 394 | { |
63 | 394 | const char *progname; |
64 | 394 | size_t i; |
65 | | |
66 | | /* Fall back on "name" if getprogname() returns an empty string. */ |
67 | 394 | if ((progname = getprogname()) != NULL && *progname != '\0') { |
68 | 394 | name = progname; |
69 | 394 | } else { |
70 | | /* Make sure user-specified name is relative. */ |
71 | 0 | name = sudo_basename(name); |
72 | 0 | } |
73 | | |
74 | | /* Check for libtool prefix and strip it if present. */ |
75 | 394 | if (name[0] == 'l' && name[1] == 't' && name[2] == '-' && name[3] != '\0') |
76 | 0 | name += 3; |
77 | | |
78 | | /* Check allow list if present (first element is the default). */ |
79 | 394 | if (allowed != NULL) { |
80 | 0 | for (i = 0; ; i++) { |
81 | 0 | if (allowed[i] == NULL) { |
82 | 0 | name = allowed[0]; |
83 | 0 | break; |
84 | 0 | } |
85 | 0 | if (strcmp(allowed[i], name) == 0) |
86 | 0 | break; |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | | /* Update internal progname if needed. */ |
91 | 394 | if (name != progname) |
92 | 0 | setprogname(name); |
93 | 394 | return; |
94 | 394 | } |
95 | | |
96 | | void |
97 | | initprogname(const char *name) |
98 | 394 | { |
99 | 394 | initprogname2(name, NULL); |
100 | 394 | } |