r10491: First step towards wbinfo -t: This issues a name request for the primary
[jelmer/samba4-debian.git] / source / winbind / wb_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Main winbindd server routines
4
5    Copyright (C) Stefan Metzmacher      2005
6    Copyright (C) Andrew Tridgell        2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/socket/socket.h"
25 #include "system/dir.h"
26 #include "system/filesys.h"
27 #include "dlinklist.h"
28 #include "lib/events/events.h"
29 #include "smbd/service_task.h"
30 #include "smbd/service_stream.h"
31 #include "nsswitch/winbind_nss_config.h"
32 #include "nsswitch/winbindd_nss.h"
33 #include "winbind/wb_server.h"
34
35 void wbsrv_terminate_connection(struct wbsrv_connection *wbconn, const char *reason)
36 {
37         stream_terminate_connection(wbconn->conn, reason);
38 }
39
40 /*
41   called when we get a new connection
42 */
43 static void wbsrv_accept(struct stream_connection *conn)
44 {
45         struct wbsrv_listen_socket *listen_socket = talloc_get_type(conn->private,
46                                                                     struct wbsrv_listen_socket);
47         struct wbsrv_connection *wbconn;
48
49         wbconn = talloc_zero(conn, struct wbsrv_connection);
50         if (!wbconn) {
51                 stream_terminate_connection(conn, "wbsrv_accept: out of memory");
52                 return;
53         }
54         wbconn->conn            = conn;
55         wbconn->listen_socket   = listen_socket;
56         conn->private = wbconn;
57 }
58
59 /*
60   receive some data on a winbind connection
61 */
62 static void wbsrv_recv(struct stream_connection *conn, uint16_t flags)
63 {
64         struct wbsrv_connection *wbconn = talloc_get_type(conn->private, struct wbsrv_connection);
65         const struct wbsrv_protocol_ops *ops = wbconn->listen_socket->ops;
66         struct wbsrv_call *call;
67         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
68         size_t nread;
69
70         /* avoid recursion, because of half async code */
71         if (wbconn->processing) {
72                 EVENT_FD_NOT_READABLE(conn->event.fde);
73                 return;
74         }
75
76         /* if the used protocol doesn't support pending requests disallow them */
77         if (wbconn->pending_calls && !ops->allow_pending_calls) {
78                 EVENT_FD_NOT_READABLE(conn->event.fde);
79                 return;
80         }
81
82         if (wbconn->partial.length == 0) {
83                 wbconn->partial = data_blob_talloc(wbconn, NULL, 4);
84                 if (!wbconn->partial.data) goto nomem;
85
86                 wbconn->partial_read = 0;
87         }
88
89         /* read in the packet length */
90         if (wbconn->partial_read < 4) {
91                 uint32_t packet_length;
92
93                 status = socket_recv(conn->socket, 
94                                      wbconn->partial.data + wbconn->partial_read,
95                                      4 - wbconn->partial_read,
96                                      &nread, 0);
97                 if (NT_STATUS_IS_ERR(status)) goto failed;
98                 if (!NT_STATUS_IS_OK(status)) return;
99
100                 wbconn->partial_read += nread;
101                 if (wbconn->partial_read != 4) return;
102
103                 packet_length = ops->packet_length(wbconn->partial);
104
105                 wbconn->partial.data = talloc_realloc(wbconn, wbconn->partial.data, 
106                                                       uint8_t, packet_length);
107                 if (!wbconn->partial.data) goto nomem;
108
109                 wbconn->partial.length = packet_length;
110         }
111
112         /* read in the body */
113         status = socket_recv(conn->socket, 
114                              wbconn->partial.data + wbconn->partial_read,
115                              wbconn->partial.length - wbconn->partial_read,
116                              &nread, 0);
117         if (NT_STATUS_IS_ERR(status)) goto failed;
118         if (!NT_STATUS_IS_OK(status)) return;
119
120         wbconn->partial_read += nread;
121         if (wbconn->partial_read != wbconn->partial.length) return;
122
123         /* we have a full request - parse it */
124         status = ops->pull_request(wbconn->partial, wbconn, &call);
125         if (!NT_STATUS_IS_OK(status)) goto failed;
126         call->wbconn    = wbconn;
127         call->event_ctx = conn->event.ctx;
128
129         /*
130          * we have parsed the request, so we can reset the wbconn->partial_read,
131          * maybe we could also free wbconn->partial, but for now we keep it,
132          * and overwrite it the next time
133          */
134         wbconn->partial_read = 0;
135
136         /* actually process the request */
137         wbconn->pending_calls++;
138         wbconn->processing = True;
139         status = ops->handle_call(call);
140         wbconn->processing = False;
141         if (!NT_STATUS_IS_OK(status)) goto failed;
142
143         /* if the backend want to reply later just return here */
144         if (call->flags & WBSRV_CALL_FLAGS_REPLY_ASYNC) {
145                 return;
146         }
147
148         /*
149          * and queue the reply, this implies talloc_free(call),
150          * and set the socket to readable again
151          */
152         status = wbsrv_send_reply(call);
153         if (!NT_STATUS_IS_OK(status)) goto failed;
154
155         return;
156 nomem:
157         status = NT_STATUS_NO_MEMORY;
158 failed:
159         wbsrv_terminate_connection(wbconn, nt_errstr(status));
160 }
161
162 /*
163  * queue a wbsrv_call reply on a wbsrv_connection
164  * NOTE: that this implies talloc_free(call),
165  *       use talloc_reference(call) if you need it after
166  *       calling wbsrv_queue_reply
167  * NOTE: if this function desn't return NT_STATUS_OK,
168  *       the caller needs to call
169  *           wbsrv_terminate_connection(call->wbconn, "reason...");
170  *           return;
171  *       to drop the connection
172  */
173 NTSTATUS wbsrv_send_reply(struct wbsrv_call *call)
174 {
175         struct wbsrv_connection *wbconn = call->wbconn;
176         const struct wbsrv_protocol_ops *ops = wbconn->listen_socket->ops;
177         struct data_blob_list_item *rep;
178         NTSTATUS status;
179
180         /* and now encode the reply */
181         rep = talloc(wbconn, struct data_blob_list_item);
182         NT_STATUS_HAVE_NO_MEMORY(rep);
183
184         status = ops->push_reply(call, rep, &rep->blob);
185         NT_STATUS_NOT_OK_RETURN(status);
186
187         if (!wbconn->send_queue) {
188                 EVENT_FD_WRITEABLE(wbconn->conn->event.fde);
189         }
190         DLIST_ADD_END(wbconn->send_queue, rep, struct data_blob_list_item *);
191
192         EVENT_FD_READABLE(wbconn->conn->event.fde);
193
194         /* the call isn't needed any more */
195         wbconn->pending_calls--;
196         talloc_free(call);
197         return NT_STATUS_OK;
198 }
199
200 /*
201   called when we can write to a connection
202 */
203 static void wbsrv_send(struct stream_connection *conn, uint16_t flags)
204 {
205         struct wbsrv_connection *wbconn = talloc_get_type(conn->private, struct wbsrv_connection);
206         NTSTATUS status;
207
208         while (wbconn->send_queue) {
209                 struct data_blob_list_item *q = wbconn->send_queue;
210                 size_t sendlen;
211
212                 status = socket_send(conn->socket, &q->blob, &sendlen, 0);
213                 if (NT_STATUS_IS_ERR(status)) goto failed;
214                 if (!NT_STATUS_IS_OK(status)) return;
215
216                 q->blob.length -= sendlen;
217                 q->blob.data   += sendlen;
218
219                 if (q->blob.length == 0) {
220                         DLIST_REMOVE(wbconn->send_queue, q);
221                         talloc_free(q);
222                 }
223         }
224
225         EVENT_FD_NOT_WRITEABLE(conn->event.fde);
226         return;
227 failed:
228         wbsrv_terminate_connection(wbconn, nt_errstr(status));
229 }
230
231 static const struct stream_server_ops wbsrv_ops = {
232         .name                   = "winbind",
233         .accept_connection      = wbsrv_accept,
234         .recv_handler           = wbsrv_recv,
235         .send_handler           = wbsrv_send
236 };
237
238 static const struct wbsrv_protocol_ops wbsrv_samba3_protocol_ops = {
239         .name                   = "winbind samba3 protocol",
240         .allow_pending_calls    = False,
241         .packet_length          = wbsrv_samba3_packet_length,
242         .pull_request           = wbsrv_samba3_pull_request,
243         .handle_call            = wbsrv_samba3_handle_call,
244         .push_reply             = wbsrv_samba3_push_reply
245 };
246
247 /*
248   startup the winbind task
249 */
250 static void winbind_task_init(struct task_server *task)
251 {
252         uint16_t port = 1;
253         const struct model_ops *model_ops;
254         NTSTATUS status;
255         struct wbsrv_service *service;
256         struct wbsrv_listen_socket *listen_socket;
257
258         /* within the winbind task we want to be a single process, so
259            ask for the single process model ops and pass these to the
260            stream_setup_socket() call. */
261         model_ops = process_model_byname("single");
262         if (!model_ops) {
263                 task_server_terminate(task, "Can't find 'single' process model_ops");
264                 return;
265         }
266
267         /* Make sure the directory for NCALRPC exists */
268         if (!directory_exist(WINBINDD_DIR)) {
269                 mkdir(WINBINDD_DIR, 0755);
270         }
271
272         service = talloc_zero(task, struct wbsrv_service);
273         if (!service) goto nomem;
274         service->task   = task;
275
276         /* setup the unprivileged samba3 socket */
277         listen_socket = talloc(service, struct wbsrv_listen_socket);
278         if (!listen_socket) goto nomem;
279         listen_socket->socket_path      = WINBINDD_SAMBA3_SOCKET;
280         if (!listen_socket->socket_path) goto nomem;
281         listen_socket->service          = service;
282         listen_socket->privileged       = False;
283         listen_socket->ops              = &wbsrv_samba3_protocol_ops;
284         status = stream_setup_socket(task->event_ctx, model_ops,
285                                      &wbsrv_ops, "unix",
286                                      listen_socket->socket_path, &port, listen_socket);
287         if (!NT_STATUS_IS_OK(status)) goto listen_failed;
288
289         /* setup the privileged samba3 socket */
290         listen_socket = talloc(service, struct wbsrv_listen_socket);
291         if (!listen_socket) goto nomem;
292         listen_socket->socket_path      = smbd_tmp_path(listen_socket, WINBINDD_SAMBA3_PRIVILEGED_SOCKET);
293         if (!listen_socket->socket_path) goto nomem;
294         listen_socket->service          = service;
295         listen_socket->privileged       = True;
296         listen_socket->ops              = &wbsrv_samba3_protocol_ops;
297         status = stream_setup_socket(task->event_ctx, model_ops,
298                                      &wbsrv_ops, "unix",
299                                      listen_socket->socket_path, &port, listen_socket);
300         if (!NT_STATUS_IS_OK(status)) goto listen_failed;
301
302         return;
303
304 listen_failed:
305         DEBUG(0,("stream_setup_socket(path=%s) failed - %s\n",
306                  listen_socket->socket_path, nt_errstr(status)));
307         task_server_terminate(task, nt_errstr(status));
308         return;
309 nomem:
310         task_server_terminate(task, nt_errstr(NT_STATUS_NO_MEMORY));
311         return;
312 }
313
314 /*
315   initialise the winbind server
316  */
317 static NTSTATUS winbind_init(struct event_context *event_ctx, const struct model_ops *model_ops)
318 {
319         return task_server_startup(event_ctx, model_ops, winbind_task_init);
320 }
321
322 /*
323   register ourselves as a available server
324 */
325 NTSTATUS server_service_winbind_init(void)
326 {
327         return register_server_service("winbind", winbind_init);
328 }