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