Add define guards around FSTRING_LEN.
[Samba/vl.git] / source3 / libsmb / ntlmssp_parse.c
blobac8846ad1e079f875c2ea72deb03b2b0d660820b
1 /*
2 Unix SMB/CIFS implementation.
3 simple kerberos5/SPNEGO routines
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
6 Copyright (C) Andrew Bartlett 2002-2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
25 this is a tiny msrpc packet generator. I am only using this to
26 avoid tying this code to a particular varient of our rpc code. This
27 generator is not general enough for all our rpc needs, its just
28 enough for the spnego/ntlmssp code
30 format specifiers are:
32 U = unicode string (input is unix string)
33 a = address (input is char *unix_string)
34 (1 byte type, 1 byte length, unicode/ASCII string, all inline)
35 A = ASCII string (input is unix string)
36 B = data blob (pointer + length)
37 b = data blob in header (pointer + length)
39 d = word (4 bytes)
40 C = constant ascii string
42 bool msrpc_gen(DATA_BLOB *blob,
43 const char *format, ...)
45 int i, n;
46 va_list ap;
47 char *s;
48 uint8 *b;
49 int head_size=0, data_size=0;
50 int head_ofs, data_ofs;
52 /* first scan the format to work out the header and body size */
53 va_start(ap, format);
54 for (i=0; format[i]; i++) {
55 switch (format[i]) {
56 case 'U':
57 s = va_arg(ap, char *);
58 head_size += 8;
59 data_size += str_charnum(s) * 2;
60 break;
61 case 'A':
62 s = va_arg(ap, char *);
63 head_size += 8;
64 data_size += str_ascii_charnum(s);
65 break;
66 case 'a':
67 n = va_arg(ap, int);
68 s = va_arg(ap, char *);
69 data_size += (str_charnum(s) * 2) + 4;
70 break;
71 case 'B':
72 b = va_arg(ap, uint8 *);
73 head_size += 8;
74 data_size += va_arg(ap, int);
75 break;
76 case 'b':
77 b = va_arg(ap, uint8 *);
78 head_size += va_arg(ap, int);
79 break;
80 case 'd':
81 n = va_arg(ap, int);
82 head_size += 4;
83 break;
84 case 'C':
85 s = va_arg(ap, char *);
86 head_size += str_charnum(s) + 1;
87 break;
90 va_end(ap);
92 /* allocate the space, then scan the format
93 * again to fill in the values */
95 *blob = data_blob(NULL, head_size + data_size);
97 head_ofs = 0;
98 data_ofs = head_size;
100 va_start(ap, format);
101 for (i=0; format[i]; i++) {
102 switch (format[i]) {
103 case 'U':
104 s = va_arg(ap, char *);
105 n = str_charnum(s);
106 SSVAL(blob->data, head_ofs, n*2); head_ofs += 2;
107 SSVAL(blob->data, head_ofs, n*2); head_ofs += 2;
108 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
109 push_string(NULL, blob->data+data_ofs,
110 s, n*2, STR_UNICODE|STR_NOALIGN);
111 data_ofs += n*2;
112 break;
113 case 'A':
114 s = va_arg(ap, char *);
115 n = str_ascii_charnum(s);
116 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
117 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
118 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
119 push_string(NULL, blob->data+data_ofs,
120 s, n, STR_ASCII|STR_NOALIGN);
121 data_ofs += n;
122 break;
123 case 'a':
124 n = va_arg(ap, int);
125 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
126 s = va_arg(ap, char *);
127 n = str_charnum(s);
128 SSVAL(blob->data, data_ofs, n*2); data_ofs += 2;
129 if (0 < n) {
130 push_string(NULL, blob->data+data_ofs, s, n*2,
131 STR_UNICODE|STR_NOALIGN);
133 data_ofs += n*2;
134 break;
136 case 'B':
137 b = va_arg(ap, uint8 *);
138 n = va_arg(ap, int);
139 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
140 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
141 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
142 if (n && b) /* don't follow null pointers... */
143 memcpy(blob->data+data_ofs, b, n);
144 data_ofs += n;
145 break;
146 case 'd':
147 n = va_arg(ap, int);
148 SIVAL(blob->data, head_ofs, n); head_ofs += 4;
149 break;
150 case 'b':
151 b = va_arg(ap, uint8 *);
152 n = va_arg(ap, int);
153 memcpy(blob->data + head_ofs, b, n);
154 head_ofs += n;
155 break;
156 case 'C':
157 s = va_arg(ap, char *);
158 n = str_charnum(s) + 1;
159 head_ofs += push_string(NULL, blob->data+head_ofs, s, n,
160 STR_ASCII|STR_TERMINATE);
161 break;
164 va_end(ap);
166 return true;
170 /* a helpful macro to avoid running over the end of our blob */
171 #define NEED_DATA(amount) \
172 if ((head_ofs + amount) > blob->length) { \
173 return False; \
177 this is a tiny msrpc packet parser. This the the partner of msrpc_gen
179 format specifiers are:
181 U = unicode string (output is unix string)
182 A = ascii string
183 B = data blob
184 b = data blob in header
185 d = word (4 bytes)
186 C = constant ascii string
189 bool msrpc_parse(const DATA_BLOB *blob,
190 const char *format, ...)
192 int i;
193 va_list ap;
194 char **ps, *s;
195 DATA_BLOB *b;
196 size_t head_ofs = 0;
197 uint16 len1, len2;
198 uint32 ptr;
199 uint32 *v;
201 va_start(ap, format);
202 for (i=0; format[i]; i++) {
203 switch (format[i]) {
204 case 'U':
205 NEED_DATA(8);
206 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
207 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
208 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
210 ps = va_arg(ap, char **);
211 if (len1 == 0 && len2 == 0) {
212 *ps = smb_xstrdup("");
213 } else {
214 /* make sure its in the right format
215 * be strict */
216 if ((len1 != len2) || (ptr + len1 < ptr) ||
217 (ptr + len1 < len1) ||
218 (ptr + len1 > blob->length)) {
219 return false;
221 if (len1 & 1) {
222 /* if odd length and unicode */
223 return false;
225 if (blob->data + ptr <
226 (uint8 *)(unsigned long)ptr ||
227 blob->data + ptr < blob->data)
228 return false;
230 if (0 < len1) {
231 char *p = NULL;
232 pull_string_talloc(talloc_tos(),
233 NULL,
236 blob->data + ptr,
237 len1,
238 STR_UNICODE|STR_NOALIGN);
239 if (p) {
240 (*ps) = smb_xstrdup(p);
241 TALLOC_FREE(p);
242 } else {
243 (*ps) = smb_xstrdup("");
245 } else {
246 (*ps) = smb_xstrdup("");
249 break;
250 case 'A':
251 NEED_DATA(8);
252 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
253 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
254 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
256 ps = va_arg(ap, char **);
257 /* make sure its in the right format - be strict */
258 if (len1 == 0 && len2 == 0) {
259 *ps = smb_xstrdup("");
260 } else {
261 if ((len1 != len2) || (ptr + len1 < ptr) ||
262 (ptr + len1 < len1) ||
263 (ptr + len1 > blob->length)) {
264 return false;
267 if (blob->data + ptr <
268 (uint8 *)(unsigned long)ptr ||
269 blob->data + ptr < blob->data)
270 return false;
272 if (0 < len1) {
273 char *p = NULL;
274 pull_string_talloc(talloc_tos(),
275 NULL,
278 blob->data + ptr,
279 len1,
280 STR_ASCII|STR_NOALIGN);
281 if (p) {
282 (*ps) = smb_xstrdup(p);
283 TALLOC_FREE(p);
284 } else {
285 (*ps) = smb_xstrdup("");
287 } else {
288 (*ps) = smb_xstrdup("");
291 break;
292 case 'B':
293 NEED_DATA(8);
294 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
295 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
296 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
298 b = (DATA_BLOB *)va_arg(ap, void *);
299 if (len1 == 0 && len2 == 0) {
300 *b = data_blob_null;
301 } else {
302 /* make sure its in the right format
303 * be strict */
304 if ((len1 != len2) || (ptr + len1 < ptr) ||
305 (ptr + len1 < len1) ||
306 (ptr + len1 > blob->length)) {
307 return false;
310 if (blob->data + ptr <
311 (uint8 *)(unsigned long)ptr ||
312 blob->data + ptr < blob->data)
313 return false;
315 *b = data_blob(blob->data + ptr, len1);
317 break;
318 case 'b':
319 b = (DATA_BLOB *)va_arg(ap, void *);
320 len1 = va_arg(ap, unsigned);
321 /* make sure its in the right format - be strict */
322 NEED_DATA(len1);
323 if (blob->data + head_ofs < (uint8 *)head_ofs ||
324 blob->data + head_ofs < blob->data) {
325 return false;
328 *b = data_blob(blob->data + head_ofs, len1);
329 head_ofs += len1;
330 break;
331 case 'd':
332 v = va_arg(ap, uint32 *);
333 NEED_DATA(4);
334 *v = IVAL(blob->data, head_ofs); head_ofs += 4;
335 break;
336 case 'C':
337 s = va_arg(ap, char *);
339 if (blob->data + head_ofs < (uint8 *)head_ofs ||
340 blob->data + head_ofs < blob->data) {
341 return false;
345 char *p = NULL;
346 size_t ret = pull_string_talloc(talloc_tos(),
347 NULL,
350 blob->data+head_ofs,
351 blob->length - head_ofs,
352 STR_ASCII|STR_TERMINATE);
353 if (ret == (size_t)-1 || p == NULL) {
354 return false;
356 head_ofs += ret;
357 if (strcmp(s, p) != 0) {
358 TALLOC_FREE(p);
359 return false;
361 TALLOC_FREE(p);
363 break;
366 va_end(ap);
368 return True;