2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2002
7 Copyright (C) Volker Lendecke 2004,2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * We fork a child per domain to be able to act non-blocking in the main
26 * winbind daemon. A domain controller thousands of miles away being being
27 * slow replying with a 10.000 user list should not hold up netlogon calls
28 * that can be handled locally.
35 #define DBGC_CLASS DBGC_WINBIND
37 /* Read some data from a client connection */
39 static void child_read_request(struct winbindd_cli_state *state)
45 len = read_data(state->sock, (char *)&state->request,
46 sizeof(state->request));
48 if (len != sizeof(state->request)) {
49 DEBUG(0, ("Got invalid request length: %d\n", (int)len));
50 state->finished = True;
54 if (state->request.extra_len == 0) {
55 state->request.extra_data = NULL;
59 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request.extra_len));
61 state->request.extra_data =
62 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
64 if (state->request.extra_data == NULL) {
65 DEBUG(0, ("malloc failed\n"));
66 state->finished = True;
70 /* Ensure null termination */
71 state->request.extra_data[state->request.extra_len] = '\0';
73 len = read_data(state->sock, state->request.extra_data,
74 state->request.extra_len);
76 if (len != state->request.extra_len) {
77 DEBUG(0, ("Could not read extra data\n"));
78 state->finished = True;
84 * Machinery for async requests sent to children. You set up a
85 * winbindd_request, select a child to query, and issue a async_request
86 * call. When the request is completed, the callback function you specified is
87 * called back with the private pointer you gave to async_request.
90 struct winbindd_async_request {
91 struct winbindd_async_request *next, *prev;
93 struct winbindd_child *child;
94 struct winbindd_request *request;
95 struct winbindd_response *response;
96 void (*continuation)(void *private_data, BOOL success);
100 static void async_main_request_sent(void *private_data, BOOL success);
101 static void async_request_sent(void *private_data, BOOL success);
102 static void async_reply_recv(void *private_data, BOOL success);
103 static void schedule_async_request(struct winbindd_child *child);
105 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
106 struct winbindd_request *request,
107 struct winbindd_response *response,
108 void (*continuation)(void *private_data, BOOL success),
111 struct winbindd_async_request *state, *tmp;
113 SMB_ASSERT(continuation != NULL);
115 state = TALLOC_P(mem_ctx, struct winbindd_async_request);
118 DEBUG(0, ("talloc failed\n"));
119 continuation(private_data, False);
123 state->mem_ctx = mem_ctx;
124 state->child = child;
125 state->request = request;
126 state->response = response;
127 state->continuation = continuation;
128 state->private_data = private_data;
130 DLIST_ADD_END(child->requests, state, tmp);
132 schedule_async_request(child);
137 static void async_main_request_sent(void *private_data, BOOL success)
139 struct winbindd_async_request *state =
140 talloc_get_type_abort(private_data, struct winbindd_async_request);
143 DEBUG(5, ("Could not send async request\n"));
145 state->response->length = sizeof(struct winbindd_response);
146 state->response->result = WINBINDD_ERROR;
147 state->continuation(state->private_data, False);
151 if (state->request->extra_len == 0) {
152 async_request_sent(private_data, True);
156 setup_async_write(&state->child->event, state->request->extra_data,
157 state->request->extra_len,
158 async_request_sent, state);
161 static void async_request_sent(void *private_data_data, BOOL success)
163 struct winbindd_async_request *state =
164 talloc_get_type_abort(private_data_data, struct winbindd_async_request);
167 DEBUG(5, ("Could not send async request\n"));
169 state->response->length = sizeof(struct winbindd_response);
170 state->response->result = WINBINDD_ERROR;
171 state->continuation(state->private_data, False);
175 /* Request successfully sent to the child, setup the wait for reply */
177 setup_async_read(&state->child->event,
178 &state->response->result,
179 sizeof(state->response->result),
180 async_reply_recv, state);
183 static void async_reply_recv(void *private_data, BOOL success)
185 struct winbindd_async_request *state =
186 talloc_get_type_abort(private_data, struct winbindd_async_request);
187 struct winbindd_child *child = state->child;
189 state->response->length = sizeof(struct winbindd_response);
192 DEBUG(5, ("Could not receive async reply\n"));
193 state->response->result = WINBINDD_ERROR;
197 SMB_ASSERT(cache_retrieve_response(child->pid,
200 DLIST_REMOVE(child->requests, state);
202 schedule_async_request(child);
204 state->continuation(state->private_data, True);
207 static BOOL fork_domain_child(struct winbindd_child *child);
209 static void schedule_async_request(struct winbindd_child *child)
211 struct winbindd_async_request *request = child->requests;
213 if (request == NULL) {
217 if (child->event.flags != 0) {
221 if ((child->pid == 0) && (!fork_domain_child(child))) {
222 /* Cancel all outstanding requests */
224 while (request != NULL) {
225 /* request might be free'd in the continuation */
226 struct winbindd_async_request *next = request->next;
227 request->continuation(request->private_data, False);
233 setup_async_write(&child->event, request->request,
234 sizeof(*request->request),
235 async_main_request_sent, request);
239 struct domain_request_state {
241 struct winbindd_domain *domain;
242 struct winbindd_request *request;
243 struct winbindd_response *response;
244 void (*continuation)(void *private_data_data, BOOL success);
245 void *private_data_data;
248 static void domain_init_recv(void *private_data_data, BOOL success);
250 void async_domain_request(TALLOC_CTX *mem_ctx,
251 struct winbindd_domain *domain,
252 struct winbindd_request *request,
253 struct winbindd_response *response,
254 void (*continuation)(void *private_data_data, BOOL success),
255 void *private_data_data)
257 struct domain_request_state *state;
259 if (domain->initialized) {
260 async_request(mem_ctx, &domain->child, request, response,
261 continuation, private_data_data);
265 state = TALLOC_P(mem_ctx, struct domain_request_state);
267 DEBUG(0, ("talloc failed\n"));
268 continuation(private_data_data, False);
272 state->mem_ctx = mem_ctx;
273 state->domain = domain;
274 state->request = request;
275 state->response = response;
276 state->continuation = continuation;
277 state->private_data_data = private_data_data;
279 init_child_connection(domain, domain_init_recv, state);
282 static void recvfrom_child(void *private_data_data, BOOL success)
284 struct winbindd_cli_state *state =
285 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
286 enum winbindd_result result = state->response.result;
288 /* This is an optimization: The child has written directly to the
289 * response buffer. The request itself is still in pending state,
290 * state that in the result code. */
292 state->response.result = WINBINDD_PENDING;
294 if ((!success) || (result != WINBINDD_OK)) {
295 request_error(state);
302 void sendto_child(struct winbindd_cli_state *state,
303 struct winbindd_child *child)
305 async_request(state->mem_ctx, child, &state->request,
306 &state->response, recvfrom_child, state);
309 void sendto_domain(struct winbindd_cli_state *state,
310 struct winbindd_domain *domain)
312 async_domain_request(state->mem_ctx, domain,
313 &state->request, &state->response,
314 recvfrom_child, state);
317 static void domain_init_recv(void *private_data_data, BOOL success)
319 struct domain_request_state *state =
320 talloc_get_type_abort(private_data_data, struct domain_request_state);
323 DEBUG(5, ("Domain init returned an error\n"));
324 state->continuation(state->private_data_data, False);
328 async_request(state->mem_ctx, &state->domain->child,
329 state->request, state->response,
330 state->continuation, state->private_data_data);
333 struct winbindd_child_dispatch_table {
334 enum winbindd_cmd cmd;
335 enum winbindd_result (*fn)(struct winbindd_domain *domain,
336 struct winbindd_cli_state *state);
337 const char *winbindd_cmd_name;
340 static struct winbindd_child_dispatch_table child_dispatch_table[] = {
342 { WINBINDD_LOOKUPSID, winbindd_dual_lookupsid, "LOOKUPSID" },
343 { WINBINDD_LOOKUPNAME, winbindd_dual_lookupname, "LOOKUPNAME" },
344 { WINBINDD_LIST_TRUSTDOM, winbindd_dual_list_trusted_domains, "LIST_TRUSTDOM" },
345 { WINBINDD_INIT_CONNECTION, winbindd_dual_init_connection, "INIT_CONNECTION" },
346 { WINBINDD_GETDCNAME, winbindd_dual_getdcname, "GETDCNAME" },
347 { WINBINDD_SHOW_SEQUENCE, winbindd_dual_show_sequence, "SHOW_SEQUENCE" },
348 { WINBINDD_PAM_AUTH, winbindd_dual_pam_auth, "PAM_AUTH" },
349 { WINBINDD_PAM_AUTH_CRAP, winbindd_dual_pam_auth_crap, "AUTH_CRAP" },
350 { WINBINDD_CHECK_MACHACC, winbindd_dual_check_machine_acct, "CHECK_MACHACC" },
351 { WINBINDD_DUAL_SID2UID, winbindd_dual_sid2uid, "DUAL_SID2UID" },
352 { WINBINDD_DUAL_SID2GID, winbindd_dual_sid2gid, "DUAL_SID2GID" },
353 { WINBINDD_DUAL_UID2NAME, winbindd_dual_uid2name, "DUAL_UID2NAME" },
354 { WINBINDD_DUAL_NAME2UID, winbindd_dual_name2uid, "DUAL_NAME2UID" },
355 { WINBINDD_DUAL_GID2NAME, winbindd_dual_gid2name, "DUAL_GID2NAME" },
356 { WINBINDD_DUAL_NAME2GID, winbindd_dual_name2gid, "DUAL_NAME2GID" },
357 { WINBINDD_DUAL_IDMAPSET, winbindd_dual_idmapset, "DUAL_IDMAPSET" },
358 { WINBINDD_DUAL_USERINFO, winbindd_dual_userinfo, "DUAL_USERINFO" },
359 { WINBINDD_ALLOCATE_RID, winbindd_dual_allocate_rid, "ALLOCATE_RID" },
360 { WINBINDD_ALLOCATE_RID_AND_GID, winbindd_dual_allocate_rid_and_gid, "ALLOCATE_RID_AND_GID" },
361 { WINBINDD_GETUSERDOMGROUPS, winbindd_dual_getuserdomgroups, "GETUSERDOMGROUPS" },
362 { WINBINDD_DUAL_GETSIDALIASES, winbindd_dual_getsidaliases, "GETSIDALIASES" },
365 { WINBINDD_NUM_CMDS, NULL, "NONE" }
368 static void child_process_request(struct winbindd_domain *domain,
369 struct winbindd_cli_state *state)
371 struct winbindd_child_dispatch_table *table;
373 /* Free response data - we may be interrupted and receive another
374 command before being able to send this data off. */
376 state->response.result = WINBINDD_ERROR;
377 state->response.length = sizeof(struct winbindd_response);
379 state->mem_ctx = talloc_init("winbind request");
380 if (state->mem_ctx == NULL)
383 /* Process command */
385 for (table = child_dispatch_table; table->fn; table++) {
386 if (state->request.cmd == table->cmd) {
387 DEBUG(10,("process_request: request fn %s\n",
388 table->winbindd_cmd_name ));
389 state->response.result = table->fn(domain, state);
395 DEBUG(10,("process_request: unknown request fn number %d\n",
396 (int)state->request.cmd ));
397 state->response.result = WINBINDD_ERROR;
400 talloc_destroy(state->mem_ctx);
403 void setup_domain_child(struct winbindd_domain *domain,
404 struct winbindd_child *child,
405 const char *explicit_logfile)
407 if (explicit_logfile != NULL) {
408 pstr_sprintf(child->logfilename, "%s/log.winbindd-%s",
409 dyn_LOGFILEBASE, explicit_logfile);
410 } else if (domain != NULL) {
411 pstr_sprintf(child->logfilename, "%s/log.wb-%s",
412 dyn_LOGFILEBASE, domain->name);
414 smb_panic("Internal error: domain == NULL && "
415 "explicit_logfile == NULL");
418 child->domain = domain;
421 struct winbindd_child *children = NULL;
423 void winbind_child_died(pid_t pid)
425 struct winbindd_child *child;
427 for (child = children; child != NULL; child = child->next) {
428 if (child->pid == pid) {
434 DEBUG(0, ("Unknown child %d died!\n", pid));
438 remove_fd_event(&child->event);
439 close(child->event.fd);
441 child->event.flags = 0;
444 schedule_async_request(child);
447 static BOOL fork_domain_child(struct winbindd_child *child)
450 struct winbindd_cli_state state;
451 extern BOOL override_logfile;
453 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
454 DEBUG(0, ("Could not open child pipe: %s\n",
460 state.pid = getpid();
462 child->pid = sys_fork();
464 if (child->pid == -1) {
465 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
469 if (child->pid != 0) {
472 child->next = child->prev = NULL;
473 DLIST_ADD(children, child);
474 child->event.fd = fdpair[1];
475 child->event.flags = 0;
476 child->requests = NULL;
477 add_fd_event(&child->event);
483 state.sock = fdpair[0];
486 /* tdb needs special fork handling */
487 if (tdb_reopen_all() == -1) {
488 DEBUG(0,("tdb_reopen_all failed.\n"));
492 close_conns_after_fork();
494 if (!override_logfile) {
495 lp_set_logfile(child->logfilename);
500 /* free up any talloc memory */
502 main_loop_talloc_free();
504 /* fetch a request from the main daemon */
505 child_read_request(&state);
507 if (state.finished) {
508 /* we lost contact with our parent */
512 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
514 ZERO_STRUCT(state.response);
515 state.request.null_term = '\0';
516 child_process_request(child->domain, &state);
518 SAFE_FREE(state.request.extra_data);
520 cache_store_response(sys_getpid(), &state.response);
522 SAFE_FREE(state.response.extra_data);
524 /* We just send the result code back, the result
525 * structure needs to be fetched via the
526 * winbindd_cache. Hmm. That needs fixing... */
528 if (write_data(state.sock, (void *)&state.response.result,
529 sizeof(state.response.result)) !=
530 sizeof(state.response.result)) {
531 DEBUG(0, ("Could not write result\n"));