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 */
56 static int arg_len = 0;
57 static int arg_size = 0;
66 s += 2; /* skip \\x */
67 for (i = 0; i < 2; i++) {
102 return (unsigned char)result;
106 fill_txt(src, len, olen)
112 dst = olen ? realloc(yylval.string, olen + len + 1) : malloc(len + 1);
114 yyerror("unable to allocate memory");
119 /* Copy the string and collapse any escaped characters. */
122 if (*src == '\\' && len) {
123 if (src[1] == 'x' && len >= 3 &&
124 isxdigit((unsigned char) src[2]) &&
125 isxdigit((unsigned char) src[3])) {
126 *dst++ = hexchar(src);
149 if (yylval.string != NULL)
150 olen = strlen(yylval.string);
152 return fill_txt(src, len, olen);
156 ((c) == ',' || (c) == ':' || (c) == '=' || (c) == ' ' || (c) == '\t' || (c) == '#')
166 arg_len = arg_size = 0;
168 dst = yylval.command.cmnd = (char *) malloc(len + 1);
169 if (yylval.command.cmnd == NULL) {
170 yyerror("unable to allocate memory");
174 /* Copy the string and collapse any escaped sudo-specific characters. */
175 for (i = 0; i < len; i++) {
176 if (src[i] == '\\' && i != len - 1 && SPECIAL(src[i + 1]))
183 yylval.command.args = NULL;
188 fill_args(s, len, addspace)
196 if (yylval.command.args == NULL) {
200 new_len = arg_len + len + addspace;
202 if (new_len >= arg_size) {
203 /* Allocate more space than we need for subsequent args */
204 while (new_len >= (arg_size += COMMANDARGINC))
207 p = yylval.command.args ?
208 (char *) realloc(yylval.command.args, arg_size) :
209 (char *) malloc(arg_size);
211 efree(yylval.command.args);
212 yyerror("unable to allocate memory");
215 yylval.command.args = p;
218 /* Efficiently append the arg (with a leading space if needed). */
219 p = yylval.command.args + arg_len;
222 if (strlcpy(p, s, arg_size - (p - yylval.command.args)) != len) {
223 yyerror("fill_args: buffer overflow"); /* paranoia */
231 * Check to make sure an IPv6 address does not contain multiple instances
232 * of the string "::". Assumes strlen(s) >= 1.
233 * Returns TRUE if address is valid else FALSE.
241 for (; *s != '\0'; s++) {
242 if (s[0] == ':' && s[1] == ':') {
247 nmatch = 0; /* reset if we hit netmask */