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