358d9add3a388c10f3de544bcbfe8f3a8e946c2d
[samba.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         { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
227
228         /* PAM auth functions */
229
230         { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
231         { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
232         { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
233
234         /* Enumeration functions */
235
236         { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
237         { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
238         { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
239         { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
240
241         /* SID related functions */
242
243         { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
244         { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
245
246         /* Lookup related functions */
247
248         { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
249         { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
250         { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
251         { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
252
253         /* Miscellaneous */
254
255         { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
256         { WINBINDD_PING, winbindd_ping, "PING" },
257         { WINBINDD_INFO, winbindd_info, "INFO" },
258         { WINBINDD_INTERFACE_VERSION, winbindd_interface_version, "INTERFACE_VERSION" },
259         { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
260
261         /* WINS functions */
262
263         { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
264         { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
265
266         /* End of list */
267
268         { WINBINDD_NUM_CMDS, NULL, "NONE" }
269 };
270
271 static void process_request(struct winbindd_cli_state *state)
272 {
273         struct dispatch_table *table = dispatch_table;
274
275         /* Free response data - we may be interrupted and receive another
276            command before being able to send this data off. */
277
278         SAFE_FREE(state->response.extra_data);  
279
280         ZERO_STRUCT(state->response);
281
282         state->response.result = WINBINDD_ERROR;
283         state->response.length = sizeof(struct winbindd_response);
284
285         /* Process command */
286
287         for (table = dispatch_table; table->fn; table++) {
288                 if (state->request.cmd == table->cmd) {
289                         DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
290                         state->response.result = table->fn(state);
291                         break;
292                 }
293         }
294
295         if (!table->fn)
296                 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
297
298         /* In case extra data pointer is NULL */
299
300         if (!state->response.extra_data)
301                 state->response.length = sizeof(struct winbindd_response);
302 }
303
304 /* Process a new connection by adding it to the client connection list */
305
306 static void new_connection(int accept_sock)
307 {
308         struct sockaddr_un sunaddr;
309         struct winbindd_cli_state *state;
310         socklen_t len;
311         int sock;
312         
313         /* Accept connection */
314         
315         len = sizeof(sunaddr);
316
317         do {
318                 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
319         } while (sock == -1 && errno == EINTR);
320
321         if (sock == -1)
322                 return;
323         
324         DEBUG(6,("accepted socket %d\n", sock));
325         
326         /* Create new connection structure */
327         
328         if ((state = (struct winbindd_cli_state *) 
329              malloc(sizeof(*state))) == NULL)
330                 return;
331         
332         ZERO_STRUCTP(state);
333         state->sock = sock;
334         
335         /* Add to connection list */
336         
337         DLIST_ADD(client_list, state);
338         num_clients++;
339 }
340
341 /* Remove a client connection from client connection list */
342
343 static void remove_client(struct winbindd_cli_state *state)
344 {
345         /* It's a dead client - hold a funeral */
346         
347         if (state != NULL) {
348                 
349                 /* Close socket */
350                 
351                 close(state->sock);
352                 
353                 /* Free any getent state */
354                 
355                 free_getent_state(state->getpwent_state);
356                 free_getent_state(state->getgrent_state);
357                 
358                 /* We may have some extra data that was not freed if the
359                    client was killed unexpectedly */
360
361                 SAFE_FREE(state->response.extra_data);
362                 
363                 /* Remove from list and free */
364                 
365                 DLIST_REMOVE(client_list, state);
366                 SAFE_FREE(state);
367                 num_clients--;
368         }
369 }
370
371
372 /* Process a complete received packet from a client */
373
374 void winbind_process_packet(struct winbindd_cli_state *state)
375 {
376         /* Process request */
377         
378         state->pid = state->request.pid;
379         
380         process_request(state);
381
382         /* Update client state */
383         
384         state->read_buf_len = 0;
385         state->write_buf_len = sizeof(struct winbindd_response);
386
387         /* we might need to send it to the dual daemon */
388         if (opt_dual_daemon) {
389                 dual_send_request(state);
390         }
391 }
392
393 /* Read some data from a client connection */
394
395 void winbind_client_read(struct winbindd_cli_state *state)
396 {
397         int n;
398     
399         /* Read data */
400
401         do {
402
403                 n = read(state->sock, state->read_buf_len + 
404                          (char *)&state->request, 
405                          sizeof(state->request) - state->read_buf_len);
406
407         } while (n == -1 && errno == EINTR);
408         
409         DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n, sizeof(state->request) - n - state->read_buf_len ));
410
411         /* Read failed, kill client */
412         
413         if (n == -1 || n == 0) {
414                 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
415                          state->sock, state->pid, 
416                          (n == -1) ? strerror(errno) : "EOF"));
417                 
418                 state->finished = True;
419                 return;
420         }
421         
422         /* Update client state */
423         
424         state->read_buf_len += n;
425 }
426
427 /* Write some data to a client connection */
428
429 static void client_write(struct winbindd_cli_state *state)
430 {
431         char *data;
432         int num_written;
433         
434         /* Write some data */
435         
436         if (!state->write_extra_data) {
437
438                 /* Write response structure */
439                 
440                 data = (char *)&state->response + sizeof(state->response) - 
441                         state->write_buf_len;
442
443         } else {
444
445                 /* Write extra data */
446                 
447                 data = (char *)state->response.extra_data + 
448                         state->response.length - 
449                         sizeof(struct winbindd_response) - 
450                         state->write_buf_len;
451         }
452         
453         do {
454                 num_written = write(state->sock, data, state->write_buf_len);
455         } while (num_written == -1 && errno == EINTR);
456         
457         DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
458
459         /* Write failed, kill cilent */
460         
461         if (num_written == -1 || num_written == 0) {
462                 
463                 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
464                          state->sock, state->pid, 
465                          (num_written == -1) ? strerror(errno) : "EOF"));
466                 
467                 state->finished = True;
468
469                 SAFE_FREE(state->response.extra_data);
470
471                 return;
472         }
473         
474         /* Update client state */
475         
476         state->write_buf_len -= num_written;
477         
478         /* Have we written all data? */
479         
480         if (state->write_buf_len == 0) {
481                 
482                 /* Take care of extra data */
483                 
484                 if (state->write_extra_data) {
485
486                         SAFE_FREE(state->response.extra_data);
487
488                         state->write_extra_data = False;
489
490                         DEBUG(10,("client_write: client_write: complete response written.\n"));
491
492                 } else if (state->response.length > 
493                            sizeof(struct winbindd_response)) {
494                         
495                         /* Start writing extra data */
496
497                         state->write_buf_len = 
498                                 state->response.length -
499                                 sizeof(struct winbindd_response);
500                         
501                         DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
502
503                         state->write_extra_data = True;
504                 }
505         }
506 }
507
508 /* Process incoming clients on accept_sock.  We use a tricky non-blocking,
509    non-forking, non-threaded model which allows us to handle many
510    simultaneous connections while remaining impervious to many denial of
511    service attacks. */
512
513 static void process_loop(int accept_sock)
514 {
515         /* We'll be doing this a lot */
516
517         while (1) {
518                 struct winbindd_cli_state *state;
519                 fd_set r_fds, w_fds;
520                 int maxfd = accept_sock, selret;
521                 struct timeval timeout;
522
523                 /* Handle messages */
524
525                 message_dispatch();
526
527                 /* Free up temporary memory */
528
529                 lp_talloc_free();
530                 main_loop_talloc_free();
531
532                 /* Initialise fd lists for select() */
533
534                 FD_ZERO(&r_fds);
535                 FD_ZERO(&w_fds);
536                 FD_SET(accept_sock, &r_fds);
537
538                 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
539                 timeout.tv_usec = 0;
540
541                 if (opt_dual_daemon) {
542                         maxfd = dual_select_setup(&w_fds, maxfd);
543                 }
544
545                 /* Set up client readers and writers */
546
547                 state = client_list;
548
549                 while (state) {
550
551                         /* Dispose of client connection if it is marked as 
552                            finished */ 
553
554                         if (state->finished) {
555                                 struct winbindd_cli_state *next = state->next;
556
557                                 remove_client(state);
558                                 state = next;
559                                 continue;
560                         }
561
562                         /* Select requires we know the highest fd used */
563
564                         if (state->sock > maxfd)
565                                 maxfd = state->sock;
566
567                         /* Add fd for reading */
568
569                         if (state->read_buf_len != sizeof(state->request))
570                                 FD_SET(state->sock, &r_fds);
571
572                         /* Add fd for writing */
573
574                         if (state->write_buf_len)
575                                 FD_SET(state->sock, &w_fds);
576
577                         state = state->next;
578                 }
579
580                 /* Call select */
581         
582                 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
583
584                 if (selret == 0)
585                         continue;
586
587                 if ((selret == -1 && errno != EINTR) || selret == 0) {
588
589                         /* Select error, something is badly wrong */
590
591                         perror("select");
592                         exit(1);
593                 }
594
595                 /* Create a new connection if accept_sock readable */
596
597                 if (selret > 0) {
598
599                         if (opt_dual_daemon) {
600                                 dual_select(&w_fds);
601                         }
602
603                         if (FD_ISSET(accept_sock, &r_fds))
604                                 new_connection(accept_sock);
605             
606                         /* Process activity on client connections */
607             
608                         for (state = client_list; state; state = state->next) {
609                 
610                                 /* Data available for reading */
611                 
612                                 if (FD_ISSET(state->sock, &r_fds)) {
613                     
614                                         /* Read data */
615                     
616                                         winbind_client_read(state);
617
618                                         /* 
619                                          * If we have the start of a
620                                          * packet, then check the
621                                          * length field to make sure
622                                          * the client's not talking
623                                          * Mock Swedish.
624                                          */
625
626                                         if (state->read_buf_len >= sizeof(uint32)
627                                             && *(uint32 *) &state->request != sizeof(state->request)) {
628                                                 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
629                                                                 *(uint32 *) &state->request, sizeof(state->request)));
630
631                                                 remove_client(state);
632                                                 break;
633                                         }
634
635                                         /* A request packet might be 
636                                            complete */
637                     
638                                         if (state->read_buf_len == 
639                                             sizeof(state->request)) {
640                                                 winbind_process_packet(state);
641                                         }
642                                 }
643                 
644                                 /* Data available for writing */
645                 
646                                 if (FD_ISSET(state->sock, &w_fds))
647                                         client_write(state);
648                         }
649                 }
650
651 #if 0
652                 winbindd_check_cache_size(time(NULL));
653 #endif
654
655                 /* Check signal handling things */
656
657                 if (do_sigterm)
658                         terminate();
659
660                 if (do_sighup) {
661
662                         /* Flush winbindd cache */
663
664                         flush_caches();
665                         reload_services_file(True);
666                         do_sighup = False;
667                 }
668
669                 if (do_sigusr2) {
670                         print_winbindd_status();
671                         do_sigusr2 = False;
672                 }
673         }
674 }
675
676
677 /*
678   these are split out from the main winbindd for use by the background daemon
679  */
680 int winbind_setup_common(void)
681 {
682         load_interfaces();
683
684         if (!secrets_init()) {
685
686                 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
687                 return 1;
688
689         }
690
691         /* Get list of domains we look up requests for.  This includes the
692            domain which we are a member of as well as any trusted
693            domains. */ 
694
695         init_domain_list();
696
697         ZERO_STRUCT(server_state);
698
699         /* Winbind daemon initialisation */
700
701         if (!winbindd_param_init())
702                 return 1;
703
704         if (!winbindd_idmap_init())
705                 return 1;
706
707         /* Unblock all signals we are interested in as they may have been
708            blocked by the parent process. */
709
710         BlockSignals(False, SIGINT);
711         BlockSignals(False, SIGQUIT);
712         BlockSignals(False, SIGTERM);
713         BlockSignals(False, SIGUSR1);
714         BlockSignals(False, SIGUSR2);
715         BlockSignals(False, SIGHUP);
716
717         /* Setup signal handlers */
718         
719         CatchSignal(SIGINT, termination_handler);      /* Exit on these sigs */
720         CatchSignal(SIGQUIT, termination_handler);
721         CatchSignal(SIGTERM, termination_handler);
722
723         CatchSignal(SIGPIPE, SIG_IGN);                 /* Ignore sigpipe */
724
725         CatchSignal(SIGUSR2, sigusr2_handler);         /* Debugging sigs */
726         CatchSignal(SIGHUP, sighup_handler);
727
728         return 0;
729 }
730
731
732 /* Main function */
733
734 struct winbindd_state server_state;   /* Server state information */
735
736
737 static void usage(void)
738 {
739         printf("Usage: winbindd [options]\n");
740         printf("\t-i                interactive mode\n");
741         printf("\t-B                dual daemon mode\n");
742         printf("\t-n                disable cacheing\n");
743         printf("\t-d level          set debug level\n");
744         printf("\t-s configfile     choose smb.conf location\n");
745         printf("\t-h                show this help message\n");
746 }
747
748  int main(int argc, char **argv)
749 {
750         extern BOOL AllowDebugChange;
751         extern pstring global_myname;
752         extern fstring global_myworkgroup;
753         extern BOOL append_log;
754         pstring logfile;
755         int accept_sock;
756         BOOL interactive = False;
757         int opt;
758
759         /* glibc (?) likes to print "User defined signal 1" and exit if a
760            SIGUSR[12] is received before a handler is installed */
761
762         CatchSignal(SIGUSR1, SIG_IGN);
763         CatchSignal(SIGUSR2, SIG_IGN);
764
765         fault_setup((void (*)(void *))fault_quit );
766
767         /* Append to log file by default as we are a single process daemon
768            program. */
769
770         append_log = True;
771
772         snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
773         lp_set_logfile(logfile);
774
775         /* Initialise for running in non-root mode */
776
777         sec_init();
778
779         /* Set environment variable so we don't recursively call ourselves.
780            This may also be useful interactively. */
781
782         SETENV(WINBINDD_DONT_ENV, "1", 1);
783
784         /* Initialise samba/rpc client stuff */
785
786         while ((opt = getopt(argc, argv, "id:s:nhB")) != EOF) {
787                 switch (opt) {
788
789                         /* Don't become a daemon */
790                 case 'i':
791                         interactive = True;
792                         break;
793
794                         /* dual daemon system */
795                 case 'B':
796                         opt_dual_daemon = True;
797                         break;
798
799                         /* disable cacheing */
800                 case 'n':
801                         opt_nocache = True;
802                         break;
803
804                         /* Run with specified debug level */
805                 case 'd':
806                         DEBUGLEVEL = atoi(optarg);
807                         AllowDebugChange = False;
808                         break;
809
810                         /* Load a different smb.conf file */
811                 case 's':
812                         pstrcpy(dyn_CONFIGFILE,optarg);
813                         break;
814
815                 case 'h':
816                         usage();
817                         exit(0);
818
819                 default:
820                         printf("Unknown option %c\n", (char)opt);
821                         exit(1);
822                 }
823         }
824
825         snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
826         lp_set_logfile(logfile);
827         setup_logging("winbindd", interactive);
828         reopen_logs();
829
830         DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
831         DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
832
833         if (!reload_services_file(False)) {
834                 DEBUG(0, ("error opening config file\n"));
835                 exit(1);
836         }
837
838         /* Setup names. */
839
840         if (!*global_myname) {
841                 char *p;
842
843                 fstrcpy(global_myname, myhostname());
844                 p = strchr(global_myname, '.');
845                 if (p)
846                         *p = 0;
847         }
848
849         fstrcpy(global_myworkgroup, lp_workgroup());
850
851         if (!interactive) {
852                 become_daemon();
853                 pidfile_create("winbindd");
854         }
855
856 #if HAVE_SETPGID
857         /*
858          * If we're interactive we want to set our own process group for
859          * signal management.
860          */
861         if (interactive)
862                 setpgid( (pid_t)0, (pid_t)0);
863 #endif
864
865         if (opt_dual_daemon) {
866                 do_dual_daemon();
867         }
868
869         if (winbind_setup_common() != 0) {
870                 return 1;
871         }
872
873         /* Initialise messaging system */
874
875         if (!message_init()) {
876                 DEBUG(0, ("unable to initialise messaging system\n"));
877                 exit(1);
878         }
879
880         register_msg_pool_usage();
881
882         /* Create UNIX domain socket */
883         
884         if ((accept_sock = create_sock()) == -1) {
885                 DEBUG(0, ("failed to create socket\n"));
886                 return 1;
887         }
888
889         /* Loop waiting for requests */
890
891         process_loop(accept_sock);
892
893         uni_group_cache_shutdown();
894         return 0;
895 }