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