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