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