Annotation of embedaddon/sudo/plugins/sudoers/toke_util.c, revision 1.1.1.3
1.1 misho 1: /*
1.1.1.3 ! misho 2: * Copyright (c) 1996, 1998-2005, 2007-2013
1.1 misho 3: * Todd C. Miller <Todd.Miller@courtesan.com>
4: *
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.
8: *
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.
19: *
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.
23: */
24:
25: #include <config.h>
26:
27: #include <sys/types.h>
28: #include <stdio.h>
29: #ifdef STDC_HEADERS
30: # include <stdlib.h>
31: # include <stddef.h>
32: #else
33: # ifdef HAVE_STDLIB_H
34: # include <stdlib.h>
35: # endif
36: #endif /* STDC_HEADERS */
37: #ifdef HAVE_STRING_H
38: # include <string.h>
39: #endif /* HAVE_STRING_H */
40: #ifdef HAVE_STRINGS_H
41: # include <strings.h>
42: #endif /* HAVE_STRINGS_H */
43: #ifdef HAVE_UNISTD_H
44: # include <unistd.h>
45: #endif /* HAVE_UNISTD_H */
46: #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
47: # include <malloc.h>
48: #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
49: #include <ctype.h>
1.1.1.3 ! misho 50: #include <errno.h>
1.1.1.2 misho 51:
1.1 misho 52: #include "sudoers.h"
53: #include "parse.h"
54: #include "toke.h"
55: #include <gram.h>
56:
57: static int arg_len = 0;
58: static int arg_size = 0;
59:
1.1.1.2 misho 60: bool
1.1 misho 61: fill_txt(const char *src, int len, int olen)
62: {
63: char *dst;
1.1.1.2 misho 64: debug_decl(fill_txt, SUDO_DEBUG_PARSER)
1.1 misho 65:
1.1.1.3 ! misho 66: dst = olen ? realloc(sudoerslval.string, olen + len + 1) : malloc(len + 1);
1.1 misho 67: if (dst == NULL) {
1.1.1.3 ! misho 68: warning(NULL);
! 69: sudoerserror(NULL);
1.1.1.2 misho 70: debug_return_bool(false);
1.1 misho 71: }
1.1.1.3 ! misho 72: sudoerslval.string = dst;
1.1 misho 73:
74: /* Copy the string and collapse any escaped characters. */
75: dst += olen;
76: while (len--) {
77: if (*src == '\\' && len) {
78: if (src[1] == 'x' && len >= 3 &&
79: isxdigit((unsigned char) src[2]) &&
80: isxdigit((unsigned char) src[3])) {
1.1.1.3 ! misho 81: *dst++ = hexchar(src + 2);
1.1 misho 82: src += 4;
83: len -= 3;
84: } else {
85: src++;
86: len--;
87: *dst++ = *src++;
88: }
89: } else {
90: *dst++ = *src++;
91: }
92: }
93: *dst = '\0';
1.1.1.2 misho 94: debug_return_bool(true);
1.1 misho 95: }
96:
1.1.1.2 misho 97: bool
1.1 misho 98: append(const char *src, int len)
99: {
100: int olen = 0;
1.1.1.2 misho 101: debug_decl(append, SUDO_DEBUG_PARSER)
1.1 misho 102:
1.1.1.3 ! misho 103: if (sudoerslval.string != NULL)
! 104: olen = strlen(sudoerslval.string);
1.1 misho 105:
1.1.1.2 misho 106: debug_return_bool(fill_txt(src, len, olen));
1.1 misho 107: }
108:
109: #define SPECIAL(c) \
110: ((c) == ',' || (c) == ':' || (c) == '=' || (c) == ' ' || (c) == '\t' || (c) == '#')
111:
1.1.1.2 misho 112: bool
1.1 misho 113: fill_cmnd(const char *src, int len)
114: {
115: char *dst;
116: int i;
1.1.1.2 misho 117: debug_decl(fill_cmnd, SUDO_DEBUG_PARSER)
1.1 misho 118:
119: arg_len = arg_size = 0;
120:
1.1.1.3 ! misho 121: dst = sudoerslval.command.cmnd = (char *) malloc(len + 1);
! 122: if (sudoerslval.command.cmnd == NULL) {
! 123: warning(NULL);
! 124: sudoerserror(NULL);
1.1.1.2 misho 125: debug_return_bool(false);
1.1 misho 126: }
127:
128: /* Copy the string and collapse any escaped sudo-specific characters. */
129: for (i = 0; i < len; i++) {
130: if (src[i] == '\\' && i != len - 1 && SPECIAL(src[i + 1]))
131: *dst++ = src[++i];
132: else
133: *dst++ = src[i];
134: }
135: *dst = '\0';
136:
1.1.1.3 ! misho 137: sudoerslval.command.args = NULL;
1.1.1.2 misho 138: debug_return_bool(true);
1.1 misho 139: }
140:
1.1.1.2 misho 141: bool
1.1 misho 142: fill_args(const char *s, int len, int addspace)
143: {
144: int new_len;
145: char *p;
1.1.1.2 misho 146: debug_decl(fill_args, SUDO_DEBUG_PARSER)
1.1 misho 147:
1.1.1.3 ! misho 148: if (sudoerslval.command.args == NULL) {
1.1 misho 149: addspace = 0;
150: new_len = len;
151: } else
152: new_len = arg_len + len + addspace;
153:
154: if (new_len >= arg_size) {
155: /* Allocate more space than we need for subsequent args */
156: while (new_len >= (arg_size += COMMANDARGINC))
157: ;
158:
1.1.1.3 ! misho 159: p = sudoerslval.command.args ?
! 160: (char *) realloc(sudoerslval.command.args, arg_size) :
1.1 misho 161: (char *) malloc(arg_size);
162: if (p == NULL) {
1.1.1.3 ! misho 163: efree(sudoerslval.command.args);
! 164: warning(NULL);
! 165: sudoerserror(NULL);
1.1.1.2 misho 166: debug_return_bool(false);
1.1 misho 167: } else
1.1.1.3 ! misho 168: sudoerslval.command.args = p;
1.1 misho 169: }
170:
171: /* Efficiently append the arg (with a leading space if needed). */
1.1.1.3 ! misho 172: p = sudoerslval.command.args + arg_len;
1.1 misho 173: if (addspace)
174: *p++ = ' ';
1.1.1.3 ! misho 175: if (strlcpy(p, s, arg_size - (p - sudoerslval.command.args)) != len) {
! 176: warningx(_("fill_args: buffer overflow")); /* paranoia */
! 177: sudoerserror(NULL);
1.1.1.2 misho 178: debug_return_bool(false);
1.1 misho 179: }
180: arg_len = new_len;
1.1.1.2 misho 181: debug_return_bool(true);
1.1 misho 182: }
183:
184: /*
185: * Check to make sure an IPv6 address does not contain multiple instances
186: * of the string "::". Assumes strlen(s) >= 1.
1.1.1.2 misho 187: * Returns true if address is valid else false.
1.1 misho 188: */
1.1.1.2 misho 189: bool
1.1 misho 190: ipv6_valid(const char *s)
191: {
192: int nmatch = 0;
1.1.1.2 misho 193: debug_decl(ipv6_valid, SUDO_DEBUG_PARSER)
1.1 misho 194:
195: for (; *s != '\0'; s++) {
196: if (s[0] == ':' && s[1] == ':') {
197: if (++nmatch > 1)
198: break;
199: }
200: if (s[0] == '/')
201: nmatch = 0; /* reset if we hit netmask */
202: }
203:
1.1.1.2 misho 204: debug_return_bool(nmatch <= 1);
1.1 misho 205: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>