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