source: branches/samba-3.2.x/source/winbindd/winbindd.c@ 787

Last change on this file since 787 was 272, checked in by Herwig Bauernfeind, 16 years ago

Update 3.2 to 3.2.12

File size: 33.2 KB
Line 
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 "winbindd.h"
27
28#undef DBGC_CLASS
29#define DBGC_CLASS DBGC_WINBIND
30
31bool opt_nocache = False;
32static bool interactive = False;
33
34extern bool override_logfile;
35
36struct event_context *winbind_event_context(void)
37{
38 static struct event_context *ctx;
39
40 if (!ctx && !(ctx = event_context_init(NULL))) {
41 smb_panic("Could not init winbind event context");
42 }
43 return ctx;
44}
45
46struct messaging_context *winbind_messaging_context(void)
47{
48 static struct messaging_context *ctx;
49
50 if (!ctx && !(ctx = messaging_init(NULL, server_id_self(),
51 winbind_event_context()))) {
52 smb_panic("Could not init winbind messaging context");
53 }
54 return ctx;
55}
56
57/* Reload configuration */
58
59static bool reload_services_file(const char *logfile)
60{
61 bool ret;
62
63 if (lp_loaded()) {
64 const char *fname = lp_configfile();
65
66 if (file_exist(fname,NULL) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
67 set_dyn_CONFIGFILE(fname);
68 }
69 }
70
71 /* if this is a child, restore the logfile to the special
72 name - <domain>, idmap, etc. */
73 if (logfile && *logfile) {
74 lp_set_logfile(logfile);
75 }
76
77 reopen_logs();
78 ret = lp_load(get_dyn_CONFIGFILE(),False,False,True,True);
79
80 reopen_logs();
81 load_interfaces();
82
83 return(ret);
84}
85
86
87/**************************************************************************** **
88 Handle a fault..
89 **************************************************************************** */
90
91static void fault_quit(void)
92{
93 dump_core();
94}
95
96static void winbindd_status(void)
97{
98 struct winbindd_cli_state *tmp;
99
100 DEBUG(0, ("winbindd status:\n"));
101
102 /* Print client state information */
103
104 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
105
106 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
107 DEBUG(2, ("\tclient list:\n"));
108 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
109 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
110 (unsigned long)tmp->pid, tmp->sock));
111 }
112 }
113}
114
115/* Print winbindd status to log file */
116
117static void print_winbindd_status(void)
118{
119 winbindd_status();
120}
121
122/* Flush client cache */
123
124static void flush_caches(void)
125{
126 /* We need to invalidate cached user list entries on a SIGHUP
127 otherwise cached access denied errors due to restrict anonymous
128 hang around until the sequence number changes. */
129
130 if (!wcache_invalidate_cache()) {
131 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
132 if (!winbindd_cache_validate_and_initialize()) {
133 exit(1);
134 }
135 }
136}
137
138/* Handle the signal by unlinking socket and exiting */
139
140static void terminate(bool is_parent)
141{
142 if (is_parent) {
143 /* When parent goes away we should
144 * remove the socket file. Not so
145 * when children terminate.
146 */
147 char *path = NULL;
148
149 if (asprintf(&path, "%s/%s",
150 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME) > 0) {
151 unlink(path);
152 SAFE_FREE(path);
153 }
154 }
155
156 idmap_close();
157
158 trustdom_cache_shutdown();
159
160#if 0
161 if (interactive) {
162 TALLOC_CTX *mem_ctx = talloc_init("end_description");
163 char *description = talloc_describe_all(mem_ctx);
164
165 DEBUG(3, ("tallocs left:\n%s\n", description));
166 talloc_destroy(mem_ctx);
167 }
168#endif
169
170 exit(0);
171}
172
173static SIG_ATOMIC_T do_sigterm = 0;
174
175static void termination_handler(int signum)
176{
177 do_sigterm = 1;
178 sys_select_signal(signum);
179}
180
181static SIG_ATOMIC_T do_sigusr2 = 0;
182
183static void sigusr2_handler(int signum)
184{
185 do_sigusr2 = 1;
186 sys_select_signal(SIGUSR2);
187}
188
189static SIG_ATOMIC_T do_sighup = 0;
190
191static void sighup_handler(int signum)
192{
193 do_sighup = 1;
194 sys_select_signal(SIGHUP);
195}
196
197static SIG_ATOMIC_T do_sigchld = 0;
198
199static void sigchld_handler(int signum)
200{
201 do_sigchld = 1;
202 sys_select_signal(SIGCHLD);
203}
204
205/* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
206static void msg_reload_services(struct messaging_context *msg,
207 void *private_data,
208 uint32_t msg_type,
209 struct server_id server_id,
210 DATA_BLOB *data)
211{
212 /* Flush various caches */
213 flush_caches();
214 reload_services_file((const char *) private_data);
215}
216
217/* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
218static void msg_shutdown(struct messaging_context *msg,
219 void *private_data,
220 uint32_t msg_type,
221 struct server_id server_id,
222 DATA_BLOB *data)
223{
224 do_sigterm = 1;
225}
226
227
228static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
229 void *private_data,
230 uint32_t msg_type,
231 struct server_id server_id,
232 DATA_BLOB *data)
233{
234 uint8 ret;
235 pid_t child_pid;
236 struct sigaction act;
237 struct sigaction oldact;
238
239 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
240 "message.\n"));
241
242 /*
243 * call the validation code from a child:
244 * so we don't block the main winbindd and the validation
245 * code can safely use fork/waitpid...
246 */
247 CatchChild();
248 child_pid = sys_fork();
249
250 if (child_pid == -1) {
251 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
252 strerror(errno)));
253 return;
254 }
255
256 if (child_pid != 0) {
257 /* parent */
258 DEBUG(5, ("winbind_msg_validate_cache: child created with "
259 "pid %d.\n", child_pid));
260 return;
261 }
262
263 /* child */
264
265 /* install default SIGCHLD handler: validation code uses fork/waitpid */
266 ZERO_STRUCT(act);
267 act.sa_handler = SIG_DFL;
268#ifdef SA_RESTART
269 /* We *want* SIGALRM to interrupt a system call. */
270 act.sa_flags = SA_RESTART;
271#endif
272 sigemptyset(&act.sa_mask);
273 sigaddset(&act.sa_mask,SIGCHLD);
274 sigaction(SIGCHLD,&act,&oldact);
275
276 ret = (uint8)winbindd_validate_cache_nobackup();
277 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
278 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
279 (size_t)1);
280 _exit(0);
281}
282
283static struct winbindd_dispatch_table {
284 enum winbindd_cmd cmd;
285 void (*fn)(struct winbindd_cli_state *state);
286 const char *winbindd_cmd_name;
287} dispatch_table[] = {
288
289 /* User functions */
290
291 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
292 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
293
294 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
295 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
296 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
297
298 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
299 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
300 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
301 "GETUSERDOMGROUPS" },
302
303 /* Group functions */
304
305 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
306 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
307 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
308 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
309 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
310 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
311
312 /* PAM auth functions */
313
314 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
315 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
316 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
317 { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
318 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
319
320 /* Enumeration functions */
321
322 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
323 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
324 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
325 "LIST_TRUSTDOM" },
326 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
327
328 /* SID related functions */
329
330 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
331 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
332 { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
333
334 /* Lookup related functions */
335
336 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
337 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
338 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
339 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
340#if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
341 { WINBINDD_SIDS_TO_XIDS, winbindd_sids_to_unixids, "SIDS_TO_XIDS" },
342#endif /* end DISABLED */
343 { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
344 { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
345 { WINBINDD_SET_MAPPING, winbindd_set_mapping, "SET_MAPPING" },
346 { WINBINDD_SET_HWM, winbindd_set_hwm, "SET_HWMS" },
347
348 /* Miscellaneous */
349
350 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
351 { WINBINDD_PING, winbindd_ping, "PING" },
352 { WINBINDD_INFO, winbindd_info, "INFO" },
353 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
354 "INTERFACE_VERSION" },
355 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
356 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
357 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
358 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
359 "WINBINDD_PRIV_PIPE_DIR" },
360 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
361 { WINBINDD_DSGETDCNAME, winbindd_dsgetdcname, "DSGETDCNAME" },
362
363 /* Credential cache access */
364 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
365
366 /* WINS functions */
367
368 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
369 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
370
371 /* End of list */
372
373 { WINBINDD_NUM_CMDS, NULL, "NONE" }
374};
375
376static void process_request(struct winbindd_cli_state *state)
377{
378 struct winbindd_dispatch_table *table = dispatch_table;
379
380 /* Free response data - we may be interrupted and receive another
381 command before being able to send this data off. */
382
383 SAFE_FREE(state->response.extra_data.data);
384
385 ZERO_STRUCT(state->response);
386
387 state->response.result = WINBINDD_PENDING;
388 state->response.length = sizeof(struct winbindd_response);
389
390 state->mem_ctx = talloc_init("winbind request");
391 if (state->mem_ctx == NULL)
392 return;
393
394 /* Remember who asked us. */
395 state->pid = state->request.pid;
396
397 /* Process command */
398
399 for (table = dispatch_table; table->fn; table++) {
400 if (state->request.cmd == table->cmd) {
401 DEBUG(10,("process_request: request fn %s\n",
402 table->winbindd_cmd_name ));
403 table->fn(state);
404 break;
405 }
406 }
407
408 if (!table->fn) {
409 DEBUG(10,("process_request: unknown request fn number %d\n",
410 (int)state->request.cmd ));
411 request_error(state);
412 }
413}
414
415/*
416 * A list of file descriptors being monitored by select in the main processing
417 * loop. fd_event->handler is called whenever the socket is readable/writable.
418 */
419
420static struct fd_event *fd_events = NULL;
421
422void add_fd_event(struct fd_event *ev)
423{
424 struct fd_event *match;
425
426 /* only add unique fd_event structs */
427
428 for (match=fd_events; match; match=match->next ) {
429#ifdef DEVELOPER
430 SMB_ASSERT( match != ev );
431#else
432 if ( match == ev )
433 return;
434#endif
435 }
436
437 DLIST_ADD(fd_events, ev);
438}
439
440void remove_fd_event(struct fd_event *ev)
441{
442 DLIST_REMOVE(fd_events, ev);
443}
444
445/*
446 * Handler for fd_events to complete a read/write request, set up by
447 * setup_async_read/setup_async_write.
448 */
449
450static void rw_callback(struct fd_event *event, int flags)
451{
452 size_t todo;
453 ssize_t done = 0;
454
455 todo = event->length - event->done;
456
457 if (event->flags & EVENT_FD_WRITE) {
458 SMB_ASSERT(flags == EVENT_FD_WRITE);
459 done = sys_write(event->fd,
460 &((char *)event->data)[event->done],
461 todo);
462
463 if (done <= 0) {
464 event->flags = 0;
465 event->finished(event->private_data, False);
466 return;
467 }
468 }
469
470 if (event->flags & EVENT_FD_READ) {
471 SMB_ASSERT(flags == EVENT_FD_READ);
472 done = sys_read(event->fd, &((char *)event->data)[event->done],
473 todo);
474
475 if (done <= 0) {
476 event->flags = 0;
477 event->finished(event->private_data, False);
478 return;
479 }
480 }
481
482 event->done += done;
483
484 if (event->done == event->length) {
485 event->flags = 0;
486 event->finished(event->private_data, True);
487 }
488}
489
490/*
491 * Request an async read/write on a fd_event structure. (*finished) is called
492 * when the request is completed or an error had occurred.
493 */
494
495void setup_async_read(struct fd_event *event, void *data, size_t length,
496 void (*finished)(void *private_data, bool success),
497 void *private_data)
498{
499 SMB_ASSERT(event->flags == 0);
500 event->data = data;
501 event->length = length;
502 event->done = 0;
503 event->handler = rw_callback;
504 event->finished = finished;
505 event->private_data = private_data;
506 event->flags = EVENT_FD_READ;
507}
508
509void setup_async_write(struct fd_event *event, void *data, size_t length,
510 void (*finished)(void *private_data, bool success),
511 void *private_data)
512{
513 SMB_ASSERT(event->flags == 0);
514 event->data = data;
515 event->length = length;
516 event->done = 0;
517 event->handler = rw_callback;
518 event->finished = finished;
519 event->private_data = private_data;
520 event->flags = EVENT_FD_WRITE;
521}
522
523/*
524 * This is the main event loop of winbind requests. It goes through a
525 * state-machine of 3 read/write requests, 4 if you have extra data to send.
526 *
527 * An idle winbind client has a read request of 4 bytes outstanding,
528 * finalizing function is request_len_recv, checking the length. request_recv
529 * then processes the packet. The processing function then at some point has
530 * to call request_finished which schedules sending the response.
531 */
532
533static void request_len_recv(void *private_data, bool success);
534static void request_recv(void *private_data, bool success);
535static void request_main_recv(void *private_data, bool success);
536static void request_finished(struct winbindd_cli_state *state);
537void request_finished_cont(void *private_data, bool success);
538static void response_main_sent(void *private_data, bool success);
539static void response_extra_sent(void *private_data, bool success);
540
541static void response_extra_sent(void *private_data, bool success)
542{
543 struct winbindd_cli_state *state =
544 talloc_get_type_abort(private_data, struct winbindd_cli_state);
545
546 if (state->mem_ctx != NULL) {
547 talloc_destroy(state->mem_ctx);
548 state->mem_ctx = NULL;
549 }
550
551 if (!success) {
552 state->finished = True;
553 return;
554 }
555
556 SAFE_FREE(state->response.extra_data.data);
557
558 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
559 request_len_recv, state);
560}
561
562static void response_main_sent(void *private_data, bool success)
563{
564 struct winbindd_cli_state *state =
565 talloc_get_type_abort(private_data, struct winbindd_cli_state);
566
567 if (!success) {
568 state->finished = True;
569 return;
570 }
571
572 if (state->response.length == sizeof(state->response)) {
573 if (state->mem_ctx != NULL) {
574 talloc_destroy(state->mem_ctx);
575 state->mem_ctx = NULL;
576 }
577
578 setup_async_read(&state->fd_event, &state->request,
579 sizeof(uint32), request_len_recv, state);
580 return;
581 }
582
583 setup_async_write(&state->fd_event, state->response.extra_data.data,
584 state->response.length - sizeof(state->response),
585 response_extra_sent, state);
586}
587
588static void request_finished(struct winbindd_cli_state *state)
589{
590 /* Make sure request.extra_data is freed when finish processing a request */
591 SAFE_FREE(state->request.extra_data.data);
592 setup_async_write(&state->fd_event, &state->response,
593 sizeof(state->response), response_main_sent, state);
594}
595
596void request_error(struct winbindd_cli_state *state)
597{
598 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
599 state->response.result = WINBINDD_ERROR;
600 request_finished(state);
601}
602
603void request_ok(struct winbindd_cli_state *state)
604{
605 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
606 state->response.result = WINBINDD_OK;
607 request_finished(state);
608}
609
610void request_finished_cont(void *private_data, bool success)
611{
612 struct winbindd_cli_state *state =
613 talloc_get_type_abort(private_data, struct winbindd_cli_state);
614
615 if (success)
616 request_ok(state);
617 else
618 request_error(state);
619}
620
621static void request_len_recv(void *private_data, bool success)
622{
623 struct winbindd_cli_state *state =
624 talloc_get_type_abort(private_data, struct winbindd_cli_state);
625
626 if (!success) {
627 state->finished = True;
628 return;
629 }
630
631 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
632 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %u)\n",
633 *(uint32_t *)(&state->request), (uint32_t)sizeof(state->request)));
634 state->finished = True;
635 return;
636 }
637
638 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
639 sizeof(state->request) - sizeof(uint32),
640 request_main_recv, state);
641}
642
643static void request_main_recv(void *private_data, bool success)
644{
645 struct winbindd_cli_state *state =
646 talloc_get_type_abort(private_data, struct winbindd_cli_state);
647
648 if (!success) {
649 state->finished = True;
650 return;
651 }
652
653 if (state->request.extra_len == 0) {
654 state->request.extra_data.data = NULL;
655 request_recv(state, True);
656 return;
657 }
658
659 if ((!state->privileged) &&
660 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
661 DEBUG(3, ("Got request with %d bytes extra data on "
662 "unprivileged socket\n", (int)state->request.extra_len));
663 state->request.extra_data.data = NULL;
664 state->finished = True;
665 return;
666 }
667
668 state->request.extra_data.data =
669 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
670
671 if (state->request.extra_data.data == NULL) {
672 DEBUG(0, ("malloc failed\n"));
673 state->finished = True;
674 return;
675 }
676
677 /* Ensure null termination */
678 state->request.extra_data.data[state->request.extra_len] = '\0';
679
680 setup_async_read(&state->fd_event, state->request.extra_data.data,
681 state->request.extra_len, request_recv, state);
682}
683
684static void request_recv(void *private_data, bool success)
685{
686 struct winbindd_cli_state *state =
687 talloc_get_type_abort(private_data, struct winbindd_cli_state);
688
689 if (!success) {
690 state->finished = True;
691 return;
692 }
693
694 process_request(state);
695}
696
697/* Process a new connection by adding it to the client connection list */
698
699static void new_connection(int listen_sock, bool privileged)
700{
701 struct sockaddr_un sunaddr;
702 struct winbindd_cli_state *state;
703 socklen_t len;
704 int sock;
705
706 /* Accept connection */
707
708 len = sizeof(sunaddr);
709
710 do {
711 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
712 } while (sock == -1 && errno == EINTR);
713
714 if (sock == -1)
715 return;
716
717 DEBUG(6,("accepted socket %d\n", sock));
718
719 /* Create new connection structure */
720
721 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
722 close(sock);
723 return;
724 }
725
726 state->sock = sock;
727
728 state->last_access = time(NULL);
729
730 state->privileged = privileged;
731
732 state->fd_event.fd = state->sock;
733 state->fd_event.flags = 0;
734 add_fd_event(&state->fd_event);
735
736 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
737 request_len_recv, state);
738
739 /* Add to connection list */
740
741 winbindd_add_client(state);
742}
743
744/* Remove a client connection from client connection list */
745
746static void remove_client(struct winbindd_cli_state *state)
747{
748 char c = 0;
749 int nwritten;
750
751 /* It's a dead client - hold a funeral */
752
753 if (state == NULL) {
754 return;
755 }
756
757 /* tell client, we are closing ... */
758 nwritten = write(state->sock, &c, sizeof(c));
759 if (nwritten == -1) {
760 DEBUG(2, ("final write to client failed: %s\n",
761 strerror(errno)));
762 }
763
764 /* Close socket */
765
766 close(state->sock);
767
768 /* Free any getent state */
769
770 free_getent_state(state->getpwent_state);
771 free_getent_state(state->getgrent_state);
772
773 /* We may have some extra data that was not freed if the client was
774 killed unexpectedly */
775
776 SAFE_FREE(state->response.extra_data.data);
777
778 if (state->mem_ctx != NULL) {
779 talloc_destroy(state->mem_ctx);
780 state->mem_ctx = NULL;
781 }
782
783 remove_fd_event(&state->fd_event);
784
785 /* Remove from list and free */
786
787 winbindd_remove_client(state);
788 TALLOC_FREE(state);
789}
790
791/* Shutdown client connection which has been idle for the longest time */
792
793static bool remove_idle_client(void)
794{
795 struct winbindd_cli_state *state, *remove_state = NULL;
796 time_t last_access = 0;
797 int nidle = 0;
798
799 for (state = winbindd_client_list(); state; state = state->next) {
800 if (state->response.result != WINBINDD_PENDING &&
801 state->fd_event.flags == EVENT_FD_READ &&
802 !state->getpwent_state && !state->getgrent_state) {
803 nidle++;
804 if (!last_access || state->last_access < last_access) {
805 last_access = state->last_access;
806 remove_state = state;
807 }
808 }
809 }
810
811 if (remove_state) {
812 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
813 nidle, remove_state->sock, (unsigned int)remove_state->pid));
814 remove_client(remove_state);
815 return True;
816 }
817
818 return False;
819}
820
821/* check if HUP has been received and reload files */
822void winbind_check_sighup(const char *logfile)
823{
824 if (do_sighup) {
825
826 DEBUG(3, ("got SIGHUP\n"));
827
828 flush_caches();
829 reload_services_file(logfile);
830
831 do_sighup = 0;
832 }
833}
834
835/* check if TERM has been received */
836void winbind_check_sigterm(bool is_parent)
837{
838 if (do_sigterm)
839 terminate(is_parent);
840}
841
842/* Process incoming clients on listen_sock. We use a tricky non-blocking,
843 non-forking, non-threaded model which allows us to handle many
844 simultaneous connections while remaining impervious to many denial of
845 service attacks. */
846
847static void process_loop(void)
848{
849 struct winbindd_cli_state *state;
850 struct fd_event *ev;
851 fd_set r_fds, w_fds;
852 int maxfd, listen_sock, listen_priv_sock, selret;
853 struct timeval timeout, ev_timeout;
854
855 /* Open Sockets here to get stuff going ASAP */
856 listen_sock = open_winbindd_socket();
857 listen_priv_sock = open_winbindd_priv_socket();
858
859 if (listen_sock == -1 || listen_priv_sock == -1) {
860 perror("open_winbind_socket");
861 exit(1);
862 }
863
864 /* We'll be doing this a lot */
865
866 /* Handle messages */
867
868 message_dispatch(winbind_messaging_context());
869
870 run_events(winbind_event_context(), 0, NULL, NULL);
871
872 /* refresh the trusted domain cache */
873
874 rescan_trusted_domains();
875
876 /* Initialise fd lists for select() */
877
878 maxfd = MAX(listen_sock, listen_priv_sock);
879
880 FD_ZERO(&r_fds);
881 FD_ZERO(&w_fds);
882 FD_SET(listen_sock, &r_fds);
883 FD_SET(listen_priv_sock, &r_fds);
884
885 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
886 timeout.tv_usec = 0;
887
888 /* Check for any event timeouts. */
889 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
890 timeout = timeval_min(&timeout, &ev_timeout);
891 }
892
893 /* Set up client readers and writers */
894
895 state = winbindd_client_list();
896
897 while (state) {
898
899 struct winbindd_cli_state *next = state->next;
900
901 /* Dispose of client connection if it is marked as
902 finished */
903
904 if (state->finished)
905 remove_client(state);
906
907 state = next;
908 }
909
910 for (ev = fd_events; ev; ev = ev->next) {
911 if (ev->flags & EVENT_FD_READ) {
912 FD_SET(ev->fd, &r_fds);
913 maxfd = MAX(ev->fd, maxfd);
914 }
915 if (ev->flags & EVENT_FD_WRITE) {
916 FD_SET(ev->fd, &w_fds);
917 maxfd = MAX(ev->fd, maxfd);
918 }
919 }
920
921 /* Call select */
922
923 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
924
925 if (selret == 0) {
926 goto no_fds_ready;
927 }
928
929 if (selret == -1) {
930 if (errno == EINTR) {
931 goto no_fds_ready;
932 }
933
934 /* Select error, something is badly wrong */
935
936 perror("select");
937 exit(1);
938 }
939
940 /* selret > 0 */
941
942 ev = fd_events;
943 while (ev != NULL) {
944 struct fd_event *next = ev->next;
945 int flags = 0;
946 if (FD_ISSET(ev->fd, &r_fds))
947 flags |= EVENT_FD_READ;
948 if (FD_ISSET(ev->fd, &w_fds))
949 flags |= EVENT_FD_WRITE;
950 if (flags)
951 ev->handler(ev, flags);
952 ev = next;
953 }
954
955 if (FD_ISSET(listen_sock, &r_fds)) {
956 while (winbindd_num_clients() >
957 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
958 DEBUG(5,("winbindd: Exceeding %d client "
959 "connections, removing idle "
960 "connection.\n",
961 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
962 if (!remove_idle_client()) {
963 DEBUG(0,("winbindd: Exceeding %d "
964 "client connections, no idle "
965 "connection found\n",
966 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
967 break;
968 }
969 }
970 /* new, non-privileged connection */
971 new_connection(listen_sock, False);
972 }
973
974 if (FD_ISSET(listen_priv_sock, &r_fds)) {
975 while (winbindd_num_clients() >
976 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
977 DEBUG(5,("winbindd: Exceeding %d client "
978 "connections, removing idle "
979 "connection.\n",
980 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
981 if (!remove_idle_client()) {
982 DEBUG(0,("winbindd: Exceeding %d "
983 "client connections, no idle "
984 "connection found\n",
985 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
986 break;
987 }
988 }
989 /* new, privileged connection */
990 new_connection(listen_priv_sock, True);
991 }
992
993 no_fds_ready:
994
995#if 0
996 winbindd_check_cache_size(time(NULL));
997#endif
998
999 /* Check signal handling things */
1000
1001 winbind_check_sigterm(true);
1002 winbind_check_sighup(NULL);
1003
1004 if (do_sigusr2) {
1005 print_winbindd_status();
1006 do_sigusr2 = 0;
1007 }
1008
1009 if (do_sigchld) {
1010 pid_t pid;
1011
1012 do_sigchld = 0;
1013
1014 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
1015 winbind_child_died(pid);
1016 }
1017 }
1018}
1019
1020/* Main function */
1021
1022int main(int argc, char **argv, char **envp)
1023{
1024 static bool is_daemon = False;
1025 static bool Fork = True;
1026 static bool log_stdout = False;
1027 static bool no_process_group = False;
1028 enum {
1029 OPT_DAEMON = 1000,
1030 OPT_FORK,
1031 OPT_NO_PROCESS_GROUP,
1032 OPT_LOG_STDOUT
1033 };
1034 struct poptOption long_options[] = {
1035 POPT_AUTOHELP
1036 { "stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
1037 { "foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Daemon in foreground mode" },
1038 { "no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
1039 { "daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
1040 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
1041 { "no-caching", 'n', POPT_ARG_NONE, NULL, 'n', "Disable caching" },
1042 POPT_COMMON_SAMBA
1043 POPT_TABLEEND
1044 };
1045 poptContext pc;
1046 int opt;
1047 TALLOC_CTX *frame = talloc_stackframe();
1048
1049 /* glibc (?) likes to print "User defined signal 1" and exit if a
1050 SIGUSR[12] is received before a handler is installed */
1051
1052 CatchSignal(SIGUSR1, SIG_IGN);
1053 CatchSignal(SIGUSR2, SIG_IGN);
1054
1055 fault_setup((void (*)(void *))fault_quit );
1056 dump_core_setup("winbindd");
1057
1058 load_case_tables();
1059
1060 db_tdb2_setup_messaging(NULL, false);
1061
1062 /* Initialise for running in non-root mode */
1063
1064 sec_init();
1065
1066 set_remote_machine_name("winbindd", False);
1067
1068 /* Set environment variable so we don't recursively call ourselves.
1069 This may also be useful interactively. */
1070
1071 if ( !winbind_off() ) {
1072 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1073 exit(1);
1074 }
1075
1076 /* Initialise samba/rpc client stuff */
1077
1078 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
1079
1080 while ((opt = poptGetNextOpt(pc)) != -1) {
1081 switch (opt) {
1082 /* Don't become a daemon */
1083 case OPT_DAEMON:
1084 is_daemon = True;
1085 break;
1086 case 'i':
1087 interactive = True;
1088 log_stdout = True;
1089 Fork = False;
1090 break;
1091 case OPT_FORK:
1092 Fork = false;
1093 break;
1094 case OPT_NO_PROCESS_GROUP:
1095 no_process_group = true;
1096 break;
1097 case OPT_LOG_STDOUT:
1098 log_stdout = true;
1099 break;
1100 case 'n':
1101 opt_nocache = true;
1102 break;
1103 default:
1104 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
1105 poptBadOption(pc, 0), poptStrerror(opt));
1106 poptPrintUsage(pc, stderr, 0);
1107 exit(1);
1108 }
1109 }
1110
1111 if (is_daemon && interactive) {
1112 d_fprintf(stderr,"\nERROR: "
1113 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1114 poptPrintUsage(pc, stderr, 0);
1115 exit(1);
1116 }
1117
1118 if (log_stdout && Fork) {
1119 d_fprintf(stderr, "\nERROR: "
1120 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1121 poptPrintUsage(pc, stderr, 0);
1122 exit(1);
1123 }
1124
1125 poptFreeContext(pc);
1126
1127 if (!override_logfile) {
1128 char *logfile = NULL;
1129 if (asprintf(&logfile,"%s/log.winbindd",
1130 get_dyn_LOGFILEBASE()) > 0) {
1131 lp_set_logfile(logfile);
1132 SAFE_FREE(logfile);
1133 }
1134 }
1135 setup_logging("winbindd", log_stdout);
1136 reopen_logs();
1137
1138 DEBUG(0,("winbindd version %s started.\n", SAMBA_VERSION_STRING));
1139 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
1140
1141 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1142 DEBUG(0, ("error opening config file\n"));
1143 exit(1);
1144 }
1145
1146 /* Initialise messaging system */
1147
1148 if (winbind_messaging_context() == NULL) {
1149 DEBUG(0, ("unable to initialize messaging system\n"));
1150 exit(1);
1151 }
1152
1153 db_tdb2_setup_messaging(winbind_messaging_context(), true);
1154
1155 if (!reload_services_file(NULL)) {
1156 DEBUG(0, ("error opening config file\n"));
1157 exit(1);
1158 }
1159
1160 if (!directory_exist(lp_lockdir(), NULL)) {
1161 mkdir(lp_lockdir(), 0755);
1162 }
1163
1164 /* Setup names. */
1165
1166 if (!init_names())
1167 exit(1);
1168
1169 load_interfaces();
1170
1171 if (!secrets_init()) {
1172
1173 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1174 return False;
1175 }
1176
1177 /* Enable netbios namecache */
1178
1179 namecache_enable();
1180
1181 /* Winbind daemon initialisation */
1182
1183 if ( ! NT_STATUS_IS_OK(idmap_init_cache()) ) {
1184 DEBUG(1, ("Could not init idmap cache!\n"));
1185 }
1186
1187 /* Unblock all signals we are interested in as they may have been
1188 blocked by the parent process. */
1189
1190 BlockSignals(False, SIGINT);
1191 BlockSignals(False, SIGQUIT);
1192 BlockSignals(False, SIGTERM);
1193 BlockSignals(False, SIGUSR1);
1194 BlockSignals(False, SIGUSR2);
1195 BlockSignals(False, SIGHUP);
1196 BlockSignals(False, SIGCHLD);
1197
1198 /* Setup signal handlers */
1199
1200 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
1201 CatchSignal(SIGQUIT, termination_handler);
1202 CatchSignal(SIGTERM, termination_handler);
1203 CatchSignal(SIGCHLD, sigchld_handler);
1204
1205 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1206
1207 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
1208 CatchSignal(SIGHUP, sighup_handler);
1209
1210 if (!interactive)
1211 become_daemon(Fork, no_process_group);
1212
1213 pidfile_create("winbindd");
1214
1215#if HAVE_SETPGID
1216 /*
1217 * If we're interactive we want to set our own process group for
1218 * signal management.
1219 */
1220 if (interactive && !no_process_group)
1221 setpgid( (pid_t)0, (pid_t)0);
1222#endif
1223
1224 TimeInit();
1225
1226 /* Don't use winbindd_reinit_after_fork here as
1227 * we're just starting up and haven't created any
1228 * winbindd-specific resources we must free yet. JRA.
1229 */
1230
1231 if (!reinit_after_fork(winbind_messaging_context(),
1232 winbind_event_context(), false)) {
1233 DEBUG(0,("reinit_after_fork() failed\n"));
1234 exit(1);
1235 }
1236
1237 /*
1238 * Ensure all cache and idmap caches are consistent
1239 * and initialized before we startup.
1240 */
1241 if (!winbindd_cache_validate_and_initialize()) {
1242 exit(1);
1243 }
1244
1245 /* get broadcast messages */
1246 claim_connection(NULL,"",FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP);
1247
1248 /* React on 'smbcontrol winbindd reload-config' in the same way
1249 as to SIGHUP signal */
1250 messaging_register(winbind_messaging_context(), NULL,
1251 MSG_SMB_CONF_UPDATED, msg_reload_services);
1252 messaging_register(winbind_messaging_context(), NULL,
1253 MSG_SHUTDOWN, msg_shutdown);
1254
1255 /* Handle online/offline messages. */
1256 messaging_register(winbind_messaging_context(), NULL,
1257 MSG_WINBIND_OFFLINE, winbind_msg_offline);
1258 messaging_register(winbind_messaging_context(), NULL,
1259 MSG_WINBIND_ONLINE, winbind_msg_online);
1260 messaging_register(winbind_messaging_context(), NULL,
1261 MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
1262
1263 messaging_register(winbind_messaging_context(), NULL,
1264 MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
1265
1266 messaging_register(winbind_messaging_context(), NULL,
1267 MSG_WINBIND_VALIDATE_CACHE,
1268 winbind_msg_validate_cache);
1269
1270 messaging_register(winbind_messaging_context(), NULL,
1271 MSG_WINBIND_DUMP_DOMAIN_LIST,
1272 winbind_msg_dump_domain_list);
1273
1274 /* Register handler for MSG_DEBUG. */
1275 messaging_register(winbind_messaging_context(), NULL,
1276 MSG_DEBUG,
1277 winbind_msg_debug);
1278
1279 netsamlogon_cache_init(); /* Non-critical */
1280
1281 /* clear the cached list of trusted domains */
1282
1283 wcache_tdc_clear();
1284
1285 if (!init_domain_list()) {
1286 DEBUG(0,("unable to initialize domain list\n"));
1287 exit(1);
1288 }
1289
1290 init_idmap_child();
1291 init_locator_child();
1292
1293 smb_nscd_flush_user_cache();
1294 smb_nscd_flush_group_cache();
1295
1296 /* Loop waiting for requests */
1297
1298 TALLOC_FREE(frame);
1299 while (1) {
1300 frame = talloc_stackframe();
1301 process_loop();
1302 TALLOC_FREE(frame);
1303 }
1304
1305 return 0;
1306}
Note: See TracBrowser for help on using the repository browser.