r24551: rename dcerpc_interface_table -> ndr_interface_table
[jelmer/samba4-debian.git] / source / librpc / rpc / dcerpc_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc utility functions
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2004
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
9    Copyright (C) Rafal Szczesniak 2006
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 3 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, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "lib/events/events.h"
27 #include "libcli/composite/composite.h"
28 #include "librpc/gen_ndr/ndr_epmapper_c.h"
29 #include "librpc/gen_ndr/ndr_dcerpc.h"
30 #include "librpc/gen_ndr/ndr_misc.h"
31 #include "auth/credentials/credentials.h"
32
33 /*
34   find a dcerpc call on an interface by name
35 */
36 const struct ndr_interface_call *dcerpc_iface_find_call(const struct ndr_interface_table *iface,
37                                                         const char *name)
38 {
39         int i;
40         for (i=0;i<iface->num_calls;i++) {
41                 if (strcmp(iface->calls[i].name, name) == 0) {
42                         return &iface->calls[i];
43                 }
44         }
45         return NULL;
46 }
47
48 /* 
49    push a ncacn_packet into a blob, potentially with auth info
50 */
51 NTSTATUS ncacn_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
52                           struct ncacn_packet *pkt,
53                           struct dcerpc_auth *auth_info)
54 {
55         NTSTATUS status;
56         struct ndr_push *ndr;
57
58         ndr = ndr_push_init_ctx(mem_ctx);
59         if (!ndr) {
60                 return NT_STATUS_NO_MEMORY;
61         }
62
63         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
64                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
65         }
66
67         if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
68                 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
69         }
70
71         if (auth_info) {
72                 pkt->auth_length = auth_info->credentials.length;
73         } else {
74                 pkt->auth_length = 0;
75         }
76
77         status = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
78         if (!NT_STATUS_IS_OK(status)) {
79                 return status;
80         }
81
82         if (auth_info) {
83                 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
84         }
85
86         *blob = ndr_push_blob(ndr);
87
88         /* fill in the frag length */
89         dcerpc_set_frag_length(blob, blob->length);
90
91         return NT_STATUS_OK;
92 }
93
94 #define MAX_PROTSEQ             10
95
96 static const struct {
97         const char *name;
98         enum dcerpc_transport_t transport;
99         int num_protocols;
100         enum epm_protocol protseq[MAX_PROTSEQ];
101 } transports[] = {
102         { "ncacn_np",     NCACN_NP, 3, 
103                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
104         { "ncacn_ip_tcp", NCACN_IP_TCP, 3, 
105                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } }, 
106         { "ncacn_http", NCACN_HTTP, 3, 
107                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } }, 
108         { "ncadg_ip_udp", NCACN_IP_UDP, 3, 
109                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
110         { "ncalrpc", NCALRPC, 2, 
111                 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
112         { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2, 
113                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
114         { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2, 
115                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
116         { "ncacn_at_dsp", NCACN_AT_DSP, 3, 
117                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
118         { "ncadg_at_ddp", NCADG_AT_DDP, 3, 
119                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
120         { "ncacn_vns_ssp", NCACN_VNS_SPP, 3, 
121                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
122         { "ncacn_vns_ipc", NCACN_VNS_IPC, 3, 
123                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
124         { "ncadg_ipx", NCADG_IPX, 2,
125                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
126         },
127         { "ncacn_spx", NCACN_SPX, 3,
128                 /* I guess some MS programmer confused the identifier for 
129                  * EPM_PROTOCOL_UUID (0x0D or 13) with the one for 
130                  * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
131                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
132         },
133 };
134
135 static const struct {
136         const char *name;
137         uint32_t flag;
138 } ncacn_options[] = {
139         {"sign", DCERPC_SIGN},
140         {"seal", DCERPC_SEAL},
141         {"connect", DCERPC_CONNECT},
142         {"spnego", DCERPC_AUTH_SPNEGO},
143         {"ntlm", DCERPC_AUTH_NTLM},
144         {"krb5", DCERPC_AUTH_KRB5},
145         {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
146         {"print", DCERPC_DEBUG_PRINT_BOTH},
147         {"padcheck", DCERPC_DEBUG_PAD_CHECK},
148         {"bigendian", DCERPC_PUSH_BIGENDIAN},
149         {"smb2", DCERPC_SMB2}
150 };
151
152 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
153 {
154         struct ndr_syntax_id syntax;
155         NTSTATUS status;
156
157         switch(epm_floor->lhs.protocol) {
158                 case EPM_PROTOCOL_UUID:
159                         status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
160                         if (NT_STATUS_IS_OK(status)) {
161                                 /* lhs is used: UUID */
162                                 char *uuidstr;
163
164                                 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax.uuid)) {
165                                         return "NDR";
166                                 } 
167
168                                 if (GUID_equal(&syntax.uuid, &ndr64_transfer_syntax.uuid)) {
169                                         return "NDR64";
170                                 } 
171
172                                 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
173
174                                 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
175                         } else { /* IPX */
176                                 return talloc_asprintf(mem_ctx, "IPX:%s", 
177                                                 data_blob_hex_string(mem_ctx, &epm_floor->rhs.uuid.unknown));
178                         }
179
180                 case EPM_PROTOCOL_NCACN:
181                         return "RPC-C";
182
183                 case EPM_PROTOCOL_NCADG:
184                         return "RPC";
185
186                 case EPM_PROTOCOL_NCALRPC:
187                         return "NCALRPC";
188
189                 case EPM_PROTOCOL_DNET_NSP:
190                         return "DNET/NSP";
191
192                 case EPM_PROTOCOL_IP:
193                         return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
194
195                 case EPM_PROTOCOL_PIPE:
196                         return talloc_asprintf(mem_ctx, "PIPE:%s", epm_floor->rhs.pipe.path);
197
198                 case EPM_PROTOCOL_SMB:
199                         return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
200
201                 case EPM_PROTOCOL_UNIX_DS:
202                         return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
203
204                 case EPM_PROTOCOL_NETBIOS:
205                         return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
206
207                 case EPM_PROTOCOL_NETBEUI:
208                         return "NETBeui";
209
210                 case EPM_PROTOCOL_SPX:
211                         return "SPX";
212
213                 case EPM_PROTOCOL_NB_IPX:
214                         return "NB_IPX";
215
216                 case EPM_PROTOCOL_HTTP:
217                         return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
218
219                 case EPM_PROTOCOL_TCP:
220                         return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
221
222                 case EPM_PROTOCOL_UDP:
223                         return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
224
225                 default:
226                         return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
227         }
228 }
229
230
231 /*
232   form a binding string from a binding structure
233 */
234 char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
235 {
236         char *s = talloc_strdup(mem_ctx, "");
237         int i;
238         const char *t_name=NULL;
239
240         for (i=0;i<ARRAY_SIZE(transports);i++) {
241                 if (transports[i].transport == b->transport) {
242                         t_name = transports[i].name;
243                 }
244         }
245         if (!t_name) {
246                 return NULL;
247         }
248
249         if (!GUID_all_zero(&b->object.uuid)) { 
250                 s = talloc_asprintf(s, "%s@",
251                                     GUID_string(mem_ctx, &b->object.uuid));
252         }
253
254         s = talloc_asprintf_append(s, "%s:", t_name);
255         if (!s) return NULL;
256
257         if (b->host) {
258                 s = talloc_asprintf_append(s, "%s", b->host);
259         }
260
261         if (!b->endpoint && !b->options && !b->flags) {
262                 return s;
263         }
264
265         s = talloc_asprintf_append(s, "[");
266
267         if (b->endpoint) {
268                 s = talloc_asprintf_append(s, "%s", b->endpoint);
269         }
270
271         /* this is a *really* inefficent way of dealing with strings,
272            but this is rarely called and the strings are always short,
273            so I don't care */
274         for (i=0;b->options && b->options[i];i++) {
275                 s = talloc_asprintf_append(s, ",%s", b->options[i]);
276                 if (!s) return NULL;
277         }
278
279         for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
280                 if (b->flags & ncacn_options[i].flag) {
281                         s = talloc_asprintf_append(s, ",%s", ncacn_options[i].name);
282                         if (!s) return NULL;
283                 }
284         }
285
286         s = talloc_asprintf_append(s, "]");
287
288         return s;
289 }
290
291 /*
292   parse a binding string into a dcerpc_binding structure
293 */
294 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
295 {
296         struct dcerpc_binding *b;
297         char *options, *type;
298         char *p;
299         int i, j, comma_count;
300
301         b = talloc(mem_ctx, struct dcerpc_binding);
302         if (!b) {
303                 return NT_STATUS_NO_MEMORY;
304         }
305
306         p = strchr(s, '@');
307
308         if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
309                 NTSTATUS status;
310
311                 status = GUID_from_string(s, &b->object.uuid);
312
313                 if (NT_STATUS_IS_ERR(status)) {
314                         DEBUG(0, ("Failed parsing UUID\n"));
315                         return status;
316                 }
317
318                 s = p + 1;
319         } else {
320                 ZERO_STRUCT(b->object);
321         }
322
323         b->object.if_version = 0;
324
325         p = strchr(s, ':');
326         if (!p) {
327                 return NT_STATUS_INVALID_PARAMETER;
328         }
329
330         type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
331         if (!type) {
332                 return NT_STATUS_NO_MEMORY;
333         }
334
335         for (i=0;i<ARRAY_SIZE(transports);i++) {
336                 if (strcasecmp(type, transports[i].name) == 0) {
337                         b->transport = transports[i].transport;
338                         break;
339                 }
340         }
341         if (i==ARRAY_SIZE(transports)) {
342                 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
343                 return NT_STATUS_INVALID_PARAMETER;
344         }
345         
346         s = p+1;
347
348         p = strchr(s, '[');
349         if (p) {
350                 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
351                 options = talloc_strdup(mem_ctx, p+1);
352                 if (options[strlen(options)-1] != ']') {
353                         return NT_STATUS_INVALID_PARAMETER;
354                 }
355                 options[strlen(options)-1] = 0;
356         } else {
357                 b->host = talloc_strdup(b, s);
358                 options = NULL;
359         }
360         if (!b->host) {
361                 return NT_STATUS_NO_MEMORY;
362         }
363
364         b->target_hostname = b->host;
365
366         b->options = NULL;
367         b->flags = 0;
368         b->assoc_group_id = 0;
369         b->endpoint = NULL;
370
371         if (!options) {
372                 *b_out = b;
373                 return NT_STATUS_OK;
374         }
375
376         comma_count = count_chars(options, ',');
377
378         b->options = talloc_array(b, const char *, comma_count+2);
379         if (!b->options) {
380                 return NT_STATUS_NO_MEMORY;
381         }
382
383         for (i=0; (p = strchr(options, ',')); i++) {
384                 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
385                 if (!b->options[i]) {
386                         return NT_STATUS_NO_MEMORY;
387                 }
388                 options = p+1;
389         }
390         b->options[i] = options;
391         b->options[i+1] = NULL;
392
393         /* some options are pre-parsed for convenience */
394         for (i=0;b->options[i];i++) {
395                 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
396                         if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
397                                 int k;
398                                 b->flags |= ncacn_options[j].flag;
399                                 for (k=i;b->options[k];k++) {
400                                         b->options[k] = b->options[k+1];
401                                 }
402                                 i--;
403                                 break;
404                         }
405                 }
406         }
407
408         if (b->options[0]) {
409                 /* Endpoint is first option */
410                 b->endpoint = b->options[0];
411                 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
412
413                 for (i=0;b->options[i];i++) {
414                         b->options[i] = b->options[i+1];
415                 }
416         }
417
418         if (b->options[0] == NULL)
419                 b->options = NULL;
420         
421         *b_out = b;
422         return NT_STATUS_OK;
423 }
424
425 NTSTATUS dcerpc_floor_get_lhs_data(struct epm_floor *epm_floor, struct ndr_syntax_id *syntax)
426 {
427         TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
428         struct ndr_pull *ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
429         NTSTATUS status;
430         uint16_t if_version=0;
431
432         ndr->flags |= LIBNDR_FLAG_NOALIGN;
433
434         status = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
435         if (NT_STATUS_IS_ERR(status)) {
436                 talloc_free(mem_ctx);
437                 return status;
438         }
439
440         status = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
441         syntax->if_version = if_version;
442
443         talloc_free(mem_ctx);
444
445         return status;
446 }
447
448 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
449 {
450         struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
451
452         ndr->flags |= LIBNDR_FLAG_NOALIGN;
453
454         ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
455         ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
456
457         return ndr_push_blob(ndr);
458 }
459
460 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
461 {
462         switch (epm_floor->lhs.protocol) {
463         case EPM_PROTOCOL_TCP:
464                 if (epm_floor->rhs.tcp.port == 0) return NULL;
465                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
466                 
467         case EPM_PROTOCOL_UDP:
468                 if (epm_floor->rhs.udp.port == 0) return NULL;
469                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
470
471         case EPM_PROTOCOL_HTTP:
472                 if (epm_floor->rhs.http.port == 0) return NULL;
473                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
474
475         case EPM_PROTOCOL_IP:
476                 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
477
478         case EPM_PROTOCOL_NCACN:
479                 return NULL;
480
481         case EPM_PROTOCOL_NCADG:
482                 return NULL;
483
484         case EPM_PROTOCOL_SMB:
485                 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
486                 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
487
488         case EPM_PROTOCOL_PIPE:
489                 if (strlen(epm_floor->rhs.pipe.path) == 0) return NULL;
490                 return talloc_strdup(mem_ctx, epm_floor->rhs.pipe.path);
491
492         case EPM_PROTOCOL_NETBIOS:
493                 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
494                 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
495
496         case EPM_PROTOCOL_NCALRPC:
497                 return NULL;
498                 
499         case EPM_PROTOCOL_VINES_SPP:
500                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
501                 
502         case EPM_PROTOCOL_VINES_IPC:
503                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
504                 
505         case EPM_PROTOCOL_STREETTALK:
506                 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
507                 
508         case EPM_PROTOCOL_UNIX_DS:
509                 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
510                 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
511                 
512         case EPM_PROTOCOL_NULL:
513                 return NULL;
514
515         default:
516                 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
517                 break;
518         }
519
520         return NULL;
521 }
522
523 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor,  const char *data)
524 {
525         switch (epm_floor->lhs.protocol) {
526         case EPM_PROTOCOL_TCP:
527                 epm_floor->rhs.tcp.port = atoi(data);
528                 return NT_STATUS_OK;
529                 
530         case EPM_PROTOCOL_UDP:
531                 epm_floor->rhs.udp.port = atoi(data);
532                 return NT_STATUS_OK;
533
534         case EPM_PROTOCOL_HTTP:
535                 epm_floor->rhs.http.port = atoi(data);
536                 return NT_STATUS_OK;
537
538         case EPM_PROTOCOL_IP:
539                 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
540                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
541                 return NT_STATUS_OK;
542
543         case EPM_PROTOCOL_NCACN:
544                 epm_floor->rhs.ncacn.minor_version = 0;
545                 return NT_STATUS_OK;
546
547         case EPM_PROTOCOL_NCADG:
548                 epm_floor->rhs.ncadg.minor_version = 0;
549                 return NT_STATUS_OK;
550
551         case EPM_PROTOCOL_SMB:
552                 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
553                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
554                 return NT_STATUS_OK;
555
556         case EPM_PROTOCOL_PIPE:
557                 epm_floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
558                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.pipe.path);
559                 return NT_STATUS_OK;
560
561         case EPM_PROTOCOL_NETBIOS:
562                 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
563                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
564                 return NT_STATUS_OK;
565
566         case EPM_PROTOCOL_NCALRPC:
567                 return NT_STATUS_OK;
568                 
569         case EPM_PROTOCOL_VINES_SPP:
570                 epm_floor->rhs.vines_spp.port = atoi(data);
571                 return NT_STATUS_OK;
572                 
573         case EPM_PROTOCOL_VINES_IPC:
574                 epm_floor->rhs.vines_ipc.port = atoi(data);
575                 return NT_STATUS_OK;
576                 
577         case EPM_PROTOCOL_STREETTALK:
578                 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
579                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
580                 return NT_STATUS_OK;
581                 
582         case EPM_PROTOCOL_UNIX_DS:
583                 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
584                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
585                 return NT_STATUS_OK;
586                 
587         case EPM_PROTOCOL_NULL:
588                 return NT_STATUS_OK;
589
590         default:
591                 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
592                 break;
593         }
594
595         return NT_STATUS_NOT_SUPPORTED;
596 }
597
598 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
599 {
600         int i;
601
602         /* Find a transport that has 'prot' as 4th protocol */
603         for (i=0;i<ARRAY_SIZE(transports);i++) {
604                 if (transports[i].num_protocols >= 2 && 
605                         transports[i].protseq[1] == prot) {
606                         return transports[i].transport;
607                 }
608         }
609         
610         /* Unknown transport */
611         return (unsigned int)-1;
612 }
613
614 enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
615 {
616         int i;
617
618         /* Find a transport that matches this tower */
619         for (i=0;i<ARRAY_SIZE(transports);i++) {
620                 int j;
621                 if (transports[i].num_protocols != tower->num_floors - 2) {
622                         continue; 
623                 }
624
625                 for (j = 0; j < transports[i].num_protocols; j++) {
626                         if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
627                                 break;
628                         }
629                 }
630
631                 if (j == transports[i].num_protocols) {
632                         return transports[i].transport;
633                 }
634         }
635         
636         /* Unknown transport */
637         return (unsigned int)-1;
638 }
639
640 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower, struct dcerpc_binding **b_out)
641 {
642         NTSTATUS status;
643         struct dcerpc_binding *binding;
644
645         binding = talloc(mem_ctx, struct dcerpc_binding);
646         NT_STATUS_HAVE_NO_MEMORY(binding);
647
648         ZERO_STRUCT(binding->object);
649         binding->options = NULL;
650         binding->host = NULL;
651         binding->target_hostname = NULL;
652         binding->flags = 0;
653         binding->assoc_group_id = 0;
654
655         binding->transport = dcerpc_transport_by_tower(tower);
656
657         if (binding->transport == (unsigned int)-1) {
658                 return NT_STATUS_NOT_SUPPORTED;
659         }
660
661         if (tower->num_floors < 1) {
662                 return NT_STATUS_OK;
663         }
664
665         /* Set object uuid */
666         status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
667         
668         if (!NT_STATUS_IS_OK(status)) {
669                 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));     
670                 return status;
671         }
672
673         /* Ignore floor 1, it contains the NDR version info */
674         
675         binding->options = NULL;
676
677         /* Set endpoint */
678         if (tower->num_floors >= 4) {
679                 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
680         } else {
681                 binding->endpoint = NULL;
682         }
683
684         /* Set network address */
685         if (tower->num_floors >= 5) {
686                 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
687                 NT_STATUS_HAVE_NO_MEMORY(binding->host);
688                 binding->target_hostname = binding->host;
689         }
690         *b_out = binding;
691         return NT_STATUS_OK;
692 }
693
694 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
695 {
696         const enum epm_protocol *protseq = NULL;
697         int num_protocols = -1, i;
698         NTSTATUS status;
699         
700         /* Find transport */
701         for (i=0;i<ARRAY_SIZE(transports);i++) {
702                 if (transports[i].transport == binding->transport) {
703                         protseq = transports[i].protseq;
704                         num_protocols = transports[i].num_protocols;
705                         break;
706                 }
707         }
708
709         if (num_protocols == -1) {
710                 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
711                 return NT_STATUS_UNSUCCESSFUL;
712         }
713
714         tower->num_floors = 2 + num_protocols;
715         tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
716
717         /* Floor 0 */
718         tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
719
720         tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &binding->object);
721
722         tower->floors[0].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
723         
724         /* Floor 1 */
725         tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
726
727         tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, 
728                                                                 &ndr_transfer_syntax);
729         
730         tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
731         
732         /* Floor 2 to num_protocols */
733         for (i = 0; i < num_protocols; i++) {
734                 tower->floors[2 + i].lhs.protocol = protseq[i];
735                 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
736                 ZERO_STRUCT(tower->floors[2 + i].rhs);
737                 dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
738         }
739
740         /* The 4th floor contains the endpoint */
741         if (num_protocols >= 2 && binding->endpoint) {
742                 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->endpoint);
743                 if (NT_STATUS_IS_ERR(status)) {
744                         return status;
745                 }
746         }
747         
748         /* The 5th contains the network address */
749         if (num_protocols >= 3 && binding->host) {
750                 if (is_ipaddress(binding->host)) {
751                         status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4], 
752                                                            binding->host);
753                 } else {
754                         /* note that we don't attempt to resolve the
755                            name here - when we get a hostname here we
756                            are in the client code, and want to put in
757                            a wildcard all-zeros IP for the server to
758                            fill in */
759                         status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4], 
760                                                            "0.0.0.0");
761                 }
762                 if (NT_STATUS_IS_ERR(status)) {
763                         return status;
764                 }
765         }
766
767         return NT_STATUS_OK;
768 }
769
770
771 struct epm_map_binding_state {
772         struct dcerpc_binding *binding;
773         const struct ndr_interface_table *table;
774         struct dcerpc_pipe *pipe;
775         struct policy_handle handle;
776         struct GUID guid;
777         struct epm_twr_t twr;
778         struct epm_twr_t *twr_r;
779         struct epm_Map r;
780 };
781
782
783 static void continue_epm_recv_binding(struct composite_context *ctx);
784 static void continue_epm_map(struct rpc_request *req);
785
786
787 /*
788   Stage 2 of epm_map_binding: Receive connected rpc pipe and send endpoint
789   mapping rpc request
790 */
791 static void continue_epm_recv_binding(struct composite_context *ctx)
792 {
793         struct rpc_request *map_req;
794
795         struct composite_context *c = talloc_get_type(ctx->async.private_data,
796                                                       struct composite_context);
797         struct epm_map_binding_state *s = talloc_get_type(c->private_data,
798                                                           struct epm_map_binding_state);
799
800         /* receive result of rpc pipe connect request */
801         c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
802         if (!composite_is_ok(c)) return;
803
804         s->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
805
806         /* prepare requested binding parameters */
807         s->binding->object         = s->table->syntax_id;
808
809         c->status = dcerpc_binding_build_tower(s->pipe, s->binding, &s->twr.tower);
810         if (!composite_is_ok(c)) return;
811         
812         /* with some nice pretty paper around it of course */
813         s->r.in.object        = &s->guid;
814         s->r.in.map_tower     = &s->twr;
815         s->r.in.entry_handle  = &s->handle;
816         s->r.in.max_towers    = 1;
817         s->r.out.entry_handle = &s->handle;
818
819         /* send request for an endpoint mapping - a rpc request on connected pipe */
820         map_req = dcerpc_epm_Map_send(s->pipe, c, &s->r);
821         if (composite_nomem(map_req, c)) return;
822         
823         composite_continue_rpc(c, map_req, continue_epm_map, c);
824 }
825
826
827 /*
828   Stage 3 of epm_map_binding: Receive endpoint mapping and provide binding details
829 */
830 static void continue_epm_map(struct rpc_request *req)
831 {
832         struct composite_context *c = talloc_get_type(req->async.private_data,
833                                                       struct composite_context);
834         struct epm_map_binding_state *s = talloc_get_type(c->private_data,
835                                                           struct epm_map_binding_state);
836
837         /* receive result of a rpc request */
838         c->status = dcerpc_ndr_request_recv(req);
839         if (!composite_is_ok(c)) return;
840
841         /* check the details */
842         if (s->r.out.result != 0 || *s->r.out.num_towers != 1) {
843                 composite_error(c, NT_STATUS_PORT_UNREACHABLE);
844                 return;
845         }
846         
847         s->twr_r = s->r.out.towers[0].twr;
848         if (s->twr_r == NULL) {
849                 composite_error(c, NT_STATUS_PORT_UNREACHABLE);
850                 return;
851         }
852
853         if (s->twr_r->tower.num_floors != s->twr.tower.num_floors ||
854             s->twr_r->tower.floors[3].lhs.protocol != s->twr.tower.floors[3].lhs.protocol) {
855                 composite_error(c, NT_STATUS_PORT_UNREACHABLE);
856                 return;
857         }
858
859         /* get received endpoint */
860         s->binding->endpoint = talloc_reference(s->binding,
861                                                 dcerpc_floor_get_rhs_data(c, &s->twr_r->tower.floors[3]));
862         if (composite_nomem(s->binding->endpoint, c)) return;
863
864         composite_done(c);
865 }
866
867
868 /*
869   Request for endpoint mapping of dcerpc binding - try to request for endpoint
870   unless there is default one.
871 */
872 struct composite_context *dcerpc_epm_map_binding_send(TALLOC_CTX *mem_ctx,
873                                                       struct dcerpc_binding *binding,
874                                                       const struct ndr_interface_table *table,
875                                                       struct event_context *ev)
876 {
877         struct composite_context *c;
878         struct epm_map_binding_state *s;
879         struct composite_context *pipe_connect_req;
880         struct cli_credentials *anon_creds;
881         struct event_context *new_ev = NULL;
882
883         NTSTATUS status;
884         struct dcerpc_binding *epmapper_binding;
885         int i;
886
887         /* Try to find event context in memory context in case passed
888          * event_context (argument) was NULL. If there's none, just
889          * create a new one.
890          */
891         if (ev == NULL) {
892                 ev = event_context_find(mem_ctx);
893                 if (ev == NULL) {
894                         new_ev = event_context_init(mem_ctx);
895                         if (new_ev == NULL) return NULL;
896                         ev = new_ev;
897                 }
898         }
899
900         /* composite context allocation and setup */
901         c = composite_create(mem_ctx, ev);
902         if (c == NULL) {
903                 talloc_free(new_ev);
904                 return NULL;
905         }
906         talloc_steal(c, new_ev);
907
908         s = talloc_zero(c, struct epm_map_binding_state);
909         if (composite_nomem(s, c)) return c;
910         c->private_data = s;
911
912         s->binding = binding;
913         s->table   = table;
914
915         /* anonymous credentials for rpc connection used to get endpoint mapping */
916         anon_creds = cli_credentials_init(mem_ctx);
917         cli_credentials_set_event_context(anon_creds, ev);
918         cli_credentials_set_conf(anon_creds);
919         cli_credentials_set_anonymous(anon_creds);
920
921         /*
922           First, check if there is a default endpoint specified in the IDL
923         */
924         if (table) {
925                 struct dcerpc_binding *default_binding;
926
927                 /* Find one of the default pipes for this interface */
928                 for (i = 0; i < table->endpoints->count; i++) {
929                         status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
930
931                         if (NT_STATUS_IS_OK(status)) {
932                                 if (default_binding->transport == binding->transport && default_binding->endpoint) {
933                                         binding->endpoint = talloc_reference(binding, default_binding->endpoint);
934                                         talloc_free(default_binding);
935
936                                         composite_done(c);
937                                         return c;
938
939                                 } else {
940                                         talloc_free(default_binding);
941                                 }
942                         }
943                 }
944         }
945
946         epmapper_binding = talloc_zero(c, struct dcerpc_binding);
947         if (composite_nomem(epmapper_binding, c)) return c;
948
949         /* basic endpoint mapping data */
950         epmapper_binding->transport             = binding->transport;
951         epmapper_binding->host                  = talloc_reference(epmapper_binding, binding->host);
952         epmapper_binding->target_hostname       = epmapper_binding->host;
953         epmapper_binding->options               = NULL;
954         epmapper_binding->flags                 = 0;
955         epmapper_binding->assoc_group_id        = 0;
956         epmapper_binding->endpoint              = NULL;
957
958         /* initiate rpc pipe connection */
959         pipe_connect_req = dcerpc_pipe_connect_b_send(c, epmapper_binding, &dcerpc_table_epmapper,
960                                                       anon_creds, c->event_ctx);
961         if (composite_nomem(pipe_connect_req, c)) return c;
962         
963         composite_continue(c, pipe_connect_req, continue_epm_recv_binding, c);
964         return c;
965 }
966
967
968 /*
969   Receive result of endpoint mapping request
970  */
971 NTSTATUS dcerpc_epm_map_binding_recv(struct composite_context *c)
972 {
973         NTSTATUS status = composite_wait(c);
974         
975         talloc_free(c);
976         return status;
977 }
978
979
980 /*
981   Get endpoint mapping for rpc connection
982 */
983 NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding,
984                                 const struct ndr_interface_table *table, struct event_context *ev)
985 {
986         struct composite_context *c;
987
988         c = dcerpc_epm_map_binding_send(mem_ctx, binding, table, ev);
989         return dcerpc_epm_map_binding_recv(c);
990 }
991
992
993 struct pipe_auth_state {
994         struct dcerpc_pipe *pipe;
995         struct dcerpc_binding *binding;
996         const struct ndr_interface_table *table;
997         struct cli_credentials *credentials;
998 };
999
1000
1001 static void continue_auth_schannel(struct composite_context *ctx);
1002 static void continue_auth(struct composite_context *ctx);
1003 static void continue_auth_none(struct composite_context *ctx);
1004 static void continue_ntlmssp_connection(struct composite_context *ctx);
1005 static void continue_spnego_after_wrong_pass(struct composite_context *ctx);
1006
1007
1008 /*
1009   Stage 2 of pipe_auth: Receive result of schannel bind request
1010 */
1011 static void continue_auth_schannel(struct composite_context *ctx)
1012 {
1013         struct composite_context *c = talloc_get_type(ctx->async.private_data,
1014                                                       struct composite_context);
1015
1016         c->status = dcerpc_bind_auth_schannel_recv(ctx);
1017         if (!composite_is_ok(c)) return;
1018
1019         composite_done(c);
1020 }
1021
1022
1023 /*
1024   Stage 2 of pipe_auth: Receive result of authenticated bind request
1025 */
1026 static void continue_auth(struct composite_context *ctx)
1027 {
1028         struct composite_context *c = talloc_get_type(ctx->async.private_data,
1029                                                       struct composite_context);
1030
1031         c->status = dcerpc_bind_auth_recv(ctx);
1032         if (!composite_is_ok(c)) return;
1033         
1034         composite_done(c);
1035 }
1036 /*
1037   Stage 2 of pipe_auth: Receive result of authenticated bind request, but handle fallbacks:
1038   SPNEGO -> NTLMSSP
1039 */
1040 static void continue_auth_auto(struct composite_context *ctx)
1041 {
1042         struct composite_context *c = talloc_get_type(ctx->async.private_data,
1043                                                       struct composite_context);
1044         struct pipe_auth_state *s = talloc_get_type(c->private_data, struct pipe_auth_state);
1045         struct composite_context *sec_conn_req;
1046
1047         c->status = dcerpc_bind_auth_recv(ctx);
1048         if (NT_STATUS_EQUAL(c->status, NT_STATUS_INVALID_PARAMETER)) {
1049                 /*
1050                  * Retry with NTLMSSP auth as fallback
1051                  * send a request for secondary rpc connection
1052                  */
1053                 sec_conn_req = dcerpc_secondary_connection_send(s->pipe,
1054                                                                 s->binding);
1055                 composite_continue(c, sec_conn_req, continue_ntlmssp_connection, c);
1056                 return;
1057         } else if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
1058                 if (cli_credentials_wrong_password(s->credentials)) {
1059                         /*
1060                          * Retry SPNEGO with a better password
1061                          * send a request for secondary rpc connection
1062                          */
1063                         sec_conn_req = dcerpc_secondary_connection_send(s->pipe,
1064                                                                         s->binding);
1065                         composite_continue(c, sec_conn_req, continue_spnego_after_wrong_pass, c);
1066                         return;
1067                 }
1068         }
1069
1070         if (!composite_is_ok(c)) return;
1071
1072         composite_done(c);
1073 }
1074
1075 /*
1076   Stage 3 of pipe_auth (fallback to NTLMSSP case): Receive secondary
1077   rpc connection (the first one can't be used any more, due to the
1078   bind nak) and perform authenticated bind request
1079 */
1080 static void continue_ntlmssp_connection(struct composite_context *ctx)
1081 {
1082         struct composite_context *c;
1083         struct pipe_auth_state *s;
1084         struct composite_context *auth_req;
1085         struct dcerpc_pipe *p2;
1086
1087         c = talloc_get_type(ctx->async.private_data, struct composite_context);
1088         s = talloc_get_type(c->private_data, struct pipe_auth_state);
1089
1090         /* receive secondary rpc connection */
1091         c->status = dcerpc_secondary_connection_recv(ctx, &p2);
1092         if (!composite_is_ok(c)) return;
1093
1094         talloc_steal(s, p2);
1095         talloc_steal(p2, s->pipe);
1096         s->pipe = p2;
1097
1098         /* initiate a authenticated bind */
1099         auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
1100                                          s->credentials, DCERPC_AUTH_TYPE_NTLMSSP,
1101                                          dcerpc_auth_level(s->pipe->conn),
1102                                          s->table->authservices->names[0]);
1103         composite_continue(c, auth_req, continue_auth, c);
1104 }
1105
1106 /*
1107   Stage 3 of pipe_auth (retry on wrong password): Receive secondary
1108   rpc connection (the first one can't be used any more, due to the
1109   bind nak) and perform authenticated bind request
1110 */
1111 static void continue_spnego_after_wrong_pass(struct composite_context *ctx)
1112 {
1113         struct composite_context *c;
1114         struct pipe_auth_state *s;
1115         struct composite_context *auth_req;
1116         struct dcerpc_pipe *p2;
1117
1118         c = talloc_get_type(ctx->async.private_data, struct composite_context);
1119         s = talloc_get_type(c->private_data, struct pipe_auth_state);
1120
1121         /* receive secondary rpc connection */
1122         c->status = dcerpc_secondary_connection_recv(ctx, &p2);
1123         if (!composite_is_ok(c)) return;
1124
1125         talloc_steal(s, p2);
1126         talloc_steal(p2, s->pipe);
1127         s->pipe = p2;
1128
1129         /* initiate a authenticated bind */
1130         auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
1131                                          s->credentials, DCERPC_AUTH_TYPE_SPNEGO,
1132                                          dcerpc_auth_level(s->pipe->conn),
1133                                          s->table->authservices->names[0]);
1134         composite_continue(c, auth_req, continue_auth, c);
1135 }
1136
1137
1138 /*
1139   Stage 2 of pipe_auth: Receive result of non-authenticated bind request
1140 */
1141 static void continue_auth_none(struct composite_context *ctx)
1142 {
1143         struct composite_context *c = talloc_get_type(ctx->async.private_data,
1144                                                       struct composite_context);
1145
1146         c->status = dcerpc_bind_auth_none_recv(ctx);
1147         if (!composite_is_ok(c)) return;
1148         
1149         composite_done(c);
1150 }
1151
1152
1153 /*
1154   Request to perform an authenticated bind if required. Authentication
1155   is determined using credentials passed and binding flags.
1156 */
1157 struct composite_context *dcerpc_pipe_auth_send(struct dcerpc_pipe *p, 
1158                                                 struct dcerpc_binding *binding,
1159                                                 const struct ndr_interface_table *table,
1160                                                 struct cli_credentials *credentials)
1161 {
1162         struct composite_context *c;
1163         struct pipe_auth_state *s;
1164         struct composite_context *auth_schannel_req;
1165         struct composite_context *auth_req;
1166         struct composite_context *auth_none_req;
1167         struct dcerpc_connection *conn;
1168         uint8_t auth_type;
1169
1170         /* composite context allocation and setup */
1171         c = composite_create(p, p->conn->event_ctx);
1172         if (c == NULL) return NULL;
1173
1174         s = talloc_zero(c, struct pipe_auth_state);
1175         if (composite_nomem(s, c)) return c;
1176         c->private_data = s;
1177
1178         /* store parameters in state structure */
1179         s->binding      = binding;
1180         s->table        = table;
1181         s->credentials  = credentials;
1182         s->pipe         = p;
1183
1184         conn = s->pipe->conn;
1185         conn->flags = binding->flags;
1186         
1187         /* remember the binding string for possible secondary connections */
1188         conn->binding_string = dcerpc_binding_string(p, binding);
1189
1190         if (cli_credentials_is_anonymous(s->credentials)) {
1191                 auth_none_req = dcerpc_bind_auth_none_send(c, s->pipe, s->table);
1192                 composite_continue(c, auth_none_req, continue_auth_none, c);
1193                 return c;
1194         }
1195
1196         if ((binding->flags & DCERPC_SCHANNEL) &&
1197             !cli_credentials_get_netlogon_creds(s->credentials)) {
1198                 /* If we don't already have netlogon credentials for
1199                  * the schannel bind, then we have to get these
1200                  * first */
1201                 auth_schannel_req = dcerpc_bind_auth_schannel_send(c, s->pipe, s->table,
1202                                                                    s->credentials,
1203                                                                    dcerpc_auth_level(conn));
1204                 composite_continue(c, auth_schannel_req, continue_auth_schannel, c);
1205                 return c;
1206         }
1207
1208         /*
1209          * we rely on the already authenticated CIFS connection
1210          * if not doing sign or seal
1211          */
1212         if (conn->transport.transport == NCACN_NP &&
1213             !(s->binding->flags & (DCERPC_SIGN|DCERPC_SEAL))) {
1214                 auth_none_req = dcerpc_bind_auth_none_send(c, s->pipe, s->table);
1215                 composite_continue(c, auth_none_req, continue_auth_none, c);
1216                 return c;
1217         }
1218
1219
1220         /* Perform an authenticated DCE-RPC bind
1221          */
1222         if (!(conn->flags & (DCERPC_SIGN|DCERPC_SEAL))) {
1223                 /*
1224                   we are doing an authenticated connection,
1225                   but not using sign or seal. We must force
1226                   the CONNECT dcerpc auth type as a NONE auth
1227                   type doesn't allow authentication
1228                   information to be passed.
1229                 */
1230                 conn->flags |= DCERPC_CONNECT;
1231         }
1232
1233         if (s->binding->flags & DCERPC_AUTH_SPNEGO) {
1234                 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
1235
1236         } else if (s->binding->flags & DCERPC_AUTH_KRB5) {
1237                 auth_type = DCERPC_AUTH_TYPE_KRB5;
1238
1239         } else if (s->binding->flags & DCERPC_SCHANNEL) {
1240                 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
1241
1242         } else if (s->binding->flags & DCERPC_AUTH_NTLM) {
1243                 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1244
1245         } else {
1246                 /* try SPNEGO with fallback to NTLMSSP */
1247                 auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
1248                                                  s->credentials, DCERPC_AUTH_TYPE_SPNEGO,
1249                                                  dcerpc_auth_level(conn),
1250                                                  s->table->authservices->names[0]);
1251                 composite_continue(c, auth_req, continue_auth_auto, c);
1252                 return c;
1253         }
1254
1255         auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
1256                                          s->credentials, auth_type,
1257                                          dcerpc_auth_level(conn),
1258                                          s->table->authservices->names[0]);
1259         composite_continue(c, auth_req, continue_auth, c);
1260         return c;
1261 }
1262
1263
1264 /*
1265   Receive result of authenticated bind request on dcerpc pipe
1266
1267   This returns *p, which may be different to the one originally
1268   supllied, as it rebinds to a new pipe due to authentication fallback
1269
1270 */
1271 NTSTATUS dcerpc_pipe_auth_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, 
1272                                struct dcerpc_pipe **p)
1273 {
1274         NTSTATUS status;
1275
1276         struct pipe_auth_state *s = talloc_get_type(c->private_data,
1277                                                     struct pipe_auth_state);
1278         status = composite_wait(c);
1279         if (!NT_STATUS_IS_OK(status)) {
1280                 char *uuid_str = GUID_string(s->pipe, &s->table->syntax_id.uuid);
1281                 DEBUG(0, ("Failed to bind to uuid %s - %s\n", uuid_str, nt_errstr(status)));
1282                 talloc_free(uuid_str);
1283         } else {
1284                 talloc_steal(mem_ctx, s->pipe);
1285                 *p = s->pipe;
1286         }
1287
1288         talloc_free(c);
1289         return status;
1290 }
1291
1292
1293 /* 
1294    Perform an authenticated bind if needed - sync version
1295
1296    This may change *p, as it rebinds to a new pipe due to authentication fallback
1297 */
1298 NTSTATUS dcerpc_pipe_auth(TALLOC_CTX *mem_ctx,
1299                           struct dcerpc_pipe **p, 
1300                           struct dcerpc_binding *binding,
1301                           const struct ndr_interface_table *table,
1302                           struct cli_credentials *credentials)
1303 {
1304         struct composite_context *c;
1305
1306         c = dcerpc_pipe_auth_send(*p, binding, table, credentials);
1307         return dcerpc_pipe_auth_recv(c, mem_ctx, p);
1308 }
1309
1310
1311 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
1312                                     DATA_BLOB *session_key)
1313 {
1314         /* this took quite a few CPU cycles to find ... */
1315         session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1316         session_key->length = 16;
1317         return NT_STATUS_OK;
1318 }
1319
1320 /*
1321   fetch the user session key - may be default (above) or the SMB session key
1322 */
1323 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1324                                   DATA_BLOB *session_key)
1325 {
1326         return p->conn->security_state.session_key(p->conn, session_key);
1327 }
1328
1329
1330 /*
1331   log a rpc packet in a format suitable for ndrdump. This is especially useful
1332   for sealed packets, where ethereal cannot easily see the contents
1333
1334   this triggers on a debug level of >= 10
1335 */
1336 void dcerpc_log_packet(const struct ndr_interface_table *ndr,
1337                        uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1338 {
1339         const int num_examples = 20;
1340         int i;
1341
1342         if (DEBUGLEVEL < 10) return;
1343
1344         for (i=0;i<num_examples;i++) {
1345                 char *name=NULL;
1346                 asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
1347                          lp_lockdir(), ndr->name, opnum, i,
1348                          (flags&NDR_IN)?"in":"out");
1349                 if (name == NULL) {
1350                         return;
1351                 }
1352                 if (!file_exist(name)) {
1353                         if (file_save(name, pkt->data, pkt->length)) {
1354                                 DEBUG(10,("Logged rpc packet to %s\n", name));
1355                         }
1356                         free(name);
1357                         break;
1358                 }
1359                 free(name);
1360         }
1361 }
1362
1363
1364
1365 /*
1366   create a secondary context from a primary connection
1367
1368   this uses dcerpc_alter_context() to create a new dcerpc context_id
1369 */
1370 NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p, 
1371                                   struct dcerpc_pipe **pp2,
1372                                   const struct ndr_interface_table *table)
1373 {
1374         NTSTATUS status;
1375         struct dcerpc_pipe *p2;
1376         
1377         p2 = talloc_zero(p, struct dcerpc_pipe);
1378         if (p2 == NULL) {
1379                 return NT_STATUS_NO_MEMORY;
1380         }
1381         p2->conn = talloc_reference(p2, p->conn);
1382         p2->request_timeout = p->request_timeout;
1383
1384         p2->context_id = ++p->conn->next_context_id;
1385
1386         p2->syntax = table->syntax_id;
1387
1388         p2->transfer_syntax = ndr_transfer_syntax;
1389
1390         p2->binding = talloc_reference(p2, p->binding);
1391
1392         status = dcerpc_alter_context(p2, p2, &p2->syntax, &p2->transfer_syntax);
1393         if (!NT_STATUS_IS_OK(status)) {
1394                 talloc_free(p2);
1395                 return status;
1396         }
1397
1398         *pp2 = p2;
1399
1400         return status;
1401 }