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