c752ffeaac92527f117e5178d7fe7631cb696c9b
[mat/samba.git] / source3 / 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 #include "nsswitch/wb_reqtrans.h"
33 #include "secrets.h"
34 #include "../lib/util/select.h"
35 #include "../libcli/security/security.h"
36 #include "system/select.h"
37 #include "messages.h"
38 #include "../lib/util/tevent_unix.h"
39 #include "lib/param/loadparm.h"
40
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_WINBIND
43
44 extern bool override_logfile;
45 extern struct winbindd_methods cache_methods;
46
47 static struct winbindd_child *winbindd_children = NULL;
48
49 /* Read some data from a client connection */
50
51 static NTSTATUS child_read_request(struct winbindd_cli_state *state)
52 {
53         NTSTATUS status;
54
55         /* Read data */
56
57         status = read_data(state->sock, (char *)state->request,
58                            sizeof(*state->request));
59
60         if (!NT_STATUS_IS_OK(status)) {
61                 DEBUG(3, ("child_read_request: read_data failed: %s\n",
62                           nt_errstr(status)));
63                 return status;
64         }
65
66         if (state->request->extra_len == 0) {
67                 state->request->extra_data.data = NULL;
68                 return NT_STATUS_OK;
69         }
70
71         DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request->extra_len));
72
73         state->request->extra_data.data =
74                 SMB_MALLOC_ARRAY(char, state->request->extra_len + 1);
75
76         if (state->request->extra_data.data == NULL) {
77                 DEBUG(0, ("malloc failed\n"));
78                 return NT_STATUS_NO_MEMORY;
79         }
80
81         /* Ensure null termination */
82         state->request->extra_data.data[state->request->extra_len] = '\0';
83
84         status= read_data(state->sock, state->request->extra_data.data,
85                           state->request->extra_len);
86
87         if (!NT_STATUS_IS_OK(status)) {
88                 DEBUG(0, ("Could not read extra data: %s\n",
89                           nt_errstr(status)));
90         }
91         return status;
92 }
93
94 /*
95  * Do winbind child async request. This is not simply wb_simple_trans. We have
96  * to do the queueing ourselves because while a request is queued, the child
97  * might have crashed, and we have to re-fork it in the _trigger function.
98  */
99
100 struct wb_child_request_state {
101         struct tevent_context *ev;
102         struct winbindd_child *child;
103         struct winbindd_request *request;
104         struct winbindd_response *response;
105 };
106
107 static bool fork_domain_child(struct winbindd_child *child);
108
109 static void wb_child_request_trigger(struct tevent_req *req,
110                                             void *private_data);
111 static void wb_child_request_done(struct tevent_req *subreq);
112
113 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
114                                          struct tevent_context *ev,
115                                          struct winbindd_child *child,
116                                          struct winbindd_request *request)
117 {
118         struct tevent_req *req;
119         struct wb_child_request_state *state;
120
121         req = tevent_req_create(mem_ctx, &state,
122                                 struct wb_child_request_state);
123         if (req == NULL) {
124                 return NULL;
125         }
126
127         state->ev = ev;
128         state->child = child;
129         state->request = request;
130
131         if (!tevent_queue_add(child->queue, ev, req,
132                               wb_child_request_trigger, NULL)) {
133                 tevent_req_oom(req);
134                 return tevent_req_post(req, ev);
135         }
136         return req;
137 }
138
139 static void wb_child_request_trigger(struct tevent_req *req,
140                                      void *private_data)
141 {
142         struct wb_child_request_state *state = tevent_req_data(
143                 req, struct wb_child_request_state);
144         struct tevent_req *subreq;
145
146         if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
147                 tevent_req_error(req, errno);
148                 return;
149         }
150
151         subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
152                                       state->child->sock, state->request);
153         if (tevent_req_nomem(subreq, req)) {
154                 return;
155         }
156         tevent_req_set_callback(subreq, wb_child_request_done, req);
157         tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
158 }
159
160 static void wb_child_request_done(struct tevent_req *subreq)
161 {
162         struct tevent_req *req = tevent_req_callback_data(
163                 subreq, struct tevent_req);
164         struct wb_child_request_state *state = tevent_req_data(
165                 req, struct wb_child_request_state);
166         int ret, err;
167
168         ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
169         TALLOC_FREE(subreq);
170         if (ret == -1) {
171                 /*
172                  * The basic parent/child communication broke, close
173                  * our socket
174                  */
175                 close(state->child->sock);
176                 state->child->sock = -1;
177                 DLIST_REMOVE(winbindd_children, state->child);
178                 tevent_req_error(req, err);
179                 return;
180         }
181         tevent_req_done(req);
182 }
183
184 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
185                           struct winbindd_response **presponse, int *err)
186 {
187         struct wb_child_request_state *state = tevent_req_data(
188                 req, struct wb_child_request_state);
189
190         if (tevent_req_is_unix_error(req, err)) {
191                 return -1;
192         }
193         *presponse = talloc_move(mem_ctx, &state->response);
194         return 0;
195 }
196
197 static bool winbindd_child_busy(struct winbindd_child *child)
198 {
199         return tevent_queue_length(child->queue) > 0;
200 }
201
202 static struct winbindd_child *find_idle_child(struct winbindd_domain *domain)
203 {
204         int i;
205
206         for (i=0; i<lp_winbind_max_domain_connections(); i++) {
207                 if (!winbindd_child_busy(&domain->children[i])) {
208                         return &domain->children[i];
209                 }
210         }
211
212         return NULL;
213 }
214
215 struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
216 {
217         struct winbindd_child *result;
218
219         result = find_idle_child(domain);
220         if (result != NULL) {
221                 return result;
222         }
223         return &domain->children[rand() % lp_winbind_max_domain_connections()];
224 }
225
226 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
227 {
228         struct winbindd_child *child;
229
230         child = choose_domain_child(domain);
231         return child->binding_handle;
232 }
233
234 struct wb_domain_request_state {
235         struct tevent_context *ev;
236         struct winbindd_domain *domain;
237         struct winbindd_child *child;
238         struct winbindd_request *request;
239         struct winbindd_request *init_req;
240         struct winbindd_response *response;
241 };
242
243 static void wb_domain_request_gotdc(struct tevent_req *subreq);
244 static void wb_domain_request_initialized(struct tevent_req *subreq);
245 static void wb_domain_request_done(struct tevent_req *subreq);
246
247 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
248                                           struct tevent_context *ev,
249                                           struct winbindd_domain *domain,
250                                           struct winbindd_request *request)
251 {
252         struct tevent_req *req, *subreq;
253         struct wb_domain_request_state *state;
254
255         req = tevent_req_create(mem_ctx, &state,
256                                 struct wb_domain_request_state);
257         if (req == NULL) {
258                 return NULL;
259         }
260
261         state->child = choose_domain_child(domain);
262
263         if (domain->initialized) {
264                 subreq = wb_child_request_send(state, ev, state->child,
265                                                request);
266                 if (tevent_req_nomem(subreq, req)) {
267                         return tevent_req_post(req, ev);
268                 }
269                 tevent_req_set_callback(subreq, wb_domain_request_done, req);
270                 return req;
271         }
272
273         state->domain = domain;
274         state->ev = ev;
275         state->request = request;
276
277         state->init_req = talloc_zero(state, struct winbindd_request);
278         if (tevent_req_nomem(state->init_req, req)) {
279                 return tevent_req_post(req, ev);
280         }
281
282         if (IS_DC || domain->primary || domain->internal) {
283                 /* The primary domain has to find the DC name itself */
284                 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
285                 fstrcpy(state->init_req->domain_name, domain->name);
286                 state->init_req->data.init_conn.is_primary = domain->primary;
287                 fstrcpy(state->init_req->data.init_conn.dcname, "");
288
289                 subreq = wb_child_request_send(state, ev, state->child,
290                                                state->init_req);
291                 if (tevent_req_nomem(subreq, req)) {
292                         return tevent_req_post(req, ev);
293                 }
294                 tevent_req_set_callback(subreq, wb_domain_request_initialized,
295                                         req);
296                 return req;
297         }
298
299         /*
300          * Ask our DC for a DC name
301          */
302         domain = find_our_domain();
303
304         /* This is *not* the primary domain, let's ask our DC about a DC
305          * name */
306
307         state->init_req->cmd = WINBINDD_GETDCNAME;
308         fstrcpy(state->init_req->domain_name, domain->name);
309
310         subreq = wb_child_request_send(state, ev, state->child, request);
311         if (tevent_req_nomem(subreq, req)) {
312                 return tevent_req_post(req, ev);
313         }
314         tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
315         return req;
316 }
317
318 static void wb_domain_request_gotdc(struct tevent_req *subreq)
319 {
320         struct tevent_req *req = tevent_req_callback_data(
321                 subreq, struct tevent_req);
322         struct wb_domain_request_state *state = tevent_req_data(
323                 req, struct wb_domain_request_state);
324         struct winbindd_response *response;
325         int ret, err;
326
327         ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
328         TALLOC_FREE(subreq);
329         if (ret == -1) {
330                 tevent_req_error(req, err);
331                 return;
332         }
333         state->init_req->cmd = WINBINDD_INIT_CONNECTION;
334         fstrcpy(state->init_req->domain_name, state->domain->name);
335         state->init_req->data.init_conn.is_primary = False;
336         fstrcpy(state->init_req->data.init_conn.dcname,
337                 response->data.dc_name);
338
339         TALLOC_FREE(response);
340
341         subreq = wb_child_request_send(state, state->ev, state->child,
342                                        state->init_req);
343         if (tevent_req_nomem(subreq, req)) {
344                 return;
345         }
346         tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
347 }
348
349 static void wb_domain_request_initialized(struct tevent_req *subreq)
350 {
351         struct tevent_req *req = tevent_req_callback_data(
352                 subreq, struct tevent_req);
353         struct wb_domain_request_state *state = tevent_req_data(
354                 req, struct wb_domain_request_state);
355         struct winbindd_response *response;
356         int ret, err;
357
358         ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
359         TALLOC_FREE(subreq);
360         if (ret == -1) {
361                 tevent_req_error(req, err);
362                 return;
363         }
364
365         if (!string_to_sid(&state->domain->sid,
366                            response->data.domain_info.sid)) {
367                 DEBUG(1,("init_child_recv: Could not convert sid %s "
368                         "from string\n", response->data.domain_info.sid));
369                 tevent_req_error(req, EINVAL);
370                 return;
371         }
372
373         talloc_free(state->domain->name);
374         state->domain->name = talloc_strdup(state->domain,
375                                             response->data.domain_info.name);
376         if (state->domain->name == NULL) {
377                 tevent_req_error(req, ENOMEM);
378                 return;
379         }
380
381         talloc_free(state->domain->alt_name);
382         state->domain->alt_name = talloc_strdup(state->domain,
383                                                 response->data.domain_info.alt_name);
384         if (state->domain->alt_name == NULL) {
385                 tevent_req_error(req, ENOMEM);
386                 return;
387         }
388
389         state->domain->native_mode = response->data.domain_info.native_mode;
390         state->domain->active_directory =
391                 response->data.domain_info.active_directory;
392         state->domain->initialized = true;
393
394         TALLOC_FREE(response);
395
396         subreq = wb_child_request_send(state, state->ev, state->child,
397                                        state->request);
398         if (tevent_req_nomem(subreq, req)) {
399                 return;
400         }
401         tevent_req_set_callback(subreq, wb_domain_request_done, req);
402 }
403
404 static void wb_domain_request_done(struct tevent_req *subreq)
405 {
406         struct tevent_req *req = tevent_req_callback_data(
407                 subreq, struct tevent_req);
408         struct wb_domain_request_state *state = tevent_req_data(
409                 req, struct wb_domain_request_state);
410         int ret, err;
411
412         ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
413                                     &err);
414         TALLOC_FREE(subreq);
415         if (ret == -1) {
416                 tevent_req_error(req, err);
417                 return;
418         }
419         tevent_req_done(req);
420 }
421
422 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
423                            struct winbindd_response **presponse, int *err)
424 {
425         struct wb_domain_request_state *state = tevent_req_data(
426                 req, struct wb_domain_request_state);
427
428         if (tevent_req_is_unix_error(req, err)) {
429                 return -1;
430         }
431         *presponse = talloc_move(mem_ctx, &state->response);
432         return 0;
433 }
434
435 static void child_process_request(struct winbindd_child *child,
436                                   struct winbindd_cli_state *state)
437 {
438         struct winbindd_domain *domain = child->domain;
439         const struct winbindd_child_dispatch_table *table = child->table;
440
441         /* Free response data - we may be interrupted and receive another
442            command before being able to send this data off. */
443
444         state->response->result = WINBINDD_ERROR;
445         state->response->length = sizeof(struct winbindd_response);
446
447         /* as all requests in the child are sync, we can use talloc_tos() */
448         state->mem_ctx = talloc_tos();
449
450         /* Process command */
451
452         for (; table->name; table++) {
453                 if (state->request->cmd == table->struct_cmd) {
454                         DEBUG(10,("child_process_request: request fn %s\n",
455                                   table->name));
456                         state->response->result = table->struct_fn(domain, state);
457                         return;
458                 }
459         }
460
461         DEBUG(1, ("child_process_request: unknown request fn number %d\n",
462                   (int)state->request->cmd));
463         state->response->result = WINBINDD_ERROR;
464 }
465
466 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
467                  const struct winbindd_child_dispatch_table *table,
468                  const char *logprefix,
469                  const char *logname)
470 {
471         if (logprefix && logname) {
472                 char *logbase = NULL;
473
474                 if (*lp_logfile(talloc_tos())) {
475                         char *end = NULL;
476
477                         if (asprintf(&logbase, "%s", lp_logfile(talloc_tos())) < 0) {
478                                 smb_panic("Internal error: asprintf failed");
479                         }
480
481                         if ((end = strrchr_m(logbase, '/'))) {
482                                 *end = '\0';
483                         }
484                 } else {
485                         if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
486                                 smb_panic("Internal error: asprintf failed");
487                         }
488                 }
489
490                 if (asprintf(&child->logfilename, "%s/%s-%s",
491                              logbase, logprefix, logname) < 0) {
492                         SAFE_FREE(logbase);
493                         smb_panic("Internal error: asprintf failed");
494                 }
495
496                 SAFE_FREE(logbase);
497         } else {
498                 smb_panic("Internal error: logprefix == NULL && "
499                           "logname == NULL");
500         }
501
502         child->sock = -1;
503         child->domain = domain;
504         child->table = table;
505         child->queue = tevent_queue_create(NULL, "winbind_child");
506         SMB_ASSERT(child->queue != NULL);
507         child->binding_handle = wbint_binding_handle(NULL, domain, child);
508         SMB_ASSERT(child->binding_handle != NULL);
509 }
510
511 void winbind_child_died(pid_t pid)
512 {
513         struct winbindd_child *child;
514
515         for (child = winbindd_children; child != NULL; child = child->next) {
516                 if (child->pid == pid) {
517                         break;
518                 }
519         }
520
521         if (child == NULL) {
522                 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
523                 return;
524         }
525
526         /* This will be re-added in fork_domain_child() */
527
528         DLIST_REMOVE(winbindd_children, child);
529         child->pid = 0;
530
531         if (child->sock != -1) {
532                 close(child->sock);
533                 child->sock = -1;
534         }
535 }
536
537 /* Ensure any negative cache entries with the netbios or realm names are removed. */
538
539 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
540 {
541         flush_negative_conn_cache_for_domain(domain->name);
542         if (*domain->alt_name) {
543                 flush_negative_conn_cache_for_domain(domain->alt_name);
544         }
545 }
546
547 /* 
548  * Parent winbindd process sets its own debug level first and then
549  * sends a message to all the winbindd children to adjust their debug
550  * level to that of parents.
551  */
552
553 void winbind_msg_debug(struct messaging_context *msg_ctx,
554                          void *private_data,
555                          uint32_t msg_type,
556                          struct server_id server_id,
557                          DATA_BLOB *data)
558 {
559         struct winbindd_child *child;
560
561         DEBUG(10,("winbind_msg_debug: got debug message.\n"));
562
563         debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
564
565         for (child = winbindd_children; child != NULL; child = child->next) {
566
567                 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
568                         (unsigned int)child->pid));
569
570                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
571                            MSG_DEBUG,
572                            data->data,
573                            strlen((char *) data->data) + 1);
574         }
575 }
576
577 /* Set our domains as offline and forward the offline message to our children. */
578
579 void winbind_msg_offline(struct messaging_context *msg_ctx,
580                          void *private_data,
581                          uint32_t msg_type,
582                          struct server_id server_id,
583                          DATA_BLOB *data)
584 {
585         struct winbindd_child *child;
586         struct winbindd_domain *domain;
587
588         DEBUG(10,("winbind_msg_offline: got offline message.\n"));
589
590         if (!lp_winbind_offline_logon()) {
591                 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
592                 return;
593         }
594
595         /* Set our global state as offline. */
596         if (!set_global_winbindd_state_offline()) {
597                 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
598                 return;
599         }
600
601         /* Set all our domains as offline. */
602         for (domain = domain_list(); domain; domain = domain->next) {
603                 if (domain->internal) {
604                         continue;
605                 }
606                 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
607                 set_domain_offline(domain);
608         }
609
610         for (child = winbindd_children; child != NULL; child = child->next) {
611                 /* Don't send message to internal children.  We've already
612                    done so above. */
613                 if (!child->domain || winbindd_internal_child(child)) {
614                         continue;
615                 }
616
617                 /* Or internal domains (this should not be possible....) */
618                 if (child->domain->internal) {
619                         continue;
620                 }
621
622                 /* Each winbindd child should only process requests for one domain - make sure
623                    we only set it online / offline for that domain. */
624
625                 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
626                         (unsigned int)child->pid, domain->name ));
627
628                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
629                                    MSG_WINBIND_OFFLINE,
630                                    (const uint8_t *)child->domain->name,
631                                    strlen(child->domain->name)+1);
632         }
633 }
634
635 /* Set our domains as online and forward the online message to our children. */
636
637 void winbind_msg_online(struct messaging_context *msg_ctx,
638                         void *private_data,
639                         uint32_t msg_type,
640                         struct server_id server_id,
641                         DATA_BLOB *data)
642 {
643         struct winbindd_child *child;
644         struct winbindd_domain *domain;
645
646         DEBUG(10,("winbind_msg_online: got online message.\n"));
647
648         if (!lp_winbind_offline_logon()) {
649                 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
650                 return;
651         }
652
653         /* Set our global state as online. */
654         set_global_winbindd_state_online();
655
656         smb_nscd_flush_user_cache();
657         smb_nscd_flush_group_cache();
658
659         /* Set all our domains as online. */
660         for (domain = domain_list(); domain; domain = domain->next) {
661                 if (domain->internal) {
662                         continue;
663                 }
664                 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
665
666                 winbindd_flush_negative_conn_cache(domain);
667                 set_domain_online_request(domain);
668
669                 /* Send an online message to the idmap child when our
670                    primary domain comes back online */
671
672                 if ( domain->primary ) {
673                         struct winbindd_child *idmap = idmap_child();
674
675                         if ( idmap->pid != 0 ) {
676                                 messaging_send_buf(msg_ctx,
677                                                    pid_to_procid(idmap->pid), 
678                                                    MSG_WINBIND_ONLINE,
679                                                    (const uint8_t *)domain->name,
680                                                    strlen(domain->name)+1);
681                         }
682                 }
683         }
684
685         for (child = winbindd_children; child != NULL; child = child->next) {
686                 /* Don't send message to internal childs. */
687                 if (!child->domain || winbindd_internal_child(child)) {
688                         continue;
689                 }
690
691                 /* Or internal domains (this should not be possible....) */
692                 if (child->domain->internal) {
693                         continue;
694                 }
695
696                 /* Each winbindd child should only process requests for one domain - make sure
697                    we only set it online / offline for that domain. */
698
699                 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
700                         (unsigned int)child->pid, child->domain->name ));
701
702                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
703                                    MSG_WINBIND_ONLINE,
704                                    (const uint8_t *)child->domain->name,
705                                    strlen(child->domain->name)+1);
706         }
707 }
708
709 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
710 {
711         struct winbindd_domain *domain;
712         char *buf = NULL;
713
714         if ((buf = talloc_asprintf(mem_ctx, "global:%s ", 
715                                    get_global_winbindd_state_offline() ? 
716                                    "Offline":"Online")) == NULL) {
717                 return NULL;
718         }
719
720         for (domain = domain_list(); domain; domain = domain->next) {
721                 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ", 
722                                                   domain->name, 
723                                                   domain->online ?
724                                                   "Online":"Offline")) == NULL) {
725                         return NULL;
726                 }
727         }
728
729         buf = talloc_asprintf_append_buffer(buf, "\n");
730
731         DEBUG(5,("collect_onlinestatus: %s", buf));
732
733         return buf;
734 }
735
736 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
737                               void *private_data,
738                               uint32_t msg_type,
739                               struct server_id server_id,
740                               DATA_BLOB *data)
741 {
742         TALLOC_CTX *mem_ctx;
743         const char *message;
744         struct server_id *sender;
745
746         DEBUG(5,("winbind_msg_onlinestatus received.\n"));
747
748         if (!data->data) {
749                 return;
750         }
751
752         sender = (struct server_id *)data->data;
753
754         mem_ctx = talloc_init("winbind_msg_onlinestatus");
755         if (mem_ctx == NULL) {
756                 return;
757         }
758
759         message = collect_onlinestatus(mem_ctx);
760         if (message == NULL) {
761                 talloc_destroy(mem_ctx);
762                 return;
763         }
764
765         messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS, 
766                            (const uint8 *)message, strlen(message) + 1);
767
768         talloc_destroy(mem_ctx);
769 }
770
771 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
772                                  void *private_data,
773                                  uint32_t msg_type,
774                                  struct server_id server_id,
775                                  DATA_BLOB *data)
776 {
777         struct winbindd_child *child;
778
779         DEBUG(10,("winbind_msg_dump_event_list received\n"));
780
781         dump_event_list(winbind_event_context());
782
783         for (child = winbindd_children; child != NULL; child = child->next) {
784
785                 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
786                         (unsigned int)child->pid));
787
788                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
789                                    MSG_DUMP_EVENT_LIST,
790                                    NULL, 0);
791         }
792
793 }
794
795 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
796                                   void *private_data,
797                                   uint32_t msg_type,
798                                   struct server_id server_id,
799                                   DATA_BLOB *data)
800 {
801         TALLOC_CTX *mem_ctx;
802         const char *message = NULL;
803         struct server_id *sender = NULL;
804         const char *domain = NULL;
805         char *s = NULL;
806         NTSTATUS status;
807         struct winbindd_domain *dom = NULL;
808
809         DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
810
811         if (!data || !data->data) {
812                 return;
813         }
814
815         if (data->length < sizeof(struct server_id)) {
816                 return;
817         }
818
819         mem_ctx = talloc_init("winbind_msg_dump_domain_list");
820         if (!mem_ctx) {
821                 return;
822         }
823
824         sender = (struct server_id *)data->data;
825         if (data->length > sizeof(struct server_id)) {
826                 domain = (const char *)data->data+sizeof(struct server_id);
827         }
828
829         if (domain) {
830
831                 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
832                         domain));
833
834                 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
835                                                   find_domain_from_name_noinit(domain));
836                 if (!message) {
837                         talloc_destroy(mem_ctx);
838                         return;
839                 }
840
841                 messaging_send_buf(msg_ctx, *sender,
842                                    MSG_WINBIND_DUMP_DOMAIN_LIST,
843                                    (const uint8_t *)message, strlen(message) + 1);
844
845                 talloc_destroy(mem_ctx);
846
847                 return;
848         }
849
850         DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
851
852         for (dom = domain_list(); dom; dom=dom->next) {
853                 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
854                 if (!message) {
855                         talloc_destroy(mem_ctx);
856                         return;
857                 }
858
859                 s = talloc_asprintf_append(s, "%s\n", message);
860                 if (!s) {
861                         talloc_destroy(mem_ctx);
862                         return;
863                 }
864         }
865
866         status = messaging_send_buf(msg_ctx, *sender,
867                                     MSG_WINBIND_DUMP_DOMAIN_LIST,
868                                     (uint8_t *)s, strlen(s) + 1);
869         if (!NT_STATUS_IS_OK(status)) {
870                 DEBUG(0,("failed to send message: %s\n",
871                 nt_errstr(status)));
872         }
873
874         talloc_destroy(mem_ctx);
875 }
876
877 static void account_lockout_policy_handler(struct tevent_context *ctx,
878                                            struct tevent_timer *te,
879                                            struct timeval now,
880                                            void *private_data)
881 {
882         struct winbindd_child *child =
883                 (struct winbindd_child *)private_data;
884         TALLOC_CTX *mem_ctx = NULL;
885         struct winbindd_methods *methods;
886         struct samr_DomInfo12 lockout_policy;
887         NTSTATUS result;
888
889         DEBUG(10,("account_lockout_policy_handler called\n"));
890
891         TALLOC_FREE(child->lockout_policy_event);
892
893         if ( !winbindd_can_contact_domain( child->domain ) ) {
894                 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
895                           "do not have an incoming trust to domain %s\n", 
896                           child->domain->name));
897
898                 return;         
899         }
900
901         methods = child->domain->methods;
902
903         mem_ctx = talloc_init("account_lockout_policy_handler ctx");
904         if (!mem_ctx) {
905                 result = NT_STATUS_NO_MEMORY;
906         } else {
907                 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
908         }
909         TALLOC_FREE(mem_ctx);
910
911         if (!NT_STATUS_IS_OK(result)) {
912                 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
913                          nt_errstr(result)));
914         }
915
916         child->lockout_policy_event = tevent_add_timer(winbind_event_context(), NULL,
917                                                       timeval_current_ofs(3600, 0),
918                                                       account_lockout_policy_handler,
919                                                       child);
920 }
921
922 static time_t get_machine_password_timeout(void)
923 {
924         /* until we have gpo support use lp setting */
925         return lp_machine_password_timeout();
926 }
927
928 static bool calculate_next_machine_pwd_change(const char *domain,
929                                               struct timeval *t)
930 {
931         time_t pass_last_set_time;
932         time_t timeout;
933         time_t next_change;
934         struct timeval tv;
935         char *pw;
936
937         pw = secrets_fetch_machine_password(domain,
938                                             &pass_last_set_time,
939                                             NULL);
940
941         if (pw == NULL) {
942                 DEBUG(0,("cannot fetch own machine password ????"));
943                 return false;
944         }
945
946         SAFE_FREE(pw);
947
948         timeout = get_machine_password_timeout();
949         if (timeout == 0) {
950                 DEBUG(10,("machine password never expires\n"));
951                 return false;
952         }
953
954         tv.tv_sec = pass_last_set_time;
955         DEBUG(10, ("password last changed %s\n",
956                    timeval_string(talloc_tos(), &tv, false)));
957         tv.tv_sec += timeout;
958         DEBUGADD(10, ("password valid until %s\n",
959                       timeval_string(talloc_tos(), &tv, false)));
960
961         if (time(NULL) < (pass_last_set_time + timeout)) {
962                 next_change = pass_last_set_time + timeout;
963                 DEBUG(10,("machine password still valid until: %s\n",
964                         http_timestring(talloc_tos(), next_change)));
965                 *t = timeval_set(next_change, 0);
966
967                 if (lp_clustering()) {
968                         uint8_t randbuf;
969                         /*
970                          * When having a cluster, we have several
971                          * winbinds racing for the password change. In
972                          * the machine_password_change_handler()
973                          * function we check if someone else was
974                          * faster when the event triggers. We add a
975                          * 255-second random delay here, so that we
976                          * don't run to change the password at the
977                          * exact same moment.
978                          */
979                         generate_random_buffer(&randbuf, sizeof(randbuf));
980                         DEBUG(10, ("adding %d seconds randomness\n",
981                                    (int)randbuf));
982                         t->tv_sec += randbuf;
983                 }
984                 return true;
985         }
986
987         DEBUG(10,("machine password expired, needs immediate change\n"));
988
989         *t = timeval_zero();
990
991         return true;
992 }
993
994 static void machine_password_change_handler(struct tevent_context *ctx,
995                                             struct tevent_timer *te,
996                                             struct timeval now,
997                                             void *private_data)
998 {
999         struct winbindd_child *child =
1000                 (struct winbindd_child *)private_data;
1001         struct rpc_pipe_client *netlogon_pipe = NULL;
1002         TALLOC_CTX *frame;
1003         NTSTATUS result;
1004         struct timeval next_change;
1005
1006         DEBUG(10,("machine_password_change_handler called\n"));
1007
1008         TALLOC_FREE(child->machine_password_change_event);
1009
1010         if (!calculate_next_machine_pwd_change(child->domain->name,
1011                                                &next_change)) {
1012                 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1013                 return;
1014         }
1015
1016         DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1017                    timeval_string(talloc_tos(), &next_change, false)));
1018
1019         if (!timeval_expired(&next_change)) {
1020                 DEBUG(10, ("Someone else has already changed the pw\n"));
1021                 goto done;
1022         }
1023
1024         if (!winbindd_can_contact_domain(child->domain)) {
1025                 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1026                           "do not have an incoming trust to domain %s\n",
1027                           child->domain->name));
1028                 return;
1029         }
1030
1031         result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1032         if (!NT_STATUS_IS_OK(result)) {
1033                 DEBUG(10,("machine_password_change_handler: "
1034                         "failed to connect netlogon pipe: %s\n",
1035                          nt_errstr(result)));
1036                 return;
1037         }
1038
1039         frame = talloc_stackframe();
1040
1041         result = trust_pw_find_change_and_store_it(netlogon_pipe,
1042                                                    frame,
1043                                                    child->domain->name);
1044         TALLOC_FREE(frame);
1045
1046         DEBUG(10, ("machine_password_change_handler: "
1047                    "trust_pw_find_change_and_store_it returned %s\n",
1048                    nt_errstr(result)));
1049
1050         if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1051                 DEBUG(3,("machine_password_change_handler: password set returned "
1052                          "ACCESS_DENIED.  Maybe the trust account "
1053                          "password was changed and we didn't know it. "
1054                          "Killing connections to domain %s\n",
1055                          child->domain->name));
1056                 TALLOC_FREE(child->domain->conn.netlogon_pipe);
1057         }
1058
1059         if (!calculate_next_machine_pwd_change(child->domain->name,
1060                                                &next_change)) {
1061                 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1062                 return;
1063         }
1064
1065         DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1066                    timeval_string(talloc_tos(), &next_change, false)));
1067
1068         if (!NT_STATUS_IS_OK(result)) {
1069                 struct timeval tmp;
1070                 /*
1071                  * In case of failure, give the DC a minute to recover
1072                  */
1073                 tmp = timeval_current_ofs(60, 0);
1074                 next_change = timeval_max(&next_change, &tmp);
1075         }
1076
1077 done:
1078         child->machine_password_change_event = tevent_add_timer(winbind_event_context(), NULL,
1079                                                               next_change,
1080                                                               machine_password_change_handler,
1081                                                               child);
1082 }
1083
1084 /* Deal with a request to go offline. */
1085
1086 static void child_msg_offline(struct messaging_context *msg,
1087                               void *private_data,
1088                               uint32_t msg_type,
1089                               struct server_id server_id,
1090                               DATA_BLOB *data)
1091 {
1092         struct winbindd_domain *domain;
1093         struct winbindd_domain *primary_domain = NULL;
1094         const char *domainname = (const char *)data->data;
1095
1096         if (data->data == NULL || data->length == 0) {
1097                 return;
1098         }
1099
1100         DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1101
1102         if (!lp_winbind_offline_logon()) {
1103                 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1104                 return;
1105         }
1106
1107         primary_domain = find_our_domain();
1108
1109         /* Mark the requested domain offline. */
1110
1111         for (domain = domain_list(); domain; domain = domain->next) {
1112                 if (domain->internal) {
1113                         continue;
1114                 }
1115                 if (strequal(domain->name, domainname)) {
1116                         DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1117                         set_domain_offline(domain);
1118                         /* we are in the trusted domain, set the primary domain 
1119                          * offline too */
1120                         if (domain != primary_domain) {
1121                                 set_domain_offline(primary_domain);
1122                         }
1123                 }
1124         }
1125 }
1126
1127 /* Deal with a request to go online. */
1128
1129 static void child_msg_online(struct messaging_context *msg,
1130                              void *private_data,
1131                              uint32_t msg_type,
1132                              struct server_id server_id,
1133                              DATA_BLOB *data)
1134 {
1135         struct winbindd_domain *domain;
1136         struct winbindd_domain *primary_domain = NULL;
1137         const char *domainname = (const char *)data->data;
1138
1139         if (data->data == NULL || data->length == 0) {
1140                 return;
1141         }
1142
1143         DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1144
1145         if (!lp_winbind_offline_logon()) {
1146                 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1147                 return;
1148         }
1149
1150         primary_domain = find_our_domain();
1151
1152         /* Set our global state as online. */
1153         set_global_winbindd_state_online();
1154
1155         /* Try and mark everything online - delete any negative cache entries
1156            to force a reconnect now. */
1157
1158         for (domain = domain_list(); domain; domain = domain->next) {
1159                 if (domain->internal) {
1160                         continue;
1161                 }
1162                 if (strequal(domain->name, domainname)) {
1163                         DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1164                         winbindd_flush_negative_conn_cache(domain);
1165                         set_domain_online_request(domain);
1166
1167                         /* we can be in trusted domain, which will contact primary domain
1168                          * we have to bring primary domain online in trusted domain process
1169                          * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1170                          * --> contact_domain = find_our_domain()
1171                          * */
1172                         if (domain != primary_domain) {
1173                                 winbindd_flush_negative_conn_cache(primary_domain);
1174                                 set_domain_online_request(primary_domain);
1175                         }
1176                 }
1177         }
1178 }
1179
1180 static void child_msg_dump_event_list(struct messaging_context *msg,
1181                                       void *private_data,
1182                                       uint32_t msg_type,
1183                                       struct server_id server_id,
1184                                       DATA_BLOB *data)
1185 {
1186         DEBUG(5,("child_msg_dump_event_list received\n"));
1187
1188         dump_event_list(winbind_event_context());
1189 }
1190
1191 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1192                                     const char *logfilename)
1193 {
1194         struct winbindd_domain *domain;
1195         struct winbindd_child *cl;
1196         NTSTATUS status;
1197
1198         status = reinit_after_fork(
1199                 winbind_messaging_context(),
1200                 winbind_event_context(),
1201                 true);
1202         if (!NT_STATUS_IS_OK(status)) {
1203                 DEBUG(0,("reinit_after_fork() failed\n"));
1204                 return status;
1205         }
1206
1207         close_conns_after_fork();
1208
1209         if (!override_logfile && logfilename) {
1210                 lp_set_logfile(logfilename);
1211                 reopen_logs();
1212         }
1213
1214         if (!winbindd_setup_sig_term_handler(false))
1215                 return NT_STATUS_NO_MEMORY;
1216         if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1217                                             logfilename))
1218                 return NT_STATUS_NO_MEMORY;
1219
1220         /* Stop zombies in children */
1221         CatchChild();
1222
1223         /* Don't handle the same messages as our parent. */
1224         messaging_deregister(winbind_messaging_context(),
1225                              MSG_SMB_CONF_UPDATED, NULL);
1226         messaging_deregister(winbind_messaging_context(),
1227                              MSG_SHUTDOWN, NULL);
1228         messaging_deregister(winbind_messaging_context(),
1229                              MSG_WINBIND_OFFLINE, NULL);
1230         messaging_deregister(winbind_messaging_context(),
1231                              MSG_WINBIND_ONLINE, NULL);
1232         messaging_deregister(winbind_messaging_context(),
1233                              MSG_WINBIND_ONLINESTATUS, NULL);
1234         messaging_deregister(winbind_messaging_context(),
1235                              MSG_DUMP_EVENT_LIST, NULL);
1236         messaging_deregister(winbind_messaging_context(),
1237                              MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1238         messaging_deregister(winbind_messaging_context(),
1239                              MSG_DEBUG, NULL);
1240
1241         /* We have destroyed all events in the winbindd_event_context
1242          * in reinit_after_fork(), so clean out all possible pending
1243          * event pointers. */
1244
1245         /* Deal with check_online_events. */
1246
1247         for (domain = domain_list(); domain; domain = domain->next) {
1248                 TALLOC_FREE(domain->check_online_event);
1249         }
1250
1251         /* Ensure we're not handling a credential cache event inherited
1252          * from our parent. */
1253
1254         ccache_remove_all_after_fork();
1255
1256         /* Destroy all possible events in child list. */
1257         for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1258                 TALLOC_FREE(cl->lockout_policy_event);
1259                 TALLOC_FREE(cl->machine_password_change_event);
1260
1261                 /* Children should never be able to send
1262                  * each other messages, all messages must
1263                  * go through the parent.
1264                  */
1265                 cl->pid = (pid_t)0;
1266
1267                 /*
1268                  * Close service sockets to all other children
1269                  */
1270                 if ((cl != myself) && (cl->sock != -1)) {
1271                         close(cl->sock);
1272                         cl->sock = -1;
1273                 }
1274         }
1275         /*
1276          * This is a little tricky, children must not
1277          * send an MSG_WINBIND_ONLINE message to idmap_child().
1278          * If we are in a child of our primary domain or
1279          * in the process created by fork_child_dc_connect(),
1280          * and the primary domain cannot go online,
1281          * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1282          * periodically to idmap_child().
1283          *
1284          * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1285          * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1286          * ---> init_dc_connection() ---> cm_open_connection --->
1287          * set_domain_online(), sends MSG_WINBIND_ONLINE to
1288          * idmap_child(). Disallow children sending messages
1289          * to each other, all messages must go through the parent.
1290          */
1291         cl = idmap_child();
1292         cl->pid = (pid_t)0;
1293
1294         return NT_STATUS_OK;
1295 }
1296
1297 /*
1298  * In a child there will be only one domain, reference that here.
1299  */
1300 static struct winbindd_domain *child_domain;
1301
1302 struct winbindd_domain *wb_child_domain(void)
1303 {
1304         return child_domain;
1305 }
1306
1307 struct child_handler_state {
1308         struct winbindd_child *child;
1309         struct winbindd_cli_state cli;
1310 };
1311
1312 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1313                           uint16_t flags, void *private_data)
1314 {
1315         struct child_handler_state *state =
1316                 (struct child_handler_state *)private_data;
1317         NTSTATUS status;
1318         struct iovec iov[2];
1319         int iov_count;
1320
1321         /* fetch a request from the main daemon */
1322         status = child_read_request(&state->cli);
1323
1324         if (!NT_STATUS_IS_OK(status)) {
1325                 /* we lost contact with our parent */
1326                 _exit(0);
1327         }
1328
1329         DEBUG(4,("child daemon request %d\n",
1330                  (int)state->cli.request->cmd));
1331
1332         ZERO_STRUCTP(state->cli.response);
1333         state->cli.request->null_term = '\0';
1334         state->cli.mem_ctx = talloc_tos();
1335         child_process_request(state->child, &state->cli);
1336
1337         DEBUG(4, ("Finished processing child request %d\n",
1338                   (int)state->cli.request->cmd));
1339
1340         SAFE_FREE(state->cli.request->extra_data.data);
1341
1342         iov[0].iov_base = (void *)state->cli.response;
1343         iov[0].iov_len = sizeof(struct winbindd_response);
1344         iov_count = 1;
1345
1346         if (state->cli.response->length >
1347             sizeof(struct winbindd_response)) {
1348                 iov[1].iov_base =
1349                         (void *)state->cli.response->extra_data.data;
1350                 iov[1].iov_len = state->cli.response->length-iov[0].iov_len;
1351                 iov_count = 2;
1352         }
1353
1354         DEBUG(10, ("Writing %d bytes to parent\n",
1355                    (int)state->cli.response->length));
1356
1357         if (write_data_iov(state->cli.sock, iov, iov_count) !=
1358             state->cli.response->length) {
1359                 DEBUG(0, ("Could not write result\n"));
1360                 exit(1);
1361         }
1362 }
1363
1364 static bool fork_domain_child(struct winbindd_child *child)
1365 {
1366         int fdpair[2];
1367         struct child_handler_state state;
1368         struct winbindd_request request;
1369         struct winbindd_response response;
1370         struct winbindd_domain *primary_domain = NULL;
1371         NTSTATUS status;
1372         ssize_t nwritten;
1373         struct tevent_fd *fde;
1374
1375         if (child->domain) {
1376                 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1377                            child->domain->name));
1378         } else {
1379                 DEBUG(10, ("fork_domain_child called without domain.\n"));
1380         }
1381
1382         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1383                 DEBUG(0, ("Could not open child pipe: %s\n",
1384                           strerror(errno)));
1385                 return False;
1386         }
1387
1388         ZERO_STRUCT(state);
1389         state.child = child;
1390         state.cli.pid = getpid();
1391         state.cli.request = &request;
1392         state.cli.response = &response;
1393
1394         child->pid = fork();
1395
1396         if (child->pid == -1) {
1397                 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1398                 return False;
1399         }
1400
1401         if (child->pid != 0) {
1402                 /* Parent */
1403                 ssize_t nread;
1404
1405                 close(fdpair[0]);
1406
1407                 nread = sys_read(fdpair[1], &status, sizeof(status));
1408                 if (nread != sizeof(status)) {
1409                         DEBUG(1, ("fork_domain_child: Could not read child status: "
1410                                   "nread=%d, error=%s\n", (int)nread,
1411                                   strerror(errno)));
1412                         close(fdpair[1]);
1413                         return false;
1414                 }
1415                 if (!NT_STATUS_IS_OK(status)) {
1416                         DEBUG(1, ("fork_domain_child: Child status is %s\n",
1417                                   nt_errstr(status)));
1418                         close(fdpair[1]);
1419                         return false;
1420                 }
1421
1422                 child->next = child->prev = NULL;
1423                 DLIST_ADD(winbindd_children, child);
1424                 child->sock = fdpair[1];
1425                 return True;
1426         }
1427
1428         /* Child */
1429         child_domain = child->domain;
1430
1431         DEBUG(10, ("Child process %d\n", (int)getpid()));
1432
1433         state.cli.sock = fdpair[0];
1434         close(fdpair[1]);
1435
1436         status = winbindd_reinit_after_fork(child, child->logfilename);
1437
1438         nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1439         if (nwritten != sizeof(status)) {
1440                 DEBUG(1, ("fork_domain_child: Could not write status: "
1441                           "nwritten=%d, error=%s\n", (int)nwritten,
1442                           strerror(errno)));
1443                 _exit(0);
1444         }
1445         if (!NT_STATUS_IS_OK(status)) {
1446                 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1447                           nt_errstr(status)));
1448                 _exit(0);
1449         }
1450
1451         /* Handle online/offline messages. */
1452         messaging_register(winbind_messaging_context(), NULL,
1453                            MSG_WINBIND_OFFLINE, child_msg_offline);
1454         messaging_register(winbind_messaging_context(), NULL,
1455                            MSG_WINBIND_ONLINE, child_msg_online);
1456         messaging_register(winbind_messaging_context(), NULL,
1457                            MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1458         messaging_register(winbind_messaging_context(), NULL,
1459                            MSG_DEBUG, debug_message);
1460         messaging_register(winbind_messaging_context(), NULL,
1461                            MSG_WINBIND_IP_DROPPED,
1462                            winbind_msg_ip_dropped);
1463
1464
1465         primary_domain = find_our_domain();
1466
1467         if (primary_domain == NULL) {
1468                 smb_panic("no primary domain found");
1469         }
1470
1471         /* It doesn't matter if we allow cache login,
1472          * try to bring domain online after fork. */
1473         if ( child->domain ) {
1474                 child->domain->startup = True;
1475                 child->domain->startup_time = time_mono(NULL);
1476                 /* we can be in primary domain or in trusted domain
1477                  * If we are in trusted domain, set the primary domain
1478                  * in start-up mode */
1479                 if (!(child->domain->internal)) {
1480                         set_domain_online_request(child->domain);
1481                         if (!(child->domain->primary)) {
1482                                 primary_domain->startup = True;
1483                                 primary_domain->startup_time = time_mono(NULL);
1484                                 set_domain_online_request(primary_domain);
1485                         }
1486                 }
1487         }
1488
1489         /*
1490          * We are in idmap child, make sure that we set the
1491          * check_online_event to bring primary domain online.
1492          */
1493         if (child == idmap_child()) {
1494                 set_domain_online_request(primary_domain);
1495         }
1496
1497         /* We might be in the idmap child...*/
1498         if (child->domain && !(child->domain->internal) &&
1499             lp_winbind_offline_logon()) {
1500
1501                 set_domain_online_request(child->domain);
1502
1503                 if (primary_domain && (primary_domain != child->domain)) {
1504                         /* We need to talk to the primary
1505                          * domain as well as the trusted
1506                          * domain inside a trusted domain
1507                          * child.
1508                          * See the code in :
1509                          * set_dc_type_and_flags_trustinfo()
1510                          * for details.
1511                          */
1512                         set_domain_online_request(primary_domain);
1513                 }
1514
1515                 child->lockout_policy_event = tevent_add_timer(
1516                         winbind_event_context(), NULL, timeval_zero(),
1517                         account_lockout_policy_handler,
1518                         child);
1519         }
1520
1521         if (child->domain && child->domain->primary &&
1522             !USE_KERBEROS_KEYTAB &&
1523             lp_server_role() == ROLE_DOMAIN_MEMBER) {
1524
1525                 struct timeval next_change;
1526
1527                 if (calculate_next_machine_pwd_change(child->domain->name,
1528                                                        &next_change)) {
1529                         child->machine_password_change_event = tevent_add_timer(
1530                                 winbind_event_context(), NULL, next_change,
1531                                 machine_password_change_handler,
1532                                 child);
1533                 }
1534         }
1535
1536         fde = tevent_add_fd(winbind_event_context(), NULL, state.cli.sock,
1537                             TEVENT_FD_READ, child_handler, &state);
1538         if (fde == NULL) {
1539                 DEBUG(1, ("tevent_add_fd failed\n"));
1540                 _exit(1);
1541         }
1542
1543         while (1) {
1544
1545                 int ret;
1546                 TALLOC_CTX *frame = talloc_stackframe();
1547
1548                 ret = tevent_loop_once(winbind_event_context());
1549                 if (ret != 0) {
1550                         DEBUG(1, ("tevent_loop_once failed: %s\n",
1551                                   strerror(errno)));
1552                         _exit(1);
1553                 }
1554
1555                 if (child->domain && child->domain->startup &&
1556                                 (time_mono(NULL) > child->domain->startup_time + 30)) {
1557                         /* No longer in "startup" mode. */
1558                         DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1559                                 child->domain->name ));
1560                         child->domain->startup = False;
1561                 }
1562
1563                 TALLOC_FREE(frame);
1564         }
1565 }
1566
1567 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1568                                    void *private_data,
1569                                    uint32_t msg_type,
1570                                    struct server_id server_id,
1571                                    DATA_BLOB *data)
1572 {
1573         struct winbindd_child *child;
1574
1575         winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1576                                server_id, data);
1577
1578
1579         for (child = winbindd_children; child != NULL; child = child->next) {
1580                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1581                                    msg_type, data->data, data->length);
1582         }
1583 }