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