r9322: fixing debug log and ensuring that we set the right winbind_methods
[Samba.git] / source / nsswitch / winbindd.c
blobb651a7c4887b66657c88de50dbedb1c30e4b7f61
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 BOOL opt_nocache = False;
30 static BOOL interactive = False;
32 extern BOOL override_logfile;
34 /* Reload configuration */
36 static BOOL reload_services_file(void)
38 BOOL ret;
40 if (lp_loaded()) {
41 pstring fname;
43 pstrcpy(fname,lp_configfile());
44 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
45 pstrcpy(dyn_CONFIGFILE,fname);
49 reopen_logs();
50 ret = lp_load(dyn_CONFIGFILE,False,False,True);
52 reopen_logs();
53 load_interfaces();
55 return(ret);
59 #if DUMP_CORE
61 /**************************************************************************** **
62 Prepare to dump a core file - carefully!
63 **************************************************************************** */
65 static BOOL dump_core(void)
67 char *p;
68 pstring dname;
69 pstrcpy( dname, lp_logfile() );
70 if ((p=strrchr(dname,'/')))
71 *p=0;
72 pstrcat( dname, "/corefiles" );
73 mkdir( dname, 0700 );
74 sys_chown( dname, getuid(), getgid() );
75 chmod( dname, 0700 );
76 if ( chdir(dname) )
77 return( False );
78 umask( ~(0700) );
80 #ifdef HAVE_GETRLIMIT
81 #ifdef RLIMIT_CORE
83 struct rlimit rlp;
84 getrlimit( RLIMIT_CORE, &rlp );
85 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
86 setrlimit( RLIMIT_CORE, &rlp );
87 getrlimit( RLIMIT_CORE, &rlp );
88 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
90 #endif
91 #endif
93 DEBUG(0,("Dumping core in %s\n",dname));
94 abort();
95 return( True );
96 } /* dump_core */
97 #endif
99 /**************************************************************************** **
100 Handle a fault..
101 **************************************************************************** */
103 static void fault_quit(void)
105 #if DUMP_CORE
106 dump_core();
107 #endif
110 static void winbindd_status(void)
112 struct winbindd_cli_state *tmp;
114 DEBUG(0, ("winbindd status:\n"));
116 /* Print client state information */
118 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
120 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
121 DEBUG(2, ("\tclient list:\n"));
122 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
123 DEBUG(2, ("\t\tpid %lu, sock %d, rbl %d, wbl %d\n",
124 (unsigned long)tmp->pid, tmp->sock, tmp->read_buf_len,
125 tmp->write_buf_len));
130 /* Print winbindd status to log file */
132 static void print_winbindd_status(void)
134 winbindd_status();
137 /* Flush client cache */
139 static void flush_caches(void)
141 /* We need to invalidate cached user list entries on a SIGHUP
142 otherwise cached access denied errors due to restrict anonymous
143 hang around until the sequence number changes. */
145 wcache_invalidate_cache();
148 /* Handle the signal by unlinking socket and exiting */
150 static void terminate(void)
152 pstring path;
154 idmap_close();
156 /* Remove socket file */
157 pstr_sprintf(path, "%s/%s",
158 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
159 unlink(path);
161 #if 0
162 if (interactive) {
163 TALLOC_CTX *mem_ctx = talloc_init("end_description");
164 char *description = talloc_describe_all(mem_ctx);
166 DEBUG(3, ("tallocs left:\n%s\n", description));
167 talloc_destroy(mem_ctx);
169 #endif
171 exit(0);
174 static BOOL do_sigterm;
176 static void termination_handler(int signum)
178 do_sigterm = True;
179 sys_select_signal(signum);
182 static BOOL do_sigusr2;
184 static void sigusr2_handler(int signum)
186 do_sigusr2 = True;
187 sys_select_signal(SIGUSR2);
190 static BOOL do_sighup;
192 static void sighup_handler(int signum)
194 do_sighup = True;
195 sys_select_signal(SIGHUP);
198 static BOOL do_sigchld;
200 static void sigchld_handler(int signum)
202 do_sigchld = True;
203 sys_select_signal(SIGCHLD);
206 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
207 static void msg_reload_services(int msg_type, pid_t src, void *buf, size_t len)
209 /* Flush various caches */
210 flush_caches();
211 reload_services_file();
214 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
215 static void msg_shutdown(int msg_type, pid_t src, void *buf, size_t len)
217 terminate();
220 static struct winbindd_dispatch_table {
221 enum winbindd_cmd cmd;
222 void (*fn)(struct winbindd_cli_state *state);
223 const char *winbindd_cmd_name;
224 } dispatch_table[] = {
226 /* User functions */
228 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
229 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
231 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
232 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
233 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
235 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
236 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
237 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
238 "GETUSERDOMGROUPS" },
239 { WINBINDD_QUERY_ALIASMEM, winbindd_query_aliasmem,
240 "QUERY_ALIASMEM" },
241 { WINBINDD_QUERY_GROUPMEM, winbindd_group_members,
242 "QUERY_GROUPMEM" },
244 /* Group functions */
246 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
247 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
248 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
249 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
250 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
251 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
253 /* PAM auth functions */
255 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
256 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
257 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
259 /* Enumeration functions */
261 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
262 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
263 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
264 "LIST_TRUSTDOM" },
265 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
267 /* SID related functions */
269 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
270 { WINBINDD_LOOKUPSIDS, winbindd_lookupsids, "LOOKUPSIDS" },
271 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
273 /* Lookup related functions */
275 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
276 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
277 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
278 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
279 { WINBINDD_ALLOCATE_RID, winbindd_allocate_rid, "ALLOCATE_RID" },
280 { WINBINDD_ALLOCATE_RID_AND_GID, winbindd_allocate_rid_and_gid,
281 "ALLOCATE_RID_AND_GID" },
283 /* Miscellaneous */
285 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
286 { WINBINDD_PING, winbindd_ping, "PING" },
287 { WINBINDD_INFO, winbindd_info, "INFO" },
288 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
289 "INTERFACE_VERSION" },
290 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
291 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
292 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
293 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
294 "WINBINDD_PRIV_PIPE_DIR" },
295 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
297 /* WINS functions */
299 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
300 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
302 /* End of list */
304 { WINBINDD_NUM_CMDS, NULL, "NONE" }
307 static void process_request(struct winbindd_cli_state *state)
309 struct winbindd_dispatch_table *table = dispatch_table;
311 /* Free response data - we may be interrupted and receive another
312 command before being able to send this data off. */
314 SAFE_FREE(state->response.extra_data);
316 ZERO_STRUCT(state->response);
318 state->response.result = WINBINDD_PENDING;
319 state->response.length = sizeof(struct winbindd_response);
321 state->mem_ctx = talloc_init("winbind request");
322 if (state->mem_ctx == NULL)
323 return;
325 /* Process command */
327 for (table = dispatch_table; table->fn; table++) {
328 if (state->request.cmd == table->cmd) {
329 DEBUG(10,("process_request: request fn %s\n",
330 table->winbindd_cmd_name ));
331 table->fn(state);
332 break;
336 if (!table->fn) {
337 DEBUG(10,("process_request: unknown request fn number %d\n",
338 (int)state->request.cmd ));
339 request_error(state);
344 * A list of file descriptors being monitored by select in the main processing
345 * loop. fd_event->handler is called whenever the socket is readable/writable.
348 static struct fd_event *fd_events = NULL;
350 void add_fd_event(struct fd_event *ev)
352 struct fd_event *match;
354 /* only add unique fd_event structs */
356 for (match=fd_events; match; match=match->next ) {
357 #ifdef DEVELOPER
358 SMB_ASSERT( match != ev );
359 #else
360 if ( match == ev )
361 return;
362 #endif
365 DLIST_ADD(fd_events, ev);
368 void remove_fd_event(struct fd_event *ev)
370 DLIST_REMOVE(fd_events, ev);
374 * Handler for fd_events to complete a read/write request, set up by
375 * setup_async_read/setup_async_write.
378 static void rw_callback(struct fd_event *event, int flags)
380 size_t todo;
381 ssize_t done = 0;
383 todo = event->length - event->done;
385 if (event->flags & EVENT_FD_WRITE) {
386 SMB_ASSERT(flags == EVENT_FD_WRITE);
387 done = sys_write(event->fd,
388 &((char *)event->data)[event->done],
389 todo);
391 if (done <= 0) {
392 event->flags = 0;
393 event->finished(event->private_data, False);
394 return;
398 if (event->flags & EVENT_FD_READ) {
399 SMB_ASSERT(flags == EVENT_FD_READ);
400 done = sys_read(event->fd, &((char *)event->data)[event->done],
401 todo);
403 if (done <= 0) {
404 event->flags = 0;
405 event->finished(event->private_data, False);
406 return;
410 event->done += done;
412 if (event->done == event->length) {
413 event->flags = 0;
414 event->finished(event->private_data, True);
419 * Request an async read/write on a fd_event structure. (*finished) is called
420 * when the request is completed or an error had occurred.
423 void setup_async_read(struct fd_event *event, void *data, size_t length,
424 void (*finished)(void *private_data, BOOL success),
425 void *private_data)
427 SMB_ASSERT(event->flags == 0);
428 event->data = data;
429 event->length = length;
430 event->done = 0;
431 event->handler = rw_callback;
432 event->finished = finished;
433 event->private_data = private_data;
434 event->flags = EVENT_FD_READ;
437 void setup_async_write(struct fd_event *event, void *data, size_t length,
438 void (*finished)(void *private_data, BOOL success),
439 void *private_data)
441 SMB_ASSERT(event->flags == 0);
442 event->data = data;
443 event->length = length;
444 event->done = 0;
445 event->handler = rw_callback;
446 event->finished = finished;
447 event->private_data = private_data;
448 event->flags = EVENT_FD_WRITE;
452 * This is the main event loop of winbind requests. It goes through a
453 * state-machine of 3 read/write requests, 4 if you have extra data to send.
455 * An idle winbind client has a read request of 4 bytes outstanding,
456 * finalizing function is request_len_recv, checking the length. request_recv
457 * then processes the packet. The processing function then at some point has
458 * to call request_finished which schedules sending the response.
461 static void request_len_recv(void *private_data, BOOL success);
462 static void request_recv(void *private_data, BOOL success);
463 static void request_main_recv(void *private_data, BOOL success);
464 static void request_finished(struct winbindd_cli_state *state);
465 void request_finished_cont(void *private_data, BOOL success);
466 static void response_main_sent(void *private_data, BOOL success);
467 static void response_extra_sent(void *private_data, BOOL success);
469 static void response_extra_sent(void *private_data, BOOL success)
471 struct winbindd_cli_state *state =
472 talloc_get_type_abort(private_data, struct winbindd_cli_state);
474 if (state->mem_ctx != NULL) {
475 talloc_destroy(state->mem_ctx);
476 state->mem_ctx = NULL;
479 if (!success) {
480 state->finished = True;
481 return;
484 SAFE_FREE(state->request.extra_data);
485 SAFE_FREE(state->response.extra_data);
487 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
488 request_len_recv, state);
491 static void response_main_sent(void *private_data, BOOL success)
493 struct winbindd_cli_state *state =
494 talloc_get_type_abort(private_data, struct winbindd_cli_state);
496 if (!success) {
497 state->finished = True;
498 return;
501 if (state->response.length == sizeof(state->response)) {
502 if (state->mem_ctx != NULL) {
503 talloc_destroy(state->mem_ctx);
504 state->mem_ctx = NULL;
507 setup_async_read(&state->fd_event, &state->request,
508 sizeof(uint32), request_len_recv, state);
509 return;
512 setup_async_write(&state->fd_event, state->response.extra_data,
513 state->response.length - sizeof(state->response),
514 response_extra_sent, state);
517 static void request_finished(struct winbindd_cli_state *state)
519 setup_async_write(&state->fd_event, &state->response,
520 sizeof(state->response), response_main_sent, state);
523 void request_error(struct winbindd_cli_state *state)
525 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
526 state->response.result = WINBINDD_ERROR;
527 request_finished(state);
530 void request_ok(struct winbindd_cli_state *state)
532 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
533 state->response.result = WINBINDD_OK;
534 request_finished(state);
537 void request_finished_cont(void *private_data, BOOL success)
539 struct winbindd_cli_state *state =
540 talloc_get_type_abort(private_data, struct winbindd_cli_state);
542 if (success)
543 request_ok(state);
544 else
545 request_error(state);
548 static void request_len_recv(void *private_data, BOOL success)
550 struct winbindd_cli_state *state =
551 talloc_get_type_abort(private_data, struct winbindd_cli_state);
553 if (!success) {
554 state->finished = True;
555 return;
558 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
559 DEBUG(0,("request_len_recv: Invalid request size received: %d\n",
560 *(uint32 *)(&state->request)));
561 state->finished = True;
562 return;
565 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
566 sizeof(state->request) - sizeof(uint32),
567 request_main_recv, state);
570 static void request_main_recv(void *private_data, BOOL success)
572 struct winbindd_cli_state *state =
573 talloc_get_type_abort(private_data, struct winbindd_cli_state);
575 if (!success) {
576 state->finished = True;
577 return;
580 if (state->request.extra_len == 0) {
581 state->request.extra_data = NULL;
582 request_recv(state, True);
583 return;
586 if ((!state->privileged) &&
587 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
588 DEBUG(3, ("Got request with %d bytes extra data on "
589 "unprivileged socket\n", state->request.extra_len));
590 state->request.extra_data = NULL;
591 state->finished = True;
592 return;
595 state->request.extra_data =
596 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
598 if (state->request.extra_data == NULL) {
599 DEBUG(0, ("malloc failed\n"));
600 state->finished = True;
601 return;
604 /* Ensure null termination */
605 state->request.extra_data[state->request.extra_len] = '\0';
607 setup_async_read(&state->fd_event, state->request.extra_data,
608 state->request.extra_len, request_recv, state);
611 static void request_recv(void *private_data, BOOL success)
613 struct winbindd_cli_state *state =
614 talloc_get_type_abort(private_data, struct winbindd_cli_state);
616 if (!success) {
617 state->finished = True;
618 return;
621 process_request(state);
624 /* Process a new connection by adding it to the client connection list */
626 static void new_connection(int listen_sock, BOOL privileged)
628 struct sockaddr_un sunaddr;
629 struct winbindd_cli_state *state;
630 socklen_t len;
631 int sock;
633 /* Accept connection */
635 len = sizeof(sunaddr);
637 do {
638 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
639 } while (sock == -1 && errno == EINTR);
641 if (sock == -1)
642 return;
644 DEBUG(6,("accepted socket %d\n", sock));
646 /* Create new connection structure */
648 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL)
649 return;
651 state->sock = sock;
653 state->last_access = time(NULL);
655 state->privileged = privileged;
657 state->fd_event.fd = state->sock;
658 state->fd_event.flags = 0;
659 add_fd_event(&state->fd_event);
661 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
662 request_len_recv, state);
664 /* Add to connection list */
666 winbindd_add_client(state);
669 /* Remove a client connection from client connection list */
671 static void remove_client(struct winbindd_cli_state *state)
673 /* It's a dead client - hold a funeral */
675 if (state != NULL) {
677 /* Close socket */
679 close(state->sock);
681 /* Free any getent state */
683 free_getent_state(state->getpwent_state);
684 free_getent_state(state->getgrent_state);
686 /* We may have some extra data that was not freed if the
687 client was killed unexpectedly */
689 SAFE_FREE(state->response.extra_data);
691 if (state->mem_ctx != NULL) {
692 talloc_destroy(state->mem_ctx);
693 state->mem_ctx = NULL;
696 remove_fd_event(&state->fd_event);
698 /* Remove from list and free */
700 winbindd_remove_client(state);
701 talloc_free(state);
706 /* Shutdown client connection which has been idle for the longest time */
708 static BOOL remove_idle_client(void)
710 struct winbindd_cli_state *state, *remove_state = NULL;
711 time_t last_access = 0;
712 int nidle = 0;
714 for (state = winbindd_client_list(); state; state = state->next) {
715 if (state->read_buf_len == 0 && state->write_buf_len == 0 &&
716 state->response.result != WINBINDD_PENDING &&
717 !state->getpwent_state && !state->getgrent_state) {
718 nidle++;
719 if (!last_access || state->last_access < last_access) {
720 last_access = state->last_access;
721 remove_state = state;
726 if (remove_state) {
727 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
728 nidle, remove_state->sock, (unsigned int)remove_state->pid));
729 remove_client(remove_state);
730 return True;
733 return False;
736 /* Process a complete received packet from a client */
738 void winbind_process_packet(struct winbindd_cli_state *state)
740 /* Process request */
742 /* Ensure null termination of entire request */
743 state->request.null_term = '\0';
745 state->pid = state->request.pid;
747 process_request(state);
749 /* Update client state */
751 state->read_buf_len = 0;
752 state->write_buf_len = sizeof(struct winbindd_response);
755 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
756 non-forking, non-threaded model which allows us to handle many
757 simultaneous connections while remaining impervious to many denial of
758 service attacks. */
760 static void process_loop(void)
762 struct winbindd_cli_state *state;
763 struct fd_event *ev;
764 fd_set r_fds, w_fds;
765 int maxfd, listen_sock, listen_priv_sock, selret;
766 struct timeval timeout;
768 /* We'll be doing this a lot */
770 /* Handle messages */
772 message_dispatch();
774 /* refresh the trusted domain cache */
776 rescan_trusted_domains();
778 /* Free up temporary memory */
780 lp_talloc_free();
781 main_loop_talloc_free();
783 /* Initialise fd lists for select() */
785 listen_sock = open_winbindd_socket();
786 listen_priv_sock = open_winbindd_priv_socket();
788 if (listen_sock == -1 || listen_priv_sock == -1) {
789 perror("open_winbind_socket");
790 exit(1);
793 maxfd = MAX(listen_sock, listen_priv_sock);
795 FD_ZERO(&r_fds);
796 FD_ZERO(&w_fds);
797 FD_SET(listen_sock, &r_fds);
798 FD_SET(listen_priv_sock, &r_fds);
800 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
801 timeout.tv_usec = 0;
803 /* Set up client readers and writers */
805 state = winbindd_client_list();
807 while (state) {
809 struct winbindd_cli_state *next = state->next;
811 /* Dispose of client connection if it is marked as
812 finished */
814 if (state->finished)
815 remove_client(state);
817 state = next;
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);
825 if (ev->flags & EVENT_FD_WRITE) {
826 FD_SET(ev->fd, &w_fds);
827 maxfd = MAX(ev->fd, maxfd);
831 /* Call select */
833 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
835 if (selret == 0)
836 goto no_fds_ready;
838 if ((selret == -1 && errno != EINTR) || selret == 0) {
840 /* Select error, something is badly wrong */
842 perror("select");
843 exit(1);
846 /* Create a new connection if listen_sock readable */
848 ev = fd_events;
849 while (ev != NULL) {
850 struct fd_event *next = ev->next;
851 int flags = 0;
852 if (FD_ISSET(ev->fd, &r_fds))
853 flags |= EVENT_FD_READ;
854 if (FD_ISSET(ev->fd, &w_fds))
855 flags |= EVENT_FD_WRITE;
856 if (flags)
857 ev->handler(ev, flags);
858 ev = next;
861 if (FD_ISSET(listen_sock, &r_fds)) {
862 while (winbindd_num_clients() >
863 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
864 DEBUG(5,("winbindd: Exceeding %d client "
865 "connections, removing idle "
866 "connection.\n",
867 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
868 if (!remove_idle_client()) {
869 DEBUG(0,("winbindd: Exceeding %d "
870 "client connections, no idle "
871 "connection found\n",
872 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
873 break;
876 /* new, non-privileged connection */
877 new_connection(listen_sock, False);
880 if (FD_ISSET(listen_priv_sock, &r_fds)) {
881 while (winbindd_num_clients() >
882 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
883 DEBUG(5,("winbindd: Exceeding %d client "
884 "connections, removing idle "
885 "connection.\n",
886 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
887 if (!remove_idle_client()) {
888 DEBUG(0,("winbindd: Exceeding %d "
889 "client connections, no idle "
890 "connection found\n",
891 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
892 break;
895 /* new, privileged connection */
896 new_connection(listen_priv_sock, True);
899 no_fds_ready:
901 #if 0
902 winbindd_check_cache_size(time(NULL));
903 #endif
905 /* Check signal handling things */
907 if (do_sigterm)
908 terminate();
910 if (do_sighup) {
912 DEBUG(3, ("got SIGHUP\n"));
914 msg_reload_services(MSG_SMB_CONF_UPDATED, (pid_t) 0, NULL, 0);
915 do_sighup = False;
918 if (do_sigusr2) {
919 print_winbindd_status();
920 do_sigusr2 = False;
923 if (do_sigchld) {
924 pid_t pid;
926 do_sigchld = False;
928 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
929 winbind_child_died(pid);
934 /* Main function */
936 struct winbindd_state server_state; /* Server state information */
938 int main(int argc, char **argv)
940 pstring logfile;
941 static BOOL Fork = True;
942 static BOOL log_stdout = False;
943 struct poptOption long_options[] = {
944 POPT_AUTOHELP
945 { "stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
946 { "foreground", 'F', POPT_ARG_VAL, &Fork, False, "Daemon in foreground mode" },
947 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
948 { "no-caching", 'n', POPT_ARG_VAL, &opt_nocache, True, "Disable caching" },
949 POPT_COMMON_SAMBA
950 POPT_TABLEEND
952 poptContext pc;
953 int opt;
955 /* glibc (?) likes to print "User defined signal 1" and exit if a
956 SIGUSR[12] is received before a handler is installed */
958 CatchSignal(SIGUSR1, SIG_IGN);
959 CatchSignal(SIGUSR2, SIG_IGN);
961 fault_setup((void (*)(void *))fault_quit );
963 /* Initialise for running in non-root mode */
965 sec_init();
967 set_remote_machine_name("winbindd", False);
969 /* Set environment variable so we don't recursively call ourselves.
970 This may also be useful interactively. */
972 setenv(WINBINDD_DONT_ENV, "1", 1);
974 /* Initialise samba/rpc client stuff */
976 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options,
977 POPT_CONTEXT_KEEP_FIRST);
979 while ((opt = poptGetNextOpt(pc)) != -1) {
980 switch (opt) {
981 /* Don't become a daemon */
982 case 'i':
983 interactive = True;
984 log_stdout = True;
985 Fork = False;
986 break;
991 if (log_stdout && Fork) {
992 printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
993 poptPrintUsage(pc, stderr, 0);
994 exit(1);
997 if (!override_logfile) {
998 pstr_sprintf(logfile, "%s/log.winbindd", dyn_LOGFILEBASE);
999 lp_set_logfile(logfile);
1001 setup_logging("winbindd", log_stdout);
1002 reopen_logs();
1004 DEBUG(1, ("winbindd version %s started.\n", SAMBA_VERSION_STRING) );
1005 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2004\n" ) );
1007 if (!reload_services_file()) {
1008 DEBUG(0, ("error opening config file\n"));
1009 exit(1);
1012 /* Setup names. */
1014 if (!init_names())
1015 exit(1);
1017 load_interfaces();
1019 if (!secrets_init()) {
1021 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1022 return False;
1025 /* Enable netbios namecache */
1027 namecache_enable();
1029 /* Check winbindd parameters are valid */
1031 ZERO_STRUCT(server_state);
1033 /* Winbind daemon initialisation */
1035 if ( (!winbindd_param_init()) || (!winbindd_upgrade_idmap()) ||
1036 (!idmap_init(lp_idmap_backend())) ) {
1037 DEBUG(1, ("Could not init idmap -- netlogon proxy only\n"));
1038 idmap_set_proxyonly();
1041 /* Unblock all signals we are interested in as they may have been
1042 blocked by the parent process. */
1044 BlockSignals(False, SIGINT);
1045 BlockSignals(False, SIGQUIT);
1046 BlockSignals(False, SIGTERM);
1047 BlockSignals(False, SIGUSR1);
1048 BlockSignals(False, SIGUSR2);
1049 BlockSignals(False, SIGHUP);
1050 BlockSignals(False, SIGCHLD);
1052 /* Setup signal handlers */
1054 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
1055 CatchSignal(SIGQUIT, termination_handler);
1056 CatchSignal(SIGTERM, termination_handler);
1057 CatchSignal(SIGCHLD, sigchld_handler);
1059 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1061 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
1062 CatchSignal(SIGHUP, sighup_handler);
1064 if (!interactive)
1065 become_daemon(Fork);
1067 pidfile_create("winbindd");
1069 #if HAVE_SETPGID
1071 * If we're interactive we want to set our own process group for
1072 * signal management.
1074 if (interactive)
1075 setpgid( (pid_t)0, (pid_t)0);
1076 #endif
1078 /* Initialise messaging system */
1080 if (!message_init()) {
1081 DEBUG(0, ("unable to initialise messaging system\n"));
1082 exit(1);
1085 /* React on 'smbcontrol winbindd reload-config' in the same way
1086 as to SIGHUP signal */
1087 message_register(MSG_SMB_CONF_UPDATED, msg_reload_services);
1088 message_register(MSG_SHUTDOWN, msg_shutdown);
1090 poptFreeContext(pc);
1092 netsamlogon_cache_init(); /* Non-critical */
1094 init_domain_list();
1096 init_idmap_child();
1098 /* Loop waiting for requests */
1100 while (1)
1101 process_loop();
1103 trustdom_cache_shutdown();
1105 return 0;