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