Append to log.winbindd instead of overwriting it so we are consistent with
[kai/samba.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_sigusr1;
173
174 static void sigusr1_handler(int signum)
175 {
176         do_sigusr1 = 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_FROM_USER, winbindd_getpwnam_from_user, "GETPWNAM_FROM_USER" },
289         { WINBINDD_GETPWNAM_FROM_UID, winbindd_getpwnam_from_uid, "GETPWNAM_FROM_UID" },
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_FROM_GROUP, winbindd_getgrnam_from_group, "GETGRNAM_FROM_GROUP" },
300         { WINBINDD_GETGRNAM_FROM_GID, winbindd_getgrnam_from_gid, "GETGRNAM_FROM_GID" },
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
333         /* End of list */
334
335         { WINBINDD_NUM_CMDS, NULL, "NONE" }
336 };
337
338 static void process_request(struct winbindd_cli_state *state)
339 {
340         struct dispatch_table *table = dispatch_table;
341
342         /* Free response data - we may be interrupted and receive another
343            command before being able to send this data off. */
344
345         SAFE_FREE(state->response.extra_data);  
346
347         ZERO_STRUCT(state->response);
348
349         state->response.result = WINBINDD_ERROR;
350         state->response.length = sizeof(struct winbindd_response);
351
352         /* Process command */
353
354         for (table = dispatch_table; table->fn; table++) {
355                 if (state->request.cmd == table->cmd) {
356                         DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
357                         state->response.result = table->fn(state);
358                         break;
359                 }
360         }
361
362         if (!table->fn)
363                 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
364
365         /* In case extra data pointer is NULL */
366
367         if (!state->response.extra_data)
368                 state->response.length = sizeof(struct winbindd_response);
369 }
370
371 /* Process a new connection by adding it to the client connection list */
372
373 static void new_connection(int accept_sock)
374 {
375         struct sockaddr_un sunaddr;
376         struct winbindd_cli_state *state;
377         socklen_t len;
378         int sock;
379         
380         /* Accept connection */
381         
382         len = sizeof(sunaddr);
383
384         do {
385                 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
386         } while (sock == -1 && errno == EINTR);
387
388         if (sock == -1)
389                 return;
390         
391         DEBUG(6,("accepted socket %d\n", sock));
392         
393         /* Create new connection structure */
394         
395         if ((state = (struct winbindd_cli_state *) 
396              malloc(sizeof(*state))) == NULL)
397                 return;
398         
399         ZERO_STRUCTP(state);
400         state->sock = sock;
401         
402         /* Add to connection list */
403         
404         DLIST_ADD(client_list, state);
405         num_clients++;
406 }
407
408 /* Remove a client connection from client connection list */
409
410 static void remove_client(struct winbindd_cli_state *state)
411 {
412         /* It's a dead client - hold a funeral */
413         
414         if (state != NULL) {
415                 
416                 /* Close socket */
417                 
418                 close(state->sock);
419                 
420                 /* Free any getent state */
421                 
422                 free_getent_state(state->getpwent_state);
423                 free_getent_state(state->getgrent_state);
424                 
425                 /* We may have some extra data that was not freed if the
426                    client was killed unexpectedly */
427
428                 SAFE_FREE(state->response.extra_data);
429                 
430                 /* Remove from list and free */
431                 
432                 DLIST_REMOVE(client_list, state);
433                 SAFE_FREE(state);
434                 num_clients--;
435         }
436 }
437
438 /* Process a complete received packet from a client */
439
440 static void process_packet(struct winbindd_cli_state *state)
441 {
442         /* Process request */
443         
444         state->pid = state->request.pid;
445         
446         process_request(state);
447
448         /* Update client state */
449         
450         state->read_buf_len = 0;
451         state->write_buf_len = sizeof(struct winbindd_response);
452 }
453
454 /* Read some data from a client connection */
455
456 static void client_read(struct winbindd_cli_state *state)
457 {
458         int n;
459     
460         /* Read data */
461
462         do {
463                 n = read(state->sock, state->read_buf_len + (char *)&state->request, 
464                                  sizeof(state->request) - state->read_buf_len);
465         } while (n == -1 && errno == EINTR);
466         
467         DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n,
468                         sizeof(state->request) - n - state->read_buf_len ));
469
470         /* Read failed, kill client */
471         
472         if (n == -1 || n == 0) {
473                 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
474                          state->sock, state->pid, 
475                          (n == -1) ? strerror(errno) : "EOF"));
476                 
477                 state->finished = True;
478                 return;
479         }
480         
481         /* Update client state */
482         
483         state->read_buf_len += n;
484 }
485
486 /* Write some data to a client connection */
487
488 static void client_write(struct winbindd_cli_state *state)
489 {
490         char *data;
491         int num_written;
492         
493         /* Write some data */
494         
495         if (!state->write_extra_data) {
496
497                 /* Write response structure */
498                 
499                 data = (char *)&state->response + sizeof(state->response) - 
500                         state->write_buf_len;
501
502         } else {
503
504                 /* Write extra data */
505                 
506                 data = (char *)state->response.extra_data + 
507                         state->response.length - 
508                         sizeof(struct winbindd_response) - 
509                         state->write_buf_len;
510         }
511         
512         do {
513                 num_written = write(state->sock, data, state->write_buf_len);
514         } while (num_written == -1 && errno == EINTR);
515         
516         DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
517
518         /* Write failed, kill cilent */
519         
520         if (num_written == -1 || num_written == 0) {
521                 
522                 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
523                          state->sock, state->pid, 
524                          (num_written == -1) ? strerror(errno) : "EOF"));
525                 
526                 state->finished = True;
527
528                 SAFE_FREE(state->response.extra_data);
529
530                 return;
531         }
532         
533         /* Update client state */
534         
535         state->write_buf_len -= num_written;
536         
537         /* Have we written all data? */
538         
539         if (state->write_buf_len == 0) {
540                 
541                 /* Take care of extra data */
542                 
543                 if (state->write_extra_data) {
544
545                         SAFE_FREE(state->response.extra_data);
546
547                         state->write_extra_data = False;
548
549                         DEBUG(10,("client_write: client_write: complete response written.\n"));
550
551                 } else if (state->response.length > 
552                            sizeof(struct winbindd_response)) {
553                         
554                         /* Start writing extra data */
555
556                         state->write_buf_len = 
557                                 state->response.length -
558                                 sizeof(struct winbindd_response);
559                         
560                         DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
561
562                         state->write_extra_data = True;
563                 }
564         }
565 }
566
567 /* Process incoming clients on accept_sock.  We use a tricky non-blocking,
568    non-forking, non-threaded model which allows us to handle many
569    simultaneous connections while remaining impervious to many denial of
570    service attacks. */
571
572 static void process_loop(int accept_sock)
573 {
574         /* We'll be doing this a lot */
575
576         while (1) {
577                 struct winbindd_cli_state *state;
578                 fd_set r_fds, w_fds;
579                 int maxfd = accept_sock, selret;
580                 struct timeval timeout;
581
582                 /* Free up temporary memory */
583
584                 lp_talloc_free();
585
586                 /* Initialise fd lists for select() */
587
588                 FD_ZERO(&r_fds);
589                 FD_ZERO(&w_fds);
590                 FD_SET(accept_sock, &r_fds);
591
592                 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
593                 timeout.tv_usec = 0;
594
595                 /* Set up client readers and writers */
596
597                 state = client_list;
598
599                 while (state) {
600
601                         /* Dispose of client connection if it is marked as 
602                            finished */ 
603
604                         if (state->finished) {
605                                 struct winbindd_cli_state *next = state->next;
606
607                                 remove_client(state);
608                                 state = next;
609                                 continue;
610                         }
611
612                         /* Select requires we know the highest fd used */
613
614                         if (state->sock > maxfd)
615                                 maxfd = state->sock;
616
617                         /* Add fd for reading */
618
619                         if (state->read_buf_len != sizeof(state->request))
620                                 FD_SET(state->sock, &r_fds);
621
622                         /* Add fd for writing */
623
624                         if (state->write_buf_len)
625                                 FD_SET(state->sock, &w_fds);
626
627                         state = state->next;
628                 }
629
630                 /* Call select */
631         
632                 selret = select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
633
634                 if (selret == 0)
635                         continue;
636
637                 if ((selret == -1 && errno != EINTR) || selret == 0) {
638
639                         /* Select error, something is badly wrong */
640
641                         perror("select");
642                         exit(1);
643                 }
644
645                 /* Create a new connection if accept_sock readable */
646
647                 if (selret > 0) {
648
649                         if (FD_ISSET(accept_sock, &r_fds))
650                                 new_connection(accept_sock);
651             
652                         /* Process activity on client connections */
653             
654                         for (state = client_list; state; state = state->next) {
655                 
656                                 /* Data available for reading */
657                 
658                                 if (FD_ISSET(state->sock, &r_fds)) {
659                     
660                                         /* Read data */
661                     
662                                         client_read(state);
663
664 #if 0
665                                         /* JRA - currently there's no length field in the request... */
666                                         /* 
667                                          * If we have the start of a
668                                          * packet, then check the
669                                          * length field to make sure
670                                          * the client's not talking
671                                          * Mock Swedish.
672                                          */
673
674                                         if (state->read_buf_len >= sizeof(int)
675                                             && *(int *) state->buf != sizeof(state->request)) {
676
677                                                 struct winbindd_cli_state *rem_state = state;
678
679                                                 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
680                                                                 *(int *) rem_state->buf, sizeof(rem_state->request) ));
681
682                                                 state = state_next;
683                                                 remove_client(rem_state);
684                                                 continue;
685                                         }
686 #endif
687
688                                         /* A request packet might be 
689                                            complete */
690                     
691                                         if (state->read_buf_len == 
692                                             sizeof(state->request)) {
693                                                 process_packet(state);
694                                         }
695                                 }
696                 
697                                 /* Data available for writing */
698                 
699                                 if (FD_ISSET(state->sock, &w_fds))
700                                         client_write(state);
701                         }
702                 }
703
704                 /* Check signal handling things */
705
706                 if (do_sigterm)
707                         terminate();
708
709                 if (do_sighup) {
710
711                         /* Flush winbindd cache */
712
713                         flush_caches();
714                         reload_services_file(True);
715                         do_sighup = False;
716                 }
717
718                 if (do_sigusr1) {
719                         print_winbindd_status();
720                         do_sigusr1 = False;
721                 }
722         }
723 }
724
725 /* Main function */
726
727 struct winbindd_state server_state;   /* Server state information */
728
729 int main(int argc, char **argv)
730 {
731         extern pstring global_myname;
732         extern fstring global_myworkgroup;
733         extern BOOL append_log;
734         pstring logfile;
735         int accept_sock;
736         BOOL interactive = False;
737         int opt, new_debuglevel = -1;
738
739         /* glibc (?) likes to print "User defined signal 1" and exit if a
740                 SIGUSR1 is received before a handler is installed */
741
742         CatchSignal(SIGUSR1, SIG_IGN);
743
744         fault_setup((void (*)(void *))fault_quit );
745
746         /* Append to log file by default as we are a single process daemon
747            program. */
748
749         append_log = True;
750
751         snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
752         lp_set_logfile(logfile);
753
754         /* Initialise for running in non-root mode */
755
756         sec_init();
757
758         /* Set environment variable so we don't recursively call ourselves.
759            This may also be useful interactively. */
760
761         SETENV(WINBINDD_DONT_ENV, "1", 1);
762
763         /* Initialise samba/rpc client stuff */
764
765         while ((opt = getopt(argc, argv, "id:s:n")) != EOF) {
766                 switch (opt) {
767
768                         /* Don't become a daemon */
769                 case 'i':
770                         interactive = True;
771                         break;
772
773                         /* disable cacheing */
774                 case 'n':
775                         opt_nocache = True;
776                         break;
777
778                         /* Run with specified debug level */
779                 case 'd':
780                         new_debuglevel = atoi(optarg);
781                         break;
782
783                         /* Load a different smb.conf file */
784                 case 's':
785                         pstrcpy(dyn_CONFIGFILE,optarg);
786                         break;
787
788                 default:
789                         printf("Unknown option %c\n", (char)opt);
790                         exit(1);
791                 }
792         }
793
794         snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
795         lp_set_logfile(logfile);
796         setup_logging("winbindd", interactive);
797         reopen_logs();
798
799         DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
800         DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
801
802         if (!reload_services_file(False)) {
803                 DEBUG(0, ("error opening config file\n"));
804                 exit(1);
805         }
806
807         /* Setup names. */
808         if (!*global_myname) {
809                 char *p;
810
811                 fstrcpy(global_myname, myhostname());
812                 p = strchr(global_myname, '.');
813                 if (p)
814                         *p = 0;
815         }
816
817         fstrcpy(global_myworkgroup, lp_workgroup());
818
819         if (new_debuglevel != -1)
820                 DEBUGLEVEL = new_debuglevel;
821
822         if (!interactive)
823                 become_daemon();
824
825         load_interfaces();
826
827         secrets_init();
828
829         /* Get list of domains we look up requests for.  This includes the
830                 domain which we are a member of as well as any trusted
831                 domains. */ 
832
833         get_domain_info();
834
835         ZERO_STRUCT(server_state);
836
837         /* Winbind daemon initialisation */
838
839         if (!winbindd_param_init())
840                 return 1;
841
842         if (!winbindd_idmap_init())
843                 return 1;
844
845         /* Unblock all signals we are interested in as they may have been
846            blocked by the parent process. */
847
848         BlockSignals(False, SIGINT);
849         BlockSignals(False, SIGQUIT);
850         BlockSignals(False, SIGTERM);
851         BlockSignals(False, SIGUSR1);
852         BlockSignals(False, SIGHUP);
853
854         /* Setup signal handlers */
855         
856         CatchSignal(SIGINT, termination_handler);      /* Exit on these sigs */
857         CatchSignal(SIGQUIT, termination_handler);
858         CatchSignal(SIGTERM, termination_handler);
859
860         CatchSignal(SIGPIPE, SIG_IGN);                 /* Ignore sigpipe */
861
862         CatchSignal(SIGUSR1, sigusr1_handler);         /* Debugging sigs */
863         CatchSignal(SIGHUP, sighup_handler);
864
865         /* Create UNIX domain socket */
866         
867         if ((accept_sock = create_sock()) == -1) {
868                 DEBUG(0, ("failed to create socket\n"));
869                 return 1;
870         }
871
872         /* Loop waiting for requests */
873
874         process_loop(accept_sock);
875
876         return 0;
877 }