return results on prs_xxxxx() and all xxx_io_xxx() routines.
[samba.git] / source3 / rpc_parse / parse_rpc.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-1999,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1999,
8  *  Copyright (C) Paul Ashton                  1997-1999.
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25
26 #include "includes.h"
27
28 extern int DEBUGLEVEL;
29
30
31 /*******************************************************************
32 interface/version dce/rpc pipe identification
33 ********************************************************************/
34
35 #define TRANS_SYNT_V2             \
36 {                                 \
37         {                             \
38                 0x04, 0x5d, 0x88, 0x8a,   \
39                 0xeb, 0x1c, 0xc9, 0x11,   \
40                 0x9f, 0xe8, 0x08, 0x00,   \
41                 0x2b, 0x10, 0x48, 0x60    \
42         }, 0x02                       \
43 }                                 \
44
45 #define SYNT_SVCCTL_V2          \
46 {                                 \
47         {                             \
48                 0x81, 0xbb, 0x7a, 0x36,   \
49                 0x44, 0x98, 0xf1, 0x35,   \
50                 0xad, 0x32, 0x98, 0xf0,   \
51                 0x38, 0x00, 0x10, 0x03    \
52         }, 0x02                       \
53 }                                 \
54
55 #define SYNT_NETLOGON_V2          \
56 {                                 \
57         {                             \
58                 0x04, 0x5d, 0x88, 0x8a,   \
59                 0xeb, 0x1c, 0xc9, 0x11,   \
60                 0x9f, 0xe8, 0x08, 0x00,   \
61                 0x2b, 0x10, 0x48, 0x60    \
62         }, 0x02                       \
63 }                                 \
64
65 #define SYNT_WKSSVC_V1            \
66 {                                 \
67         {                             \
68                 0x98, 0xd0, 0xff, 0x6b,   \
69                 0x12, 0xa1, 0x10, 0x36,   \
70                 0x98, 0x33, 0x46, 0xc3,   \
71                 0xf8, 0x7e, 0x34, 0x5a    \
72         }, 0x01                       \
73 }                                 \
74
75 #define SYNT_SRVSVC_V3            \
76 {                                 \
77         {                             \
78                 0xc8, 0x4f, 0x32, 0x4b,   \
79                 0x70, 0x16, 0xd3, 0x01,   \
80                 0x12, 0x78, 0x5a, 0x47,   \
81                 0xbf, 0x6e, 0xe1, 0x88    \
82         }, 0x03                       \
83 }                                 \
84
85 #define SYNT_LSARPC_V0            \
86 {                                 \
87         {                             \
88                 0x78, 0x57, 0x34, 0x12,   \
89                 0x34, 0x12, 0xcd, 0xab,   \
90                 0xef, 0x00, 0x01, 0x23,   \
91                 0x45, 0x67, 0x89, 0xab    \
92         }, 0x00                       \
93 }                                 \
94
95 #define SYNT_SAMR_V1              \
96 {                                 \
97         {                             \
98                 0x78, 0x57, 0x34, 0x12,   \
99                 0x34, 0x12, 0xcd, 0xab,   \
100                 0xef, 0x00, 0x01, 0x23,   \
101                 0x45, 0x67, 0x89, 0xac    \
102         }, 0x01                       \
103 }                                 \
104
105 #define SYNT_NETLOGON_V1          \
106 {                                 \
107         {                             \
108                 0x78, 0x56, 0x34, 0x12,   \
109                 0x34, 0x12, 0xcd, 0xab,   \
110                 0xef, 0x00, 0x01, 0x23,   \
111                 0x45, 0x67, 0xcf, 0xfb    \
112         }, 0x01                       \
113 }                                 \
114
115 #define SYNT_WINREG_V1            \
116 {                                 \
117         {                             \
118                 0x01, 0xd0, 0x8c, 0x33,   \
119                 0x44, 0x22, 0xf1, 0x31,   \
120                 0xaa, 0xaa, 0x90, 0x00,   \
121                 0x38, 0x00, 0x10, 0x03    \
122         }, 0x01                       \
123 }                                 \
124
125 #define SYNT_ATSVC_V1            \
126 {                                 \
127         {                             \
128                 0x82, 0x06, 0xf7, 0x1f,   \
129                 0x51, 0x0a, 0xe8, 0x30,   \
130                 0x07, 0x6d, 0x74, 0x0b,   \
131                 0xe8, 0xce, 0xe9, 0x8b    \
132         }, 0x01                       \
133 }                                 \
134
135 #define SYNT_SPOOLSS_V1           \
136 {                                 \
137         {                             \
138                 0x78, 0x56, 0x34, 0x12,   \
139                 0x34, 0x12, 0xcd, 0xab,   \
140                 0xef, 0x00, 0x01, 0x23,   \
141                 0x45, 0x67, 0x89, 0xab    \
142         }, 0x01                       \
143 }                                 \
144
145 #define SYNT_NONE_V0              \
146 {                                 \
147         {                             \
148                 0x00, 0x00, 0x00, 0x00,   \
149                 0x00, 0x00, 0x00, 0x00,   \
150                 0x00, 0x00, 0x00, 0x00,   \
151                 0x00, 0x00, 0x00, 0x00    \
152         }, 0x00                       \
153 }                                 \
154
155 #define SYNT_EVENTLOG_V0            \
156 {                                 \
157         {                             \
158                 0xdc, 0x3f, 0x27, 0x82,   \
159                 0x2a, 0xe3, 0xc3, 0x18,   \
160                 0x3f, 0x78, 0x82, 0x79,   \
161                 0x29, 0xdc, 0x23, 0xea    \
162         }, 0x00                       \
163 }
164                                  \
165 struct pipe_id_info pipe_names [] =
166 {
167         /* client pipe , abstract syntax , server pipe   , transfer syntax */
168         { PIPE_LSARPC  , SYNT_LSARPC_V0  , PIPE_LSASS    , TRANS_SYNT_V2 },
169         { PIPE_SAMR    , SYNT_SAMR_V1    , PIPE_LSASS    , TRANS_SYNT_V2 },
170         { PIPE_NETLOGON, SYNT_NETLOGON_V1, PIPE_LSASS    , TRANS_SYNT_V2 },
171         { PIPE_SRVSVC  , SYNT_SRVSVC_V3  , PIPE_NTSVCS   , TRANS_SYNT_V2 },
172         { PIPE_SVCCTL  , SYNT_SVCCTL_V2  , PIPE_NTSVCS   , TRANS_SYNT_V2 },
173         { PIPE_WKSSVC  , SYNT_WKSSVC_V1  , PIPE_NTSVCS   , TRANS_SYNT_V2 },
174         { PIPE_WINREG  , SYNT_WINREG_V1  , PIPE_WINREG   , TRANS_SYNT_V2 },
175         { PIPE_ATSVC   , SYNT_ATSVC_V1   , PIPE_ATSVC    , TRANS_SYNT_V2 },
176         { PIPE_SPOOLSS , SYNT_SPOOLSS_V1 , PIPE_SPOOLSS  , TRANS_SYNT_V2 },
177         { PIPE_EVENTLOG, SYNT_EVENTLOG_V0, PIPE_EVENTLOG , TRANS_SYNT_V2 },
178         { NULL         , SYNT_NONE_V0    , NULL          , SYNT_NONE_V0  }
179 };
180
181 /*******************************************************************
182 creates an RPC_HDR structure.
183 ********************************************************************/
184 BOOL make_rpc_hdr(RPC_HDR *hdr, enum RPC_PKT_TYPE pkt_type, uint8 flags,
185                                 uint32 call_id, int data_len, int auth_len)
186 {
187         if (hdr == NULL) return False;
188
189         hdr->major        = 5;               /* RPC version 5 */
190         hdr->minor        = 0;               /* minor version 0 */
191         hdr->pkt_type     = pkt_type;        /* RPC packet type */
192         hdr->flags        = flags;           /* dce/rpc flags */
193         hdr->pack_type    = 0x10;            /* packed data representation */
194         hdr->frag_len     = data_len;        /* fragment length, fill in later */
195         hdr->auth_len     = auth_len;        /* authentication length */
196         hdr->call_id      = call_id;         /* call identifier - match incoming RPC */
197
198         return True;
199 }
200
201 /*******************************************************************
202 reads or writes an RPC_HDR structure.
203 ********************************************************************/
204 BOOL smb_io_rpc_hdr(char *desc,  RPC_HDR *rpc, prs_struct *ps, int depth)
205 {
206         if (rpc == NULL) return False;
207
208         prs_debug(ps, depth, desc, "smb_io_rpc_hdr");
209         depth++;
210
211         prs_uint8 ("major     ", ps, depth, &(rpc->major));
212         prs_uint8 ("minor     ", ps, depth, &(rpc->minor));
213         prs_uint8 ("pkt_type  ", ps, depth, &(rpc->pkt_type));
214         prs_uint8 ("flags     ", ps, depth, &(rpc->flags));
215         prs_uint32("pack_type ", ps, depth, &(rpc->pack_type));
216         prs_uint16("frag_len  ", ps, depth, &(rpc->frag_len));
217         prs_uint16("auth_len  ", ps, depth, &(rpc->auth_len));
218         prs_uint32("call_id   ", ps, depth, &(rpc->call_id));
219
220         return True;
221 }
222
223 /*******************************************************************
224 reads or writes an RPC_IFACE structure.
225 ********************************************************************/
226 static BOOL smb_io_rpc_iface(char *desc,  RPC_IFACE *ifc, prs_struct *ps, int depth)
227 {
228         if (ifc == NULL) return False;
229
230         prs_debug(ps, depth, desc, "smb_io_rpc_iface");
231         depth++;
232
233         prs_align(ps);
234
235         prs_uint8s (False, "data   ", ps, depth, ifc->data, sizeof(ifc->data));
236         prs_uint32 (       "version", ps, depth, &(ifc->version));
237
238         return True;
239 }
240
241 /*******************************************************************
242 creates an RPC_ADDR_STR structure.
243
244 The name can be null (RPC Alter-Context)
245 ********************************************************************/
246 static BOOL make_rpc_addr_str(RPC_ADDR_STR *str, char *name)
247 {
248         if (str == NULL ) return False;
249         if (name == NULL)
250         {
251                 str->len = 1;
252                 fstrcpy(str->str, "");
253         }
254         else
255         {
256                 str->len = strlen(name) + 1;
257                 fstrcpy(str->str, name);
258         }
259
260         return True;
261 }
262
263 /*******************************************************************
264 reads or writes an RPC_ADDR_STR structure.
265 ********************************************************************/
266 static BOOL smb_io_rpc_addr_str(char *desc,  RPC_ADDR_STR *str, prs_struct *ps, int depth)
267 {
268         if (str == NULL) return False;
269
270         prs_debug(ps, depth, desc, "smb_io_rpc_addr_str");
271         depth++;
272         prs_align(ps);
273
274         prs_uint16 (      "len", ps, depth, &(str->len));
275         prs_uint8s (True, "str", ps, depth, (uchar*)str->str, str->len);
276
277         return True;
278 }
279
280 /*******************************************************************
281 creates an RPC_HDR_BBA structure.
282 ********************************************************************/
283 static BOOL make_rpc_hdr_bba(RPC_HDR_BBA *bba, uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid)
284 {
285         if (bba == NULL) return False;
286
287         bba->max_tsize = max_tsize; /* maximum transmission fragment size (0x1630) */
288         bba->max_rsize = max_rsize; /* max receive fragment size (0x1630) */   
289         bba->assoc_gid = assoc_gid; /* associated group id (0x0) */ 
290
291         return True;
292 }
293
294 /*******************************************************************
295 reads or writes an RPC_HDR_BBA structure.
296 ********************************************************************/
297 static BOOL smb_io_rpc_hdr_bba(char *desc,  RPC_HDR_BBA *rpc, prs_struct *ps, int depth)
298 {
299         if (rpc == NULL) return False;
300
301         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_bba");
302         depth++;
303
304         prs_uint16("max_tsize", ps, depth, &(rpc->max_tsize));
305         prs_uint16("max_rsize", ps, depth, &(rpc->max_rsize));
306         prs_uint32("assoc_gid", ps, depth, &(rpc->assoc_gid));
307
308         return True;
309 }
310
311 /*******************************************************************
312 creates an RPC_HDR_RB structure.
313 ********************************************************************/
314 BOOL make_rpc_hdr_rb(RPC_HDR_RB *rpc, 
315                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
316                                 uint32 num_elements, uint16 context_id, uint8 num_syntaxes,
317                                 RPC_IFACE *abstract, RPC_IFACE *transfer)
318 {
319         if (rpc == NULL) return False;
320
321         make_rpc_hdr_bba(&(rpc->bba), max_tsize, max_rsize, assoc_gid);
322
323         rpc->num_elements = num_elements ; /* the number of elements (0x1) */
324         rpc->context_id   = context_id   ; /* presentation context identifier (0x0) */
325         rpc->num_syntaxes = num_syntaxes ; /* the number of syntaxes (has always been 1?)(0x1) */
326
327         /* num and vers. of interface client is using */
328         memcpy(&(rpc->abstract), abstract, sizeof(rpc->abstract));
329
330         /* num and vers. of interface to use for replies */
331         memcpy(&(rpc->transfer), transfer, sizeof(rpc->transfer));
332
333         return True;
334 }
335
336 /*******************************************************************
337 reads or writes an RPC_HDR_RB structure.
338 ********************************************************************/
339 BOOL smb_io_rpc_hdr_rb(char *desc,  RPC_HDR_RB *rpc, prs_struct *ps, int depth)
340 {
341         if (rpc == NULL) return False;
342
343         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_rb");
344         depth++;
345
346         smb_io_rpc_hdr_bba("", &(rpc->bba), ps, depth);
347
348         prs_uint32("num_elements", ps, depth, &(rpc->num_elements));
349         prs_uint16("context_id  ", ps, depth, &(rpc->context_id  ));
350         prs_uint8 ("num_syntaxes", ps, depth, &(rpc->num_syntaxes));
351
352         smb_io_rpc_iface("", &(rpc->abstract), ps, depth);
353         smb_io_rpc_iface("", &(rpc->transfer), ps, depth);
354
355         return True;
356 }
357
358 /*******************************************************************
359 creates an RPC_RESULTS structure.
360
361 lkclXXXX only one reason at the moment!
362
363 ********************************************************************/
364 static BOOL make_rpc_results(RPC_RESULTS *res, 
365                                 uint8 num_results, uint16 result, uint16 reason)
366 {
367         if (res == NULL) return False;
368
369         res->num_results = num_results; /* the number of results (0x01) */
370         res->result      = result     ;  /* result (0x00 = accept) */
371         res->reason      = reason     ;  /* reason (0x00 = no reason specified) */
372
373         return True;
374 }
375
376 /*******************************************************************
377 reads or writes an RPC_RESULTS structure.
378
379 lkclXXXX only one reason at the moment!
380
381 ********************************************************************/
382 static BOOL smb_io_rpc_results(char *desc,  RPC_RESULTS *res, prs_struct *ps, int depth)
383 {
384         if (res == NULL) return False;
385
386         prs_debug(ps, depth, desc, "smb_io_rpc_results");
387         depth++;
388
389         prs_align(ps);
390         
391         prs_uint8 ("num_results", ps, depth, &(res->num_results));
392
393         prs_align(ps);
394         
395         prs_uint16("result     ", ps, depth, &(res->result     ));
396         prs_uint16("reason     ", ps, depth, &(res->reason     ));
397
398         return True;
399 }
400
401 /*******************************************************************
402 creates an RPC_HDR_BA structure.
403
404 lkclXXXX only one reason at the moment!
405 jfm: nope two ! The pipe_addr can be NULL !
406
407 ********************************************************************/
408 BOOL make_rpc_hdr_ba(RPC_HDR_BA *rpc, 
409                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
410                                 char *pipe_addr,
411                                 uint8 num_results, uint16 result, uint16 reason,
412                                 RPC_IFACE *transfer)
413 {
414         if (rpc == NULL || transfer == NULL) return False;
415
416         make_rpc_hdr_bba (&(rpc->bba ), max_tsize, max_rsize, assoc_gid);
417         make_rpc_addr_str(&(rpc->addr), pipe_addr);
418         make_rpc_results (&(rpc->res ), num_results, result, reason);
419
420         /* the transfer syntax from the request */
421         memcpy(&(rpc->transfer), transfer, sizeof(rpc->transfer));
422
423         return True;
424 }
425
426 /*******************************************************************
427 reads or writes an RPC_HDR_BA structure.
428 ********************************************************************/
429 BOOL smb_io_rpc_hdr_ba(char *desc,  RPC_HDR_BA *rpc, prs_struct *ps, int depth)
430 {
431         if (rpc == NULL) return False;
432
433         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_ba");
434         depth++;
435
436         smb_io_rpc_hdr_bba ("", &(rpc->bba)     , ps, depth);
437         smb_io_rpc_addr_str("", &(rpc->addr)    , ps, depth);
438         smb_io_rpc_results ("", &(rpc->res)     , ps, depth);
439         smb_io_rpc_iface   ("", &(rpc->transfer), ps, depth);
440
441         return True;
442 }
443
444 /*******************************************************************
445 creates an RPC_HDR_REQ structure.
446 ********************************************************************/
447 BOOL make_rpc_hdr_req(RPC_HDR_REQ *hdr, uint32 alloc_hint, uint16 opnum)
448 {
449         if (hdr == NULL) return False;
450
451         hdr->alloc_hint   = alloc_hint; /* allocation hint */
452         hdr->context_id   = 0;         /* presentation context identifier */
453         hdr->opnum        = opnum;     /* opnum */
454
455         return True;
456 }
457
458 /*******************************************************************
459 reads or writes an RPC_HDR_REQ structure.
460 ********************************************************************/
461 BOOL smb_io_rpc_hdr_req(char *desc,  RPC_HDR_REQ *rpc, prs_struct *ps, int depth)
462 {
463         if (rpc == NULL) return False;
464
465         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_req");
466         depth++;
467
468         prs_uint32("alloc_hint", ps, depth, &(rpc->alloc_hint));
469         prs_uint16("context_id", ps, depth, &(rpc->context_id));
470         prs_uint16("opnum     ", ps, depth, &(rpc->opnum));
471
472         return True;
473 }
474
475 /*******************************************************************
476 reads or writes an RPC_HDR_RESP structure.
477 ********************************************************************/
478 BOOL smb_io_rpc_hdr_resp(char *desc,  RPC_HDR_RESP *rpc, prs_struct *ps, int depth)
479 {
480         if (rpc == NULL) return False;
481
482         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_resp");
483         depth++;
484
485         prs_uint32("alloc_hint", ps, depth, &(rpc->alloc_hint));
486         prs_uint16("context_id", ps, depth, &(rpc->context_id));
487         prs_uint8 ("cancel_ct ", ps, depth, &(rpc->cancel_count));
488         prs_uint8 ("reserved  ", ps, depth, &(rpc->reserved));
489
490         return True;
491 }
492
493 /*******************************************************************
494 creates an RPC_HDR_AUTHA structure.
495 ********************************************************************/
496 BOOL make_rpc_hdr_autha(RPC_HDR_AUTHA *rai,
497                                 uint16 max_tsize, uint16 max_rsize,
498                                 uint8 auth_type, uint8 auth_level,
499                                 uint8 stub_type_len)
500 {
501         if (rai == NULL) return False;
502
503         rai->max_tsize = max_tsize; /* maximum transmission fragment size (0x1630) */
504         rai->max_rsize = max_rsize; /* max receive fragment size (0x1630) */   
505
506         rai->auth_type     = auth_type; /* nt lm ssp 0x0a */
507         rai->auth_level    = auth_level; /* 0x06 */
508         rai->stub_type_len = stub_type_len; /* 0x00 */
509         rai->padding       = 0; /* padding 0x00 */
510
511         rai->unknown       = 0x0014a0c0; /* non-zero pointer to something */
512
513         return True;
514 }
515
516 /*******************************************************************
517 reads or writes an RPC_HDR_AUTHA structure.
518 ********************************************************************/
519 BOOL smb_io_rpc_hdr_autha(char *desc, RPC_HDR_AUTHA *rai, prs_struct *ps, int depth)
520 {
521         if (rai == NULL) return False;
522
523         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_autha");
524         depth++;
525
526         prs_uint16("max_tsize    ", ps, depth, &(rai->max_tsize));
527         prs_uint16("max_rsize    ", ps, depth, &(rai->max_rsize));
528
529         prs_uint8 ("auth_type    ", ps, depth, &(rai->auth_type    )); /* 0x0a nt lm ssp */
530         prs_uint8 ("auth_level   ", ps, depth, &(rai->auth_level   ));/* 0x06 */
531         prs_uint8 ("stub_type_len", ps, depth, &(rai->stub_type_len));
532         prs_uint8 ("padding      ", ps, depth, &(rai->padding      ));
533
534         prs_uint32("unknown      ", ps, depth, &(rai->unknown      )); /* 0x0014a0c0 */
535
536         return True;
537 }
538
539 /*******************************************************************
540 checks an RPC_HDR_AUTH structure.
541 ********************************************************************/
542 BOOL rpc_hdr_auth_chk(RPC_HDR_AUTH *rai)
543 {
544         return ((rai->auth_type == 0x0a || rai->auth_type == 0x44) &&
545                  rai->auth_level == 0x06);
546
547         return True;
548 }
549
550 /*******************************************************************
551 creates an RPC_HDR_AUTH structure.
552 ********************************************************************/
553 BOOL make_rpc_hdr_auth(RPC_HDR_AUTH *rai,
554                                 uint8 auth_type, uint8 auth_level,
555                                 uint8 stub_type_len,
556                                 uint32 ptr)
557 {
558         if (rai == NULL) return False;
559
560         rai->auth_type     = auth_type; /* nt lm ssp 0x0a */
561         rai->auth_level    = auth_level; /* 0x06 */
562         rai->stub_type_len = stub_type_len; /* 0x00 */
563         rai->padding       = 0; /* padding 0x00 */
564
565         rai->unknown       = ptr; /* non-zero pointer to something */
566
567         return True;
568 }
569
570 /*******************************************************************
571 reads or writes an RPC_HDR_AUTH structure.
572 ********************************************************************/
573 BOOL smb_io_rpc_hdr_auth(char *desc, RPC_HDR_AUTH *rai, prs_struct *ps, int depth)
574 {
575         if (rai == NULL) return False;
576
577         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_auth");
578         depth++;
579
580         prs_uint8 ("auth_type    ", ps, depth, &(rai->auth_type    )); /* 0x0a nt lm ssp */
581         prs_uint8 ("auth_level   ", ps, depth, &(rai->auth_level   ));/* 0x06 */
582         prs_uint8 ("stub_type_len", ps, depth, &(rai->stub_type_len));
583         prs_uint8 ("padding      ", ps, depth, &(rai->padding      ));
584
585         prs_uint32("unknown      ", ps, depth, &(rai->unknown      )); /* 0x0014a0c0 */
586
587         return True;
588 }
589
590 /*******************************************************************
591 checks an RPC_AUTH_NTLMSSP_VERIFIER structure.
592 ********************************************************************/
593 BOOL rpc_auth_ntlmssp_verifier_chk(RPC_AUTH_NTLMSSP_VERIFIER *rav,
594                                 char *signature, uint32 msg_type)
595 {
596         return (strequal(rav->signature, signature) && rav->msg_type == msg_type);
597
598         return True;
599 }
600
601 /*******************************************************************
602 creates an RPC_AUTH_NTLMSSP_VERIFIER structure.
603 ********************************************************************/
604 BOOL make_rpc_auth_ntlmssp_verifier(RPC_AUTH_NTLMSSP_VERIFIER *rav,
605                                 char *signature, uint32 msg_type)
606 {
607         if (rav == NULL) return False;
608
609         fstrcpy(rav->signature, signature); /* "NTLMSSP" */
610         rav->msg_type = msg_type; /* NTLMSSP_MESSAGE_TYPE */
611
612         return True;
613 }
614
615 /*******************************************************************
616 reads or writes an RPC_AUTH_NTLMSSP_VERIFIER structure.
617 ********************************************************************/
618 BOOL smb_io_rpc_auth_ntlmssp_verifier(char *desc, RPC_AUTH_NTLMSSP_VERIFIER *rav, prs_struct *ps, int depth)
619 {
620         if (rav == NULL) return False;
621
622         prs_debug(ps, depth, desc, "smb_io_rpc_auth_verifier");
623         depth++;
624
625         prs_string("signature", ps, depth, rav->signature, 0, sizeof(rav->signature)); /* "NTLMSSP" */
626         prs_uint32("msg_type ", ps, depth, &(rav->msg_type  )); /* NTLMSSP_MESSAGE_TYPE */
627
628         return True;
629 }
630
631 /*******************************************************************
632 creates an RPC_AUTH_NTLMSSP_NEG structure.
633 ********************************************************************/
634 BOOL make_rpc_auth_ntlmssp_neg(RPC_AUTH_NTLMSSP_NEG *neg,
635                                 uint32 neg_flgs,
636                                 fstring myname, fstring domain)
637 {
638         int len_myname = strlen(myname);
639         int len_domain = strlen(domain);
640
641         if (neg == NULL) return False;
642
643         neg->neg_flgs = neg_flgs ; /* 0x00b2b3 */
644
645         make_str_hdr(&neg->hdr_domain, len_domain, len_domain, 0x20 + len_myname); 
646         make_str_hdr(&neg->hdr_myname, len_myname, len_myname, 0x20); 
647
648         fstrcpy(neg->myname, myname);
649         fstrcpy(neg->domain, domain);
650
651         return True;
652 }
653
654 /*******************************************************************
655 reads or writes an RPC_AUTH_NTLMSSP_NEG structure.
656
657 *** lkclXXXX HACK ALERT! ***
658
659 ********************************************************************/
660 BOOL smb_io_rpc_auth_ntlmssp_neg(char *desc, RPC_AUTH_NTLMSSP_NEG *neg, prs_struct *ps, int depth)
661 {
662         if (neg == NULL) return False;
663
664         prs_debug(ps, depth, desc, "smb_io_rpc_auth_ntlmssp_neg");
665         depth++;
666
667         prs_uint32("neg_flgs ", ps, depth, &(neg->neg_flgs));
668
669         if (ps->io)
670         {
671                 uint32 old_offset;
672
673                 /* reading */
674
675                 ZERO_STRUCTP(neg);
676
677                 smb_io_strhdr("hdr_domain", &(neg->hdr_domain), ps, depth); 
678                 smb_io_strhdr("hdr_myname", &(neg->hdr_myname), ps, depth); 
679
680                 old_offset = ps->offset;
681
682                 ps->offset = neg->hdr_myname  .buffer + 0x50; /* lkclXXXX HACK! */
683                 prs_uint8s(True , "myname", ps, depth, (uint8*)neg->myname  , MIN(neg->hdr_myname  .str_str_len, sizeof(neg->myname  ))); 
684                 old_offset += neg->hdr_myname  .str_str_len;
685
686                 ps->offset = neg->hdr_domain  .buffer + 0x50; /* lkclXXXX HACK! */
687                 prs_uint8s(True , "domain", ps, depth, (uint8*)neg->domain  , MIN(neg->hdr_domain  .str_str_len, sizeof(neg->domain  ))); 
688                 old_offset += neg->hdr_domain  .str_str_len;
689
690                 ps->offset = old_offset;
691         }
692         else
693         {
694                 /* writing */
695                 smb_io_strhdr("hdr_domain", &(neg->hdr_domain), ps, depth); 
696                 smb_io_strhdr("hdr_myname", &(neg->hdr_myname), ps, depth); 
697
698                 prs_uint8s(True , "myname", ps, depth, (uint8*)neg->myname  , MIN(neg->hdr_myname  .str_str_len, sizeof(neg->myname  ))); 
699                 prs_uint8s(True , "domain", ps, depth, (uint8*)neg->domain  , MIN(neg->hdr_domain  .str_str_len, sizeof(neg->domain  ))); 
700         }
701
702         return True;
703 }
704
705 /*******************************************************************
706 creates an RPC_AUTH_NTLMSSP_CHAL structure.
707 ********************************************************************/
708 BOOL make_rpc_auth_ntlmssp_chal(RPC_AUTH_NTLMSSP_CHAL *chl,
709                                 uint32 neg_flags,
710                                 uint8 challenge[8])
711 {
712         if (chl == NULL) return False;
713
714         chl->unknown_1 = 0x0; 
715         chl->unknown_2 = 0x00000028;
716         chl->neg_flags = neg_flags; /* 0x0082b1 */
717
718         memcpy(chl->challenge, challenge, sizeof(chl->challenge)); 
719         bzero (chl->reserved ,            sizeof(chl->reserved)); 
720
721         return True;
722 }
723
724 /*******************************************************************
725 reads or writes an RPC_AUTH_NTLMSSP_CHAL structure.
726 ********************************************************************/
727 BOOL smb_io_rpc_auth_ntlmssp_chal(char *desc, RPC_AUTH_NTLMSSP_CHAL *chl, prs_struct *ps, int depth)
728 {
729         if (chl == NULL) return False;
730
731         prs_debug(ps, depth, desc, "smb_io_rpc_auth_ntlmssp_chal");
732         depth++;
733
734         prs_uint32("unknown_1", ps, depth, &(chl->unknown_1)); /* 0x0000 0000 */
735         prs_uint32("unknown_2", ps, depth, &(chl->unknown_2)); /* 0x0000 b2b3 */
736         prs_uint32("neg_flags", ps, depth, &(chl->neg_flags)); /* 0x0000 82b1 */
737
738         prs_uint8s (False, "challenge", ps, depth, chl->challenge, sizeof(chl->challenge));
739         prs_uint8s (False, "reserved ", ps, depth, chl->reserved , sizeof(chl->reserved ));
740
741         return True;
742 }
743
744 /*******************************************************************
745 creates an RPC_AUTH_NTLMSSP_RESP structure.
746
747 *** lkclXXXX FUDGE!  HAVE TO MANUALLY SPECIFY OFFSET HERE (0x1c bytes) ***
748 *** lkclXXXX the actual offset is at the start of the auth verifier    ***
749
750 ********************************************************************/
751 BOOL make_rpc_auth_ntlmssp_resp(RPC_AUTH_NTLMSSP_RESP *rsp,
752                                 uchar lm_resp[24],
753                                 uchar *nt_resp, size_t nt_len,
754                                 char *domain, char *user, char *wks,
755                                 uint32 neg_flags)
756 {
757         uint32 offset;
758         int dom_len = strlen(domain);
759         int wks_len = strlen(wks   );
760         int usr_len = strlen(user  );
761         int lm_len  = nt_len != 0 ? (lm_resp != NULL ? 24 : 0) : 1;
762
763         DEBUG(5,("make_rpc_auth_ntlmssp_resp\n"));
764
765         if (rsp == NULL) return False;
766
767 #ifdef DEBUG_PASSWORD
768         DEBUG(100,("lm_resp\n"));
769         if (lm_resp != NULL)
770         {
771                 dump_data(100, lm_resp, lm_len);
772         }
773         DEBUG(100,("nt_resp\n"));
774         if (nt_resp != NULL)
775         {
776                 dump_data(100, nt_resp, nt_len);
777         }
778 #endif
779
780         DEBUG(6,("dom: %s user: %s wks: %s neg_flgs: 0x%x\n",
781                   domain, user, wks, neg_flags));
782
783         offset = 0x40;
784
785         if (IS_BITS_SET_ALL(neg_flags, NTLMSSP_NEGOTIATE_UNICODE))
786         {
787                 dom_len *= 2;
788                 wks_len *= 2;
789                 usr_len *= 2;
790         }
791
792         make_str_hdr(&rsp->hdr_domain , dom_len, dom_len, offset);
793         offset += dom_len;
794
795         make_str_hdr(&rsp->hdr_usr    , usr_len, usr_len, offset);
796         offset += usr_len;
797
798         make_str_hdr(&rsp->hdr_wks    , wks_len, wks_len, offset);
799         offset += wks_len;
800
801         make_str_hdr(&rsp->hdr_lm_resp, lm_len , lm_len , offset);
802         offset += lm_len;
803
804         make_str_hdr(&rsp->hdr_nt_resp, nt_len , nt_len , offset);
805         offset += nt_len;
806
807         make_str_hdr(&rsp->hdr_sess_key, 0, 0, offset);
808
809         rsp->neg_flags = neg_flags;
810
811         if (lm_resp != NULL && lm_len != 1)
812         {
813                 memcpy(rsp->lm_resp, lm_resp, lm_len);
814         }
815         else
816         {
817                 rsp->lm_resp[0] = 0;
818         }
819         if (nt_resp != NULL)
820         {
821                 memcpy(rsp->nt_resp, nt_resp, nt_len);
822         }
823         else
824         {
825                 rsp->nt_resp[0] = 0;
826         }
827
828         if (IS_BITS_SET_ALL(neg_flags, NTLMSSP_NEGOTIATE_UNICODE))
829         {
830                 ascii_to_unibuf(rsp->domain, domain, sizeof(rsp->domain)-2);
831                 ascii_to_unibuf(rsp->user  , user  , sizeof(rsp->user  )-2);
832                 ascii_to_unibuf(rsp->wks   , wks   , sizeof(rsp->wks   )-2);
833         }
834         else
835         {
836                 fstrcpy(rsp->domain, domain);
837                 fstrcpy(rsp->user  , user  );
838                 fstrcpy(rsp->wks   , wks   );
839         }
840         rsp->sess_key[0] = 0;
841
842         return True;
843 }
844
845 /*******************************************************************
846 reads or writes an RPC_AUTH_NTLMSSP_RESP structure.
847
848 *** lkclXXXX FUDGE!  HAVE TO MANUALLY SPECIFY OFFSET HERE (0x1c bytes) ***
849 *** lkclXXXX the actual offset is at the start of the auth verifier    ***
850
851 ********************************************************************/
852 BOOL smb_io_rpc_auth_ntlmssp_resp(char *desc, RPC_AUTH_NTLMSSP_RESP *rsp, prs_struct *ps, int depth)
853 {
854         if (rsp == NULL) return False;
855
856         prs_debug(ps, depth, desc, "smb_io_rpc_auth_ntlmssp_resp");
857         depth++;
858
859         if (ps->io)
860         {
861                 uint32 old_offset;
862
863                 /* reading */
864
865                 ZERO_STRUCTP(rsp);
866
867                 smb_io_strhdr("hdr_lm_resp ", &rsp->hdr_lm_resp , ps, depth); 
868                 smb_io_strhdr("hdr_nt_resp ", &rsp->hdr_nt_resp , ps, depth); 
869                 smb_io_strhdr("hdr_domain  ", &rsp->hdr_domain  , ps, depth); 
870                 smb_io_strhdr("hdr_user    ", &rsp->hdr_usr     , ps, depth); 
871                 smb_io_strhdr("hdr_wks     ", &rsp->hdr_wks     , ps, depth); 
872                 smb_io_strhdr("hdr_sess_key", &rsp->hdr_sess_key, ps, depth); 
873
874                 prs_uint32("neg_flags", ps, depth, &(rsp->neg_flags)); /* 0x0000 82b1 */
875
876                 old_offset = ps->offset;
877
878                 ps->offset = rsp->hdr_domain  .buffer + 0x1c;
879                 prs_uint8s(True , "domain  ", ps, depth, (uint8*)rsp->domain  , MIN(rsp->hdr_domain  .str_str_len, sizeof(rsp->domain  ))); 
880                 old_offset += rsp->hdr_domain  .str_str_len;
881
882                 ps->offset = rsp->hdr_usr     .buffer + 0x1c;
883                 prs_uint8s(True , "user    ", ps, depth, (uint8*)rsp->user    , MIN(rsp->hdr_usr     .str_str_len, sizeof(rsp->user    ))); 
884                 old_offset += rsp->hdr_usr     .str_str_len;
885
886                 ps->offset = rsp->hdr_wks     .buffer + 0x1c;
887                 prs_uint8s(True , "wks     ", ps, depth, (uint8*)rsp->wks     , MIN(rsp->hdr_wks     .str_str_len, sizeof(rsp->wks     ))); 
888                 old_offset += rsp->hdr_wks     .str_str_len;
889
890                 ps->offset = rsp->hdr_lm_resp .buffer + 0x1c;
891                 prs_uint8s(False, "lm_resp ", ps, depth, (uint8*)rsp->lm_resp , MIN(rsp->hdr_lm_resp .str_str_len, sizeof(rsp->lm_resp ))); 
892                 old_offset += rsp->hdr_lm_resp .str_str_len;
893
894                 ps->offset = rsp->hdr_nt_resp .buffer + 0x1c;
895                 prs_uint8s(False, "nt_resp ", ps, depth, (uint8*)rsp->nt_resp , MIN(rsp->hdr_nt_resp .str_str_len, sizeof(rsp->nt_resp ))); 
896                 old_offset += rsp->hdr_nt_resp .str_str_len;
897
898                 if (rsp->hdr_sess_key.str_str_len != 0)
899                 {
900                         ps->offset = rsp->hdr_sess_key.buffer + 0x1c;
901                         old_offset += rsp->hdr_sess_key.str_str_len;
902                         prs_uint8s(False, "sess_key", ps, depth, (uint8*)rsp->sess_key, MIN(rsp->hdr_sess_key.str_str_len, sizeof(rsp->sess_key))); 
903                 }
904
905                 ps->offset = old_offset;
906         }
907         else
908         {
909                 /* writing */
910                 smb_io_strhdr("hdr_lm_resp ", &rsp->hdr_lm_resp , ps, depth); 
911                 smb_io_strhdr("hdr_nt_resp ", &rsp->hdr_nt_resp , ps, depth); 
912                 smb_io_strhdr("hdr_domain  ", &rsp->hdr_domain  , ps, depth); 
913                 smb_io_strhdr("hdr_user    ", &rsp->hdr_usr     , ps, depth); 
914                 smb_io_strhdr("hdr_wks     ", &rsp->hdr_wks     , ps, depth); 
915                 smb_io_strhdr("hdr_sess_key", &rsp->hdr_sess_key, ps, depth); 
916
917                 prs_uint32("neg_flags", ps, depth, &(rsp->neg_flags)); /* 0x0000 82b1 */
918
919                 prs_uint8s(True , "domain  ", ps, depth, (uint8*)rsp->domain  , MIN(rsp->hdr_domain  .str_str_len, sizeof(rsp->domain  ))); 
920                 prs_uint8s(True , "user    ", ps, depth, (uint8*)rsp->user    , MIN(rsp->hdr_usr     .str_str_len, sizeof(rsp->user    ))); 
921                 prs_uint8s(True , "wks     ", ps, depth, (uint8*)rsp->wks     , MIN(rsp->hdr_wks     .str_str_len, sizeof(rsp->wks     ))); 
922                 prs_uint8s(False, "lm_resp ", ps, depth, (uint8*)rsp->lm_resp , MIN(rsp->hdr_lm_resp .str_str_len, sizeof(rsp->lm_resp ))); 
923                 prs_uint8s(False, "nt_resp ", ps, depth, (uint8*)rsp->nt_resp , MIN(rsp->hdr_nt_resp .str_str_len, sizeof(rsp->nt_resp ))); 
924                 prs_uint8s(False, "sess_key", ps, depth, (uint8*)rsp->sess_key, MIN(rsp->hdr_sess_key.str_str_len, sizeof(rsp->sess_key))); 
925         }
926
927         return True;
928 }
929
930 /*******************************************************************
931 checks an RPC_AUTH_NTLMSSP_CHK structure.
932 ********************************************************************/
933 BOOL rpc_auth_ntlmssp_chk(RPC_AUTH_NTLMSSP_CHK *chk, uint32 crc32, uint32 seq_num)
934 {
935         if (chk == NULL)
936         {
937                 return False;
938         }
939
940         if (chk->crc32 != crc32 ||
941             chk->ver   != NTLMSSP_SIGN_VERSION ||
942             chk->seq_num != seq_num)
943         {
944                 DEBUG(5,("verify failed - crc %x ver %x seq %d\n",
945                         crc32, NTLMSSP_SIGN_VERSION, seq_num));
946                 DEBUG(5,("verify expect - crc %x ver %x seq %d\n",
947                         chk->crc32, chk->ver, chk->seq_num));
948                 return False;
949         }
950         return True;
951
952         return True;
953 }
954
955 /*******************************************************************
956 creates an RPC_AUTH_NTLMSSP_CHK structure.
957 ********************************************************************/
958 BOOL make_rpc_auth_ntlmssp_chk(RPC_AUTH_NTLMSSP_CHK *chk,
959                                 uint32 ver, uint32 crc32, uint32 seq_num)
960 {
961         if (chk == NULL) return False;
962
963         chk->ver      = ver     ;
964         chk->reserved = 0x0;
965         chk->crc32    = crc32   ;
966         chk->seq_num  = seq_num ;
967
968         return True;
969 }
970
971 /*******************************************************************
972 reads or writes an RPC_AUTH_NTLMSSP_CHK structure.
973 ********************************************************************/
974 BOOL smb_io_rpc_auth_ntlmssp_chk(char *desc, RPC_AUTH_NTLMSSP_CHK *chk, prs_struct *ps, int depth)
975 {
976         if (chk == NULL) return False;
977
978         prs_debug(ps, depth, desc, "smb_io_rpc_auth_ntlmssp_chk");
979         depth++;
980
981         prs_uint32("ver     ", ps, depth, &(chk->ver     )); 
982         prs_uint32("reserved", ps, depth, &(chk->reserved)); 
983         prs_uint32("crc32   ", ps, depth, &(chk->crc32   )); 
984         prs_uint32("seq_num ", ps, depth, &(chk->seq_num )); 
985
986         return True;
987 }
988