/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  | 51.8k  | { | 
39  | 51.8k  |     char *ep;  | 
40  | 51.8k  |     long lval;  | 
41  | 51.8k  |     debug_decl(sudo_strtomode, SUDO_DEBUG_UTIL);  | 
42  |  |  | 
43  | 51.8k  |     errno = 0;  | 
44  | 51.8k  |     lval = strtol(cp, &ep, 8);  | 
45  | 51.8k  |     if (ep == cp || *ep != '\0') { | 
46  | 50  |   if (errstr != NULL)  | 
47  | 50  |       *errstr = N_("invalid value"); | 
48  | 50  |   errno = EINVAL;  | 
49  | 50  |   debug_return_mode_t(0);  | 
50  | 50  |     }  | 
51  | 51.8k  |     if (lval < 0 || lval > ACCESSPERMS) { | 
52  | 302  |   if (errstr != NULL)  | 
53  | 302  |       *errstr = lval < 0 ? N_("value too small") : N_("value too large"); | 
54  | 302  |   errno = ERANGE;  | 
55  | 302  |   debug_return_mode_t(0);  | 
56  | 302  |     }  | 
57  | 51.5k  |     if (errstr != NULL)  | 
58  | 51.5k  |   *errstr = NULL;  | 
59  | 51.5k  |     debug_return_mode_t((mode_t)lval);  | 
60  | 51.5k  | }  | 
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  | }  |