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