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