s4:libcli/raw: s/SMBchkpth/SMBcheckpath
[Samba/gebeck_regimport.git] / source3 / librpc / rpc / dcerpc_ep.c
blob7341bc4b76afe64c66b7b12587b1579e4aafbe50
1 /*
2 * Endpoint Mapper Functions
3 * DCERPC local endpoint mapper client routines
4 * Copyright (c) 2010 Andreas Schneider.
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.
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.
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/>.
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"
29 #define EPM_MAX_ANNOTATION_SIZE 64
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)
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;
44 tmp_ctx = talloc_stackframe();
45 if (tmp_ctx == NULL) {
46 return NT_STATUS_NO_MEMORY;
49 ep_count = iface->endpoints->count;
51 bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
52 if (bvec == NULL) {
53 status = NT_STATUS_NO_MEMORY;
54 goto done;
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;
63 for (i = 0; i < ep_count; i++) {
64 struct dcerpc_binding *b;
66 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
67 if (b == NULL) {
68 status = NT_STATUS_NO_MEMORY;
69 goto done;
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;
78 b->object = iface->syntax_id;
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;
87 break;
88 case NCACN_IP_TCP:
89 if (port == 0) {
90 talloc_free(b);
91 continue;
94 b->endpoint = talloc_asprintf(b, "%u", port);
95 if (b->endpoint == NULL) {
96 status = NT_STATUS_NO_MEMORY;
97 goto done;
100 break;
101 case NCALRPC:
102 if (ncalrpc == NULL) {
103 talloc_free(b);
104 continue;
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;
115 break;
116 default:
117 talloc_free(b);
118 continue;
121 bvec->bindings[count] = *b;
122 count++;
125 bvec->count = count;
127 *pbvec = talloc_move(mem_ctx, &bvec);
129 status = NT_STATUS_OK;
130 done:
131 talloc_free(tmp_ctx);
133 return status;
136 static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
137 const struct ndr_interface_table *iface,
138 const struct dcerpc_binding_vector *bind_vec,
139 const struct GUID *object_guid,
140 const char *annotation,
141 uint32_t replace,
142 uint32_t unregister,
143 struct dcerpc_binding_handle **pbh)
145 struct rpc_pipe_client *cli = NULL;
146 struct dcerpc_binding_handle *h;
147 struct pipe_auth_data *auth;
148 const char *ncalrpc_sock;
149 const char *rpcsrv_type;
150 struct epm_entry_t *entries;
151 uint32_t num_ents, i;
152 TALLOC_CTX *tmp_ctx;
153 uint32_t result = EPMAPPER_STATUS_OK;
154 NTSTATUS status;
156 if (iface == NULL) {
157 return NT_STATUS_INVALID_PARAMETER;
160 if (bind_vec == NULL || bind_vec->count == 0) {
161 return NT_STATUS_INVALID_PARAMETER;
164 tmp_ctx = talloc_stackframe();
165 if (tmp_ctx == NULL) {
166 return NT_STATUS_NO_MEMORY;
169 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
170 "rpc_server", "epmapper",
171 "none");
173 if (strcasecmp_m(rpcsrv_type, "embedded") == 0) {
174 struct tsocket_address *local;
175 int rc;
177 rc = tsocket_address_inet_from_strings(tmp_ctx,
178 "ip",
179 "127.0.0.1",
181 &local);
182 if (rc < 0) {
183 return NT_STATUS_NO_MEMORY;
186 status = rpcint_binding_handle(tmp_ctx,
187 &ndr_table_epmapper,
188 local,
189 get_session_info_system(),
190 server_messaging_context(),
191 &h);
192 if (!NT_STATUS_IS_OK(status)) {
193 DEBUG(1, ("dcerpc_ep_register: Could not connect to "
194 "epmapper (%s)", nt_errstr(status)));
195 goto done;
197 } else if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
198 /* Connect to the endpoint mapper locally */
199 ncalrpc_sock = talloc_asprintf(tmp_ctx,
200 "%s/%s",
201 lp_ncalrpc_dir(),
202 "EPMAPPER");
203 if (ncalrpc_sock == NULL) {
204 status = NT_STATUS_NO_MEMORY;
205 goto done;
208 status = rpc_pipe_open_ncalrpc(tmp_ctx,
209 ncalrpc_sock,
210 &ndr_table_epmapper.syntax_id,
211 &cli);
212 if (!NT_STATUS_IS_OK(status)) {
213 goto done;
216 status = rpccli_ncalrpc_bind_data(cli, &auth);
217 if (!NT_STATUS_IS_OK(status)) {
218 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
219 goto done;
222 status = rpc_pipe_bind(cli, auth);
223 if (!NT_STATUS_IS_OK(status)) {
224 DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
225 goto done;
228 h = cli->binding_handle;
229 } else {
230 status = NT_STATUS_INVALID_PARAMETER;
231 goto done;
234 num_ents = bind_vec->count;
235 entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);
237 for (i = 0; i < num_ents; i++) {
238 struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
239 struct epm_twr_t *map_tower;
241 map_tower = talloc_zero(entries, struct epm_twr_t);
242 if (map_tower == NULL) {
243 status = NT_STATUS_NO_MEMORY;
244 goto done;
247 status = dcerpc_binding_build_tower(entries,
248 map_binding,
249 &map_tower->tower);
250 if (!NT_STATUS_IS_OK(status)) {
251 goto done;
254 entries[i].tower = map_tower;
255 if (annotation == NULL) {
256 entries[i].annotation = talloc_strdup(entries, "");
257 } else {
258 entries[i].annotation = talloc_strndup(entries,
259 annotation,
260 EPM_MAX_ANNOTATION_SIZE);
262 if (entries[i].annotation == NULL) {
263 status = NT_STATUS_NO_MEMORY;
264 goto done;
267 if (object_guid != NULL) {
268 entries[i].object = *object_guid;
269 } else {
270 entries[i].object = map_binding->object.uuid;
274 if (unregister) {
275 status = dcerpc_epm_Delete(h,
276 tmp_ctx,
277 num_ents,
278 entries,
279 &result);
280 } else {
281 status = dcerpc_epm_Insert(h,
282 tmp_ctx,
283 num_ents,
284 entries,
285 replace,
286 &result);
288 if (!NT_STATUS_IS_OK(status)) {
289 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
290 nt_errstr(status)));
291 goto done;
293 if (result != EPMAPPER_STATUS_OK) {
294 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
295 result));
296 status = NT_STATUS_UNSUCCESSFUL;
297 goto done;
300 if (pbh != NULL) {
301 *pbh = talloc_move(mem_ctx, &h);
302 talloc_steal(*pbh, cli);
305 done:
306 talloc_free(tmp_ctx);
308 return status;
311 NTSTATUS dcerpc_ep_register(TALLOC_CTX *mem_ctx,
312 const struct ndr_interface_table *iface,
313 const struct dcerpc_binding_vector *bind_vec,
314 const struct GUID *object_guid,
315 const char *annotation,
316 struct dcerpc_binding_handle **ph)
318 return ep_register(mem_ctx,
319 iface,
320 bind_vec,
321 object_guid,
322 annotation,
325 ph);
328 NTSTATUS dcerpc_ep_register_noreplace(TALLOC_CTX *mem_ctx,
329 const struct ndr_interface_table *iface,
330 const struct dcerpc_binding_vector *bind_vec,
331 const struct GUID *object_guid,
332 const char *annotation,
333 struct dcerpc_binding_handle **ph)
335 return ep_register(mem_ctx,
336 iface,
337 bind_vec,
338 object_guid,
339 annotation,
342 ph);
345 NTSTATUS dcerpc_ep_unregister(const struct ndr_interface_table *iface,
346 const struct dcerpc_binding_vector *bind_vec,
347 const struct GUID *object_guid)
349 return ep_register(NULL,
350 iface,
351 bind_vec,
352 object_guid,
353 NULL,
356 NULL);
359 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */