2 Unix SMB/CIFS implementation.
3 Main winbindd server routines
5 Copyright (C) Stefan Metzmacher 2005
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
24 #include "lib/socket/socket.h"
25 #include "dlinklist.h"
26 #include "lib/events/events.h"
27 #include "smbd/service_task.h"
28 #include "smbd/service_stream.h"
29 #include "nsswitch/winbind_nss_config.h"
30 #include "winbind/wb_server.h"
32 void wbsrv_terminate_connection(struct wbsrv_connection *wbconn, const char *reason)
34 stream_terminate_connection(wbconn->conn, reason);
38 called when we get a new connection
40 static void wbsrv_accept(struct stream_connection *conn)
42 struct wbsrv_listen_socket *listen_socket =
43 talloc_get_type(conn->private, struct wbsrv_listen_socket);
44 struct wbsrv_connection *wbconn;
46 wbconn = talloc_zero(conn, struct wbsrv_connection);
48 stream_terminate_connection(conn,
49 "wbsrv_accept: out of memory");
53 wbconn->listen_socket = listen_socket;
54 conn->private = wbconn;
58 receive some data on a winbind connection
60 static void wbsrv_recv(struct stream_connection *conn, uint16_t flags)
62 struct wbsrv_connection *wbconn =
63 talloc_get_type(conn->private, struct wbsrv_connection);
64 const struct wbsrv_protocol_ops *ops = wbconn->listen_socket->ops;
65 struct wbsrv_call *call;
66 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
69 /* avoid recursion, because of half async code */
70 if (wbconn->processing) {
71 EVENT_FD_NOT_READABLE(conn->event.fde);
75 /* if the used protocol doesn't support pending requests disallow
77 if (wbconn->pending_calls && !ops->allow_pending_calls) {
78 EVENT_FD_NOT_READABLE(conn->event.fde);
82 if (wbconn->partial.length == 0) {
83 wbconn->partial = data_blob_talloc(wbconn, NULL, 4);
84 if (!wbconn->partial.data) goto nomem;
86 wbconn->partial_read = 0;
89 /* read in the packet length */
90 if (wbconn->partial_read < 4) {
91 uint32_t packet_length;
93 status = socket_recv(conn->socket,
94 wbconn->partial.data+wbconn->partial_read,
95 4 - wbconn->partial_read,
97 if (NT_STATUS_IS_ERR(status)) goto failed;
98 if (!NT_STATUS_IS_OK(status)) return;
100 wbconn->partial_read += nread;
101 if (wbconn->partial_read != 4) return;
103 packet_length = ops->packet_length(wbconn->partial);
105 wbconn->partial.data =
106 talloc_realloc(wbconn, wbconn->partial.data, uint8_t,
108 if (!wbconn->partial.data) goto nomem;
110 wbconn->partial.length = packet_length;
113 /* read in the body */
114 status = socket_recv(conn->socket,
115 wbconn->partial.data + wbconn->partial_read,
116 wbconn->partial.length - wbconn->partial_read,
118 if (NT_STATUS_IS_ERR(status)) goto failed;
119 if (!NT_STATUS_IS_OK(status)) return;
121 wbconn->partial_read += nread;
122 if (wbconn->partial_read != wbconn->partial.length) return;
124 /* we have a full request - parse it */
125 status = ops->pull_request(wbconn->partial, wbconn, &call);
126 if (!NT_STATUS_IS_OK(status)) goto failed;
127 call->wbconn = wbconn;
128 call->event_ctx = conn->event.ctx;
131 * we have parsed the request, so we can reset the
132 * wbconn->partial_read, maybe we could also free wbconn->partial, but
133 * for now we keep it, and overwrite it the next time
135 wbconn->partial_read = 0;
137 /* actually process the request */
138 wbconn->pending_calls++;
139 wbconn->processing = True;
140 status = ops->handle_call(call);
141 wbconn->processing = False;
142 if (!NT_STATUS_IS_OK(status)) goto failed;
144 /* if the backend want to reply later just return here */
145 if (call->flags & WBSRV_CALL_FLAGS_REPLY_ASYNC) {
150 * and queue the reply, this implies talloc_free(call),
151 * and set the socket to readable again
153 status = wbsrv_send_reply(call);
154 if (!NT_STATUS_IS_OK(status)) goto failed;
158 status = NT_STATUS_NO_MEMORY;
160 wbsrv_terminate_connection(wbconn, nt_errstr(status));
164 * queue a wbsrv_call reply on a wbsrv_connection
165 * NOTE: that this implies talloc_free(call),
166 * use talloc_reference(call) if you need it after
167 * calling wbsrv_queue_reply
168 * NOTE: if this function desn't return NT_STATUS_OK,
169 * the caller needs to call
170 * wbsrv_terminate_connection(call->wbconn, "reason...");
172 * to drop the connection
174 NTSTATUS wbsrv_send_reply(struct wbsrv_call *call)
176 struct wbsrv_connection *wbconn = call->wbconn;
177 const struct wbsrv_protocol_ops *ops = wbconn->listen_socket->ops;
178 struct data_blob_list_item *rep;
181 /* and now encode the reply */
182 rep = talloc(wbconn, struct data_blob_list_item);
183 NT_STATUS_HAVE_NO_MEMORY(rep);
185 status = ops->push_reply(call, rep, &rep->blob);
186 NT_STATUS_NOT_OK_RETURN(status);
188 if (!wbconn->send_queue) {
189 EVENT_FD_WRITEABLE(wbconn->conn->event.fde);
191 DLIST_ADD_END(wbconn->send_queue, rep, struct data_blob_list_item *);
193 EVENT_FD_READABLE(wbconn->conn->event.fde);
195 /* the call isn't needed any more */
196 wbconn->pending_calls--;
202 called when we can write to a connection
204 static void wbsrv_send(struct stream_connection *conn, uint16_t flags)
206 struct wbsrv_connection *wbconn = talloc_get_type(conn->private, struct wbsrv_connection);
209 while (wbconn->send_queue) {
210 struct data_blob_list_item *q = wbconn->send_queue;
213 status = socket_send(conn->socket, &q->blob, &sendlen, 0);
214 if (NT_STATUS_IS_ERR(status)) goto failed;
215 if (!NT_STATUS_IS_OK(status)) return;
217 q->blob.length -= sendlen;
218 q->blob.data += sendlen;
220 if (q->blob.length == 0) {
221 DLIST_REMOVE(wbconn->send_queue, q);
226 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
229 wbsrv_terminate_connection(wbconn, nt_errstr(status));
232 static const struct stream_server_ops wbsrv_ops = {
234 .accept_connection = wbsrv_accept,
235 .recv_handler = wbsrv_recv,
236 .send_handler = wbsrv_send
239 static const struct wbsrv_protocol_ops wbsrv_samba3_protocol_ops = {
240 .name = "winbind samba3 protocol",
241 .allow_pending_calls = False,
242 .packet_length = wbsrv_samba3_packet_length,
243 .pull_request = wbsrv_samba3_pull_request,
244 .handle_call = wbsrv_samba3_handle_call,
245 .push_reply = wbsrv_samba3_push_reply
249 startup the winbind task
251 static void winbind_task_init(struct task_server *task)
254 const struct model_ops *model_ops;
256 struct wbsrv_service *service;
257 struct wbsrv_listen_socket *listen_socket;
259 /* within the winbind task we want to be a single process, so
260 ask for the single process model ops and pass these to the
261 stream_setup_socket() call. */
262 model_ops = process_model_byname("single");
264 task_server_terminate(task,
265 "Can't find 'single' process model_ops");
269 /* Make sure the directory for NCALRPC exists */
270 if (!directory_exist(WINBINDD_DIR)) {
271 mkdir(WINBINDD_DIR, 0755);
274 service = talloc_zero(task, struct wbsrv_service);
275 if (!service) goto nomem;
276 service->task = task;
278 service->primary_sid = secrets_get_domain_sid(service,
280 if (service->primary_sid == NULL) {
281 task_server_terminate(
282 task, nt_errstr(NT_STATUS_CANT_ACCESS_DOMAIN_INFO));
286 /* setup the unprivileged samba3 socket */
287 listen_socket = talloc(service, struct wbsrv_listen_socket);
288 if (!listen_socket) goto nomem;
289 listen_socket->socket_path = WINBINDD_SAMBA3_SOCKET;
290 if (!listen_socket->socket_path) goto nomem;
291 listen_socket->service = service;
292 listen_socket->privileged = False;
293 listen_socket->ops = &wbsrv_samba3_protocol_ops;
294 status = stream_setup_socket(task->event_ctx, model_ops,
296 listen_socket->socket_path, &port,
298 if (!NT_STATUS_IS_OK(status)) goto listen_failed;
300 /* setup the privileged samba3 socket */
301 listen_socket = talloc(service, struct wbsrv_listen_socket);
302 if (!listen_socket) goto nomem;
303 listen_socket->socket_path =
304 smbd_tmp_path(listen_socket,
305 WINBINDD_SAMBA3_PRIVILEGED_SOCKET);
306 if (!listen_socket->socket_path) goto nomem;
307 listen_socket->service = service;
308 listen_socket->privileged = True;
309 listen_socket->ops = &wbsrv_samba3_protocol_ops;
310 status = stream_setup_socket(task->event_ctx, model_ops,
312 listen_socket->socket_path, &port,
314 if (!NT_STATUS_IS_OK(status)) goto listen_failed;
319 DEBUG(0,("stream_setup_socket(path=%s) failed - %s\n",
320 listen_socket->socket_path, nt_errstr(status)));
321 task_server_terminate(task, nt_errstr(status));
324 task_server_terminate(task, nt_errstr(NT_STATUS_NO_MEMORY));
329 initialise the winbind server
331 static NTSTATUS winbind_init(struct event_context *event_ctx,
332 const struct model_ops *model_ops)
334 return task_server_startup(event_ctx, model_ops, winbind_task_init);
338 register ourselves as a available server
340 NTSTATUS server_service_winbind_init(void)
342 return register_server_service("winbind", winbind_init);