trying to get HEAD building again. If you want the code
[abartlet/samba.git/.git] / source3 / libsmb / ntlmssp_parse.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple kerberos5/SPNEGO routines
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Jim McDonough   2002
6    Copyright (C) Andrew Bartlett 2002-2003
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24
25 /*
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
30
31   format specifiers are:
32
33   U = unicode string (input is unix string)
34   a = address (input is BOOL unicode, 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)
39   D
40   d = word (4 bytes)
41   C = constant ascii string
42  */
43 BOOL msrpc_gen(DATA_BLOB *blob,
44                const char *format, ...)
45 {
46         int i, n;
47         va_list ap;
48         char *s;
49         uint8 *b;
50         int head_size=0, data_size=0;
51         int head_ofs, data_ofs;
52         BOOL unicode;
53
54         /* first scan the format to work out the header and body size */
55         va_start(ap, format);
56         for (i=0; format[i]; i++) {
57                 switch (format[i]) {
58                 case 'U':
59                         s = va_arg(ap, char *);
60                         head_size += 8;
61                         data_size += str_charnum(s) * 2;
62                         break;
63                 case 'A':
64                         s = va_arg(ap, char *);
65                         head_size += 8;
66                         data_size += str_ascii_charnum(s);
67                         break;
68                 case 'a':
69                         unicode = va_arg(ap, BOOL);
70                         n = va_arg(ap, int);
71                         s = va_arg(ap, char *);
72                         if (unicode) {
73                                 data_size += (str_charnum(s) * 2) + 4;
74                         } else {
75                                 data_size += (str_ascii_charnum(s)) + 4;
76                         }
77                         break;
78                 case 'B':
79                         b = va_arg(ap, uint8 *);
80                         head_size += 8;
81                         data_size += va_arg(ap, int);
82                         break;
83                 case 'b':
84                         b = va_arg(ap, uint8 *);
85                         head_size += va_arg(ap, int);
86                         break;
87                 case 'd':
88                         n = va_arg(ap, int);
89                         head_size += 4;
90                         break;
91                 case 'C':
92                         s = va_arg(ap, char *);
93                         head_size += str_charnum(s) + 1;
94                         break;
95                 }
96         }
97         va_end(ap);
98
99         /* allocate the space, then scan the format again to fill in the values */
100         *blob = data_blob(NULL, head_size + data_size);
101
102         head_ofs = 0;
103         data_ofs = head_size;
104
105         va_start(ap, format);
106         for (i=0; format[i]; i++) {
107                 switch (format[i]) {
108                 case 'U':
109                         s = va_arg(ap, char *);
110                         n = str_charnum(s);
111                         SSVAL(blob->data, head_ofs, n*2); head_ofs += 2;
112                         SSVAL(blob->data, head_ofs, n*2); head_ofs += 2;
113                         SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
114                         push_string(NULL, blob->data+data_ofs, s, n*2, STR_UNICODE|STR_NOALIGN);
115                         data_ofs += n*2;
116                         break;
117                 case 'A':
118                         s = va_arg(ap, char *);
119                         n = str_ascii_charnum(s);
120                         SSVAL(blob->data, head_ofs, n); head_ofs += 2;
121                         SSVAL(blob->data, head_ofs, n); head_ofs += 2;
122                         SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
123                         push_string(NULL, blob->data+data_ofs, s, n, STR_ASCII|STR_NOALIGN);
124                         data_ofs += n;
125                         break;
126                 case 'a':
127                         unicode = va_arg(ap, BOOL);
128                         n = va_arg(ap, int);
129                         SSVAL(blob->data, data_ofs, n); data_ofs += 2;
130                         s = va_arg(ap, char *);
131                         if (unicode) {
132                                 n = str_charnum(s);
133                                 SSVAL(blob->data, data_ofs, n*2); data_ofs += 2;
134                                 if (0 < n) {
135                                         push_string(NULL, blob->data+data_ofs, s, n*2,
136                                                     STR_UNICODE|STR_NOALIGN);
137                                 }
138                                 data_ofs += n*2;
139                         } else {
140                                 n = str_ascii_charnum(s);
141                                 SSVAL(blob->data, data_ofs, n); data_ofs += 2;
142                                 if (0 < n) {
143                                         push_string(NULL, blob->data+data_ofs, s, n,
144                                                     STR_ASCII|STR_NOALIGN);
145                                 }
146                                 data_ofs += n;
147                         }
148                         break;
149
150                 case 'B':
151                         b = va_arg(ap, uint8 *);
152                         n = va_arg(ap, int);
153                         SSVAL(blob->data, head_ofs, n); head_ofs += 2;
154                         SSVAL(blob->data, head_ofs, n); head_ofs += 2;
155                         SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
156                         memcpy(blob->data+data_ofs, b, n);
157                         data_ofs += n;
158                         break;
159                 case 'd':
160                         n = va_arg(ap, int);
161                         SIVAL(blob->data, head_ofs, n); head_ofs += 4;
162                         break;
163                 case 'b':
164                         b = va_arg(ap, uint8 *);
165                         n = va_arg(ap, int);
166                         memcpy(blob->data + head_ofs, b, n);
167                         head_ofs += n;
168                         break;
169                 case 'C':
170                         s = va_arg(ap, char *);
171                         head_ofs += push_string(NULL, blob->data+head_ofs, s, -1, 
172                                                 STR_ASCII|STR_TERMINATE);
173                         break;
174                 }
175         }
176         va_end(ap);
177
178         return True;
179 }
180
181
182 /* a helpful macro to avoid running over the end of our blob */
183 #define NEED_DATA(amount) \
184 if ((head_ofs + amount) > blob->length) { \
185         return False; \
186 }
187
188 /*
189   this is a tiny msrpc packet parser. This the the partner of msrpc_gen
190
191   format specifiers are:
192
193   U = unicode string (output is unix string)
194   A = ascii string
195   B = data blob
196   b = data blob in header
197   d = word (4 bytes)
198   C = constant ascii string
199  */
200
201 BOOL msrpc_parse(const DATA_BLOB *blob,
202                  const char *format, ...)
203 {
204         int i;
205         va_list ap;
206         char **ps, *s;
207         DATA_BLOB *b;
208         size_t head_ofs = 0;
209         uint16 len1, len2;
210         uint32 ptr;
211         uint32 *v;
212         pstring p;
213
214         va_start(ap, format);
215         for (i=0; format[i]; i++) {
216                 switch (format[i]) {
217                 case 'U':
218                         NEED_DATA(8);
219                         len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
220                         len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
221                         ptr =  IVAL(blob->data, head_ofs); head_ofs += 4;
222
223                         ps = va_arg(ap, char **);
224                         if (len1 == 0 && len2 == 0) {
225                                 *ps = smb_xstrdup("");
226                         } else {
227                                 /* make sure its in the right format - be strict */
228                                 if (len1 != len2 || ptr + len1 > blob->length) {
229                                         return False;
230                                 }
231                                 if (len1 & 1) {
232                                         /* if odd length and unicode */
233                                         return False;
234                                 }
235                                 
236                                 if (0 < len1) {
237                                         pull_string(NULL, p, blob->data + ptr, sizeof(p), 
238                                                     len1, 
239                                                     STR_UNICODE|STR_NOALIGN);
240                                         (*ps) = smb_xstrdup(p);
241                                 } else {
242                                         (*ps) = smb_xstrdup("");
243                                 }
244                         }
245                         break;
246                 case 'A':
247                         NEED_DATA(8);
248                         len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
249                         len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
250                         ptr =  IVAL(blob->data, head_ofs); head_ofs += 4;
251
252                         ps = va_arg(ap, char **);
253                         /* make sure its in the right format - be strict */
254                         if (len1 == 0 && len2 == 0) {
255                                 *ps = smb_xstrdup("");
256                         } else {
257                                 if (len1 != len2 || ptr + len1 > blob->length) {
258                                         return False;
259                                 }
260                                 
261                                 if (0 < len1) {
262                                         pull_string(NULL, p, blob->data + ptr, sizeof(p), 
263                                                     len1, 
264                                                     STR_ASCII|STR_NOALIGN);
265                                         (*ps) = smb_xstrdup(p);
266                                 } else {
267                                         (*ps) = smb_xstrdup("");
268                                 }
269                         }
270                         break;
271                 case 'B':
272                         NEED_DATA(8);
273                         len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
274                         len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
275                         ptr =  IVAL(blob->data, head_ofs); head_ofs += 4;
276
277                         b = (DATA_BLOB *)va_arg(ap, void *);
278                         if (len1 == 0 && len2 == 0) {
279                                 *b = data_blob(NULL, 0);
280                         } else {
281                                 /* make sure its in the right format - be strict */
282                                 if (len1 != len2 || ptr + len1 > blob->length) {
283                                         return False;
284                                 }
285                                 *b = data_blob(blob->data + ptr, len1);
286                         }
287                         break;
288                 case 'b':
289                         b = (DATA_BLOB *)va_arg(ap, void *);
290                         len1 = va_arg(ap, unsigned);
291                         /* make sure its in the right format - be strict */
292                         NEED_DATA(len1);
293                         *b = data_blob(blob->data + head_ofs, len1);
294                         head_ofs += len1;
295                         break;
296                 case 'd':
297                         v = va_arg(ap, uint32 *);
298                         NEED_DATA(4);
299                         *v = IVAL(blob->data, head_ofs); head_ofs += 4;
300                         break;
301                 case 'C':
302                         s = va_arg(ap, char *);
303                         head_ofs += pull_string(NULL, p, blob->data+head_ofs, sizeof(p), 
304                                                 blob->length - head_ofs, 
305                                                 STR_ASCII|STR_TERMINATE);
306                         if (strcmp(s, p) != 0) {
307                                 return False;
308                         }
309                         break;
310                 }
311         }
312         va_end(ap);
313
314         return True;
315 }
316