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