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,
45 struct smb_iconv_convenience *iconv_convenience,
47 const char *format, ...)
54 int head_size=0, data_size=0;
55 int head_ofs, data_ofs;
60 pointers = talloc_array(mem_ctx, DATA_BLOB, strlen(format));
61 intargs = talloc_array(pointers, int, strlen(format));
63 /* first scan the format to work out the header and body size */
65 for (i=0; format[i]; i++) {
68 s = va_arg(ap, char *);
70 n = push_ucs2_talloc(pointers, iconv_convenience, (void **)&pointers[i].data, s);
74 pointers[i].length = n;
75 pointers[i].length -= 2;
76 data_size += pointers[i].length;
79 s = va_arg(ap, char *);
81 n = push_ascii_talloc(pointers, iconv_convenience, (char **)&pointers[i].data, s);
85 pointers[i].length = n;
86 pointers[i].length -= 1;
87 data_size += pointers[i].length;
92 s = va_arg(ap, char *);
93 n = push_ucs2_talloc(pointers, iconv_convenience, (void **)&pointers[i].data, s);
97 pointers[i].length = n;
98 pointers[i].length -= 2;
99 data_size += pointers[i].length + 4;
102 b = va_arg(ap, uint8_t *);
104 pointers[i].data = b;
105 pointers[i].length = va_arg(ap, int);
106 data_size += pointers[i].length;
109 b = va_arg(ap, uint8_t *);
110 pointers[i].data = b;
111 pointers[i].length = va_arg(ap, int);
112 head_size += pointers[i].length;
120 s = va_arg(ap, char *);
121 pointers[i].data = (uint8_t *)s;
122 pointers[i].length = strlen(s)+1;
123 head_size += pointers[i].length;
129 /* allocate the space, then scan the format again to fill in the values */
130 *blob = data_blob_talloc(mem_ctx, NULL, head_size + data_size);
133 data_ofs = head_size;
135 va_start(ap, format);
136 for (i=0; format[i]; i++) {
141 n = pointers[i].length;
142 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
143 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
144 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
145 if (pointers[i].data && n) /* don't follow null pointers... */
146 memcpy(blob->data+data_ofs, pointers[i].data, n);
151 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
153 n = pointers[i].length;
154 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
156 memcpy(blob->data+data_ofs, pointers[i].data, n);
162 SIVAL(blob->data, head_ofs, n);
166 n = pointers[i].length;
167 memcpy(blob->data + head_ofs, pointers[i].data, n);
171 n = pointers[i].length;
172 memcpy(blob->data + head_ofs, pointers[i].data, n);
179 talloc_free(pointers);
185 /* a helpful macro to avoid running over the end of our blob */
186 #define NEED_DATA(amount) \
187 if ((head_ofs + amount) > blob->length) { \
192 this is a tiny msrpc packet parser. This the the partner of msrpc_gen
194 format specifiers are:
196 U = unicode string (output is unix string)
199 b = data blob in header
201 C = constant ascii string
204 bool msrpc_parse(TALLOC_CTX *mem_ctx,
205 struct smb_iconv_convenience *iconv_convenience,
206 const DATA_BLOB *blob,
207 const char *format, ...)
218 char *p = talloc_array(mem_ctx, char, p_len);
221 va_start(ap, format);
222 for (i=0; format[i]; i++) {
226 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
227 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
228 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
230 ps = (const char **)va_arg(ap, char **);
231 if (len1 == 0 && len2 == 0) {
234 /* make sure its in the right format - be strict */
235 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
240 /* if odd length and unicode */
244 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
245 blob->data + ptr < blob->data) {
251 pull_string(iconv_convenience, p, blob->data + ptr, p_len,
252 len1, STR_UNICODE|STR_NOALIGN);
253 (*ps) = talloc_strdup(mem_ctx, p);
265 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
266 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
267 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
269 ps = (const char **)va_arg(ap, char **);
270 /* make sure its in the right format - be strict */
271 if (len1 == 0 && len2 == 0) {
274 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
279 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
280 blob->data + ptr < blob->data) {
286 pull_string(iconv_convenience, p, blob->data + ptr, p_len,
287 len1, STR_ASCII|STR_NOALIGN);
288 (*ps) = talloc_strdup(mem_ctx, p);
300 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
301 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
302 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
304 b = (DATA_BLOB *)va_arg(ap, void *);
305 if (len1 == 0 && len2 == 0) {
306 *b = data_blob_talloc(mem_ctx, NULL, 0);
308 /* make sure its in the right format - be strict */
309 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
314 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
315 blob->data + ptr < blob->data) {
320 *b = data_blob_talloc(mem_ctx, blob->data + ptr, len1);
324 b = (DATA_BLOB *)va_arg(ap, void *);
325 len1 = va_arg(ap, uint_t);
326 /* make sure its in the right format - be strict */
328 if (blob->data + head_ofs < (uint8_t *)head_ofs ||
329 blob->data + head_ofs < blob->data) {
334 *b = data_blob_talloc(mem_ctx, blob->data + head_ofs, len1);
338 v = va_arg(ap, uint32_t *);
340 *v = IVAL(blob->data, head_ofs); head_ofs += 4;
343 s = va_arg(ap, char *);
345 if (blob->data + head_ofs < (uint8_t *)head_ofs ||
346 blob->data + head_ofs < blob->data) {
351 head_ofs += pull_string(iconv_convenience, p,
352 blob->data+head_ofs, p_len,
353 blob->length - head_ofs,
354 STR_ASCII|STR_TERMINATE);
355 if (strcmp(s, p) != 0) {