2 * Copyright (c) 1996, 1998-2005, 2007-2011
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
17 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
18 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20 * Sponsored in part by the Defense Advanced Research Projects
21 * Agency (DARPA) and Air Force Research Laboratory, Air Force
22 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
27 #include <sys/types.h>
28 #include <sys/param.h>
37 #endif /* STDC_HEADERS */
40 #endif /* HAVE_STRING_H */
43 #endif /* HAVE_STRINGS_H */
46 #endif /* HAVE_UNISTD_H */
47 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
49 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
57 static int arg_len = 0;
58 static int arg_size = 0;
61 hexchar(const char *s)
64 debug_decl(hexchar, SUDO_DEBUG_PARSER)
66 s += 2; /* skip \\x */
67 for (i = 0; i < 2; i++) {
102 debug_return_int(result);
106 fill_txt(const char *src, int len, int olen)
109 debug_decl(fill_txt, SUDO_DEBUG_PARSER)
111 dst = olen ? realloc(yylval.string, olen + len + 1) : malloc(len + 1);
113 yyerror(_("unable to allocate memory"));
114 debug_return_bool(false);
118 /* Copy the string and collapse any escaped characters. */
121 if (*src == '\\' && len) {
122 if (src[1] == 'x' && len >= 3 &&
123 isxdigit((unsigned char) src[2]) &&
124 isxdigit((unsigned char) src[3])) {
125 *dst++ = hexchar(src);
138 debug_return_bool(true);
142 append(const char *src, int len)
145 debug_decl(append, SUDO_DEBUG_PARSER)
147 if (yylval.string != NULL)
148 olen = strlen(yylval.string);
150 debug_return_bool(fill_txt(src, len, olen));
154 ((c) == ',' || (c) == ':' || (c) == '=' || (c) == ' ' || (c) == '\t' || (c) == '#')
157 fill_cmnd(const char *src, int len)
161 debug_decl(fill_cmnd, SUDO_DEBUG_PARSER)
163 arg_len = arg_size = 0;
165 dst = yylval.command.cmnd = (char *) malloc(len + 1);
166 if (yylval.command.cmnd == NULL) {
167 yyerror(_("unable to allocate memory"));
168 debug_return_bool(false);
171 /* Copy the string and collapse any escaped sudo-specific characters. */
172 for (i = 0; i < len; i++) {
173 if (src[i] == '\\' && i != len - 1 && SPECIAL(src[i + 1]))
180 yylval.command.args = NULL;
181 debug_return_bool(true);
185 fill_args(const char *s, int len, int addspace)
189 debug_decl(fill_args, SUDO_DEBUG_PARSER)
191 if (yylval.command.args == NULL) {
195 new_len = arg_len + len + addspace;
197 if (new_len >= arg_size) {
198 /* Allocate more space than we need for subsequent args */
199 while (new_len >= (arg_size += COMMANDARGINC))
202 p = yylval.command.args ?
203 (char *) realloc(yylval.command.args, arg_size) :
204 (char *) malloc(arg_size);
206 efree(yylval.command.args);
207 yyerror(_("unable to allocate memory"));
208 debug_return_bool(false);
210 yylval.command.args = p;
213 /* Efficiently append the arg (with a leading space if needed). */
214 p = yylval.command.args + arg_len;
217 if (strlcpy(p, s, arg_size - (p - yylval.command.args)) != len) {
218 yyerror(_("fill_args: buffer overflow")); /* paranoia */
219 debug_return_bool(false);
222 debug_return_bool(true);
226 * Check to make sure an IPv6 address does not contain multiple instances
227 * of the string "::". Assumes strlen(s) >= 1.
228 * Returns true if address is valid else false.
231 ipv6_valid(const char *s)
234 debug_decl(ipv6_valid, SUDO_DEBUG_PARSER)
236 for (; *s != '\0'; s++) {
237 if (s[0] == ':' && s[1] == ':') {
242 nmatch = 0; /* reset if we hit netmask */
245 debug_return_bool(nmatch <= 1);