s3-auth: rpc_server needs auth.h
[mat/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
27 #define EPM_MAX_ANNOTATION_SIZE 64
28
29 NTSTATUS dcerpc_binding_vector_create(TALLOC_CTX *mem_ctx,
30                                       const struct ndr_interface_table *iface,
31                                       uint16_t port,
32                                       const char *ncalrpc,
33                                       struct dcerpc_binding_vector **pbvec)
34 {
35         struct dcerpc_binding_vector *bvec;
36         uint32_t ep_count;
37         uint32_t count = 0;
38         uint32_t i;
39         NTSTATUS status;
40         TALLOC_CTX *tmp_ctx;
41
42         tmp_ctx = talloc_stackframe();
43         if (tmp_ctx == NULL) {
44                 return NT_STATUS_NO_MEMORY;
45         }
46
47         ep_count = iface->endpoints->count;
48
49         bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
50         if (bvec == NULL) {
51                 status = NT_STATUS_NO_MEMORY;
52                 goto done;
53         }
54
55         bvec->bindings = talloc_zero_array(bvec, struct dcerpc_binding, ep_count);
56         if (bvec->bindings == NULL) {
57                 status = NT_STATUS_NO_MEMORY;
58                 goto done;
59         }
60
61         for (i = 0; i < ep_count; i++) {
62                 struct dcerpc_binding *b;
63
64                 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
65                 if (b == NULL) {
66                         status = NT_STATUS_NO_MEMORY;
67                         goto done;
68                 }
69
70                 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
71                 if (!NT_STATUS_IS_OK(status)) {
72                         status = NT_STATUS_UNSUCCESSFUL;
73                         goto done;
74                 }
75
76                 b->object = iface->syntax_id;
77
78                 switch (b->transport) {
79                         case NCACN_NP:
80                                 b->host = talloc_asprintf(b, "\\\\%s", global_myname());
81                                 if (b->host == NULL) {
82                                         status = NT_STATUS_NO_MEMORY;
83                                         goto done;
84                                 }
85                                 break;
86                         case NCACN_IP_TCP:
87                                 if (port == 0) {
88                                         talloc_free(b);
89                                         continue;
90                                 }
91
92                                 b->endpoint = talloc_asprintf(b, "%u", port);
93                                 if (b->endpoint == NULL) {
94                                         status = NT_STATUS_NO_MEMORY;
95                                         goto done;
96                                 }
97
98                                 break;
99                         case NCALRPC:
100                                 if (ncalrpc == NULL) {
101                                         talloc_free(b);
102                                         continue;
103                                 }
104
105                                 b->endpoint = talloc_asprintf(b,
106                                                               "%s/%s",
107                                                               lp_ncalrpc_dir(),
108                                                               ncalrpc);
109                                 if (b->endpoint == NULL) {
110                                         status = NT_STATUS_NO_MEMORY;
111                                         goto done;
112                                 }
113                                 break;
114                         default:
115                                 talloc_free(b);
116                                 continue;
117                 }
118
119                 bvec->bindings[count] = *b;
120                 count++;
121         }
122
123         bvec->count = count;
124
125         *pbvec = talloc_move(mem_ctx, &bvec);
126
127         status = NT_STATUS_OK;
128 done:
129         talloc_free(tmp_ctx);
130
131         return status;
132 }
133
134 static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
135                             const struct ndr_interface_table *iface,
136                             const struct dcerpc_binding_vector *bind_vec,
137                             const struct GUID *object_guid,
138                             const char *annotation,
139                             uint32_t replace,
140                             uint32_t unregister,
141                             struct dcerpc_binding_handle **pbh)
142 {
143         struct rpc_pipe_client *cli = NULL;
144         struct dcerpc_binding_handle *h;
145         struct pipe_auth_data *auth;
146         const char *ncalrpc_sock;
147         const char *rpcsrv_type;
148         struct epm_entry_t *entries;
149         uint32_t num_ents, i;
150         TALLOC_CTX *tmp_ctx;
151         uint32_t result = EPMAPPER_STATUS_OK;
152         NTSTATUS status;
153
154         if (iface == NULL) {
155                 return NT_STATUS_INVALID_PARAMETER;
156         }
157
158         if (bind_vec == NULL || bind_vec->count == 0) {
159                 return NT_STATUS_INVALID_PARAMETER;
160         }
161
162         tmp_ctx = talloc_stackframe();
163         if (tmp_ctx == NULL) {
164                 return NT_STATUS_NO_MEMORY;
165         }
166
167         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
168                                            "rpc_server", "epmapper",
169                                            "none");
170
171         if (StrCaseCmp(rpcsrv_type, "embedded") == 0) {
172                 static struct client_address client_id;
173
174                 strlcpy(client_id.addr, "localhost", sizeof(client_id.addr));
175                 client_id.name = "localhost";
176
177                 status = rpcint_binding_handle(tmp_ctx,
178                                                &ndr_table_epmapper,
179                                                &client_id,
180                                                get_session_info_system(),
181                                                server_messaging_context(),
182                                                &h);
183                 if (!NT_STATUS_IS_OK(status)) {
184                         DEBUG(1, ("dcerpc_ep_register: Could not connect to "
185                                   "epmapper (%s)", nt_errstr(status)));
186                         goto done;
187                 }
188         } else if (StrCaseCmp(rpcsrv_type, "daemon") == 0) {
189                 /* Connect to the endpoint mapper locally */
190                 ncalrpc_sock = talloc_asprintf(tmp_ctx,
191                                               "%s/%s",
192                                               lp_ncalrpc_dir(),
193                                               "EPMAPPER");
194                 if (ncalrpc_sock == NULL) {
195                         status = NT_STATUS_NO_MEMORY;
196                         goto done;
197                 }
198
199                 status = rpc_pipe_open_ncalrpc(tmp_ctx,
200                                                ncalrpc_sock,
201                                                &ndr_table_epmapper.syntax_id,
202                                                &cli);
203                 if (!NT_STATUS_IS_OK(status)) {
204                         goto done;
205                 }
206
207                 status = rpccli_ncalrpc_bind_data(cli, &auth);
208                 if (!NT_STATUS_IS_OK(status)) {
209                         DEBUG(0, ("Failed to initialize anonymous bind.\n"));
210                         goto done;
211                 }
212
213                 status = rpc_pipe_bind(cli, auth);
214                 if (!NT_STATUS_IS_OK(status)) {
215                         DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
216                         goto done;
217                 }
218
219                 h = cli->binding_handle;
220         } else {
221                 status = NT_STATUS_INVALID_PARAMETER;
222                 goto done;
223         }
224
225         num_ents = bind_vec->count;
226         entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);
227
228         for (i = 0; i < num_ents; i++) {
229                 struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
230                 struct epm_twr_t *map_tower;
231
232                 map_tower = talloc_zero(entries, struct epm_twr_t);
233                 if (map_tower == NULL) {
234                         status = NT_STATUS_NO_MEMORY;
235                         goto done;
236                 }
237
238                 status = dcerpc_binding_build_tower(entries,
239                                                     map_binding,
240                                                     &map_tower->tower);
241                 if (!NT_STATUS_IS_OK(status)) {
242                         goto done;
243                 }
244
245                 entries[i].tower = map_tower;
246                 if (annotation == NULL) {
247                         entries[i].annotation = talloc_strdup(entries, "");
248                 } else {
249                         entries[i].annotation = talloc_strndup(entries,
250                                                                annotation,
251                                                                EPM_MAX_ANNOTATION_SIZE);
252                 }
253                 if (entries[i].annotation == NULL) {
254                         status = NT_STATUS_NO_MEMORY;
255                         goto done;
256                 }
257
258                 if (object_guid != NULL) {
259                         entries[i].object = *object_guid;
260                 } else {
261                         entries[i].object = map_binding->object.uuid;
262                 }
263         }
264
265         if (unregister) {
266                 status = dcerpc_epm_Delete(h,
267                                            tmp_ctx,
268                                            num_ents,
269                                            entries,
270                                            &result);
271         } else {
272                 status = dcerpc_epm_Insert(h,
273                                            tmp_ctx,
274                                            num_ents,
275                                            entries,
276                                            replace,
277                                            &result);
278         }
279         if (!NT_STATUS_IS_OK(status)) {
280                 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
281                           nt_errstr(status)));
282                 goto done;
283         }
284         if (result != EPMAPPER_STATUS_OK) {
285                 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
286                           result));
287                 status = NT_STATUS_UNSUCCESSFUL;
288                 goto done;
289         }
290
291         if (pbh != NULL) {
292                 *pbh = talloc_move(mem_ctx, &h);
293                 talloc_steal(*pbh, cli);
294         }
295
296 done:
297         talloc_free(tmp_ctx);
298
299         return status;
300 }
301
302 NTSTATUS dcerpc_ep_register(TALLOC_CTX *mem_ctx,
303                             const struct ndr_interface_table *iface,
304                             const struct dcerpc_binding_vector *bind_vec,
305                             const struct GUID *object_guid,
306                             const char *annotation,
307                             struct dcerpc_binding_handle **ph)
308 {
309         return ep_register(mem_ctx,
310                            iface,
311                            bind_vec,
312                            object_guid,
313                            annotation,
314                            1,
315                            0,
316                            ph);
317 }
318
319 NTSTATUS dcerpc_ep_register_noreplace(TALLOC_CTX *mem_ctx,
320                                       const struct ndr_interface_table *iface,
321                                       const struct dcerpc_binding_vector *bind_vec,
322                                       const struct GUID *object_guid,
323                                       const char *annotation,
324                                       struct dcerpc_binding_handle **ph)
325 {
326         return ep_register(mem_ctx,
327                            iface,
328                            bind_vec,
329                            object_guid,
330                            annotation,
331                            0,
332                            0,
333                            ph);
334 }
335
336 NTSTATUS dcerpc_ep_unregister(const struct ndr_interface_table *iface,
337                               const struct dcerpc_binding_vector *bind_vec,
338                               const struct GUID *object_guid)
339 {
340         return ep_register(NULL,
341                            iface,
342                            bind_vec,
343                            object_guid,
344                            NULL,
345                            0,
346                            1,
347                            NULL);
348 }
349
350 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */