Fix bug #5504. winbindd children and parent were handing SIGTERM in the same way...
[tprouty/samba.git] / source / winbindd / 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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 /*
24  * We fork a child per domain to be able to act non-blocking in the main
25  * winbind daemon. A domain controller thousands of miles away being being
26  * slow replying with a 10.000 user list should not hold up netlogon calls
27  * that can be handled locally.
28  */
29
30 #include "includes.h"
31 #include "winbindd.h"
32
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_WINBIND
35
36 extern bool override_logfile;
37 extern struct winbindd_methods cache_methods;
38
39 /* Read some data from a client connection */
40
41 static void child_read_request(struct winbindd_cli_state *state)
42 {
43         NTSTATUS status;
44
45         /* Read data */
46
47         status = read_data(state->sock, (char *)&state->request,
48                            sizeof(state->request));
49
50         if (!NT_STATUS_IS_OK(status)) {
51                 DEBUG(3, ("child_read_request: read_data failed: %s\n",
52                           nt_errstr(status)));
53                 state->finished = True;
54                 return;
55         }
56
57         if (state->request.extra_len == 0) {
58                 state->request.extra_data.data = NULL;
59                 return;
60         }
61
62         DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request.extra_len));
63
64         state->request.extra_data.data =
65                 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
66
67         if (state->request.extra_data.data == NULL) {
68                 DEBUG(0, ("malloc failed\n"));
69                 state->finished = True;
70                 return;
71         }
72
73         /* Ensure null termination */
74         state->request.extra_data.data[state->request.extra_len] = '\0';
75
76         status= read_data(state->sock, state->request.extra_data.data,
77                           state->request.extra_len);
78
79         if (!NT_STATUS_IS_OK(status)) {
80                 DEBUG(0, ("Could not read extra data: %s\n",
81                           nt_errstr(status)));
82                 state->finished = True;
83                 return;
84         }
85 }
86
87 /*
88  * Machinery for async requests sent to children. You set up a
89  * winbindd_request, select a child to query, and issue a async_request
90  * call. When the request is completed, the callback function you specified is
91  * called back with the private pointer you gave to async_request.
92  */
93
94 struct winbindd_async_request {
95         struct winbindd_async_request *next, *prev;
96         TALLOC_CTX *mem_ctx;
97         struct winbindd_child *child;
98         struct winbindd_request *request;
99         struct winbindd_response *response;
100         void (*continuation)(void *private_data, bool success);
101         struct timed_event *reply_timeout_event;
102         pid_t child_pid; /* pid of the child we're waiting on. Used to detect
103                             a restart of the child (child->pid != child_pid). */
104         void *private_data;
105 };
106
107 static void async_main_request_sent(void *private_data, bool success);
108 static void async_request_sent(void *private_data, bool success);
109 static void async_reply_recv(void *private_data, bool success);
110 static void schedule_async_request(struct winbindd_child *child);
111
112 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
113                    struct winbindd_request *request,
114                    struct winbindd_response *response,
115                    void (*continuation)(void *private_data, bool success),
116                    void *private_data)
117 {
118         struct winbindd_async_request *state;
119
120         SMB_ASSERT(continuation != NULL);
121
122         state = TALLOC_P(mem_ctx, struct winbindd_async_request);
123
124         if (state == NULL) {
125                 DEBUG(0, ("talloc failed\n"));
126                 continuation(private_data, False);
127                 return;
128         }
129
130         state->mem_ctx = mem_ctx;
131         state->child = child;
132         state->request = request;
133         state->response = response;
134         state->continuation = continuation;
135         state->private_data = private_data;
136
137         DLIST_ADD_END(child->requests, state, struct winbindd_async_request *);
138
139         schedule_async_request(child);
140
141         return;
142 }
143
144 static void async_main_request_sent(void *private_data, bool success)
145 {
146         struct winbindd_async_request *state =
147                 talloc_get_type_abort(private_data, struct winbindd_async_request);
148
149         if (!success) {
150                 DEBUG(5, ("Could not send async request\n"));
151
152                 state->response->length = sizeof(struct winbindd_response);
153                 state->response->result = WINBINDD_ERROR;
154                 state->continuation(state->private_data, False);
155                 return;
156         }
157
158         if (state->request->extra_len == 0) {
159                 async_request_sent(private_data, True);
160                 return;
161         }
162
163         setup_async_write(&state->child->event, state->request->extra_data.data,
164                           state->request->extra_len,
165                           async_request_sent, state);
166 }
167
168 /****************************************************************
169  Handler triggered if the child winbindd doesn't respond within
170  a given timeout.
171 ****************************************************************/
172
173 static void async_request_timeout_handler(struct event_context *ctx,
174                                         struct timed_event *te,
175                                         const struct timeval *now,
176                                         void *private_data)
177 {
178         struct winbindd_async_request *state =
179                 talloc_get_type_abort(private_data, struct winbindd_async_request);
180
181         DEBUG(0,("async_request_timeout_handler: child pid %u is not responding. "
182                 "Closing connection to it.\n",
183                 state->child_pid ));
184
185         /* Deal with the reply - set to error. */
186         async_reply_recv(private_data, False);
187 }
188
189 /**************************************************************
190  Common function called on both async send and recv fail.
191  Cleans up the child and schedules the next request.
192 **************************************************************/
193
194 static void async_request_fail(struct winbindd_async_request *state)
195 {
196         DLIST_REMOVE(state->child->requests, state);
197
198         TALLOC_FREE(state->reply_timeout_event);
199
200         SMB_ASSERT(state->child_pid != (pid_t)0);
201
202         /* If not already reaped, send kill signal to child. */
203         if (state->child->pid == state->child_pid) {
204                 kill(state->child_pid, SIGTERM);
205
206                 /* 
207                  * Close the socket to the child.
208                  */
209                 winbind_child_died(state->child_pid);
210         }
211
212         state->response->length = sizeof(struct winbindd_response);
213         state->response->result = WINBINDD_ERROR;
214         state->continuation(state->private_data, False);
215 }
216
217 static void async_request_sent(void *private_data_data, bool success)
218 {
219         struct winbindd_async_request *state =
220                 talloc_get_type_abort(private_data_data, struct winbindd_async_request);
221
222         if (!success) {
223                 DEBUG(5, ("Could not send async request to child pid %u\n",
224                         (unsigned int)state->child_pid ));
225                 async_request_fail(state);
226                 return;
227         }
228
229         /* Request successfully sent to the child, setup the wait for reply */
230
231         setup_async_read(&state->child->event,
232                          &state->response->result,
233                          sizeof(state->response->result),
234                          async_reply_recv, state);
235
236         /* 
237          * Set up a timeout of 300 seconds for the response.
238          * If we don't get it close the child socket and
239          * report failure.
240          */
241
242         state->reply_timeout_event = event_add_timed(winbind_event_context(),
243                                                         NULL,
244                                                         timeval_current_ofs(300,0),
245                                                         "async_request_timeout",
246                                                         async_request_timeout_handler,
247                                                         state);
248         if (!state->reply_timeout_event) {
249                 smb_panic("async_request_sent: failed to add timeout handler.\n");
250         }
251 }
252
253 static void async_reply_recv(void *private_data, bool success)
254 {
255         struct winbindd_async_request *state =
256                 talloc_get_type_abort(private_data, struct winbindd_async_request);
257         struct winbindd_child *child = state->child;
258
259         TALLOC_FREE(state->reply_timeout_event);
260
261         state->response->length = sizeof(struct winbindd_response);
262
263         if (!success) {
264                 DEBUG(5, ("Could not receive async reply from child pid %u\n",
265                         (unsigned int)state->child_pid ));
266
267                 cache_cleanup_response(state->child_pid);
268                 async_request_fail(state);
269                 return;
270         }
271
272         SMB_ASSERT(cache_retrieve_response(state->child_pid,
273                                            state->response));
274
275         cache_cleanup_response(state->child_pid);
276         
277         DLIST_REMOVE(child->requests, state);
278
279         schedule_async_request(child);
280
281         state->continuation(state->private_data, True);
282 }
283
284 static bool fork_domain_child(struct winbindd_child *child);
285
286 static void schedule_async_request(struct winbindd_child *child)
287 {
288         struct winbindd_async_request *request = child->requests;
289
290         if (request == NULL) {
291                 return;
292         }
293
294         if (child->event.flags != 0) {
295                 return;         /* Busy */
296         }
297
298         if ((child->pid == 0) && (!fork_domain_child(child))) {
299                 /* Cancel all outstanding requests */
300
301                 while (request != NULL) {
302                         /* request might be free'd in the continuation */
303                         struct winbindd_async_request *next = request->next;
304                         request->continuation(request->private_data, False);
305                         request = next;
306                 }
307                 return;
308         }
309
310         /* Now we know who we're sending to - remember the pid. */
311         request->child_pid = child->pid;
312
313         setup_async_write(&child->event, request->request,
314                           sizeof(*request->request),
315                           async_main_request_sent, request);
316
317         return;
318 }
319
320 struct domain_request_state {
321         TALLOC_CTX *mem_ctx;
322         struct winbindd_domain *domain;
323         struct winbindd_request *request;
324         struct winbindd_response *response;
325         void (*continuation)(void *private_data_data, bool success);
326         void *private_data_data;
327 };
328
329 static void domain_init_recv(void *private_data_data, bool success);
330
331 void async_domain_request(TALLOC_CTX *mem_ctx,
332                           struct winbindd_domain *domain,
333                           struct winbindd_request *request,
334                           struct winbindd_response *response,
335                           void (*continuation)(void *private_data_data, bool success),
336                           void *private_data_data)
337 {
338         struct domain_request_state *state;
339
340         if (domain->initialized) {
341                 async_request(mem_ctx, &domain->child, request, response,
342                               continuation, private_data_data);
343                 return;
344         }
345
346         state = TALLOC_P(mem_ctx, struct domain_request_state);
347         if (state == NULL) {
348                 DEBUG(0, ("talloc failed\n"));
349                 continuation(private_data_data, False);
350                 return;
351         }
352
353         state->mem_ctx = mem_ctx;
354         state->domain = domain;
355         state->request = request;
356         state->response = response;
357         state->continuation = continuation;
358         state->private_data_data = private_data_data;
359
360         init_child_connection(domain, domain_init_recv, state);
361 }
362
363 static void domain_init_recv(void *private_data_data, bool success)
364 {
365         struct domain_request_state *state =
366                 talloc_get_type_abort(private_data_data, struct domain_request_state);
367
368         if (!success) {
369                 DEBUG(5, ("Domain init returned an error\n"));
370                 state->continuation(state->private_data_data, False);
371                 return;
372         }
373
374         async_request(state->mem_ctx, &state->domain->child,
375                       state->request, state->response,
376                       state->continuation, state->private_data_data);
377 }
378
379 static void recvfrom_child(void *private_data_data, bool success)
380 {
381         struct winbindd_cli_state *state =
382                 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
383         enum winbindd_result result = state->response.result;
384
385         /* This is an optimization: The child has written directly to the
386          * response buffer. The request itself is still in pending state,
387          * state that in the result code. */
388
389         state->response.result = WINBINDD_PENDING;
390
391         if ((!success) || (result != WINBINDD_OK)) {
392                 request_error(state);
393                 return;
394         }
395
396         request_ok(state);
397 }
398
399 void sendto_child(struct winbindd_cli_state *state,
400                   struct winbindd_child *child)
401 {
402         async_request(state->mem_ctx, child, &state->request,
403                       &state->response, recvfrom_child, state);
404 }
405
406 void sendto_domain(struct winbindd_cli_state *state,
407                    struct winbindd_domain *domain)
408 {
409         async_domain_request(state->mem_ctx, domain,
410                              &state->request, &state->response,
411                              recvfrom_child, state);
412 }
413
414 static void child_process_request(struct winbindd_child *child,
415                                   struct winbindd_cli_state *state)
416 {
417         struct winbindd_domain *domain = child->domain;
418         const struct winbindd_child_dispatch_table *table = child->table;
419
420         /* Free response data - we may be interrupted and receive another
421            command before being able to send this data off. */
422
423         state->response.result = WINBINDD_ERROR;
424         state->response.length = sizeof(struct winbindd_response);
425
426         /* as all requests in the child are sync, we can use talloc_tos() */
427         state->mem_ctx = talloc_tos();
428
429         /* Process command */
430
431         for (; table->name; table++) {
432                 if (state->request.cmd == table->struct_cmd) {
433                         DEBUG(10,("child_process_request: request fn %s\n",
434                                   table->name));
435                         state->response.result = table->struct_fn(domain, state);
436                         return;
437                 }
438         }
439
440         DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
441                   (int)state->request.cmd));
442         state->response.result = WINBINDD_ERROR;
443 }
444
445 void setup_child(struct winbindd_child *child,
446                  const struct winbindd_child_dispatch_table *table,
447                  const char *logprefix,
448                  const char *logname)
449 {
450         if (logprefix && logname) {
451                 if (asprintf(&child->logfilename, "%s/%s-%s",
452                              get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
453                         smb_panic("Internal error: asprintf failed");
454                 }
455         } else {
456                 smb_panic("Internal error: logprefix == NULL && "
457                           "logname == NULL");
458         }
459
460         child->domain = NULL;
461         child->table = table;
462 }
463
464 struct winbindd_child *children = NULL;
465
466 void winbind_child_died(pid_t pid)
467 {
468         struct winbindd_child *child;
469
470         for (child = children; child != NULL; child = child->next) {
471                 if (child->pid == pid) {
472                         break;
473                 }
474         }
475
476         if (child == NULL) {
477                 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
478                 return;
479         }
480
481         /* This will be re-added in fork_domain_child() */
482
483         DLIST_REMOVE(children, child);
484         
485         remove_fd_event(&child->event);
486         close(child->event.fd);
487         child->event.fd = 0;
488         child->event.flags = 0;
489         child->pid = 0;
490
491         schedule_async_request(child);
492 }
493
494 /* Ensure any negative cache entries with the netbios or realm names are removed. */
495
496 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
497 {
498         flush_negative_conn_cache_for_domain(domain->name);
499         if (*domain->alt_name) {
500                 flush_negative_conn_cache_for_domain(domain->alt_name);
501         }
502 }
503
504 /* Set our domains as offline and forward the offline message to our children. */
505
506 void winbind_msg_offline(struct messaging_context *msg_ctx,
507                          void *private_data,
508                          uint32_t msg_type,
509                          struct server_id server_id,
510                          DATA_BLOB *data)
511 {
512         struct winbindd_child *child;
513         struct winbindd_domain *domain;
514
515         DEBUG(10,("winbind_msg_offline: got offline message.\n"));
516
517         if (!lp_winbind_offline_logon()) {
518                 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
519                 return;
520         }
521
522         /* Set our global state as offline. */
523         if (!set_global_winbindd_state_offline()) {
524                 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
525                 return;
526         }
527
528         /* Set all our domains as offline. */
529         for (domain = domain_list(); domain; domain = domain->next) {
530                 if (domain->internal) {
531                         continue;
532                 }
533                 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
534                 set_domain_offline(domain);
535         }
536
537         for (child = children; child != NULL; child = child->next) {
538                 /* Don't send message to internal childs.  We've already
539                    done so above. */
540                 if (!child->domain || winbindd_internal_child(child)) {
541                         continue;
542                 }
543
544                 /* Or internal domains (this should not be possible....) */
545                 if (child->domain->internal) {
546                         continue;
547                 }
548
549                 /* Each winbindd child should only process requests for one domain - make sure
550                    we only set it online / offline for that domain. */
551
552                 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
553                         (unsigned int)child->pid, domain->name ));
554
555                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
556                                    MSG_WINBIND_OFFLINE,
557                                    (uint8 *)child->domain->name,
558                                    strlen(child->domain->name)+1);
559         }
560 }
561
562 /* Set our domains as online and forward the online message to our children. */
563
564 void winbind_msg_online(struct messaging_context *msg_ctx,
565                         void *private_data,
566                         uint32_t msg_type,
567                         struct server_id server_id,
568                         DATA_BLOB *data)
569 {
570         struct winbindd_child *child;
571         struct winbindd_domain *domain;
572
573         DEBUG(10,("winbind_msg_online: got online message.\n"));
574
575         if (!lp_winbind_offline_logon()) {
576                 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
577                 return;
578         }
579
580         /* Set our global state as online. */
581         set_global_winbindd_state_online();
582
583         smb_nscd_flush_user_cache();
584         smb_nscd_flush_group_cache();
585
586         /* Set all our domains as online. */
587         for (domain = domain_list(); domain; domain = domain->next) {
588                 if (domain->internal) {
589                         continue;
590                 }
591                 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
592
593                 winbindd_flush_negative_conn_cache(domain);
594                 set_domain_online_request(domain);
595
596                 /* Send an online message to the idmap child when our
597                    primary domain comes back online */
598
599                 if ( domain->primary ) {
600                         struct winbindd_child *idmap = idmap_child();
601                         
602                         if ( idmap->pid != 0 ) {
603                                 messaging_send_buf(msg_ctx,
604                                                    pid_to_procid(idmap->pid), 
605                                                    MSG_WINBIND_ONLINE,
606                                                    (uint8 *)domain->name,
607                                                    strlen(domain->name)+1);
608                         }
609                         
610                 }
611         }
612
613         for (child = children; child != NULL; child = child->next) {
614                 /* Don't send message to internal childs. */
615                 if (!child->domain || winbindd_internal_child(child)) {
616                         continue;
617                 }
618
619                 /* Or internal domains (this should not be possible....) */
620                 if (child->domain->internal) {
621                         continue;
622                 }
623
624                 /* Each winbindd child should only process requests for one domain - make sure
625                    we only set it online / offline for that domain. */
626
627                 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
628                         (unsigned int)child->pid, child->domain->name ));
629
630                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
631                                    MSG_WINBIND_ONLINE,
632                                    (uint8 *)child->domain->name,
633                                    strlen(child->domain->name)+1);
634         }
635 }
636
637 /* Forward the online/offline messages to our children. */
638 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
639                               void *private_data,
640                               uint32_t msg_type,
641                               struct server_id server_id,
642                               DATA_BLOB *data)
643 {
644         struct winbindd_child *child;
645
646         DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
647
648         for (child = children; child != NULL; child = child->next) {
649                 if (child->domain && child->domain->primary) {
650                         DEBUG(10,("winbind_msg_onlinestatus: "
651                                   "sending message to pid %u of primary domain.\n",
652                                   (unsigned int)child->pid));
653                         messaging_send_buf(msg_ctx, pid_to_procid(child->pid), 
654                                            MSG_WINBIND_ONLINESTATUS,
655                                            (uint8 *)data->data,
656                                            data->length);
657                         break;
658                 }
659         }
660 }
661
662 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
663                                  void *private_data,
664                                  uint32_t msg_type,
665                                  struct server_id server_id,
666                                  DATA_BLOB *data)
667 {
668         struct winbindd_child *child;
669
670         DEBUG(10,("winbind_msg_dump_event_list received\n"));
671
672         dump_event_list(winbind_event_context());
673
674         for (child = children; child != NULL; child = child->next) {
675
676                 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
677                         (unsigned int)child->pid));
678
679                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
680                                    MSG_DUMP_EVENT_LIST,
681                                    NULL, 0);
682         }
683
684 }
685
686 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
687                                   void *private_data,
688                                   uint32_t msg_type,
689                                   struct server_id server_id,
690                                   DATA_BLOB *data)
691 {
692         TALLOC_CTX *mem_ctx;
693         const char *message = NULL;
694         struct server_id *sender = NULL;
695         const char *domain = NULL;
696         char *s = NULL;
697         NTSTATUS status;
698         struct winbindd_domain *dom = NULL;
699
700         DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
701
702         if (!data || !data->data) {
703                 return;
704         }
705
706         if (data->length < sizeof(struct server_id)) {
707                 return;
708         }
709
710         mem_ctx = talloc_init("winbind_msg_dump_domain_list");
711         if (!mem_ctx) {
712                 return;
713         }
714
715         sender = (struct server_id *)data->data;
716         if (data->length > sizeof(struct server_id)) {
717                 domain = (const char *)data->data+sizeof(struct server_id);
718         }
719
720         if (domain) {
721
722                 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
723                         domain));
724
725                 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
726                                                   find_domain_from_name_noinit(domain));
727                 if (!message) {
728                         talloc_destroy(mem_ctx);
729                         return;
730                 }
731
732                 messaging_send_buf(msg_ctx, *sender,
733                                    MSG_WINBIND_DUMP_DOMAIN_LIST,
734                                    (uint8_t *)message, strlen(message) + 1);
735
736                 talloc_destroy(mem_ctx);
737
738                 return;
739         }
740
741         DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
742
743         for (dom = domain_list(); dom; dom=dom->next) {
744                 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
745                 if (!message) {
746                         talloc_destroy(mem_ctx);
747                         return;
748                 }
749
750                 s = talloc_asprintf_append(s, "%s\n", message);
751                 if (!s) {
752                         talloc_destroy(mem_ctx);
753                         return;
754                 }
755         }
756
757         status = messaging_send_buf(msg_ctx, *sender,
758                                     MSG_WINBIND_DUMP_DOMAIN_LIST,
759                                     (uint8_t *)s, strlen(s) + 1);
760         if (!NT_STATUS_IS_OK(status)) {
761                 DEBUG(0,("failed to send message: %s\n",
762                 nt_errstr(status)));
763         }
764
765         talloc_destroy(mem_ctx);
766 }
767
768 static void account_lockout_policy_handler(struct event_context *ctx,
769                                            struct timed_event *te,
770                                            const struct timeval *now,
771                                            void *private_data)
772 {
773         struct winbindd_child *child =
774                 (struct winbindd_child *)private_data;
775         TALLOC_CTX *mem_ctx = NULL;
776         struct winbindd_methods *methods;
777         struct samr_DomInfo12 lockout_policy;
778         NTSTATUS result;
779
780         DEBUG(10,("account_lockout_policy_handler called\n"));
781
782         TALLOC_FREE(child->lockout_policy_event);
783
784         if ( !winbindd_can_contact_domain( child->domain ) ) {
785                 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
786                           "do not have an incoming trust to domain %s\n", 
787                           child->domain->name));
788
789                 return;         
790         }
791
792         methods = child->domain->methods;
793
794         mem_ctx = talloc_init("account_lockout_policy_handler ctx");
795         if (!mem_ctx) {
796                 result = NT_STATUS_NO_MEMORY;
797         } else {
798                 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
799         }
800         TALLOC_FREE(mem_ctx);
801
802         if (!NT_STATUS_IS_OK(result)) {
803                 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
804                          nt_errstr(result)));
805         }
806
807         child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
808                                                       timeval_current_ofs(3600, 0),
809                                                       "account_lockout_policy_handler",
810                                                       account_lockout_policy_handler,
811                                                       child);
812 }
813
814 /* Deal with a request to go offline. */
815
816 static void child_msg_offline(struct messaging_context *msg,
817                               void *private_data,
818                               uint32_t msg_type,
819                               struct server_id server_id,
820                               DATA_BLOB *data)
821 {
822         struct winbindd_domain *domain;
823         const char *domainname = (const char *)data->data;
824
825         if (data->data == NULL || data->length == 0) {
826                 return;
827         }
828
829         DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
830
831         if (!lp_winbind_offline_logon()) {
832                 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
833                 return;
834         }
835
836         /* Mark the requested domain offline. */
837
838         for (domain = domain_list(); domain; domain = domain->next) {
839                 if (domain->internal) {
840                         continue;
841                 }
842                 if (strequal(domain->name, domainname)) {
843                         DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
844                         set_domain_offline(domain);
845                 }
846         }
847 }
848
849 /* Deal with a request to go online. */
850
851 static void child_msg_online(struct messaging_context *msg,
852                              void *private_data,
853                              uint32_t msg_type,
854                              struct server_id server_id,
855                              DATA_BLOB *data)
856 {
857         struct winbindd_domain *domain;
858         const char *domainname = (const char *)data->data;
859
860         if (data->data == NULL || data->length == 0) {
861                 return;
862         }
863
864         DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
865
866         if (!lp_winbind_offline_logon()) {
867                 DEBUG(10,("child_msg_online: rejecting online message.\n"));
868                 return;
869         }
870
871         /* Set our global state as online. */
872         set_global_winbindd_state_online();
873
874         /* Try and mark everything online - delete any negative cache entries
875            to force a reconnect now. */
876
877         for (domain = domain_list(); domain; domain = domain->next) {
878                 if (domain->internal) {
879                         continue;
880                 }
881                 if (strequal(domain->name, domainname)) {
882                         DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
883                         winbindd_flush_negative_conn_cache(domain);
884                         set_domain_online_request(domain);
885                 }
886         }
887 }
888
889 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
890 {
891         struct winbindd_domain *domain;
892         char *buf = NULL;
893
894         if ((buf = talloc_asprintf(mem_ctx, "global:%s ", 
895                                    get_global_winbindd_state_offline() ? 
896                                    "Offline":"Online")) == NULL) {
897                 return NULL;
898         }
899
900         for (domain = domain_list(); domain; domain = domain->next) {
901                 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ", 
902                                                   domain->name, 
903                                                   domain->online ?
904                                                   "Online":"Offline")) == NULL) {
905                         return NULL;
906                 }
907         }
908
909         buf = talloc_asprintf_append_buffer(buf, "\n");
910
911         DEBUG(5,("collect_onlinestatus: %s", buf));
912
913         return buf;
914 }
915
916 static void child_msg_onlinestatus(struct messaging_context *msg_ctx,
917                                    void *private_data,
918                                    uint32_t msg_type,
919                                    struct server_id server_id,
920                                    DATA_BLOB *data)
921 {
922         TALLOC_CTX *mem_ctx;
923         const char *message;
924         struct server_id *sender;
925         
926         DEBUG(5,("winbind_msg_onlinestatus received.\n"));
927
928         if (!data->data) {
929                 return;
930         }
931
932         sender = (struct server_id *)data->data;
933
934         mem_ctx = talloc_init("winbind_msg_onlinestatus");
935         if (mem_ctx == NULL) {
936                 return;
937         }
938         
939         message = collect_onlinestatus(mem_ctx);
940         if (message == NULL) {
941                 talloc_destroy(mem_ctx);
942                 return;
943         }
944
945         messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS, 
946                            (uint8 *)message, strlen(message) + 1);
947
948         talloc_destroy(mem_ctx);
949 }
950
951 static void child_msg_dump_event_list(struct messaging_context *msg,
952                                       void *private_data,
953                                       uint32_t msg_type,
954                                       struct server_id server_id,
955                                       DATA_BLOB *data)
956 {
957         DEBUG(5,("child_msg_dump_event_list received\n"));
958
959         dump_event_list(winbind_event_context());
960 }
961
962
963 static bool fork_domain_child(struct winbindd_child *child)
964 {
965         int fdpair[2];
966         struct winbindd_cli_state state;
967         struct winbindd_domain *domain;
968
969         if (child->domain) {
970                 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
971                            child->domain->name));
972         } else {
973                 DEBUG(10, ("fork_domain_child called without domain.\n"));
974         }
975
976         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
977                 DEBUG(0, ("Could not open child pipe: %s\n",
978                           strerror(errno)));
979                 return False;
980         }
981
982         ZERO_STRUCT(state);
983         state.pid = sys_getpid();
984
985         child->pid = sys_fork();
986
987         if (child->pid == -1) {
988                 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
989                 return False;
990         }
991
992         if (child->pid != 0) {
993                 /* Parent */
994                 close(fdpair[0]);
995                 child->next = child->prev = NULL;
996                 DLIST_ADD(children, child);
997                 child->event.fd = fdpair[1];
998                 child->event.flags = 0;
999                 child->requests = NULL;
1000                 add_fd_event(&child->event);
1001                 return True;
1002         }
1003
1004         /* Child */
1005
1006         /* Stop zombies in children */
1007         CatchChild();
1008
1009         state.sock = fdpair[0];
1010         close(fdpair[1]);
1011
1012         if (!reinit_after_fork(winbind_messaging_context(), true)) {
1013                 DEBUG(0,("reinit_after_fork() failed\n"));
1014                 _exit(0);
1015         }
1016
1017         close_conns_after_fork();
1018
1019         if (!override_logfile) {
1020                 lp_set_logfile(child->logfilename);
1021                 reopen_logs();
1022         }
1023
1024         /*
1025          * For clustering, we need to re-init our ctdbd connection after the
1026          * fork
1027          */
1028         if (!NT_STATUS_IS_OK(messaging_reinit(winbind_messaging_context())))
1029                 exit(1);
1030
1031         /* Don't handle the same messages as our parent. */
1032         messaging_deregister(winbind_messaging_context(),
1033                              MSG_SMB_CONF_UPDATED, NULL);
1034         messaging_deregister(winbind_messaging_context(),
1035                              MSG_SHUTDOWN, NULL);
1036         messaging_deregister(winbind_messaging_context(),
1037                              MSG_WINBIND_OFFLINE, NULL);
1038         messaging_deregister(winbind_messaging_context(),
1039                              MSG_WINBIND_ONLINE, NULL);
1040         messaging_deregister(winbind_messaging_context(),
1041                              MSG_WINBIND_ONLINESTATUS, NULL);
1042         messaging_deregister(winbind_messaging_context(),
1043                              MSG_DUMP_EVENT_LIST, NULL);
1044         messaging_deregister(winbind_messaging_context(),
1045                              MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1046
1047         /* Handle online/offline messages. */
1048         messaging_register(winbind_messaging_context(), NULL,
1049                            MSG_WINBIND_OFFLINE, child_msg_offline);
1050         messaging_register(winbind_messaging_context(), NULL,
1051                            MSG_WINBIND_ONLINE, child_msg_online);
1052         messaging_register(winbind_messaging_context(), NULL,
1053                            MSG_WINBIND_ONLINESTATUS, child_msg_onlinestatus);
1054         messaging_register(winbind_messaging_context(), NULL,
1055                            MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1056
1057         if ( child->domain ) {
1058                 child->domain->startup = True;
1059                 child->domain->startup_time = time(NULL);
1060         }
1061
1062         /* Ensure we have no pending check_online events other
1063            than one for this domain. */
1064
1065         for (domain = domain_list(); domain; domain = domain->next) {
1066                 if (domain != child->domain) {
1067                         TALLOC_FREE(domain->check_online_event);
1068                 }
1069         }
1070
1071         /* Ensure we're not handling an event inherited from
1072            our parent. */
1073
1074         cancel_named_event(winbind_event_context(),
1075                            "krb5_ticket_refresh_handler");
1076
1077         /* We might be in the idmap child...*/
1078         if (child->domain && !(child->domain->internal) &&
1079             lp_winbind_offline_logon()) {
1080
1081                 set_domain_online_request(child->domain);
1082
1083                 child->lockout_policy_event = event_add_timed(
1084                         winbind_event_context(), NULL, timeval_zero(),
1085                         "account_lockout_policy_handler",
1086                         account_lockout_policy_handler,
1087                         child);
1088         }
1089
1090         while (1) {
1091
1092                 int ret;
1093                 fd_set read_fds;
1094                 struct timeval t;
1095                 struct timeval *tp;
1096                 struct timeval now;
1097                 TALLOC_CTX *frame = talloc_stackframe();
1098
1099                 /* check for signals */
1100                 winbind_check_sigterm(false);
1101                 winbind_check_sighup();
1102
1103                 run_events(winbind_event_context(), 0, NULL, NULL);
1104
1105                 GetTimeOfDay(&now);
1106
1107                 if (child->domain && child->domain->startup &&
1108                                 (now.tv_sec > child->domain->startup_time + 30)) {
1109                         /* No longer in "startup" mode. */
1110                         DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1111                                 child->domain->name ));
1112                         child->domain->startup = False;
1113                 }
1114
1115                 tp = get_timed_events_timeout(winbind_event_context(), &t);
1116                 if (tp) {
1117                         DEBUG(11,("select will use timeout of %u.%u seconds\n",
1118                                 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1119                 }
1120
1121                 /* Handle messages */
1122
1123                 message_dispatch(winbind_messaging_context());
1124
1125                 FD_ZERO(&read_fds);
1126                 FD_SET(state.sock, &read_fds);
1127
1128                 ret = sys_select(state.sock + 1, &read_fds, NULL, NULL, tp);
1129
1130                 if (ret == 0) {
1131                         DEBUG(11,("nothing is ready yet, continue\n"));
1132                         TALLOC_FREE(frame);
1133                         continue;
1134                 }
1135
1136                 if (ret == -1 && errno == EINTR) {
1137                         /* We got a signal - continue. */
1138                         TALLOC_FREE(frame);
1139                         continue;
1140                 }
1141
1142                 if (ret == -1 && errno != EINTR) {
1143                         DEBUG(0,("select error occured\n"));
1144                         TALLOC_FREE(frame);
1145                         perror("select");
1146                         return False;
1147                 }
1148
1149                 /* fetch a request from the main daemon */
1150                 child_read_request(&state);
1151
1152                 if (state.finished) {
1153                         /* we lost contact with our parent */
1154                         exit(0);
1155                 }
1156
1157                 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
1158
1159                 ZERO_STRUCT(state.response);
1160                 state.request.null_term = '\0';
1161                 child_process_request(child, &state);
1162
1163                 SAFE_FREE(state.request.extra_data.data);
1164
1165                 cache_store_response(sys_getpid(), &state.response);
1166
1167                 SAFE_FREE(state.response.extra_data.data);
1168
1169                 /* We just send the result code back, the result
1170                  * structure needs to be fetched via the
1171                  * winbindd_cache. Hmm. That needs fixing... */
1172
1173                 if (write_data(state.sock,
1174                                (const char *)&state.response.result,
1175                                sizeof(state.response.result)) !=
1176                     sizeof(state.response.result)) {
1177                         DEBUG(0, ("Could not write result\n"));
1178                         exit(1);
1179                 }
1180                 TALLOC_FREE(frame);
1181         }
1182 }