s3/packaging: source -> source3
[Samba/gbeck.git] / libcli / auth / msrpc_parse.c
blob9125c1cd7877c3e85a3dc74ea93d23c9d0404ff5
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"
23 #include "libcli/auth/msrpc_parse.h"
26 this is a tiny msrpc packet generator. I am only using this to
27 avoid tying this code to a particular varient of our rpc code. This
28 generator is not general enough for all our rpc needs, its just
29 enough for the spnego/ntlmssp code
31 format specifiers are:
33 U = unicode string (input is unix string)
34 a = address (input is char *unix_string)
35 (1 byte type, 1 byte length, unicode/ASCII string, all inline)
36 A = ASCII string (input is unix string)
37 B = data blob (pointer + length)
38 b = data blob in header (pointer + length)
40 d = word (4 bytes)
41 C = constant ascii string
43 bool msrpc_gen(TALLOC_CTX *mem_ctx,
44 DATA_BLOB *blob,
45 const char *format, ...)
47 int i, j;
48 bool ret;
49 va_list ap;
50 char *s;
51 uint8_t *b;
52 int head_size=0, data_size=0;
53 int head_ofs, data_ofs;
54 int *intargs;
55 size_t n;
57 DATA_BLOB *pointers;
59 pointers = talloc_array(mem_ctx, DATA_BLOB, strlen(format));
60 intargs = talloc_array(pointers, int, strlen(format));
62 /* first scan the format to work out the header and body size */
63 va_start(ap, format);
64 for (i=0; format[i]; i++) {
65 switch (format[i]) {
66 case 'U':
67 s = va_arg(ap, char *);
68 head_size += 8;
69 ret = push_ucs2_talloc(
70 pointers,
71 (smb_ucs2_t **)(void *)&pointers[i].data,
72 s, &n);
73 if (!ret) {
74 return false;
76 pointers[i].length = n;
77 pointers[i].length -= 2;
78 data_size += pointers[i].length;
79 break;
80 case 'A':
81 s = va_arg(ap, char *);
82 head_size += 8;
83 ret = push_ascii_talloc(
84 pointers, (char **)(void *)&pointers[i].data,
85 s, &n);
86 if (!ret) {
87 return false;
89 pointers[i].length = n;
90 pointers[i].length -= 1;
91 data_size += pointers[i].length;
92 break;
93 case 'a':
94 j = va_arg(ap, int);
95 intargs[i] = j;
96 s = va_arg(ap, char *);
97 ret = push_ucs2_talloc(
98 pointers,
99 (smb_ucs2_t **)(void *)&pointers[i].data,
100 s, &n);
101 if (!ret) {
102 return false;
104 pointers[i].length = n;
105 pointers[i].length -= 2;
106 data_size += pointers[i].length + 4;
107 break;
108 case 'B':
109 b = va_arg(ap, uint8_t *);
110 head_size += 8;
111 pointers[i].data = b;
112 pointers[i].length = va_arg(ap, int);
113 data_size += pointers[i].length;
114 break;
115 case 'b':
116 b = va_arg(ap, uint8_t *);
117 pointers[i].data = b;
118 pointers[i].length = va_arg(ap, int);
119 head_size += pointers[i].length;
120 break;
121 case 'd':
122 j = va_arg(ap, int);
123 intargs[i] = j;
124 head_size += 4;
125 break;
126 case 'C':
127 s = va_arg(ap, char *);
128 pointers[i].data = (uint8_t *)s;
129 pointers[i].length = strlen(s)+1;
130 head_size += pointers[i].length;
131 break;
134 va_end(ap);
136 /* allocate the space, then scan the format again to fill in the values */
137 *blob = data_blob_talloc(mem_ctx, NULL, head_size + data_size);
139 head_ofs = 0;
140 data_ofs = head_size;
142 va_start(ap, format);
143 for (i=0; format[i]; i++) {
144 switch (format[i]) {
145 case 'U':
146 case 'A':
147 case 'B':
148 n = pointers[i].length;
149 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
150 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
151 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
152 if (pointers[i].data && n) /* don't follow null pointers... */
153 memcpy(blob->data+data_ofs, pointers[i].data, n);
154 data_ofs += n;
155 break;
156 case 'a':
157 j = intargs[i];
158 SSVAL(blob->data, data_ofs, j); data_ofs += 2;
160 n = pointers[i].length;
161 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
162 if (n >= 0) {
163 memcpy(blob->data+data_ofs, pointers[i].data, n);
165 data_ofs += n;
166 break;
167 case 'd':
168 j = intargs[i];
169 SIVAL(blob->data, head_ofs, j);
170 head_ofs += 4;
171 break;
172 case 'b':
173 n = pointers[i].length;
174 memcpy(blob->data + head_ofs, pointers[i].data, n);
175 head_ofs += n;
176 break;
177 case 'C':
178 n = pointers[i].length;
179 memcpy(blob->data + head_ofs, pointers[i].data, n);
180 head_ofs += n;
181 break;
184 va_end(ap);
186 talloc_free(pointers);
188 return true;
192 /* a helpful macro to avoid running over the end of our blob */
193 #define NEED_DATA(amount) \
194 if ((head_ofs + amount) > blob->length) { \
195 return false; \
199 this is a tiny msrpc packet parser. This the the partner of msrpc_gen
201 format specifiers are:
203 U = unicode string (output is unix string)
204 A = ascii string
205 B = data blob
206 b = data blob in header
207 d = word (4 bytes)
208 C = constant ascii string
211 bool msrpc_parse(TALLOC_CTX *mem_ctx,
212 const DATA_BLOB *blob,
213 const char *format, ...)
215 int i;
216 va_list ap;
217 char **ps, *s;
218 DATA_BLOB *b;
219 size_t head_ofs = 0;
220 uint16_t len1, len2;
221 uint32_t ptr;
222 uint32_t *v;
223 size_t p_len = 1024;
224 char *p = talloc_array(mem_ctx, char, p_len);
225 bool ret = true;
227 va_start(ap, format);
228 for (i=0; format[i]; i++) {
229 switch (format[i]) {
230 case 'U':
231 NEED_DATA(8);
232 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
233 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
234 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
236 ps = va_arg(ap, char **);
237 if (len1 == 0 && len2 == 0) {
238 *ps = (char *)discard_const("");
239 } else {
240 /* make sure its in the right format - be strict */
241 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
242 ret = false;
243 goto cleanup;
245 if (len1 & 1) {
246 /* if odd length and unicode */
247 ret = false;
248 goto cleanup;
250 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
251 blob->data + ptr < blob->data) {
252 ret = false;
253 goto cleanup;
256 if (0 < len1) {
257 size_t pull_len;
258 if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
259 blob->data + ptr, len1,
260 ps, &pull_len, false)) {
261 ret = false;
262 goto cleanup;
264 } else {
265 (*ps) = (char *)discard_const("");
268 break;
269 case 'A':
270 NEED_DATA(8);
271 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
272 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
273 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
275 ps = (char **)va_arg(ap, char **);
276 /* make sure its in the right format - be strict */
277 if (len1 == 0 && len2 == 0) {
278 *ps = (char *)discard_const("");
279 } else {
280 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
281 ret = false;
282 goto cleanup;
285 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
286 blob->data + ptr < blob->data) {
287 ret = false;
288 goto cleanup;
291 if (0 < len1) {
292 size_t pull_len;
294 if (!convert_string_talloc(mem_ctx, CH_DOS, CH_UNIX,
295 blob->data + ptr, len1,
296 ps, &pull_len, false)) {
297 ret = false;
298 goto cleanup;
300 } else {
301 (*ps) = (char *)discard_const("");
304 break;
305 case 'B':
306 NEED_DATA(8);
307 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
308 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
309 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
311 b = (DATA_BLOB *)va_arg(ap, void *);
312 if (len1 == 0 && len2 == 0) {
313 *b = data_blob_talloc(mem_ctx, NULL, 0);
314 } else {
315 /* make sure its in the right format - be strict */
316 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
317 ret = false;
318 goto cleanup;
321 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
322 blob->data + ptr < blob->data) {
323 ret = false;
324 goto cleanup;
327 *b = data_blob_talloc(mem_ctx, blob->data + ptr, len1);
329 break;
330 case 'b':
331 b = (DATA_BLOB *)va_arg(ap, void *);
332 len1 = va_arg(ap, uint_t);
333 /* make sure its in the right format - be strict */
334 NEED_DATA(len1);
335 if (blob->data + head_ofs < (uint8_t *)head_ofs ||
336 blob->data + head_ofs < blob->data) {
337 ret = false;
338 goto cleanup;
341 *b = data_blob_talloc(mem_ctx, blob->data + head_ofs, len1);
342 head_ofs += len1;
343 break;
344 case 'd':
345 v = va_arg(ap, uint32_t *);
346 NEED_DATA(4);
347 *v = IVAL(blob->data, head_ofs); head_ofs += 4;
348 break;
349 case 'C':
350 s = va_arg(ap, char *);
352 if (blob->data + head_ofs < (uint8_t *)head_ofs ||
353 blob->data + head_ofs < blob->data ||
354 (head_ofs + (strlen(s) + 1)) > blob->length) {
355 ret = false;
356 goto cleanup;
359 if (memcmp(blob->data + head_ofs, s, strlen(s)+1) != 0) {
360 ret = false;
361 goto cleanup;
363 head_ofs += (strlen(s) + 1);
365 break;
369 cleanup:
370 va_end(ap);
371 talloc_free(p);
372 return ret;