source: branches/samba-3.0/source/nsswitch/winbindd.c@ 140

Last change on this file since 140 was 140, checked in by Paul Smedley, 17 years ago

Update branch to 3.0.31 release

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