r23792: convert Samba4 to GPLv3
[gd/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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "lib/socket/socket.h"
26 #include "lib/events/events.h"
27 #include "rpc_server/dcerpc_server.h"
28 #include "smbd/service_stream.h"
29 #include "smbd/service.h"
30 #include "lib/messaging/irpc.h"
31 #include "system/network.h"
32 #include "lib/socket/netif.h"
33 #include "auth/auth.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);
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         struct auth_session_info *session_info = NULL;
101
102         status = auth_anonymous_session_info(srv_conn, &session_info);
103         if (!NT_STATUS_IS_OK(status)) {
104                 DEBUG(0,("dcesrv_sock_accept: auth_anonymous_session_info failed: %s\n", 
105                         nt_errstr(status)));
106                 stream_terminate_connection(srv_conn, nt_errstr(status));
107                 return;
108         }
109
110         status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
111                                          srv_conn,
112                                          dcesrv_sock->endpoint,
113                                          session_info,
114                                          srv_conn->event.ctx,
115                                          srv_conn->msg_ctx,
116                                          srv_conn->server_id,
117                                          DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
118                                          &dcesrv_conn);
119         if (!NT_STATUS_IS_OK(status)) {
120                 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n", 
121                         nt_errstr(status)));
122                 stream_terminate_connection(srv_conn, nt_errstr(status));
123                 return;
124         }
125
126         dcesrv_conn->transport.private_data             = srv_conn;
127         dcesrv_conn->transport.report_output_data       = dcesrv_sock_report_output_data;
128         dcesrv_conn->transport.get_my_addr              = dcesrv_sock_get_my_addr;
129         dcesrv_conn->transport.get_peer_addr            = dcesrv_sock_get_peer_addr;
130
131         srv_conn->private = dcesrv_conn;
132
133         irpc_add_name(srv_conn->msg_ctx, "rpc_server");
134
135         return; 
136 }
137
138 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
139 {
140         NTSTATUS status;
141         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
142         DATA_BLOB tmp_blob;
143         size_t nread;
144
145         if (dce_conn->processing) {
146                 EVENT_FD_NOT_READABLE(conn->event.fde);
147                 return;
148         }
149
150         tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000);
151         if (tmp_blob.data == NULL) {
152                 dcesrv_terminate_connection(dce_conn, "out of memory");
153                 return;
154         }
155
156         status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread);
157         if (NT_STATUS_IS_ERR(status)) {
158                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
159                 return;
160         }
161         if (nread == 0) {
162                 talloc_free(tmp_blob.data);
163                 return;
164         }
165
166         tmp_blob.length = nread;
167
168         dce_conn->processing = True;
169         status = dcesrv_input(dce_conn, &tmp_blob);
170         dce_conn->processing = False;
171         talloc_free(tmp_blob.data);
172
173         EVENT_FD_READABLE(conn->event.fde);
174
175         if (!NT_STATUS_IS_OK(status)) {
176                 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
177                 return;
178         }
179
180         if (dce_conn->call_list && dce_conn->call_list->replies) {
181                 EVENT_FD_WRITEABLE(conn->event.fde);
182         }
183 }
184
185 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
186 {
187         struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
188         NTSTATUS status;
189
190         status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn);
191         if (NT_STATUS_IS_ERR(status)) {
192                 dcesrv_terminate_connection(dce_conn, "eof on socket");
193                 return;
194         }
195
196         if (!dce_conn->call_list || !dce_conn->call_list->replies) {
197                 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
198         }
199 }
200
201
202 static const struct stream_server_ops dcesrv_stream_ops = {
203         .name                   = "rpc",
204         .accept_connection      = dcesrv_sock_accept,
205         .recv_handler           = dcesrv_sock_recv,
206         .send_handler           = dcesrv_sock_send,
207 };
208
209
210
211 NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
212                                     struct event_context *event_ctx, const struct model_ops *model_ops)
213 {
214         struct dcesrv_socket_context *dcesrv_sock;
215         uint16_t port = 1;
216         NTSTATUS status;
217
218         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
219         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
220
221         /* remember the endpoint of this socket */
222         dcesrv_sock->endpoint           = e;
223         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
224
225         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
226                                      "unix", e->ep_description->endpoint, &port, 
227                                      dcesrv_sock);
228         if (!NT_STATUS_IS_OK(status)) {
229                 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
230                          e->ep_description->endpoint, nt_errstr(status)));
231         }
232
233         return status;
234 }
235
236 NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
237                                        struct event_context *event_ctx, const struct model_ops *model_ops)
238 {
239         struct dcesrv_socket_context *dcesrv_sock;
240         uint16_t port = 1;
241         char *full_path;
242         NTSTATUS status;
243
244         if (!e->ep_description->endpoint) {
245                 /* No identifier specified: use DEFAULT. 
246                  * DO NOT hardcode this value anywhere else. Rather, specify 
247                  * no endpoint and let the epmapper worry about it. */
248                 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
249         }
250
251         full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(), e->ep_description->endpoint);
252
253         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
254         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
255
256         /* remember the endpoint of this socket */
257         dcesrv_sock->endpoint           = e;
258         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
259
260         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
261                                      "unix", full_path, &port, dcesrv_sock);
262         if (!NT_STATUS_IS_OK(status)) {
263                 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
264                          e->ep_description->endpoint, full_path, nt_errstr(status)));
265         }
266         return status;
267 }
268
269
270 /*
271   add a socket address to the list of events, one event per dcerpc endpoint
272 */
273 static NTSTATUS add_socket_rpc_pipe_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
274                                          struct event_context *event_ctx, const struct model_ops *model_ops)
275 {
276         struct dcesrv_socket_context *dcesrv_sock;
277         NTSTATUS status;
278                         
279         if (e->ep_description->endpoint == NULL) {
280                 DEBUG(0, ("Endpoint mandatory for named pipes\n"));
281                 return NT_STATUS_INVALID_PARAMETER;
282         }
283
284         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
285         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
286
287         /* remember the endpoint of this socket */
288         dcesrv_sock->endpoint           = e;
289         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
290
291         status = NT_STATUS_OK;
292 #if 0
293
294         status = stream_setup_smb_pipe(event_ctx, model_ops, &dcesrv_stream_ops, 
295                                      e->ep_description->endpoint, dcesrv_sock);
296         if (!NT_STATUS_IS_OK(status)) {
297                 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n", 
298                          e->ep_description->endpoint, nt_errstr(status)));
299         }
300 #endif
301         return status;
302 }
303
304 NTSTATUS dcesrv_add_ep_np(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
305                                    struct event_context *event_ctx, const struct model_ops *model_ops)
306 {
307         NTSTATUS status;
308
309         status = add_socket_rpc_pipe_iface(dce_ctx, e, event_ctx, model_ops);
310         NT_STATUS_NOT_OK_RETURN(status);
311
312         return status;
313 }
314
315 /*
316   add a socket address to the list of events, one event per dcerpc endpoint
317 */
318 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
319                                          struct event_context *event_ctx, const struct model_ops *model_ops,
320                                          const char *address)
321 {
322         struct dcesrv_socket_context *dcesrv_sock;
323         uint16_t port = 0;
324         NTSTATUS status;
325                         
326         if (e->ep_description->endpoint) {
327                 port = atoi(e->ep_description->endpoint);
328         }
329
330         dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
331         NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
332
333         /* remember the endpoint of this socket */
334         dcesrv_sock->endpoint           = e;
335         dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
336
337         status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, 
338                                      "ipv4", address, &port, dcesrv_sock);
339         if (!NT_STATUS_IS_OK(status)) {
340                 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n", 
341                          address, port, nt_errstr(status)));
342         }
343
344         if (e->ep_description->endpoint == NULL) {
345                 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
346         }
347
348         return status;
349 }
350
351 NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
352                                    struct event_context *event_ctx, const struct model_ops *model_ops)
353 {
354         NTSTATUS status;
355
356         /* Add TCP/IP sockets */
357         if (lp_interfaces() && lp_bind_interfaces_only()) {
358                 int num_interfaces = iface_count();
359                 int i;
360                 for(i = 0; i < num_interfaces; i++) {
361                         const char *address = iface_n_ip(i);
362                         status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
363                         NT_STATUS_NOT_OK_RETURN(status);
364                 }
365         } else {
366                 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, lp_socket_address());
367                 NT_STATUS_NOT_OK_RETURN(status);
368         }
369
370         return NT_STATUS_OK;
371 }