a fairly large commit!
[samba.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    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /*
26   this ndr_size_* stuff should really be auto-generated ....
27 */
28
29 static size_t ndr_size_epm_floor(struct epm_floor *fl)
30 {
31         size_t ret = 5;
32         if (fl->lhs.protocol == EPM_PROTOCOL_UUID) {
33                 ret += 18;
34         } else {
35                 ret += fl->lhs.info.lhs_data.length;
36         }
37         ret += fl->rhs.rhs_data.length;
38         return ret;
39 }
40
41 size_t ndr_size_epm_towers(struct epm_towers *towers)
42 {
43         size_t ret = 2;
44         int i;
45         for (i=0;i<towers->num_floors;i++) {
46                 ret += ndr_size_epm_floor(&towers->floors[i]);
47         }
48         return ret;
49 }
50
51 /*
52   work out what TCP port to use for a given interface on a given host
53 */
54 NTSTATUS dcerpc_epm_map_tcp_port(const char *server, 
55                                  const char *uuid, unsigned version,
56                                  uint32 *port)
57 {
58         struct dcerpc_pipe *p;
59         NTSTATUS status;
60         struct epm_Map r;
61         struct policy_handle handle;
62         struct GUID guid;
63         struct epm_twr_t twr, *twr_r;
64
65         if (strcasecmp(uuid, DCERPC_EPMAPPER_UUID) == 0 ||
66             strcasecmp(uuid, DCERPC_MGMT_UUID) == 0) {
67                 /* don't lookup epmapper via epmapper! */
68                 *port = EPMAPPER_PORT;
69                 return NT_STATUS_OK;
70         }
71
72         status = dcerpc_pipe_open_tcp(&p, server, EPMAPPER_PORT);
73         if (!NT_STATUS_IS_OK(status)) {
74                 return status;
75         }
76
77         /* we can use the pipes memory context here as we will have a short
78            lived connection */
79         status = dcerpc_bind_byuuid(p, p->mem_ctx, 
80                                     DCERPC_EPMAPPER_UUID,
81                                     DCERPC_EPMAPPER_VERSION);
82         if (!NT_STATUS_IS_OK(status)) {
83                 dcerpc_pipe_close(p);
84                 return status;
85         }
86
87         ZERO_STRUCT(handle);
88         ZERO_STRUCT(guid);
89
90         twr.towers.num_floors = 5;
91         twr.towers.floors = talloc(p->mem_ctx, sizeof(twr.towers.floors[0]) * 5);
92
93         /* what I'd like for christmas ... */
94
95         /* an RPC interface ... */
96         twr.towers.floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
97         GUID_from_string(uuid, &twr.towers.floors[0].lhs.info.uuid.uuid);
98         twr.towers.floors[0].lhs.info.uuid.version = version;
99         twr.towers.floors[0].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
100
101         /* encoded with NDR ... */
102         twr.towers.floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
103         GUID_from_string(NDR_GUID, &twr.towers.floors[1].lhs.info.uuid.uuid);
104         twr.towers.floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
105         twr.towers.floors[1].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
106
107         /* on an RPC connection ... */
108         twr.towers.floors[2].lhs.protocol = EPM_PROTOCOL_RPC_C;
109         twr.towers.floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
110         twr.towers.floors[2].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
111
112         /* on a TCP port ... */
113         twr.towers.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
114         twr.towers.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
115         twr.towers.floors[3].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
116
117         /* on an IP link ... */
118         twr.towers.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
119         twr.towers.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
120         twr.towers.floors[4].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 4);
121
122         /* with some nice pretty paper around it of course */
123         r.in.object = &guid;
124         r.in.map_tower = &twr;
125         r.in.entry_handle = &handle;
126         r.in.max_towers = 1;
127         r.out.entry_handle = &handle;
128
129         status = dcerpc_epm_Map(p, p->mem_ctx, &r);
130         if (!NT_STATUS_IS_OK(status)) {
131                 dcerpc_pipe_close(p);
132                 return status;
133         }
134         if (r.out.status != 0 || r.out.num_towers != 1) {
135                 dcerpc_pipe_close(p);
136                 return NT_STATUS_PORT_UNREACHABLE;
137         }
138
139         twr_r = r.out.towers[0].twr;
140         if (!twr_r) {
141                 dcerpc_pipe_close(p);
142                 return NT_STATUS_PORT_UNREACHABLE;
143         }
144
145         if (twr_r->towers.num_floors != 5 ||
146             twr_r->towers.floors[3].lhs.protocol != twr.towers.floors[3].lhs.protocol ||
147             twr_r->towers.floors[3].rhs.rhs_data.length != 2) {
148                 dcerpc_pipe_close(p);
149                 return NT_STATUS_PORT_UNREACHABLE;
150         }
151
152         *port = RSVAL(twr_r->towers.floors[3].rhs.rhs_data.data, 0);
153
154         dcerpc_pipe_close(p);
155
156         return NT_STATUS_OK;
157 }
158
159
160 /*
161   find the pipe name for a local IDL interface
162 */
163 const char *idl_pipe_name(const char *uuid, uint32 if_version)
164 {
165         int i;
166         for (i=0;dcerpc_pipes[i];i++) {
167                 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
168                     dcerpc_pipes[i]->if_version == if_version) {
169                         return dcerpc_pipes[i]->name;
170                 }
171         }
172         return "UNKNOWN";
173 }
174
175 /*
176   find the number of calls defined by local IDL
177 */
178 int idl_num_calls(const char *uuid, uint32 if_version)
179 {
180         int i;
181         for (i=0;dcerpc_pipes[i];i++) {
182                 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
183                     dcerpc_pipes[i]->if_version == if_version) {
184                         return dcerpc_pipes[i]->num_calls;
185                 }
186         }
187         return -1;
188 }
189
190
191 /*
192   find a dcerpc interface by name
193 */
194 const struct dcerpc_interface_table *idl_iface_by_name(const char *name)
195 {
196         int i;
197         for (i=0;dcerpc_pipes[i];i++) {
198                 if (strcasecmp(dcerpc_pipes[i]->name, name) == 0) {
199                         return dcerpc_pipes[i];
200                 }
201         }
202         return NULL;
203 }
204
205 /*
206   find a dcerpc interface by uuid
207 */
208 const struct dcerpc_interface_table *idl_iface_by_uuid(const char *uuid)
209 {
210         int i;
211         for (i=0;dcerpc_pipes[i];i++) {
212                 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0) {
213                         return dcerpc_pipes[i];
214                 }
215         }
216         return NULL;
217 }
218
219
220
221 /* 
222    push a dcerpc_packet into a blob, potentially with auth info
223 */
224 NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
225                           struct dcerpc_packet *pkt,
226                           struct dcerpc_auth *auth_info,
227                           unsigned flags)
228 {
229         NTSTATUS status;
230         struct ndr_push *ndr;
231
232         ndr = ndr_push_init_ctx(mem_ctx);
233         if (!ndr) {
234                 return NT_STATUS_NO_MEMORY;
235         }
236
237         if (flags & DCERPC_PUSH_BIGENDIAN) {
238                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
239         }
240
241         if (auth_info) {
242                 pkt->auth_length = auth_info->credentials.length;
243         } else {
244                 pkt->auth_length = 0;
245         }
246
247         status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
248         if (!NT_STATUS_IS_OK(status)) {
249                 return status;
250         }
251
252         if (auth_info) {
253                 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
254         }
255
256         *blob = ndr_push_blob(ndr);
257
258         /* fill in the frag length */
259         dcerpc_set_frag_length(blob, blob->length);
260
261         return NT_STATUS_OK;
262 }
263
264
265 /*
266   parse a binding string into a dcerpc_binding structure
267 */
268 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding *b)
269 {
270         char *part1, *part2, *part3;
271         char *p;
272         int i, j, comma_count;
273         const struct {
274                 const char *name;
275                 enum dcerpc_transport_t transport;
276         } transports[] = {
277                 {"ncacn_np",     NCACN_NP},
278                 {"ncacn_ip_tcp", NCACN_IP_TCP}
279         };
280         const struct {
281                 const char *name;
282                 uint32 flag;
283         } options[] = {
284                 {"sign", DCERPC_SIGN},
285                 {"seal", DCERPC_SEAL},
286                 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
287                 {"bigendian", DCERPC_PUSH_BIGENDIAN}
288         };
289
290         p = strchr(s, ':');
291         if (!p) {
292                 return NT_STATUS_INVALID_PARAMETER;
293         }
294         part1 = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
295         if (!part1) {
296                 return NT_STATUS_NO_MEMORY;
297         }
298         s = p+1;
299
300         p = strchr(s, ':');
301         if (!p) {
302                 p = strchr(s, '[');
303                 if (p) {
304                         part2 = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
305                         part3 = talloc_strdup(mem_ctx, p+1);
306                         if (part3[strlen(part3)-1] != ']') {
307                                 return NT_STATUS_INVALID_PARAMETER;
308                         }
309                         part3[strlen(part3)-1] = 0;
310                 } else {
311                         part2 = talloc_strdup(mem_ctx, s);
312                         part3 = NULL;
313                 }
314         } else {
315                 part2 = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
316                 part3 = talloc_strdup(mem_ctx, p+1);
317         }
318         if (!part2) {
319                 return NT_STATUS_NO_MEMORY;
320         }
321
322         for (i=0;i<ARRAY_SIZE(transports);i++) {
323                 if (strcasecmp(part1, transports[i].name) == 0) {
324                         b->transport = transports[i].transport;
325                         break;
326                 }
327         }
328         if (i==ARRAY_SIZE(transports)) {
329                 DEBUG(1,("Unknown dcerpc transport '%s'\n", part1));
330                 return NT_STATUS_INVALID_PARAMETER;
331         }
332
333         b->host = part2;
334         b->options = NULL;
335         b->flags = 0;
336
337         if (!part3) {
338                 return NT_STATUS_OK;
339         }
340
341         /* the [] brackets are optional */
342         if (*part3 == '[' && part3[strlen(part3)-1] == ']') {
343                 part3++;
344                 part3[strlen(part3)-1] = 0;
345         }
346
347         comma_count = count_chars(part3, ',');
348         b->options = talloc_array_p(mem_ctx, const char *, comma_count+2);
349         if (!b->options) {
350                 return NT_STATUS_NO_MEMORY;
351         }
352
353         for (i=0; (p = strchr(part3, ',')); i++) {
354                 b->options[i] = talloc_strndup(mem_ctx, part3, PTR_DIFF(p, part3));
355                 if (!b->options[i]) {
356                         return NT_STATUS_NO_MEMORY;
357                 }
358                 part3 = p+1;
359         }
360         b->options[i] = part3;
361         b->options[i+1] = NULL;
362
363         /* some options are pre-parsed for convenience */
364         for (i=0;b->options[i];i++) {
365                 for (j=0;j<ARRAY_SIZE(options);j++) {
366                         if (strcasecmp(options[j].name, b->options[i]) == 0) {
367                                 b->flags |= options[j].flag;
368                                 break;
369                         }
370                 }
371         }
372         
373         return NT_STATUS_OK;
374 }
375
376
377 /* open a rpc connection to a rpc pipe on SMP using the binding
378    structure to determine the endpoint and options */
379 static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p, 
380                                              struct dcerpc_binding *binding,
381                                              const char *pipe_uuid, 
382                                              uint32 pipe_version,
383                                              const char *domain,
384                                              const char *username,
385                                              const char *password)
386 {
387         NTSTATUS status;
388         BOOL retry;
389         struct cli_state *cli;
390         const char *pipe_name;
391         
392         if (!binding->options || !binding->options[0]) {
393                 const struct dcerpc_interface_table *table = idl_iface_by_uuid(pipe_uuid);
394                 if (!table) {
395                         DEBUG(0,("Unknown interface endpoint '%s'\n", pipe_uuid));
396                         return NT_STATUS_INVALID_PARAMETER;
397                 }
398                 pipe_name = table->endpoints->names[0];
399         } else {
400                 pipe_name = binding->options[0];
401         }
402
403         if (strncasecmp(pipe_name, "\\pipe\\", 6) == 0) {
404                 pipe_name += 6;
405         }
406         if (strncasecmp(pipe_name, "/pipe/", 6) == 0) {
407                 pipe_name += 6;
408         }
409             
410         status = cli_full_connection(&cli, lp_netbios_name(),
411                                      binding->host, NULL, 
412                                      "ipc$", "?????", 
413                                      username, username[0]?domain:"",
414                                      password, 0, &retry);
415         if (!NT_STATUS_IS_OK(status)) {
416                 return status;
417         }
418
419         status = dcerpc_pipe_open_smb(p, cli->tree, pipe_name);
420         if (!NT_STATUS_IS_OK(status)) {
421                 cli_tdis(cli);
422                 cli_shutdown(cli);
423                 return status;
424         }
425         
426         /* this ensures that the reference count is decremented so
427            a pipe close will really close the link */
428         cli_tree_close(cli->tree);
429         
430         (*p)->flags = binding->flags;
431
432         if (binding->flags & (DCERPC_SIGN | DCERPC_SEAL)) {
433                 status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
434         } else {    
435                 status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
436         }
437         if (!NT_STATUS_IS_OK(status)) {
438                 dcerpc_pipe_close(*p);
439                 return status;
440         }
441
442         return NT_STATUS_OK;
443 }
444
445
446 /* open a rpc connection to a rpc pipe on SMP using the binding
447    structure to determine the endpoint and options */
448 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **p, 
449                                                  struct dcerpc_binding *binding,
450                                                  const char *pipe_uuid, 
451                                                  uint32 pipe_version,
452                                                  const char *domain,
453                                                  const char *username,
454                                                  const char *password)
455 {
456         NTSTATUS status;
457         uint32 port = 0;
458
459         if (binding->options && binding->options[0]) {
460                 port = atoi(binding->options[0]);
461         }
462
463         if (port == 0) {
464                 status = dcerpc_epm_map_tcp_port(binding->host, 
465                                                  pipe_uuid, pipe_version,
466                                                  &port);
467                 if (!NT_STATUS_IS_OK(status)) {
468                         DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n", 
469                                  pipe_uuid, nt_errstr(status)));
470                         return status;
471                 }
472                 DEBUG(1,("Mapped to DCERPC/TCP port %u\n", port));
473         }
474
475         status = dcerpc_pipe_open_tcp(p, binding->host, port);
476         if (!NT_STATUS_IS_OK(status)) {
477                 DEBUG(0,("Failed to connect to %s:%d\n", binding->host, port));
478                 return status;
479         }
480
481         /* it doesn't seem to work to do a null NTLMSSP session without either sign
482            or seal, so force signing if we are doing ntlmssp */
483         if (username[0] && !(binding->flags & (DCERPC_SIGN|DCERPC_SEAL))) {
484                 binding->flags |= DCERPC_SIGN;
485         }
486
487         (*p)->flags = binding->flags;
488
489         if (!(binding->flags & (DCERPC_SIGN|DCERPC_SEAL)) && !username[0]) {
490                 status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
491         } else {
492                 status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version,
493                                                domain, username, password);
494         }
495
496         if (!NT_STATUS_IS_OK(status)) {
497                 dcerpc_pipe_close(*p);
498                 return status;
499         }
500  
501         return status;
502 }
503
504
505 /* open a rpc connection to a rpc pipe, using the specified 
506    binding structure to determine the endpoint and options */
507 NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **p, 
508                                struct dcerpc_binding *binding,
509                                const char *pipe_uuid, 
510                                uint32 pipe_version,
511                                const char *domain,
512                                const char *username,
513                                const char *password)
514 {
515         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
516
517         switch (binding->transport) {
518         case NCACN_NP:
519                 status = dcerpc_pipe_connect_ncacn_np(p, binding, pipe_uuid, pipe_version,
520                                                       domain, username, password);
521                 break;
522         case NCACN_IP_TCP:
523                 status = dcerpc_pipe_connect_ncacn_ip_tcp(p, binding, pipe_uuid, pipe_version,
524                                                           domain, username, password);
525                 break;
526         }
527
528         return status;
529 }
530
531
532 /* open a rpc connection to a rpc pipe, using the specified string
533    binding to determine the endpoint and options */
534 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **p, 
535                              const char *binding,
536                              const char *pipe_uuid, 
537                              uint32 pipe_version,
538                              const char *domain,
539                              const char *username,
540                              const char *password)
541 {
542         struct dcerpc_binding b;
543         NTSTATUS status;
544         TALLOC_CTX *mem_ctx;
545
546         mem_ctx = talloc_init("dcerpc_pipe_connect");
547         if (!mem_ctx) return NT_STATUS_NO_MEMORY;
548
549         status = dcerpc_parse_binding(mem_ctx, binding, &b);
550         if (!NT_STATUS_IS_OK(status)) {
551                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
552                 talloc_destroy(mem_ctx);
553                 return status;
554         }
555
556         status = dcerpc_pipe_connect_b(p, &b, pipe_uuid, pipe_version, domain, username, password);
557
558         talloc_destroy(mem_ctx);
559         return status;
560 }