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