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/>.
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)
41 C = constant ascii string
43 bool msrpc_gen(TALLOC_CTX *mem_ctx,
45 const char *format, ...)
52 int head_size=0, data_size=0;
53 int head_ofs, data_ofs;
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 */
64 for (i=0; format[i]; i++) {
67 s = va_arg(ap, char *);
69 ret = push_ucs2_talloc(
71 (smb_ucs2_t **)(void *)&pointers[i].data,
77 pointers[i].length = n;
78 pointers[i].length -= 2;
79 data_size += pointers[i].length;
82 s = va_arg(ap, char *);
84 ret = push_ascii_talloc(
85 pointers, (char **)(void *)&pointers[i].data,
91 pointers[i].length = n;
92 pointers[i].length -= 1;
93 data_size += pointers[i].length;
98 s = va_arg(ap, char *);
99 ret = push_ucs2_talloc(
101 (smb_ucs2_t **)(void *)&pointers[i].data,
107 pointers[i].length = n;
108 pointers[i].length -= 2;
109 data_size += pointers[i].length + 4;
112 b = va_arg(ap, uint8_t *);
114 pointers[i].data = b;
115 pointers[i].length = va_arg(ap, int);
116 data_size += pointers[i].length;
119 b = va_arg(ap, uint8_t *);
120 pointers[i].data = b;
121 pointers[i].length = va_arg(ap, int);
122 head_size += pointers[i].length;
130 s = va_arg(ap, char *);
131 pointers[i].data = (uint8_t *)s;
132 pointers[i].length = strlen(s)+1;
133 head_size += pointers[i].length;
139 /* allocate the space, then scan the format again to fill in the values */
140 *blob = data_blob_talloc(mem_ctx, NULL, head_size + data_size);
143 data_ofs = head_size;
145 va_start(ap, format);
146 for (i=0; format[i]; i++) {
151 n = pointers[i].length;
152 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
153 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
154 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
155 if (pointers[i].data && n) /* don't follow null pointers... */
156 memcpy(blob->data+data_ofs, pointers[i].data, n);
161 SSVAL(blob->data, data_ofs, j); data_ofs += 2;
163 n = pointers[i].length;
164 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
166 memcpy(blob->data+data_ofs, pointers[i].data, n);
172 SIVAL(blob->data, head_ofs, j);
176 n = pointers[i].length;
177 memcpy(blob->data + head_ofs, pointers[i].data, n);
181 n = pointers[i].length;
182 memcpy(blob->data + head_ofs, pointers[i].data, n);
189 talloc_free(pointers);
195 /* a helpful macro to avoid running over the end of our blob */
196 #define NEED_DATA(amount) \
197 if ((head_ofs + amount) > blob->length) { \
203 this is a tiny msrpc packet parser. This the the partner of msrpc_gen
205 format specifiers are:
207 U = unicode string (output is unix string)
210 b = data blob in header
212 C = constant ascii string
215 bool msrpc_parse(TALLOC_CTX *mem_ctx,
216 const DATA_BLOB *blob,
217 const char *format, ...)
228 char *p = talloc_array(mem_ctx, char, p_len);
231 va_start(ap, format);
232 for (i=0; format[i]; i++) {
236 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
237 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
238 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
240 ps = va_arg(ap, char **);
241 if (len1 == 0 && len2 == 0) {
242 *ps = (char *)discard_const("");
244 /* make sure its in the right format - be strict */
245 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
250 /* if odd length and unicode */
254 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
255 blob->data + ptr < blob->data) {
262 if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
263 blob->data + ptr, len1,
264 ps, &pull_len, false)) {
269 (*ps) = (char *)discard_const("");
275 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
276 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
277 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
279 ps = (char **)va_arg(ap, char **);
280 /* make sure its in the right format - be strict */
281 if (len1 == 0 && len2 == 0) {
282 *ps = (char *)discard_const("");
284 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
289 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
290 blob->data + ptr < blob->data) {
298 if (!convert_string_talloc(mem_ctx, CH_DOS, CH_UNIX,
299 blob->data + ptr, len1,
300 ps, &pull_len, false)) {
305 (*ps) = (char *)discard_const("");
311 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
312 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
313 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
315 b = (DATA_BLOB *)va_arg(ap, void *);
316 if (len1 == 0 && len2 == 0) {
317 *b = data_blob_talloc(mem_ctx, NULL, 0);
319 /* make sure its in the right format - be strict */
320 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
325 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
326 blob->data + ptr < blob->data) {
331 *b = data_blob_talloc(mem_ctx, blob->data + ptr, len1);
335 b = (DATA_BLOB *)va_arg(ap, void *);
336 len1 = va_arg(ap, uint_t);
337 /* make sure its in the right format - be strict */
339 if (blob->data + head_ofs < (uint8_t *)head_ofs ||
340 blob->data + head_ofs < blob->data) {
345 *b = data_blob_talloc(mem_ctx, blob->data + head_ofs, len1);
349 v = va_arg(ap, uint32_t *);
351 *v = IVAL(blob->data, head_ofs); head_ofs += 4;
354 s = va_arg(ap, char *);
356 if (blob->data + head_ofs < (uint8_t *)head_ofs ||
357 blob->data + head_ofs < blob->data ||
358 (head_ofs + (strlen(s) + 1)) > blob->length) {
363 if (memcmp(blob->data + head_ofs, s, strlen(s)+1) != 0) {
367 head_ofs += (strlen(s) + 1);