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