/src/sudo/lib/util/strtoid.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2013-2020 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 <sys/types.h> /* for id_t */ |
22 | | |
23 | | #ifdef HAVE_STDBOOL_H |
24 | | # include <stdbool.h> |
25 | | #else |
26 | | # include <compat/stdbool.h> |
27 | | #endif |
28 | | #include <ctype.h> |
29 | | #include <errno.h> |
30 | | #include <limits.h> |
31 | | |
32 | | #include <sudo_compat.h> |
33 | | #include <sudo_debug.h> |
34 | | #include <sudo_gettext.h> |
35 | | #include <sudo_util.h> |
36 | | |
37 | | /* |
38 | | * Make sure that the ID ends with a valid separator char. |
39 | | */ |
40 | | static bool |
41 | | valid_separator(const char *p, const char *ep, const char *sep) |
42 | 652k | { |
43 | 652k | bool valid = false; |
44 | | |
45 | 652k | if (ep != p) { |
46 | | /* check for valid separator (including '\0') */ |
47 | 652k | if (sep == NULL) |
48 | 68.1k | sep = ""; |
49 | 1.23M | do { |
50 | 1.23M | if (*ep == *sep) |
51 | 652k | valid = true; |
52 | 1.23M | } while (*sep++ != '\0'); |
53 | 652k | } |
54 | 652k | return valid; |
55 | 652k | } |
56 | | |
57 | | /* |
58 | | * Parse a uid/gid in string form. |
59 | | * If sep is non-NULL, it contains valid separator characters (e.g. comma, space) |
60 | | * If endp is non-NULL it is set to the next char after the ID. |
61 | | * On success, returns the parsed ID and clears errstr. |
62 | | * On error, returns 0 and sets errstr. |
63 | | */ |
64 | | id_t |
65 | | sudo_strtoidx_v1(const char *p, const char *sep, char **endp, const char **errstrp) |
66 | 652k | { |
67 | 652k | const char *errstr; |
68 | 652k | char *ep; |
69 | 652k | id_t ret; |
70 | 652k | debug_decl(sudo_strtoid, SUDO_DEBUG_UTIL); |
71 | | |
72 | 652k | ret = (id_t)sudo_strtonumx(p, INT_MIN, UINT_MAX, &ep, &errstr); |
73 | 652k | if (errstr == NULL) { |
74 | | /* |
75 | | * Disallow id -1 (UINT_MAX), which means "no change" |
76 | | * and check for a valid separator (if specified). |
77 | | */ |
78 | 652k | if (ret == (id_t)-1 || ret == (id_t)UINT_MAX || !valid_separator(p, ep, sep)) { |
79 | 223 | errstr = N_("invalid value"); |
80 | 223 | errno = EINVAL; |
81 | 223 | ret = 0; |
82 | 223 | } |
83 | 652k | } |
84 | 652k | if (errstrp != NULL) |
85 | 652k | *errstrp = errstr; |
86 | 652k | if (endp != NULL) |
87 | 584k | *endp = ep; |
88 | 652k | debug_return_id_t(ret); |
89 | 652k | } |
90 | | |
91 | | /* Backward compatibility */ |
92 | | id_t |
93 | | sudo_strtoid_v1(const char *p, const char *sep, char **endp, const char **errstrp) |
94 | 0 | { |
95 | 0 | return sudo_strtoidx_v1(p, sep, endp, errstrp); |
96 | 0 | } |
97 | | |
98 | | /* Simplified interface */ |
99 | | id_t |
100 | | sudo_strtoid_v2(const char *p, const char **errstrp) |
101 | 68.3k | { |
102 | 68.3k | return sudo_strtoidx_v1(p, NULL, NULL, errstrp); |
103 | 68.3k | } |