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 "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"
35 void wbsrv_terminate_connection(struct wbsrv_connection *wbconn, const char *reason)
37 stream_terminate_connection(wbconn->conn, reason);
41 called when we get a new connection
43 static void wbsrv_accept(struct stream_connection *conn)
45 struct wbsrv_listen_socket *listen_socket = talloc_get_type(conn->private,
46 struct wbsrv_listen_socket);
47 struct wbsrv_connection *wbconn;
49 wbconn = talloc_zero(conn, struct wbsrv_connection);
51 stream_terminate_connection(conn, "wbsrv_accept: out of memory");
55 wbconn->listen_socket = listen_socket;
56 conn->private = wbconn;
60 receive some data on a winbind connection
62 static void wbsrv_recv(struct stream_connection *conn, uint16_t flags)
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;
70 /* avoid recursion, because of half async code */
71 if (wbconn->processing) {
72 EVENT_FD_NOT_READABLE(conn->event.fde);
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);
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 = talloc_realloc(wbconn, wbconn->partial.data,
106 uint8_t, packet_length);
107 if (!wbconn->partial.data) goto nomem;
109 wbconn->partial.length = packet_length;
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,
117 if (NT_STATUS_IS_ERR(status)) goto failed;
118 if (!NT_STATUS_IS_OK(status)) return;
120 wbconn->partial_read += nread;
121 if (wbconn->partial_read != wbconn->partial.length) return;
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;
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
134 wbconn->partial_read = 0;
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;
143 /* if the backend want to reply later just return here */
144 if (call->flags & WBSRV_CALL_FLAGS_REPLY_ASYNC) {
149 * and queue the reply, this implies talloc_free(call),
150 * and set the socket to readable again
152 status = wbsrv_send_reply(call);
153 if (!NT_STATUS_IS_OK(status)) goto failed;
157 status = NT_STATUS_NO_MEMORY;
159 wbsrv_terminate_connection(wbconn, nt_errstr(status));
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...");
171 * to drop the connection
173 NTSTATUS wbsrv_send_reply(struct wbsrv_call *call)
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;
180 /* and now encode the reply */
181 rep = talloc(wbconn, struct data_blob_list_item);
182 NT_STATUS_HAVE_NO_MEMORY(rep);
184 status = ops->push_reply(call, rep, &rep->blob);
185 NT_STATUS_NOT_OK_RETURN(status);
187 if (!wbconn->send_queue) {
188 EVENT_FD_WRITEABLE(wbconn->conn->event.fde);
190 DLIST_ADD_END(wbconn->send_queue, rep, struct data_blob_list_item *);
192 EVENT_FD_READABLE(wbconn->conn->event.fde);
194 /* the call isn't needed any more */
195 wbconn->pending_calls--;
201 called when we can write to a connection
203 static void wbsrv_send(struct stream_connection *conn, uint16_t flags)
205 struct wbsrv_connection *wbconn = talloc_get_type(conn->private, struct wbsrv_connection);
208 while (wbconn->send_queue) {
209 struct data_blob_list_item *q = wbconn->send_queue;
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;
216 q->blob.length -= sendlen;
217 q->blob.data += sendlen;
219 if (q->blob.length == 0) {
220 DLIST_REMOVE(wbconn->send_queue, q);
225 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
228 wbsrv_terminate_connection(wbconn, nt_errstr(status));
231 static const struct stream_server_ops wbsrv_ops = {
233 .accept_connection = wbsrv_accept,
234 .recv_handler = wbsrv_recv,
235 .send_handler = wbsrv_send
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
248 startup the winbind task
250 static void winbind_task_init(struct task_server *task)
253 const struct model_ops *model_ops;
255 struct wbsrv_service *service;
256 struct wbsrv_listen_socket *listen_socket;
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");
263 task_server_terminate(task, "Can't find 'single' process model_ops");
267 /* Make sure the directory for NCALRPC exists */
268 if (!directory_exist(WINBINDD_DIR)) {
269 mkdir(WINBINDD_DIR, 0755);
272 service = talloc_zero(task, struct wbsrv_service);
273 if (!service) goto nomem;
274 service->task = task;
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,
286 listen_socket->socket_path, &port, listen_socket);
287 if (!NT_STATUS_IS_OK(status)) goto listen_failed;
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,
299 listen_socket->socket_path, &port, listen_socket);
300 if (!NT_STATUS_IS_OK(status)) goto 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));
310 task_server_terminate(task, nt_errstr(NT_STATUS_NO_MEMORY));
315 initialise the winbind server
317 static NTSTATUS winbind_init(struct event_context *event_ctx, const struct model_ops *model_ops)
319 return task_server_startup(event_ctx, model_ops, winbind_task_init);
323 register ourselves as a available server
325 NTSTATUS server_service_winbind_init(void)
327 return register_server_service("winbind", winbind_init);