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

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

Update source to 3.0.29

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