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