s3-rpc_client: Move client pipe functions to own header.
[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
26 #define EPM_MAX_ANNOTATION_SIZE 64
27
28 NTSTATUS dcerpc_binding_vector_create(TALLOC_CTX *mem_ctx,
29                                       const struct ndr_interface_table *iface,
30                                       struct dcerpc_binding_vector **pbvec)
31 {
32         struct dcerpc_binding_vector *bvec;
33         uint32_t ep_count;
34         uint32_t count = 0;
35         uint32_t i;
36         NTSTATUS status;
37         TALLOC_CTX *tmp_ctx;
38
39         tmp_ctx = talloc_stackframe();
40         if (tmp_ctx == NULL) {
41                 return NT_STATUS_NO_MEMORY;
42         }
43
44         ep_count = iface->endpoints->count;
45
46         bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
47         if (bvec == NULL) {
48                 status = NT_STATUS_NO_MEMORY;
49                 goto done;
50         }
51
52         bvec->bindings = talloc_array(bvec, struct dcerpc_binding, ep_count);
53         if (bvec->bindings == NULL) {
54                 status = NT_STATUS_NO_MEMORY;
55                 goto done;
56         }
57
58         for (i = 0; i < ep_count; i++) {
59                 struct dcerpc_binding *b;
60
61                 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
62                 if (b == NULL) {
63                         status = NT_STATUS_NO_MEMORY;
64                         goto done;
65                 }
66
67                 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
68                 if (!NT_STATUS_IS_OK(status)) {
69                         status = NT_STATUS_UNSUCCESSFUL;
70                         goto done;
71                 }
72
73                 b->object = iface->syntax_id;
74
75                 switch (b->transport) {
76                         case NCACN_NP:
77                                 b->host = talloc_asprintf(b, "\\\\%s", global_myname());
78                                 if (b->host == NULL) {
79                                         status = NT_STATUS_NO_MEMORY;
80                                         goto done;
81                                 }
82                                 break;
83                         case NCACN_IP_TCP:
84                                 /* TODO */
85                         case NCALRPC:
86                                 /* TODO */
87                         default:
88                                 continue;
89                 }
90
91                 bvec->bindings[count] = *b;
92                 count++;
93         }
94
95         bvec->count = count;
96
97         *pbvec = talloc_move(mem_ctx, &bvec);
98
99         status = NT_STATUS_OK;
100 done:
101         talloc_free(tmp_ctx);
102
103         return status;
104 }
105
106 static NTSTATUS ep_register(const struct ndr_interface_table *iface,
107                             const struct dcerpc_binding_vector *bind_vec,
108                             const struct GUID *object_guid,
109                             const char *annotation,
110                             uint32_t replace,
111                             uint32_t unregister)
112 {
113         struct dcerpc_binding_handle *h = NULL;
114         static struct client_address client_id;
115         struct epm_entry_t *entries;
116         uint32_t num_ents, i;
117         TALLOC_CTX *tmp_ctx;
118         uint32_t result = EPMAPPER_STATUS_OK;
119         NTSTATUS status;
120
121         if (iface == NULL) {
122                 return NT_STATUS_INVALID_PARAMETER;
123         }
124
125         if (bind_vec == NULL || bind_vec->count == 0) {
126                 return NT_STATUS_INVALID_PARAMETER;
127         }
128
129         tmp_ctx = talloc_stackframe();
130         if (tmp_ctx == NULL) {
131                 return NT_STATUS_NO_MEMORY;
132         }
133
134 #if 0
135         /* NOTE: Samba3 doesn't have a ncalrpc server component yet. As soon as
136          * this is supported, we should talk to the endpoint mapper over the
137          * local transport.
138          */
139
140         /* Connect to the endpoint mapper locally */
141         ncalrpc_sock = talloc_asprintf(tmp_ctx,
142                                       "%s/%s",
143                                       get_dyn_NCALRPCDIR(),
144                                       "epmapper");
145         if (ncalrpc_sock == NULL) {
146                 status = NT_STATUS_NO_MEMORY;
147                 goto done;
148         }
149
150         status = rpc_pipe_open_ncalrpc(tmp_ctx,
151                                        ncalrpc_sock,
152                                        &ndr_table_epmapper.syntax_id,
153                                        &cli);
154         if (!NT_STATUS_IS_OK(status)) {
155                 goto done;
156         }
157 #endif
158
159         strlcpy(client_id.addr, "localhost", sizeof(client_id.addr));
160         client_id.name = "localhost";
161
162         status = rpcint_binding_handle(tmp_ctx,
163                                        &ndr_table_epmapper,
164                                        &client_id,
165                                        get_session_info_system(),
166                                        server_messaging_context(),
167                                        &h);
168         if (!NT_STATUS_IS_OK(status)) {
169                 DEBUG(0, ("dcerpc_ep_register: Could not connect to epmapper (%s)",
170                           nt_errstr(status)));
171                 goto done;
172         }
173
174         num_ents = bind_vec->count;
175         entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);
176
177         for (i = 0; i < num_ents; i++) {
178                 struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
179                 struct epm_twr_t *map_tower;
180
181                 map_tower = talloc_zero(entries, struct epm_twr_t);
182                 if (map_tower == NULL) {
183                         status = NT_STATUS_NO_MEMORY;
184                         goto done;
185                 }
186
187                 status = dcerpc_binding_build_tower(entries,
188                                                     map_binding,
189                                                     &map_tower->tower);
190                 if (!NT_STATUS_IS_OK(status)) {
191                         goto done;
192                 }
193
194                 entries[i].tower = map_tower;
195                 if (annotation == NULL) {
196                         entries[i].annotation = talloc_strdup(entries, "");
197                 } else {
198                         entries[i].annotation = talloc_strndup(entries,
199                                                                annotation,
200                                                                EPM_MAX_ANNOTATION_SIZE);
201                 }
202                 if (entries[i].annotation == NULL) {
203                         status = NT_STATUS_NO_MEMORY;
204                         goto done;
205                 }
206
207                 if (object_guid != NULL) {
208                         entries[i].object = *object_guid;
209                 } else {
210                         entries[i].object = map_binding->object.uuid;
211                 }
212         }
213
214         if (unregister) {
215                 status = dcerpc_epm_Delete(h,
216                                            tmp_ctx,
217                                            num_ents,
218                                            entries,
219                                            &result);
220         } else {
221                 status = dcerpc_epm_Insert(h,
222                                            tmp_ctx,
223                                            num_ents,
224                                            entries,
225                                            replace,
226                                            &result);
227         }
228         if (!NT_STATUS_IS_OK(status)) {
229                 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
230                           nt_errstr(status)));
231                 goto done;
232         }
233         if (result != EPMAPPER_STATUS_OK) {
234                 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
235                           result));
236                 status = NT_STATUS_UNSUCCESSFUL;
237                 goto done;
238         }
239
240 done:
241         talloc_free(tmp_ctx);
242
243         return status;
244 }
245
246 NTSTATUS dcerpc_ep_register(const struct ndr_interface_table *iface,
247                             const struct dcerpc_binding_vector *bind_vec,
248                             const struct GUID *object_guid,
249                             const char *annotation)
250 {
251         return ep_register(iface, bind_vec, object_guid, annotation, 1, 0);
252 }
253
254 NTSTATUS dcerpc_ep_register_noreplace(const struct ndr_interface_table *iface,
255                                       const struct dcerpc_binding_vector *bind_vec,
256                                       const struct GUID *object_guid,
257                                       const char *annotation)
258 {
259         return ep_register(iface, bind_vec, object_guid, annotation, 0, 0);
260 }
261
262 NTSTATUS dcerpc_ep_unregister(const struct ndr_interface_table *iface,
263                               const struct dcerpc_binding_vector *bind_vec,
264                               const struct GUID *object_guid)
265 {
266         return ep_register(iface, bind_vec, object_guid, NULL, 0, 1);
267 }
268
269 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */