Merge branch 'master' of ssh://git.samba.org/data/git/samba
[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                 char *logbase = NULL;
409
410                 if (*lp_logfile()) {
411                         char *end = NULL;
412
413                         if (asprintf(&logbase, "%s", lp_logfile()) < 0) {
414                                 smb_panic("Internal error: asprintf failed");
415                         }
416
417                         if ((end = strrchr_m(logbase, '/'))) {
418                                 *end = '\0';
419                         }
420                 } else {
421                         if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
422                                 smb_panic("Internal error: asprintf failed");
423                         }
424                 }
425
426                 if (asprintf(&child->logfilename, "%s/%s-%s",
427                              logbase, logprefix, logname) < 0) {
428                         SAFE_FREE(logbase);
429                         smb_panic("Internal error: asprintf failed");
430                 }
431
432                 SAFE_FREE(logbase);
433         } else {
434                 smb_panic("Internal error: logprefix == NULL && "
435                           "logname == NULL");
436         }
437
438         child->domain = domain;
439         child->table = table;
440         child->queue = tevent_queue_create(NULL, "winbind_child");
441         SMB_ASSERT(child->queue != NULL);
442         child->rpccli = wbint_rpccli_create(NULL, domain, child);
443         SMB_ASSERT(child->rpccli != NULL);
444 }
445
446 static struct winbindd_child *winbindd_children = NULL;
447
448 void winbind_child_died(pid_t pid)
449 {
450         struct winbindd_child *child;
451
452         for (child = winbindd_children; child != NULL; child = child->next) {
453                 if (child->pid == pid) {
454                         break;
455                 }
456         }
457
458         if (child == NULL) {
459                 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
460                 return;
461         }
462
463         /* This will be re-added in fork_domain_child() */
464
465         DLIST_REMOVE(winbindd_children, child);
466
467         close(child->sock);
468         child->sock = -1;
469         child->pid = 0;
470 }
471
472 /* Ensure any negative cache entries with the netbios or realm names are removed. */
473
474 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
475 {
476         flush_negative_conn_cache_for_domain(domain->name);
477         if (*domain->alt_name) {
478                 flush_negative_conn_cache_for_domain(domain->alt_name);
479         }
480 }
481
482 /* 
483  * Parent winbindd process sets its own debug level first and then
484  * sends a message to all the winbindd children to adjust their debug
485  * level to that of parents.
486  */
487
488 void winbind_msg_debug(struct messaging_context *msg_ctx,
489                          void *private_data,
490                          uint32_t msg_type,
491                          struct server_id server_id,
492                          DATA_BLOB *data)
493 {
494         struct winbindd_child *child;
495
496         DEBUG(10,("winbind_msg_debug: got debug message.\n"));
497
498         debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
499
500         for (child = winbindd_children; child != NULL; child = child->next) {
501
502                 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
503                         (unsigned int)child->pid));
504
505                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
506                            MSG_DEBUG,
507                            data->data,
508                            strlen((char *) data->data) + 1);
509         }
510 }
511
512 /* Set our domains as offline and forward the offline message to our children. */
513
514 void winbind_msg_offline(struct messaging_context *msg_ctx,
515                          void *private_data,
516                          uint32_t msg_type,
517                          struct server_id server_id,
518                          DATA_BLOB *data)
519 {
520         struct winbindd_child *child;
521         struct winbindd_domain *domain;
522
523         DEBUG(10,("winbind_msg_offline: got offline message.\n"));
524
525         if (!lp_winbind_offline_logon()) {
526                 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
527                 return;
528         }
529
530         /* Set our global state as offline. */
531         if (!set_global_winbindd_state_offline()) {
532                 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
533                 return;
534         }
535
536         /* Set all our domains as offline. */
537         for (domain = domain_list(); domain; domain = domain->next) {
538                 if (domain->internal) {
539                         continue;
540                 }
541                 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
542                 set_domain_offline(domain);
543         }
544
545         for (child = winbindd_children; child != NULL; child = child->next) {
546                 /* Don't send message to internal children.  We've already
547                    done so above. */
548                 if (!child->domain || winbindd_internal_child(child)) {
549                         continue;
550                 }
551
552                 /* Or internal domains (this should not be possible....) */
553                 if (child->domain->internal) {
554                         continue;
555                 }
556
557                 /* Each winbindd child should only process requests for one domain - make sure
558                    we only set it online / offline for that domain. */
559
560                 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
561                         (unsigned int)child->pid, domain->name ));
562
563                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
564                                    MSG_WINBIND_OFFLINE,
565                                    (uint8 *)child->domain->name,
566                                    strlen(child->domain->name)+1);
567         }
568 }
569
570 /* Set our domains as online and forward the online message to our children. */
571
572 void winbind_msg_online(struct messaging_context *msg_ctx,
573                         void *private_data,
574                         uint32_t msg_type,
575                         struct server_id server_id,
576                         DATA_BLOB *data)
577 {
578         struct winbindd_child *child;
579         struct winbindd_domain *domain;
580
581         DEBUG(10,("winbind_msg_online: got online message.\n"));
582
583         if (!lp_winbind_offline_logon()) {
584                 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
585                 return;
586         }
587
588         /* Set our global state as online. */
589         set_global_winbindd_state_online();
590
591         smb_nscd_flush_user_cache();
592         smb_nscd_flush_group_cache();
593
594         /* Set all our domains as online. */
595         for (domain = domain_list(); domain; domain = domain->next) {
596                 if (domain->internal) {
597                         continue;
598                 }
599                 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
600
601                 winbindd_flush_negative_conn_cache(domain);
602                 set_domain_online_request(domain);
603
604                 /* Send an online message to the idmap child when our
605                    primary domain comes back online */
606
607                 if ( domain->primary ) {
608                         struct winbindd_child *idmap = idmap_child();
609
610                         if ( idmap->pid != 0 ) {
611                                 messaging_send_buf(msg_ctx,
612                                                    pid_to_procid(idmap->pid), 
613                                                    MSG_WINBIND_ONLINE,
614                                                    (uint8 *)domain->name,
615                                                    strlen(domain->name)+1);
616                         }
617                 }
618         }
619
620         for (child = winbindd_children; child != NULL; child = child->next) {
621                 /* Don't send message to internal childs. */
622                 if (!child->domain || winbindd_internal_child(child)) {
623                         continue;
624                 }
625
626                 /* Or internal domains (this should not be possible....) */
627                 if (child->domain->internal) {
628                         continue;
629                 }
630
631                 /* Each winbindd child should only process requests for one domain - make sure
632                    we only set it online / offline for that domain. */
633
634                 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
635                         (unsigned int)child->pid, child->domain->name ));
636
637                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
638                                    MSG_WINBIND_ONLINE,
639                                    (uint8 *)child->domain->name,
640                                    strlen(child->domain->name)+1);
641         }
642 }
643
644 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
645 {
646         struct winbindd_domain *domain;
647         char *buf = NULL;
648
649         if ((buf = talloc_asprintf(mem_ctx, "global:%s ", 
650                                    get_global_winbindd_state_offline() ? 
651                                    "Offline":"Online")) == NULL) {
652                 return NULL;
653         }
654
655         for (domain = domain_list(); domain; domain = domain->next) {
656                 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ", 
657                                                   domain->name, 
658                                                   domain->online ?
659                                                   "Online":"Offline")) == NULL) {
660                         return NULL;
661                 }
662         }
663
664         buf = talloc_asprintf_append_buffer(buf, "\n");
665
666         DEBUG(5,("collect_onlinestatus: %s", buf));
667
668         return buf;
669 }
670
671 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
672                               void *private_data,
673                               uint32_t msg_type,
674                               struct server_id server_id,
675                               DATA_BLOB *data)
676 {
677         TALLOC_CTX *mem_ctx;
678         const char *message;
679         struct server_id *sender;
680
681         DEBUG(5,("winbind_msg_onlinestatus received.\n"));
682
683         if (!data->data) {
684                 return;
685         }
686
687         sender = (struct server_id *)data->data;
688
689         mem_ctx = talloc_init("winbind_msg_onlinestatus");
690         if (mem_ctx == NULL) {
691                 return;
692         }
693
694         message = collect_onlinestatus(mem_ctx);
695         if (message == NULL) {
696                 talloc_destroy(mem_ctx);
697                 return;
698         }
699
700         messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS, 
701                            (uint8 *)message, strlen(message) + 1);
702
703         talloc_destroy(mem_ctx);
704 }
705
706 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
707                                  void *private_data,
708                                  uint32_t msg_type,
709                                  struct server_id server_id,
710                                  DATA_BLOB *data)
711 {
712         struct winbindd_child *child;
713
714         DEBUG(10,("winbind_msg_dump_event_list received\n"));
715
716         dump_event_list(winbind_event_context());
717
718         for (child = winbindd_children; child != NULL; child = child->next) {
719
720                 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
721                         (unsigned int)child->pid));
722
723                 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
724                                    MSG_DUMP_EVENT_LIST,
725                                    NULL, 0);
726         }
727
728 }
729
730 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
731                                   void *private_data,
732                                   uint32_t msg_type,
733                                   struct server_id server_id,
734                                   DATA_BLOB *data)
735 {
736         TALLOC_CTX *mem_ctx;
737         const char *message = NULL;
738         struct server_id *sender = NULL;
739         const char *domain = NULL;
740         char *s = NULL;
741         NTSTATUS status;
742         struct winbindd_domain *dom = NULL;
743
744         DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
745
746         if (!data || !data->data) {
747                 return;
748         }
749
750         if (data->length < sizeof(struct server_id)) {
751                 return;
752         }
753
754         mem_ctx = talloc_init("winbind_msg_dump_domain_list");
755         if (!mem_ctx) {
756                 return;
757         }
758
759         sender = (struct server_id *)data->data;
760         if (data->length > sizeof(struct server_id)) {
761                 domain = (const char *)data->data+sizeof(struct server_id);
762         }
763
764         if (domain) {
765
766                 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
767                         domain));
768
769                 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
770                                                   find_domain_from_name_noinit(domain));
771                 if (!message) {
772                         talloc_destroy(mem_ctx);
773                         return;
774                 }
775
776                 messaging_send_buf(msg_ctx, *sender,
777                                    MSG_WINBIND_DUMP_DOMAIN_LIST,
778                                    (uint8_t *)message, strlen(message) + 1);
779
780                 talloc_destroy(mem_ctx);
781
782                 return;
783         }
784
785         DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
786
787         for (dom = domain_list(); dom; dom=dom->next) {
788                 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
789                 if (!message) {
790                         talloc_destroy(mem_ctx);
791                         return;
792                 }
793
794                 s = talloc_asprintf_append(s, "%s\n", message);
795                 if (!s) {
796                         talloc_destroy(mem_ctx);
797                         return;
798                 }
799         }
800
801         status = messaging_send_buf(msg_ctx, *sender,
802                                     MSG_WINBIND_DUMP_DOMAIN_LIST,
803                                     (uint8_t *)s, strlen(s) + 1);
804         if (!NT_STATUS_IS_OK(status)) {
805                 DEBUG(0,("failed to send message: %s\n",
806                 nt_errstr(status)));
807         }
808
809         talloc_destroy(mem_ctx);
810 }
811
812 static void account_lockout_policy_handler(struct event_context *ctx,
813                                            struct timed_event *te,
814                                            struct timeval now,
815                                            void *private_data)
816 {
817         struct winbindd_child *child =
818                 (struct winbindd_child *)private_data;
819         TALLOC_CTX *mem_ctx = NULL;
820         struct winbindd_methods *methods;
821         struct samr_DomInfo12 lockout_policy;
822         NTSTATUS result;
823
824         DEBUG(10,("account_lockout_policy_handler called\n"));
825
826         TALLOC_FREE(child->lockout_policy_event);
827
828         if ( !winbindd_can_contact_domain( child->domain ) ) {
829                 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
830                           "do not have an incoming trust to domain %s\n", 
831                           child->domain->name));
832
833                 return;         
834         }
835
836         methods = child->domain->methods;
837
838         mem_ctx = talloc_init("account_lockout_policy_handler ctx");
839         if (!mem_ctx) {
840                 result = NT_STATUS_NO_MEMORY;
841         } else {
842                 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
843         }
844         TALLOC_FREE(mem_ctx);
845
846         if (!NT_STATUS_IS_OK(result)) {
847                 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
848                          nt_errstr(result)));
849         }
850
851         child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
852                                                       timeval_current_ofs(3600, 0),
853                                                       account_lockout_policy_handler,
854                                                       child);
855 }
856
857 static time_t get_machine_password_timeout(void)
858 {
859         /* until we have gpo support use lp setting */
860         return lp_machine_password_timeout();
861 }
862
863 static bool calculate_next_machine_pwd_change(const char *domain,
864                                               struct timeval *t)
865 {
866         time_t pass_last_set_time;
867         time_t timeout;
868         time_t next_change;
869         struct timeval tv;
870         char *pw;
871
872         pw = secrets_fetch_machine_password(domain,
873                                             &pass_last_set_time,
874                                             NULL);
875
876         if (pw == NULL) {
877                 DEBUG(0,("cannot fetch own machine password ????"));
878                 return false;
879         }
880
881         SAFE_FREE(pw);
882
883         timeout = get_machine_password_timeout();
884         if (timeout == 0) {
885                 DEBUG(10,("machine password never expires\n"));
886                 return false;
887         }
888
889         tv.tv_sec = pass_last_set_time;
890         DEBUG(10, ("password last changed %s\n",
891                    timeval_string(talloc_tos(), &tv, false)));
892         tv.tv_sec += timeout;
893         DEBUGADD(10, ("password valid until %s\n",
894                       timeval_string(talloc_tos(), &tv, false)));
895
896         if (time(NULL) < (pass_last_set_time + timeout)) {
897                 next_change = pass_last_set_time + timeout;
898                 DEBUG(10,("machine password still valid until: %s\n",
899                         http_timestring(talloc_tos(), next_change)));
900                 *t = timeval_set(next_change, 0);
901
902                 if (lp_clustering()) {
903                         uint8_t randbuf;
904                         /*
905                          * When having a cluster, we have several
906                          * winbinds racing for the password change. In
907                          * the machine_password_change_handler()
908                          * function we check if someone else was
909                          * faster when the event triggers. We add a
910                          * 255-second random delay here, so that we
911                          * don't run to change the password at the
912                          * exact same moment.
913                          */
914                         generate_random_buffer(&randbuf, sizeof(randbuf));
915                         DEBUG(10, ("adding %d seconds randomness\n",
916                                    (int)randbuf));
917                         t->tv_sec += randbuf;
918                 }
919                 return true;
920         }
921
922         DEBUG(10,("machine password expired, needs immediate change\n"));
923
924         *t = timeval_zero();
925
926         return true;
927 }
928
929 static void machine_password_change_handler(struct event_context *ctx,
930                                             struct timed_event *te,
931                                             struct timeval now,
932                                             void *private_data)
933 {
934         struct winbindd_child *child =
935                 (struct winbindd_child *)private_data;
936         struct rpc_pipe_client *netlogon_pipe = NULL;
937         TALLOC_CTX *frame;
938         NTSTATUS result;
939         struct timeval next_change;
940
941         DEBUG(10,("machine_password_change_handler called\n"));
942
943         TALLOC_FREE(child->machine_password_change_event);
944
945         if (!calculate_next_machine_pwd_change(child->domain->name,
946                                                &next_change)) {
947                 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
948                 return;
949         }
950
951         DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
952                    timeval_string(talloc_tos(), &next_change, false)));
953
954         if (!timeval_expired(&next_change)) {
955                 DEBUG(10, ("Someone else has already changed the pw\n"));
956                 goto done;
957         }
958
959         if (!winbindd_can_contact_domain(child->domain)) {
960                 DEBUG(10,("machine_password_change_handler: Removing myself since I "
961                           "do not have an incoming trust to domain %s\n",
962                           child->domain->name));
963                 return;
964         }
965
966         result = cm_connect_netlogon(child->domain, &netlogon_pipe);
967         if (!NT_STATUS_IS_OK(result)) {
968                 DEBUG(10,("machine_password_change_handler: "
969                         "failed to connect netlogon pipe: %s\n",
970                          nt_errstr(result)));
971                 return;
972         }
973
974         frame = talloc_stackframe();
975
976         result = trust_pw_find_change_and_store_it(netlogon_pipe,
977                                                    frame,
978                                                    child->domain->name);
979         TALLOC_FREE(frame);
980
981         DEBUG(10, ("machine_password_change_handler: "
982                    "trust_pw_find_change_and_store_it returned %s\n",
983                    nt_errstr(result)));
984
985         if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
986                 DEBUG(3,("machine_password_change_handler: password set returned "
987                          "ACCESS_DENIED.  Maybe the trust account "
988                          "password was changed and we didn't know it. "
989                          "Killing connections to domain %s\n",
990                          child->domain->name));
991                 TALLOC_FREE(child->domain->conn.netlogon_pipe);
992         }
993
994         if (!calculate_next_machine_pwd_change(child->domain->name,
995                                                &next_change)) {
996                 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
997                 return;
998         }
999
1000         DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1001                    timeval_string(talloc_tos(), &next_change, false)));
1002
1003         if (!NT_STATUS_IS_OK(result)) {
1004                 struct timeval tmp;
1005                 /*
1006                  * In case of failure, give the DC a minute to recover
1007                  */
1008                 tmp = timeval_current_ofs(60, 0);
1009                 next_change = timeval_max(&next_change, &tmp);
1010         }
1011
1012 done:
1013         child->machine_password_change_event = event_add_timed(winbind_event_context(), NULL,
1014                                                               next_change,
1015                                                               machine_password_change_handler,
1016                                                               child);
1017 }
1018
1019 /* Deal with a request to go offline. */
1020
1021 static void child_msg_offline(struct messaging_context *msg,
1022                               void *private_data,
1023                               uint32_t msg_type,
1024                               struct server_id server_id,
1025                               DATA_BLOB *data)
1026 {
1027         struct winbindd_domain *domain;
1028         struct winbindd_domain *primary_domain = NULL;
1029         const char *domainname = (const char *)data->data;
1030
1031         if (data->data == NULL || data->length == 0) {
1032                 return;
1033         }
1034
1035         DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1036
1037         if (!lp_winbind_offline_logon()) {
1038                 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1039                 return;
1040         }
1041
1042         primary_domain = find_our_domain();
1043
1044         /* Mark the requested domain offline. */
1045
1046         for (domain = domain_list(); domain; domain = domain->next) {
1047                 if (domain->internal) {
1048                         continue;
1049                 }
1050                 if (strequal(domain->name, domainname)) {
1051                         DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1052                         set_domain_offline(domain);
1053                         /* we are in the trusted domain, set the primary domain 
1054                          * offline too */
1055                         if (domain != primary_domain) {
1056                                 set_domain_offline(primary_domain);
1057                         }
1058                 }
1059         }
1060 }
1061
1062 /* Deal with a request to go online. */
1063
1064 static void child_msg_online(struct messaging_context *msg,
1065                              void *private_data,
1066                              uint32_t msg_type,
1067                              struct server_id server_id,
1068                              DATA_BLOB *data)
1069 {
1070         struct winbindd_domain *domain;
1071         struct winbindd_domain *primary_domain = NULL;
1072         const char *domainname = (const char *)data->data;
1073
1074         if (data->data == NULL || data->length == 0) {
1075                 return;
1076         }
1077
1078         DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1079
1080         if (!lp_winbind_offline_logon()) {
1081                 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1082                 return;
1083         }
1084
1085         primary_domain = find_our_domain();
1086
1087         /* Set our global state as online. */
1088         set_global_winbindd_state_online();
1089
1090         /* Try and mark everything online - delete any negative cache entries
1091            to force a reconnect now. */
1092
1093         for (domain = domain_list(); domain; domain = domain->next) {
1094                 if (domain->internal) {
1095                         continue;
1096                 }
1097                 if (strequal(domain->name, domainname)) {
1098                         DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1099                         winbindd_flush_negative_conn_cache(domain);
1100                         set_domain_online_request(domain);
1101
1102                         /* we can be in trusted domain, which will contact primary domain
1103                          * we have to bring primary domain online in trusted domain process
1104                          * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1105                          * --> contact_domain = find_our_domain()
1106                          * */
1107                         if (domain != primary_domain) {
1108                                 winbindd_flush_negative_conn_cache(primary_domain);
1109                                 set_domain_online_request(primary_domain);
1110                         }
1111                 }
1112         }
1113 }
1114
1115 static void child_msg_dump_event_list(struct messaging_context *msg,
1116                                       void *private_data,
1117                                       uint32_t msg_type,
1118                                       struct server_id server_id,
1119                                       DATA_BLOB *data)
1120 {
1121         DEBUG(5,("child_msg_dump_event_list received\n"));
1122
1123         dump_event_list(winbind_event_context());
1124 }
1125
1126 bool winbindd_reinit_after_fork(const char *logfilename)
1127 {
1128         struct winbindd_domain *domain;
1129         struct winbindd_child *cl;
1130         NTSTATUS status;
1131
1132         status = reinit_after_fork(
1133                 winbind_messaging_context(),
1134                 winbind_event_context(),
1135                 messaging_server_id(winbind_messaging_context()),
1136                 true);
1137         if (!NT_STATUS_IS_OK(status)) {
1138                 DEBUG(0,("reinit_after_fork() failed\n"));
1139                 return false;
1140         }
1141
1142         close_conns_after_fork();
1143
1144         if (!override_logfile && logfilename) {
1145                 lp_set_logfile(logfilename);
1146                 reopen_logs();
1147         }
1148
1149         if (!winbindd_setup_sig_term_handler(false))
1150                 return false;
1151         if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1152                                             logfilename))
1153                 return false;
1154
1155         /* Stop zombies in children */
1156         CatchChild();
1157
1158         /* Don't handle the same messages as our parent. */
1159         messaging_deregister(winbind_messaging_context(),
1160                              MSG_SMB_CONF_UPDATED, NULL);
1161         messaging_deregister(winbind_messaging_context(),
1162                              MSG_SHUTDOWN, NULL);
1163         messaging_deregister(winbind_messaging_context(),
1164                              MSG_WINBIND_OFFLINE, NULL);
1165         messaging_deregister(winbind_messaging_context(),
1166                              MSG_WINBIND_ONLINE, NULL);
1167         messaging_deregister(winbind_messaging_context(),
1168                              MSG_WINBIND_ONLINESTATUS, NULL);
1169         messaging_deregister(winbind_messaging_context(),
1170                              MSG_DUMP_EVENT_LIST, NULL);
1171         messaging_deregister(winbind_messaging_context(),
1172                              MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1173         messaging_deregister(winbind_messaging_context(),
1174                              MSG_DEBUG, NULL);
1175
1176         /* We have destroyed all events in the winbindd_event_context
1177          * in reinit_after_fork(), so clean out all possible pending
1178          * event pointers. */
1179
1180         /* Deal with check_online_events. */
1181
1182         for (domain = domain_list(); domain; domain = domain->next) {
1183                 TALLOC_FREE(domain->check_online_event);
1184         }
1185
1186         /* Ensure we're not handling a credential cache event inherited
1187          * from our parent. */
1188
1189         ccache_remove_all_after_fork();
1190
1191         /* Destroy all possible events in child list. */
1192         for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1193                 TALLOC_FREE(cl->lockout_policy_event);
1194                 TALLOC_FREE(cl->machine_password_change_event);
1195
1196                 /* Children should never be able to send
1197                  * each other messages, all messages must
1198                  * go through the parent.
1199                  */
1200                 cl->pid = (pid_t)0;
1201         }
1202         /*
1203          * This is a little tricky, children must not
1204          * send an MSG_WINBIND_ONLINE message to idmap_child().
1205          * If we are in a child of our primary domain or
1206          * in the process created by fork_child_dc_connect(),
1207          * and the primary domain cannot go online,
1208          * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1209          * periodically to idmap_child().
1210          *
1211          * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1212          * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1213          * ---> init_dc_connection() ---> cm_open_connection --->
1214          * set_domain_online(), sends MSG_WINBIND_ONLINE to
1215          * idmap_child(). Disallow children sending messages
1216          * to each other, all messages must go through the parent.
1217          */
1218         cl = idmap_child();
1219         cl->pid = (pid_t)0;
1220
1221         return true;
1222 }
1223
1224 /*
1225  * In a child there will be only one domain, reference that here.
1226  */
1227 static struct winbindd_domain *child_domain;
1228
1229 struct winbindd_domain *wb_child_domain(void)
1230 {
1231         return child_domain;
1232 }
1233
1234 static bool fork_domain_child(struct winbindd_child *child)
1235 {
1236         int fdpair[2];
1237         struct winbindd_cli_state state;
1238         struct winbindd_request request;
1239         struct winbindd_response response;
1240         struct winbindd_domain *primary_domain = NULL;
1241
1242         if (child->domain) {
1243                 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1244                            child->domain->name));
1245         } else {
1246                 DEBUG(10, ("fork_domain_child called without domain.\n"));
1247         }
1248
1249         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1250                 DEBUG(0, ("Could not open child pipe: %s\n",
1251                           strerror(errno)));
1252                 return False;
1253         }
1254
1255         ZERO_STRUCT(state);
1256         state.pid = sys_getpid();
1257         state.request = &request;
1258         state.response = &response;
1259
1260         child->pid = sys_fork();
1261
1262         if (child->pid == -1) {
1263                 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1264                 return False;
1265         }
1266
1267         if (child->pid != 0) {
1268                 /* Parent */
1269                 close(fdpair[0]);
1270                 child->next = child->prev = NULL;
1271                 DLIST_ADD(winbindd_children, child);
1272                 child->sock = fdpair[1];
1273                 return True;
1274         }
1275
1276         /* Child */
1277         child_domain = child->domain;
1278
1279         DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1280
1281         state.sock = fdpair[0];
1282         close(fdpair[1]);
1283
1284         if (!winbindd_reinit_after_fork(child->logfilename)) {
1285                 _exit(0);
1286         }
1287
1288         /* Handle online/offline messages. */
1289         messaging_register(winbind_messaging_context(), NULL,
1290                            MSG_WINBIND_OFFLINE, child_msg_offline);
1291         messaging_register(winbind_messaging_context(), NULL,
1292                            MSG_WINBIND_ONLINE, child_msg_online);
1293         messaging_register(winbind_messaging_context(), NULL,
1294                            MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1295         messaging_register(winbind_messaging_context(), NULL,
1296                            MSG_DEBUG, debug_message);
1297
1298         primary_domain = find_our_domain();
1299
1300         if (primary_domain == NULL) {
1301                 smb_panic("no primary domain found");
1302         }
1303
1304         /* It doesn't matter if we allow cache login,
1305          * try to bring domain online after fork. */
1306         if ( child->domain ) {
1307                 child->domain->startup = True;
1308                 child->domain->startup_time = time(NULL);
1309                 /* we can be in primary domain or in trusted domain
1310                  * If we are in trusted domain, set the primary domain
1311                  * in start-up mode */
1312                 if (!(child->domain->internal)) {
1313                         set_domain_online_request(child->domain);
1314                         if (!(child->domain->primary)) {
1315                                 primary_domain->startup = True;
1316                                 primary_domain->startup_time = time(NULL);
1317                                 set_domain_online_request(primary_domain);
1318                         }
1319                 }
1320         }
1321
1322         /*
1323          * We are in idmap child, make sure that we set the
1324          * check_online_event to bring primary domain online.
1325          */
1326         if (child == idmap_child()) {
1327                 set_domain_online_request(primary_domain);
1328         }
1329
1330         /* We might be in the idmap child...*/
1331         if (child->domain && !(child->domain->internal) &&
1332             lp_winbind_offline_logon()) {
1333
1334                 set_domain_online_request(child->domain);
1335
1336                 if (primary_domain && (primary_domain != child->domain)) {
1337                         /* We need to talk to the primary
1338                          * domain as well as the trusted
1339                          * domain inside a trusted domain
1340                          * child.
1341                          * See the code in :
1342                          * set_dc_type_and_flags_trustinfo()
1343                          * for details.
1344                          */
1345                         set_domain_online_request(primary_domain);
1346                 }
1347
1348                 child->lockout_policy_event = event_add_timed(
1349                         winbind_event_context(), NULL, timeval_zero(),
1350                         account_lockout_policy_handler,
1351                         child);
1352         }
1353
1354         if (child->domain && child->domain->primary &&
1355             !USE_KERBEROS_KEYTAB &&
1356             lp_server_role() == ROLE_DOMAIN_MEMBER) {
1357
1358                 struct timeval next_change;
1359
1360                 if (calculate_next_machine_pwd_change(child->domain->name,
1361                                                        &next_change)) {
1362                         child->machine_password_change_event = event_add_timed(
1363                                 winbind_event_context(), NULL, next_change,
1364                                 machine_password_change_handler,
1365                                 child);
1366                 }
1367         }
1368
1369         while (1) {
1370
1371                 int ret;
1372                 fd_set r_fds;
1373                 fd_set w_fds;
1374                 int maxfd;
1375                 struct timeval t;
1376                 struct timeval *tp;
1377                 struct timeval now;
1378                 TALLOC_CTX *frame = talloc_stackframe();
1379                 struct iovec iov[2];
1380                 int iov_count;
1381                 NTSTATUS status;
1382
1383                 if (run_events(winbind_event_context(), 0, NULL, NULL)) {
1384                         TALLOC_FREE(frame);
1385                         continue;
1386                 }
1387
1388                 GetTimeOfDay(&now);
1389
1390                 if (child->domain && child->domain->startup &&
1391                                 (now.tv_sec > child->domain->startup_time + 30)) {
1392                         /* No longer in "startup" mode. */
1393                         DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1394                                 child->domain->name ));
1395                         child->domain->startup = False;
1396                 }
1397
1398                 FD_ZERO(&r_fds);
1399                 FD_ZERO(&w_fds);
1400                 FD_SET(state.sock, &r_fds);
1401                 maxfd = state.sock;
1402
1403                 /*
1404                  * Initialize this high as event_add_to_select_args()
1405                  * uses a timeval_min() on this and next_event. Fix
1406                  * from Roel van Meer <rolek@alt001.com>.
1407                  */
1408                 t.tv_sec = 999999;
1409                 t.tv_usec = 0;
1410
1411                 event_add_to_select_args(winbind_event_context(), &now,
1412                                          &r_fds, &w_fds, &t, &maxfd);
1413                 tp = get_timed_events_timeout(winbind_event_context(), &t);
1414                 if (tp) {
1415                         DEBUG(11,("select will use timeout of %u.%u seconds\n",
1416                                 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1417                 }
1418
1419                 ret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, tp);
1420
1421                 if (run_events(winbind_event_context(), ret, &r_fds, &w_fds)) {
1422                         /* We got a signal - continue. */
1423                         TALLOC_FREE(frame);
1424                         continue;
1425                 }
1426
1427                 if (ret == 0) {
1428                         DEBUG(11,("nothing is ready yet, continue\n"));
1429                         TALLOC_FREE(frame);
1430                         continue;
1431                 }
1432
1433                 if (ret == -1 && errno == EINTR) {
1434                         /* We got a signal - continue. */
1435                         TALLOC_FREE(frame);
1436                         continue;
1437                 }
1438
1439                 if (ret == -1 && errno != EINTR) {
1440                         DEBUG(0,("select error occured\n"));
1441                         TALLOC_FREE(frame);
1442                         perror("select");
1443                         _exit(1);
1444                 }
1445
1446                 /* fetch a request from the main daemon */
1447                 status = child_read_request(&state);
1448
1449                 if (!NT_STATUS_IS_OK(status)) {
1450                         /* we lost contact with our parent */
1451                         _exit(0);
1452                 }
1453
1454                 DEBUG(4,("child daemon request %d\n", (int)state.request->cmd));
1455
1456                 ZERO_STRUCTP(state.response);
1457                 state.request->null_term = '\0';
1458                 state.mem_ctx = frame;
1459                 child_process_request(child, &state);
1460
1461                 DEBUG(4, ("Finished processing child request %d\n",
1462                           (int)state.request->cmd));
1463
1464                 SAFE_FREE(state.request->extra_data.data);
1465
1466                 iov[0].iov_base = (void *)state.response;
1467                 iov[0].iov_len = sizeof(struct winbindd_response);
1468                 iov_count = 1;
1469
1470                 if (state.response->length > sizeof(struct winbindd_response)) {
1471                         iov[1].iov_base =
1472                                 (void *)state.response->extra_data.data;
1473                         iov[1].iov_len = state.response->length-iov[0].iov_len;
1474                         iov_count = 2;
1475                 }
1476
1477                 DEBUG(10, ("Writing %d bytes to parent\n",
1478                            (int)state.response->length));
1479
1480                 if (write_data_iov(state.sock, iov, iov_count) !=
1481                     state.response->length) {
1482                         DEBUG(0, ("Could not write result\n"));
1483                         exit(1);
1484                 }
1485                 TALLOC_FREE(frame);
1486         }
1487 }