s3-lib Replace StrCaseCmp() with strcasecmp_m()
[Samba.git] / source3 / librpc / rpc / dcerpc_ep.c
blob5088e7efcf1697b45f1ade264ba668f5addc2bc5
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"
28 #define EPM_MAX_ANNOTATION_SIZE 64
30 NTSTATUS dcerpc_binding_vector_create(TALLOC_CTX *mem_ctx,
31 const struct ndr_interface_table *iface,
32 uint16_t port,
33 const char *ncalrpc,
34 struct dcerpc_binding_vector **pbvec)
36 struct dcerpc_binding_vector *bvec;
37 uint32_t ep_count;
38 uint32_t count = 0;
39 uint32_t i;
40 NTSTATUS status;
41 TALLOC_CTX *tmp_ctx;
43 tmp_ctx = talloc_stackframe();
44 if (tmp_ctx == NULL) {
45 return NT_STATUS_NO_MEMORY;
48 ep_count = iface->endpoints->count;
50 bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
51 if (bvec == NULL) {
52 status = NT_STATUS_NO_MEMORY;
53 goto done;
56 bvec->bindings = talloc_zero_array(bvec, struct dcerpc_binding, ep_count);
57 if (bvec->bindings == NULL) {
58 status = NT_STATUS_NO_MEMORY;
59 goto done;
62 for (i = 0; i < ep_count; i++) {
63 struct dcerpc_binding *b;
65 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
66 if (b == NULL) {
67 status = NT_STATUS_NO_MEMORY;
68 goto done;
71 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
72 if (!NT_STATUS_IS_OK(status)) {
73 status = NT_STATUS_UNSUCCESSFUL;
74 goto done;
77 b->object = iface->syntax_id;
79 switch (b->transport) {
80 case NCACN_NP:
81 b->host = talloc_asprintf(b, "\\\\%s", global_myname());
82 if (b->host == NULL) {
83 status = NT_STATUS_NO_MEMORY;
84 goto done;
86 break;
87 case NCACN_IP_TCP:
88 if (port == 0) {
89 talloc_free(b);
90 continue;
93 b->endpoint = talloc_asprintf(b, "%u", port);
94 if (b->endpoint == NULL) {
95 status = NT_STATUS_NO_MEMORY;
96 goto done;
99 break;
100 case NCALRPC:
101 if (ncalrpc == NULL) {
102 talloc_free(b);
103 continue;
106 b->endpoint = talloc_asprintf(b,
107 "%s/%s",
108 lp_ncalrpc_dir(),
109 ncalrpc);
110 if (b->endpoint == NULL) {
111 status = NT_STATUS_NO_MEMORY;
112 goto done;
114 break;
115 default:
116 talloc_free(b);
117 continue;
120 bvec->bindings[count] = *b;
121 count++;
124 bvec->count = count;
126 *pbvec = talloc_move(mem_ctx, &bvec);
128 status = NT_STATUS_OK;
129 done:
130 talloc_free(tmp_ctx);
132 return status;
135 static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
136 const struct ndr_interface_table *iface,
137 const struct dcerpc_binding_vector *bind_vec,
138 const struct GUID *object_guid,
139 const char *annotation,
140 uint32_t replace,
141 uint32_t unregister,
142 struct dcerpc_binding_handle **pbh)
144 struct rpc_pipe_client *cli = NULL;
145 struct dcerpc_binding_handle *h;
146 struct pipe_auth_data *auth;
147 const char *ncalrpc_sock;
148 const char *rpcsrv_type;
149 struct epm_entry_t *entries;
150 uint32_t num_ents, i;
151 TALLOC_CTX *tmp_ctx;
152 uint32_t result = EPMAPPER_STATUS_OK;
153 NTSTATUS status;
155 if (iface == NULL) {
156 return NT_STATUS_INVALID_PARAMETER;
159 if (bind_vec == NULL || bind_vec->count == 0) {
160 return NT_STATUS_INVALID_PARAMETER;
163 tmp_ctx = talloc_stackframe();
164 if (tmp_ctx == NULL) {
165 return NT_STATUS_NO_MEMORY;
168 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
169 "rpc_server", "epmapper",
170 "none");
172 if (strcasecmp_m(rpcsrv_type, "embedded") == 0) {
173 static struct client_address client_id;
175 strlcpy(client_id.addr, "localhost", sizeof(client_id.addr));
176 client_id.name = "localhost";
178 status = rpcint_binding_handle(tmp_ctx,
179 &ndr_table_epmapper,
180 &client_id,
181 get_session_info_system(),
182 server_messaging_context(),
183 &h);
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(1, ("dcerpc_ep_register: Could not connect to "
186 "epmapper (%s)", nt_errstr(status)));
187 goto done;
189 } else if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
190 /* Connect to the endpoint mapper locally */
191 ncalrpc_sock = talloc_asprintf(tmp_ctx,
192 "%s/%s",
193 lp_ncalrpc_dir(),
194 "EPMAPPER");
195 if (ncalrpc_sock == NULL) {
196 status = NT_STATUS_NO_MEMORY;
197 goto done;
200 status = rpc_pipe_open_ncalrpc(tmp_ctx,
201 ncalrpc_sock,
202 &ndr_table_epmapper.syntax_id,
203 &cli);
204 if (!NT_STATUS_IS_OK(status)) {
205 goto done;
208 status = rpccli_ncalrpc_bind_data(cli, &auth);
209 if (!NT_STATUS_IS_OK(status)) {
210 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
211 goto done;
214 status = rpc_pipe_bind(cli, auth);
215 if (!NT_STATUS_IS_OK(status)) {
216 DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
217 goto done;
220 h = cli->binding_handle;
221 } else {
222 status = NT_STATUS_INVALID_PARAMETER;
223 goto done;
226 num_ents = bind_vec->count;
227 entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);
229 for (i = 0; i < num_ents; i++) {
230 struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
231 struct epm_twr_t *map_tower;
233 map_tower = talloc_zero(entries, struct epm_twr_t);
234 if (map_tower == NULL) {
235 status = NT_STATUS_NO_MEMORY;
236 goto done;
239 status = dcerpc_binding_build_tower(entries,
240 map_binding,
241 &map_tower->tower);
242 if (!NT_STATUS_IS_OK(status)) {
243 goto done;
246 entries[i].tower = map_tower;
247 if (annotation == NULL) {
248 entries[i].annotation = talloc_strdup(entries, "");
249 } else {
250 entries[i].annotation = talloc_strndup(entries,
251 annotation,
252 EPM_MAX_ANNOTATION_SIZE);
254 if (entries[i].annotation == NULL) {
255 status = NT_STATUS_NO_MEMORY;
256 goto done;
259 if (object_guid != NULL) {
260 entries[i].object = *object_guid;
261 } else {
262 entries[i].object = map_binding->object.uuid;
266 if (unregister) {
267 status = dcerpc_epm_Delete(h,
268 tmp_ctx,
269 num_ents,
270 entries,
271 &result);
272 } else {
273 status = dcerpc_epm_Insert(h,
274 tmp_ctx,
275 num_ents,
276 entries,
277 replace,
278 &result);
280 if (!NT_STATUS_IS_OK(status)) {
281 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
282 nt_errstr(status)));
283 goto done;
285 if (result != EPMAPPER_STATUS_OK) {
286 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
287 result));
288 status = NT_STATUS_UNSUCCESSFUL;
289 goto done;
292 if (pbh != NULL) {
293 *pbh = talloc_move(mem_ctx, &h);
294 talloc_steal(*pbh, cli);
297 done:
298 talloc_free(tmp_ctx);
300 return status;
303 NTSTATUS dcerpc_ep_register(TALLOC_CTX *mem_ctx,
304 const struct ndr_interface_table *iface,
305 const struct dcerpc_binding_vector *bind_vec,
306 const struct GUID *object_guid,
307 const char *annotation,
308 struct dcerpc_binding_handle **ph)
310 return ep_register(mem_ctx,
311 iface,
312 bind_vec,
313 object_guid,
314 annotation,
317 ph);
320 NTSTATUS dcerpc_ep_register_noreplace(TALLOC_CTX *mem_ctx,
321 const struct ndr_interface_table *iface,
322 const struct dcerpc_binding_vector *bind_vec,
323 const struct GUID *object_guid,
324 const char *annotation,
325 struct dcerpc_binding_handle **ph)
327 return ep_register(mem_ctx,
328 iface,
329 bind_vec,
330 object_guid,
331 annotation,
334 ph);
337 NTSTATUS dcerpc_ep_unregister(const struct ndr_interface_table *iface,
338 const struct dcerpc_binding_vector *bind_vec,
339 const struct GUID *object_guid)
341 return ep_register(NULL,
342 iface,
343 bind_vec,
344 object_guid,
345 NULL,
348 NULL);
351 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */