VERSION: Raise version number up to 3.0.36.
[Samba.git] / source / nsswitch / winbindd.c
blob48b94f20da34c4ecf96e466fba1efa8f2c4994cf
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(const char *logfile)
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 /* if this is a child, restore the logfile to the special
66 name - <domain>, idmap, etc. */
67 if (logfile && *logfile)
68 lp_set_logfile(logfile);
70 reopen_logs();
71 load_interfaces();
73 return(ret);
77 /**************************************************************************** **
78 Handle a fault..
79 **************************************************************************** */
81 static void fault_quit(void)
83 dump_core();
86 static void winbindd_status(void)
88 struct winbindd_cli_state *tmp;
90 DEBUG(0, ("winbindd status:\n"));
92 /* Print client state information */
94 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
96 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
97 DEBUG(2, ("\tclient list:\n"));
98 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
99 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
100 (unsigned long)tmp->pid, tmp->sock));
105 /* Print winbindd status to log file */
107 static void print_winbindd_status(void)
109 winbindd_status();
112 /* Flush client cache */
114 static void flush_caches(void)
116 /* We need to invalidate cached user list entries on a SIGHUP
117 otherwise cached access denied errors due to restrict anonymous
118 hang around until the sequence number changes. */
120 wcache_invalidate_cache();
123 /* Handle the signal by unlinking socket and exiting */
125 static void terminate(bool in_parent)
127 if (in_parent) {
128 /* When parent goes away we should
129 * remove the socket file. Not so
130 * when children terminate.
133 pstring path;
135 /* Remove socket file */
136 pstr_sprintf(path, "%s/%s",
137 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
138 unlink(path);
141 idmap_close();
143 trustdom_cache_shutdown();
145 #if 0
146 if (interactive) {
147 TALLOC_CTX *mem_ctx = talloc_init("end_description");
148 char *description = talloc_describe_all(mem_ctx);
150 DEBUG(3, ("tallocs left:\n%s\n", description));
151 talloc_destroy(mem_ctx);
153 #endif
155 exit(0);
158 static BOOL do_sigterm;
160 static void termination_handler(int signum)
162 do_sigterm = True;
163 sys_select_signal(signum);
166 static BOOL do_sigusr2;
168 static void sigusr2_handler(int signum)
170 do_sigusr2 = True;
171 sys_select_signal(SIGUSR2);
174 static BOOL do_sighup;
176 static void sighup_handler(int signum)
178 do_sighup = True;
179 sys_select_signal(SIGHUP);
182 static BOOL do_sigchld;
184 static void sigchld_handler(int signum)
186 do_sigchld = True;
187 sys_select_signal(SIGCHLD);
190 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
191 static void msg_reload_services(int msg_type, struct process_id src,
192 void *buf, size_t len, void *private_data)
194 /* Flush various caches */
195 flush_caches();
196 reload_services_file((const char *) private_data);
199 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
200 static void msg_shutdown(int msg_type, struct process_id src,
201 void *buf, size_t len, void *private_data)
203 do_sigterm = True;
206 static struct winbindd_dispatch_table {
207 enum winbindd_cmd cmd;
208 void (*fn)(struct winbindd_cli_state *state);
209 const char *winbindd_cmd_name;
210 } dispatch_table[] = {
212 /* User functions */
214 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
215 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
217 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
218 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
219 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
221 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
222 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
223 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
224 "GETUSERDOMGROUPS" },
226 /* Group functions */
228 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
229 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
230 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
231 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
232 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
233 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
235 /* PAM auth functions */
237 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
238 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
239 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
240 { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
241 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
243 /* Enumeration functions */
245 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
246 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
247 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
248 "LIST_TRUSTDOM" },
249 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
251 /* SID related functions */
253 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
254 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
255 { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
257 /* Lookup related functions */
259 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
260 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
261 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
262 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
263 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
264 { WINBINDD_SIDS_TO_XIDS, winbindd_sids_to_unixids, "SIDS_TO_XIDS" },
265 #endif /* end DISABLED */
266 { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
267 { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
268 { WINBINDD_SET_MAPPING, winbindd_set_mapping, "SET_MAPPING" },
269 { WINBINDD_SET_HWM, winbindd_set_hwm, "SET_HWMS" },
271 /* Miscellaneous */
273 { WINBINDD_DUMP_MAPS, winbindd_dump_maps, "DUMP_MAPS" },
275 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
276 { WINBINDD_PING, winbindd_ping, "PING" },
277 { WINBINDD_INFO, winbindd_info, "INFO" },
278 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
279 "INTERFACE_VERSION" },
280 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
281 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
282 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
283 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
284 "WINBINDD_PRIV_PIPE_DIR" },
285 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
287 /* Credential cache access */
288 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
290 /* WINS functions */
292 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
293 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
295 /* End of list */
297 { WINBINDD_NUM_CMDS, NULL, "NONE" }
300 static void process_request(struct winbindd_cli_state *state)
302 struct winbindd_dispatch_table *table = dispatch_table;
304 /* Free response data - we may be interrupted and receive another
305 command before being able to send this data off. */
307 SAFE_FREE(state->response.extra_data.data);
309 ZERO_STRUCT(state->response);
311 state->response.result = WINBINDD_PENDING;
312 state->response.length = sizeof(struct winbindd_response);
314 state->mem_ctx = talloc_init("winbind request");
315 if (state->mem_ctx == NULL)
316 return;
318 /* Remember who asked us. */
319 state->pid = state->request.pid;
321 /* Process command */
323 for (table = dispatch_table; table->fn; table++) {
324 if (state->request.cmd == table->cmd) {
325 DEBUG(10,("process_request: request fn %s\n",
326 table->winbindd_cmd_name ));
327 table->fn(state);
328 break;
332 if (!table->fn) {
333 DEBUG(10,("process_request: unknown request fn number %d\n",
334 (int)state->request.cmd ));
335 request_error(state);
340 * A list of file descriptors being monitored by select in the main processing
341 * loop. fd_event->handler is called whenever the socket is readable/writable.
344 static struct fd_event *fd_events = NULL;
346 void add_fd_event(struct fd_event *ev)
348 struct fd_event *match;
350 /* only add unique fd_event structs */
352 for (match=fd_events; match; match=match->next ) {
353 #ifdef DEVELOPER
354 SMB_ASSERT( match != ev );
355 #else
356 if ( match == ev )
357 return;
358 #endif
361 DLIST_ADD(fd_events, ev);
364 void remove_fd_event(struct fd_event *ev)
366 DLIST_REMOVE(fd_events, ev);
370 * Handler for fd_events to complete a read/write request, set up by
371 * setup_async_read/setup_async_write.
374 static void rw_callback(struct fd_event *event, int flags)
376 size_t todo;
377 ssize_t done = 0;
379 todo = event->length - event->done;
381 if (event->flags & EVENT_FD_WRITE) {
382 SMB_ASSERT(flags == EVENT_FD_WRITE);
383 done = sys_write(event->fd,
384 &((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 if (event->flags & EVENT_FD_READ) {
395 SMB_ASSERT(flags == EVENT_FD_READ);
396 done = sys_read(event->fd, &((char *)event->data)[event->done],
397 todo);
399 if (done <= 0) {
400 event->flags = 0;
401 event->finished(event->private_data, False);
402 return;
406 event->done += done;
408 if (event->done == event->length) {
409 event->flags = 0;
410 event->finished(event->private_data, True);
415 * Request an async read/write on a fd_event structure. (*finished) is called
416 * when the request is completed or an error had occurred.
419 void setup_async_read(struct fd_event *event, void *data, size_t length,
420 void (*finished)(void *private_data, BOOL success),
421 void *private_data)
423 SMB_ASSERT(event->flags == 0);
424 event->data = data;
425 event->length = length;
426 event->done = 0;
427 event->handler = rw_callback;
428 event->finished = finished;
429 event->private_data = private_data;
430 event->flags = EVENT_FD_READ;
433 void setup_async_write(struct fd_event *event, void *data, size_t length,
434 void (*finished)(void *private_data, BOOL success),
435 void *private_data)
437 SMB_ASSERT(event->flags == 0);
438 event->data = data;
439 event->length = length;
440 event->done = 0;
441 event->handler = rw_callback;
442 event->finished = finished;
443 event->private_data = private_data;
444 event->flags = EVENT_FD_WRITE;
448 * This is the main event loop of winbind requests. It goes through a
449 * state-machine of 3 read/write requests, 4 if you have extra data to send.
451 * An idle winbind client has a read request of 4 bytes outstanding,
452 * finalizing function is request_len_recv, checking the length. request_recv
453 * then processes the packet. The processing function then at some point has
454 * to call request_finished which schedules sending the response.
457 static void request_len_recv(void *private_data, BOOL success);
458 static void request_recv(void *private_data, BOOL success);
459 static void request_main_recv(void *private_data, BOOL success);
460 static void request_finished(struct winbindd_cli_state *state);
461 void request_finished_cont(void *private_data, BOOL success);
462 static void response_main_sent(void *private_data, BOOL success);
463 static void response_extra_sent(void *private_data, BOOL success);
465 static void response_extra_sent(void *private_data, BOOL success)
467 struct winbindd_cli_state *state =
468 talloc_get_type_abort(private_data, struct winbindd_cli_state);
470 if (state->mem_ctx != NULL) {
471 talloc_destroy(state->mem_ctx);
472 state->mem_ctx = NULL;
475 if (!success) {
476 state->finished = True;
477 return;
480 SAFE_FREE(state->response.extra_data.data);
482 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
483 request_len_recv, state);
486 static void response_main_sent(void *private_data, BOOL success)
488 struct winbindd_cli_state *state =
489 talloc_get_type_abort(private_data, struct winbindd_cli_state);
491 if (!success) {
492 state->finished = True;
493 return;
496 if (state->response.length == sizeof(state->response)) {
497 if (state->mem_ctx != NULL) {
498 talloc_destroy(state->mem_ctx);
499 state->mem_ctx = NULL;
502 setup_async_read(&state->fd_event, &state->request,
503 sizeof(uint32), request_len_recv, state);
504 return;
507 setup_async_write(&state->fd_event, state->response.extra_data.data,
508 state->response.length - sizeof(state->response),
509 response_extra_sent, state);
512 static void request_finished(struct winbindd_cli_state *state)
514 /* Make sure request.extra_data is freed when finish processing a request */
515 SAFE_FREE(state->request.extra_data.data);
516 setup_async_write(&state->fd_event, &state->response,
517 sizeof(state->response), response_main_sent, state);
520 void request_error(struct winbindd_cli_state *state)
522 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
523 state->response.result = WINBINDD_ERROR;
524 request_finished(state);
527 void request_ok(struct winbindd_cli_state *state)
529 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
530 state->response.result = WINBINDD_OK;
531 request_finished(state);
534 void request_finished_cont(void *private_data, BOOL success)
536 struct winbindd_cli_state *state =
537 talloc_get_type_abort(private_data, struct winbindd_cli_state);
539 if (success)
540 request_ok(state);
541 else
542 request_error(state);
545 static void request_len_recv(void *private_data, BOOL success)
547 struct winbindd_cli_state *state =
548 talloc_get_type_abort(private_data, struct winbindd_cli_state);
550 if (!success) {
551 state->finished = True;
552 return;
555 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
556 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %d)\n",
557 *(uint32 *)(&state->request), (uint32)sizeof(state->request)));
558 state->finished = True;
559 return;
562 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
563 sizeof(state->request) - sizeof(uint32),
564 request_main_recv, state);
567 static void request_main_recv(void *private_data, BOOL success)
569 struct winbindd_cli_state *state =
570 talloc_get_type_abort(private_data, struct winbindd_cli_state);
572 if (!success) {
573 state->finished = True;
574 return;
577 if (state->request.extra_len == 0) {
578 state->request.extra_data.data = NULL;
579 request_recv(state, True);
580 return;
583 if ((!state->privileged) &&
584 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
585 DEBUG(3, ("Got request with %d bytes extra data on "
586 "unprivileged socket\n", (int)state->request.extra_len));
587 state->request.extra_data.data = NULL;
588 state->finished = True;
589 return;
592 state->request.extra_data.data =
593 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
595 if (state->request.extra_data.data == NULL) {
596 DEBUG(0, ("malloc failed\n"));
597 state->finished = True;
598 return;
601 /* Ensure null termination */
602 state->request.extra_data.data[state->request.extra_len] = '\0';
604 setup_async_read(&state->fd_event, state->request.extra_data.data,
605 state->request.extra_len, request_recv, state);
608 static void request_recv(void *private_data, BOOL success)
610 struct winbindd_cli_state *state =
611 talloc_get_type_abort(private_data, struct winbindd_cli_state);
613 if (!success) {
614 state->finished = True;
615 return;
618 process_request(state);
621 /* Process a new connection by adding it to the client connection list */
623 static void new_connection(int listen_sock, BOOL privileged)
625 struct sockaddr_un sunaddr;
626 struct winbindd_cli_state *state;
627 socklen_t len;
628 int sock;
630 /* Accept connection */
632 len = sizeof(sunaddr);
634 do {
635 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
636 } while (sock == -1 && errno == EINTR);
638 if (sock == -1)
639 return;
641 DEBUG(6,("accepted socket %d\n", sock));
643 /* Create new connection structure */
645 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
646 close(sock);
647 return;
650 state->sock = sock;
652 state->last_access = time(NULL);
654 state->privileged = privileged;
656 state->fd_event.fd = state->sock;
657 state->fd_event.flags = 0;
658 add_fd_event(&state->fd_event);
660 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
661 request_len_recv, state);
663 /* Add to connection list */
665 winbindd_add_client(state);
668 /* Remove a client connection from client connection list */
670 static void remove_client(struct winbindd_cli_state *state)
672 char c = 0;
674 /* It's a dead client - hold a funeral */
676 if (state == NULL) {
677 return;
680 /* tell client, we are closing ... */
681 write(state->sock, &c, sizeof(c));
683 /* Close socket */
685 close(state->sock);
687 /* Free any getent state */
689 free_getent_state(state->getpwent_state);
690 free_getent_state(state->getgrent_state);
692 /* We may have some extra data that was not freed if the client was
693 killed unexpectedly */
695 SAFE_FREE(state->response.extra_data.data);
697 if (state->mem_ctx != NULL) {
698 talloc_destroy(state->mem_ctx);
699 state->mem_ctx = NULL;
702 remove_fd_event(&state->fd_event);
704 /* Remove from list and free */
706 winbindd_remove_client(state);
707 TALLOC_FREE(state);
710 /* Shutdown client connection which has been idle for the longest time */
712 static BOOL remove_idle_client(void)
714 struct winbindd_cli_state *state, *remove_state = NULL;
715 time_t last_access = 0;
716 int nidle = 0;
718 for (state = winbindd_client_list(); state; state = state->next) {
719 if (state->response.result != WINBINDD_PENDING &&
720 !state->getpwent_state && !state->getgrent_state) {
721 nidle++;
722 if (!last_access || state->last_access < last_access) {
723 last_access = state->last_access;
724 remove_state = state;
729 if (remove_state) {
730 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
731 nidle, remove_state->sock, (unsigned int)remove_state->pid));
732 remove_client(remove_state);
733 return True;
736 return False;
739 /* check if HUP has been received and reload files */
740 void winbind_check_sighup(const char *logfile)
742 if (do_sighup) {
744 DEBUG(3, ("got SIGHUP\n"));
746 msg_reload_services(MSG_SMB_CONF_UPDATED, pid_to_procid(0), NULL, 0, logfile);
747 do_sighup = False;
751 /* check if TERM has been received */
752 void winbind_check_sigterm(bool in_parent)
754 if (do_sigterm)
755 terminate(in_parent);
758 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
759 non-forking, non-threaded model which allows us to handle many
760 simultaneous connections while remaining impervious to many denial of
761 service attacks. */
763 static void process_loop(void)
765 struct winbindd_cli_state *state;
766 struct fd_event *ev;
767 fd_set r_fds, w_fds;
768 int maxfd, listen_sock, listen_priv_sock, selret;
769 struct timeval timeout, ev_timeout;
771 /* Open Sockets here to get stuff going ASAP */
772 listen_sock = open_winbindd_socket();
773 listen_priv_sock = open_winbindd_priv_socket();
775 if (listen_sock == -1 || listen_priv_sock == -1) {
776 perror("open_winbind_socket");
777 exit(1);
780 /* We'll be doing this a lot */
782 /* Handle messages */
784 message_dispatch();
786 run_events(winbind_event_context(), 0, NULL, NULL);
788 /* refresh the trusted domain cache */
790 rescan_trusted_domains();
792 /* Free up temporary memory */
794 lp_TALLOC_FREE();
795 main_loop_TALLOC_FREE();
797 /* Initialise fd lists for select() */
799 maxfd = MAX(listen_sock, listen_priv_sock);
801 FD_ZERO(&r_fds);
802 FD_ZERO(&w_fds);
803 FD_SET(listen_sock, &r_fds);
804 FD_SET(listen_priv_sock, &r_fds);
806 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
807 timeout.tv_usec = 0;
809 /* Check for any event timeouts. */
810 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
811 timeout = timeval_min(&timeout, &ev_timeout);
814 /* Set up client readers and writers */
816 state = winbindd_client_list();
818 while (state) {
820 struct winbindd_cli_state *next = state->next;
822 /* Dispose of client connection if it is marked as
823 finished */
825 if (state->finished)
826 remove_client(state);
828 state = next;
831 for (ev = fd_events; ev; ev = ev->next) {
832 if (ev->flags & EVENT_FD_READ) {
833 FD_SET(ev->fd, &r_fds);
834 maxfd = MAX(ev->fd, maxfd);
836 if (ev->flags & EVENT_FD_WRITE) {
837 FD_SET(ev->fd, &w_fds);
838 maxfd = MAX(ev->fd, maxfd);
842 /* Call select */
844 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
846 if (selret == 0) {
847 goto no_fds_ready;
850 if (selret == -1) {
851 if (errno == EINTR) {
852 goto no_fds_ready;
855 /* Select error, something is badly wrong */
857 perror("select");
858 exit(1);
861 /* selret > 0 */
863 ev = fd_events;
864 while (ev != NULL) {
865 struct fd_event *next = ev->next;
866 int flags = 0;
867 if (FD_ISSET(ev->fd, &r_fds))
868 flags |= EVENT_FD_READ;
869 if (FD_ISSET(ev->fd, &w_fds))
870 flags |= EVENT_FD_WRITE;
871 if (flags)
872 ev->handler(ev, flags);
873 ev = next;
876 if (FD_ISSET(listen_sock, &r_fds)) {
877 while (winbindd_num_clients() >
878 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
879 DEBUG(5,("winbindd: Exceeding %d client "
880 "connections, removing idle "
881 "connection.\n",
882 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
883 if (!remove_idle_client()) {
884 DEBUG(0,("winbindd: Exceeding %d "
885 "client connections, no idle "
886 "connection found\n",
887 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
888 break;
891 /* new, non-privileged connection */
892 new_connection(listen_sock, False);
895 if (FD_ISSET(listen_priv_sock, &r_fds)) {
896 while (winbindd_num_clients() >
897 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
898 DEBUG(5,("winbindd: Exceeding %d client "
899 "connections, removing idle "
900 "connection.\n",
901 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
902 if (!remove_idle_client()) {
903 DEBUG(0,("winbindd: Exceeding %d "
904 "client connections, no idle "
905 "connection found\n",
906 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
907 break;
910 /* new, privileged connection */
911 new_connection(listen_priv_sock, True);
914 no_fds_ready:
916 #if 0
917 winbindd_check_cache_size(time(NULL));
918 #endif
920 /* Check signal handling things */
922 winbind_check_sigterm(true);
923 winbind_check_sighup(NULL);
925 if (do_sigusr2) {
926 print_winbindd_status();
927 do_sigusr2 = False;
930 if (do_sigchld) {
931 pid_t pid;
933 do_sigchld = False;
935 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
936 winbind_child_died(pid);
941 /* Main function */
943 int main(int argc, char **argv, char **envp)
945 pstring logfile;
946 static BOOL Fork = True;
947 static BOOL log_stdout = False;
948 static BOOL no_process_group = False;
949 struct poptOption long_options[] = {
950 POPT_AUTOHELP
951 { "stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
952 { "foreground", 'F', POPT_ARG_VAL, &Fork, False, "Daemon in foreground mode" },
953 { "no-process-group", 0, POPT_ARG_VAL, &no_process_group, True, "Don't create a new process group" },
954 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
955 { "no-caching", 'n', POPT_ARG_VAL, &opt_nocache, True, "Disable caching" },
956 POPT_COMMON_SAMBA
957 POPT_TABLEEND
959 poptContext pc;
960 int opt;
962 /* glibc (?) likes to print "User defined signal 1" and exit if a
963 SIGUSR[12] is received before a handler is installed */
965 CatchSignal(SIGUSR1, SIG_IGN);
966 CatchSignal(SIGUSR2, SIG_IGN);
968 fault_setup((void (*)(void *))fault_quit );
969 dump_core_setup("winbindd");
971 load_case_tables();
973 /* Initialise for running in non-root mode */
975 sec_init();
977 set_remote_machine_name("winbindd", False);
979 /* Set environment variable so we don't recursively call ourselves.
980 This may also be useful interactively. */
982 if ( !winbind_off() ) {
983 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
984 exit(1);
987 /* Initialise samba/rpc client stuff */
989 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options,
990 POPT_CONTEXT_KEEP_FIRST);
992 while ((opt = poptGetNextOpt(pc)) != -1) {
993 switch (opt) {
994 /* Don't become a daemon */
995 case 'i':
996 interactive = True;
997 log_stdout = True;
998 Fork = False;
999 break;
1004 if (log_stdout && Fork) {
1005 printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
1006 poptPrintUsage(pc, stderr, 0);
1007 exit(1);
1010 if (!override_logfile) {
1011 pstr_sprintf(logfile, "%s/log.winbindd", dyn_LOGFILEBASE);
1012 lp_set_logfile(logfile);
1014 setup_logging("winbindd", log_stdout);
1015 reopen_logs();
1017 DEBUG(1, ("winbindd version %s started.\n%s\n",
1018 SAMBA_VERSION_STRING,
1019 COPYRIGHT_STARTUP_MESSAGE) );
1021 if (!reload_services_file(NULL)) {
1022 DEBUG(0, ("error opening config file\n"));
1023 exit(1);
1026 if (!directory_exist(lp_lockdir(), NULL)) {
1027 mkdir(lp_lockdir(), 0755);
1030 /* Setup names. */
1032 if (!init_names())
1033 exit(1);
1035 load_interfaces();
1037 if (!secrets_init()) {
1039 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1040 return False;
1043 /* Enable netbios namecache */
1045 namecache_enable();
1047 /* Winbind daemon initialisation */
1049 if ( ! NT_STATUS_IS_OK(idmap_init_cache()) ) {
1050 DEBUG(1, ("Could not init idmap cache!\n"));
1053 /* Unblock all signals we are interested in as they may have been
1054 blocked by the parent process. */
1056 BlockSignals(False, SIGINT);
1057 BlockSignals(False, SIGQUIT);
1058 BlockSignals(False, SIGTERM);
1059 BlockSignals(False, SIGUSR1);
1060 BlockSignals(False, SIGUSR2);
1061 BlockSignals(False, SIGHUP);
1062 BlockSignals(False, SIGCHLD);
1064 /* Setup signal handlers */
1066 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
1067 CatchSignal(SIGQUIT, termination_handler);
1068 CatchSignal(SIGTERM, termination_handler);
1069 CatchSignal(SIGCHLD, sigchld_handler);
1071 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1073 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
1074 CatchSignal(SIGHUP, sighup_handler);
1076 if (!interactive)
1077 become_daemon(Fork, no_process_group);
1079 pidfile_create("winbindd");
1081 #if HAVE_SETPGID
1083 * If we're interactive we want to set our own process group for
1084 * signal management.
1086 if (interactive && !no_process_group)
1087 setpgid( (pid_t)0, (pid_t)0);
1088 #endif
1090 TimeInit();
1092 /* Initialise messaging system */
1094 if (!message_init()) {
1095 DEBUG(0, ("unable to initialize messaging system\n"));
1096 exit(1);
1099 /* Initialize cache (ensure version is correct). */
1100 if (!initialize_winbindd_cache()) {
1101 exit(1);
1104 /* React on 'smbcontrol winbindd reload-config' in the same way
1105 as to SIGHUP signal */
1106 message_register(MSG_SMB_CONF_UPDATED, msg_reload_services, NULL);
1107 message_register(MSG_SHUTDOWN, msg_shutdown, NULL);
1109 /* Handle online/offline messages. */
1110 message_register(MSG_WINBIND_OFFLINE, winbind_msg_offline, NULL);
1111 message_register(MSG_WINBIND_ONLINE, winbind_msg_online, NULL);
1112 message_register(MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus,
1113 NULL);
1115 poptFreeContext(pc);
1117 netsamlogon_cache_init(); /* Non-critical */
1119 if (!init_domain_list()) {
1120 DEBUG(0,("unable to initalize domain list\n"));
1121 exit(1);
1124 init_idmap_child();
1126 smb_nscd_flush_user_cache();
1127 smb_nscd_flush_group_cache();
1129 /* Loop waiting for requests */
1131 while (1)
1132 process_loop();
1134 return 0;