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 "param/param.h"
24 #include "auth/ntlmssp/msrpc_parse.h"
27 this is a tiny msrpc packet generator. I am only using this to
28 avoid tying this code to a particular varient of our rpc code. This
29 generator is not general enough for all our rpc needs, its just
30 enough for the spnego/ntlmssp code
32 format specifiers are:
34 U = unicode string (input is unix string)
35 a = address (input is char *unix_string)
36 (1 byte type, 1 byte length, unicode/ASCII string, all inline)
37 A = ASCII string (input is unix string)
38 B = data blob (pointer + length)
39 b = data blob in header (pointer + length)
42 C = constant ascii string
44 bool msrpc_gen(TALLOC_CTX *mem_ctx,
46 const char *format, ...)
53 int head_size=0, data_size=0;
54 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 n = push_ucs2_talloc(pointers, (void **)&pointers[i].data, s);
73 pointers[i].length = n;
74 pointers[i].length -= 2;
75 data_size += pointers[i].length;
78 s = va_arg(ap, char *);
80 n = push_ascii_talloc(pointers, (char **)&pointers[i].data, s);
84 pointers[i].length = n;
85 pointers[i].length -= 1;
86 data_size += pointers[i].length;
91 s = va_arg(ap, char *);
92 n = push_ucs2_talloc(pointers, (void **)&pointers[i].data, s);
96 pointers[i].length = n;
97 pointers[i].length -= 2;
98 data_size += pointers[i].length + 4;
101 b = va_arg(ap, uint8_t *);
103 pointers[i].data = b;
104 pointers[i].length = va_arg(ap, int);
105 data_size += pointers[i].length;
108 b = va_arg(ap, uint8_t *);
109 pointers[i].data = b;
110 pointers[i].length = va_arg(ap, int);
111 head_size += pointers[i].length;
119 s = va_arg(ap, char *);
120 pointers[i].data = (uint8_t *)s;
121 pointers[i].length = strlen(s)+1;
122 head_size += pointers[i].length;
128 /* allocate the space, then scan the format again to fill in the values */
129 *blob = data_blob_talloc(mem_ctx, NULL, head_size + data_size);
132 data_ofs = head_size;
134 va_start(ap, format);
135 for (i=0; format[i]; i++) {
140 n = pointers[i].length;
141 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
142 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
143 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
144 if (pointers[i].data && n) /* don't follow null pointers... */
145 memcpy(blob->data+data_ofs, pointers[i].data, n);
150 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
152 n = pointers[i].length;
153 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
155 memcpy(blob->data+data_ofs, pointers[i].data, n);
161 SIVAL(blob->data, head_ofs, n);
165 n = pointers[i].length;
166 memcpy(blob->data + head_ofs, pointers[i].data, n);
170 n = pointers[i].length;
171 memcpy(blob->data + head_ofs, pointers[i].data, n);
178 talloc_free(pointers);
184 /* a helpful macro to avoid running over the end of our blob */
185 #define NEED_DATA(amount) \
186 if ((head_ofs + amount) > blob->length) { \
191 this is a tiny msrpc packet parser. This the the partner of msrpc_gen
193 format specifiers are:
195 U = unicode string (output is unix string)
198 b = data blob in header
200 C = constant ascii string
203 bool msrpc_parse(TALLOC_CTX *mem_ctx,
204 struct smb_iconv_convenience *iconv_convenience,
205 const DATA_BLOB *blob,
206 const char *format, ...)
217 char *p = talloc_array(mem_ctx, char, p_len);
220 va_start(ap, format);
221 for (i=0; format[i]; i++) {
225 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
226 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
227 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
229 ps = (const char **)va_arg(ap, char **);
230 if (len1 == 0 && len2 == 0) {
233 /* make sure its in the right format - be strict */
234 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
239 /* if odd length and unicode */
243 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
244 blob->data + ptr < blob->data) {
250 pull_string(p, blob->data + ptr, p_len,
251 len1, STR_UNICODE|STR_NOALIGN);
252 (*ps) = talloc_strdup(mem_ctx, p);
264 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
265 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
266 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
268 ps = (const char **)va_arg(ap, char **);
269 /* make sure its in the right format - be strict */
270 if (len1 == 0 && len2 == 0) {
273 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
278 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
279 blob->data + ptr < blob->data) {
285 pull_string(p, blob->data + ptr, p_len,
286 len1, STR_ASCII|STR_NOALIGN);
287 (*ps) = talloc_strdup(mem_ctx, p);
299 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
300 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
301 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
303 b = (DATA_BLOB *)va_arg(ap, void *);
304 if (len1 == 0 && len2 == 0) {
305 *b = data_blob_talloc(mem_ctx, NULL, 0);
307 /* make sure its in the right format - be strict */
308 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
313 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
314 blob->data + ptr < blob->data) {
319 *b = data_blob_talloc(mem_ctx, blob->data + ptr, len1);
323 b = (DATA_BLOB *)va_arg(ap, void *);
324 len1 = va_arg(ap, uint_t);
325 /* make sure its in the right format - be strict */
327 if (blob->data + head_ofs < (uint8_t *)head_ofs ||
328 blob->data + head_ofs < blob->data) {
333 *b = data_blob_talloc(mem_ctx, blob->data + head_ofs, len1);
337 v = va_arg(ap, uint32_t *);
339 *v = IVAL(blob->data, head_ofs); head_ofs += 4;
342 s = va_arg(ap, char *);
344 if (blob->data + head_ofs < (uint8_t *)head_ofs ||
345 blob->data + head_ofs < blob->data) {
350 head_ofs += pull_string(p,
351 blob->data+head_ofs, p_len,
352 blob->length - head_ofs,
353 STR_ASCII|STR_TERMINATE);
354 if (strcmp(s, p) != 0) {