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 "auth/ntlmssp/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(pointers, (smb_ucs2_t **)&pointers[i].data,
74 pointers[i].length = n;
75 pointers[i].length -= 2;
76 data_size += pointers[i].length;
79 s = va_arg(ap, char *);
81 ret = push_ascii_talloc(pointers, (char **)&pointers[i].data, s,
86 pointers[i].length = n;
87 pointers[i].length -= 1;
88 data_size += pointers[i].length;
93 s = va_arg(ap, char *);
94 ret = push_ucs2_talloc(pointers, (smb_ucs2_t **)&pointers[i].data,
99 pointers[i].length = n;
100 pointers[i].length -= 2;
101 data_size += pointers[i].length + 4;
104 b = va_arg(ap, uint8_t *);
106 pointers[i].data = b;
107 pointers[i].length = va_arg(ap, int);
108 data_size += pointers[i].length;
111 b = va_arg(ap, uint8_t *);
112 pointers[i].data = b;
113 pointers[i].length = va_arg(ap, int);
114 head_size += pointers[i].length;
122 s = va_arg(ap, char *);
123 pointers[i].data = (uint8_t *)s;
124 pointers[i].length = strlen(s)+1;
125 head_size += pointers[i].length;
131 /* allocate the space, then scan the format again to fill in the values */
132 *blob = data_blob_talloc(mem_ctx, NULL, head_size + data_size);
135 data_ofs = head_size;
137 va_start(ap, format);
138 for (i=0; format[i]; i++) {
143 n = pointers[i].length;
144 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
145 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
146 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
147 if (pointers[i].data && n) /* don't follow null pointers... */
148 memcpy(blob->data+data_ofs, pointers[i].data, n);
153 SSVAL(blob->data, data_ofs, j); data_ofs += 2;
155 n = pointers[i].length;
156 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
158 memcpy(blob->data+data_ofs, pointers[i].data, n);
164 SIVAL(blob->data, head_ofs, j);
168 n = pointers[i].length;
169 memcpy(blob->data + head_ofs, pointers[i].data, n);
173 n = pointers[i].length;
174 memcpy(blob->data + head_ofs, pointers[i].data, n);
181 talloc_free(pointers);
187 /* a helpful macro to avoid running over the end of our blob */
188 #define NEED_DATA(amount) \
189 if ((head_ofs + amount) > blob->length) { \
194 this is a tiny msrpc packet parser. This the the partner of msrpc_gen
196 format specifiers are:
198 U = unicode string (output is unix string)
201 b = data blob in header
203 C = constant ascii string
206 bool msrpc_parse(TALLOC_CTX *mem_ctx,
207 const DATA_BLOB *blob,
208 const char *format, ...)
219 char *p = talloc_array(mem_ctx, char, p_len);
222 va_start(ap, format);
223 for (i=0; format[i]; i++) {
227 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
228 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
229 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
231 ps = va_arg(ap, char **);
232 if (len1 == 0 && len2 == 0) {
233 *ps = discard_const("");
235 /* make sure its in the right format - be strict */
236 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
241 /* if odd length and unicode */
245 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
246 blob->data + ptr < blob->data) {
253 if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
254 blob->data + ptr, len1,
255 ps, &pull_len, false)) {
260 (*ps) = discard_const("");
266 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
267 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
268 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
270 ps = (char **)va_arg(ap, char **);
271 /* make sure its in the right format - be strict */
272 if (len1 == 0 && len2 == 0) {
273 *ps = discard_const("");
275 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
280 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
281 blob->data + ptr < blob->data) {
289 if (!convert_string_talloc(mem_ctx, CH_DOS, CH_UNIX,
290 blob->data + ptr, len1,
291 ps, &pull_len, false)) {
296 (*ps) = discard_const("");
302 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
303 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
304 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
306 b = (DATA_BLOB *)va_arg(ap, void *);
307 if (len1 == 0 && len2 == 0) {
308 *b = data_blob_talloc(mem_ctx, NULL, 0);
310 /* make sure its in the right format - be strict */
311 if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
316 if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
317 blob->data + ptr < blob->data) {
322 *b = data_blob_talloc(mem_ctx, blob->data + ptr, len1);
326 b = (DATA_BLOB *)va_arg(ap, void *);
327 len1 = va_arg(ap, uint_t);
328 /* make sure its in the right format - be strict */
330 if (blob->data + head_ofs < (uint8_t *)head_ofs ||
331 blob->data + head_ofs < blob->data) {
336 *b = data_blob_talloc(mem_ctx, blob->data + head_ofs, len1);
340 v = va_arg(ap, uint32_t *);
342 *v = IVAL(blob->data, head_ofs); head_ofs += 4;
345 s = va_arg(ap, char *);
347 if (blob->data + head_ofs < (uint8_t *)head_ofs ||
348 blob->data + head_ofs < blob->data ||
349 (head_ofs + (strlen(s) + 1)) > blob->length) {
354 if (memcmp(blob->data + head_ofs, s, strlen(s)+1) != 0) {
358 head_ofs += (strlen(s) + 1);