14d475fbef44b011f69d10fada461a730661ae1d
[samba.git] / source3 / librpc / rpc / dcerpc_ep.c
1 /*
2  *  Endpoint Mapper Functions
3  *  DCERPC local endpoint mapper client routines
4  *  Copyright (c) 2010      Andreas Schneider.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "librpc/rpc/dcerpc.h"
22 #include "librpc/rpc/dcerpc_ep.h"
23 #include "../librpc/gen_ndr/ndr_epmapper_c.h"
24 #include "rpc_client/cli_pipe.h"
25 #include "auth.h"
26 #include "rpc_server/rpc_ncacn_np.h"
27 #include "../lib/tsocket/tsocket.h"
28
29 #define EPM_MAX_ANNOTATION_SIZE 64
30
31 NTSTATUS dcerpc_binding_vector_create(TALLOC_CTX *mem_ctx,
32                                       const struct ndr_interface_table *iface,
33                                       uint16_t port,
34                                       const char *ncalrpc,
35                                       struct dcerpc_binding_vector **pbvec)
36 {
37         struct dcerpc_binding_vector *bvec;
38         uint32_t ep_count;
39         uint32_t count = 0;
40         uint32_t i;
41         NTSTATUS status;
42         TALLOC_CTX *tmp_ctx;
43
44         tmp_ctx = talloc_stackframe();
45         if (tmp_ctx == NULL) {
46                 return NT_STATUS_NO_MEMORY;
47         }
48
49         ep_count = iface->endpoints->count;
50
51         bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
52         if (bvec == NULL) {
53                 status = NT_STATUS_NO_MEMORY;
54                 goto done;
55         }
56
57         bvec->bindings = talloc_zero_array(bvec, struct dcerpc_binding, ep_count);
58         if (bvec->bindings == NULL) {
59                 status = NT_STATUS_NO_MEMORY;
60                 goto done;
61         }
62
63         for (i = 0; i < ep_count; i++) {
64                 struct dcerpc_binding *b;
65
66                 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
67                 if (b == NULL) {
68                         status = NT_STATUS_NO_MEMORY;
69                         goto done;
70                 }
71
72                 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
73                 if (!NT_STATUS_IS_OK(status)) {
74                         status = NT_STATUS_UNSUCCESSFUL;
75                         goto done;
76                 }
77
78                 b->object = iface->syntax_id;
79
80                 switch (b->transport) {
81                         case NCACN_NP:
82                                 b->host = talloc_asprintf(b, "\\\\%s", lp_netbios_name());
83                                 if (b->host == NULL) {
84                                         status = NT_STATUS_NO_MEMORY;
85                                         goto done;
86                                 }
87                                 break;
88                         case NCACN_IP_TCP:
89                                 if (port == 0) {
90                                         talloc_free(b);
91                                         continue;
92                                 }
93
94                                 b->endpoint = talloc_asprintf(b, "%u", port);
95                                 if (b->endpoint == NULL) {
96                                         status = NT_STATUS_NO_MEMORY;
97                                         goto done;
98                                 }
99
100                                 break;
101                         case NCALRPC:
102                                 if (ncalrpc == NULL) {
103                                         talloc_free(b);
104                                         continue;
105                                 }
106
107                                 b->endpoint = talloc_asprintf(b,
108                                                               "%s/%s",
109                                                               lp_ncalrpc_dir(),
110                                                               ncalrpc);
111                                 if (b->endpoint == NULL) {
112                                         status = NT_STATUS_NO_MEMORY;
113                                         goto done;
114                                 }
115                                 break;
116                         default:
117                                 talloc_free(b);
118                                 continue;
119                 }
120
121                 bvec->bindings[count] = *b;
122                 count++;
123         }
124
125         bvec->count = count;
126
127         *pbvec = talloc_move(mem_ctx, &bvec);
128
129         status = NT_STATUS_OK;
130 done:
131         talloc_free(tmp_ctx);
132
133         return status;
134 }
135
136 static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
137                             struct messaging_context *msg_ctx,
138                             const struct ndr_interface_table *iface,
139                             const struct dcerpc_binding_vector *bind_vec,
140                             const struct GUID *object_guid,
141                             const char *annotation,
142                             uint32_t replace,
143                             uint32_t unregister,
144                             struct dcerpc_binding_handle **pbh)
145 {
146         struct rpc_pipe_client *cli = NULL;
147         struct dcerpc_binding_handle *h;
148         struct pipe_auth_data *auth;
149         const char *ncalrpc_sock;
150         const char *rpcsrv_type;
151         struct epm_entry_t *entries;
152         uint32_t num_ents, i;
153         TALLOC_CTX *tmp_ctx;
154         uint32_t result = EPMAPPER_STATUS_OK;
155         NTSTATUS status;
156
157         if (iface == NULL) {
158                 return NT_STATUS_INVALID_PARAMETER;
159         }
160
161         if (bind_vec == NULL || bind_vec->count == 0) {
162                 return NT_STATUS_INVALID_PARAMETER;
163         }
164
165         tmp_ctx = talloc_stackframe();
166         if (tmp_ctx == NULL) {
167                 return NT_STATUS_NO_MEMORY;
168         }
169
170         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
171                                            "rpc_server", "epmapper",
172                                            "none");
173
174         if (strcasecmp_m(rpcsrv_type, "embedded") == 0) {
175                 struct tsocket_address *local;
176                 int rc;
177
178                 rc = tsocket_address_inet_from_strings(tmp_ctx,
179                                                        "ip",
180                                                        "127.0.0.1",
181                                                        0,
182                                                        &local);
183                 if (rc < 0) {
184                         return NT_STATUS_NO_MEMORY;
185                 }
186
187                 status = rpcint_binding_handle(tmp_ctx,
188                                                &ndr_table_epmapper,
189                                                local,
190                                                get_session_info_system(),
191                                                msg_ctx,
192                                                &h);
193                 if (!NT_STATUS_IS_OK(status)) {
194                         DEBUG(1, ("dcerpc_ep_register: Could not connect to "
195                                   "epmapper (%s)", nt_errstr(status)));
196                         goto done;
197                 }
198         } else if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
199                 /* Connect to the endpoint mapper locally */
200                 ncalrpc_sock = talloc_asprintf(tmp_ctx,
201                                               "%s/%s",
202                                               lp_ncalrpc_dir(),
203                                               "EPMAPPER");
204                 if (ncalrpc_sock == NULL) {
205                         status = NT_STATUS_NO_MEMORY;
206                         goto done;
207                 }
208
209                 status = rpc_pipe_open_ncalrpc(tmp_ctx,
210                                                ncalrpc_sock,
211                                                &ndr_table_epmapper.syntax_id,
212                                                &cli);
213                 if (!NT_STATUS_IS_OK(status)) {
214                         goto done;
215                 }
216
217                 status = rpccli_ncalrpc_bind_data(cli, &auth);
218                 if (!NT_STATUS_IS_OK(status)) {
219                         DEBUG(0, ("Failed to initialize anonymous bind.\n"));
220                         goto done;
221                 }
222
223                 status = rpc_pipe_bind(cli, auth);
224                 if (!NT_STATUS_IS_OK(status)) {
225                         DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
226                         goto done;
227                 }
228
229                 h = cli->binding_handle;
230         } else {
231                 status = NT_STATUS_INVALID_PARAMETER;
232                 goto done;
233         }
234
235         num_ents = bind_vec->count;
236         entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);
237
238         for (i = 0; i < num_ents; i++) {
239                 struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
240                 struct epm_twr_t *map_tower;
241
242                 map_tower = talloc_zero(entries, struct epm_twr_t);
243                 if (map_tower == NULL) {
244                         status = NT_STATUS_NO_MEMORY;
245                         goto done;
246                 }
247
248                 status = dcerpc_binding_build_tower(entries,
249                                                     map_binding,
250                                                     &map_tower->tower);
251                 if (!NT_STATUS_IS_OK(status)) {
252                         goto done;
253                 }
254
255                 entries[i].tower = map_tower;
256                 if (annotation == NULL) {
257                         entries[i].annotation = talloc_strdup(entries, "");
258                 } else {
259                         entries[i].annotation = talloc_strndup(entries,
260                                                                annotation,
261                                                                EPM_MAX_ANNOTATION_SIZE);
262                 }
263                 if (entries[i].annotation == NULL) {
264                         status = NT_STATUS_NO_MEMORY;
265                         goto done;
266                 }
267
268                 if (object_guid != NULL) {
269                         entries[i].object = *object_guid;
270                 } else {
271                         entries[i].object = map_binding->object.uuid;
272                 }
273         }
274
275         if (unregister) {
276                 status = dcerpc_epm_Delete(h,
277                                            tmp_ctx,
278                                            num_ents,
279                                            entries,
280                                            &result);
281         } else {
282                 status = dcerpc_epm_Insert(h,
283                                            tmp_ctx,
284                                            num_ents,
285                                            entries,
286                                            replace,
287                                            &result);
288         }
289         if (!NT_STATUS_IS_OK(status)) {
290                 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
291                           nt_errstr(status)));
292                 goto done;
293         }
294         if (result != EPMAPPER_STATUS_OK) {
295                 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
296                           result));
297                 status = NT_STATUS_UNSUCCESSFUL;
298                 goto done;
299         }
300
301         if (pbh != NULL) {
302                 *pbh = talloc_move(mem_ctx, &h);
303                 talloc_steal(*pbh, cli);
304         }
305
306 done:
307         talloc_free(tmp_ctx);
308
309         return status;
310 }
311
312 NTSTATUS dcerpc_ep_register(TALLOC_CTX *mem_ctx,
313                             struct messaging_context *msg_ctx,
314                             const struct ndr_interface_table *iface,
315                             const struct dcerpc_binding_vector *bind_vec,
316                             const struct GUID *object_guid,
317                             const char *annotation,
318                             struct dcerpc_binding_handle **ph)
319 {
320         return ep_register(mem_ctx,
321                            msg_ctx,
322                            iface,
323                            bind_vec,
324                            object_guid,
325                            annotation,
326                            1,
327                            0,
328                            ph);
329 }
330
331 NTSTATUS dcerpc_ep_register_noreplace(TALLOC_CTX *mem_ctx,
332                                       struct messaging_context *msg_ctx,
333                                       const struct ndr_interface_table *iface,
334                                       const struct dcerpc_binding_vector *bind_vec,
335                                       const struct GUID *object_guid,
336                                       const char *annotation,
337                                       struct dcerpc_binding_handle **ph)
338 {
339         return ep_register(mem_ctx,
340                            msg_ctx,
341                            iface,
342                            bind_vec,
343                            object_guid,
344                            annotation,
345                            0,
346                            0,
347                            ph);
348 }
349
350 NTSTATUS dcerpc_ep_unregister(struct messaging_context *msg_ctx,
351                               const struct ndr_interface_table *iface,
352                               const struct dcerpc_binding_vector *bind_vec,
353                               const struct GUID *object_guid)
354 {
355         return ep_register(NULL,
356                            msg_ctx,
357                            iface,
358                            bind_vec,
359                            object_guid,
360                            NULL,
361                            0,
362                            1,
363                            NULL);
364 }
365
366 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */