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