[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / nsswitch / winbindd.c
blob34f04adab2ec4400694813281c411b9d0e52953e
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
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
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.
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.
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.
26 #include "includes.h"
27 #include "winbindd.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_WINBIND
32 BOOL opt_nocache = False;
33 static BOOL interactive = False;
35 extern BOOL override_logfile;
37 struct event_context *winbind_event_context(void)
39 static struct event_context *ctx;
41 if (!ctx && !(ctx = event_context_init(NULL))) {
42 smb_panic("Could not init winbind event context\n");
44 return ctx;
47 /* Reload configuration */
49 static BOOL reload_services_file(void)
51 BOOL ret;
53 if (lp_loaded()) {
54 pstring fname;
56 pstrcpy(fname,lp_configfile());
57 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
58 pstrcpy(dyn_CONFIGFILE,fname);
62 reopen_logs();
63 ret = lp_load(dyn_CONFIGFILE,False,False,True,True);
65 reopen_logs();
66 load_interfaces();
68 return(ret);
72 /**************************************************************************** **
73 Handle a fault..
74 **************************************************************************** */
76 static void fault_quit(void)
78 dump_core();
81 static void winbindd_status(void)
83 struct winbindd_cli_state *tmp;
85 DEBUG(0, ("winbindd status:\n"));
87 /* Print client state information */
89 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
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));
100 /* Print winbindd status to log file */
102 static void print_winbindd_status(void)
104 winbindd_status();
107 /* Flush client cache */
109 static void flush_caches(void)
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. */
115 wcache_invalidate_cache();
118 /* Handle the signal by unlinking socket and exiting */
120 static void terminate(void)
122 pstring path;
124 /* Remove socket file */
125 pstr_sprintf(path, "%s/%s",
126 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
127 unlink(path);
129 idmap_close();
131 trustdom_cache_shutdown();
133 #if 0
134 if (interactive) {
135 TALLOC_CTX *mem_ctx = talloc_init("end_description");
136 char *description = talloc_describe_all(mem_ctx);
138 DEBUG(3, ("tallocs left:\n%s\n", description));
139 talloc_destroy(mem_ctx);
141 #endif
143 exit(0);
146 static BOOL do_sigterm;
148 static void termination_handler(int signum)
150 do_sigterm = True;
151 sys_select_signal(signum);
154 static BOOL do_sigusr2;
156 static void sigusr2_handler(int signum)
158 do_sigusr2 = True;
159 sys_select_signal(SIGUSR2);
162 static BOOL do_sighup;
164 static void sighup_handler(int signum)
166 do_sighup = True;
167 sys_select_signal(SIGHUP);
170 static BOOL do_sigchld;
172 static void sigchld_handler(int signum)
174 do_sigchld = True;
175 sys_select_signal(SIGCHLD);
178 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
179 static void msg_reload_services(int msg_type, struct process_id src,
180 void *buf, size_t len, void *private_data)
182 /* Flush various caches */
183 flush_caches();
184 reload_services_file();
187 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
188 static void msg_shutdown(int msg_type, struct process_id src,
189 void *buf, size_t len, void *private_data)
191 do_sigterm = True;
194 static struct winbindd_dispatch_table {
195 enum winbindd_cmd cmd;
196 void (*fn)(struct winbindd_cli_state *state);
197 const char *winbindd_cmd_name;
198 } dispatch_table[] = {
200 /* User functions */
202 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
203 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
205 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
206 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
207 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
209 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
210 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
211 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
212 "GETUSERDOMGROUPS" },
214 /* Group functions */
216 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
217 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
218 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
219 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
220 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
221 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
223 /* PAM auth functions */
225 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
226 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
227 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
228 { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
229 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
231 /* Enumeration functions */
233 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
234 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
235 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
236 "LIST_TRUSTDOM" },
237 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
239 /* SID related functions */
241 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
242 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
243 { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
245 /* Lookup related functions */
247 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
248 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
249 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
250 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
251 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
252 { WINBINDD_SIDS_TO_XIDS, winbindd_sids_to_unixids, "SIDS_TO_XIDS" },
253 #endif /* end DISABLED */
254 { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
255 { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
256 { WINBINDD_SET_MAPPING, winbindd_set_mapping, "SET_MAPPING" },
257 { WINBINDD_SET_HWM, winbindd_set_hwm, "SET_HWMS" },
259 /* Miscellaneous */
261 { WINBINDD_DUMP_MAPS, winbindd_dump_maps, "DUMP_MAPS" },
263 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
264 { WINBINDD_PING, winbindd_ping, "PING" },
265 { WINBINDD_INFO, winbindd_info, "INFO" },
266 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
267 "INTERFACE_VERSION" },
268 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
269 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
270 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
271 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
272 "WINBINDD_PRIV_PIPE_DIR" },
273 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
275 /* Credential cache access */
276 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
278 /* WINS functions */
280 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
281 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
283 /* End of list */
285 { WINBINDD_NUM_CMDS, NULL, "NONE" }
288 static void process_request(struct winbindd_cli_state *state)
290 struct winbindd_dispatch_table *table = dispatch_table;
292 /* Free response data - we may be interrupted and receive another
293 command before being able to send this data off. */
295 SAFE_FREE(state->response.extra_data.data);
297 ZERO_STRUCT(state->response);
299 state->response.result = WINBINDD_PENDING;
300 state->response.length = sizeof(struct winbindd_response);
302 state->mem_ctx = talloc_init("winbind request");
303 if (state->mem_ctx == NULL)
304 return;
306 /* Remember who asked us. */
307 state->pid = state->request.pid;
309 /* Process command */
311 for (table = dispatch_table; table->fn; table++) {
312 if (state->request.cmd == table->cmd) {
313 DEBUG(10,("process_request: request fn %s\n",
314 table->winbindd_cmd_name ));
315 table->fn(state);
316 break;
320 if (!table->fn) {
321 DEBUG(10,("process_request: unknown request fn number %d\n",
322 (int)state->request.cmd ));
323 request_error(state);
328 * A list of file descriptors being monitored by select in the main processing
329 * loop. fd_event->handler is called whenever the socket is readable/writable.
332 static struct fd_event *fd_events = NULL;
334 void add_fd_event(struct fd_event *ev)
336 struct fd_event *match;
338 /* only add unique fd_event structs */
340 for (match=fd_events; match; match=match->next ) {
341 #ifdef DEVELOPER
342 SMB_ASSERT( match != ev );
343 #else
344 if ( match == ev )
345 return;
346 #endif
349 DLIST_ADD(fd_events, ev);
352 void remove_fd_event(struct fd_event *ev)
354 DLIST_REMOVE(fd_events, ev);
358 * Handler for fd_events to complete a read/write request, set up by
359 * setup_async_read/setup_async_write.
362 static void rw_callback(struct fd_event *event, int flags)
364 size_t todo;
365 ssize_t done = 0;
367 todo = event->length - event->done;
369 if (event->flags & EVENT_FD_WRITE) {
370 SMB_ASSERT(flags == EVENT_FD_WRITE);
371 done = sys_write(event->fd,
372 &((char *)event->data)[event->done],
373 todo);
375 if (done <= 0) {
376 event->flags = 0;
377 event->finished(event->private_data, False);
378 return;
382 if (event->flags & EVENT_FD_READ) {
383 SMB_ASSERT(flags == EVENT_FD_READ);
384 done = sys_read(event->fd, &((char *)event->data)[event->done],
385 todo);
387 if (done <= 0) {
388 event->flags = 0;
389 event->finished(event->private_data, False);
390 return;
394 event->done += done;
396 if (event->done == event->length) {
397 event->flags = 0;
398 event->finished(event->private_data, True);
403 * Request an async read/write on a fd_event structure. (*finished) is called
404 * when the request is completed or an error had occurred.
407 void setup_async_read(struct fd_event *event, void *data, size_t length,
408 void (*finished)(void *private_data, BOOL success),
409 void *private_data)
411 SMB_ASSERT(event->flags == 0);
412 event->data = data;
413 event->length = length;
414 event->done = 0;
415 event->handler = rw_callback;
416 event->finished = finished;
417 event->private_data = private_data;
418 event->flags = EVENT_FD_READ;
421 void setup_async_write(struct fd_event *event, void *data, size_t length,
422 void (*finished)(void *private_data, BOOL success),
423 void *private_data)
425 SMB_ASSERT(event->flags == 0);
426 event->data = data;
427 event->length = length;
428 event->done = 0;
429 event->handler = rw_callback;
430 event->finished = finished;
431 event->private_data = private_data;
432 event->flags = EVENT_FD_WRITE;
436 * This is the main event loop of winbind requests. It goes through a
437 * state-machine of 3 read/write requests, 4 if you have extra data to send.
439 * An idle winbind client has a read request of 4 bytes outstanding,
440 * finalizing function is request_len_recv, checking the length. request_recv
441 * then processes the packet. The processing function then at some point has
442 * to call request_finished which schedules sending the response.
445 static void request_len_recv(void *private_data, BOOL success);
446 static void request_recv(void *private_data, BOOL success);
447 static void request_main_recv(void *private_data, BOOL success);
448 static void request_finished(struct winbindd_cli_state *state);
449 void request_finished_cont(void *private_data, BOOL success);
450 static void response_main_sent(void *private_data, BOOL success);
451 static void response_extra_sent(void *private_data, BOOL success);
453 static void response_extra_sent(void *private_data, BOOL success)
455 struct winbindd_cli_state *state =
456 talloc_get_type_abort(private_data, struct winbindd_cli_state);
458 if (state->mem_ctx != NULL) {
459 talloc_destroy(state->mem_ctx);
460 state->mem_ctx = NULL;
463 if (!success) {
464 state->finished = True;
465 return;
468 SAFE_FREE(state->request.extra_data.data);
469 SAFE_FREE(state->response.extra_data.data);
471 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
472 request_len_recv, state);
475 static void response_main_sent(void *private_data, BOOL success)
477 struct winbindd_cli_state *state =
478 talloc_get_type_abort(private_data, struct winbindd_cli_state);
480 if (!success) {
481 state->finished = True;
482 return;
485 if (state->response.length == sizeof(state->response)) {
486 if (state->mem_ctx != NULL) {
487 talloc_destroy(state->mem_ctx);
488 state->mem_ctx = NULL;
491 setup_async_read(&state->fd_event, &state->request,
492 sizeof(uint32), request_len_recv, state);
493 return;
496 setup_async_write(&state->fd_event, state->response.extra_data.data,
497 state->response.length - sizeof(state->response),
498 response_extra_sent, state);
501 static void request_finished(struct winbindd_cli_state *state)
503 setup_async_write(&state->fd_event, &state->response,
504 sizeof(state->response), response_main_sent, state);
507 void request_error(struct winbindd_cli_state *state)
509 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
510 state->response.result = WINBINDD_ERROR;
511 request_finished(state);
514 void request_ok(struct winbindd_cli_state *state)
516 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
517 state->response.result = WINBINDD_OK;
518 request_finished(state);
521 void request_finished_cont(void *private_data, BOOL success)
523 struct winbindd_cli_state *state =
524 talloc_get_type_abort(private_data, struct winbindd_cli_state);
526 if (success)
527 request_ok(state);
528 else
529 request_error(state);
532 static void request_len_recv(void *private_data, BOOL success)
534 struct winbindd_cli_state *state =
535 talloc_get_type_abort(private_data, struct winbindd_cli_state);
537 if (!success) {
538 state->finished = True;
539 return;
542 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
543 DEBUG(0,("request_len_recv: Invalid request size received: %d\n",
544 *(uint32 *)(&state->request)));
545 state->finished = True;
546 return;
549 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
550 sizeof(state->request) - sizeof(uint32),
551 request_main_recv, state);
554 static void request_main_recv(void *private_data, BOOL success)
556 struct winbindd_cli_state *state =
557 talloc_get_type_abort(private_data, struct winbindd_cli_state);
559 if (!success) {
560 state->finished = True;
561 return;
564 if (state->request.extra_len == 0) {
565 state->request.extra_data.data = NULL;
566 request_recv(state, True);
567 return;
570 if ((!state->privileged) &&
571 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
572 DEBUG(3, ("Got request with %d bytes extra data on "
573 "unprivileged socket\n", (int)state->request.extra_len));
574 state->request.extra_data.data = NULL;
575 state->finished = True;
576 return;
579 state->request.extra_data.data =
580 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
582 if (state->request.extra_data.data == NULL) {
583 DEBUG(0, ("malloc failed\n"));
584 state->finished = True;
585 return;
588 /* Ensure null termination */
589 state->request.extra_data.data[state->request.extra_len] = '\0';
591 setup_async_read(&state->fd_event, state->request.extra_data.data,
592 state->request.extra_len, request_recv, state);
595 static void request_recv(void *private_data, BOOL success)
597 struct winbindd_cli_state *state =
598 talloc_get_type_abort(private_data, struct winbindd_cli_state);
600 if (!success) {
601 state->finished = True;
602 return;
605 process_request(state);
608 /* Process a new connection by adding it to the client connection list */
610 static void new_connection(int listen_sock, BOOL privileged)
612 struct sockaddr_un sunaddr;
613 struct winbindd_cli_state *state;
614 socklen_t len;
615 int sock;
617 /* Accept connection */
619 len = sizeof(sunaddr);
621 do {
622 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
623 } while (sock == -1 && errno == EINTR);
625 if (sock == -1)
626 return;
628 DEBUG(6,("accepted socket %d\n", sock));
630 /* Create new connection structure */
632 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
633 close(sock);
634 return;
637 state->sock = sock;
639 state->last_access = time(NULL);
641 state->privileged = privileged;
643 state->fd_event.fd = state->sock;
644 state->fd_event.flags = 0;
645 add_fd_event(&state->fd_event);
647 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
648 request_len_recv, state);
650 /* Add to connection list */
652 winbindd_add_client(state);
655 /* Remove a client connection from client connection list */
657 static void remove_client(struct winbindd_cli_state *state)
659 /* It's a dead client - hold a funeral */
661 if (state == NULL) {
662 return;
665 /* Close socket */
667 close(state->sock);
669 /* Free any getent state */
671 free_getent_state(state->getpwent_state);
672 free_getent_state(state->getgrent_state);
674 /* We may have some extra data that was not freed if the client was
675 killed unexpectedly */
677 SAFE_FREE(state->response.extra_data.data);
679 if (state->mem_ctx != NULL) {
680 talloc_destroy(state->mem_ctx);
681 state->mem_ctx = NULL;
684 remove_fd_event(&state->fd_event);
686 /* Remove from list and free */
688 winbindd_remove_client(state);
689 TALLOC_FREE(state);
692 /* Shutdown client connection which has been idle for the longest time */
694 static BOOL remove_idle_client(void)
696 struct winbindd_cli_state *state, *remove_state = NULL;
697 time_t last_access = 0;
698 int nidle = 0;
700 for (state = winbindd_client_list(); state; state = state->next) {
701 if (state->response.result != WINBINDD_PENDING &&
702 !state->getpwent_state && !state->getgrent_state) {
703 nidle++;
704 if (!last_access || state->last_access < last_access) {
705 last_access = state->last_access;
706 remove_state = state;
711 if (remove_state) {
712 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
713 nidle, remove_state->sock, (unsigned int)remove_state->pid));
714 remove_client(remove_state);
715 return True;
718 return False;
721 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
722 non-forking, non-threaded model which allows us to handle many
723 simultaneous connections while remaining impervious to many denial of
724 service attacks. */
726 static void process_loop(void)
728 struct winbindd_cli_state *state;
729 struct fd_event *ev;
730 fd_set r_fds, w_fds;
731 int maxfd, listen_sock, listen_priv_sock, selret;
732 struct timeval timeout, ev_timeout;
734 /* Open Sockets here to get stuff going ASAP */
735 listen_sock = open_winbindd_socket();
736 listen_priv_sock = open_winbindd_priv_socket();
738 if (listen_sock == -1 || listen_priv_sock == -1) {
739 perror("open_winbind_socket");
740 exit(1);
743 /* We'll be doing this a lot */
745 /* Handle messages */
747 message_dispatch();
749 run_events(winbind_event_context(), 0, NULL, NULL);
751 /* refresh the trusted domain cache */
753 rescan_trusted_domains();
755 /* Free up temporary memory */
757 lp_TALLOC_FREE();
758 main_loop_TALLOC_FREE();
760 /* Initialise fd lists for select() */
762 maxfd = MAX(listen_sock, listen_priv_sock);
764 FD_ZERO(&r_fds);
765 FD_ZERO(&w_fds);
766 FD_SET(listen_sock, &r_fds);
767 FD_SET(listen_priv_sock, &r_fds);
769 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
770 timeout.tv_usec = 0;
772 /* Check for any event timeouts. */
773 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
774 timeout = timeval_min(&timeout, &ev_timeout);
777 /* Set up client readers and writers */
779 state = winbindd_client_list();
781 while (state) {
783 struct winbindd_cli_state *next = state->next;
785 /* Dispose of client connection if it is marked as
786 finished */
788 if (state->finished)
789 remove_client(state);
791 state = next;
794 for (ev = fd_events; ev; ev = ev->next) {
795 if (ev->flags & EVENT_FD_READ) {
796 FD_SET(ev->fd, &r_fds);
797 maxfd = MAX(ev->fd, maxfd);
799 if (ev->flags & EVENT_FD_WRITE) {
800 FD_SET(ev->fd, &w_fds);
801 maxfd = MAX(ev->fd, maxfd);
805 /* Call select */
807 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
809 if (selret == 0) {
810 goto no_fds_ready;
813 if (selret == -1) {
814 if (errno == EINTR) {
815 goto no_fds_ready;
818 /* Select error, something is badly wrong */
820 perror("select");
821 exit(1);
824 /* selret > 0 */
826 ev = fd_events;
827 while (ev != NULL) {
828 struct fd_event *next = ev->next;
829 int flags = 0;
830 if (FD_ISSET(ev->fd, &r_fds))
831 flags |= EVENT_FD_READ;
832 if (FD_ISSET(ev->fd, &w_fds))
833 flags |= EVENT_FD_WRITE;
834 if (flags)
835 ev->handler(ev, flags);
836 ev = next;
839 if (FD_ISSET(listen_sock, &r_fds)) {
840 while (winbindd_num_clients() >
841 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
842 DEBUG(5,("winbindd: Exceeding %d client "
843 "connections, removing idle "
844 "connection.\n",
845 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
846 if (!remove_idle_client()) {
847 DEBUG(0,("winbindd: Exceeding %d "
848 "client connections, no idle "
849 "connection found\n",
850 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
851 break;
854 /* new, non-privileged connection */
855 new_connection(listen_sock, False);
858 if (FD_ISSET(listen_priv_sock, &r_fds)) {
859 while (winbindd_num_clients() >
860 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
861 DEBUG(5,("winbindd: Exceeding %d client "
862 "connections, removing idle "
863 "connection.\n",
864 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
865 if (!remove_idle_client()) {
866 DEBUG(0,("winbindd: Exceeding %d "
867 "client connections, no idle "
868 "connection found\n",
869 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
870 break;
873 /* new, privileged connection */
874 new_connection(listen_priv_sock, True);
877 no_fds_ready:
879 #if 0
880 winbindd_check_cache_size(time(NULL));
881 #endif
883 /* Check signal handling things */
885 if (do_sigterm)
886 terminate();
888 if (do_sighup) {
890 DEBUG(3, ("got SIGHUP\n"));
892 msg_reload_services(MSG_SMB_CONF_UPDATED, pid_to_procid(0), NULL, 0, NULL);
893 do_sighup = False;
896 if (do_sigusr2) {
897 print_winbindd_status();
898 do_sigusr2 = False;
901 if (do_sigchld) {
902 pid_t pid;
904 do_sigchld = False;
906 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
907 winbind_child_died(pid);
912 /* Main function */
914 int main(int argc, char **argv, char **envp)
916 pstring logfile;
917 static BOOL Fork = True;
918 static BOOL log_stdout = False;
919 static BOOL no_process_group = False;
920 struct poptOption long_options[] = {
921 POPT_AUTOHELP
922 { "stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
923 { "foreground", 'F', POPT_ARG_VAL, &Fork, False, "Daemon in foreground mode" },
924 { "no-process-group", 0, POPT_ARG_VAL, &no_process_group, True, "Don't create a new process group" },
925 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
926 { "no-caching", 'n', POPT_ARG_VAL, &opt_nocache, True, "Disable caching" },
927 POPT_COMMON_SAMBA
928 POPT_TABLEEND
930 poptContext pc;
931 int opt;
933 /* glibc (?) likes to print "User defined signal 1" and exit if a
934 SIGUSR[12] is received before a handler is installed */
936 CatchSignal(SIGUSR1, SIG_IGN);
937 CatchSignal(SIGUSR2, SIG_IGN);
939 fault_setup((void (*)(void *))fault_quit );
940 dump_core_setup("winbindd");
942 load_case_tables();
944 /* Initialise for running in non-root mode */
946 sec_init();
948 set_remote_machine_name("winbindd", False);
950 /* Set environment variable so we don't recursively call ourselves.
951 This may also be useful interactively. */
953 if ( !winbind_off() ) {
954 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
955 exit(1);
958 /* Initialise samba/rpc client stuff */
960 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options,
961 POPT_CONTEXT_KEEP_FIRST);
963 while ((opt = poptGetNextOpt(pc)) != -1) {
964 switch (opt) {
965 /* Don't become a daemon */
966 case 'i':
967 interactive = True;
968 log_stdout = True;
969 Fork = False;
970 break;
975 if (log_stdout && Fork) {
976 printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
977 poptPrintUsage(pc, stderr, 0);
978 exit(1);
981 if (!override_logfile) {
982 pstr_sprintf(logfile, "%s/log.winbindd", dyn_LOGFILEBASE);
983 lp_set_logfile(logfile);
985 setup_logging("winbindd", log_stdout);
986 reopen_logs();
988 DEBUG(1, ("winbindd version %s started.\n%s\n",
989 SAMBA_VERSION_STRING,
990 COPYRIGHT_STARTUP_MESSAGE) );
992 if (!reload_services_file()) {
993 DEBUG(0, ("error opening config file\n"));
994 exit(1);
997 if (!directory_exist(lp_lockdir(), NULL)) {
998 mkdir(lp_lockdir(), 0755);
1001 /* Setup names. */
1003 if (!init_names())
1004 exit(1);
1006 load_interfaces();
1008 if (!secrets_init()) {
1010 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1011 return False;
1014 /* Enable netbios namecache */
1016 namecache_enable();
1018 /* Winbind daemon initialisation */
1020 if ( ! NT_STATUS_IS_OK(idmap_init_cache()) ) {
1021 DEBUG(1, ("Could not init idmap cache!\n"));
1024 /* Unblock all signals we are interested in as they may have been
1025 blocked by the parent process. */
1027 BlockSignals(False, SIGINT);
1028 BlockSignals(False, SIGQUIT);
1029 BlockSignals(False, SIGTERM);
1030 BlockSignals(False, SIGUSR1);
1031 BlockSignals(False, SIGUSR2);
1032 BlockSignals(False, SIGHUP);
1033 BlockSignals(False, SIGCHLD);
1035 /* Setup signal handlers */
1037 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
1038 CatchSignal(SIGQUIT, termination_handler);
1039 CatchSignal(SIGTERM, termination_handler);
1040 CatchSignal(SIGCHLD, sigchld_handler);
1042 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1044 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
1045 CatchSignal(SIGHUP, sighup_handler);
1047 if (!interactive)
1048 become_daemon(Fork, no_process_group);
1050 pidfile_create("winbindd");
1052 #if HAVE_SETPGID
1054 * If we're interactive we want to set our own process group for
1055 * signal management.
1057 if (interactive && !no_process_group)
1058 setpgid( (pid_t)0, (pid_t)0);
1059 #endif
1061 TimeInit();
1063 /* Initialise messaging system */
1065 if (!message_init()) {
1066 DEBUG(0, ("unable to initialize messaging system\n"));
1067 exit(1);
1070 /* Initialize cache (ensure version is correct). */
1071 if (!initialize_winbindd_cache()) {
1072 exit(1);
1075 /* React on 'smbcontrol winbindd reload-config' in the same way
1076 as to SIGHUP signal */
1077 message_register(MSG_SMB_CONF_UPDATED, msg_reload_services, NULL);
1078 message_register(MSG_SHUTDOWN, msg_shutdown, NULL);
1080 /* Handle online/offline messages. */
1081 message_register(MSG_WINBIND_OFFLINE, winbind_msg_offline, NULL);
1082 message_register(MSG_WINBIND_ONLINE, winbind_msg_online, NULL);
1083 message_register(MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus,
1084 NULL);
1086 poptFreeContext(pc);
1088 netsamlogon_cache_init(); /* Non-critical */
1090 if (!init_domain_list()) {
1091 DEBUG(0,("unable to initalize domain list\n"));
1092 exit(1);
1095 init_idmap_child();
1097 smb_nscd_flush_user_cache();
1098 smb_nscd_flush_group_cache();
1100 /* Loop waiting for requests */
1102 while (1)
1103 process_loop();
1105 return 0;