/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  | 854k  | { | 
43  | 854k  |     bool valid = false;  | 
44  |  |  | 
45  | 854k  |     if (ep != p) { | 
46  |  |   /* check for valid separator (including '\0') */  | 
47  | 854k  |   if (sep == NULL)  | 
48  | 60.5k  |       sep = "";  | 
49  | 1.64M  |   do { | 
50  | 1.64M  |       if (*ep == *sep)  | 
51  | 854k  |     valid = true;  | 
52  | 1.64M  |   } while (*sep++ != '\0');  | 
53  | 854k  |     }  | 
54  | 854k  |     return valid;  | 
55  | 854k  | }  | 
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  | 854k  | { | 
67  | 854k  |     const char *errstr;  | 
68  | 854k  |     char *ep;  | 
69  | 854k  |     id_t ret;  | 
70  | 854k  |     debug_decl(sudo_strtoid, SUDO_DEBUG_UTIL);  | 
71  |  |  | 
72  | 854k  |     ret = (id_t)sudo_strtonumx(p, INT_MIN, UINT_MAX, &ep, &errstr);  | 
73  | 854k  |     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  | 854k  |   if (ret == (id_t)-1 || ret == (id_t)UINT_MAX || !valid_separator(p, ep, sep)) { | 
79  | 202  |       errstr = N_("invalid value"); | 
80  | 202  |       errno = EINVAL;  | 
81  | 202  |       ret = 0;  | 
82  | 202  |   }  | 
83  | 854k  |     }  | 
84  | 854k  |     if (errstrp != NULL)  | 
85  | 854k  |   *errstrp = errstr;  | 
86  | 854k  |     if (endp != NULL)  | 
87  | 794k  |   *endp = ep;  | 
88  | 854k  |     debug_return_id_t(ret);  | 
89  | 854k  | }  | 
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  | 60.7k  | { | 
102  | 60.7k  |     return sudo_strtoidx_v1(p, NULL, NULL, errstrp);  | 
103  | 60.7k  | }  |