Initial import
[samba] / source / nsswitch / winbindd_dual.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind child daemons
5
6    Copyright (C) Andrew Tridgell 2002
7    Copyright (C) Volker Lendecke 2004,2005
8    
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.
13    
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.
18    
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.
22 */
23
24 /*
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.
29  */
30
31 #include "includes.h"
32 #include "winbindd.h"
33
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_WINBIND
36
37 /* Read some data from a client connection */
38
39 static void child_read_request(struct winbindd_cli_state *state)
40 {
41         ssize_t len;
42
43         /* Read data */
44
45         len = read_data(state->sock, (char *)&state->request,
46                         sizeof(state->request));
47
48         if (len != sizeof(state->request)) {
49                 DEBUG(0, ("Got invalid request length: %d\n", (int)len));
50                 state->finished = True;
51                 return;
52         }
53
54         if (state->request.extra_len == 0) {
55                 state->request.extra_data = NULL;
56                 return;
57         }
58
59         DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request.extra_len));
60
61         state->request.extra_data =
62                 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
63
64         if (state->request.extra_data == NULL) {
65                 DEBUG(0, ("malloc failed\n"));
66                 state->finished = True;
67                 return;
68         }
69
70         /* Ensure null termination */
71         state->request.extra_data[state->request.extra_len] = '\0';
72
73         len = read_data(state->sock, state->request.extra_data,
74                         state->request.extra_len);
75
76         if (len != state->request.extra_len) {
77                 DEBUG(0, ("Could not read extra data\n"));
78                 state->finished = True;
79                 return;
80         }
81 }
82
83 /*
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.
88  */
89
90 struct winbindd_async_request {
91         struct winbindd_async_request *next, *prev;
92         TALLOC_CTX *mem_ctx;
93         struct winbindd_child *child;
94         struct winbindd_request *request;
95         struct winbindd_response *response;
96         void (*continuation)(void *private_data, BOOL success);
97         void *private_data;
98 };
99
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);
104
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),
109                    void *private_data)
110 {
111         struct winbindd_async_request *state, *tmp;
112
113         SMB_ASSERT(continuation != NULL);
114
115         state = TALLOC_P(mem_ctx, struct winbindd_async_request);
116
117         if (state == NULL) {
118                 DEBUG(0, ("talloc failed\n"));
119                 continuation(private_data, False);
120                 return;
121         }
122
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;
129
130         DLIST_ADD_END(child->requests, state, tmp);
131
132         schedule_async_request(child);
133
134         return;
135 }
136
137 static void async_main_request_sent(void *private_data, BOOL success)
138 {
139         struct winbindd_async_request *state =
140                 talloc_get_type_abort(private_data, struct winbindd_async_request);
141
142         if (!success) {
143                 DEBUG(5, ("Could not send async request\n"));
144
145                 state->response->length = sizeof(struct winbindd_response);
146                 state->response->result = WINBINDD_ERROR;
147                 state->continuation(state->private_data, False);
148                 return;
149         }
150
151         if (state->request->extra_len == 0) {
152                 async_request_sent(private_data, True);
153                 return;
154         }
155
156         setup_async_write(&state->child->event, state->request->extra_data,
157                           state->request->extra_len,
158                           async_request_sent, state);
159 }
160
161 static void async_request_sent(void *private_data_data, BOOL success)
162 {
163         struct winbindd_async_request *state =
164                 talloc_get_type_abort(private_data_data, struct winbindd_async_request);
165
166         if (!success) {
167                 DEBUG(5, ("Could not send async request\n"));
168
169                 state->response->length = sizeof(struct winbindd_response);
170                 state->response->result = WINBINDD_ERROR;
171                 state->continuation(state->private_data, False);
172                 return;
173         }
174
175         /* Request successfully sent to the child, setup the wait for reply */
176
177         setup_async_read(&state->child->event,
178                          &state->response->result,
179                          sizeof(state->response->result),
180                          async_reply_recv, state);
181 }
182
183 static void async_reply_recv(void *private_data, BOOL success)
184 {
185         struct winbindd_async_request *state =
186                 talloc_get_type_abort(private_data, struct winbindd_async_request);
187         struct winbindd_child *child = state->child;
188
189         state->response->length = sizeof(struct winbindd_response);
190
191         if (!success) {
192                 DEBUG(5, ("Could not receive async reply\n"));
193                 state->response->result = WINBINDD_ERROR;
194                 return;
195         }
196
197         SMB_ASSERT(cache_retrieve_response(child->pid,
198                                            state->response));
199
200         DLIST_REMOVE(child->requests, state);
201
202         schedule_async_request(child);
203
204         state->continuation(state->private_data, True);
205 }
206
207 static BOOL fork_domain_child(struct winbindd_child *child);
208
209 static void schedule_async_request(struct winbindd_child *child)
210 {
211         struct winbindd_async_request *request = child->requests;
212
213         if (request == NULL) {
214                 return;
215         }
216
217         if (child->event.flags != 0) {
218                 return;         /* Busy */
219         }
220
221         if ((child->pid == 0) && (!fork_domain_child(child))) {
222                 /* Cancel all outstanding requests */
223
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);
228                         request = next;
229                 }
230                 return;
231         }
232
233         setup_async_write(&child->event, request->request,
234                           sizeof(*request->request),
235                           async_main_request_sent, request);
236         return;
237 }
238
239 struct domain_request_state {
240         TALLOC_CTX *mem_ctx;
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;
246 };
247
248 static void domain_init_recv(void *private_data_data, BOOL success);
249
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)
256 {
257         struct domain_request_state *state;
258
259         if (domain->initialized) {
260                 async_request(mem_ctx, &domain->child, request, response,
261                               continuation, private_data_data);
262                 return;
263         }
264
265         state = TALLOC_P(mem_ctx, struct domain_request_state);
266         if (state == NULL) {
267                 DEBUG(0, ("talloc failed\n"));
268                 continuation(private_data_data, False);
269                 return;
270         }
271
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;
278
279         init_child_connection(domain, domain_init_recv, state);
280 }
281
282 static void recvfrom_child(void *private_data_data, BOOL success)
283 {
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;
287
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. */
291
292         state->response.result = WINBINDD_PENDING;
293
294         if ((!success) || (result != WINBINDD_OK)) {
295                 request_error(state);
296                 return;
297         }
298
299         request_ok(state);
300 }
301
302 void sendto_child(struct winbindd_cli_state *state,
303                   struct winbindd_child *child)
304 {
305         async_request(state->mem_ctx, child, &state->request,
306                       &state->response, recvfrom_child, state);
307 }
308
309 void sendto_domain(struct winbindd_cli_state *state,
310                    struct winbindd_domain *domain)
311 {
312         async_domain_request(state->mem_ctx, domain,
313                              &state->request, &state->response,
314                              recvfrom_child, state);
315 }
316
317 static void domain_init_recv(void *private_data_data, BOOL success)
318 {
319         struct domain_request_state *state =
320                 talloc_get_type_abort(private_data_data, struct domain_request_state);
321
322         if (!success) {
323                 DEBUG(5, ("Domain init returned an error\n"));
324                 state->continuation(state->private_data_data, False);
325                 return;
326         }
327
328         async_request(state->mem_ctx, &state->domain->child,
329                       state->request, state->response,
330                       state->continuation, state->private_data_data);
331 }
332
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;
338 };
339
340 static struct winbindd_child_dispatch_table child_dispatch_table[] = {
341         
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" },
363         /* End of list */
364
365         { WINBINDD_NUM_CMDS, NULL, "NONE" }
366 };
367
368 static void child_process_request(struct winbindd_domain *domain,
369                                   struct winbindd_cli_state *state)
370 {
371         struct winbindd_child_dispatch_table *table;
372
373         /* Free response data - we may be interrupted and receive another
374            command before being able to send this data off. */
375
376         state->response.result = WINBINDD_ERROR;
377         state->response.length = sizeof(struct winbindd_response);
378
379         state->mem_ctx = talloc_init("winbind request");
380         if (state->mem_ctx == NULL)
381                 return;
382
383         /* Process command */
384
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);
390                         break;
391                 }
392         }
393
394         if (!table->fn) {
395                 DEBUG(10,("process_request: unknown request fn number %d\n",
396                           (int)state->request.cmd ));
397                 state->response.result = WINBINDD_ERROR;
398         }
399
400         talloc_destroy(state->mem_ctx);
401 }
402
403 void setup_domain_child(struct winbindd_domain *domain,
404                         struct winbindd_child *child,
405                         const char *explicit_logfile)
406 {
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);
413         } else {
414                 smb_panic("Internal error: domain == NULL && "
415                           "explicit_logfile == NULL");
416         }
417
418         child->domain = domain;
419 }
420
421 struct winbindd_child *children = NULL;
422
423 void winbind_child_died(pid_t pid)
424 {
425         struct winbindd_child *child;
426
427         for (child = children; child != NULL; child = child->next) {
428                 if (child->pid == pid) {
429                         break;
430                 }
431         }
432
433         if (child == NULL) {
434                 DEBUG(0, ("Unknown child %d died!\n", pid));
435                 return;
436         }
437
438         remove_fd_event(&child->event);
439         close(child->event.fd);
440         child->event.fd = 0;
441         child->event.flags = 0;
442         child->pid = 0;
443
444         schedule_async_request(child);
445 }
446
447 static BOOL fork_domain_child(struct winbindd_child *child)
448 {
449         int fdpair[2];
450         struct winbindd_cli_state state;
451         extern BOOL override_logfile;
452
453         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
454                 DEBUG(0, ("Could not open child pipe: %s\n",
455                           strerror(errno)));
456                 return False;
457         }
458
459         ZERO_STRUCT(state);
460         state.pid = getpid();
461
462         child->pid = sys_fork();
463
464         if (child->pid == -1) {
465                 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
466                 return False;
467         }
468
469         if (child->pid != 0) {
470                 /* Parent */
471                 close(fdpair[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);
478                 return True;
479         }
480
481         /* Child */
482
483         state.sock = fdpair[0];
484         close(fdpair[1]);
485
486         /* tdb needs special fork handling */
487         if (tdb_reopen_all() == -1) {
488                 DEBUG(0,("tdb_reopen_all failed.\n"));
489                 _exit(0);
490         }
491
492         close_conns_after_fork();
493
494         if (!override_logfile) {
495                 lp_set_logfile(child->logfilename);
496                 reopen_logs();
497         }
498         
499         while (1) {
500                 /* free up any talloc memory */
501                 lp_talloc_free();
502                 main_loop_talloc_free();
503
504                 /* fetch a request from the main daemon */
505                 child_read_request(&state);
506
507                 if (state.finished) {
508                         /* we lost contact with our parent */
509                         exit(0);
510                 }
511
512                 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
513
514                 ZERO_STRUCT(state.response);
515                 state.request.null_term = '\0';
516                 child_process_request(child->domain, &state);
517
518                 SAFE_FREE(state.request.extra_data);
519
520                 cache_store_response(sys_getpid(), &state.response);
521
522                 SAFE_FREE(state.response.extra_data);
523
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... */
527
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"));
532                         exit(1);
533                 }
534         }
535 }