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