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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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, DATA_BLOB *blob,
45 const char *format, ...)
52 int head_size=0, data_size=0;
53 int head_ofs, data_ofs;
58 pointers = talloc_array(mem_ctx, DATA_BLOB, strlen(format));
59 intargs = talloc_array(pointers, int, strlen(format));
61 /* first scan the format to work out the header and body size */
63 for (i=0; format[i]; i++) {
66 s = va_arg(ap, char *);
68 n = push_ucs2_talloc(pointers, (void **)&pointers[i].data, s);
72 pointers[i].length = n;
73 pointers[i].length -= 2;
74 data_size += pointers[i].length;
77 s = va_arg(ap, char *);
79 n = push_ascii_talloc(pointers, (char **)&pointers[i].data, s);
83 pointers[i].length = n;
84 pointers[i].length -= 1;
85 data_size += pointers[i].length;
90 s = va_arg(ap, char *);
91 n = push_ucs2_talloc(pointers, (void **)&pointers[i].data, s);
95 pointers[i].length = n;
96 pointers[i].length -= 2;
97 data_size += pointers[i].length + 4;
100 b = va_arg(ap, uint8_t *);
102 pointers[i].data = b;
103 pointers[i].length = va_arg(ap, int);
104 data_size += pointers[i].length;
107 b = va_arg(ap, uint8_t *);
108 pointers[i].data = b;
109 pointers[i].length = va_arg(ap, int);
110 head_size += pointers[i].length;
118 s = va_arg(ap, char *);
119 pointers[i].data = (uint8_t *)s;
120 pointers[i].length = strlen(s)+1;
121 head_size += pointers[i].length;
127 /* allocate the space, then scan the format again to fill in the values */
128 *blob = data_blob_talloc(mem_ctx, NULL, head_size + data_size);
131 data_ofs = head_size;
133 va_start(ap, format);
134 for (i=0; format[i]; i++) {
139 n = pointers[i].length;
140 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
141 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
142 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
143 if (pointers[i].data && n) /* don't follow null pointers... */
144 memcpy(blob->data+data_ofs, pointers[i].data, n);
149 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
151 n = pointers[i].length;
152 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
154 memcpy(blob->data+data_ofs, pointers[i].data, n);
160 SIVAL(blob->data, head_ofs, n);
164 n = pointers[i].length;
165 memcpy(blob->data + head_ofs, pointers[i].data, n);
169 n = pointers[i].length;
170 memcpy(blob->data + head_ofs, pointers[i].data, n);
177 talloc_free(pointers);
183 /* a helpful macro to avoid running over the end of our blob */
184 #define NEED_DATA(amount) \
185 if ((head_ofs + amount) > blob->length) { \
190 this is a tiny msrpc packet parser. This the the partner of msrpc_gen
192 format specifiers are:
194 U = unicode string (output is unix string)
197 b = data blob in header
199 C = constant ascii string
202 BOOL msrpc_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob,
203 const char *format, ...)
215 va_start(ap, format);
216 for (i=0; format[i]; i++) {
220 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
221 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
222 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
224 ps = (const char **)va_arg(ap, char **);
225 if (len1 == 0 && len2 == 0) {
228 /* make sure its in the right format - be strict */
229 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
233 /* if odd length and unicode */
236 if (blob->data + ptr < (uint8_t *)ptr || blob->data + ptr < blob->data)
240 pull_string(p, blob->data + ptr, sizeof(p),
241 len1, STR_UNICODE|STR_NOALIGN);
242 (*ps) = talloc_strdup(mem_ctx, p);
253 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
254 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
255 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
257 ps = (const char **)va_arg(ap, char **);
258 /* make sure its in the right format - be strict */
259 if (len1 == 0 && len2 == 0) {
262 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
266 if (blob->data + ptr < (uint8_t *)ptr || blob->data + ptr < blob->data)
270 pull_string(p, blob->data + ptr, sizeof(p),
271 len1, STR_ASCII|STR_NOALIGN);
272 (*ps) = talloc_strdup(mem_ctx, p);
283 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
284 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
285 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
287 b = (DATA_BLOB *)va_arg(ap, void *);
288 if (len1 == 0 && len2 == 0) {
289 *b = data_blob_talloc(mem_ctx, NULL, 0);
291 /* make sure its in the right format - be strict */
292 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
296 if (blob->data + ptr < (uint8_t *)ptr || blob->data + ptr < blob->data)
299 *b = data_blob_talloc(mem_ctx, blob->data + ptr, len1);
303 b = (DATA_BLOB *)va_arg(ap, void *);
304 len1 = va_arg(ap, uint_t);
305 /* make sure its in the right format - be strict */
307 if (blob->data + head_ofs < (uint8_t *)head_ofs || blob->data + head_ofs < blob->data)
310 *b = data_blob_talloc(mem_ctx, blob->data + head_ofs, len1);
314 v = va_arg(ap, uint32_t *);
316 *v = IVAL(blob->data, head_ofs); head_ofs += 4;
319 s = va_arg(ap, char *);
321 if (blob->data + head_ofs < (uint8_t *)head_ofs || blob->data + head_ofs < blob->data)
324 head_ofs += pull_string(p, blob->data+head_ofs, sizeof(p),
325 blob->length - head_ofs,
326 STR_ASCII|STR_TERMINATE);
327 if (strcmp(s, p) != 0) {