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;
60 hexchar(const char *s)
65 s += 2; /* skip \\x */
66 for (i = 0; i < 2; i++) {
101 return (unsigned char)result;
105 fill_txt(const char *src, int len, int olen)
109 dst = olen ? realloc(yylval.string, olen + len + 1) : malloc(len + 1);
111 yyerror(_("unable to allocate memory"));
116 /* Copy the string and collapse any escaped characters. */
119 if (*src == '\\' && len) {
120 if (src[1] == 'x' && len >= 3 &&
121 isxdigit((unsigned char) src[2]) &&
122 isxdigit((unsigned char) src[3])) {
123 *dst++ = hexchar(src);
140 append(const char *src, int len)
144 if (yylval.string != NULL)
145 olen = strlen(yylval.string);
147 return fill_txt(src, len, olen);
151 ((c) == ',' || (c) == ':' || (c) == '=' || (c) == ' ' || (c) == '\t' || (c) == '#')
154 fill_cmnd(const char *src, int len)
159 arg_len = arg_size = 0;
161 dst = yylval.command.cmnd = (char *) malloc(len + 1);
162 if (yylval.command.cmnd == NULL) {
163 yyerror(_("unable to allocate memory"));
167 /* Copy the string and collapse any escaped sudo-specific characters. */
168 for (i = 0; i < len; i++) {
169 if (src[i] == '\\' && i != len - 1 && SPECIAL(src[i + 1]))
176 yylval.command.args = NULL;
181 fill_args(const char *s, int len, int addspace)
186 if (yylval.command.args == NULL) {
190 new_len = arg_len + len + addspace;
192 if (new_len >= arg_size) {
193 /* Allocate more space than we need for subsequent args */
194 while (new_len >= (arg_size += COMMANDARGINC))
197 p = yylval.command.args ?
198 (char *) realloc(yylval.command.args, arg_size) :
199 (char *) malloc(arg_size);
201 efree(yylval.command.args);
202 yyerror(_("unable to allocate memory"));
205 yylval.command.args = p;
208 /* Efficiently append the arg (with a leading space if needed). */
209 p = yylval.command.args + arg_len;
212 if (strlcpy(p, s, arg_size - (p - yylval.command.args)) != len) {
213 yyerror(_("fill_args: buffer overflow")); /* paranoia */
221 * Check to make sure an IPv6 address does not contain multiple instances
222 * of the string "::". Assumes strlen(s) >= 1.
223 * Returns TRUE if address is valid else FALSE.
226 ipv6_valid(const char *s)
230 for (; *s != '\0'; s++) {
231 if (s[0] == ':' && s[1] == ':') {
236 nmatch = 0; /* reset if we hit netmask */