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 | #ifdef __OS2__
|
---|
25 | #define INCL_DOSEXCEPTIONS
|
---|
26 | #define INCL_DOSPROCESS
|
---|
27 | #define INCL_DOSMODULEMGR
|
---|
28 | #define INCL_LOADEXCEPTQ
|
---|
29 | #define INCL_FORKEXCEPTQ
|
---|
30 | #include "os2.h"
|
---|
31 | #include "exceptq.h"
|
---|
32 | #undef FILE_CREATE
|
---|
33 | #undef FILE_OPEN
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include "includes.h"
|
---|
37 | #include "popt_common.h"
|
---|
38 | #include "winbindd.h"
|
---|
39 | #include "nsswitch/winbind_client.h"
|
---|
40 | #include "nsswitch/wb_reqtrans.h"
|
---|
41 | #include "ntdomain.h"
|
---|
42 | #include "../librpc/gen_ndr/srv_lsa.h"
|
---|
43 | #include "../librpc/gen_ndr/srv_samr.h"
|
---|
44 | #include "secrets.h"
|
---|
45 | #include "idmap.h"
|
---|
46 | #include "lib/addrchange.h"
|
---|
47 | #include "serverid.h"
|
---|
48 | #include "auth.h"
|
---|
49 | #include "messages.h"
|
---|
50 |
|
---|
51 | #undef DBGC_CLASS
|
---|
52 | #define DBGC_CLASS DBGC_WINBIND
|
---|
53 |
|
---|
54 | static bool client_is_idle(struct winbindd_cli_state *state);
|
---|
55 | static void remove_client(struct winbindd_cli_state *state);
|
---|
56 |
|
---|
57 | static bool opt_nocache = False;
|
---|
58 | static bool interactive = False;
|
---|
59 |
|
---|
60 | extern bool override_logfile;
|
---|
61 |
|
---|
62 | struct messaging_context *winbind_messaging_context(void)
|
---|
63 | {
|
---|
64 | struct messaging_context *msg_ctx = server_messaging_context();
|
---|
65 | if (likely(msg_ctx != NULL)) {
|
---|
66 | return msg_ctx;
|
---|
67 | }
|
---|
68 | smb_panic("Could not init winbindd's messaging context.\n");
|
---|
69 | return NULL;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* Reload configuration */
|
---|
73 |
|
---|
74 | static bool reload_services_file(const char *lfile)
|
---|
75 | {
|
---|
76 | bool ret;
|
---|
77 |
|
---|
78 | if (lp_loaded()) {
|
---|
79 | char *fname = lp_configfile();
|
---|
80 |
|
---|
81 | if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
|
---|
82 | set_dyn_CONFIGFILE(fname);
|
---|
83 | }
|
---|
84 | TALLOC_FREE(fname);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /* if this is a child, restore the logfile to the special
|
---|
88 | name - <domain>, idmap, etc. */
|
---|
89 | if (lfile && *lfile) {
|
---|
90 | lp_set_logfile(lfile);
|
---|
91 | }
|
---|
92 |
|
---|
93 | reopen_logs();
|
---|
94 | ret = lp_load(get_dyn_CONFIGFILE(),False,False,True,True);
|
---|
95 |
|
---|
96 | reopen_logs();
|
---|
97 | load_interfaces();
|
---|
98 |
|
---|
99 | return(ret);
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /**************************************************************************** **
|
---|
104 | Handle a fault..
|
---|
105 | **************************************************************************** */
|
---|
106 |
|
---|
107 | static void fault_quit(void)
|
---|
108 | {
|
---|
109 | dump_core();
|
---|
110 | }
|
---|
111 |
|
---|
112 | static void winbindd_status(void)
|
---|
113 | {
|
---|
114 | struct winbindd_cli_state *tmp;
|
---|
115 |
|
---|
116 | DEBUG(0, ("winbindd status:\n"));
|
---|
117 |
|
---|
118 | /* Print client state information */
|
---|
119 |
|
---|
120 | DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
|
---|
121 |
|
---|
122 | if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
|
---|
123 | DEBUG(2, ("\tclient list:\n"));
|
---|
124 | for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
|
---|
125 | DEBUGADD(2, ("\t\tpid %lu, sock %d (%s)\n",
|
---|
126 | (unsigned long)tmp->pid, tmp->sock,
|
---|
127 | client_is_idle(tmp) ? "idle" : "active"));
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* Flush client cache */
|
---|
133 |
|
---|
134 | static void flush_caches(void)
|
---|
135 | {
|
---|
136 | /* We need to invalidate cached user list entries on a SIGHUP
|
---|
137 | otherwise cached access denied errors due to restrict anonymous
|
---|
138 | hang around until the sequence number changes. */
|
---|
139 |
|
---|
140 | if (!wcache_invalidate_cache()) {
|
---|
141 | DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
|
---|
142 | if (!winbindd_cache_validate_and_initialize()) {
|
---|
143 | exit(1);
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | static void flush_caches_noinit(void)
|
---|
149 | {
|
---|
150 | /*
|
---|
151 | * We need to invalidate cached user list entries on a SIGHUP
|
---|
152 | * otherwise cached access denied errors due to restrict anonymous
|
---|
153 | * hang around until the sequence number changes.
|
---|
154 | * NB
|
---|
155 | * Skip uninitialized domains when flush cache.
|
---|
156 | * If domain is not initialized, it means it is never
|
---|
157 | * used or never become online. look, wcache_invalidate_cache()
|
---|
158 | * -> get_cache() -> init_dc_connection(). It causes a lot of traffic
|
---|
159 | * for unused domains and large traffic for primay domain's DC if there
|
---|
160 | * are many domains..
|
---|
161 | */
|
---|
162 |
|
---|
163 | if (!wcache_invalidate_cache_noinit()) {
|
---|
164 | DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
|
---|
165 | if (!winbindd_cache_validate_and_initialize()) {
|
---|
166 | exit(1);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* Handle the signal by unlinking socket and exiting */
|
---|
172 |
|
---|
173 | static void terminate(bool is_parent)
|
---|
174 | {
|
---|
175 | if (is_parent) {
|
---|
176 | /* When parent goes away we should
|
---|
177 | * remove the socket file. Not so
|
---|
178 | * when children terminate.
|
---|
179 | */
|
---|
180 | char *path = NULL;
|
---|
181 |
|
---|
182 | if (asprintf(&path, "%s/%s",
|
---|
183 | get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME) > 0) {
|
---|
184 | unlink(path);
|
---|
185 | SAFE_FREE(path);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | idmap_close();
|
---|
190 |
|
---|
191 | trustdom_cache_shutdown();
|
---|
192 |
|
---|
193 | gencache_stabilize();
|
---|
194 |
|
---|
195 | #if 0
|
---|
196 | if (interactive) {
|
---|
197 | TALLOC_CTX *mem_ctx = talloc_init("end_description");
|
---|
198 | char *description = talloc_describe_all(mem_ctx);
|
---|
199 |
|
---|
200 | DEBUG(3, ("tallocs left:\n%s\n", description));
|
---|
201 | talloc_destroy(mem_ctx);
|
---|
202 | }
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | if (is_parent) {
|
---|
206 | serverid_deregister(procid_self());
|
---|
207 | pidfile_unlink();
|
---|
208 | }
|
---|
209 |
|
---|
210 | exit(0);
|
---|
211 | }
|
---|
212 |
|
---|
213 | static void winbindd_sig_term_handler(struct tevent_context *ev,
|
---|
214 | struct tevent_signal *se,
|
---|
215 | int signum,
|
---|
216 | int count,
|
---|
217 | void *siginfo,
|
---|
218 | void *private_data)
|
---|
219 | {
|
---|
220 | bool *is_parent = talloc_get_type_abort(private_data, bool);
|
---|
221 |
|
---|
222 | DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
|
---|
223 | signum, (int)*is_parent));
|
---|
224 | terminate(*is_parent);
|
---|
225 | }
|
---|
226 |
|
---|
227 | bool winbindd_setup_sig_term_handler(bool parent)
|
---|
228 | {
|
---|
229 | struct tevent_signal *se;
|
---|
230 | bool *is_parent;
|
---|
231 |
|
---|
232 | is_parent = talloc(winbind_event_context(), bool);
|
---|
233 | if (!is_parent) {
|
---|
234 | return false;
|
---|
235 | }
|
---|
236 |
|
---|
237 | *is_parent = parent;
|
---|
238 |
|
---|
239 | se = tevent_add_signal(winbind_event_context(),
|
---|
240 | is_parent,
|
---|
241 | SIGTERM, 0,
|
---|
242 | winbindd_sig_term_handler,
|
---|
243 | is_parent);
|
---|
244 | if (!se) {
|
---|
245 | DEBUG(0,("failed to setup SIGTERM handler"));
|
---|
246 | talloc_free(is_parent);
|
---|
247 | return false;
|
---|
248 | }
|
---|
249 |
|
---|
250 | se = tevent_add_signal(winbind_event_context(),
|
---|
251 | is_parent,
|
---|
252 | SIGINT, 0,
|
---|
253 | winbindd_sig_term_handler,
|
---|
254 | is_parent);
|
---|
255 | if (!se) {
|
---|
256 | DEBUG(0,("failed to setup SIGINT handler"));
|
---|
257 | talloc_free(is_parent);
|
---|
258 | return false;
|
---|
259 | }
|
---|
260 |
|
---|
261 | se = tevent_add_signal(winbind_event_context(),
|
---|
262 | is_parent,
|
---|
263 | SIGQUIT, 0,
|
---|
264 | winbindd_sig_term_handler,
|
---|
265 | is_parent);
|
---|
266 | if (!se) {
|
---|
267 | DEBUG(0,("failed to setup SIGINT handler"));
|
---|
268 | talloc_free(is_parent);
|
---|
269 | return false;
|
---|
270 | }
|
---|
271 |
|
---|
272 | return true;
|
---|
273 | }
|
---|
274 |
|
---|
275 | static void winbindd_sig_hup_handler(struct tevent_context *ev,
|
---|
276 | struct tevent_signal *se,
|
---|
277 | int signum,
|
---|
278 | int count,
|
---|
279 | void *siginfo,
|
---|
280 | void *private_data)
|
---|
281 | {
|
---|
282 | const char *file = (const char *)private_data;
|
---|
283 |
|
---|
284 | DEBUG(1,("Reloading services after SIGHUP\n"));
|
---|
285 | flush_caches_noinit();
|
---|
286 | reload_services_file(file);
|
---|
287 | }
|
---|
288 |
|
---|
289 | bool winbindd_setup_sig_hup_handler(const char *lfile)
|
---|
290 | {
|
---|
291 | struct tevent_signal *se;
|
---|
292 | char *file = NULL;
|
---|
293 |
|
---|
294 | if (lfile) {
|
---|
295 | file = talloc_strdup(winbind_event_context(),
|
---|
296 | lfile);
|
---|
297 | if (!file) {
|
---|
298 | return false;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | se = tevent_add_signal(winbind_event_context(),
|
---|
303 | winbind_event_context(),
|
---|
304 | SIGHUP, 0,
|
---|
305 | winbindd_sig_hup_handler,
|
---|
306 | file);
|
---|
307 | if (!se) {
|
---|
308 | return false;
|
---|
309 | }
|
---|
310 |
|
---|
311 | return true;
|
---|
312 | }
|
---|
313 |
|
---|
314 | static void winbindd_sig_chld_handler(struct tevent_context *ev,
|
---|
315 | struct tevent_signal *se,
|
---|
316 | int signum,
|
---|
317 | int count,
|
---|
318 | void *siginfo,
|
---|
319 | void *private_data)
|
---|
320 | {
|
---|
321 | pid_t pid;
|
---|
322 |
|
---|
323 | while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
|
---|
324 | winbind_child_died(pid);
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | static bool winbindd_setup_sig_chld_handler(void)
|
---|
329 | {
|
---|
330 | struct tevent_signal *se;
|
---|
331 |
|
---|
332 | se = tevent_add_signal(winbind_event_context(),
|
---|
333 | winbind_event_context(),
|
---|
334 | SIGCHLD, 0,
|
---|
335 | winbindd_sig_chld_handler,
|
---|
336 | NULL);
|
---|
337 | if (!se) {
|
---|
338 | return false;
|
---|
339 | }
|
---|
340 |
|
---|
341 | return true;
|
---|
342 | }
|
---|
343 |
|
---|
344 | static void winbindd_sig_usr2_handler(struct tevent_context *ev,
|
---|
345 | struct tevent_signal *se,
|
---|
346 | int signum,
|
---|
347 | int count,
|
---|
348 | void *siginfo,
|
---|
349 | void *private_data)
|
---|
350 | {
|
---|
351 | winbindd_status();
|
---|
352 | }
|
---|
353 |
|
---|
354 | static bool winbindd_setup_sig_usr2_handler(void)
|
---|
355 | {
|
---|
356 | struct tevent_signal *se;
|
---|
357 |
|
---|
358 | se = tevent_add_signal(winbind_event_context(),
|
---|
359 | winbind_event_context(),
|
---|
360 | SIGUSR2, 0,
|
---|
361 | winbindd_sig_usr2_handler,
|
---|
362 | NULL);
|
---|
363 | if (!se) {
|
---|
364 | return false;
|
---|
365 | }
|
---|
366 |
|
---|
367 | return true;
|
---|
368 | }
|
---|
369 |
|
---|
370 | /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
|
---|
371 | static void msg_reload_services(struct messaging_context *msg,
|
---|
372 | void *private_data,
|
---|
373 | uint32_t msg_type,
|
---|
374 | struct server_id server_id,
|
---|
375 | DATA_BLOB *data)
|
---|
376 | {
|
---|
377 | /* Flush various caches */
|
---|
378 | flush_caches();
|
---|
379 | reload_services_file((const char *) private_data);
|
---|
380 | }
|
---|
381 |
|
---|
382 | /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
|
---|
383 | static void msg_shutdown(struct messaging_context *msg,
|
---|
384 | void *private_data,
|
---|
385 | uint32_t msg_type,
|
---|
386 | struct server_id server_id,
|
---|
387 | DATA_BLOB *data)
|
---|
388 | {
|
---|
389 | /* only the parent waits for this message */
|
---|
390 | DEBUG(0,("Got shutdown message\n"));
|
---|
391 | terminate(true);
|
---|
392 | }
|
---|
393 |
|
---|
394 |
|
---|
395 | static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
|
---|
396 | void *private_data,
|
---|
397 | uint32_t msg_type,
|
---|
398 | struct server_id server_id,
|
---|
399 | DATA_BLOB *data)
|
---|
400 | {
|
---|
401 | uint8 ret;
|
---|
402 | pid_t child_pid;
|
---|
403 | NTSTATUS status;
|
---|
404 |
|
---|
405 | DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
|
---|
406 | "message.\n"));
|
---|
407 |
|
---|
408 | /*
|
---|
409 | * call the validation code from a child:
|
---|
410 | * so we don't block the main winbindd and the validation
|
---|
411 | * code can safely use fork/waitpid...
|
---|
412 | */
|
---|
413 | child_pid = sys_fork();
|
---|
414 |
|
---|
415 | if (child_pid == -1) {
|
---|
416 | DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
|
---|
417 | strerror(errno)));
|
---|
418 | return;
|
---|
419 | }
|
---|
420 |
|
---|
421 | if (child_pid != 0) {
|
---|
422 | /* parent */
|
---|
423 | DEBUG(5, ("winbind_msg_validate_cache: child created with "
|
---|
424 | "pid %d.\n", (int)child_pid));
|
---|
425 | return;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /* child */
|
---|
429 |
|
---|
430 | status = winbindd_reinit_after_fork(NULL, NULL);
|
---|
431 | if (!NT_STATUS_IS_OK(status)) {
|
---|
432 | DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
|
---|
433 | nt_errstr(status)));
|
---|
434 | _exit(0);
|
---|
435 | }
|
---|
436 |
|
---|
437 | /* install default SIGCHLD handler: validation code uses fork/waitpid */
|
---|
438 | CatchSignal(SIGCHLD, SIG_DFL);
|
---|
439 |
|
---|
440 | ret = (uint8)winbindd_validate_cache_nobackup();
|
---|
441 | DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
|
---|
442 | messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
|
---|
443 | (size_t)1);
|
---|
444 | _exit(0);
|
---|
445 | }
|
---|
446 |
|
---|
447 | static struct winbindd_dispatch_table {
|
---|
448 | enum winbindd_cmd cmd;
|
---|
449 | void (*fn)(struct winbindd_cli_state *state);
|
---|
450 | const char *winbindd_cmd_name;
|
---|
451 | } dispatch_table[] = {
|
---|
452 |
|
---|
453 | /* Enumeration functions */
|
---|
454 |
|
---|
455 | { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
|
---|
456 | "LIST_TRUSTDOM" },
|
---|
457 |
|
---|
458 | /* Miscellaneous */
|
---|
459 |
|
---|
460 | { WINBINDD_INFO, winbindd_info, "INFO" },
|
---|
461 | { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
|
---|
462 | "INTERFACE_VERSION" },
|
---|
463 | { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
|
---|
464 | { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
|
---|
465 | { WINBINDD_DC_INFO, winbindd_dc_info, "DC_INFO" },
|
---|
466 | { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
|
---|
467 | { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
|
---|
468 | "WINBINDD_PRIV_PIPE_DIR" },
|
---|
469 |
|
---|
470 | /* Credential cache access */
|
---|
471 | { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
|
---|
472 | { WINBINDD_CCACHE_SAVE, winbindd_ccache_save, "CCACHE_SAVE" },
|
---|
473 |
|
---|
474 | /* WINS functions */
|
---|
475 |
|
---|
476 | { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
|
---|
477 | { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
|
---|
478 |
|
---|
479 | /* End of list */
|
---|
480 |
|
---|
481 | { WINBINDD_NUM_CMDS, NULL, "NONE" }
|
---|
482 | };
|
---|
483 |
|
---|
484 | struct winbindd_async_dispatch_table {
|
---|
485 | enum winbindd_cmd cmd;
|
---|
486 | const char *cmd_name;
|
---|
487 | struct tevent_req *(*send_req)(TALLOC_CTX *mem_ctx,
|
---|
488 | struct tevent_context *ev,
|
---|
489 | struct winbindd_cli_state *cli,
|
---|
490 | struct winbindd_request *request);
|
---|
491 | NTSTATUS (*recv_req)(struct tevent_req *req,
|
---|
492 | struct winbindd_response *presp);
|
---|
493 | };
|
---|
494 |
|
---|
495 | static struct winbindd_async_dispatch_table async_nonpriv_table[] = {
|
---|
496 | { WINBINDD_PING, "PING",
|
---|
497 | wb_ping_send, wb_ping_recv },
|
---|
498 | { WINBINDD_LOOKUPSID, "LOOKUPSID",
|
---|
499 | winbindd_lookupsid_send, winbindd_lookupsid_recv },
|
---|
500 | { WINBINDD_LOOKUPSIDS, "LOOKUPSIDS",
|
---|
501 | winbindd_lookupsids_send, winbindd_lookupsids_recv },
|
---|
502 | { WINBINDD_LOOKUPNAME, "LOOKUPNAME",
|
---|
503 | winbindd_lookupname_send, winbindd_lookupname_recv },
|
---|
504 | { WINBINDD_SID_TO_UID, "SID_TO_UID",
|
---|
505 | winbindd_sid_to_uid_send, winbindd_sid_to_uid_recv },
|
---|
506 | { WINBINDD_SID_TO_GID, "SID_TO_GID",
|
---|
507 | winbindd_sid_to_gid_send, winbindd_sid_to_gid_recv },
|
---|
508 | { WINBINDD_UID_TO_SID, "UID_TO_SID",
|
---|
509 | winbindd_uid_to_sid_send, winbindd_uid_to_sid_recv },
|
---|
510 | { WINBINDD_GID_TO_SID, "GID_TO_SID",
|
---|
511 | winbindd_gid_to_sid_send, winbindd_gid_to_sid_recv },
|
---|
512 | { WINBINDD_SIDS_TO_XIDS, "SIDS_TO_XIDS",
|
---|
513 | winbindd_sids_to_xids_send, winbindd_sids_to_xids_recv },
|
---|
514 | { WINBINDD_GETPWSID, "GETPWSID",
|
---|
515 | winbindd_getpwsid_send, winbindd_getpwsid_recv },
|
---|
516 | { WINBINDD_GETPWNAM, "GETPWNAM",
|
---|
517 | winbindd_getpwnam_send, winbindd_getpwnam_recv },
|
---|
518 | { WINBINDD_GETPWUID, "GETPWUID",
|
---|
519 | winbindd_getpwuid_send, winbindd_getpwuid_recv },
|
---|
520 | { WINBINDD_GETSIDALIASES, "GETSIDALIASES",
|
---|
521 | winbindd_getsidaliases_send, winbindd_getsidaliases_recv },
|
---|
522 | { WINBINDD_GETUSERDOMGROUPS, "GETUSERDOMGROUPS",
|
---|
523 | winbindd_getuserdomgroups_send, winbindd_getuserdomgroups_recv },
|
---|
524 | { WINBINDD_GETGROUPS, "GETGROUPS",
|
---|
525 | winbindd_getgroups_send, winbindd_getgroups_recv },
|
---|
526 | { WINBINDD_SHOW_SEQUENCE, "SHOW_SEQUENCE",
|
---|
527 | winbindd_show_sequence_send, winbindd_show_sequence_recv },
|
---|
528 | { WINBINDD_GETGRGID, "GETGRGID",
|
---|
529 | winbindd_getgrgid_send, winbindd_getgrgid_recv },
|
---|
530 | { WINBINDD_GETGRNAM, "GETGRNAM",
|
---|
531 | winbindd_getgrnam_send, winbindd_getgrnam_recv },
|
---|
532 | { WINBINDD_GETUSERSIDS, "GETUSERSIDS",
|
---|
533 | winbindd_getusersids_send, winbindd_getusersids_recv },
|
---|
534 | { WINBINDD_LOOKUPRIDS, "LOOKUPRIDS",
|
---|
535 | winbindd_lookuprids_send, winbindd_lookuprids_recv },
|
---|
536 | { WINBINDD_SETPWENT, "SETPWENT",
|
---|
537 | winbindd_setpwent_send, winbindd_setpwent_recv },
|
---|
538 | { WINBINDD_GETPWENT, "GETPWENT",
|
---|
539 | winbindd_getpwent_send, winbindd_getpwent_recv },
|
---|
540 | { WINBINDD_ENDPWENT, "ENDPWENT",
|
---|
541 | winbindd_endpwent_send, winbindd_endpwent_recv },
|
---|
542 | { WINBINDD_DSGETDCNAME, "DSGETDCNAME",
|
---|
543 | winbindd_dsgetdcname_send, winbindd_dsgetdcname_recv },
|
---|
544 | { WINBINDD_GETDCNAME, "GETDCNAME",
|
---|
545 | winbindd_getdcname_send, winbindd_getdcname_recv },
|
---|
546 | { WINBINDD_SETGRENT, "SETGRENT",
|
---|
547 | winbindd_setgrent_send, winbindd_setgrent_recv },
|
---|
548 | { WINBINDD_GETGRENT, "GETGRENT",
|
---|
549 | winbindd_getgrent_send, winbindd_getgrent_recv },
|
---|
550 | { WINBINDD_ENDGRENT, "ENDGRENT",
|
---|
551 | winbindd_endgrent_send, winbindd_endgrent_recv },
|
---|
552 | { WINBINDD_LIST_USERS, "LIST_USERS",
|
---|
553 | winbindd_list_users_send, winbindd_list_users_recv },
|
---|
554 | { WINBINDD_LIST_GROUPS, "LIST_GROUPS",
|
---|
555 | winbindd_list_groups_send, winbindd_list_groups_recv },
|
---|
556 | { WINBINDD_CHECK_MACHACC, "CHECK_MACHACC",
|
---|
557 | winbindd_check_machine_acct_send, winbindd_check_machine_acct_recv },
|
---|
558 | { WINBINDD_PING_DC, "PING_DC",
|
---|
559 | winbindd_ping_dc_send, winbindd_ping_dc_recv },
|
---|
560 | { WINBINDD_PAM_AUTH, "PAM_AUTH",
|
---|
561 | winbindd_pam_auth_send, winbindd_pam_auth_recv },
|
---|
562 | { WINBINDD_PAM_LOGOFF, "PAM_LOGOFF",
|
---|
563 | winbindd_pam_logoff_send, winbindd_pam_logoff_recv },
|
---|
564 | { WINBINDD_PAM_CHAUTHTOK, "PAM_CHAUTHTOK",
|
---|
565 | winbindd_pam_chauthtok_send, winbindd_pam_chauthtok_recv },
|
---|
566 | { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, "PAM_CHNG_PSWD_AUTH_CRAP",
|
---|
567 | winbindd_pam_chng_pswd_auth_crap_send,
|
---|
568 | winbindd_pam_chng_pswd_auth_crap_recv },
|
---|
569 |
|
---|
570 | { 0, NULL, NULL, NULL }
|
---|
571 | };
|
---|
572 |
|
---|
573 | static struct winbindd_async_dispatch_table async_priv_table[] = {
|
---|
574 | { WINBINDD_ALLOCATE_UID, "ALLOCATE_UID",
|
---|
575 | winbindd_allocate_uid_send, winbindd_allocate_uid_recv },
|
---|
576 | { WINBINDD_ALLOCATE_GID, "ALLOCATE_GID",
|
---|
577 | winbindd_allocate_gid_send, winbindd_allocate_gid_recv },
|
---|
578 | { WINBINDD_CHANGE_MACHACC, "CHANGE_MACHACC",
|
---|
579 | winbindd_change_machine_acct_send, winbindd_change_machine_acct_recv },
|
---|
580 | { WINBINDD_PAM_AUTH_CRAP, "PAM_AUTH_CRAP",
|
---|
581 | winbindd_pam_auth_crap_send, winbindd_pam_auth_crap_recv },
|
---|
582 |
|
---|
583 | { 0, NULL, NULL, NULL }
|
---|
584 | };
|
---|
585 |
|
---|
586 | static void wb_request_done(struct tevent_req *req);
|
---|
587 |
|
---|
588 | static void process_request(struct winbindd_cli_state *state)
|
---|
589 | {
|
---|
590 | struct winbindd_dispatch_table *table = dispatch_table;
|
---|
591 | struct winbindd_async_dispatch_table *atable;
|
---|
592 |
|
---|
593 | state->mem_ctx = talloc_named(state, 0, "winbind request");
|
---|
594 | if (state->mem_ctx == NULL)
|
---|
595 | return;
|
---|
596 |
|
---|
597 | /* Remember who asked us. */
|
---|
598 | state->pid = state->request->pid;
|
---|
599 |
|
---|
600 | state->cmd_name = "unknown request";
|
---|
601 | state->recv_fn = NULL;
|
---|
602 | state->last_access = time(NULL);
|
---|
603 |
|
---|
604 | /* Process command */
|
---|
605 |
|
---|
606 | for (atable = async_nonpriv_table; atable->send_req; atable += 1) {
|
---|
607 | if (state->request->cmd == atable->cmd) {
|
---|
608 | break;
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | if ((atable->send_req == NULL) && state->privileged) {
|
---|
613 | for (atable = async_priv_table; atable->send_req;
|
---|
614 | atable += 1) {
|
---|
615 | if (state->request->cmd == atable->cmd) {
|
---|
616 | break;
|
---|
617 | }
|
---|
618 | }
|
---|
619 | }
|
---|
620 |
|
---|
621 | if (atable->send_req != NULL) {
|
---|
622 | struct tevent_req *req;
|
---|
623 |
|
---|
624 | state->cmd_name = atable->cmd_name;
|
---|
625 | state->recv_fn = atable->recv_req;
|
---|
626 |
|
---|
627 | DEBUG(10, ("process_request: Handling async request %d:%s\n",
|
---|
628 | (int)state->pid, state->cmd_name));
|
---|
629 |
|
---|
630 | req = atable->send_req(state->mem_ctx, winbind_event_context(),
|
---|
631 | state, state->request);
|
---|
632 | if (req == NULL) {
|
---|
633 | DEBUG(0, ("process_request: atable->send failed for "
|
---|
634 | "%s\n", atable->cmd_name));
|
---|
635 | request_error(state);
|
---|
636 | return;
|
---|
637 | }
|
---|
638 | tevent_req_set_callback(req, wb_request_done, state);
|
---|
639 | return;
|
---|
640 | }
|
---|
641 |
|
---|
642 | state->response = talloc_zero(state->mem_ctx,
|
---|
643 | struct winbindd_response);
|
---|
644 | if (state->response == NULL) {
|
---|
645 | DEBUG(10, ("talloc failed\n"));
|
---|
646 | remove_client(state);
|
---|
647 | return;
|
---|
648 | }
|
---|
649 | state->response->result = WINBINDD_PENDING;
|
---|
650 | state->response->length = sizeof(struct winbindd_response);
|
---|
651 |
|
---|
652 | for (table = dispatch_table; table->fn; table++) {
|
---|
653 | if (state->request->cmd == table->cmd) {
|
---|
654 | DEBUG(10,("process_request: request fn %s\n",
|
---|
655 | table->winbindd_cmd_name ));
|
---|
656 | state->cmd_name = table->winbindd_cmd_name;
|
---|
657 | table->fn(state);
|
---|
658 | break;
|
---|
659 | }
|
---|
660 | }
|
---|
661 |
|
---|
662 | if (!table->fn) {
|
---|
663 | DEBUG(10,("process_request: unknown request fn number %d\n",
|
---|
664 | (int)state->request->cmd ));
|
---|
665 | request_error(state);
|
---|
666 | }
|
---|
667 | }
|
---|
668 |
|
---|
669 | static void wb_request_done(struct tevent_req *req)
|
---|
670 | {
|
---|
671 | struct winbindd_cli_state *state = tevent_req_callback_data(
|
---|
672 | req, struct winbindd_cli_state);
|
---|
673 | NTSTATUS status;
|
---|
674 |
|
---|
675 | state->response = talloc_zero(state->mem_ctx,
|
---|
676 | struct winbindd_response);
|
---|
677 | if (state->response == NULL) {
|
---|
678 | DEBUG(0, ("wb_request_done[%d:%s]: talloc_zero failed - removing client\n",
|
---|
679 | (int)state->pid, state->cmd_name));
|
---|
680 | remove_client(state);
|
---|
681 | return;
|
---|
682 | }
|
---|
683 | state->response->result = WINBINDD_PENDING;
|
---|
684 | state->response->length = sizeof(struct winbindd_response);
|
---|
685 |
|
---|
686 | status = state->recv_fn(req, state->response);
|
---|
687 | TALLOC_FREE(req);
|
---|
688 |
|
---|
689 | DEBUG(10,("wb_request_done[%d:%s]: %s\n",
|
---|
690 | (int)state->pid, state->cmd_name, nt_errstr(status)));
|
---|
691 |
|
---|
692 | if (!NT_STATUS_IS_OK(status)) {
|
---|
693 | request_error(state);
|
---|
694 | return;
|
---|
695 | }
|
---|
696 | request_ok(state);
|
---|
697 | }
|
---|
698 |
|
---|
699 | /*
|
---|
700 | * This is the main event loop of winbind requests. It goes through a
|
---|
701 | * state-machine of 3 read/write requests, 4 if you have extra data to send.
|
---|
702 | *
|
---|
703 | * An idle winbind client has a read request of 4 bytes outstanding,
|
---|
704 | * finalizing function is request_len_recv, checking the length. request_recv
|
---|
705 | * then processes the packet. The processing function then at some point has
|
---|
706 | * to call request_finished which schedules sending the response.
|
---|
707 | */
|
---|
708 |
|
---|
709 | static void request_finished(struct winbindd_cli_state *state);
|
---|
710 |
|
---|
711 | static void winbind_client_request_read(struct tevent_req *req);
|
---|
712 | static void winbind_client_response_written(struct tevent_req *req);
|
---|
713 |
|
---|
714 | static void request_finished(struct winbindd_cli_state *state)
|
---|
715 | {
|
---|
716 | struct tevent_req *req;
|
---|
717 |
|
---|
718 | TALLOC_FREE(state->request);
|
---|
719 |
|
---|
720 | req = wb_resp_write_send(state, winbind_event_context(),
|
---|
721 | state->out_queue, state->sock,
|
---|
722 | state->response);
|
---|
723 | if (req == NULL) {
|
---|
724 | DEBUG(10,("request_finished[%d:%s]: wb_resp_write_send() failed\n",
|
---|
725 | (int)state->pid, state->cmd_name));
|
---|
726 | remove_client(state);
|
---|
727 | return;
|
---|
728 | }
|
---|
729 | tevent_req_set_callback(req, winbind_client_response_written, state);
|
---|
730 | }
|
---|
731 |
|
---|
732 | static void winbind_client_response_written(struct tevent_req *req)
|
---|
733 | {
|
---|
734 | struct winbindd_cli_state *state = tevent_req_callback_data(
|
---|
735 | req, struct winbindd_cli_state);
|
---|
736 | ssize_t ret;
|
---|
737 | int err;
|
---|
738 |
|
---|
739 | ret = wb_resp_write_recv(req, &err);
|
---|
740 | TALLOC_FREE(req);
|
---|
741 | if (ret == -1) {
|
---|
742 | close(state->sock);
|
---|
743 | state->sock = -1;
|
---|
744 | DEBUG(2, ("Could not write response[%d:%s] to client: %s\n",
|
---|
745 | (int)state->pid, state->cmd_name, strerror(err)));
|
---|
746 | remove_client(state);
|
---|
747 | return;
|
---|
748 | }
|
---|
749 |
|
---|
750 | DEBUG(10,("winbind_client_response_written[%d:%s]: delivered response "
|
---|
751 | "to client\n", (int)state->pid, state->cmd_name));
|
---|
752 |
|
---|
753 | TALLOC_FREE(state->mem_ctx);
|
---|
754 | state->response = NULL;
|
---|
755 | state->cmd_name = "no request";
|
---|
756 | state->recv_fn = NULL;
|
---|
757 |
|
---|
758 | req = wb_req_read_send(state, winbind_event_context(), state->sock,
|
---|
759 | WINBINDD_MAX_EXTRA_DATA);
|
---|
760 | if (req == NULL) {
|
---|
761 | remove_client(state);
|
---|
762 | return;
|
---|
763 | }
|
---|
764 | tevent_req_set_callback(req, winbind_client_request_read, state);
|
---|
765 | }
|
---|
766 |
|
---|
767 | void request_error(struct winbindd_cli_state *state)
|
---|
768 | {
|
---|
769 | SMB_ASSERT(state->response->result == WINBINDD_PENDING);
|
---|
770 | state->response->result = WINBINDD_ERROR;
|
---|
771 | request_finished(state);
|
---|
772 | }
|
---|
773 |
|
---|
774 | void request_ok(struct winbindd_cli_state *state)
|
---|
775 | {
|
---|
776 | SMB_ASSERT(state->response->result == WINBINDD_PENDING);
|
---|
777 | state->response->result = WINBINDD_OK;
|
---|
778 | request_finished(state);
|
---|
779 | }
|
---|
780 |
|
---|
781 | /* Process a new connection by adding it to the client connection list */
|
---|
782 |
|
---|
783 | static void new_connection(int listen_sock, bool privileged)
|
---|
784 | {
|
---|
785 | struct sockaddr_un sunaddr;
|
---|
786 | struct winbindd_cli_state *state;
|
---|
787 | struct tevent_req *req;
|
---|
788 | socklen_t len;
|
---|
789 | int sock;
|
---|
790 |
|
---|
791 | /* Accept connection */
|
---|
792 |
|
---|
793 | len = sizeof(sunaddr);
|
---|
794 |
|
---|
795 | sock = accept(listen_sock, (struct sockaddr *)(void *)&sunaddr, &len);
|
---|
796 |
|
---|
797 | if (sock == -1) {
|
---|
798 | if (errno != EINTR) {
|
---|
799 | DEBUG(0, ("Faild to accept socket - %s\n",
|
---|
800 | strerror(errno)));
|
---|
801 | }
|
---|
802 | return;
|
---|
803 | }
|
---|
804 |
|
---|
805 | DEBUG(6,("accepted socket %d\n", sock));
|
---|
806 |
|
---|
807 | /* Create new connection structure */
|
---|
808 |
|
---|
809 | if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
|
---|
810 | close(sock);
|
---|
811 | return;
|
---|
812 | }
|
---|
813 |
|
---|
814 | state->sock = sock;
|
---|
815 |
|
---|
816 | state->out_queue = tevent_queue_create(state, "winbind client reply");
|
---|
817 | if (state->out_queue == NULL) {
|
---|
818 | close(sock);
|
---|
819 | TALLOC_FREE(state);
|
---|
820 | return;
|
---|
821 | }
|
---|
822 |
|
---|
823 | state->last_access = time(NULL);
|
---|
824 |
|
---|
825 | state->privileged = privileged;
|
---|
826 |
|
---|
827 | req = wb_req_read_send(state, winbind_event_context(), state->sock,
|
---|
828 | WINBINDD_MAX_EXTRA_DATA);
|
---|
829 | if (req == NULL) {
|
---|
830 | TALLOC_FREE(state);
|
---|
831 | close(sock);
|
---|
832 | return;
|
---|
833 | }
|
---|
834 | tevent_req_set_callback(req, winbind_client_request_read, state);
|
---|
835 |
|
---|
836 | /* Add to connection list */
|
---|
837 |
|
---|
838 | winbindd_add_client(state);
|
---|
839 | }
|
---|
840 |
|
---|
841 | static void winbind_client_request_read(struct tevent_req *req)
|
---|
842 | {
|
---|
843 | struct winbindd_cli_state *state = tevent_req_callback_data(
|
---|
844 | req, struct winbindd_cli_state);
|
---|
845 | ssize_t ret;
|
---|
846 | int err;
|
---|
847 |
|
---|
848 | ret = wb_req_read_recv(req, state, &state->request, &err);
|
---|
849 | TALLOC_FREE(req);
|
---|
850 | if (ret == -1) {
|
---|
851 | if (err == EPIPE) {
|
---|
852 | DEBUG(6, ("closing socket %d, client exited\n",
|
---|
853 | state->sock));
|
---|
854 | } else {
|
---|
855 | DEBUG(2, ("Could not read client request from fd %d: "
|
---|
856 | "%s\n", state->sock, strerror(err)));
|
---|
857 | }
|
---|
858 | close(state->sock);
|
---|
859 | state->sock = -1;
|
---|
860 | remove_client(state);
|
---|
861 | return;
|
---|
862 | }
|
---|
863 | process_request(state);
|
---|
864 | }
|
---|
865 |
|
---|
866 | /* Remove a client connection from client connection list */
|
---|
867 |
|
---|
868 | static void remove_client(struct winbindd_cli_state *state)
|
---|
869 | {
|
---|
870 | char c = 0;
|
---|
871 | int nwritten;
|
---|
872 |
|
---|
873 | /* It's a dead client - hold a funeral */
|
---|
874 |
|
---|
875 | if (state == NULL) {
|
---|
876 | return;
|
---|
877 | }
|
---|
878 |
|
---|
879 | if (state->sock != -1) {
|
---|
880 | /* tell client, we are closing ... */
|
---|
881 | nwritten = write(state->sock, &c, sizeof(c));
|
---|
882 | if (nwritten == -1) {
|
---|
883 | DEBUG(2, ("final write to client failed: %s\n",
|
---|
884 | strerror(errno)));
|
---|
885 | }
|
---|
886 |
|
---|
887 | /* Close socket */
|
---|
888 |
|
---|
889 | close(state->sock);
|
---|
890 | state->sock = -1;
|
---|
891 | }
|
---|
892 |
|
---|
893 | TALLOC_FREE(state->mem_ctx);
|
---|
894 |
|
---|
895 | /* Remove from list and free */
|
---|
896 |
|
---|
897 | winbindd_remove_client(state);
|
---|
898 | TALLOC_FREE(state);
|
---|
899 | }
|
---|
900 |
|
---|
901 | /* Is a client idle? */
|
---|
902 |
|
---|
903 | static bool client_is_idle(struct winbindd_cli_state *state) {
|
---|
904 | return (state->request == NULL &&
|
---|
905 | state->response == NULL &&
|
---|
906 | !state->pwent_state && !state->grent_state);
|
---|
907 | }
|
---|
908 |
|
---|
909 | /* Shutdown client connection which has been idle for the longest time */
|
---|
910 |
|
---|
911 | static bool remove_idle_client(void)
|
---|
912 | {
|
---|
913 | struct winbindd_cli_state *state, *remove_state = NULL;
|
---|
914 | time_t last_access = 0;
|
---|
915 | int nidle = 0;
|
---|
916 |
|
---|
917 | for (state = winbindd_client_list(); state; state = state->next) {
|
---|
918 | if (client_is_idle(state)) {
|
---|
919 | nidle++;
|
---|
920 | if (!last_access || state->last_access < last_access) {
|
---|
921 | last_access = state->last_access;
|
---|
922 | remove_state = state;
|
---|
923 | }
|
---|
924 | }
|
---|
925 | }
|
---|
926 |
|
---|
927 | if (remove_state) {
|
---|
928 | DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
|
---|
929 | nidle, remove_state->sock, (unsigned int)remove_state->pid));
|
---|
930 | remove_client(remove_state);
|
---|
931 | return True;
|
---|
932 | }
|
---|
933 |
|
---|
934 | return False;
|
---|
935 | }
|
---|
936 |
|
---|
937 | struct winbindd_listen_state {
|
---|
938 | bool privileged;
|
---|
939 | int fd;
|
---|
940 | };
|
---|
941 |
|
---|
942 | static void winbindd_listen_fde_handler(struct tevent_context *ev,
|
---|
943 | struct tevent_fd *fde,
|
---|
944 | uint16_t flags,
|
---|
945 | void *private_data)
|
---|
946 | {
|
---|
947 | struct winbindd_listen_state *s = talloc_get_type_abort(private_data,
|
---|
948 | struct winbindd_listen_state);
|
---|
949 |
|
---|
950 | while (winbindd_num_clients() > lp_winbind_max_clients() - 1) {
|
---|
951 | DEBUG(5,("winbindd: Exceeding %d client "
|
---|
952 | "connections, removing idle "
|
---|
953 | "connection.\n", lp_winbind_max_clients()));
|
---|
954 | if (!remove_idle_client()) {
|
---|
955 | DEBUG(0,("winbindd: Exceeding %d "
|
---|
956 | "client connections, no idle "
|
---|
957 | "connection found\n",
|
---|
958 | lp_winbind_max_clients()));
|
---|
959 | break;
|
---|
960 | }
|
---|
961 | }
|
---|
962 | new_connection(s->fd, s->privileged);
|
---|
963 | }
|
---|
964 |
|
---|
965 | /*
|
---|
966 | * Winbindd socket accessor functions
|
---|
967 | */
|
---|
968 |
|
---|
969 | const char *get_winbind_pipe_dir(void)
|
---|
970 | {
|
---|
971 | return lp_parm_const_string(-1, "winbindd", "socket dir", WINBINDD_SOCKET_DIR);
|
---|
972 | }
|
---|
973 |
|
---|
974 | char *get_winbind_priv_pipe_dir(void)
|
---|
975 | {
|
---|
976 | return lock_path(WINBINDD_PRIV_SOCKET_SUBDIR);
|
---|
977 | }
|
---|
978 |
|
---|
979 | static bool winbindd_setup_listeners(void)
|
---|
980 | {
|
---|
981 | struct winbindd_listen_state *pub_state = NULL;
|
---|
982 | struct winbindd_listen_state *priv_state = NULL;
|
---|
983 | struct tevent_fd *fde;
|
---|
984 |
|
---|
985 | pub_state = talloc(winbind_event_context(),
|
---|
986 | struct winbindd_listen_state);
|
---|
987 | if (!pub_state) {
|
---|
988 | goto failed;
|
---|
989 | }
|
---|
990 |
|
---|
991 | pub_state->privileged = false;
|
---|
992 | pub_state->fd = create_pipe_sock(
|
---|
993 | get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME, 0755);
|
---|
994 | if (pub_state->fd == -1) {
|
---|
995 | goto failed;
|
---|
996 | }
|
---|
997 |
|
---|
998 | fde = tevent_add_fd(winbind_event_context(), pub_state, pub_state->fd,
|
---|
999 | TEVENT_FD_READ, winbindd_listen_fde_handler,
|
---|
1000 | pub_state);
|
---|
1001 | if (fde == NULL) {
|
---|
1002 | close(pub_state->fd);
|
---|
1003 | goto failed;
|
---|
1004 | }
|
---|
1005 | tevent_fd_set_auto_close(fde);
|
---|
1006 |
|
---|
1007 | priv_state = talloc(winbind_event_context(),
|
---|
1008 | struct winbindd_listen_state);
|
---|
1009 | if (!priv_state) {
|
---|
1010 | goto failed;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | priv_state->privileged = true;
|
---|
1014 | priv_state->fd = create_pipe_sock(
|
---|
1015 | get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME, 0750);
|
---|
1016 | if (priv_state->fd == -1) {
|
---|
1017 | goto failed;
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | fde = tevent_add_fd(winbind_event_context(), priv_state,
|
---|
1021 | priv_state->fd, TEVENT_FD_READ,
|
---|
1022 | winbindd_listen_fde_handler, priv_state);
|
---|
1023 | if (fde == NULL) {
|
---|
1024 | close(priv_state->fd);
|
---|
1025 | goto failed;
|
---|
1026 | }
|
---|
1027 | tevent_fd_set_auto_close(fde);
|
---|
1028 |
|
---|
1029 | return true;
|
---|
1030 | failed:
|
---|
1031 | TALLOC_FREE(pub_state);
|
---|
1032 | TALLOC_FREE(priv_state);
|
---|
1033 | return false;
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | bool winbindd_use_idmap_cache(void)
|
---|
1037 | {
|
---|
1038 | return !opt_nocache;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | bool winbindd_use_cache(void)
|
---|
1042 | {
|
---|
1043 | return !opt_nocache;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | void winbindd_register_handlers(void)
|
---|
1047 | {
|
---|
1048 | /* Setup signal handlers */
|
---|
1049 |
|
---|
1050 | if (!winbindd_setup_sig_term_handler(true))
|
---|
1051 | exit(1);
|
---|
1052 | if (!winbindd_setup_sig_hup_handler(NULL))
|
---|
1053 | exit(1);
|
---|
1054 | if (!winbindd_setup_sig_chld_handler())
|
---|
1055 | exit(1);
|
---|
1056 | if (!winbindd_setup_sig_usr2_handler())
|
---|
1057 | exit(1);
|
---|
1058 |
|
---|
1059 | CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
|
---|
1060 |
|
---|
1061 | /*
|
---|
1062 | * Ensure all cache and idmap caches are consistent
|
---|
1063 | * and initialized before we startup.
|
---|
1064 | */
|
---|
1065 | if (!winbindd_cache_validate_and_initialize()) {
|
---|
1066 | exit(1);
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | /* get broadcast messages */
|
---|
1070 |
|
---|
1071 | if (!serverid_register(procid_self(),
|
---|
1072 | FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
|
---|
1073 | DEBUG(1, ("Could not register myself in serverid.tdb\n"));
|
---|
1074 | exit(1);
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | /* React on 'smbcontrol winbindd reload-config' in the same way
|
---|
1078 | as to SIGHUP signal */
|
---|
1079 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1080 | MSG_SMB_CONF_UPDATED, msg_reload_services);
|
---|
1081 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1082 | MSG_SHUTDOWN, msg_shutdown);
|
---|
1083 |
|
---|
1084 | /* Handle online/offline messages. */
|
---|
1085 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1086 | MSG_WINBIND_OFFLINE, winbind_msg_offline);
|
---|
1087 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1088 | MSG_WINBIND_ONLINE, winbind_msg_online);
|
---|
1089 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1090 | MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
|
---|
1091 |
|
---|
1092 | /* Handle domain online/offline messages for domains */
|
---|
1093 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1094 | MSG_WINBIND_DOMAIN_OFFLINE, winbind_msg_domain_offline);
|
---|
1095 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1096 | MSG_WINBIND_DOMAIN_ONLINE, winbind_msg_domain_online);
|
---|
1097 |
|
---|
1098 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1099 | MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
|
---|
1100 |
|
---|
1101 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1102 | MSG_WINBIND_VALIDATE_CACHE,
|
---|
1103 | winbind_msg_validate_cache);
|
---|
1104 |
|
---|
1105 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1106 | MSG_WINBIND_DUMP_DOMAIN_LIST,
|
---|
1107 | winbind_msg_dump_domain_list);
|
---|
1108 |
|
---|
1109 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1110 | MSG_WINBIND_IP_DROPPED,
|
---|
1111 | winbind_msg_ip_dropped_parent);
|
---|
1112 |
|
---|
1113 | /* Register handler for MSG_DEBUG. */
|
---|
1114 | messaging_register(winbind_messaging_context(), NULL,
|
---|
1115 | MSG_DEBUG,
|
---|
1116 | winbind_msg_debug);
|
---|
1117 |
|
---|
1118 | netsamlogon_cache_init(); /* Non-critical */
|
---|
1119 |
|
---|
1120 | /* clear the cached list of trusted domains */
|
---|
1121 |
|
---|
1122 | wcache_tdc_clear();
|
---|
1123 |
|
---|
1124 | if (!init_domain_list()) {
|
---|
1125 | DEBUG(0,("unable to initialize domain list\n"));
|
---|
1126 | exit(1);
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | init_idmap_child();
|
---|
1130 | init_locator_child();
|
---|
1131 |
|
---|
1132 | smb_nscd_flush_user_cache();
|
---|
1133 | smb_nscd_flush_group_cache();
|
---|
1134 |
|
---|
1135 | if (lp_allow_trusted_domains()) {
|
---|
1136 | if (tevent_add_timer(winbind_event_context(), NULL, timeval_zero(),
|
---|
1137 | rescan_trusted_domains, NULL) == NULL) {
|
---|
1138 | DEBUG(0, ("Could not trigger rescan_trusted_domains()\n"));
|
---|
1139 | exit(1);
|
---|
1140 | }
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | struct winbindd_addrchanged_state {
|
---|
1146 | struct addrchange_context *ctx;
|
---|
1147 | struct tevent_context *ev;
|
---|
1148 | struct messaging_context *msg_ctx;
|
---|
1149 | };
|
---|
1150 |
|
---|
1151 | static void winbindd_addr_changed(struct tevent_req *req);
|
---|
1152 |
|
---|
1153 | static void winbindd_init_addrchange(TALLOC_CTX *mem_ctx,
|
---|
1154 | struct tevent_context *ev,
|
---|
1155 | struct messaging_context *msg_ctx)
|
---|
1156 | {
|
---|
1157 | struct winbindd_addrchanged_state *state;
|
---|
1158 | struct tevent_req *req;
|
---|
1159 | NTSTATUS status;
|
---|
1160 |
|
---|
1161 | state = talloc(mem_ctx, struct winbindd_addrchanged_state);
|
---|
1162 | if (state == NULL) {
|
---|
1163 | DEBUG(10, ("talloc failed\n"));
|
---|
1164 | return;
|
---|
1165 | }
|
---|
1166 | state->ev = ev;
|
---|
1167 | state->msg_ctx = msg_ctx;
|
---|
1168 |
|
---|
1169 | status = addrchange_context_create(state, &state->ctx);
|
---|
1170 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1171 | DEBUG(10, ("addrchange_context_create failed: %s\n",
|
---|
1172 | nt_errstr(status)));
|
---|
1173 | TALLOC_FREE(state);
|
---|
1174 | return;
|
---|
1175 | }
|
---|
1176 | req = addrchange_send(state, ev, state->ctx);
|
---|
1177 | if (req == NULL) {
|
---|
1178 | DEBUG(0, ("addrchange_send failed\n"));
|
---|
1179 | TALLOC_FREE(state);
|
---|
1180 | return;
|
---|
1181 | }
|
---|
1182 | tevent_req_set_callback(req, winbindd_addr_changed, state);
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | static void winbindd_addr_changed(struct tevent_req *req)
|
---|
1186 | {
|
---|
1187 | struct winbindd_addrchanged_state *state = tevent_req_callback_data(
|
---|
1188 | req, struct winbindd_addrchanged_state);
|
---|
1189 | enum addrchange_type type;
|
---|
1190 | struct sockaddr_storage addr;
|
---|
1191 | NTSTATUS status;
|
---|
1192 |
|
---|
1193 | status = addrchange_recv(req, &type, &addr);
|
---|
1194 | TALLOC_FREE(req);
|
---|
1195 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1196 | DEBUG(10, ("addrchange_recv failed: %s, stop listening\n",
|
---|
1197 | nt_errstr(status)));
|
---|
1198 | TALLOC_FREE(state);
|
---|
1199 | return;
|
---|
1200 | }
|
---|
1201 | if (type == ADDRCHANGE_DEL) {
|
---|
1202 | char addrstr[INET6_ADDRSTRLEN];
|
---|
1203 | DATA_BLOB blob;
|
---|
1204 |
|
---|
1205 | print_sockaddr(addrstr, sizeof(addrstr), &addr);
|
---|
1206 |
|
---|
1207 | DEBUG(3, ("winbindd: kernel (AF_NETLINK) dropped ip %s\n",
|
---|
1208 | addrstr));
|
---|
1209 |
|
---|
1210 | blob = data_blob_const(addrstr, strlen(addrstr)+1);
|
---|
1211 |
|
---|
1212 | status = messaging_send(state->msg_ctx,
|
---|
1213 | messaging_server_id(state->msg_ctx),
|
---|
1214 | MSG_WINBIND_IP_DROPPED, &blob);
|
---|
1215 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1216 | DEBUG(10, ("messaging_send failed: %s - ignoring\n",
|
---|
1217 | nt_errstr(status)));
|
---|
1218 | }
|
---|
1219 | }
|
---|
1220 | req = addrchange_send(state, state->ev, state->ctx);
|
---|
1221 | if (req == NULL) {
|
---|
1222 | DEBUG(0, ("addrchange_send failed\n"));
|
---|
1223 | TALLOC_FREE(state);
|
---|
1224 | return;
|
---|
1225 | }
|
---|
1226 | tevent_req_set_callback(req, winbindd_addr_changed, state);
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | /* Main function */
|
---|
1230 |
|
---|
1231 | int main(int argc, char **argv, char **envp)
|
---|
1232 | {
|
---|
1233 | static bool is_daemon = False;
|
---|
1234 | static bool Fork = True;
|
---|
1235 | static bool log_stdout = False;
|
---|
1236 | static bool no_process_group = False;
|
---|
1237 | enum {
|
---|
1238 | OPT_DAEMON = 1000,
|
---|
1239 | OPT_FORK,
|
---|
1240 | OPT_NO_PROCESS_GROUP,
|
---|
1241 | OPT_LOG_STDOUT
|
---|
1242 | };
|
---|
1243 | struct poptOption long_options[] = {
|
---|
1244 | POPT_AUTOHELP
|
---|
1245 | { "stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
|
---|
1246 | { "foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Daemon in foreground mode" },
|
---|
1247 | { "no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
|
---|
1248 | { "daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
|
---|
1249 | { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
|
---|
1250 | { "no-caching", 'n', POPT_ARG_NONE, NULL, 'n', "Disable caching" },
|
---|
1251 | POPT_COMMON_SAMBA
|
---|
1252 | POPT_TABLEEND
|
---|
1253 | };
|
---|
1254 | poptContext pc;
|
---|
1255 | int opt;
|
---|
1256 | TALLOC_CTX *frame;
|
---|
1257 | NTSTATUS status;
|
---|
1258 |
|
---|
1259 | #ifdef __OS2__
|
---|
1260 | EXCEPTIONREGISTRATIONRECORD ExRegRec;
|
---|
1261 | LoadExceptq(&ExRegRec, NULL, NULL);
|
---|
1262 | #endif
|
---|
1263 | /*
|
---|
1264 | * Do this before any other talloc operation
|
---|
1265 | */
|
---|
1266 | talloc_enable_null_tracking();
|
---|
1267 | frame = talloc_stackframe();
|
---|
1268 |
|
---|
1269 | /* glibc (?) likes to print "User defined signal 1" and exit if a
|
---|
1270 | SIGUSR[12] is received before a handler is installed */
|
---|
1271 |
|
---|
1272 | CatchSignal(SIGUSR1, SIG_IGN);
|
---|
1273 | CatchSignal(SIGUSR2, SIG_IGN);
|
---|
1274 |
|
---|
1275 | fault_setup((void (*)(void *))fault_quit );
|
---|
1276 | dump_core_setup("winbindd");
|
---|
1277 |
|
---|
1278 | load_case_tables();
|
---|
1279 |
|
---|
1280 | /* Initialise for running in non-root mode */
|
---|
1281 |
|
---|
1282 | sec_init();
|
---|
1283 |
|
---|
1284 | set_remote_machine_name("winbindd", False);
|
---|
1285 |
|
---|
1286 | /* Set environment variable so we don't recursively call ourselves.
|
---|
1287 | This may also be useful interactively. */
|
---|
1288 |
|
---|
1289 | if ( !winbind_off() ) {
|
---|
1290 | DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
|
---|
1291 | exit(1);
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | /* Initialise samba/rpc client stuff */
|
---|
1295 |
|
---|
1296 | pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
|
---|
1297 |
|
---|
1298 | while ((opt = poptGetNextOpt(pc)) != -1) {
|
---|
1299 | switch (opt) {
|
---|
1300 | /* Don't become a daemon */
|
---|
1301 | case OPT_DAEMON:
|
---|
1302 | is_daemon = True;
|
---|
1303 | break;
|
---|
1304 | case 'i':
|
---|
1305 | interactive = True;
|
---|
1306 | log_stdout = True;
|
---|
1307 | Fork = False;
|
---|
1308 | break;
|
---|
1309 | case OPT_FORK:
|
---|
1310 | Fork = false;
|
---|
1311 | break;
|
---|
1312 | case OPT_NO_PROCESS_GROUP:
|
---|
1313 | no_process_group = true;
|
---|
1314 | break;
|
---|
1315 | case OPT_LOG_STDOUT:
|
---|
1316 | log_stdout = true;
|
---|
1317 | break;
|
---|
1318 | case 'n':
|
---|
1319 | opt_nocache = true;
|
---|
1320 | break;
|
---|
1321 | default:
|
---|
1322 | d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
|
---|
1323 | poptBadOption(pc, 0), poptStrerror(opt));
|
---|
1324 | poptPrintUsage(pc, stderr, 0);
|
---|
1325 | exit(1);
|
---|
1326 | }
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | /* We call dump_core_setup one more time because the command line can
|
---|
1330 | * set the log file or the log-basename and this will influence where
|
---|
1331 | * cores are stored. Without this call get_dyn_LOGFILEBASE will be
|
---|
1332 | * the default value derived from build's prefix. For EOM this value
|
---|
1333 | * is often not related to the path where winbindd is actually run
|
---|
1334 | * in production.
|
---|
1335 | */
|
---|
1336 | dump_core_setup("winbindd");
|
---|
1337 |
|
---|
1338 | if (is_daemon && interactive) {
|
---|
1339 | d_fprintf(stderr,"\nERROR: "
|
---|
1340 | "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
|
---|
1341 | poptPrintUsage(pc, stderr, 0);
|
---|
1342 | exit(1);
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | if (log_stdout && Fork) {
|
---|
1346 | d_fprintf(stderr, "\nERROR: "
|
---|
1347 | "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
|
---|
1348 | poptPrintUsage(pc, stderr, 0);
|
---|
1349 | exit(1);
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | poptFreeContext(pc);
|
---|
1353 |
|
---|
1354 | if (!override_logfile) {
|
---|
1355 | char *lfile = NULL;
|
---|
1356 | if (asprintf(&lfile,"%s/log.winbindd",
|
---|
1357 | get_dyn_LOGFILEBASE()) > 0) {
|
---|
1358 | lp_set_logfile(lfile);
|
---|
1359 | SAFE_FREE(lfile);
|
---|
1360 | }
|
---|
1361 | }
|
---|
1362 | if (log_stdout) {
|
---|
1363 | setup_logging("winbindd", DEBUG_STDOUT);
|
---|
1364 | } else {
|
---|
1365 | setup_logging("winbindd", DEBUG_FILE);
|
---|
1366 | }
|
---|
1367 | reopen_logs();
|
---|
1368 |
|
---|
1369 | DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
|
---|
1370 | DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
|
---|
1371 | #ifdef __OS2__
|
---|
1372 | DEBUGADD(0,("%s\n", maintained_by_string()));
|
---|
1373 | #endif
|
---|
1374 |
|
---|
1375 | if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
|
---|
1376 | DEBUG(0, ("error opening config file\n"));
|
---|
1377 | exit(1);
|
---|
1378 | }
|
---|
1379 | /* After parsing the configuration file we setup the core path one more time
|
---|
1380 | * as the log file might have been set in the configuration and cores's
|
---|
1381 | * path is by default basename(lp_logfile()).
|
---|
1382 | */
|
---|
1383 | dump_core_setup("winbindd");
|
---|
1384 |
|
---|
1385 | /* Initialise messaging system */
|
---|
1386 |
|
---|
1387 | if (winbind_messaging_context() == NULL) {
|
---|
1388 | exit(1);
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | if (!reload_services_file(NULL)) {
|
---|
1392 | DEBUG(0, ("error opening config file\n"));
|
---|
1393 | exit(1);
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | if (!directory_exist(lp_lockdir())) {
|
---|
1397 | mkdir(lp_lockdir(), 0755);
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | /* Setup names. */
|
---|
1401 |
|
---|
1402 | if (!init_names())
|
---|
1403 | exit(1);
|
---|
1404 |
|
---|
1405 | load_interfaces();
|
---|
1406 |
|
---|
1407 | if (!secrets_init()) {
|
---|
1408 |
|
---|
1409 | DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
|
---|
1410 | return False;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | /* Unblock all signals we are interested in as they may have been
|
---|
1414 | blocked by the parent process. */
|
---|
1415 |
|
---|
1416 | BlockSignals(False, SIGINT);
|
---|
1417 | BlockSignals(False, SIGQUIT);
|
---|
1418 | BlockSignals(False, SIGTERM);
|
---|
1419 | BlockSignals(False, SIGUSR1);
|
---|
1420 | BlockSignals(False, SIGUSR2);
|
---|
1421 | BlockSignals(False, SIGHUP);
|
---|
1422 | BlockSignals(False, SIGCHLD);
|
---|
1423 |
|
---|
1424 | if (!interactive)
|
---|
1425 | become_daemon(Fork, no_process_group, log_stdout);
|
---|
1426 |
|
---|
1427 | pidfile_create("winbindd");
|
---|
1428 |
|
---|
1429 | #if HAVE_SETPGID
|
---|
1430 | /*
|
---|
1431 | * If we're interactive we want to set our own process group for
|
---|
1432 | * signal management.
|
---|
1433 | */
|
---|
1434 | if (interactive && !no_process_group)
|
---|
1435 | setpgid( (pid_t)0, (pid_t)0);
|
---|
1436 | #endif
|
---|
1437 |
|
---|
1438 | TimeInit();
|
---|
1439 |
|
---|
1440 | /* Don't use winbindd_reinit_after_fork here as
|
---|
1441 | * we're just starting up and haven't created any
|
---|
1442 | * winbindd-specific resources we must free yet. JRA.
|
---|
1443 | */
|
---|
1444 |
|
---|
1445 | status = reinit_after_fork(winbind_messaging_context(),
|
---|
1446 | winbind_event_context(),
|
---|
1447 | procid_self(), false);
|
---|
1448 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1449 | DEBUG(0,("reinit_after_fork() failed\n"));
|
---|
1450 | exit(1);
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | winbindd_register_handlers();
|
---|
1454 |
|
---|
1455 | status = init_system_info();
|
---|
1456 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1457 | DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
|
---|
1458 | nt_errstr(status)));
|
---|
1459 | exit(1);
|
---|
1460 | }
|
---|
1461 |
|
---|
1462 | rpc_lsarpc_init(NULL);
|
---|
1463 | rpc_samr_init(NULL);
|
---|
1464 |
|
---|
1465 | winbindd_init_addrchange(NULL, winbind_event_context(),
|
---|
1466 | winbind_messaging_context());
|
---|
1467 |
|
---|
1468 | /* setup listen sockets */
|
---|
1469 |
|
---|
1470 | if (!winbindd_setup_listeners()) {
|
---|
1471 | DEBUG(0,("winbindd_setup_listeners() failed\n"));
|
---|
1472 | exit(1);
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | TALLOC_FREE(frame);
|
---|
1476 | /* Loop waiting for requests */
|
---|
1477 | while (1) {
|
---|
1478 | frame = talloc_stackframe();
|
---|
1479 |
|
---|
1480 | if (tevent_loop_once(winbind_event_context()) == -1) {
|
---|
1481 | DEBUG(1, ("tevent_loop_once() failed: %s\n",
|
---|
1482 | strerror(errno)));
|
---|
1483 | return 1;
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | TALLOC_FREE(frame);
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | #ifdef __OS2__
|
---|
1490 | UninstallExceptq(&ExRegRec);
|
---|
1491 | #endif
|
---|
1492 | return 0;
|
---|
1493 | }
|
---|