We copy the RPC header directly from the incoming client - remember to
[samba.git] / source / 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
191         /* We always marshall in little endian format. */
192         if (MARSHALLING(ps))
193                 rpc->pack_type[0] = 0x10;
194
195         if(!prs_uint8("pack_type0", ps, depth, &rpc->pack_type[0]))
196                 return False;
197         if(!prs_uint8("pack_type1", ps, depth, &rpc->pack_type[1]))
198                 return False;
199         if(!prs_uint8("pack_type2", ps, depth, &rpc->pack_type[2]))
200                 return False;
201         if(!prs_uint8("pack_type3", ps, depth, &rpc->pack_type[3]))
202                 return False;
203
204         /*
205          * If reading and pack_type[0] == 0 then the data is in big-endian
206          * format. Set the flag in the prs_struct to specify reverse-endainness.
207          */
208
209         if (UNMARSHALLING(ps) && rpc->pack_type[0] == 0) {
210                 DEBUG(10,("smb_io_rpc_hdr: PDU data format is big-endian. Setting flag.\n"));
211                 prs_set_endian_data(ps, RPC_BIG_ENDIAN);
212         }
213
214         if(!prs_uint16("frag_len  ", ps, depth, &rpc->frag_len))
215                 return False;
216         if(!prs_uint16("auth_len  ", ps, depth, &rpc->auth_len))
217                 return False;
218         if(!prs_uint32("call_id   ", ps, depth, &rpc->call_id))
219                 return False;
220         return True;
221 }
222
223 /*******************************************************************
224  Reads or writes an RPC_IFACE structure.
225 ********************************************************************/
226
227 static BOOL smb_io_rpc_iface(char *desc, RPC_IFACE *ifc, prs_struct *ps, int depth)
228 {
229         if (ifc == NULL)
230                 return False;
231
232         prs_debug(ps, depth, desc, "smb_io_rpc_iface");
233         depth++;
234
235         if(!prs_align(ps))
236                 return False;
237
238         if(!prs_uint32 ("data   ", ps, depth, &ifc->uuid.time_low))
239                 return False;
240         if(!prs_uint16 ("data   ", ps, depth, &ifc->uuid.time_mid))
241                 return False;
242         if(!prs_uint16 ("data   ", ps, depth, &ifc->uuid.time_hi_and_version))
243                 return False;
244
245         if(!prs_uint8s (False, "data   ", ps, depth, ifc->uuid.remaining, sizeof(ifc->uuid.remaining)))
246                 return False;
247         if(!prs_uint32 (       "version", ps, depth, &ifc->version))
248                 return False;
249
250         return True;
251 }
252
253 /*******************************************************************
254  Inits an RPC_ADDR_STR structure.
255 ********************************************************************/
256
257 static void init_rpc_addr_str(RPC_ADDR_STR *str, char *name)
258 {
259         str->len = strlen(name) + 1;
260         fstrcpy(str->str, name);
261 }
262
263 /*******************************************************************
264  Reads or writes an RPC_ADDR_STR structure.
265 ********************************************************************/
266
267 static BOOL smb_io_rpc_addr_str(char *desc,  RPC_ADDR_STR *str, prs_struct *ps, int depth)
268 {
269         if (str == NULL)
270                 return False;
271
272         prs_debug(ps, depth, desc, "smb_io_rpc_addr_str");
273         depth++;
274         if(!prs_align(ps))
275                 return False;
276
277         if(!prs_uint16 (      "len", ps, depth, &str->len))
278                 return False;
279         if(!prs_uint8s (True, "str", ps, depth, (uchar*)str->str, MIN(str->len, sizeof(str->str)) ))
280                 return False;
281         return True;
282 }
283
284 /*******************************************************************
285  Inits an RPC_HDR_BBA structure.
286 ********************************************************************/
287
288 static void init_rpc_hdr_bba(RPC_HDR_BBA *bba, uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid)
289 {
290         bba->max_tsize = max_tsize; /* maximum transmission fragment size (0x1630) */
291         bba->max_rsize = max_rsize; /* max receive fragment size (0x1630) */   
292         bba->assoc_gid = assoc_gid; /* associated group id (0x0) */ 
293 }
294
295 /*******************************************************************
296  Reads or writes an RPC_HDR_BBA structure.
297 ********************************************************************/
298
299 static BOOL smb_io_rpc_hdr_bba(char *desc,  RPC_HDR_BBA *rpc, prs_struct *ps, int depth)
300 {
301         if (rpc == NULL)
302                 return False;
303
304         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_bba");
305         depth++;
306
307         if(!prs_uint16("max_tsize", ps, depth, &rpc->max_tsize))
308                 return False;
309         if(!prs_uint16("max_rsize", ps, depth, &rpc->max_rsize))
310                 return False;
311         if(!prs_uint32("assoc_gid", ps, depth, &rpc->assoc_gid))
312                 return False;
313         return True;
314 }
315
316 /*******************************************************************
317  Inits an RPC_HDR_RB structure.
318 ********************************************************************/
319
320 void init_rpc_hdr_rb(RPC_HDR_RB *rpc, 
321                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
322                                 uint32 num_elements, uint16 context_id, uint8 num_syntaxes,
323                                 RPC_IFACE *abstract, RPC_IFACE *transfer)
324 {
325         init_rpc_hdr_bba(&rpc->bba, max_tsize, max_rsize, assoc_gid);
326
327         rpc->num_elements = num_elements ; /* the number of elements (0x1) */
328         rpc->context_id   = context_id   ; /* presentation context identifier (0x0) */
329         rpc->num_syntaxes = num_syntaxes ; /* the number of syntaxes (has always been 1?)(0x1) */
330
331         /* num and vers. of interface client is using */
332         rpc->abstract = *abstract;
333
334         /* num and vers. of interface to use for replies */
335         rpc->transfer = *transfer;
336 }
337
338 /*******************************************************************
339  Reads or writes an RPC_HDR_RB structure.
340 ********************************************************************/
341
342 BOOL smb_io_rpc_hdr_rb(char *desc, RPC_HDR_RB *rpc, prs_struct *ps, int depth)
343 {
344         if (rpc == NULL)
345                 return False;
346
347         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_rb");
348         depth++;
349
350         if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
351                 return False;
352
353         if(!prs_uint32("num_elements", ps, depth, &rpc->num_elements))
354                 return False;
355         if(!prs_uint16("context_id  ", ps, depth, &rpc->context_id ))
356                 return False;
357         if(!prs_uint8 ("num_syntaxes", ps, depth, &rpc->num_syntaxes))
358                 return False;
359
360         if(!smb_io_rpc_iface("", &rpc->abstract, ps, depth))
361                 return False;
362         if(!smb_io_rpc_iface("", &rpc->transfer, ps, depth))
363                 return False;
364
365         return True;
366 }
367
368 /*******************************************************************
369  Inits an RPC_RESULTS structure.
370
371  lkclXXXX only one reason at the moment!
372 ********************************************************************/
373
374 static void init_rpc_results(RPC_RESULTS *res, 
375                                 uint8 num_results, uint16 result, uint16 reason)
376 {
377         res->num_results = num_results; /* the number of results (0x01) */
378         res->result      = result     ;  /* result (0x00 = accept) */
379         res->reason      = reason     ;  /* reason (0x00 = no reason specified) */
380 }
381
382 /*******************************************************************
383  Reads or writes an RPC_RESULTS structure.
384
385  lkclXXXX only one reason at the moment!
386 ********************************************************************/
387
388 static BOOL smb_io_rpc_results(char *desc, RPC_RESULTS *res, prs_struct *ps, int depth)
389 {
390         if (res == NULL)
391                 return False;
392
393         prs_debug(ps, depth, desc, "smb_io_rpc_results");
394         depth++;
395
396         if(!prs_align(ps))
397                 return False;
398         
399         if(!prs_uint8 ("num_results", ps, depth, &res->num_results))    
400                 return False;
401
402         if(!prs_align(ps))
403                 return False;
404         
405         if(!prs_uint16("result     ", ps, depth, &res->result))
406                 return False;
407         if(!prs_uint16("reason     ", ps, depth, &res->reason))
408                 return False;
409         return True;
410 }
411
412 /*******************************************************************
413  Init an RPC_HDR_BA structure.
414
415  lkclXXXX only one reason at the moment!
416
417 ********************************************************************/
418
419 void init_rpc_hdr_ba(RPC_HDR_BA *rpc, 
420                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
421                                 char *pipe_addr,
422                                 uint8 num_results, uint16 result, uint16 reason,
423                                 RPC_IFACE *transfer)
424 {
425         init_rpc_hdr_bba (&rpc->bba, max_tsize, max_rsize, assoc_gid);
426         init_rpc_addr_str(&rpc->addr, pipe_addr);
427         init_rpc_results (&rpc->res, num_results, result, reason);
428
429         /* the transfer syntax from the request */
430         memcpy(&rpc->transfer, transfer, sizeof(rpc->transfer));
431 }
432
433 /*******************************************************************
434  Reads or writes an RPC_HDR_BA structure.
435 ********************************************************************/
436
437 BOOL smb_io_rpc_hdr_ba(char *desc, RPC_HDR_BA *rpc, prs_struct *ps, int depth)
438 {
439         if (rpc == NULL)
440                 return False;
441
442         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_ba");
443         depth++;
444
445         if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
446                 return False;
447         if(!smb_io_rpc_addr_str("", &rpc->addr, ps, depth))
448                 return False;
449         if(!smb_io_rpc_results("", &rpc->res, ps, depth))
450                 return False;
451         if(!smb_io_rpc_iface("", &rpc->transfer, ps, depth))
452                 return False;
453         return True;
454 }
455
456 /*******************************************************************
457  Init an RPC_HDR_REQ structure.
458 ********************************************************************/
459
460 void init_rpc_hdr_req(RPC_HDR_REQ *hdr, uint32 alloc_hint, uint16 opnum)
461 {
462         hdr->alloc_hint   = alloc_hint; /* allocation hint */
463         hdr->context_id   = 0;         /* presentation context identifier */
464         hdr->opnum        = opnum;     /* opnum */
465 }
466
467 /*******************************************************************
468  Reads or writes an RPC_HDR_REQ structure.
469 ********************************************************************/
470
471 BOOL smb_io_rpc_hdr_req(char *desc, RPC_HDR_REQ *rpc, prs_struct *ps, int depth)
472 {
473         if (rpc == NULL)
474                 return False;
475
476         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_req");
477         depth++;
478
479         if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
480                 return False;
481         if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
482                 return False;
483         if(!prs_uint16("opnum     ", ps, depth, &rpc->opnum))
484                 return False;
485         return True;
486 }
487
488 /*******************************************************************
489  Reads or writes an RPC_HDR_RESP structure.
490 ********************************************************************/
491
492 BOOL smb_io_rpc_hdr_resp(char *desc, RPC_HDR_RESP *rpc, prs_struct *ps, int depth)
493 {
494         if (rpc == NULL)
495                 return False;
496
497         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_resp");
498         depth++;
499
500         if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
501                 return False;
502         if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
503                 return False;
504         if(!prs_uint8 ("cancel_ct ", ps, depth, &rpc->cancel_count))
505                 return False;
506         if(!prs_uint8 ("reserved  ", ps, depth, &rpc->reserved))
507                 return False;
508         return True;
509 }
510
511 /*******************************************************************
512  Reads or writes an RPC_HDR_FAULT structure.
513 ********************************************************************/
514
515 BOOL smb_io_rpc_hdr_fault(char *desc, RPC_HDR_FAULT *rpc, prs_struct *ps, int depth)
516 {
517         if (rpc == NULL)
518                 return False;
519
520         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_fault");
521         depth++;
522
523         if(!prs_uint32("status  ", ps, depth, &rpc->status))
524                 return False;
525         if(!prs_uint32("reserved", ps, depth, &rpc->reserved))
526                 return False;
527
528     return True;
529 }
530
531 /*******************************************************************
532  Init an RPC_HDR_AUTHA structure.
533 ********************************************************************/
534
535 void init_rpc_hdr_autha(RPC_HDR_AUTHA *rai,
536                                 uint16 max_tsize, uint16 max_rsize,
537                                 uint8 auth_type, uint8 auth_level,
538                                 uint8 stub_type_len)
539 {
540         rai->max_tsize = max_tsize; /* maximum transmission fragment size (0x1630) */
541         rai->max_rsize = max_rsize; /* max receive fragment size (0x1630) */   
542
543         rai->auth_type     = auth_type; /* nt lm ssp 0x0a */
544         rai->auth_level    = auth_level; /* 0x06 */
545         rai->stub_type_len = stub_type_len; /* 0x00 */
546         rai->padding       = 0; /* padding 0x00 */
547
548         rai->unknown       = 0x0014a0c0; /* non-zero pointer to something */
549 }
550
551 /*******************************************************************
552  Reads or writes an RPC_HDR_AUTHA structure.
553 ********************************************************************/
554
555 BOOL smb_io_rpc_hdr_autha(char *desc, RPC_HDR_AUTHA *rai, prs_struct *ps, int depth)
556 {
557         if (rai == NULL)
558                 return False;
559
560         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_autha");
561         depth++;
562
563         if(!prs_uint16("max_tsize    ", ps, depth, &rai->max_tsize))
564                 return False;
565         if(!prs_uint16("max_rsize    ", ps, depth, &rai->max_rsize))
566                 return False;
567
568         if(!prs_uint8 ("auth_type    ", ps, depth, &rai->auth_type)) /* 0x0a nt lm ssp */
569                 return False;
570         if(!prs_uint8 ("auth_level   ", ps, depth, &rai->auth_level)) /* 0x06 */
571                 return False;
572         if(!prs_uint8 ("stub_type_len", ps, depth, &rai->stub_type_len))
573                 return False;
574         if(!prs_uint8 ("padding      ", ps, depth, &rai->padding))
575                 return False;
576
577         if(!prs_uint32("unknown      ", ps, depth, &rai->unknown)) /* 0x0014a0c0 */
578                 return False;
579
580         return True;
581 }
582
583 /*******************************************************************
584  Checks an RPC_HDR_AUTH structure.
585 ********************************************************************/
586
587 BOOL rpc_hdr_auth_chk(RPC_HDR_AUTH *rai)
588 {
589         return (rai->auth_type == NTLMSSP_AUTH_TYPE && rai->auth_level == NTLMSSP_AUTH_LEVEL);
590 }
591
592 /*******************************************************************
593  Inits an RPC_HDR_AUTH structure.
594 ********************************************************************/
595
596 void init_rpc_hdr_auth(RPC_HDR_AUTH *rai,
597                                 uint8 auth_type, uint8 auth_level,
598                                 uint8 stub_type_len,
599                                 uint32 ptr)
600 {
601         rai->auth_type     = auth_type; /* nt lm ssp 0x0a */
602         rai->auth_level    = auth_level; /* 0x06 */
603         rai->stub_type_len = stub_type_len; /* 0x00 */
604         rai->padding       = 0; /* padding 0x00 */
605
606         rai->unknown       = ptr; /* non-zero pointer to something */
607 }
608
609 /*******************************************************************
610  Reads or writes an RPC_HDR_AUTH structure.
611 ********************************************************************/
612
613 BOOL smb_io_rpc_hdr_auth(char *desc, RPC_HDR_AUTH *rai, prs_struct *ps, int depth)
614 {
615         if (rai == NULL)
616                 return False;
617
618         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_auth");
619         depth++;
620
621         if(!prs_align(ps))
622                 return False;
623
624         if(!prs_uint8 ("auth_type    ", ps, depth, &rai->auth_type)) /* 0x0a nt lm ssp */
625                 return False;
626         if(!prs_uint8 ("auth_level   ", ps, depth, &rai->auth_level)) /* 0x06 */
627                 return False;
628         if(!prs_uint8 ("stub_type_len", ps, depth, &rai->stub_type_len))
629                 return False;
630         if(!prs_uint8 ("padding      ", ps, depth, &rai->padding))
631                 return False;
632
633         if(!prs_uint32("unknown      ", ps, depth, &rai->unknown)) /* 0x0014a0c0 */
634                 return False;
635
636         return True;
637 }
638
639 /*******************************************************************
640  Checks an RPC_AUTH_VERIFIER structure.
641 ********************************************************************/
642
643 BOOL rpc_auth_verifier_chk(RPC_AUTH_VERIFIER *rav,
644                                 char *signature, uint32 msg_type)
645 {
646         return (strequal(rav->signature, signature) && rav->msg_type == msg_type);
647 }
648
649 /*******************************************************************
650  Inits an RPC_AUTH_VERIFIER structure.
651 ********************************************************************/
652
653 void init_rpc_auth_verifier(RPC_AUTH_VERIFIER *rav,
654                                 char *signature, uint32 msg_type)
655 {
656         fstrcpy(rav->signature, signature); /* "NTLMSSP" */
657         rav->msg_type = msg_type; /* NTLMSSP_MESSAGE_TYPE */
658 }
659
660 /*******************************************************************
661  Reads or writes an RPC_AUTH_VERIFIER structure.
662 ********************************************************************/
663
664 BOOL smb_io_rpc_auth_verifier(char *desc, RPC_AUTH_VERIFIER *rav, prs_struct *ps, int depth)
665 {
666         if (rav == NULL)
667                 return False;
668
669         prs_debug(ps, depth, desc, "smb_io_rpc_auth_verifier");
670         depth++;
671
672         /* "NTLMSSP" */
673         if(!prs_string("signature", ps, depth, rav->signature, strlen("NTLMSSP"),
674                         sizeof(rav->signature)))
675                 return False;
676         if(!prs_uint32("msg_type ", ps, depth, &rav->msg_type)) /* NTLMSSP_MESSAGE_TYPE */
677                 return False;
678
679         return True;
680 }
681
682 /*******************************************************************
683  Inits an RPC_AUTH_NTLMSSP_NEG structure.
684 ********************************************************************/
685
686 void init_rpc_auth_ntlmssp_neg(RPC_AUTH_NTLMSSP_NEG *neg,
687                                 uint32 neg_flgs,
688                                 fstring myname, fstring domain)
689 {
690         int len_myname = strlen(myname);
691         int len_domain = strlen(domain);
692
693         neg->neg_flgs = neg_flgs ; /* 0x00b2b3 */
694
695         init_str_hdr(&neg->hdr_domain, len_domain, len_domain, 0x20 + len_myname); 
696         init_str_hdr(&neg->hdr_myname, len_myname, len_myname, 0x20); 
697
698         fstrcpy(neg->myname, myname);
699         fstrcpy(neg->domain, domain);
700 }
701
702 /*******************************************************************
703  Reads or writes an RPC_AUTH_NTLMSSP_NEG structure.
704
705  *** lkclXXXX HACK ALERT! ***
706 ********************************************************************/
707
708 BOOL smb_io_rpc_auth_ntlmssp_neg(char *desc, RPC_AUTH_NTLMSSP_NEG *neg, prs_struct *ps, int depth)
709 {
710         uint32 start_offset = prs_offset(ps);
711         if (neg == NULL)
712                 return False;
713
714         prs_debug(ps, depth, desc, "smb_io_rpc_auth_ntlmssp_neg");
715         depth++;
716
717         if(!prs_uint32("neg_flgs ", ps, depth, &neg->neg_flgs))
718                 return False;
719
720         if (ps->io) {
721                 uint32 old_offset;
722                 uint32 old_neg_flags = neg->neg_flgs;
723
724                 /* reading */
725
726                 ZERO_STRUCTP(neg);
727
728                 neg->neg_flgs = old_neg_flags;
729
730                 if(!smb_io_strhdr("hdr_domain", &neg->hdr_domain, ps, depth))
731                         return False;
732                 if(!smb_io_strhdr("hdr_myname", &neg->hdr_myname, ps, depth))
733                         return False;
734
735                 old_offset = prs_offset(ps);
736
737                 if(!prs_set_offset(ps, neg->hdr_myname.buffer + start_offset - 12))
738                         return False;
739
740                 if(!prs_uint8s(True, "myname", ps, depth, (uint8*)neg->myname, 
741                                 MIN(neg->hdr_myname.str_str_len, sizeof(neg->myname))))
742                         return False;
743
744                 old_offset += neg->hdr_myname.str_str_len;
745
746                 if(!prs_set_offset(ps, neg->hdr_domain.buffer + start_offset - 12))
747                         return False;
748
749                 if(!prs_uint8s(True, "domain", ps, depth, (uint8*)neg->domain, 
750                         MIN(neg->hdr_domain.str_str_len, sizeof(neg->domain  ))))
751                         return False;
752
753                 old_offset += neg->hdr_domain  .str_str_len;
754
755                 if(!prs_set_offset(ps, old_offset))
756                         return False;
757         } else {
758                 /* writing */
759                 if(!smb_io_strhdr("hdr_domain", &neg->hdr_domain, ps, depth))
760                         return False;
761                 if(!smb_io_strhdr("hdr_myname", &neg->hdr_myname, ps, depth))
762                         return False;
763
764                 if(!prs_uint8s(True, "myname", ps, depth, (uint8*)neg->myname, 
765                                         MIN(neg->hdr_myname.str_str_len, sizeof(neg->myname))))
766                         return False;
767                 if(!prs_uint8s(True, "domain", ps, depth, (uint8*)neg->domain, 
768                                         MIN(neg->hdr_domain.str_str_len, sizeof(neg->domain  ))))
769                         return False;
770         }
771
772         return True;
773 }
774
775 /*******************************************************************
776 creates an RPC_AUTH_NTLMSSP_CHAL structure.
777 ********************************************************************/
778
779 void init_rpc_auth_ntlmssp_chal(RPC_AUTH_NTLMSSP_CHAL *chl,
780                                 uint32 neg_flags,
781                                 uint8 challenge[8])
782 {
783         chl->unknown_1 = 0x0; 
784         chl->unknown_2 = 0x00000028;
785         chl->neg_flags = neg_flags; /* 0x0082b1 */
786
787         memcpy(chl->challenge, challenge, sizeof(chl->challenge)); 
788         memset((char *)chl->reserved , '\0', sizeof(chl->reserved)); 
789 }
790
791 /*******************************************************************
792  Reads or writes an RPC_AUTH_NTLMSSP_CHAL structure.
793 ********************************************************************/
794
795 BOOL smb_io_rpc_auth_ntlmssp_chal(char *desc, RPC_AUTH_NTLMSSP_CHAL *chl, prs_struct *ps, int depth)
796 {
797         if (chl == NULL)
798                 return False;
799
800         prs_debug(ps, depth, desc, "smb_io_rpc_auth_ntlmssp_chal");
801         depth++;
802
803         if(!prs_uint32("unknown_1", ps, depth, &chl->unknown_1)) /* 0x0000 0000 */
804                 return False;
805         if(!prs_uint32("unknown_2", ps, depth, &chl->unknown_2)) /* 0x0000 b2b3 */
806                 return False;
807         if(!prs_uint32("neg_flags", ps, depth, &chl->neg_flags)) /* 0x0000 82b1 */
808                 return False;
809
810         if(!prs_uint8s (False, "challenge", ps, depth, chl->challenge, sizeof(chl->challenge)))
811                 return False;
812         if(!prs_uint8s (False, "reserved ", ps, depth, chl->reserved , sizeof(chl->reserved )))
813                 return False;
814
815         return True;
816 }
817
818 /*******************************************************************
819  Inits an RPC_AUTH_NTLMSSP_RESP structure.
820
821  *** lkclXXXX FUDGE!  HAVE TO MANUALLY SPECIFY OFFSET HERE (0x1c bytes) ***
822  *** lkclXXXX the actual offset is at the start of the auth verifier    ***
823 ********************************************************************/
824
825 void init_rpc_auth_ntlmssp_resp(RPC_AUTH_NTLMSSP_RESP *rsp,
826                                 uchar lm_resp[24], uchar nt_resp[24],
827                                 char *domain, char *user, char *wks,
828                                 uint32 neg_flags)
829 {
830         uint32 offset;
831         int dom_len = strlen(domain);
832         int wks_len = strlen(wks);
833         int usr_len = strlen(user);
834         int lm_len  = (lm_resp != NULL) ? 24 : 0;
835         int nt_len  = (nt_resp != NULL) ? 24 : 0;
836
837         DEBUG(5,("make_rpc_auth_ntlmssp_resp\n"));
838
839 #ifdef DEBUG_PASSWORD
840         DEBUG(100,("lm_resp\n"));
841         dump_data(100, (char *)lm_resp, 24);
842         DEBUG(100,("nt_resp\n"));
843         dump_data(100, (char *)nt_resp, 24);
844 #endif
845
846         DEBUG(6,("dom: %s user: %s wks: %s neg_flgs: 0x%x\n",
847                   domain, user, wks, neg_flags));
848
849         offset = 0x40;
850
851         if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
852                 dom_len *= 2;
853                 wks_len *= 2;
854                 usr_len *= 2;
855         }
856
857         init_str_hdr(&rsp->hdr_domain, dom_len, dom_len, offset);
858         offset += dom_len;
859
860         init_str_hdr(&rsp->hdr_usr, usr_len, usr_len, offset);
861         offset += usr_len;
862
863         init_str_hdr(&rsp->hdr_wks, wks_len, wks_len, offset);
864         offset += wks_len;
865
866         init_str_hdr(&rsp->hdr_lm_resp, lm_len, lm_len, offset);
867         offset += lm_len;
868
869         init_str_hdr(&rsp->hdr_nt_resp, nt_len, nt_len, offset);
870         offset += nt_len;
871
872         init_str_hdr(&rsp->hdr_sess_key, 0, 0, offset);
873
874         rsp->neg_flags = neg_flags;
875
876         memcpy(rsp->lm_resp, lm_resp, 24);
877         memcpy(rsp->nt_resp, nt_resp, 24);
878
879         if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
880                 dos_struni2(rsp->domain, domain, sizeof(rsp->domain));
881                 dos_struni2(rsp->user, user, sizeof(rsp->user));
882                 dos_struni2(rsp->wks, wks, sizeof(rsp->wks));
883         } else {
884                 fstrcpy(rsp->domain, domain);
885                 fstrcpy(rsp->user, user);
886                 fstrcpy(rsp->wks, wks);
887         }
888         rsp->sess_key[0] = 0;
889 }
890
891 /*******************************************************************
892  Reads or writes an RPC_AUTH_NTLMSSP_RESP structure.
893
894  *** lkclXXXX FUDGE!  HAVE TO MANUALLY SPECIFY OFFSET HERE (0x1c bytes) ***
895  *** lkclXXXX the actual offset is at the start of the auth verifier    ***
896 ********************************************************************/
897
898 BOOL smb_io_rpc_auth_ntlmssp_resp(char *desc, RPC_AUTH_NTLMSSP_RESP *rsp, prs_struct *ps, int depth)
899 {
900         if (rsp == NULL)
901                 return False;
902
903         prs_debug(ps, depth, desc, "smb_io_rpc_auth_ntlmssp_resp");
904         depth++;
905
906         if (ps->io) {
907                 uint32 old_offset;
908
909                 /* reading */
910
911                 ZERO_STRUCTP(rsp);
912
913                 if(!smb_io_strhdr("hdr_lm_resp ", &rsp->hdr_lm_resp, ps, depth))
914                         return False;
915                 if(!smb_io_strhdr("hdr_nt_resp ", &rsp->hdr_nt_resp, ps, depth))
916                         return False;
917                 if(!smb_io_strhdr("hdr_domain  ", &rsp->hdr_domain, ps, depth))
918                         return False;
919                 if(!smb_io_strhdr("hdr_user    ", &rsp->hdr_usr, ps, depth))
920                         return False;
921                 if(!smb_io_strhdr("hdr_wks     ", &rsp->hdr_wks, ps, depth)) 
922                         return False;
923                 if(!smb_io_strhdr("hdr_sess_key", &rsp->hdr_sess_key, ps, depth))
924                         return False;
925
926                 if(!prs_uint32("neg_flags", ps, depth, &rsp->neg_flags)) /* 0x0000 82b1 */
927                         return False;
928
929                 old_offset = prs_offset(ps);
930
931                 if(!prs_set_offset(ps, rsp->hdr_domain.buffer + 0xc))
932                         return False;
933
934                 if(!prs_uint8s(True , "domain  ", ps, depth, (uint8*)rsp->domain,
935                                 MIN(rsp->hdr_domain.str_str_len, sizeof(rsp->domain))))
936                         return False;
937
938                 old_offset += rsp->hdr_domain.str_str_len;
939
940                 if(!prs_set_offset(ps, rsp->hdr_usr.buffer + 0xc))
941                         return False;
942
943                 if(!prs_uint8s(True , "user    ", ps, depth, (uint8*)rsp->user,
944                                 MIN(rsp->hdr_usr.str_str_len, sizeof(rsp->user))))
945                         return False;
946
947                 old_offset += rsp->hdr_usr.str_str_len;
948
949                 if(!prs_set_offset(ps, rsp->hdr_wks.buffer + 0xc))
950                         return False;
951
952                 if(!prs_uint8s(True, "wks     ", ps, depth, (uint8*)rsp->wks,
953                                 MIN(rsp->hdr_wks.str_str_len, sizeof(rsp->wks))))
954                         return False;
955
956                 old_offset += rsp->hdr_wks.str_str_len;
957
958                 if(!prs_set_offset(ps, rsp->hdr_lm_resp.buffer + 0xc))
959                         return False;
960
961                 if(!prs_uint8s(False, "lm_resp ", ps, depth, (uint8*)rsp->lm_resp,
962                                 MIN(rsp->hdr_lm_resp.str_str_len, sizeof(rsp->lm_resp ))))
963                         return False;
964
965                 old_offset += rsp->hdr_lm_resp.str_str_len;
966
967                 if(!prs_set_offset(ps, rsp->hdr_nt_resp.buffer + 0xc))
968                         return False;
969
970                 if(!prs_uint8s(False, "nt_resp ", ps, depth, (uint8*)rsp->nt_resp,
971                                 MIN(rsp->hdr_nt_resp.str_str_len, sizeof(rsp->nt_resp ))))
972                         return False;
973
974                 old_offset += rsp->hdr_nt_resp.str_str_len;
975
976                 if (rsp->hdr_sess_key.str_str_len != 0) {
977
978                         if(!prs_set_offset(ps, rsp->hdr_sess_key.buffer + 0x10))
979                                 return False;
980
981                         old_offset += rsp->hdr_sess_key.str_str_len;
982
983                         if(!prs_uint8s(False, "sess_key", ps, depth, (uint8*)rsp->sess_key,
984                                         MIN(rsp->hdr_sess_key.str_str_len, sizeof(rsp->sess_key))))
985                                 return False;
986                 }
987
988                 if(!prs_set_offset(ps, old_offset))
989                         return False;
990         } else {
991                 /* writing */
992                 if(!smb_io_strhdr("hdr_lm_resp ", &rsp->hdr_lm_resp, ps, depth))
993                         return False;
994                 if(!smb_io_strhdr("hdr_nt_resp ", &rsp->hdr_nt_resp, ps, depth))
995                         return False;
996                 if(!smb_io_strhdr("hdr_domain  ", &rsp->hdr_domain, ps, depth))
997                         return False;
998                 if(!smb_io_strhdr("hdr_user    ", &rsp->hdr_usr, ps, depth))
999                         return False;
1000                 if(!smb_io_strhdr("hdr_wks     ", &rsp->hdr_wks, ps, depth))
1001                         return False;
1002                 if(!smb_io_strhdr("hdr_sess_key", &rsp->hdr_sess_key, ps, depth))
1003                         return False;
1004
1005                 if(!prs_uint32("neg_flags", ps, depth, &rsp->neg_flags)) /* 0x0000 82b1 */
1006                         return False;
1007
1008                 if(!prs_uint8s(True , "domain  ", ps, depth, (uint8*)rsp->domain,
1009                                 MIN(rsp->hdr_domain.str_str_len, sizeof(rsp->domain))))
1010                         return False;
1011
1012                 if(!prs_uint8s(True , "user    ", ps, depth, (uint8*)rsp->user,
1013                                 MIN(rsp->hdr_usr.str_str_len, sizeof(rsp->user))))
1014                         return False;
1015
1016                 if(!prs_uint8s(True , "wks     ", ps, depth, (uint8*)rsp->wks,
1017                                 MIN(rsp->hdr_wks.str_str_len, sizeof(rsp->wks))))
1018                         return False;
1019                 if(!prs_uint8s(False, "lm_resp ", ps, depth, (uint8*)rsp->lm_resp,
1020                                 MIN(rsp->hdr_lm_resp .str_str_len, sizeof(rsp->lm_resp))))
1021                         return False;
1022                 if(!prs_uint8s(False, "nt_resp ", ps, depth, (uint8*)rsp->nt_resp,
1023                                 MIN(rsp->hdr_nt_resp .str_str_len, sizeof(rsp->nt_resp ))))
1024                         return False;
1025                 if(!prs_uint8s(False, "sess_key", ps, depth, (uint8*)rsp->sess_key,
1026                                 MIN(rsp->hdr_sess_key.str_str_len, sizeof(rsp->sess_key))))
1027                         return False;
1028         }
1029
1030         return True;
1031 }
1032
1033 /*******************************************************************
1034  Checks an RPC_AUTH_NTLMSSP_CHK structure.
1035 ********************************************************************/
1036
1037 BOOL rpc_auth_ntlmssp_chk(RPC_AUTH_NTLMSSP_CHK *chk, uint32 crc32, uint32 seq_num)
1038 {
1039         if (chk == NULL)
1040                 return False;
1041
1042         if (chk->crc32 != crc32 ||
1043             chk->ver   != NTLMSSP_SIGN_VERSION ||
1044             chk->seq_num != seq_num)
1045         {
1046                 DEBUG(5,("verify failed - crc %x ver %x seq %d\n",
1047                         crc32, NTLMSSP_SIGN_VERSION, seq_num));
1048                 DEBUG(5,("verify expect - crc %x ver %x seq %d\n",
1049                         chk->crc32, chk->ver, chk->seq_num));
1050                 return False;
1051         }
1052         return True;
1053 }
1054
1055 /*******************************************************************
1056  Inits an RPC_AUTH_NTLMSSP_CHK structure.
1057 ********************************************************************/
1058
1059 void init_rpc_auth_ntlmssp_chk(RPC_AUTH_NTLMSSP_CHK *chk,
1060                                 uint32 ver, uint32 crc32, uint32 seq_num)
1061 {
1062         chk->ver      = ver;
1063         chk->reserved = 0x0;
1064         chk->crc32    = crc32;
1065         chk->seq_num  = seq_num;
1066 }
1067
1068 /*******************************************************************
1069  Reads or writes an RPC_AUTH_NTLMSSP_CHK structure.
1070 ********************************************************************/
1071
1072 BOOL smb_io_rpc_auth_ntlmssp_chk(char *desc, RPC_AUTH_NTLMSSP_CHK *chk, prs_struct *ps, int depth)
1073 {
1074         if (chk == NULL)
1075                 return False;
1076
1077         prs_debug(ps, depth, desc, "smb_io_rpc_auth_ntlmssp_chk");
1078         depth++;
1079
1080         if(!prs_align(ps))
1081                 return False;
1082
1083         if(!prs_uint32("ver     ", ps, depth, &chk->ver))
1084                 return False;
1085         if(!prs_uint32("reserved", ps, depth, &chk->reserved))
1086                 return False;
1087         if(!prs_uint32("crc32   ", ps, depth, &chk->crc32))
1088                 return False;
1089         if(!prs_uint32("seq_num ", ps, depth, &chk->seq_num))
1090                 return False;
1091
1092         return True;
1093 }