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