/src/sudo/lib/util/strtomode.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2013-2015 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/stat.h> |
22 | | |
23 | | #include <stdlib.h> |
24 | | #include <errno.h> |
25 | | |
26 | | #include <sudo_compat.h> |
27 | | #include <sudo_debug.h> |
28 | | #include <sudo_gettext.h> |
29 | | #include <sudo_util.h> |
30 | | |
31 | | /* |
32 | | * Parse an octal file mode in the range [0, 0777]. |
33 | | * On success, returns the parsed mode and clears errstr. |
34 | | * On error, returns 0 and sets errstr. |
35 | | */ |
36 | | mode_t |
37 | | sudo_strtomode_v2(const char *cp, const char **errstr) |
38 | 413 | { |
39 | 413 | char *ep; |
40 | 413 | long lval; |
41 | 413 | debug_decl(sudo_strtomode, SUDO_DEBUG_UTIL); |
42 | | |
43 | 413 | errno = 0; |
44 | 413 | lval = strtol(cp, &ep, 8); |
45 | 413 | if (ep == cp || *ep != '\0') { |
46 | 16 | if (errstr != NULL) |
47 | 16 | *errstr = N_("invalid value"); |
48 | 16 | errno = EINVAL; |
49 | 16 | debug_return_mode_t(0); |
50 | 16 | } |
51 | 397 | if (lval < 0 || lval > ACCESSPERMS) { |
52 | 160 | if (errstr != NULL) |
53 | 160 | *errstr = lval < 0 ? N_("value too small") : N_("value too large"); |
54 | 160 | errno = ERANGE; |
55 | 160 | debug_return_mode_t(0); |
56 | 160 | } |
57 | 237 | if (errstr != NULL) |
58 | 237 | *errstr = NULL; |
59 | 237 | debug_return_mode_t((mode_t)lval); |
60 | 237 | } |
61 | | |
62 | | int |
63 | | sudo_strtomode_v1(const char *cp, const char **errstr) |
64 | 0 | { |
65 | 0 | return (int)sudo_strtomode_v2(cp, errstr); |
66 | 0 | } |