r26580: Include sentinel in build.h, in case the list is empty.
[bbaumbach/samba-autobuild/.git] / source4 / rpc_server / service_rpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    smbd-specific dcerpc server code
5
6    Copyright (C) Andrew Tridgell 2003-2005
7    Copyright (C) Stefan (metze) Metzmacher 2004-2005
8    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2004,2007
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_dcerpc.h"
26 #include "auth/auth.h"
27 #include "auth/gensec/gensec.h"
28 #include "lib/util/dlinklist.h"
29 #include "rpc_server/dcerpc_server.h"
30 #include "lib/events/events.h"
31 #include "smbd/service_task.h"
32 #include "smbd/service_stream.h"
33 #include "smbd/service.h"
34 #include "system/filesys.h"
35 #include "libcli/security/security.h"
36 #include "lib/socket/socket.h"
37 #include "lib/messaging/irpc.h"
38 #include "system/network.h"
39 #include "lib/socket/netif.h"
40 #include "build.h"
41 #include "param/param.h"
42
43 struct dcesrv_socket_context {
44         const struct dcesrv_endpoint *endpoint;
45         struct dcesrv_context *dcesrv_ctx;
46 };
47
48 /*
49   write_fn callback for dcesrv_output()
50 */
51 static NTSTATUS dcerpc_write_fn(void *private_data, DATA_BLOB *out, size_t *nwritten)
52 {
53         NTSTATUS status;
54         struct socket_context *sock = talloc_get_type(private_data, struct socket_context);
55         size_t sendlen;
56
57         status = socket_send(sock, out, &sendlen);
58         NT_STATUS_IS_ERR_RETURN(status);
59
60         *nwritten = sendlen;
61         return status;
62 }
63
64 static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
65 {
66         struct stream_connection *srv_conn;
67         srv_conn = talloc_get_type(dce_conn->transport.private_data,
68                                    struct stream_connection);
69
70         stream_terminate_connection(srv_conn, reason);
71 }
72
73 static void dcesrv_sock_report_output_data(struct dcesrv_connection *dcesrv_conn)
74 {
75         struct stream_connection *srv_conn;
76         srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
77                                    struct stream_connection);
78
79         if (srv_conn && srv_conn->event.fde) {
80                 EVENT_FD_WRITEABLE(srv_conn->event.fde);
81         }
82 }
83
84 static struct socket_address *dcesrv_sock_get_my_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx)
85 {
86         struct stream_connection *srv_conn;
87         srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
88                                    struct stream_connection);
89
90         return socket_get_my_addr(srv_conn->socket, mem_ctx);
91 }
92
93 static struct socket_address *dcesrv_sock_get_peer_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx)
94 {
95         struct stream_connection *srv_conn;
96         srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
97                                    struct stream_connection);
98
99         return socket_get_peer_addr(srv_conn->socket, mem_ctx);
100 }
101
102 static void dcesrv_sock_accept(struct stream_connection *srv_conn)
103 {
104         NTSTATUS status;
105         struct dcesrv_socket_context *dcesrv_sock = 
106                 talloc_get_type(srv_conn->private, struct dcesrv_socket_context);
107         struct dcesrv_connection *dcesrv_conn = NULL;
108         struct auth_session_info *session_info = NULL;
109
110         status = auth_anonymous_session_info(srv_conn, dcesrv_sock->dcesrv_ctx->lp_ctx, &session_info);
111         if (!NT_STATUS_IS_OK(status)) {
112                 DEBUG(0,("dcesrv_sock_accept: auth_anonymous_session_info failed: %s\n", 
113                         nt_errstr(status)));
114                 stream_terminate_connection(srv_conn, nt_errstr(status));
115                 return;
116         }
117
118         status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
119                                          srv_conn,
120                                          dcesrv_sock->endpoint,
121                                          session_info,
122                                          srv_conn->event.ctx,
123                                          srv_conn->msg_ctx,
124                                          srv_conn->server_id,
125                                          DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
126                                          &dcesrv_conn);
127         if (!NT_STATUS_IS_OK(status)) {
128                 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n", 
129                         nt_errstr(status)));
130                 stream_terminate_connection(srv_conn, nt_errstr(status));
131                 return;
132         }
133
134         dcesrv_conn->transport.private_data             = srv_conn;
135         dcesrv_conn->transport.report_output_data       = dcesrv_sock_report_output_data;
136         dcesrv_conn->transport.get_my_addr              = dcesrv_sock_get_my_addr;
137         dcesrv_conn->transport.get_peer_addr            = dcesrv_sock_get_peer_addr;
138
139         srv_conn->private = dcesrv_conn;
140
141         irpc_add_name(srv_conn->msg_ctx, "rpc_server");
142
143         return; 
144 }
145
146 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
147 {
148         NTSTATUS status;
149         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
150         DATA_BLOB tmp_blob;
151         size_t nread;
152
153         if (dce_conn->processing) {
154                 EVENT_FD_NOT_READABLE(conn->event.fde);
155                 return;
156         }
157
158         tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000);
159         if (tmp_blob.data == NULL) {
160                 dcesrv_terminate_connection(dce_conn, "out of memory");
161                 return;
162         }
163
164         status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread);
165         if (NT_STATUS_IS_ERR(status)) {
166                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
167                 return;
168         }
169         if (nread == 0) {
170                 talloc_free(tmp_blob.data);
171                 return;
172         }
173
174         tmp_blob.length = nread;
175
176         dce_conn->processing = true;
177         status = dcesrv_input(dce_conn, &tmp_blob);
178         dce_conn->processing = false;
179         talloc_free(tmp_blob.data);
180
181         EVENT_FD_READABLE(conn->event.fde);
182
183         if (!NT_STATUS_IS_OK(status)) {
184                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
185                 return;
186         }
187
188         if (dce_conn->call_list && dce_conn->call_list->replies) {
189                 EVENT_FD_WRITEABLE(conn->event.fde);
190         }
191 }
192
193 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
194 {
195         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
196         NTSTATUS status;
197
198         status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn);
199         if (NT_STATUS_IS_ERR(status)) {
200                 dcesrv_terminate_connection(dce_conn, "eof on socket");
201                 return;
202         }
203
204         if (!dce_conn->call_list || !dce_conn->call_list->replies) {
205                 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
206         }
207 }
208
209
210 static const struct stream_server_ops dcesrv_stream_ops = {
211         .name                   = "rpc",
212         .accept_connection      = dcesrv_sock_accept,
213         .recv_handler           = dcesrv_sock_recv,
214         .send_handler           = dcesrv_sock_send,
215 };
216
217
218
219 static NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, 
220                                    struct loadparm_context *lp_ctx,
221                                    struct dcesrv_endpoint *e,
222                             struct event_context *event_ctx, const struct model_ops *model_ops)
223 {
224         struct dcesrv_socket_context *dcesrv_sock;
225         uint16_t port = 1;
226         NTSTATUS status;
227
228         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
229         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
230
231         /* remember the endpoint of this socket */
232         dcesrv_sock->endpoint           = e;
233         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
234
235         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
236                                      "unix", e->ep_description->endpoint, &port, 
237                                      lp_socket_options(lp_ctx), 
238                                      dcesrv_sock);
239         if (!NT_STATUS_IS_OK(status)) {
240                 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
241                          e->ep_description->endpoint, nt_errstr(status)));
242         }
243
244         return status;
245 }
246
247 static NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, 
248                                       struct loadparm_context *lp_ctx,
249                                       struct dcesrv_endpoint *e,
250                                       struct event_context *event_ctx, const struct model_ops *model_ops)
251 {
252         struct dcesrv_socket_context *dcesrv_sock;
253         uint16_t port = 1;
254         char *full_path;
255         NTSTATUS status;
256
257         if (!e->ep_description->endpoint) {
258                 /* No identifier specified: use DEFAULT. 
259                  * DO NOT hardcode this value anywhere else. Rather, specify 
260                  * no endpoint and let the epmapper worry about it. */
261                 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
262         }
263
264         full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(lp_ctx), 
265                                     e->ep_description->endpoint);
266
267         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
268         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
269
270         /* remember the endpoint of this socket */
271         dcesrv_sock->endpoint           = e;
272         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
273
274         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
275                                      "unix", full_path, &port, 
276                                      lp_socket_options(lp_ctx), 
277                                      dcesrv_sock);
278         if (!NT_STATUS_IS_OK(status)) {
279                 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
280                          e->ep_description->endpoint, full_path, nt_errstr(status)));
281         }
282         return status;
283 }
284
285
286 /*
287   add a socket address to the list of events, one event per dcerpc endpoint
288 */
289 static NTSTATUS add_socket_rpc_pipe_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
290                                          struct event_context *event_ctx, const struct model_ops *model_ops)
291 {
292         struct dcesrv_socket_context *dcesrv_sock;
293         NTSTATUS status;
294                         
295         if (e->ep_description->endpoint == NULL) {
296                 DEBUG(0, ("Endpoint mandatory for named pipes\n"));
297                 return NT_STATUS_INVALID_PARAMETER;
298         }
299
300         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
301         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
302
303         /* remember the endpoint of this socket */
304         dcesrv_sock->endpoint           = e;
305         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
306
307         status = NT_STATUS_OK;
308 #if 0
309
310         status = stream_setup_smb_pipe(event_ctx, model_ops, &dcesrv_stream_ops, 
311                                      e->ep_description->endpoint, dcesrv_sock);
312         if (!NT_STATUS_IS_OK(status)) {
313                 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n", 
314                          e->ep_description->endpoint, nt_errstr(status)));
315         }
316 #endif
317         return status;
318 }
319
320 static NTSTATUS dcesrv_add_ep_np(struct dcesrv_context *dce_ctx, 
321                                  struct loadparm_context *lp_ctx,
322                                  struct dcesrv_endpoint *e,
323                                  struct event_context *event_ctx, const struct model_ops *model_ops)
324 {
325         NTSTATUS status;
326
327         status = add_socket_rpc_pipe_iface(dce_ctx, e, event_ctx, model_ops);
328         NT_STATUS_NOT_OK_RETURN(status);
329
330         return status;
331 }
332
333 /*
334   add a socket address to the list of events, one event per dcerpc endpoint
335 */
336 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
337                                          struct event_context *event_ctx, const struct model_ops *model_ops,
338                                          const char *address)
339 {
340         struct dcesrv_socket_context *dcesrv_sock;
341         uint16_t port = 0;
342         NTSTATUS status;
343                         
344         if (e->ep_description->endpoint) {
345                 port = atoi(e->ep_description->endpoint);
346         }
347
348         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
349         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
350
351         /* remember the endpoint of this socket */
352         dcesrv_sock->endpoint           = e;
353         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
354
355         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
356                                      "ipv4", address, &port, 
357                                      lp_socket_options(dce_ctx->lp_ctx), 
358                                      dcesrv_sock);
359         if (!NT_STATUS_IS_OK(status)) {
360                 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n", 
361                          address, port, nt_errstr(status)));
362         }
363
364         if (e->ep_description->endpoint == NULL) {
365                 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
366         }
367
368         return status;
369 }
370
371 static NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, 
372                                   struct loadparm_context *lp_ctx,
373                                   struct dcesrv_endpoint *e,
374                                   struct event_context *event_ctx, const struct model_ops *model_ops)
375 {
376         NTSTATUS status;
377
378         /* Add TCP/IP sockets */
379         if (lp_interfaces(lp_ctx) && lp_bind_interfaces_only(lp_ctx)) {
380                 int num_interfaces;
381                 int i;
382                 struct interface *ifaces;
383
384                 load_interfaces(dce_ctx, lp_interfaces(lp_ctx), &ifaces);
385
386                 num_interfaces = iface_count(ifaces);
387                 for(i = 0; i < num_interfaces; i++) {
388                         const char *address = iface_n_ip(ifaces, i);
389                         status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
390                         NT_STATUS_NOT_OK_RETURN(status);
391                 }
392         } else {
393                 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, 
394                                                   lp_socket_address(lp_ctx));
395                 NT_STATUS_NOT_OK_RETURN(status);
396         }
397
398         return NT_STATUS_OK;
399 }
400
401
402 static NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx, 
403                               struct loadparm_context *lp_ctx,
404                               struct dcesrv_endpoint *e,
405                           struct event_context *event_ctx, const struct model_ops *model_ops)
406 {
407         switch (e->ep_description->transport) {
408         case NCACN_UNIX_STREAM:
409                 return dcesrv_add_ep_unix(dce_ctx, lp_ctx, e, event_ctx, model_ops);
410
411         case NCALRPC:
412                 return dcesrv_add_ep_ncalrpc(dce_ctx, lp_ctx, e, event_ctx, model_ops);
413
414         case NCACN_IP_TCP:
415                 return dcesrv_add_ep_tcp(dce_ctx, lp_ctx, e, event_ctx, model_ops);
416
417         case NCACN_NP:
418                 return dcesrv_add_ep_np(dce_ctx, lp_ctx, e, event_ctx, model_ops);
419
420         default:
421                 return NT_STATUS_NOT_SUPPORTED;
422         }
423 }
424
425 /*
426   open the dcerpc server sockets
427 */
428 static void dcesrv_task_init(struct task_server *task)
429 {
430         NTSTATUS status;
431         struct dcesrv_context *dce_ctx;
432         struct dcesrv_endpoint *e;
433
434         task_server_set_title(task, "task[dcesrv]");
435
436         status = dcesrv_init_context(task->event_ctx,
437                                      task->lp_ctx,
438                                      lp_dcerpc_endpoint_servers(task->lp_ctx),
439                                      &dce_ctx);
440         if (!NT_STATUS_IS_OK(status)) goto failed;
441
442         /* Make sure the directory for NCALRPC exists */
443         if (!directory_exist(lp_ncalrpc_dir(task->lp_ctx))) {
444                 mkdir(lp_ncalrpc_dir(task->lp_ctx), 0755);
445         }
446
447         for (e=dce_ctx->endpoint_list;e;e=e->next) {
448                 status = dcesrv_add_ep(dce_ctx, task->lp_ctx, e, task->event_ctx, task->model_ops);
449                 if (!NT_STATUS_IS_OK(status)) goto failed;
450         }
451
452         return;
453 failed:
454         task_server_terminate(task, "Failed to startup dcerpc server task");    
455 }
456
457 /*
458   called on startup of the smb server service It's job is to start
459   listening on all configured sockets
460 */
461 static NTSTATUS dcesrv_init(struct event_context *event_context, 
462                             struct loadparm_context *lp_ctx,
463                             const struct model_ops *model_ops)
464 {       
465         return task_server_startup(event_context, model_ops, dcesrv_task_init);
466 }
467
468 NTSTATUS server_service_rpc_init(void)
469 {
470         init_module_fn static_init[] = { STATIC_dcerpc_server_MODULES };
471         init_module_fn *shared_init = load_samba_modules(NULL, global_loadparm, "dcerpc_server");
472
473         run_init_functions(static_init);
474         run_init_functions(shared_init);
475
476         talloc_free(shared_init);
477         
478         return register_server_service("rpc", dcesrv_init);
479 }