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