r8322: * get RegSetValue() working for printer subkey values
[Samba/gbeck.git] / source / nsswitch / winbindd.c
blob078aeeb059acaf9d492225ef1eb9e7172151e70d
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 BOOL opt_dual_daemon = True;
31 static BOOL interactive = False;
33 extern BOOL override_logfile;
35 /* Reload configuration */
37 static BOOL reload_services_file(void)
39 BOOL ret;
41 if (lp_loaded()) {
42 pstring fname;
44 pstrcpy(fname,lp_configfile());
45 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
46 pstrcpy(dyn_CONFIGFILE,fname);
50 reopen_logs();
51 ret = lp_load(dyn_CONFIGFILE,False,False,True);
53 reopen_logs();
54 load_interfaces();
56 return(ret);
60 #if DUMP_CORE
62 /**************************************************************************** **
63 Prepare to dump a core file - carefully!
64 **************************************************************************** */
66 static BOOL dump_core(void)
68 char *p;
69 pstring dname;
70 pstrcpy( dname, lp_logfile() );
71 if ((p=strrchr(dname,'/')))
72 *p=0;
73 pstrcat( dname, "/corefiles" );
74 mkdir( dname, 0700 );
75 sys_chown( dname, getuid(), getgid() );
76 chmod( dname, 0700 );
77 if ( chdir(dname) )
78 return( False );
79 umask( ~(0700) );
81 #ifdef HAVE_GETRLIMIT
82 #ifdef RLIMIT_CORE
84 struct rlimit rlp;
85 getrlimit( RLIMIT_CORE, &rlp );
86 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
87 setrlimit( RLIMIT_CORE, &rlp );
88 getrlimit( RLIMIT_CORE, &rlp );
89 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
91 #endif
92 #endif
94 DEBUG(0,("Dumping core in %s\n",dname));
95 abort();
96 return( True );
97 } /* dump_core */
98 #endif
100 /**************************************************************************** **
101 Handle a fault..
102 **************************************************************************** */
104 static void fault_quit(void)
106 #if DUMP_CORE
107 dump_core();
108 #endif
111 static void winbindd_status(void)
113 struct winbindd_cli_state *tmp;
115 DEBUG(0, ("winbindd status:\n"));
117 /* Print client state information */
119 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
121 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
122 DEBUG(2, ("\tclient list:\n"));
123 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
124 DEBUG(2, ("\t\tpid %lu, sock %d, rbl %d, wbl %d\n",
125 (unsigned long)tmp->pid, tmp->sock, tmp->read_buf_len,
126 tmp->write_buf_len));
131 /* Print winbindd status to log file */
133 static void print_winbindd_status(void)
135 winbindd_status();
138 /* Flush client cache */
140 static void flush_caches(void)
142 #if 0
143 /* Clear cached user and group enumation info */
144 if (!opt_dual_daemon) /* Until we have coherent cache flush. */
145 wcache_flush_cache();
146 #endif
148 /* We need to invalidate cached user list entries on a SIGHUP
149 otherwise cached access denied errors due to restrict anonymous
150 hang around until the sequence number changes. */
152 wcache_invalidate_cache();
155 /* Handle the signal by unlinking socket and exiting */
157 static void terminate(void)
159 pstring path;
161 idmap_close();
163 /* Remove socket file */
164 pstr_sprintf(path, "%s/%s",
165 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
166 unlink(path);
168 #if 0
169 if (interactive) {
170 TALLOC_CTX *mem_ctx = talloc_init("end_description");
171 char *description = talloc_describe_all(mem_ctx);
173 DEBUG(3, ("tallocs left:\n%s\n", description));
174 talloc_destroy(mem_ctx);
176 #endif
178 exit(0);
181 static BOOL do_sigterm;
183 static void termination_handler(int signum)
185 do_sigterm = True;
186 sys_select_signal(signum);
189 static BOOL do_sigusr2;
191 static void sigusr2_handler(int signum)
193 do_sigusr2 = True;
194 sys_select_signal(SIGUSR2);
197 static BOOL do_sighup;
199 static void sighup_handler(int signum)
201 do_sighup = True;
202 sys_select_signal(SIGHUP);
205 static BOOL do_sigchld;
207 static void sigchld_handler(int signum)
209 do_sigchld = True;
210 sys_select_signal(SIGCHLD);
213 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
214 static void msg_reload_services(int msg_type, pid_t src, void *buf, size_t len)
216 /* Flush various caches */
217 flush_caches();
218 reload_services_file();
221 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
222 static void msg_shutdown(int msg_type, pid_t src, void *buf, size_t len)
224 terminate();
227 static struct winbindd_dispatch_table {
228 enum winbindd_cmd cmd;
229 void (*fn)(struct winbindd_cli_state *state);
230 const char *winbindd_cmd_name;
231 } dispatch_table[] = {
233 /* User functions */
235 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
236 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
238 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
239 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
240 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
242 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
243 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
244 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
245 "GETUSERDOMGROUPS" },
247 /* Group functions */
249 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
250 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
251 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
252 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
253 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
254 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
256 /* PAM auth functions */
258 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
259 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
260 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
262 /* Enumeration functions */
264 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
265 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
266 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
267 "LIST_TRUSTDOM" },
268 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
270 /* SID related functions */
272 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
273 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
275 /* Lookup related functions */
277 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
278 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
279 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
280 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
281 { WINBINDD_ALLOCATE_RID, winbindd_allocate_rid, "ALLOCATE_RID" },
282 { WINBINDD_ALLOCATE_RID_AND_GID, winbindd_allocate_rid_and_gid,
283 "ALLOCATE_RID_AND_GID" },
285 /* Miscellaneous */
287 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
288 { WINBINDD_PING, winbindd_ping, "PING" },
289 { WINBINDD_INFO, winbindd_info, "INFO" },
290 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
291 "INTERFACE_VERSION" },
292 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
293 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
294 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
295 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
296 "WINBINDD_PRIV_PIPE_DIR" },
297 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
299 /* WINS functions */
301 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
302 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
304 /* End of list */
306 { WINBINDD_NUM_CMDS, NULL, "NONE" }
309 static void process_request(struct winbindd_cli_state *state)
311 struct winbindd_dispatch_table *table = dispatch_table;
313 /* Free response data - we may be interrupted and receive another
314 command before being able to send this data off. */
316 SAFE_FREE(state->response.extra_data);
318 ZERO_STRUCT(state->response);
320 state->response.result = WINBINDD_PENDING;
321 state->response.length = sizeof(struct winbindd_response);
323 state->mem_ctx = talloc_init("winbind request");
324 if (state->mem_ctx == NULL)
325 return;
327 /* Process command */
329 for (table = dispatch_table; table->fn; table++) {
330 if (state->request.cmd == table->cmd) {
331 DEBUG(10,("process_request: request fn %s\n",
332 table->winbindd_cmd_name ));
333 table->fn(state);
334 break;
338 if (!table->fn) {
339 DEBUG(10,("process_request: unknown request fn number %d\n",
340 (int)state->request.cmd ));
341 request_error(state);
346 * A list of file descriptors being monitored by select in the main processing
347 * loop. fd_event->handler is called whenever the socket is readable/writable.
350 static struct fd_event *fd_events = NULL;
352 void add_fd_event(struct fd_event *ev)
354 struct fd_event *match;
356 /* only add unique fd_event structs */
358 for (match=fd_events; match; match=match->next ) {
359 #ifdef DEVELOPER
360 SMB_ASSERT( match != ev );
361 #else
362 if ( match == ev )
363 return;
364 #endif
367 DLIST_ADD(fd_events, ev);
370 void remove_fd_event(struct fd_event *ev)
372 DLIST_REMOVE(fd_events, ev);
376 * Handler for fd_events to complete a read/write request, set up by
377 * setup_async_read/setup_async_write.
380 static void rw_callback(struct fd_event *event, int flags)
382 size_t todo;
383 ssize_t done = 0;
385 todo = event->length - event->done;
387 if (event->flags & EVENT_FD_WRITE) {
388 SMB_ASSERT(flags == EVENT_FD_WRITE);
389 done = sys_write(event->fd,
390 &((char *)event->data)[event->done],
391 todo);
393 if (done <= 0) {
394 event->flags = 0;
395 event->finished(event->private_data, False);
396 return;
400 if (event->flags & EVENT_FD_READ) {
401 SMB_ASSERT(flags == EVENT_FD_READ);
402 done = sys_read(event->fd, &((char *)event->data)[event->done],
403 todo);
405 if (done <= 0) {
406 event->flags = 0;
407 event->finished(event->private_data, False);
408 return;
412 event->done += done;
414 if (event->done == event->length) {
415 event->flags = 0;
416 event->finished(event->private_data, True);
421 * Request an async read/write on a fd_event structure. (*finished) is called
422 * when the request is completed or an error had occurred.
425 void setup_async_read(struct fd_event *event, void *data, size_t length,
426 void (*finished)(void *private_data, BOOL success),
427 void *private_data)
429 SMB_ASSERT(event->flags == 0);
430 event->data = data;
431 event->length = length;
432 event->done = 0;
433 event->handler = rw_callback;
434 event->finished = finished;
435 event->private_data = private_data;
436 event->flags = EVENT_FD_READ;
439 void setup_async_write(struct fd_event *event, void *data, size_t length,
440 void (*finished)(void *private_data, BOOL success),
441 void *private_data)
443 SMB_ASSERT(event->flags == 0);
444 event->data = data;
445 event->length = length;
446 event->done = 0;
447 event->handler = rw_callback;
448 event->finished = finished;
449 event->private_data = private_data;
450 event->flags = EVENT_FD_WRITE;
454 * This is the main event loop of winbind requests. It goes through a
455 * state-machine of 3 read/write requests, 4 if you have extra data to send.
457 * An idle winbind client has a read request of 4 bytes outstanding,
458 * finalizing function is request_len_recv, checking the length. request_recv
459 * then processes the packet. The processing function then at some point has
460 * to call request_finished which schedules sending the response.
463 static void request_len_recv(void *private_data, BOOL success);
464 static void request_recv(void *private_data, BOOL success);
465 static void request_finished(struct winbindd_cli_state *state);
466 void request_finished_cont(void *private_data, BOOL success);
467 static void response_main_sent(void *private_data, BOOL success);
468 static void response_extra_sent(void *private_data, BOOL success);
470 static void response_extra_sent(void *private_data, BOOL success)
472 struct winbindd_cli_state *state =
473 talloc_get_type_abort(private_data, struct winbindd_cli_state);
475 if (state->mem_ctx != NULL) {
476 talloc_destroy(state->mem_ctx);
477 state->mem_ctx = NULL;
480 if (!success) {
481 state->finished = True;
482 return;
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_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 process_request(state);
561 static void request_len_recv(void *private_data, BOOL success)
563 struct winbindd_cli_state *state =
564 talloc_get_type_abort(private_data, struct winbindd_cli_state);
566 if (!success) {
567 state->finished = True;
568 return;
571 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
572 DEBUG(0,("process_loop: Invalid request size received: %d\n",
573 *(uint32 *)(&state->request)));
574 state->finished = True;
575 return;
578 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
579 sizeof(state->request) - sizeof(uint32),
580 request_recv, state);
583 /* Process a new connection by adding it to the client connection list */
585 static void new_connection(int listen_sock, BOOL privileged)
587 struct sockaddr_un sunaddr;
588 struct winbindd_cli_state *state;
589 socklen_t len;
590 int sock;
592 /* Accept connection */
594 len = sizeof(sunaddr);
596 do {
597 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
598 } while (sock == -1 && errno == EINTR);
600 if (sock == -1)
601 return;
603 DEBUG(6,("accepted socket %d\n", sock));
605 /* Create new connection structure */
607 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL)
608 return;
610 state->sock = sock;
612 state->last_access = time(NULL);
614 state->privileged = privileged;
616 state->fd_event.fd = state->sock;
617 state->fd_event.flags = 0;
618 add_fd_event(&state->fd_event);
620 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
621 request_len_recv, state);
623 /* Add to connection list */
625 winbindd_add_client(state);
628 /* Remove a client connection from client connection list */
630 static void remove_client(struct winbindd_cli_state *state)
632 /* It's a dead client - hold a funeral */
634 if (state != NULL) {
636 /* Close socket */
638 close(state->sock);
640 /* Free any getent state */
642 free_getent_state(state->getpwent_state);
643 free_getent_state(state->getgrent_state);
645 /* We may have some extra data that was not freed if the
646 client was killed unexpectedly */
648 SAFE_FREE(state->response.extra_data);
650 if (state->mem_ctx != NULL) {
651 talloc_destroy(state->mem_ctx);
652 state->mem_ctx = NULL;
655 remove_fd_event(&state->fd_event);
657 /* Remove from list and free */
659 winbindd_remove_client(state);
660 talloc_free(state);
665 /* Shutdown client connection which has been idle for the longest time */
667 static BOOL remove_idle_client(void)
669 struct winbindd_cli_state *state, *remove_state = NULL;
670 time_t last_access = 0;
671 int nidle = 0;
673 for (state = winbindd_client_list(); state; state = state->next) {
674 if (state->read_buf_len == 0 && state->write_buf_len == 0 &&
675 state->response.result != WINBINDD_PENDING &&
676 !state->getpwent_state && !state->getgrent_state) {
677 nidle++;
678 if (!last_access || state->last_access < last_access) {
679 last_access = state->last_access;
680 remove_state = state;
685 if (remove_state) {
686 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
687 nidle, remove_state->sock, (unsigned int)remove_state->pid));
688 remove_client(remove_state);
689 return True;
692 return False;
695 /* Process a complete received packet from a client */
697 void winbind_process_packet(struct winbindd_cli_state *state)
699 /* Process request */
701 /* Ensure null termination of entire request */
702 state->request.null_term = '\0';
704 state->pid = state->request.pid;
706 process_request(state);
708 /* Update client state */
710 state->read_buf_len = 0;
711 state->write_buf_len = sizeof(struct winbindd_response);
713 /* we might need to send it to the dual daemon */
714 if (opt_dual_daemon) {
715 dual_send_request(state);
719 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
720 non-forking, non-threaded model which allows us to handle many
721 simultaneous connections while remaining impervious to many denial of
722 service attacks. */
724 static void process_loop(void)
726 struct winbindd_cli_state *state;
727 struct fd_event *ev;
728 fd_set r_fds, w_fds;
729 int maxfd, listen_sock, listen_priv_sock, selret;
730 struct timeval timeout;
732 /* We'll be doing this a lot */
734 /* Handle messages */
736 message_dispatch();
738 /* refresh the trusted domain cache */
740 rescan_trusted_domains();
742 /* Free up temporary memory */
744 lp_talloc_free();
745 main_loop_talloc_free();
747 /* Initialise fd lists for select() */
749 listen_sock = open_winbindd_socket();
750 listen_priv_sock = open_winbindd_priv_socket();
752 if (listen_sock == -1 || listen_priv_sock == -1) {
753 perror("open_winbind_socket");
754 exit(1);
757 maxfd = MAX(listen_sock, listen_priv_sock);
759 FD_ZERO(&r_fds);
760 FD_ZERO(&w_fds);
761 FD_SET(listen_sock, &r_fds);
762 FD_SET(listen_priv_sock, &r_fds);
764 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
765 timeout.tv_usec = 0;
767 if (opt_dual_daemon) {
768 maxfd = dual_select_setup(&w_fds, maxfd);
771 /* Set up client readers and writers */
773 state = winbindd_client_list();
775 while (state) {
777 struct winbindd_cli_state *next = state->next;
779 /* Dispose of client connection if it is marked as
780 finished */
782 if (state->finished)
783 remove_client(state);
785 state = next;
788 for (ev = fd_events; ev; ev = ev->next) {
789 if (ev->flags & EVENT_FD_READ) {
790 FD_SET(ev->fd, &r_fds);
791 maxfd = MAX(ev->fd, maxfd);
793 if (ev->flags & EVENT_FD_WRITE) {
794 FD_SET(ev->fd, &w_fds);
795 maxfd = MAX(ev->fd, maxfd);
799 /* Call select */
801 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
803 if (selret == 0)
804 goto no_fds_ready;
806 if ((selret == -1 && errno != EINTR) || selret == 0) {
808 /* Select error, something is badly wrong */
810 perror("select");
811 exit(1);
814 /* Create a new connection if listen_sock readable */
816 if (opt_dual_daemon) {
817 dual_select(&w_fds);
820 ev = fd_events;
821 while (ev != NULL) {
822 struct fd_event *next = ev->next;
823 int flags = 0;
824 if (FD_ISSET(ev->fd, &r_fds))
825 flags |= EVENT_FD_READ;
826 if (FD_ISSET(ev->fd, &w_fds))
827 flags |= EVENT_FD_WRITE;
828 if (flags)
829 ev->handler(ev, flags);
830 ev = next;
833 if (FD_ISSET(listen_sock, &r_fds)) {
834 while (winbindd_num_clients() >
835 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
836 DEBUG(5,("winbindd: Exceeding %d client "
837 "connections, removing idle "
838 "connection.\n",
839 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
840 if (!remove_idle_client()) {
841 DEBUG(0,("winbindd: Exceeding %d "
842 "client connections, no idle "
843 "connection found\n",
844 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
845 break;
848 /* new, non-privileged connection */
849 new_connection(listen_sock, False);
852 if (FD_ISSET(listen_priv_sock, &r_fds)) {
853 while (winbindd_num_clients() >
854 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
855 DEBUG(5,("winbindd: Exceeding %d client "
856 "connections, removing idle "
857 "connection.\n",
858 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
859 if (!remove_idle_client()) {
860 DEBUG(0,("winbindd: Exceeding %d "
861 "client connections, no idle "
862 "connection found\n",
863 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
864 break;
867 /* new, privileged connection */
868 new_connection(listen_priv_sock, True);
871 no_fds_ready:
873 #if 0
874 winbindd_check_cache_size(time(NULL));
875 #endif
877 /* Check signal handling things */
879 if (do_sigterm)
880 terminate();
882 if (do_sighup) {
884 DEBUG(3, ("got SIGHUP\n"));
886 msg_reload_services(MSG_SMB_CONF_UPDATED, (pid_t) 0, NULL, 0);
887 do_sighup = False;
890 if (do_sigusr2) {
891 print_winbindd_status();
892 do_sigusr2 = False;
895 if (do_sigchld) {
896 pid_t pid;
898 do_sigchld = False;
900 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
901 winbind_child_died(pid);
906 /* Main function */
908 struct winbindd_state server_state; /* Server state information */
910 int main(int argc, char **argv)
912 pstring logfile;
913 static BOOL Fork = True;
914 static BOOL log_stdout = False;
915 struct poptOption long_options[] = {
916 POPT_AUTOHELP
917 { "stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
918 { "foreground", 'F', POPT_ARG_VAL, &Fork, False, "Daemon in foreground mode" },
919 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
920 { "single-daemon", 'Y', POPT_ARG_VAL, &opt_dual_daemon, False, "Single daemon mode" },
921 { "no-caching", 'n', POPT_ARG_VAL, &opt_nocache, True, "Disable caching" },
922 POPT_COMMON_SAMBA
923 POPT_TABLEEND
925 poptContext pc;
926 int opt;
928 /* glibc (?) likes to print "User defined signal 1" and exit if a
929 SIGUSR[12] is received before a handler is installed */
931 CatchSignal(SIGUSR1, SIG_IGN);
932 CatchSignal(SIGUSR2, SIG_IGN);
934 fault_setup((void (*)(void *))fault_quit );
936 /* Initialise for running in non-root mode */
938 sec_init();
940 set_remote_machine_name("winbindd", False);
942 /* Set environment variable so we don't recursively call ourselves.
943 This may also be useful interactively. */
945 setenv(WINBINDD_DONT_ENV, "1", 1);
947 /* Initialise samba/rpc client stuff */
949 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options,
950 POPT_CONTEXT_KEEP_FIRST);
952 while ((opt = poptGetNextOpt(pc)) != -1) {
953 switch (opt) {
954 /* Don't become a daemon */
955 case 'i':
956 interactive = True;
957 log_stdout = True;
958 Fork = False;
959 break;
964 if (log_stdout && Fork) {
965 printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
966 poptPrintUsage(pc, stderr, 0);
967 exit(1);
970 if (!override_logfile) {
971 pstr_sprintf(logfile, "%s/log.winbindd", dyn_LOGFILEBASE);
972 lp_set_logfile(logfile);
974 setup_logging("winbindd", log_stdout);
975 reopen_logs();
977 DEBUG(1, ("winbindd version %s started.\n", SAMBA_VERSION_STRING) );
978 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2004\n" ) );
980 if (!reload_services_file()) {
981 DEBUG(0, ("error opening config file\n"));
982 exit(1);
985 /* Setup names. */
987 if (!init_names())
988 exit(1);
990 load_interfaces();
992 if (!secrets_init()) {
994 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
995 return False;
998 /* Enable netbios namecache */
1000 namecache_enable();
1002 /* Check winbindd parameters are valid */
1004 ZERO_STRUCT(server_state);
1006 /* Winbind daemon initialisation */
1008 if ( (!winbindd_param_init()) || (!winbindd_upgrade_idmap()) ||
1009 (!idmap_init(lp_idmap_backend())) ) {
1010 DEBUG(1, ("Could not init idmap -- netlogon proxy only\n"));
1011 idmap_set_proxyonly();
1014 /* Unblock all signals we are interested in as they may have been
1015 blocked by the parent process. */
1017 BlockSignals(False, SIGINT);
1018 BlockSignals(False, SIGQUIT);
1019 BlockSignals(False, SIGTERM);
1020 BlockSignals(False, SIGUSR1);
1021 BlockSignals(False, SIGUSR2);
1022 BlockSignals(False, SIGHUP);
1023 BlockSignals(False, SIGCHLD);
1025 /* Setup signal handlers */
1027 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
1028 CatchSignal(SIGQUIT, termination_handler);
1029 CatchSignal(SIGTERM, termination_handler);
1030 CatchSignal(SIGCHLD, sigchld_handler);
1032 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1034 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
1035 CatchSignal(SIGHUP, sighup_handler);
1037 if (!interactive)
1038 become_daemon(Fork);
1040 pidfile_create("winbindd");
1042 #if HAVE_SETPGID
1044 * If we're interactive we want to set our own process group for
1045 * signal management.
1047 if (interactive)
1048 setpgid( (pid_t)0, (pid_t)0);
1049 #endif
1051 if (opt_dual_daemon) {
1052 do_dual_daemon();
1055 /* Initialise messaging system */
1057 if (!message_init()) {
1058 DEBUG(0, ("unable to initialise messaging system\n"));
1059 exit(1);
1062 /* React on 'smbcontrol winbindd reload-config' in the same way
1063 as to SIGHUP signal */
1064 message_register(MSG_SMB_CONF_UPDATED, msg_reload_services);
1065 message_register(MSG_SHUTDOWN, msg_shutdown);
1067 poptFreeContext(pc);
1069 netsamlogon_cache_init(); /* Non-critical */
1071 init_domain_list();
1073 init_idmap_child();
1075 /* Loop waiting for requests */
1077 while (1)
1078 process_loop();
1080 trustdom_cache_shutdown();
1082 return 0;