540332299dce3b5147e39748b8feb1cae9b4cf61
[sfrench/samba-autobuild/.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 "lib/messaging/irpc.h"
31
32 struct dcesrv_socket_context {
33         const struct dcesrv_endpoint *endpoint;
34         struct dcesrv_context *dcesrv_ctx;      
35 };
36
37 /*
38   write_fn callback for dcesrv_output()
39 */
40 static NTSTATUS dcerpc_write_fn(void *private_data, DATA_BLOB *out, size_t *nwritten)
41 {
42         NTSTATUS status;
43         struct socket_context *sock = talloc_get_type(private_data, struct socket_context);
44         size_t sendlen;
45
46         status = socket_send(sock, out, &sendlen, 0);
47         NT_STATUS_IS_ERR_RETURN(status);
48
49         *nwritten = sendlen;
50         return status;
51 }
52
53 static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
54 {
55         stream_terminate_connection(dce_conn->srv_conn, reason);
56 }
57
58
59 static void dcesrv_sock_accept(struct stream_connection *srv_conn)
60 {
61         NTSTATUS status;
62         struct dcesrv_socket_context *dcesrv_sock = 
63                 talloc_get_type(srv_conn->private, struct dcesrv_socket_context);
64         struct dcesrv_connection *dcesrv_conn = NULL;
65
66         status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
67                                          srv_conn,
68                                          dcesrv_sock->endpoint,
69                                          srv_conn,
70                                          DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
71                                          &dcesrv_conn);
72         if (!NT_STATUS_IS_OK(status)) {
73                 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n", 
74                         nt_errstr(status)));
75                 stream_terminate_connection(srv_conn, nt_errstr(status));
76                 return;
77         }
78
79         srv_conn->private = dcesrv_conn;
80
81         irpc_add_name(srv_conn->msg_ctx, "rpc_server");
82
83         return; 
84 }
85
86 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
87 {
88         NTSTATUS status;
89         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
90         DATA_BLOB tmp_blob;
91         size_t nread;
92
93         if (dce_conn->processing) {
94                 EVENT_FD_NOT_READABLE(conn->event.fde);
95                 return;
96         }
97
98         tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000);
99         if (tmp_blob.data == NULL) {
100                 dcesrv_terminate_connection(dce_conn, "out of memory");
101                 return;
102         }
103
104         status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread, 0);
105         if (NT_STATUS_IS_ERR(status)) {
106                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
107                 return;
108         }
109         if (nread == 0) {
110                 talloc_free(tmp_blob.data);
111                 return;
112         }
113
114         tmp_blob.length = nread;
115
116         dce_conn->processing = True;
117         status = dcesrv_input(dce_conn, &tmp_blob);
118         dce_conn->processing = False;
119         talloc_free(tmp_blob.data);
120
121         EVENT_FD_READABLE(conn->event.fde);
122
123         if (!NT_STATUS_IS_OK(status)) {
124                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
125                 return;
126         }
127
128         if (dce_conn->call_list && dce_conn->call_list->replies) {
129                 EVENT_FD_WRITEABLE(conn->event.fde);
130         }
131 }
132
133 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
134 {
135         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
136         NTSTATUS status;
137
138         status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn);
139         if (NT_STATUS_IS_ERR(status)) {
140                 dcesrv_terminate_connection(dce_conn, "eof on socket");
141                 return;
142         }
143
144         if (!dce_conn->call_list || !dce_conn->call_list->replies) {
145                 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
146         }
147 }
148
149
150 static const struct stream_server_ops dcesrv_stream_ops = {
151         .name                   = "rpc",
152         .accept_connection      = dcesrv_sock_accept,
153         .recv_handler           = dcesrv_sock_recv,
154         .send_handler           = dcesrv_sock_send,
155 };
156
157
158
159 NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
160                                     struct event_context *event_ctx, const struct model_ops *model_ops)
161 {
162         struct dcesrv_socket_context *dcesrv_sock;
163         uint16_t port = 1;
164         NTSTATUS status;
165
166         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
167         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
168
169         /* remember the endpoint of this socket */
170         dcesrv_sock->endpoint           = e;
171         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
172
173         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
174                                      "unix", e->ep_description->endpoint, &port, 
175                                      dcesrv_sock);
176         if (!NT_STATUS_IS_OK(status)) {
177                 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
178                          e->ep_description->endpoint, nt_errstr(status)));
179         }
180
181         return status;
182 }
183
184 NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
185                                        struct event_context *event_ctx, const struct model_ops *model_ops)
186 {
187         struct dcesrv_socket_context *dcesrv_sock;
188         uint16_t port = 1;
189         char *full_path;
190         NTSTATUS status;
191
192         if (!e->ep_description->endpoint) {
193                 /* No identifier specified: use DEFAULT. 
194                  * DO NOT hardcode this value anywhere else. Rather, specify 
195                  * no endpoint and let the epmapper worry about it. */
196                 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
197         }
198
199         full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(), e->ep_description->endpoint);
200
201         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
202         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
203
204         /* remember the endpoint of this socket */
205         dcesrv_sock->endpoint           = e;
206         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
207
208         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
209                                      "unix", full_path, &port, dcesrv_sock);
210         if (!NT_STATUS_IS_OK(status)) {
211                 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
212                          e->ep_description->endpoint, full_path, nt_errstr(status)));
213         }
214         return status;
215 }
216
217 /*
218   add a socket address to the list of events, one event per dcerpc endpoint
219 */
220 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
221                                          struct event_context *event_ctx, const struct model_ops *model_ops,
222                                          const char *address)
223 {
224         struct dcesrv_socket_context *dcesrv_sock;
225         uint16_t port = 0;
226         NTSTATUS status;
227                         
228         if (e->ep_description->endpoint) {
229                 port = atoi(e->ep_description->endpoint);
230         }
231
232         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
233         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
234
235         /* remember the endpoint of this socket */
236         dcesrv_sock->endpoint           = e;
237         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
238
239         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
240                                      "ipv4", address, &port, dcesrv_sock);
241         if (!NT_STATUS_IS_OK(status)) {
242                 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n", 
243                          address, port, nt_errstr(status)));
244         }
245
246         if (e->ep_description->endpoint == NULL) {
247                 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
248         }
249
250         return status;
251 }
252
253 NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
254                                    struct event_context *event_ctx, const struct model_ops *model_ops)
255 {
256         NTSTATUS status;
257
258         /* Add TCP/IP sockets */
259         if (lp_interfaces() && lp_bind_interfaces_only()) {
260                 int num_interfaces = iface_count();
261                 int i;
262                 for(i = 0; i < num_interfaces; i++) {
263                         const char *address = iface_n_ip(i);
264                         status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
265                         NT_STATUS_NOT_OK_RETURN(status);
266                 }
267         } else {
268                 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, lp_socket_address());
269                 NT_STATUS_NOT_OK_RETURN(status);
270         }
271
272         return NT_STATUS_OK;
273 }
274
275