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