r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[gd/samba/.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 3 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, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_PARSE
27
28 /*******************************************************************
29 interface/version dce/rpc pipe identification
30 ********************************************************************/
31
32 #define TRANS_SYNT_V2                       \
33 {                                           \
34         {                                   \
35                 0x8a885d04, 0x1ceb, 0x11c9, \
36                 { 0x9f, 0xe8 },             \
37                 { 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 },             \
47                 { 0x08, 0x00,               \
48                   0x2b, 0x10, 0x48, 0x60 }  \
49         }, 0x02                             \
50 }
51
52 #define SYNT_WKSSVC_V1                      \
53 {                                           \
54         {                                   \
55                 0x6bffd098, 0xa112, 0x3610, \
56                 { 0x98, 0x33 },             \
57                 { 0x46, 0xc3,               \
58                   0xf8, 0x7e, 0x34, 0x5a }  \
59         }, 0x01                             \
60 }
61
62 #define SYNT_SRVSVC_V3                      \
63 {                                           \
64         {                                   \
65                 0x4b324fc8, 0x1670, 0x01d3, \
66                 { 0x12, 0x78 },             \
67                 { 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 },             \
77                 { 0x01, 0x23,               \
78                   0x45, 0x67, 0x89, 0xab }  \
79         }, 0x00                             \
80 }
81
82 #define SYNT_LSARPC_V0_DS                \
83 {                                           \
84         {                                   \
85                 0x3919286a, 0xb10c, 0x11d0, \
86                 { 0x9b, 0xa8 },             \
87                 { 0x00, 0xc0,               \
88                   0x4f, 0xd9, 0x2e, 0xf5 }  \
89         }, 0x00                             \
90 }
91
92 #define SYNT_SAMR_V1                        \
93 {                                           \
94         {                                   \
95                 0x12345778, 0x1234, 0xabcd, \
96                 { 0xef, 0x00 },             \
97                 { 0x01, 0x23,               \
98                   0x45, 0x67, 0x89, 0xac }  \
99         }, 0x01                             \
100 }
101
102 #define SYNT_NETLOGON_V1                    \
103 {                                           \
104         {                                   \
105                 0x12345678, 0x1234, 0xabcd, \
106                 { 0xef, 0x00 },             \
107                 { 0x01, 0x23,               \
108                   0x45, 0x67, 0xcf, 0xfb }  \
109         }, 0x01                             \
110 }
111
112 #define SYNT_WINREG_V1                      \
113 {                                           \
114         {                                   \
115                 0x338cd001, 0x2244, 0x31f1, \
116                 { 0xaa, 0xaa },             \
117                 { 0x90, 0x00,               \
118                   0x38, 0x00, 0x10, 0x03 }  \
119         }, 0x01                             \
120 }
121
122 #define SYNT_SPOOLSS_V1                     \
123 {                                           \
124         {                                   \
125                 0x12345678, 0x1234, 0xabcd, \
126                 { 0xef, 0x00 },             \
127                 { 0x01, 0x23,               \
128                   0x45, 0x67, 0x89, 0xab }  \
129         }, 0x01                             \
130 }
131
132 #define SYNT_NONE_V0                        \
133 {                                           \
134         {                                   \
135                 0x0, 0x0, 0x0,              \
136                 { 0x00, 0x00 },             \
137                 { 0x00, 0x00,               \
138                   0x00, 0x00, 0x00, 0x00 }  \
139         }, 0x00                             \
140 }
141
142 #define SYNT_NETDFS_V3                      \
143 {                                           \
144         {                                   \
145                 0x4fc742e0, 0x4a10, 0x11cf, \
146                 { 0x82, 0x73 },             \
147                 { 0x00, 0xaa,               \
148                   0x00, 0x4a, 0xe6, 0x73 }  \
149         }, 0x03                             \
150 }
151
152 #define SYNT_ECHO_V1                        \
153 {                                           \
154         {                                   \
155                 0x60a15ec5, 0x4de8, 0x11d7, \
156                 { 0xa6, 0x37 },             \
157                 { 0x00, 0x50,               \
158                   0x56, 0xa2, 0x01, 0x82 }  \
159         }, 0x01                             \
160 }
161
162 #define SYNT_SHUTDOWN_V1                    \
163 {                                           \
164         {                                   \
165                 0x894de0c0, 0x0d55, 0x11d3, \
166                 { 0xa3, 0x22 },             \
167                 { 0x00, 0xc0,               \
168                   0x4f, 0xa3, 0x21, 0xa1 }  \
169         }, 0x01                             \
170 }
171
172 #define SYNT_SVCCTL_V2                      \
173 {                                           \
174         {                                   \
175                 0x367abb81, 0x9844, 0x35f1, \
176                 { 0xad, 0x32 },             \
177                 { 0x98, 0xf0,               \
178                   0x38, 0x00, 0x10, 0x03 }  \
179         }, 0x02                             \
180 }
181
182
183 #define SYNT_EVENTLOG_V0                    \
184 {                                           \
185         {                                   \
186                 0x82273fdc, 0xe32a, 0x18c3, \
187                 { 0x3f, 0x78 },             \
188                 { 0x82, 0x79,               \
189                   0x29, 0xdc, 0x23, 0xea }  \
190         }, 0x00                             \
191 }
192
193 #define SYNT_UNIXINFO_V0                    \
194 {                                           \
195         {                                   \
196                 0x9c54e310, 0xa955, 0x4885, \
197                 { 0xbd, 0x31 },             \
198                 { 0x78, 0x78,               \
199                   0x71, 0x47, 0xdf, 0xa6 }  \
200         }, 0x00                             \
201 }
202
203 #define SYNT_NTSVCS_V1                      \
204 {                                           \
205         {                                   \
206                 0x8d9f4e40, 0xa03d, 0x11ce, \
207                 { 0x8f, 0x69},              \
208                 { 0x08, 0x00,               \
209                   0x3e, 0x30, 0x05, 0x1b }  \
210         }, 0x01                             \
211 }
212
213 #define SYNT_EPMAPPER_V3                                        \
214 {                                                                                       \
215         {                                                                               \
216                 0xe1af8308, 0x5d1f,0x11c9, \
217                 { 0x91,0xa4},                                           \
218                          {0x08,0x00,                            \
219                               0x2b,0x14,0xa0,0xfa}      \
220         }, 0x03 \
221 }
222
223 /*
224  * IMPORTANT!!  If you update this structure, make sure to
225  * update the index #defines in smb.h.
226  */
227
228 const struct pipe_id_info pipe_names [] =
229 {
230         /* client pipe , abstract syntax       , server pipe   , transfer syntax */
231         { PIPE_LSARPC  , SYNT_LSARPC_V0        , PIPE_LSASS    , TRANS_SYNT_V2 },
232         { PIPE_LSARPC  , SYNT_LSARPC_V0_DS     , PIPE_LSASS    , TRANS_SYNT_V2 },
233         { PIPE_SAMR    , SYNT_SAMR_V1          , PIPE_LSASS    , TRANS_SYNT_V2 },
234         { PIPE_NETLOGON, SYNT_NETLOGON_V1      , PIPE_LSASS    , TRANS_SYNT_V2 },
235         { PIPE_SRVSVC  , SYNT_SRVSVC_V3        , PIPE_NTSVCS   , TRANS_SYNT_V2 },
236         { PIPE_WKSSVC  , SYNT_WKSSVC_V1        , PIPE_NTSVCS   , TRANS_SYNT_V2 },
237         { PIPE_WINREG  , SYNT_WINREG_V1        , PIPE_WINREG   , TRANS_SYNT_V2 },
238         { PIPE_SPOOLSS , SYNT_SPOOLSS_V1       , PIPE_SPOOLSS  , TRANS_SYNT_V2 },
239         { PIPE_NETDFS  , SYNT_NETDFS_V3        , PIPE_NETDFS   , TRANS_SYNT_V2 },
240         { PIPE_ECHO    , SYNT_ECHO_V1          , PIPE_ECHO     , TRANS_SYNT_V2 },
241         { PIPE_SHUTDOWN, SYNT_SHUTDOWN_V1      , PIPE_SHUTDOWN , TRANS_SYNT_V2 },
242         { PIPE_SVCCTL  , SYNT_SVCCTL_V2        , PIPE_NTSVCS   , TRANS_SYNT_V2 },
243         { PIPE_EVENTLOG, SYNT_EVENTLOG_V0      , PIPE_EVENTLOG , TRANS_SYNT_V2 },
244         { PIPE_UNIXINFO, SYNT_UNIXINFO_V0      , PIPE_UNIXINFO , TRANS_SYNT_V2 },
245         { PIPE_NTSVCS  , SYNT_NTSVCS_V1        , PIPE_NTSVCS   , TRANS_SYNT_V2 },
246         { PIPE_EPMAPPER, SYNT_EPMAPPER_V3          , PIPE_EPMAPPER , TRANS_SYNT_V2 },
247         { NULL         , SYNT_NONE_V0          , NULL          , SYNT_NONE_V0  }
248 };
249
250 /****************************************************************************
251  Return the pipe name from the index.
252  ****************************************************************************/
253
254 const char *cli_get_pipe_name(int pipe_idx)
255 {
256         return &pipe_names[pipe_idx].client_pipe[5];
257 }
258
259 /*******************************************************************
260  Inits an RPC_HDR structure.
261 ********************************************************************/
262
263 void init_rpc_hdr(RPC_HDR *hdr, enum RPC_PKT_TYPE pkt_type, uint8 flags,
264                                 uint32 call_id, int data_len, int auth_len)
265 {
266         hdr->major        = 5;               /* RPC version 5 */
267         hdr->minor        = 0;               /* minor version 0 */
268         hdr->pkt_type     = pkt_type;        /* RPC packet type */
269         hdr->flags        = flags;           /* dce/rpc flags */
270         hdr->pack_type[0] = 0x10;            /* little-endian data representation */
271         hdr->pack_type[1] = 0;               /* packed data representation */
272         hdr->pack_type[2] = 0;               /* packed data representation */
273         hdr->pack_type[3] = 0;               /* packed data representation */
274         hdr->frag_len     = data_len;        /* fragment length, fill in later */
275         hdr->auth_len     = auth_len;        /* authentication length */
276         hdr->call_id      = call_id;         /* call identifier - match incoming RPC */
277 }
278
279 /*******************************************************************
280  Reads or writes an RPC_HDR structure.
281 ********************************************************************/
282
283 BOOL smb_io_rpc_hdr(const char *desc,  RPC_HDR *rpc, prs_struct *ps, int depth)
284 {
285         if (rpc == NULL)
286                 return False;
287
288         prs_debug(ps, depth, desc, "smb_io_rpc_hdr");
289         depth++;
290
291         if(!prs_uint8 ("major     ", ps, depth, &rpc->major))
292                 return False;
293
294         if(!prs_uint8 ("minor     ", ps, depth, &rpc->minor))
295                 return False;
296         if(!prs_uint8 ("pkt_type  ", ps, depth, &rpc->pkt_type))
297                 return False;
298         if(!prs_uint8 ("flags     ", ps, depth, &rpc->flags))
299                 return False;
300
301         /* We always marshall in little endian format. */
302         if (MARSHALLING(ps))
303                 rpc->pack_type[0] = 0x10;
304
305         if(!prs_uint8("pack_type0", ps, depth, &rpc->pack_type[0]))
306                 return False;
307         if(!prs_uint8("pack_type1", ps, depth, &rpc->pack_type[1]))
308                 return False;
309         if(!prs_uint8("pack_type2", ps, depth, &rpc->pack_type[2]))
310                 return False;
311         if(!prs_uint8("pack_type3", ps, depth, &rpc->pack_type[3]))
312                 return False;
313
314         /*
315          * If reading and pack_type[0] == 0 then the data is in big-endian
316          * format. Set the flag in the prs_struct to specify reverse-endainness.
317          */
318
319         if (UNMARSHALLING(ps) && rpc->pack_type[0] == 0) {
320                 DEBUG(10,("smb_io_rpc_hdr: PDU data format is big-endian. Setting flag.\n"));
321                 prs_set_endian_data(ps, RPC_BIG_ENDIAN);
322         }
323
324         if(!prs_uint16("frag_len  ", ps, depth, &rpc->frag_len))
325                 return False;
326         if(!prs_uint16("auth_len  ", ps, depth, &rpc->auth_len))
327                 return False;
328         if(!prs_uint32("call_id   ", ps, depth, &rpc->call_id))
329                 return False;
330         return True;
331 }
332
333 /*******************************************************************
334  Reads or writes an RPC_IFACE structure.
335 ********************************************************************/
336
337 static BOOL smb_io_rpc_iface(const char *desc, RPC_IFACE *ifc, prs_struct *ps, int depth)
338 {
339         if (ifc == NULL)
340                 return False;
341
342         prs_debug(ps, depth, desc, "smb_io_rpc_iface");
343         depth++;
344
345         if (!prs_align(ps))
346                 return False;
347
348         if (!smb_io_uuid(  "uuid", &ifc->uuid, ps, depth))
349                 return False;
350
351         if(!prs_uint32 ("version", ps, depth, &ifc->version))
352                 return False;
353
354         return True;
355 }
356
357 /*******************************************************************
358  Inits an RPC_ADDR_STR structure.
359 ********************************************************************/
360
361 static void init_rpc_addr_str(RPC_ADDR_STR *str, const char *name)
362 {
363         str->len = strlen(name) + 1;
364         fstrcpy(str->str, name);
365 }
366
367 /*******************************************************************
368  Reads or writes an RPC_ADDR_STR structure.
369 ********************************************************************/
370
371 static BOOL smb_io_rpc_addr_str(const char *desc,  RPC_ADDR_STR *str, prs_struct *ps, int depth)
372 {
373         if (str == NULL)
374                 return False;
375
376         prs_debug(ps, depth, desc, "smb_io_rpc_addr_str");
377         depth++;
378         if(!prs_align(ps))
379                 return False;
380
381         if(!prs_uint16 (      "len", ps, depth, &str->len))
382                 return False;
383         if(!prs_uint8s (True, "str", ps, depth, (uchar*)str->str, MIN(str->len, sizeof(str->str)) ))
384                 return False;
385         return True;
386 }
387
388 /*******************************************************************
389  Inits an RPC_HDR_BBA structure.
390 ********************************************************************/
391
392 static void init_rpc_hdr_bba(RPC_HDR_BBA *bba, uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid)
393 {
394         bba->max_tsize = max_tsize; /* maximum transmission fragment size (0x1630) */
395         bba->max_rsize = max_rsize; /* max receive fragment size (0x1630) */   
396         bba->assoc_gid = assoc_gid; /* associated group id (0x0) */ 
397 }
398
399 /*******************************************************************
400  Reads or writes an RPC_HDR_BBA structure.
401 ********************************************************************/
402
403 static BOOL smb_io_rpc_hdr_bba(const char *desc,  RPC_HDR_BBA *rpc, prs_struct *ps, int depth)
404 {
405         if (rpc == NULL)
406                 return False;
407
408         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_bba");
409         depth++;
410
411         if(!prs_uint16("max_tsize", ps, depth, &rpc->max_tsize))
412                 return False;
413         if(!prs_uint16("max_rsize", ps, depth, &rpc->max_rsize))
414                 return False;
415         if(!prs_uint32("assoc_gid", ps, depth, &rpc->assoc_gid))
416                 return False;
417         return True;
418 }
419
420 /*******************************************************************
421  Inits an RPC_CONTEXT structure.
422  Note the transfer pointer must remain valid until this is marshalled.
423 ********************************************************************/
424
425 void init_rpc_context(RPC_CONTEXT *rpc_ctx, uint16 context_id, RPC_IFACE *abstract, RPC_IFACE *transfer)
426 {
427         rpc_ctx->context_id   = context_id   ; /* presentation context identifier (0x0) */
428         rpc_ctx->num_transfer_syntaxes = 1 ; /* the number of syntaxes (has always been 1?)(0x1) */
429
430         /* num and vers. of interface client is using */
431         rpc_ctx->abstract = *abstract;
432
433         /* vers. of interface to use for replies */
434         rpc_ctx->transfer = transfer;
435 }
436
437 /*******************************************************************
438  Inits an RPC_HDR_RB structure.
439  Note the context pointer must remain valid until this is marshalled.
440 ********************************************************************/
441
442 void init_rpc_hdr_rb(RPC_HDR_RB *rpc, 
443                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
444                                 RPC_CONTEXT *context)
445 {
446         init_rpc_hdr_bba(&rpc->bba, max_tsize, max_rsize, assoc_gid);
447
448         rpc->num_contexts = 1;
449         rpc->rpc_context = context;
450 }
451
452 /*******************************************************************
453  Reads or writes an RPC_CONTEXT structure.
454 ********************************************************************/
455
456 BOOL smb_io_rpc_context(const char *desc, RPC_CONTEXT *rpc_ctx, prs_struct *ps, int depth)
457 {
458         int i;
459
460         if (rpc_ctx == NULL)
461                 return False;
462
463         if(!prs_align(ps))
464                 return False;
465         if(!prs_uint16("context_id  ", ps, depth, &rpc_ctx->context_id ))
466                 return False;
467         if(!prs_uint8 ("num_transfer_syntaxes", ps, depth, &rpc_ctx->num_transfer_syntaxes))
468                 return False;
469
470         /* num_transfer_syntaxes must not be zero. */
471         if (rpc_ctx->num_transfer_syntaxes == 0)
472                 return False;
473
474         if(!smb_io_rpc_iface("", &rpc_ctx->abstract, ps, depth))
475                 return False;
476
477         if (UNMARSHALLING(ps)) {
478                 if (!(rpc_ctx->transfer = PRS_ALLOC_MEM(ps, RPC_IFACE, rpc_ctx->num_transfer_syntaxes))) {
479                         return False;
480                 }
481         }
482
483         for (i = 0; i < rpc_ctx->num_transfer_syntaxes; i++ ) {
484                 if (!smb_io_rpc_iface("", &rpc_ctx->transfer[i], ps, depth))
485                         return False;
486         }
487         return True;
488
489
490 /*******************************************************************
491  Reads or writes an RPC_HDR_RB structure.
492 ********************************************************************/
493
494 BOOL smb_io_rpc_hdr_rb(const char *desc, RPC_HDR_RB *rpc, prs_struct *ps, int depth)
495 {
496         int i;
497         
498         if (rpc == NULL)
499                 return False;
500
501         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_rb");
502         depth++;
503
504         if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
505                 return False;
506
507         if(!prs_uint8("num_contexts", ps, depth, &rpc->num_contexts))
508                 return False;
509
510         /* 3 pad bytes following - will be mopped up by the prs_align in smb_io_rpc_context(). */
511
512         /* num_contexts must not be zero. */
513         if (rpc->num_contexts == 0)
514                 return False;
515
516         if (UNMARSHALLING(ps)) {
517                 if (!(rpc->rpc_context = PRS_ALLOC_MEM(ps, RPC_CONTEXT, rpc->num_contexts))) {
518                         return False;
519                 }
520         }
521
522         for (i = 0; i < rpc->num_contexts; i++ ) {
523                 if (!smb_io_rpc_context("", &rpc->rpc_context[i], ps, depth))
524                         return False;
525         }
526
527         return True;
528 }
529
530 /*******************************************************************
531  Inits an RPC_RESULTS structure.
532
533  lkclXXXX only one reason at the moment!
534 ********************************************************************/
535
536 static void init_rpc_results(RPC_RESULTS *res, 
537                                 uint8 num_results, uint16 result, uint16 reason)
538 {
539         res->num_results = num_results; /* the number of results (0x01) */
540         res->result      = result     ;  /* result (0x00 = accept) */
541         res->reason      = reason     ;  /* reason (0x00 = no reason specified) */
542 }
543
544 /*******************************************************************
545  Reads or writes an RPC_RESULTS structure.
546
547  lkclXXXX only one reason at the moment!
548 ********************************************************************/
549
550 static BOOL smb_io_rpc_results(const char *desc, RPC_RESULTS *res, prs_struct *ps, int depth)
551 {
552         if (res == NULL)
553                 return False;
554
555         prs_debug(ps, depth, desc, "smb_io_rpc_results");
556         depth++;
557
558         if(!prs_align(ps))
559                 return False;
560         
561         if(!prs_uint8 ("num_results", ps, depth, &res->num_results))    
562                 return False;
563
564         if(!prs_align(ps))
565                 return False;
566         
567         if(!prs_uint16("result     ", ps, depth, &res->result))
568                 return False;
569         if(!prs_uint16("reason     ", ps, depth, &res->reason))
570                 return False;
571         return True;
572 }
573
574 /*******************************************************************
575  Init an RPC_HDR_BA structure.
576
577  lkclXXXX only one reason at the moment!
578
579 ********************************************************************/
580
581 void init_rpc_hdr_ba(RPC_HDR_BA *rpc, 
582                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
583                                 const char *pipe_addr,
584                                 uint8 num_results, uint16 result, uint16 reason,
585                                 RPC_IFACE *transfer)
586 {
587         init_rpc_hdr_bba (&rpc->bba, max_tsize, max_rsize, assoc_gid);
588         init_rpc_addr_str(&rpc->addr, pipe_addr);
589         init_rpc_results (&rpc->res, num_results, result, reason);
590
591         /* the transfer syntax from the request */
592         memcpy(&rpc->transfer, transfer, sizeof(rpc->transfer));
593 }
594
595 /*******************************************************************
596  Reads or writes an RPC_HDR_BA structure.
597 ********************************************************************/
598
599 BOOL smb_io_rpc_hdr_ba(const char *desc, RPC_HDR_BA *rpc, prs_struct *ps, int depth)
600 {
601         if (rpc == NULL)
602                 return False;
603
604         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_ba");
605         depth++;
606
607         if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
608                 return False;
609         if(!smb_io_rpc_addr_str("", &rpc->addr, ps, depth))
610                 return False;
611         if(!smb_io_rpc_results("", &rpc->res, ps, depth))
612                 return False;
613         if(!smb_io_rpc_iface("", &rpc->transfer, ps, depth))
614                 return False;
615         return True;
616 }
617
618 /*******************************************************************
619  Init an RPC_HDR_REQ structure.
620 ********************************************************************/
621
622 void init_rpc_hdr_req(RPC_HDR_REQ *hdr, uint32 alloc_hint, uint16 opnum)
623 {
624         hdr->alloc_hint   = alloc_hint; /* allocation hint */
625         hdr->context_id   = 0;         /* presentation context identifier */
626         hdr->opnum        = opnum;     /* opnum */
627 }
628
629 /*******************************************************************
630  Reads or writes an RPC_HDR_REQ structure.
631 ********************************************************************/
632
633 BOOL smb_io_rpc_hdr_req(const char *desc, RPC_HDR_REQ *rpc, prs_struct *ps, int depth)
634 {
635         if (rpc == NULL)
636                 return False;
637
638         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_req");
639         depth++;
640
641         if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
642                 return False;
643         if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
644                 return False;
645         if(!prs_uint16("opnum     ", ps, depth, &rpc->opnum))
646                 return False;
647         return True;
648 }
649
650 /*******************************************************************
651  Reads or writes an RPC_HDR_RESP structure.
652 ********************************************************************/
653
654 BOOL smb_io_rpc_hdr_resp(const char *desc, RPC_HDR_RESP *rpc, prs_struct *ps, int depth)
655 {
656         if (rpc == NULL)
657                 return False;
658
659         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_resp");
660         depth++;
661
662         if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
663                 return False;
664         if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
665                 return False;
666         if(!prs_uint8 ("cancel_ct ", ps, depth, &rpc->cancel_count))
667                 return False;
668         if(!prs_uint8 ("reserved  ", ps, depth, &rpc->reserved))
669                 return False;
670         return True;
671 }
672
673 /*******************************************************************
674  Reads or writes an RPC_HDR_FAULT structure.
675 ********************************************************************/
676
677 BOOL smb_io_rpc_hdr_fault(const char *desc, RPC_HDR_FAULT *rpc, prs_struct *ps, int depth)
678 {
679         if (rpc == NULL)
680                 return False;
681
682         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_fault");
683         depth++;
684
685         if(!prs_dcerpc_status("status  ", ps, depth, &rpc->status))
686                 return False;
687         if(!prs_uint32("reserved", ps, depth, &rpc->reserved))
688                 return False;
689
690     return True;
691 }
692
693 /*******************************************************************
694  Inits an RPC_HDR_AUTH structure.
695 ********************************************************************/
696
697 void init_rpc_hdr_auth(RPC_HDR_AUTH *rai,
698                                 uint8 auth_type, uint8 auth_level,
699                                 uint8 auth_pad_len,
700                                 uint32 auth_context_id)
701 {
702         rai->auth_type     = auth_type;
703         rai->auth_level    = auth_level;
704         rai->auth_pad_len  = auth_pad_len;
705         rai->auth_reserved = 0;
706         rai->auth_context_id = auth_context_id;
707 }
708
709 /*******************************************************************
710  Reads or writes an RPC_HDR_AUTH structure.
711 ********************************************************************/
712
713 BOOL smb_io_rpc_hdr_auth(const char *desc, RPC_HDR_AUTH *rai, prs_struct *ps, int depth)
714 {
715         if (rai == NULL)
716                 return False;
717
718         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_auth");
719         depth++;
720
721         if(!prs_align(ps))
722                 return False;
723
724         if(!prs_uint8 ("auth_type    ", ps, depth, &rai->auth_type))
725                 return False;
726         if(!prs_uint8 ("auth_level   ", ps, depth, &rai->auth_level))
727                 return False;
728         if(!prs_uint8 ("auth_pad_len ", ps, depth, &rai->auth_pad_len))
729                 return False;
730         if(!prs_uint8 ("auth_reserved", ps, depth, &rai->auth_reserved))
731                 return False;
732         if(!prs_uint32("auth_context_id", ps, depth, &rai->auth_context_id))
733                 return False;
734
735         return True;
736 }
737
738 /*******************************************************************
739  Checks an RPC_AUTH_VERIFIER structure.
740 ********************************************************************/
741
742 BOOL rpc_auth_verifier_chk(RPC_AUTH_VERIFIER *rav,
743                                 const char *signature, uint32 msg_type)
744 {
745         return (strequal(rav->signature, signature) && rav->msg_type == msg_type);
746 }
747
748 /*******************************************************************
749  Inits an RPC_AUTH_VERIFIER structure.
750 ********************************************************************/
751
752 void init_rpc_auth_verifier(RPC_AUTH_VERIFIER *rav,
753                                 const char *signature, uint32 msg_type)
754 {
755         fstrcpy(rav->signature, signature); /* "NTLMSSP" */
756         rav->msg_type = msg_type; /* NTLMSSP_MESSAGE_TYPE */
757 }
758
759 /*******************************************************************
760  Reads or writes an RPC_AUTH_VERIFIER structure.
761 ********************************************************************/
762
763 BOOL smb_io_rpc_auth_verifier(const char *desc, RPC_AUTH_VERIFIER *rav, prs_struct *ps, int depth)
764 {
765         if (rav == NULL)
766                 return False;
767
768         prs_debug(ps, depth, desc, "smb_io_rpc_auth_verifier");
769         depth++;
770
771         /* "NTLMSSP" */
772         if(!prs_string("signature", ps, depth, rav->signature,
773                         sizeof(rav->signature)))
774                 return False;
775         if(!prs_uint32("msg_type ", ps, depth, &rav->msg_type)) /* NTLMSSP_MESSAGE_TYPE */
776                 return False;
777
778         return True;
779 }
780
781 /*******************************************************************
782  This parses an RPC_AUTH_VERIFIER for schannel. I think
783 ********************************************************************/
784
785 BOOL smb_io_rpc_schannel_verifier(const char *desc, RPC_AUTH_VERIFIER *rav, prs_struct *ps, int depth)
786 {
787         if (rav == NULL)
788                 return False;
789
790         prs_debug(ps, depth, desc, "smb_io_rpc_schannel_verifier");
791         depth++;
792
793         if(!prs_string("signature", ps, depth, rav->signature, sizeof(rav->signature)))
794                 return False;
795         if(!prs_uint32("msg_type ", ps, depth, &rav->msg_type))
796                 return False;
797
798         return True;
799 }
800
801 /*******************************************************************
802 creates an RPC_AUTH_SCHANNEL_NEG structure.
803 ********************************************************************/
804
805 void init_rpc_auth_schannel_neg(RPC_AUTH_SCHANNEL_NEG *neg,
806                               const char *domain, const char *myname)
807 {
808         neg->type1 = 0;
809         neg->type2 = 0x3;
810         fstrcpy(neg->domain, domain);
811         fstrcpy(neg->myname, myname);
812 }
813
814 /*******************************************************************
815  Reads or writes an RPC_AUTH_SCHANNEL_NEG structure.
816 ********************************************************************/
817
818 BOOL smb_io_rpc_auth_schannel_neg(const char *desc, RPC_AUTH_SCHANNEL_NEG *neg,
819                                 prs_struct *ps, int depth)
820 {
821         if (neg == NULL)
822                 return False;
823
824         prs_debug(ps, depth, desc, "smb_io_rpc_auth_schannel_neg");
825         depth++;
826
827         if(!prs_align(ps))
828                 return False;
829
830         if(!prs_uint32("type1", ps, depth, &neg->type1))
831                 return False;
832         if(!prs_uint32("type2", ps, depth, &neg->type2))
833                 return False;
834         if(!prs_string("domain  ", ps, depth, neg->domain, sizeof(neg->domain)))
835                 return False;
836         if(!prs_string("myname  ", ps, depth, neg->myname, sizeof(neg->myname)))
837                 return False;
838
839         return True;
840 }
841
842 /*******************************************************************
843 reads or writes an RPC_AUTH_SCHANNEL_CHK structure.
844 ********************************************************************/
845
846 BOOL smb_io_rpc_auth_schannel_chk(const char *desc, int auth_len, 
847                                 RPC_AUTH_SCHANNEL_CHK * chk,
848                                 prs_struct *ps, int depth)
849 {
850         if (chk == NULL)
851                 return False;
852
853         prs_debug(ps, depth, desc, "smb_io_rpc_auth_schannel_chk");
854         depth++;
855
856         if ( !prs_uint8s(False, "sig  ", ps, depth, chk->sig, sizeof(chk->sig)) )
857                 return False;
858                 
859         if ( !prs_uint8s(False, "seq_num", ps, depth, chk->seq_num, sizeof(chk->seq_num)) )
860                 return False;
861                 
862         if ( !prs_uint8s(False, "packet_digest", ps, depth, chk->packet_digest, sizeof(chk->packet_digest)) )
863                 return False;
864         
865         if ( auth_len == RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN ) {
866                 if ( !prs_uint8s(False, "confounder", ps, depth, chk->confounder, sizeof(chk->confounder)) )
867                         return False;
868         }
869
870         return True;
871 }