librpc: Simplify dcerpc_binding_string()
[asn/samba.git] / librpc / rpc / binding.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    Copyright (C) Stefan Metzmacher 2014
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "../../lib/util/util_net.h"
28 #include "librpc/gen_ndr/ndr_epmapper.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "rpc_common.h"
32
33 #undef strcasecmp
34 #undef strncasecmp
35
36 #define MAX_PROTSEQ             10
37
38 struct dcerpc_binding {
39         enum dcerpc_transport_t transport;
40         struct GUID object;
41         const char *object_string;
42         const char *host;
43         const char *target_hostname;
44         const char *target_principal;
45         const char *endpoint;
46         const char **options;
47         uint32_t flags;
48         uint32_t assoc_group_id;
49         char assoc_group_string[11]; /* 0x3456789a + '\0' */
50 };
51
52 static const struct {
53         const char *name;
54         enum dcerpc_transport_t transport;
55         int num_protocols;
56         enum epm_protocol protseq[MAX_PROTSEQ];
57 } transports[] = {
58         { "ncacn_np",     NCACN_NP, 3, 
59                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
60         { "ncacn_ip_tcp", NCACN_IP_TCP, 3, 
61                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } }, 
62         { "ncacn_http", NCACN_HTTP, 3, 
63                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } }, 
64         { "ncadg_ip_udp", NCACN_IP_UDP, 3, 
65                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
66         { "ncalrpc", NCALRPC, 2, 
67                 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_NAMED_PIPE } },
68         { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2, 
69                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
70         { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2, 
71                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
72         { "ncacn_at_dsp", NCACN_AT_DSP, 3, 
73                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
74         { "ncadg_at_ddp", NCADG_AT_DDP, 3, 
75                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
76         { "ncacn_vns_ssp", NCACN_VNS_SPP, 3, 
77                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
78         { "ncacn_vns_ipc", NCACN_VNS_IPC, 3, 
79                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
80         { "ncadg_ipx", NCADG_IPX, 2,
81                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
82         },
83         { "ncacn_spx", NCACN_SPX, 3,
84                 /* I guess some MS programmer confused the identifier for 
85                  * EPM_PROTOCOL_UUID (0x0D or 13) with the one for 
86                  * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
87                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
88         },
89 };
90
91 static const struct ncacn_option {
92         const char *name;
93         uint32_t flag;
94 } ncacn_options[] = {
95         {"sign", DCERPC_SIGN},
96         {"seal", DCERPC_SEAL},
97         {"connect", DCERPC_CONNECT},
98         {"spnego", DCERPC_AUTH_SPNEGO},
99         {"ntlm", DCERPC_AUTH_NTLM},
100         {"krb5", DCERPC_AUTH_KRB5},
101         {"schannel", DCERPC_SCHANNEL | DCERPC_SCHANNEL_AUTO},
102         {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
103         {"print", DCERPC_DEBUG_PRINT_BOTH},
104         {"padcheck", DCERPC_DEBUG_PAD_CHECK},
105         {"bigendian", DCERPC_PUSH_BIGENDIAN},
106         {"smb1", DCERPC_SMB1},
107         {"smb2", DCERPC_SMB2},
108         {"ndr64", DCERPC_NDR64},
109         {"packet", DCERPC_PACKET},
110 };
111
112 static const struct ncacn_option *ncacn_option_by_name(const char *name)
113 {
114         size_t i;
115
116         for (i=0; i<ARRAY_SIZE(ncacn_options); i++) {
117                 int ret;
118
119                 ret = strcasecmp(ncacn_options[i].name, name);
120                 if (ret != 0) {
121                         continue;
122                 }
123
124                 return &ncacn_options[i];
125         }
126
127         return NULL;
128 }
129
130 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
131 {
132         struct ndr_syntax_id syntax;
133         NTSTATUS status;
134
135         switch(epm_floor->lhs.protocol) {
136                 case EPM_PROTOCOL_UUID:
137                         status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
138                         if (NT_STATUS_IS_OK(status)) {
139                                 /* lhs is used: UUID */
140                                 struct GUID_txt_buf buf;
141
142                                 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr.uuid)) {
143                                         return "NDR";
144                                 } 
145
146                                 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr64.uuid)) {
147                                         return "NDR64";
148                                 } 
149
150                                 return talloc_asprintf(
151                                         mem_ctx,
152                                         " uuid %s/0x%02x",
153                                         GUID_buf_string(&syntax.uuid, &buf),
154                                         syntax.if_version);
155                         } else { /* IPX */
156                                 return talloc_asprintf(mem_ctx, "IPX:%s", 
157                                                 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
158                         }
159
160                 case EPM_PROTOCOL_NCACN:
161                         return "RPC-C";
162
163                 case EPM_PROTOCOL_NCADG:
164                         return "RPC";
165
166                 case EPM_PROTOCOL_NCALRPC:
167                         return "NCALRPC";
168
169                 case EPM_PROTOCOL_DNET_NSP:
170                         return "DNET/NSP";
171
172                 case EPM_PROTOCOL_IP:
173                         return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
174
175                 case EPM_PROTOCOL_NAMED_PIPE:
176                         return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
177
178                 case EPM_PROTOCOL_SMB:
179                         return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
180
181                 case EPM_PROTOCOL_UNIX_DS:
182                         return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
183
184                 case EPM_PROTOCOL_NETBIOS:
185                         return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
186
187                 case EPM_PROTOCOL_NETBEUI:
188                         return "NETBeui";
189
190                 case EPM_PROTOCOL_SPX:
191                         return "SPX";
192
193                 case EPM_PROTOCOL_NB_IPX:
194                         return "NB_IPX";
195
196                 case EPM_PROTOCOL_HTTP:
197                         return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
198
199                 case EPM_PROTOCOL_TCP:
200                         return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
201
202                 case EPM_PROTOCOL_UDP:
203                         return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
204
205                 default:
206                         return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
207         }
208 }
209
210
211 /*
212   form a binding string from a binding structure
213 */
214 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
215 {
216         char *s = talloc_strdup(mem_ctx, "");
217         char *o = s;
218         size_t i;
219         const char *t_name = NULL;
220         bool option_section = false;
221         const char *target_hostname = NULL;
222
223         if (b->transport != NCA_UNKNOWN) {
224                 t_name = derpc_transport_string_by_transport(b->transport);
225                 if (!t_name) {
226                         talloc_free(o);
227                         return NULL;
228                 }
229         }
230
231         if (!GUID_all_zero(&b->object)) {
232                 struct GUID_txt_buf buf;
233
234                 o = s;
235                 s = talloc_asprintf_append_buffer(
236                         s, "%s@", GUID_buf_string(&b->object, &buf));
237                 if (s == NULL) {
238                         talloc_free(o);
239                         return NULL;
240                 }
241         }
242
243         if (t_name != NULL) {
244                 o = s;
245                 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
246                 if (s == NULL) {
247                         talloc_free(o);
248                         return NULL;
249                 }
250         }
251
252         if (b->host) {
253                 o = s;
254                 s = talloc_asprintf_append_buffer(s, "%s", b->host);
255                 if (s == NULL) {
256                         talloc_free(o);
257                         return NULL;
258                 }
259         }
260
261         target_hostname = b->target_hostname;
262         if (target_hostname != NULL && b->host != NULL) {
263                 if (strcmp(target_hostname, b->host) == 0) {
264                         target_hostname = NULL;
265                 }
266         }
267
268         option_section =
269                 (b->endpoint != NULL) ||
270                 (target_hostname != NULL) ||
271                 (b->target_principal != NULL) ||
272                 (b->assoc_group_id != 0) ||
273                 (b->options != NULL) ||
274                 (b->flags != 0);
275
276         if (!option_section) {
277                 return s;
278         }
279
280         o = s;
281         s = talloc_asprintf_append_buffer(s, "[");
282         if (s == NULL) {
283                 talloc_free(o);
284                 return NULL;
285         }
286
287         if (b->endpoint) {
288                 o = s;
289                 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
290                 if (s == NULL) {
291                         talloc_free(o);
292                         return NULL;
293                 }
294         }
295
296         for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
297                 if (!(b->flags & ncacn_options[i].flag)) {
298                         continue;
299                 }
300
301                 o = s;
302                 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
303                 if (s == NULL) {
304                         talloc_free(o);
305                         return NULL;
306                 }
307         }
308
309         if (target_hostname) {
310                 o = s;
311                 s = talloc_asprintf_append_buffer(s, ",target_hostname=%s",
312                                                   b->target_hostname);
313                 if (s == NULL) {
314                         talloc_free(o);
315                         return NULL;
316                 }
317         }
318
319         if (b->target_principal) {
320                 o = s;
321                 s = talloc_asprintf_append_buffer(s, ",target_principal=%s",
322                                                   b->target_principal);
323                 if (s == NULL) {
324                         talloc_free(o);
325                         return NULL;
326                 }
327         }
328
329         if (b->assoc_group_id != 0) {
330                 o = s;
331                 s = talloc_asprintf_append_buffer(s, ",assoc_group_id=0x%08x",
332                                                   b->assoc_group_id);
333                 if (s == NULL) {
334                         talloc_free(o);
335                         return NULL;
336                 }
337         }
338
339         for (i=0;b->options && b->options[i];i++) {
340                 o = s;
341                 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
342                 if (s == NULL) {
343                         talloc_free(o);
344                         return NULL;
345                 }
346         }
347
348         o = s;
349         s = talloc_asprintf_append_buffer(s, "]");
350         if (s == NULL) {
351                 talloc_free(o);
352                 return NULL;
353         }
354
355         return s;
356 }
357
358 /*
359   parse a binding string into a dcerpc_binding structure
360 */
361 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *_s, struct dcerpc_binding **b_out)
362 {
363         char *_t;
364         struct dcerpc_binding *b;
365         char *s;
366         char *options = NULL;
367         char *p;
368         size_t i;
369         NTSTATUS status;
370
371         b = talloc_zero(mem_ctx, struct dcerpc_binding);
372         if (!b) {
373                 return NT_STATUS_NO_MEMORY;
374         }
375
376         _t = talloc_strdup(b, _s);
377         if (_t == NULL) {
378                 talloc_free(b);
379                 return NT_STATUS_NO_MEMORY;
380         }
381
382         s = _t;
383
384         p = strchr(s, '[');
385         if (p) {
386                 char *q = p + strlen(p) - 1;
387                 if (*q != ']') {
388                         talloc_free(b);
389                         return NT_STATUS_INVALID_PARAMETER_MIX;
390                 }
391                 *p = '\0';
392                 *q = '\0';
393                 options = p + 1;
394         }
395
396         p = strchr(s, '@');
397
398         if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
399                 *p = '\0';
400
401                 status = dcerpc_binding_set_string_option(b, "object", s);
402                 if (!NT_STATUS_IS_OK(status)) {
403                         talloc_free(b);
404                         return status;
405                 }
406
407                 s = p + 1;
408         }
409
410         p = strchr(s, ':');
411
412         if (p == NULL) {
413                 b->transport = NCA_UNKNOWN;
414         } else if (is_ipaddress_v6(s)) {
415                 b->transport = NCA_UNKNOWN;
416         } else {
417                 *p = '\0';
418
419                 status = dcerpc_binding_set_string_option(b, "transport", s);
420                 if (!NT_STATUS_IS_OK(status)) {
421                         talloc_free(b);
422                         return status;
423                 }
424
425                 s = p + 1;
426         }
427
428         if (strlen(s) > 0) {
429                 status = dcerpc_binding_set_string_option(b, "host", s);
430                 if (!NT_STATUS_IS_OK(status)) {
431                         talloc_free(b);
432                         return status;
433                 }
434
435                 b->target_hostname = talloc_strdup(b, b->host);
436                 if (b->target_hostname == NULL) {
437                         talloc_free(b);
438                         return NT_STATUS_NO_MEMORY;
439                 }
440         }
441
442         for (i=0; options != NULL; i++) {
443                 const char *name = options;
444                 const char *value = NULL;
445
446                 p = strchr(options, ',');
447                 if (p != NULL) {
448                         *p = '\0';
449                         options = p+1;
450                 } else {
451                         options = NULL;
452                 }
453
454                 p = strchr(name, '=');
455                 if (p != NULL) {
456                         *p = '\0';
457                         value = p + 1;
458                 }
459
460                 if (value == NULL) {
461                         /*
462                          * If it's not a key=value pair
463                          * it might be a ncacn_option
464                          * or if it's the first option
465                          * it's the endpoint.
466                          */
467                         const struct ncacn_option *no = NULL;
468
469                         value = name;
470
471                         no = ncacn_option_by_name(name);
472                         if (no == NULL) {
473                                 if (i > 0) {
474                                         /*
475                                          * we don't allow unknown options
476                                          */
477                                         return NT_STATUS_INVALID_PARAMETER_MIX;
478                                 }
479
480                                 /*
481                                  * This is the endpoint
482                                  */
483                                 name = "endpoint";
484                                 if (strlen(value) == 0) {
485                                         value = NULL;
486                                 }
487                         }
488                 }
489
490                 status = dcerpc_binding_set_string_option(b, name, value);
491                 if (!NT_STATUS_IS_OK(status)) {
492                         talloc_free(b);
493                         return status;
494                 }
495         }
496
497         talloc_free(_t);
498         *b_out = b;
499         return NT_STATUS_OK;
500 }
501
502 _PUBLIC_ struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b)
503 {
504         return b->object;
505 }
506
507 _PUBLIC_ NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
508                                             struct GUID object)
509 {
510         char *tmp = discard_const_p(char, b->object_string);
511
512         if (GUID_all_zero(&object)) {
513                 talloc_free(tmp);
514                 b->object_string = NULL;
515                 ZERO_STRUCT(b->object);
516                 return NT_STATUS_OK;
517         }
518
519         b->object_string = GUID_string(b, &object);
520         if (b->object_string == NULL) {
521                 b->object_string = tmp;
522                 return NT_STATUS_NO_MEMORY;
523         }
524         talloc_free(tmp);
525
526         b->object = object;
527         return NT_STATUS_OK;
528 }
529
530 _PUBLIC_ enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b)
531 {
532         return b->transport;
533 }
534
535 _PUBLIC_ NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
536                                                enum dcerpc_transport_t transport)
537 {
538         NTSTATUS status;
539
540         /*
541          * TODO: we may want to check the transport value is
542          * wellknown.
543          */
544         if (b->transport == transport) {
545                 return NT_STATUS_OK;
546         }
547
548         /*
549          * This implicitly resets the endpoint
550          * as the endpoint is transport specific.
551          *
552          * It also resets the assoc group as it's
553          * also endpoint specific.
554          *
555          * TODO: in future we may reset more options
556          * here.
557          */
558         status = dcerpc_binding_set_string_option(b, "endpoint", NULL);
559         if (!NT_STATUS_IS_OK(status)) {
560                 return status;
561         }
562
563         b->assoc_group_id = 0;
564
565         b->transport = transport;
566         return NT_STATUS_OK;
567 }
568
569 _PUBLIC_ void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
570                                            enum dcerpc_AuthType *_auth_type,
571                                            enum dcerpc_AuthLevel *_auth_level)
572 {
573         enum dcerpc_AuthType auth_type;
574         enum dcerpc_AuthLevel auth_level;
575
576         if (b->flags & DCERPC_AUTH_SPNEGO) {
577                 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
578         } else if (b->flags & DCERPC_AUTH_KRB5) {
579                 auth_type = DCERPC_AUTH_TYPE_KRB5;
580         } else if (b->flags & DCERPC_SCHANNEL) {
581                 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
582         } else if (b->flags & DCERPC_AUTH_NTLM) {
583                 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
584         } else {
585                 auth_type = DCERPC_AUTH_TYPE_NONE;
586         }
587
588         if (b->flags & DCERPC_SEAL) {
589                 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
590         } else if (b->flags & DCERPC_SIGN) {
591                 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
592         } else if (b->flags & DCERPC_CONNECT) {
593                 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
594         } else if (b->flags & DCERPC_PACKET) {
595                 auth_level = DCERPC_AUTH_LEVEL_PACKET;
596         } else if (auth_type != DCERPC_AUTH_TYPE_NONE) {
597                 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
598         } else {
599                 auth_level = DCERPC_AUTH_LEVEL_NONE;
600         }
601
602         if (_auth_type != NULL) {
603                 *_auth_type = auth_type;
604         }
605
606         if (_auth_level != NULL) {
607                 *_auth_level = auth_level;
608         }
609 }
610
611 _PUBLIC_ uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b)
612 {
613         return b->assoc_group_id;
614 }
615
616 _PUBLIC_ NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
617                                                     uint32_t assoc_group_id)
618 {
619         b->assoc_group_id = assoc_group_id;
620         return NT_STATUS_OK;
621 }
622
623 _PUBLIC_ struct ndr_syntax_id dcerpc_binding_get_abstract_syntax(const struct dcerpc_binding *b)
624 {
625         const char *s = dcerpc_binding_get_string_option(b, "abstract_syntax");
626         bool ok;
627         struct ndr_syntax_id id;
628
629         if (s == NULL) {
630                 return ndr_syntax_id_null;
631         }
632
633         ok = ndr_syntax_id_from_string(s, &id);
634         if (!ok) {
635                 return ndr_syntax_id_null;
636         }
637
638         return id;
639 }
640
641 _PUBLIC_ NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
642                                                      const struct ndr_syntax_id *syntax)
643 {
644         NTSTATUS status;
645         struct ndr_syntax_id_buf buf;
646
647         if (syntax == NULL) {
648                 status = dcerpc_binding_set_string_option(b, "abstract_syntax", NULL);
649                 return status;
650         }
651
652         if (ndr_syntax_id_equal(&ndr_syntax_id_null, syntax)) {
653                 status = dcerpc_binding_set_string_option(b, "abstract_syntax", NULL);
654                 return status;
655         }
656
657         status = dcerpc_binding_set_string_option(
658                 b, "abstract_syntax", ndr_syntax_id_buf_string(syntax, &buf));
659         return status;
660 }
661
662 _PUBLIC_ const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
663                                                       const char *name)
664 {
665         struct {
666                 const char *name;
667                 const char *value;
668 #define _SPECIAL(x) { .name = #x, .value = b->x, }
669         } specials[] = {
670                 { .name = "object", .value = b->object_string, },
671                 _SPECIAL(host),
672                 _SPECIAL(endpoint),
673                 _SPECIAL(target_hostname),
674                 _SPECIAL(target_principal),
675 #undef _SPECIAL
676         };
677         const struct ncacn_option *no = NULL;
678         size_t name_len = strlen(name);
679         size_t i;
680         int ret;
681
682         ret = strcmp(name, "transport");
683         if (ret == 0) {
684                 return derpc_transport_string_by_transport(b->transport);
685         }
686
687         ret = strcmp(name, "assoc_group_id");
688         if (ret == 0) {
689                 char *tmp = discard_const_p(char, b->assoc_group_string);
690
691                 if (b->assoc_group_id == 0) {
692                         return NULL;
693                 }
694
695                 snprintf(tmp, sizeof(b->assoc_group_string),
696                          "0x%08x", b->assoc_group_id);
697                 return (const char *)b->assoc_group_string;
698         }
699
700         for (i=0; i < ARRAY_SIZE(specials); i++) {
701                 ret = strcmp(specials[i].name, name);
702                 if (ret != 0) {
703                         continue;
704                 }
705
706                 return specials[i].value;
707         }
708
709         no = ncacn_option_by_name(name);
710         if (no != NULL) {
711                 if (b->flags & no->flag) {
712                         return no->name;
713                 }
714
715                 return NULL;
716         }
717
718         if (b->options == NULL) {
719                 return NULL;
720         }
721
722         for (i=0; b->options[i]; i++) {
723                 const char *o = b->options[i];
724                 const char *vs = NULL;
725
726                 ret = strncmp(name, o, name_len);
727                 if (ret != 0) {
728                         continue;
729                 }
730
731                 if (o[name_len] != '=') {
732                         continue;
733                 }
734
735                 vs = &o[name_len + 1];
736
737                 return vs;
738         }
739
740         return NULL;
741 }
742
743 _PUBLIC_ char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
744                                                  const struct dcerpc_binding *b,
745                                                  const char *name)
746 {
747         const char *c = dcerpc_binding_get_string_option(b, name);
748         char *v;
749
750         if (c == NULL) {
751                 errno = ENOENT;
752                 return NULL;
753         }
754
755         v = talloc_strdup(mem_ctx, c);
756         if (v == NULL) {
757                 errno = ENOMEM;
758                 return NULL;
759         }
760
761         return v;
762 }
763
764 _PUBLIC_ NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
765                                                    const char *name,
766                                                    const char *value)
767 {
768         struct {
769                 const char *name;
770                 const char **ptr;
771 #define _SPECIAL(x) { .name = #x, .ptr = &b->x, }
772         } specials[] = {
773                 _SPECIAL(host),
774                 _SPECIAL(endpoint),
775                 _SPECIAL(target_hostname),
776                 _SPECIAL(target_principal),
777 #undef _SPECIAL
778         };
779         const struct ncacn_option *no = NULL;
780         size_t name_len = strlen(name);
781         const char *opt = NULL;
782         char *tmp;
783         size_t i;
784         int ret;
785
786         /*
787          * Note: value == NULL, means delete it.
788          * value != NULL means add or reset.
789          */
790
791         ret = strcmp(name, "transport");
792         if (ret == 0) {
793                 enum dcerpc_transport_t t = dcerpc_transport_by_name(value);
794
795                 if (t == NCA_UNKNOWN && value != NULL) {
796                         return NT_STATUS_INVALID_PARAMETER_MIX;
797                 }
798
799                 return dcerpc_binding_set_transport(b, t);
800         }
801
802         ret = strcmp(name, "object");
803         if (ret == 0) {
804                 NTSTATUS status;
805                 struct GUID uuid = GUID_zero();
806
807                 if (value != NULL) {
808                         DATA_BLOB blob;
809                         blob = data_blob_string_const(value);
810                         if (blob.length != 36) {
811                                 return NT_STATUS_INVALID_PARAMETER_MIX;
812                         }
813
814                         status = GUID_from_data_blob(&blob, &uuid);
815                         if (!NT_STATUS_IS_OK(status)) {
816                                 return status;
817                         }
818                 }
819
820                 return dcerpc_binding_set_object(b, uuid);
821         }
822
823         ret = strcmp(name, "assoc_group_id");
824         if (ret == 0) {
825                 uint32_t assoc_group_id = 0;
826
827                 if (value != NULL) {
828                         char c;
829
830                         ret = sscanf(value, "0x%08x%c", &assoc_group_id, &c);
831                         if (ret != 1) {
832                                 return NT_STATUS_INVALID_PARAMETER_MIX;
833                         }
834                 }
835
836                 return dcerpc_binding_set_assoc_group_id(b, assoc_group_id);
837         }
838
839         for (i=0; i < ARRAY_SIZE(specials); i++) {
840                 ret = strcmp(specials[i].name, name);
841                 if (ret != 0) {
842                         continue;
843                 }
844
845                 tmp = discard_const_p(char, *specials[i].ptr);
846
847                 if (value == NULL) {
848                         talloc_free(tmp);
849                         *specials[i].ptr = NULL;
850                         return NT_STATUS_OK;
851                 }
852
853                 if (value[0] == '\0') {
854                         return NT_STATUS_INVALID_PARAMETER_MIX;
855                 }
856
857                 *specials[i].ptr = talloc_strdup(b, value);
858                 if (*specials[i].ptr == NULL) {
859                         *specials[i].ptr = tmp;
860                         return NT_STATUS_NO_MEMORY;
861                 }
862                 talloc_free(tmp);
863
864                 return NT_STATUS_OK;
865         }
866
867         no = ncacn_option_by_name(name);
868         if (no != NULL) {
869                 if (value == NULL) {
870                         b->flags &= ~no->flag;
871                         return NT_STATUS_OK;
872                 }
873
874                 ret = strcasecmp(no->name, value);
875                 if (ret != 0) {
876                         return NT_STATUS_INVALID_PARAMETER_MIX;
877                 }
878
879                 b->flags |= no->flag;
880                 return NT_STATUS_OK;
881         }
882
883         for (i=0; b->options && b->options[i]; i++) {
884                 const char *o = b->options[i];
885
886                 ret = strncmp(name, o, name_len);
887                 if (ret != 0) {
888                         continue;
889                 }
890
891                 if (o[name_len] != '=') {
892                         continue;
893                 }
894
895                 opt = o;
896                 break;
897         }
898
899         if (opt == NULL) {
900                 const char **n;
901
902                 if (value == NULL) {
903                         return NT_STATUS_OK;
904                 }
905
906                 n = talloc_realloc(b, b->options, const char *, i + 2);
907                 if (n == NULL) {
908                         return NT_STATUS_NO_MEMORY;
909                 }
910                 n[i] = NULL;
911                 n[i + 1] = NULL;
912                 b->options = n;
913         }
914
915         tmp = discard_const_p(char, opt);
916
917         if (value == NULL) {
918                 for (;b->options[i];i++) {
919                         b->options[i] = b->options[i+1];
920                 }
921                 talloc_free(tmp);
922                 return NT_STATUS_OK;
923         }
924
925         b->options[i] = talloc_asprintf(b->options, "%s=%s",
926                                         name, value);
927         if (b->options[i] == NULL) {
928                 b->options[i] = tmp;
929                 return NT_STATUS_NO_MEMORY;
930         }
931
932         return NT_STATUS_OK;
933 }
934
935 _PUBLIC_ uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b)
936 {
937         return b->flags;
938 }
939
940 _PUBLIC_ NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
941                                            uint32_t additional,
942                                            uint32_t clear)
943 {
944         /*
945          * TODO: in future we may want to reject invalid combinations
946          */
947         b->flags &= ~clear;
948         b->flags |= additional;
949
950         return NT_STATUS_OK;
951 }
952
953 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
954                                             struct ndr_syntax_id *syntax)
955 {
956         TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
957         struct ndr_pull *ndr;
958         enum ndr_err_code ndr_err;
959         uint16_t if_version=0;
960
961         ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
962         if (ndr == NULL) {
963                 talloc_free(mem_ctx);
964                 return NT_STATUS_NO_MEMORY;
965         }
966         ndr->flags |= LIBNDR_FLAG_NOALIGN;
967
968         ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
969         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
970                 talloc_free(mem_ctx);
971                 return ndr_map_error2ntstatus(ndr_err);
972         }
973
974         ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
975         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
976                 talloc_free(mem_ctx);
977                 return ndr_map_error2ntstatus(ndr_err);
978         }
979
980         syntax->if_version = if_version;
981
982         talloc_free(mem_ctx);
983
984         return NT_STATUS_OK;
985 }
986
987 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
988 {
989         DATA_BLOB blob;
990         enum ndr_err_code ndr_err;
991         struct ndr_push *ndr;
992
993         ndr = ndr_push_init_ctx(mem_ctx);
994         if (ndr == NULL) {
995                 return data_blob_null;
996         }
997
998         ndr->flags |= LIBNDR_FLAG_NOALIGN;
999
1000         ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
1001         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1002                 return data_blob_null;
1003         }
1004         ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
1005         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1006                 return data_blob_null;
1007         }
1008
1009         blob = ndr_push_blob(ndr);
1010         talloc_steal(mem_ctx, blob.data);
1011         talloc_free(ndr);
1012         return blob;
1013 }
1014
1015 static bool dcerpc_floor_pack_rhs_if_version_data(
1016         TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
1017         DATA_BLOB *pblob)
1018 {
1019         DATA_BLOB blob;
1020         struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
1021         enum ndr_err_code ndr_err;
1022
1023         if (ndr == NULL) {
1024                 return false;
1025         }
1026
1027         ndr->flags |= LIBNDR_FLAG_NOALIGN;
1028
1029         ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
1030         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1031                 return false;
1032         }
1033
1034         blob = ndr_push_blob(ndr);
1035         talloc_steal(mem_ctx, blob.data);
1036         talloc_free(ndr);
1037         *pblob = blob;
1038         return true;
1039 }
1040
1041 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
1042 {
1043         switch (epm_floor->lhs.protocol) {
1044         case EPM_PROTOCOL_TCP:
1045                 if (epm_floor->rhs.tcp.port == 0) return NULL;
1046                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
1047
1048         case EPM_PROTOCOL_UDP:
1049                 if (epm_floor->rhs.udp.port == 0) return NULL;
1050                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
1051
1052         case EPM_PROTOCOL_HTTP:
1053                 if (epm_floor->rhs.http.port == 0) return NULL;
1054                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
1055
1056         case EPM_PROTOCOL_IP:
1057                 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
1058
1059         case EPM_PROTOCOL_NCACN:
1060                 return NULL;
1061
1062         case EPM_PROTOCOL_NCADG:
1063                 return NULL;
1064
1065         case EPM_PROTOCOL_SMB:
1066                 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
1067                 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
1068
1069         case EPM_PROTOCOL_NAMED_PIPE:
1070                 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
1071                 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
1072
1073         case EPM_PROTOCOL_NETBIOS:
1074                 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
1075                 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
1076
1077         case EPM_PROTOCOL_NCALRPC:
1078                 return NULL;
1079
1080         case EPM_PROTOCOL_VINES_SPP:
1081                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
1082
1083         case EPM_PROTOCOL_VINES_IPC:
1084                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
1085
1086         case EPM_PROTOCOL_STREETTALK:
1087                 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
1088
1089         case EPM_PROTOCOL_UNIX_DS:
1090                 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
1091                 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
1092
1093         case EPM_PROTOCOL_NULL:
1094                 return NULL;
1095
1096         default:
1097                 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1098                 break;
1099         }
1100
1101         return NULL;
1102 }
1103
1104 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, 
1105                                           struct epm_floor *epm_floor,  
1106                                           const char *data)
1107 {
1108         if (data == NULL) {
1109                 data = "";
1110         }
1111
1112         switch (epm_floor->lhs.protocol) {
1113         case EPM_PROTOCOL_TCP:
1114                 epm_floor->rhs.tcp.port = atoi(data);
1115                 return NT_STATUS_OK;
1116
1117         case EPM_PROTOCOL_UDP:
1118                 epm_floor->rhs.udp.port = atoi(data);
1119                 return NT_STATUS_OK;
1120
1121         case EPM_PROTOCOL_HTTP:
1122                 epm_floor->rhs.http.port = atoi(data);
1123                 return NT_STATUS_OK;
1124
1125         case EPM_PROTOCOL_IP:
1126                 if (!is_ipaddress_v4(data)) {
1127                         data = "0.0.0.0";
1128                 }
1129                 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
1130                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
1131                 return NT_STATUS_OK;
1132
1133         case EPM_PROTOCOL_NCACN:
1134                 epm_floor->rhs.ncacn.minor_version = 0;
1135                 return NT_STATUS_OK;
1136
1137         case EPM_PROTOCOL_NCADG:
1138                 epm_floor->rhs.ncadg.minor_version = 0;
1139                 return NT_STATUS_OK;
1140
1141         case EPM_PROTOCOL_SMB:
1142                 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
1143                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
1144                 return NT_STATUS_OK;
1145
1146         case EPM_PROTOCOL_NAMED_PIPE:
1147                 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
1148                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
1149                 return NT_STATUS_OK;
1150
1151         case EPM_PROTOCOL_NETBIOS:
1152                 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
1153                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
1154                 return NT_STATUS_OK;
1155
1156         case EPM_PROTOCOL_NCALRPC:
1157                 return NT_STATUS_OK;
1158
1159         case EPM_PROTOCOL_VINES_SPP:
1160                 epm_floor->rhs.vines_spp.port = atoi(data);
1161                 return NT_STATUS_OK;
1162
1163         case EPM_PROTOCOL_VINES_IPC:
1164                 epm_floor->rhs.vines_ipc.port = atoi(data);
1165                 return NT_STATUS_OK;
1166
1167         case EPM_PROTOCOL_STREETTALK:
1168                 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
1169                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
1170                 return NT_STATUS_OK;
1171
1172         case EPM_PROTOCOL_UNIX_DS:
1173                 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
1174                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
1175                 return NT_STATUS_OK;
1176
1177         case EPM_PROTOCOL_NULL:
1178                 return NT_STATUS_OK;
1179
1180         default:
1181                 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1182                 break;
1183         }
1184
1185         return NT_STATUS_NOT_SUPPORTED;
1186 }
1187
1188 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
1189 {
1190         size_t i;
1191
1192         /* Find a transport that has 'prot' as 4th protocol */
1193         for (i=0;i<ARRAY_SIZE(transports);i++) {
1194                 if (transports[i].num_protocols >= 2 && 
1195                         transports[i].protseq[1] == prot) {
1196                         return transports[i].transport;
1197                 }
1198         }
1199
1200         /* Unknown transport */
1201         return (unsigned int)-1;
1202 }
1203
1204 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
1205 {
1206         size_t i;
1207
1208         /* Find a transport that matches this tower */
1209         for (i=0;i<ARRAY_SIZE(transports);i++) {
1210                 int j;
1211                 if (transports[i].num_protocols != tower->num_floors - 2) {
1212                         continue; 
1213                 }
1214
1215                 for (j = 0; j < transports[i].num_protocols && j < MAX_PROTSEQ; j++) {
1216                         if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
1217                                 break;
1218                         }
1219                 }
1220
1221                 if (j == transports[i].num_protocols) {
1222                         return transports[i].transport;
1223                 }
1224         }
1225
1226         /* Unknown transport */
1227         return (unsigned int)-1;
1228 }
1229
1230 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
1231 {
1232         size_t i;
1233
1234         for (i=0; i<ARRAY_SIZE(transports); i++) {
1235                 if (t == transports[i].transport) {
1236                         return transports[i].name;
1237                 }
1238         }
1239         return NULL;
1240 }
1241
1242 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
1243 {
1244         size_t i;
1245
1246         if (name == NULL) {
1247                 return NCA_UNKNOWN;
1248         }
1249
1250         for (i=0; i<ARRAY_SIZE(transports);i++) {
1251                 if (strcasecmp(name, transports[i].name) == 0) {
1252                         return transports[i].transport;
1253                 }
1254         }
1255
1256         return NCA_UNKNOWN;
1257 }
1258
1259 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
1260                                             struct epm_tower *tower,
1261                                             struct dcerpc_binding **b_out)
1262 {
1263         NTSTATUS status;
1264         struct dcerpc_binding *b;
1265         enum dcerpc_transport_t transport;
1266         struct ndr_syntax_id abstract_syntax;
1267         char *endpoint = NULL;
1268         char *host = NULL;
1269
1270         /*
1271          * A tower needs to have at least 4 floors to carry useful
1272          * information. Floor 3 is the transport identifier which defines
1273          * how many floors are required at least.
1274          */
1275         if (tower->num_floors < 4) {
1276                 return NT_STATUS_INVALID_PARAMETER;
1277         }
1278
1279         status = dcerpc_parse_binding(mem_ctx, "", &b);
1280         if (!NT_STATUS_IS_OK(status)) {
1281                 return status;
1282         }
1283
1284         transport = dcerpc_transport_by_tower(tower);
1285         if (transport == NCA_UNKNOWN) {
1286                 talloc_free(b);
1287                 return NT_STATUS_NOT_SUPPORTED;
1288         }
1289
1290         status = dcerpc_binding_set_transport(b, transport);
1291         if (!NT_STATUS_IS_OK(status)) {
1292                 talloc_free(b);
1293                 return status;
1294         }
1295
1296         /* Set abstract syntax */
1297         status = dcerpc_floor_get_lhs_data(&tower->floors[0], &abstract_syntax);
1298         if (!NT_STATUS_IS_OK(status)) {
1299                 talloc_free(b);
1300                 return status;
1301         }
1302
1303         status = dcerpc_binding_set_abstract_syntax(b, &abstract_syntax);
1304         if (!NT_STATUS_IS_OK(status)) {
1305                 talloc_free(b);
1306                 return status;
1307         }
1308
1309         /* Ignore floor 1, it contains the NDR version info */
1310
1311         /* Set endpoint */
1312         errno = 0;
1313         if (tower->num_floors >= 4) {
1314                 endpoint = dcerpc_floor_get_rhs_data(b, &tower->floors[3]);
1315         }
1316         if (errno != 0) {
1317                 int saved_errno = errno;
1318                 talloc_free(b);
1319                 return map_nt_error_from_unix_common(saved_errno);
1320         }
1321
1322         status = dcerpc_binding_set_string_option(b, "endpoint", endpoint);
1323         if (!NT_STATUS_IS_OK(status)) {
1324                 talloc_free(b);
1325                 return status;
1326         }
1327         TALLOC_FREE(endpoint);
1328
1329         /* Set network address */
1330         errno = 0;
1331         if (tower->num_floors >= 5) {
1332                 host = dcerpc_floor_get_rhs_data(b, &tower->floors[4]);
1333         }
1334         if (errno != 0) {
1335                 int saved_errno = errno;
1336                 talloc_free(b);
1337                 return map_nt_error_from_unix_common(saved_errno);
1338         }
1339
1340         status = dcerpc_binding_set_string_option(b, "host", host);
1341         if (!NT_STATUS_IS_OK(status)) {
1342                 talloc_free(b);
1343                 return status;
1344         }
1345         status = dcerpc_binding_set_string_option(b, "target_hostname", host);
1346         if (!NT_STATUS_IS_OK(status)) {
1347                 talloc_free(b);
1348                 return status;
1349         }
1350         TALLOC_FREE(host);
1351
1352         *b_out = b;
1353         return NT_STATUS_OK;
1354 }
1355
1356 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
1357                                                    const struct dcerpc_binding *b)
1358 {
1359         struct dcerpc_binding *n;
1360         uint32_t count;
1361
1362         n = talloc_zero(mem_ctx, struct dcerpc_binding);
1363         if (n == NULL) {
1364                 return NULL;
1365         }
1366
1367         n->transport = b->transport;
1368         n->object = b->object;
1369         n->flags = b->flags;
1370         n->assoc_group_id = b->assoc_group_id;
1371
1372         if (b->object_string != NULL) {
1373                 n->object_string = talloc_strdup(n, b->object_string);
1374                 if (n->object_string == NULL) {
1375                         talloc_free(n);
1376                         return NULL;
1377                 }
1378         }
1379         if (b->host != NULL) {
1380                 n->host = talloc_strdup(n, b->host);
1381                 if (n->host == NULL) {
1382                         talloc_free(n);
1383                         return NULL;
1384                 }
1385         }
1386
1387         if (b->target_hostname != NULL) {
1388                 n->target_hostname = talloc_strdup(n, b->target_hostname);
1389                 if (n->target_hostname == NULL) {
1390                         talloc_free(n);
1391                         return NULL;
1392                 }
1393         }
1394
1395         if (b->target_principal != NULL) {
1396                 n->target_principal = talloc_strdup(n, b->target_principal);
1397                 if (n->target_principal == NULL) {
1398                         talloc_free(n);
1399                         return NULL;
1400                 }
1401         }
1402
1403         if (b->endpoint != NULL) {
1404                 n->endpoint = talloc_strdup(n, b->endpoint);
1405                 if (n->endpoint == NULL) {
1406                         talloc_free(n);
1407                         return NULL;
1408                 }
1409         }
1410
1411         for (count = 0; b->options && b->options[count]; count++);
1412
1413         if (count > 0) {
1414                 uint32_t i;
1415
1416                 n->options = talloc_array(n, const char *, count + 1);
1417                 if (n->options == NULL) {
1418                         talloc_free(n);
1419                         return NULL;
1420                 }
1421
1422                 for (i = 0; i < count; i++) {
1423                         n->options[i] = talloc_strdup(n->options, b->options[i]);
1424                         if (n->options[i] == NULL) {
1425                                 talloc_free(n);
1426                                 return NULL;
1427                         }
1428                 }
1429                 n->options[count] = NULL;
1430         }
1431
1432         return n;
1433 }
1434
1435 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
1436                                              const struct dcerpc_binding *binding,
1437                                              struct epm_tower *tower)
1438 {
1439         const enum epm_protocol *protseq = NULL;
1440         size_t i, num_protocols;
1441         struct ndr_syntax_id abstract_syntax;
1442         NTSTATUS status;
1443
1444         /* Find transport */
1445         for (i=0;i<ARRAY_SIZE(transports);i++) {
1446                 if (transports[i].transport == binding->transport) {
1447                         protseq = transports[i].protseq;
1448                         num_protocols = transports[i].num_protocols;
1449                         break;
1450                 }
1451         }
1452
1453         if (i == ARRAY_SIZE(transports)) {
1454                 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
1455                 return NT_STATUS_UNSUCCESSFUL;
1456         }
1457
1458         tower->num_floors = 2 + num_protocols;
1459         tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
1460         if (tower->floors == NULL) {
1461                 return NT_STATUS_NO_MEMORY;
1462         }
1463
1464         /* Floor 0 */
1465         tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
1466
1467         abstract_syntax = dcerpc_binding_get_abstract_syntax(binding);
1468         tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1469                                                                    &abstract_syntax);
1470
1471         if (!dcerpc_floor_pack_rhs_if_version_data(
1472                     tower->floors, &abstract_syntax,
1473                     &tower->floors[0].rhs.uuid.unknown)) {
1474                 return NT_STATUS_NO_MEMORY;
1475         }
1476
1477         /* Floor 1 */
1478         tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
1479
1480         tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors, 
1481                                                                 &ndr_transfer_syntax_ndr);
1482
1483         tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
1484
1485         /* Floor 2 to num_protocols */
1486         for (i = 0; i < num_protocols; i++) {
1487                 tower->floors[2 + i].lhs.protocol = protseq[i];
1488                 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
1489                 ZERO_STRUCT(tower->floors[2 + i].rhs);
1490                 status = dcerpc_floor_set_rhs_data(tower->floors,
1491                                                    &tower->floors[2 + i],
1492                                                    NULL);
1493                 if (!NT_STATUS_IS_OK(status)) {
1494                         return status;
1495                 }
1496         }
1497
1498         /* The 4th floor contains the endpoint */
1499         if (num_protocols >= 2 && binding->endpoint) {
1500                 status = dcerpc_floor_set_rhs_data(tower->floors,
1501                                                    &tower->floors[3],
1502                                                    binding->endpoint);
1503                 if (!NT_STATUS_IS_OK(status)) {
1504                         return status;
1505                 }
1506         }
1507
1508         /* The 5th contains the network address */
1509         if (num_protocols >= 3 && binding->host) {
1510                 status = dcerpc_floor_set_rhs_data(tower->floors,
1511                                                    &tower->floors[4],
1512                                                    binding->host);
1513                 if (!NT_STATUS_IS_OK(status)) {
1514                         return status;
1515                 }
1516         }
1517
1518         return NT_STATUS_OK;
1519 }