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