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