1a9eae7afa545ed7b92c2a033bbd9f5c5b4bcdde
[ira/wip.git] / source3 / nsswitch / winbindd.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4
5    Winbind daemon for ntdom nss module
6
7    Copyright (C) by Tim Potter 2000, 2001
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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "winbindd.h"
25
26 /* List of all connected clients */
27
28 struct winbindd_cli_state *client_list;
29 static int num_clients;
30 BOOL opt_nocache;
31
32 /* Reload configuration */
33
34 static BOOL reload_services_file(BOOL test)
35 {
36         BOOL ret;
37         pstring logfile;
38
39         if (lp_loaded()) {
40                 pstring fname;
41
42                 pstrcpy(fname,lp_configfile());
43                 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
44                         pstrcpy(dyn_CONFIGFILE,fname);
45                         test = False;
46                 }
47         }
48
49         snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
50         lp_set_logfile(logfile);
51
52         reopen_logs();
53         ret = lp_load(dyn_CONFIGFILE,False,False,True);
54
55         snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
56         lp_set_logfile(logfile);
57
58         reopen_logs();
59         load_interfaces();
60
61         return(ret);
62 }
63
64 #if DUMP_CORE
65
66 /**************************************************************************** **
67  Prepare to dump a core file - carefully!
68  **************************************************************************** */
69
70 static BOOL dump_core(void)
71 {
72         char *p;
73         pstring dname;
74         pstrcpy( dname, lp_logfile() );
75         if ((p=strrchr(dname,'/')))
76                 *p=0;
77         pstrcat( dname, "/corefiles" );
78         mkdir( dname, 0700 );
79         sys_chown( dname, getuid(), getgid() );
80         chmod( dname, 0700 );
81         if ( chdir(dname) )
82                 return( False );
83         umask( ~(0700) );
84  
85 #ifdef HAVE_GETRLIMIT
86 #ifdef RLIMIT_CORE
87         {
88                 struct rlimit rlp;
89                 getrlimit( RLIMIT_CORE, &rlp );
90                 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
91                 setrlimit( RLIMIT_CORE, &rlp );
92                 getrlimit( RLIMIT_CORE, &rlp );
93                 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
94         }
95 #endif
96 #endif
97  
98         DEBUG(0,("Dumping core in %s\n",dname));
99         abort();
100         return( True );
101 } /* dump_core */
102 #endif
103
104 /**************************************************************************** **
105  Handle a fault..
106  **************************************************************************** */
107
108 static void fault_quit(void)
109 {
110 #if DUMP_CORE
111         dump_core();
112 #endif
113 }
114
115 static void winbindd_status(void)
116 {
117         struct winbindd_cli_state *tmp;
118
119         DEBUG(0, ("winbindd status:\n"));
120
121         /* Print client state information */
122         
123         DEBUG(0, ("\t%d clients currently active\n", num_clients));
124         
125         if (DEBUGLEVEL >= 2 && num_clients) {
126                 DEBUG(2, ("\tclient list:\n"));
127                 for(tmp = client_list; tmp; tmp = tmp->next) {
128                         DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
129                                   tmp->pid, tmp->sock, tmp->read_buf_len, 
130                                   tmp->write_buf_len));
131                 }
132         }
133 }
134
135 /* Print winbindd status to log file */
136
137 static void print_winbindd_status(void)
138 {
139         winbindd_status();
140         winbindd_idmap_status();
141         winbindd_cm_status();
142 }
143
144 /* Flush client cache */
145
146 static void flush_caches(void)
147 {
148         /* Clear cached user and group enumation info */        
149         wcache_flush_cache();
150 }
151
152 /* Handle the signal by unlinking socket and exiting */
153
154 static void terminate(void)
155 {
156         pstring path;
157         
158         /* Remove socket file */
159         snprintf(path, sizeof(path), "%s/%s", 
160                  WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
161         unlink(path);
162         exit(0);
163 }
164
165 static BOOL do_sigterm;
166
167 static void termination_handler(int signum)
168 {
169         do_sigterm = True;
170 }
171
172 static BOOL do_sigusr2;
173
174 static void sigusr2_handler(int signum)
175 {
176         do_sigusr2 = True;
177 }
178
179 static BOOL do_sighup;
180
181 static void sighup_handler(int signum)
182 {
183         do_sighup = True;
184 }
185
186 /* Create winbindd socket */
187
188 static int create_sock(void)
189 {
190         struct sockaddr_un sunaddr;
191         struct stat st;
192         int sock;
193         mode_t old_umask;
194         pstring path;
195         
196         /* Create the socket directory or reuse the existing one */
197         
198         if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
199                 
200                 if (errno == ENOENT) {
201                         
202                         /* Create directory */
203                         
204                         if (mkdir(WINBINDD_SOCKET_DIR, 0755) == -1) {
205                                 DEBUG(0, ("error creating socket directory "
206                                           "%s: %s\n", WINBINDD_SOCKET_DIR, 
207                                           strerror(errno)));
208                                 return -1;
209                         }
210                         
211                 } else {
212                         
213                         DEBUG(0, ("lstat failed on socket directory %s: %s\n",
214                                   WINBINDD_SOCKET_DIR, strerror(errno)));
215                         return -1;
216                 }
217                 
218         } else {
219                 
220                 /* Check ownership and permission on existing directory */
221                 
222                 if (!S_ISDIR(st.st_mode)) {
223                         DEBUG(0, ("socket directory %s isn't a directory\n",
224                                   WINBINDD_SOCKET_DIR));
225                         return -1;
226                 }
227                 
228                 if ((st.st_uid != sec_initial_uid()) || 
229                     ((st.st_mode & 0777) != 0755)) {
230                         DEBUG(0, ("invalid permissions on socket directory "
231                                   "%s\n", WINBINDD_SOCKET_DIR));
232                         return -1;
233                 }
234         }
235         
236         /* Create the socket file */
237         
238         old_umask = umask(0);
239         
240         sock = socket(AF_UNIX, SOCK_STREAM, 0);
241         
242         if (sock == -1) {
243                 perror("socket");
244                 return -1;
245         }
246         
247         snprintf(path, sizeof(path), "%s/%s", 
248                  WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
249         
250         unlink(path);
251         memset(&sunaddr, 0, sizeof(sunaddr));
252         sunaddr.sun_family = AF_UNIX;
253         safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
254         
255         if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
256                 DEBUG(0, ("bind failed on winbind socket %s: %s\n",
257                           path,
258                           strerror(errno)));
259                 close(sock);
260                 return -1;
261         }
262         
263         if (listen(sock, 5) == -1) {
264                 DEBUG(0, ("listen failed on winbind socket %s: %s\n",
265                           path,
266                           strerror(errno)));
267                 close(sock);
268                 return -1;
269         }
270         
271         umask(old_umask);
272         
273         /* Success! */
274         
275         return sock;
276 }
277
278 struct dispatch_table {
279         enum winbindd_cmd cmd;
280         enum winbindd_result (*fn)(struct winbindd_cli_state *state);
281         char *winbindd_cmd_name;
282 };
283
284 static struct dispatch_table dispatch_table[] = {
285         
286         /* User functions */
287
288         { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
289         { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
290
291         { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
292         { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
293         { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
294
295         { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
296
297         /* Group functions */
298
299         { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
300         { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
301         { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
302         { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
303         { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
304
305         /* PAM auth functions */
306
307         { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
308         { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
309         { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
310
311         /* Enumeration functions */
312
313         { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
314         { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
315         { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
316
317         /* SID related functions */
318
319         { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
320         { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
321
322         /* Lookup related functions */
323
324         { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
325         { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
326         { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
327         { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
328
329         /* Miscellaneous */
330
331         { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
332         { WINBINDD_PING, winbindd_ping, "PING" },
333         { WINBINDD_INFO, winbindd_info, "INFO" },
334         { WINBINDD_INTERFACE_VERSION, winbindd_interface_version, "INTERFACE_VERSION" },
335
336         /* End of list */
337
338         { WINBINDD_NUM_CMDS, NULL, "NONE" }
339 };
340
341 static void process_request(struct winbindd_cli_state *state)
342 {
343         struct dispatch_table *table = dispatch_table;
344
345         /* Free response data - we may be interrupted and receive another
346            command before being able to send this data off. */
347
348         SAFE_FREE(state->response.extra_data);  
349
350         ZERO_STRUCT(state->response);
351
352         state->response.result = WINBINDD_ERROR;
353         state->response.length = sizeof(struct winbindd_response);
354
355         /* Process command */
356
357         for (table = dispatch_table; table->fn; table++) {
358                 if (state->request.cmd == table->cmd) {
359                         DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
360                         state->response.result = table->fn(state);
361                         break;
362                 }
363         }
364
365         if (!table->fn)
366                 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
367
368         /* In case extra data pointer is NULL */
369
370         if (!state->response.extra_data)
371                 state->response.length = sizeof(struct winbindd_response);
372 }
373
374 /* Process a new connection by adding it to the client connection list */
375
376 static void new_connection(int accept_sock)
377 {
378         struct sockaddr_un sunaddr;
379         struct winbindd_cli_state *state;
380         socklen_t len;
381         int sock;
382         
383         /* Accept connection */
384         
385         len = sizeof(sunaddr);
386
387         do {
388                 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
389         } while (sock == -1 && errno == EINTR);
390
391         if (sock == -1)
392                 return;
393         
394         DEBUG(6,("accepted socket %d\n", sock));
395         
396         /* Create new connection structure */
397         
398         if ((state = (struct winbindd_cli_state *) 
399              malloc(sizeof(*state))) == NULL)
400                 return;
401         
402         ZERO_STRUCTP(state);
403         state->sock = sock;
404         
405         /* Add to connection list */
406         
407         DLIST_ADD(client_list, state);
408         num_clients++;
409 }
410
411 /* Remove a client connection from client connection list */
412
413 static void remove_client(struct winbindd_cli_state *state)
414 {
415         /* It's a dead client - hold a funeral */
416         
417         if (state != NULL) {
418                 
419                 /* Close socket */
420                 
421                 close(state->sock);
422                 
423                 /* Free any getent state */
424                 
425                 free_getent_state(state->getpwent_state);
426                 free_getent_state(state->getgrent_state);
427                 
428                 /* We may have some extra data that was not freed if the
429                    client was killed unexpectedly */
430
431                 SAFE_FREE(state->response.extra_data);
432                 
433                 /* Remove from list and free */
434                 
435                 DLIST_REMOVE(client_list, state);
436                 SAFE_FREE(state);
437                 num_clients--;
438         }
439 }
440
441 /* Process a complete received packet from a client */
442
443 static void process_packet(struct winbindd_cli_state *state)
444 {
445         /* Process request */
446         
447         state->pid = state->request.pid;
448         
449         process_request(state);
450
451         /* Update client state */
452         
453         state->read_buf_len = 0;
454         state->write_buf_len = sizeof(struct winbindd_response);
455 }
456
457 /* Read some data from a client connection */
458
459 static void client_read(struct winbindd_cli_state *state)
460 {
461         int n;
462     
463         /* Read data */
464
465         do {
466
467                 n = read(state->sock, state->read_buf_len + 
468                          (char *)&state->request, 
469                          sizeof(state->request) - state->read_buf_len);
470
471         } while (n == -1 && errno == EINTR);
472         
473         DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n, sizeof(state->request) - n - state->read_buf_len ));
474
475         /* Read failed, kill client */
476         
477         if (n == -1 || n == 0) {
478                 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
479                          state->sock, state->pid, 
480                          (n == -1) ? strerror(errno) : "EOF"));
481                 
482                 state->finished = True;
483                 return;
484         }
485         
486         /* Update client state */
487         
488         state->read_buf_len += n;
489 }
490
491 /* Write some data to a client connection */
492
493 static void client_write(struct winbindd_cli_state *state)
494 {
495         char *data;
496         int num_written;
497         
498         /* Write some data */
499         
500         if (!state->write_extra_data) {
501
502                 /* Write response structure */
503                 
504                 data = (char *)&state->response + sizeof(state->response) - 
505                         state->write_buf_len;
506
507         } else {
508
509                 /* Write extra data */
510                 
511                 data = (char *)state->response.extra_data + 
512                         state->response.length - 
513                         sizeof(struct winbindd_response) - 
514                         state->write_buf_len;
515         }
516         
517         do {
518                 num_written = write(state->sock, data, state->write_buf_len);
519         } while (num_written == -1 && errno == EINTR);
520         
521         DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
522
523         /* Write failed, kill cilent */
524         
525         if (num_written == -1 || num_written == 0) {
526                 
527                 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
528                          state->sock, state->pid, 
529                          (num_written == -1) ? strerror(errno) : "EOF"));
530                 
531                 state->finished = True;
532
533                 SAFE_FREE(state->response.extra_data);
534
535                 return;
536         }
537         
538         /* Update client state */
539         
540         state->write_buf_len -= num_written;
541         
542         /* Have we written all data? */
543         
544         if (state->write_buf_len == 0) {
545                 
546                 /* Take care of extra data */
547                 
548                 if (state->write_extra_data) {
549
550                         SAFE_FREE(state->response.extra_data);
551
552                         state->write_extra_data = False;
553
554                         DEBUG(10,("client_write: client_write: complete response written.\n"));
555
556                 } else if (state->response.length > 
557                            sizeof(struct winbindd_response)) {
558                         
559                         /* Start writing extra data */
560
561                         state->write_buf_len = 
562                                 state->response.length -
563                                 sizeof(struct winbindd_response);
564                         
565                         DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
566
567                         state->write_extra_data = True;
568                 }
569         }
570 }
571
572 /* Process incoming clients on accept_sock.  We use a tricky non-blocking,
573    non-forking, non-threaded model which allows us to handle many
574    simultaneous connections while remaining impervious to many denial of
575    service attacks. */
576
577 static void process_loop(int accept_sock)
578 {
579         /* We'll be doing this a lot */
580
581         while (1) {
582                 struct winbindd_cli_state *state;
583                 fd_set r_fds, w_fds;
584                 int maxfd = accept_sock, selret;
585                 struct timeval timeout;
586
587                 /* Free up temporary memory */
588
589                 lp_talloc_free();
590
591                 /* Initialise fd lists for select() */
592
593                 FD_ZERO(&r_fds);
594                 FD_ZERO(&w_fds);
595                 FD_SET(accept_sock, &r_fds);
596
597                 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
598                 timeout.tv_usec = 0;
599
600                 /* Set up client readers and writers */
601
602                 state = client_list;
603
604                 while (state) {
605
606                         /* Dispose of client connection if it is marked as 
607                            finished */ 
608
609                         if (state->finished) {
610                                 struct winbindd_cli_state *next = state->next;
611
612                                 remove_client(state);
613                                 state = next;
614                                 continue;
615                         }
616
617                         /* Select requires we know the highest fd used */
618
619                         if (state->sock > maxfd)
620                                 maxfd = state->sock;
621
622                         /* Add fd for reading */
623
624                         if (state->read_buf_len != sizeof(state->request))
625                                 FD_SET(state->sock, &r_fds);
626
627                         /* Add fd for writing */
628
629                         if (state->write_buf_len)
630                                 FD_SET(state->sock, &w_fds);
631
632                         state = state->next;
633                 }
634
635                 /* Call select */
636         
637                 selret = select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
638
639                 if (selret == 0)
640                         continue;
641
642                 if ((selret == -1 && errno != EINTR) || selret == 0) {
643
644                         /* Select error, something is badly wrong */
645
646                         perror("select");
647                         exit(1);
648                 }
649
650                 /* Create a new connection if accept_sock readable */
651
652                 if (selret > 0) {
653
654                         if (FD_ISSET(accept_sock, &r_fds))
655                                 new_connection(accept_sock);
656             
657                         /* Process activity on client connections */
658             
659                         for (state = client_list; state; state = state->next) {
660                 
661                                 /* Data available for reading */
662                 
663                                 if (FD_ISSET(state->sock, &r_fds)) {
664                     
665                                         /* Read data */
666                     
667                                         client_read(state);
668
669                                         /* 
670                                          * If we have the start of a
671                                          * packet, then check the
672                                          * length field to make sure
673                                          * the client's not talking
674                                          * Mock Swedish.
675                                          */
676
677                                         if (state->read_buf_len >= sizeof(uint32)
678                                             && *(uint32 *) &state->request != sizeof(state->request)) {
679                                                 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
680                                                                 *(uint32 *) &state->request, sizeof(state->request)));
681
682                                                 remove_client(state);
683                                                 break;
684                                         }
685
686                                         /* A request packet might be 
687                                            complete */
688                     
689                                         if (state->read_buf_len == 
690                                             sizeof(state->request)) {
691                                                 process_packet(state);
692                                         }
693                                 }
694                 
695                                 /* Data available for writing */
696                 
697                                 if (FD_ISSET(state->sock, &w_fds))
698                                         client_write(state);
699                         }
700                 }
701
702                 /* Check signal handling things */
703
704                 if (do_sigterm)
705                         terminate();
706
707                 if (do_sighup) {
708
709                         /* Flush winbindd cache */
710
711                         flush_caches();
712                         reload_services_file(True);
713                         do_sighup = False;
714                 }
715
716                 if (do_sigusr2) {
717                         print_winbindd_status();
718                         do_sigusr2 = False;
719                 }
720         }
721 }
722
723 /* Main function */
724
725 struct winbindd_state server_state;   /* Server state information */
726
727 int main(int argc, char **argv)
728 {
729         extern pstring global_myname;
730         extern fstring global_myworkgroup;
731         extern BOOL append_log;
732         pstring logfile;
733         int accept_sock;
734         BOOL interactive = False;
735         int opt, new_debuglevel = -1;
736
737         /* glibc (?) likes to print "User defined signal 1" and exit if a
738            SIGUSR2 is received before a handler is installed */
739
740         CatchSignal(SIGUSR2, SIG_IGN);
741
742         fault_setup((void (*)(void *))fault_quit );
743
744         /* Append to log file by default as we are a single process daemon
745            program. */
746
747         append_log = True;
748
749         snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
750         lp_set_logfile(logfile);
751
752         /* Initialise for running in non-root mode */
753
754         sec_init();
755
756         /* Set environment variable so we don't recursively call ourselves.
757            This may also be useful interactively. */
758
759         SETENV(WINBINDD_DONT_ENV, "1", 1);
760
761         /* Initialise samba/rpc client stuff */
762
763         while ((opt = getopt(argc, argv, "id:s:n")) != EOF) {
764                 switch (opt) {
765
766                         /* Don't become a daemon */
767                 case 'i':
768                         interactive = True;
769                         break;
770
771                         /* disable cacheing */
772                 case 'n':
773                         opt_nocache = True;
774                         break;
775
776                         /* Run with specified debug level */
777                 case 'd':
778                         new_debuglevel = atoi(optarg);
779                         break;
780
781                         /* Load a different smb.conf file */
782                 case 's':
783                         pstrcpy(dyn_CONFIGFILE,optarg);
784                         break;
785
786                 default:
787                         printf("Unknown option %c\n", (char)opt);
788                         exit(1);
789                 }
790         }
791
792         snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
793         lp_set_logfile(logfile);
794         setup_logging("winbindd", interactive);
795         reopen_logs();
796
797         DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
798         DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
799
800         if (!reload_services_file(False)) {
801                 DEBUG(0, ("error opening config file\n"));
802                 exit(1);
803         }
804
805         /* Setup names. */
806         if (!*global_myname) {
807                 char *p;
808
809                 fstrcpy(global_myname, myhostname());
810                 p = strchr(global_myname, '.');
811                 if (p)
812                         *p = 0;
813         }
814
815         fstrcpy(global_myworkgroup, lp_workgroup());
816
817         if (new_debuglevel != -1)
818                 DEBUGLEVEL = new_debuglevel;
819
820         if (!interactive)
821                 become_daemon();
822
823 #if HAVE_SETPGID
824         /*
825          * If we're interactive we want to set our own process group for
826          * signal management.
827          */
828         if (interactive)
829                 setpgid( (pid_t)0, (pid_t)0);
830 #endif
831
832         load_interfaces();
833
834         secrets_init();
835
836         /* Get list of domains we look up requests for.  This includes the
837            domain which we are a member of as well as any trusted
838            domains. */ 
839
840         init_domain_list();
841
842         ZERO_STRUCT(server_state);
843
844         /* Winbind daemon initialisation */
845
846         if (!winbindd_param_init())
847                 return 1;
848
849         if (!winbindd_idmap_init())
850                 return 1;
851
852         /* Unblock all signals we are interested in as they may have been
853            blocked by the parent process. */
854
855         BlockSignals(False, SIGINT);
856         BlockSignals(False, SIGQUIT);
857         BlockSignals(False, SIGTERM);
858         BlockSignals(False, SIGUSR2);
859         BlockSignals(False, SIGHUP);
860
861         /* Setup signal handlers */
862         
863         CatchSignal(SIGINT, termination_handler);      /* Exit on these sigs */
864         CatchSignal(SIGQUIT, termination_handler);
865         CatchSignal(SIGTERM, termination_handler);
866
867         CatchSignal(SIGPIPE, SIG_IGN);                 /* Ignore sigpipe */
868         CatchSignal(SIGUSR1, SIG_IGN);                 /* Samba messages */
869
870         CatchSignal(SIGUSR2, sigusr2_handler);         /* Debugging sigs */
871         CatchSignal(SIGHUP, sighup_handler);
872
873         /* Create UNIX domain socket */
874         
875         if ((accept_sock = create_sock()) == -1) {
876                 DEBUG(0, ("failed to create socket\n"));
877                 return 1;
878         }
879
880         /* Loop waiting for requests */
881
882         process_loop(accept_sock);
883
884         uni_group_cache_shutdown();
885         return 0;
886 }