r14486: remove the need of a stream_connection on a dcesrv_connection,
[kai/samba.git] / source4 / rpc_server / dcerpc_sock.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    server side dcerpc using various kinds of sockets (tcp, unix domain)
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Stefan (metze) Metzmacher 2004-2005  
8    Copyright (C) Jelmer Vernooij 2004
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 2 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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "lib/socket/socket.h"
27 #include "lib/events/events.h"
28 #include "rpc_server/dcerpc_server.h"
29 #include "smbd/service_stream.h"
30 #include "smbd/service.h"
31 #include "lib/messaging/irpc.h"
32 #include "system/network.h"
33 #include "netif/netif.h"
34
35 struct dcesrv_socket_context {
36         const struct dcesrv_endpoint *endpoint;
37         struct dcesrv_context *dcesrv_ctx;
38 };
39
40 /*
41   write_fn callback for dcesrv_output()
42 */
43 static NTSTATUS dcerpc_write_fn(void *private_data, DATA_BLOB *out, size_t *nwritten)
44 {
45         NTSTATUS status;
46         struct socket_context *sock = talloc_get_type(private_data, struct socket_context);
47         size_t sendlen;
48
49         status = socket_send(sock, out, &sendlen, 0);
50         NT_STATUS_IS_ERR_RETURN(status);
51
52         *nwritten = sendlen;
53         return status;
54 }
55
56 static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
57 {
58         struct stream_connection *srv_conn;
59         srv_conn = talloc_get_type(dce_conn->transport.private_data,
60                                    struct stream_connection);
61
62         stream_terminate_connection(srv_conn, reason);
63 }
64
65 static void dcesrv_sock_report_output_data(struct dcesrv_connection *dcesrv_conn)
66 {
67         struct stream_connection *srv_conn;
68         srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
69                                    struct stream_connection);
70
71         if (srv_conn && srv_conn->event.fde) {
72                 EVENT_FD_WRITEABLE(srv_conn->event.fde);
73         }
74 }
75
76 static struct socket_address *dcesrv_sock_get_my_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx)
77 {
78         struct stream_connection *srv_conn;
79         srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
80                                    struct stream_connection);
81
82         return socket_get_my_addr(srv_conn->socket, mem_ctx);
83 }
84
85 static struct socket_address *dcesrv_sock_get_peer_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx)
86 {
87         struct stream_connection *srv_conn;
88         srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
89                                    struct stream_connection);
90
91         return socket_get_peer_addr(srv_conn->socket, mem_ctx);
92 }
93
94 static void dcesrv_sock_accept(struct stream_connection *srv_conn)
95 {
96         NTSTATUS status;
97         struct dcesrv_socket_context *dcesrv_sock = 
98                 talloc_get_type(srv_conn->private, struct dcesrv_socket_context);
99         struct dcesrv_connection *dcesrv_conn = NULL;
100
101         status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
102                                          srv_conn,
103                                          dcesrv_sock->endpoint,
104                                          srv_conn->event.ctx,
105                                          DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
106                                          &dcesrv_conn);
107         if (!NT_STATUS_IS_OK(status)) {
108                 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n", 
109                         nt_errstr(status)));
110                 stream_terminate_connection(srv_conn, nt_errstr(status));
111                 return;
112         }
113
114         dcesrv_conn->transport.private_data             = srv_conn;
115         dcesrv_conn->transport.report_output_data       = dcesrv_sock_report_output_data;
116         dcesrv_conn->transport.get_my_addr              = dcesrv_sock_get_my_addr;
117         dcesrv_conn->transport.get_peer_addr            = dcesrv_sock_get_peer_addr;
118
119         srv_conn->private = dcesrv_conn;
120
121         irpc_add_name(srv_conn->msg_ctx, "rpc_server");
122
123         return; 
124 }
125
126 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
127 {
128         NTSTATUS status;
129         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
130         DATA_BLOB tmp_blob;
131         size_t nread;
132
133         if (dce_conn->processing) {
134                 EVENT_FD_NOT_READABLE(conn->event.fde);
135                 return;
136         }
137
138         tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000);
139         if (tmp_blob.data == NULL) {
140                 dcesrv_terminate_connection(dce_conn, "out of memory");
141                 return;
142         }
143
144         status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread, 0);
145         if (NT_STATUS_IS_ERR(status)) {
146                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
147                 return;
148         }
149         if (nread == 0) {
150                 talloc_free(tmp_blob.data);
151                 return;
152         }
153
154         tmp_blob.length = nread;
155
156         dce_conn->processing = True;
157         status = dcesrv_input(dce_conn, &tmp_blob);
158         dce_conn->processing = False;
159         talloc_free(tmp_blob.data);
160
161         EVENT_FD_READABLE(conn->event.fde);
162
163         if (!NT_STATUS_IS_OK(status)) {
164                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
165                 return;
166         }
167
168         if (dce_conn->call_list && dce_conn->call_list->replies) {
169                 EVENT_FD_WRITEABLE(conn->event.fde);
170         }
171 }
172
173 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
174 {
175         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
176         NTSTATUS status;
177
178         status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn);
179         if (NT_STATUS_IS_ERR(status)) {
180                 dcesrv_terminate_connection(dce_conn, "eof on socket");
181                 return;
182         }
183
184         if (!dce_conn->call_list || !dce_conn->call_list->replies) {
185                 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
186         }
187 }
188
189
190 static const struct stream_server_ops dcesrv_stream_ops = {
191         .name                   = "rpc",
192         .accept_connection      = dcesrv_sock_accept,
193         .recv_handler           = dcesrv_sock_recv,
194         .send_handler           = dcesrv_sock_send,
195 };
196
197
198
199 NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
200                                     struct event_context *event_ctx, const struct model_ops *model_ops)
201 {
202         struct dcesrv_socket_context *dcesrv_sock;
203         uint16_t port = 1;
204         NTSTATUS status;
205
206         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
207         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
208
209         /* remember the endpoint of this socket */
210         dcesrv_sock->endpoint           = e;
211         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
212
213         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
214                                      "unix", e->ep_description->endpoint, &port, 
215                                      dcesrv_sock);
216         if (!NT_STATUS_IS_OK(status)) {
217                 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
218                          e->ep_description->endpoint, nt_errstr(status)));
219         }
220
221         return status;
222 }
223
224 NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
225                                        struct event_context *event_ctx, const struct model_ops *model_ops)
226 {
227         struct dcesrv_socket_context *dcesrv_sock;
228         uint16_t port = 1;
229         char *full_path;
230         NTSTATUS status;
231
232         if (!e->ep_description->endpoint) {
233                 /* No identifier specified: use DEFAULT. 
234                  * DO NOT hardcode this value anywhere else. Rather, specify 
235                  * no endpoint and let the epmapper worry about it. */
236                 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
237         }
238
239         full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(), e->ep_description->endpoint);
240
241         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
242         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
243
244         /* remember the endpoint of this socket */
245         dcesrv_sock->endpoint           = e;
246         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
247
248         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
249                                      "unix", full_path, &port, dcesrv_sock);
250         if (!NT_STATUS_IS_OK(status)) {
251                 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
252                          e->ep_description->endpoint, full_path, nt_errstr(status)));
253         }
254         return status;
255 }
256
257 /*
258   add a socket address to the list of events, one event per dcerpc endpoint
259 */
260 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
261                                          struct event_context *event_ctx, const struct model_ops *model_ops,
262                                          const char *address)
263 {
264         struct dcesrv_socket_context *dcesrv_sock;
265         uint16_t port = 0;
266         NTSTATUS status;
267                         
268         if (e->ep_description->endpoint) {
269                 port = atoi(e->ep_description->endpoint);
270         }
271
272         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
273         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
274
275         /* remember the endpoint of this socket */
276         dcesrv_sock->endpoint           = e;
277         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
278
279         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
280                                      "ipv4", address, &port, dcesrv_sock);
281         if (!NT_STATUS_IS_OK(status)) {
282                 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n", 
283                          address, port, nt_errstr(status)));
284         }
285
286         if (e->ep_description->endpoint == NULL) {
287                 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
288         }
289
290         return status;
291 }
292
293 NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
294                                    struct event_context *event_ctx, const struct model_ops *model_ops)
295 {
296         NTSTATUS status;
297
298         /* Add TCP/IP sockets */
299         if (lp_interfaces() && lp_bind_interfaces_only()) {
300                 int num_interfaces = iface_count();
301                 int i;
302                 for(i = 0; i < num_interfaces; i++) {
303                         const char *address = iface_n_ip(i);
304                         status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
305                         NT_STATUS_NOT_OK_RETURN(status);
306                 }
307         } else {
308                 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, lp_socket_address());
309                 NT_STATUS_NOT_OK_RETURN(status);
310         }
311
312         return NT_STATUS_OK;
313 }
314
315