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