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 /*
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 <sys/stat.h>
27 
28 #include <stdlib.h>
29 #include <errno.h>
30 
31 #include "sudo_compat.h"
32 #include "sudo_debug.h"
33 #include "sudo_gettext.h"
34 #include "sudo_util.h"
35 
36 /*
37  * Parse an octal file mode in the range [0, 0777].
38  * On success, returns the parsed mode and clears errstr.
39  * On error, returns 0 and sets errstr.
40  */
41 int
sudo_strtomode_v1(const char * cp,const char ** errstr)42 sudo_strtomode_v1(const char *cp, const char **errstr)
43 {
44     char *ep;
45     long lval;
46     debug_decl(sudo_strtomode, SUDO_DEBUG_UTIL);
47 
48     errno = 0;
49     lval = strtol(cp, &ep, 8);
50     if (ep == cp || *ep != '\0') {
51 	if (errstr != NULL)
52 	    *errstr = N_("invalid value");
53 	errno = EINVAL;
54 	debug_return_int(0);
55     }
56     if (lval < 0 || lval > ACCESSPERMS) {
57 	if (errstr != NULL)
58 	    *errstr = lval < 0 ? N_("value too small") : N_("value too large");
59 	errno = ERANGE;
60 	debug_return_int(0);
61     }
62     if (errstr != NULL)
63 	*errstr = NULL;
64     debug_return_int((int)lval);
65 }
66