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