s3compat-only s3:winbindd Split event handlers from winbindd.c
[abartlet/samba.git/.git] / source3 / winbindd / 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    Copyright (C) Volker Lendecke 2004
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "popt_common.h"
27 #include "winbindd.h"
28 #include "nsswitch/winbind_client.h"
29 #include "../../nsswitch/libwbclient/wbc_async.h"
30 #include "librpc/gen_ndr/messaging.h"
31 #include "../librpc/gen_ndr/srv_lsa.h"
32 #include "../librpc/gen_ndr/srv_samr.h"
33 #include "secrets.h"
34
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_WINBIND
37
38 static bool opt_nocache = False;
39 static bool interactive = False;
40
41 extern bool override_logfile;
42
43 /**************************************************************************** **
44  Handle a fault..
45  **************************************************************************** */
46
47 static void fault_quit(void)
48 {
49         dump_core();
50 }
51
52 bool winbindd_use_idmap_cache(void)
53 {
54         return !opt_nocache;
55 }
56
57 bool winbindd_use_cache(void)
58 {
59         return !opt_nocache;
60 }
61
62 int main(int argc, char **argv, char **envp)
63 {
64         static bool is_daemon = False;
65         static bool Fork = True;
66         static bool log_stdout = False;
67         static bool no_process_group = False;
68         enum {
69                 OPT_DAEMON = 1000,
70                 OPT_FORK,
71                 OPT_NO_PROCESS_GROUP,
72                 OPT_LOG_STDOUT
73         };
74         struct poptOption long_options[] = {
75                 POPT_AUTOHELP
76                 { "stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
77                 { "foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Daemon in foreground mode" },
78                 { "no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
79                 { "daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
80                 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
81                 { "no-caching", 'n', POPT_ARG_NONE, NULL, 'n', "Disable caching" },
82                 POPT_COMMON_SAMBA
83                 POPT_TABLEEND
84         };
85         poptContext pc;
86         int opt;
87         TALLOC_CTX *frame = talloc_stackframe();
88         NTSTATUS status;
89
90         /* glibc (?) likes to print "User defined signal 1" and exit if a
91            SIGUSR[12] is received before a handler is installed */
92
93         CatchSignal(SIGUSR1, SIG_IGN);
94         CatchSignal(SIGUSR2, SIG_IGN);
95
96         fault_setup((void (*)(void *))fault_quit );
97         dump_core_setup("winbindd");
98
99         load_case_tables();
100
101         /* Initialise for running in non-root mode */
102
103         sec_init();
104
105         set_remote_machine_name("winbindd", False);
106
107         /* Set environment variable so we don't recursively call ourselves.
108            This may also be useful interactively. */
109
110         if ( !winbind_off() ) {
111                 DEBUG(0,("Failed to disable recusive winbindd calls.  Exiting.\n"));
112                 exit(1);
113         }
114
115         /* Initialise samba/rpc client stuff */
116
117         pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
118
119         while ((opt = poptGetNextOpt(pc)) != -1) {
120                 switch (opt) {
121                         /* Don't become a daemon */
122                 case OPT_DAEMON:
123                         is_daemon = True;
124                         break;
125                 case 'i':
126                         interactive = True;
127                         log_stdout = True;
128                         Fork = False;
129                         break;
130                 case OPT_FORK:
131                         Fork = false;
132                         break;
133                 case OPT_NO_PROCESS_GROUP:
134                         no_process_group = true;
135                         break;
136                 case OPT_LOG_STDOUT:
137                         log_stdout = true;
138                         break;
139                 case 'n':
140                         opt_nocache = true;
141                         break;
142                 default:
143                         d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
144                                   poptBadOption(pc, 0), poptStrerror(opt));
145                         poptPrintUsage(pc, stderr, 0);
146                         exit(1);
147                 }
148         }
149
150         if (is_daemon && interactive) {
151                 d_fprintf(stderr,"\nERROR: "
152                           "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
153                 poptPrintUsage(pc, stderr, 0);
154                 exit(1);
155         }
156
157         if (log_stdout && Fork) {
158                 d_fprintf(stderr, "\nERROR: "
159                           "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
160                 poptPrintUsage(pc, stderr, 0);
161                 exit(1);
162         }
163
164         poptFreeContext(pc);
165
166         if (!override_logfile) {
167                 char *lfile = NULL;
168                 if (asprintf(&lfile,"%s/log.winbindd",
169                                 get_dyn_LOGFILEBASE()) > 0) {
170                         lp_set_logfile(lfile);
171                         SAFE_FREE(lfile);
172                 }
173         }
174         setup_logging("winbindd", log_stdout);
175         reopen_logs();
176
177         DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
178         DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
179
180         if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
181                 DEBUG(0, ("error opening config file\n"));
182                 exit(1);
183         }
184
185         /* Initialise messaging system */
186
187         if (winbind_messaging_context() == NULL) {
188                 exit(1);
189         }
190
191         if (!winbindd_reload_services_file(NULL)) {
192                 DEBUG(0, ("error opening config file\n"));
193                 exit(1);
194         }
195
196         if (!directory_exist(lp_lockdir())) {
197                 mkdir(lp_lockdir(), 0755);
198         }
199
200         /* Setup names. */
201
202         if (!init_names())
203                 exit(1);
204
205         load_interfaces();
206
207         if (!secrets_init()) {
208
209                 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
210                 return False;
211         }
212
213         /* Unblock all signals we are interested in as they may have been
214            blocked by the parent process. */
215
216         BlockSignals(False, SIGINT);
217         BlockSignals(False, SIGQUIT);
218         BlockSignals(False, SIGTERM);
219         BlockSignals(False, SIGUSR1);
220         BlockSignals(False, SIGUSR2);
221         BlockSignals(False, SIGHUP);
222         BlockSignals(False, SIGCHLD);
223
224         if (!interactive)
225                 become_daemon(Fork, no_process_group, log_stdout);
226
227         pidfile_create("winbindd");
228
229 #if HAVE_SETPGID
230         /*
231          * If we're interactive we want to set our own process group for
232          * signal management.
233          */
234         if (interactive && !no_process_group)
235                 setpgid( (pid_t)0, (pid_t)0);
236 #endif
237
238         TimeInit();
239
240         /* Don't use winbindd_reinit_after_fork here as
241          * we're just starting up and haven't created any
242          * winbindd-specific resources we must free yet. JRA.
243          */
244
245         status = reinit_after_fork(winbind_messaging_context(),
246                                    winbind_event_context(),
247                                    procid_self(), false);
248         if (!NT_STATUS_IS_OK(status)) {
249                 DEBUG(0,("reinit_after_fork() failed\n"));
250                 exit(1);
251         }
252
253         winbindd_register_handlers();
254
255         rpc_lsarpc_init(NULL);
256         rpc_samr_init(NULL);
257
258         if (!init_system_info()) {
259                 DEBUG(0,("ERROR: failed to setup system user info.\n"));
260                 exit(1);
261         }
262
263         /* setup listen sockets */
264
265         if (!winbindd_setup_listeners()) {
266                 DEBUG(0,("winbindd_setup_listeners() failed\n"));
267                 exit(1);
268         }
269
270         TALLOC_FREE(frame);
271         /* Loop waiting for requests */
272         while (1) {
273                 frame = talloc_stackframe();
274
275                 if (tevent_loop_once(winbind_event_context()) == -1) {
276                         DEBUG(1, ("tevent_loop_once() failed: %s\n",
277                                   strerror(errno)));
278                         return 1;
279                 }
280
281                 TALLOC_FREE(frame);
282         }
283
284         return 0;
285 }