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

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

Update source to 3.0.32

File size: 28.4 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(const char *logfile)
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 /* 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);
69
70 reopen_logs();
71 load_interfaces();
72
73 return(ret);
74}
75
76
77/**************************************************************************** **
78 Handle a fault..
79 **************************************************************************** */
80
81static void fault_quit(void)
82{
83 dump_core();
84}
85
86static void winbindd_status(void)
87{
88 struct winbindd_cli_state *tmp;
89
90 DEBUG(0, ("winbindd status:\n"));
91
92 /* Print client state information */
93
94 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
95
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));
101 }
102 }
103}
104
105/* Print winbindd status to log file */
106
107static void print_winbindd_status(void)
108{
109 winbindd_status();
110}
111
112/* Flush client cache */
113
114static void flush_caches(void)
115{
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. */
119
120 wcache_invalidate_cache();
121}
122
123/* Handle the signal by unlinking socket and exiting */
124
125static void terminate(bool in_parent)
126{
127 if (in_parent) {
128 /* When parent goes away we should
129 * remove the socket file. Not so
130 * when children terminate.
131 */
132
133 pstring path;
134
135 /* Remove socket file */
136 pstr_sprintf(path, "%s/%s",
137 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
138 unlink(path);
139 }
140
141 idmap_close();
142
143 trustdom_cache_shutdown();
144
145#if 0
146 if (interactive) {
147 TALLOC_CTX *mem_ctx = talloc_init("end_description");
148 char *description = talloc_describe_all(mem_ctx);
149
150 DEBUG(3, ("tallocs left:\n%s\n", description));
151 talloc_destroy(mem_ctx);
152 }
153#endif
154
155 exit(0);
156}
157
158static BOOL do_sigterm;
159
160static void termination_handler(int signum)
161{
162 do_sigterm = True;
163 sys_select_signal(signum);
164}
165
166static BOOL do_sigusr2;
167
168static void sigusr2_handler(int signum)
169{
170 do_sigusr2 = True;
171 sys_select_signal(SIGUSR2);
172}
173
174static BOOL do_sighup;
175
176static void sighup_handler(int signum)
177{
178 do_sighup = True;
179 sys_select_signal(SIGHUP);
180}
181
182static BOOL do_sigchld;
183
184static void sigchld_handler(int signum)
185{
186 do_sigchld = True;
187 sys_select_signal(SIGCHLD);
188}
189
190/* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
191static void msg_reload_services(int msg_type, struct process_id src,
192 void *buf, size_t len, void *private_data)
193{
194 /* Flush various caches */
195 flush_caches();
196 reload_services_file((const char *) private_data);
197}
198
199/* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
200static void msg_shutdown(int msg_type, struct process_id src,
201 void *buf, size_t len, void *private_data)
202{
203 do_sigterm = True;
204}
205
206static 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[] = {
211
212 /* User functions */
213
214 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
215 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
216
217 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
218 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
219 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
220
221 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
222 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
223 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
224 "GETUSERDOMGROUPS" },
225
226 /* Group functions */
227
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" },
234
235 /* PAM auth functions */
236
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" },
242
243 /* Enumeration functions */
244
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" },
250
251 /* SID related functions */
252
253 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
254 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
255 { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
256
257 /* Lookup related functions */
258
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" },
270
271 /* Miscellaneous */
272
273 { WINBINDD_DUMP_MAPS, winbindd_dump_maps, "DUMP_MAPS" },
274
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" },
286
287 /* Credential cache access */
288 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
289
290 /* WINS functions */
291
292 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
293 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
294
295 /* End of list */
296
297 { WINBINDD_NUM_CMDS, NULL, "NONE" }
298};
299
300static void process_request(struct winbindd_cli_state *state)
301{
302 struct winbindd_dispatch_table *table = dispatch_table;
303
304 /* Free response data - we may be interrupted and receive another
305 command before being able to send this data off. */
306
307 SAFE_FREE(state->response.extra_data.data);
308
309 ZERO_STRUCT(state->response);
310
311 state->response.result = WINBINDD_PENDING;
312 state->response.length = sizeof(struct winbindd_response);
313
314 state->mem_ctx = talloc_init("winbind request");
315 if (state->mem_ctx == NULL)
316 return;
317
318 /* Remember who asked us. */
319 state->pid = state->request.pid;
320
321 /* Process command */
322
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;
329 }
330 }
331
332 if (!table->fn) {
333 DEBUG(10,("process_request: unknown request fn number %d\n",
334 (int)state->request.cmd ));
335 request_error(state);
336 }
337}
338
339/*
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.
342 */
343
344static struct fd_event *fd_events = NULL;
345
346void add_fd_event(struct fd_event *ev)
347{
348 struct fd_event *match;
349
350 /* only add unique fd_event structs */
351
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
359 }
360
361 DLIST_ADD(fd_events, ev);
362}
363
364void remove_fd_event(struct fd_event *ev)
365{
366 DLIST_REMOVE(fd_events, ev);
367}
368
369/*
370 * Handler for fd_events to complete a read/write request, set up by
371 * setup_async_read/setup_async_write.
372 */
373
374static void rw_callback(struct fd_event *event, int flags)
375{
376 size_t todo;
377 ssize_t done = 0;
378
379 todo = event->length - event->done;
380
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);
386
387 if (done <= 0) {
388 event->flags = 0;
389 event->finished(event->private_data, False);
390 return;
391 }
392 }
393
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);
398
399 if (done <= 0) {
400 event->flags = 0;
401 event->finished(event->private_data, False);
402 return;
403 }
404 }
405
406 event->done += done;
407
408 if (event->done == event->length) {
409 event->flags = 0;
410 event->finished(event->private_data, True);
411 }
412}
413
414/*
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.
417 */
418
419void setup_async_read(struct fd_event *event, void *data, size_t length,
420 void (*finished)(void *private_data, BOOL success),
421 void *private_data)
422{
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;
431}
432
433void setup_async_write(struct fd_event *event, void *data, size_t length,
434 void (*finished)(void *private_data, BOOL success),
435 void *private_data)
436{
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;
445}
446
447/*
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.
450 *
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.
455 */
456
457static void request_len_recv(void *private_data, BOOL success);
458static void request_recv(void *private_data, BOOL success);
459static void request_main_recv(void *private_data, BOOL success);
460static void request_finished(struct winbindd_cli_state *state);
461void request_finished_cont(void *private_data, BOOL success);
462static void response_main_sent(void *private_data, BOOL success);
463static void response_extra_sent(void *private_data, BOOL success);
464
465static void response_extra_sent(void *private_data, BOOL success)
466{
467 struct winbindd_cli_state *state =
468 talloc_get_type_abort(private_data, struct winbindd_cli_state);
469
470 if (state->mem_ctx != NULL) {
471 talloc_destroy(state->mem_ctx);
472 state->mem_ctx = NULL;
473 }
474
475 if (!success) {
476 state->finished = True;
477 return;
478 }
479
480 SAFE_FREE(state->request.extra_data.data);
481 SAFE_FREE(state->response.extra_data.data);
482
483 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
484 request_len_recv, state);
485}
486
487static void response_main_sent(void *private_data, BOOL success)
488{
489 struct winbindd_cli_state *state =
490 talloc_get_type_abort(private_data, struct winbindd_cli_state);
491
492 if (!success) {
493 state->finished = True;
494 return;
495 }
496
497 if (state->response.length == sizeof(state->response)) {
498 if (state->mem_ctx != NULL) {
499 talloc_destroy(state->mem_ctx);
500 state->mem_ctx = NULL;
501 }
502
503 setup_async_read(&state->fd_event, &state->request,
504 sizeof(uint32), request_len_recv, state);
505 return;
506 }
507
508 setup_async_write(&state->fd_event, state->response.extra_data.data,
509 state->response.length - sizeof(state->response),
510 response_extra_sent, state);
511}
512
513static void request_finished(struct winbindd_cli_state *state)
514{
515 setup_async_write(&state->fd_event, &state->response,
516 sizeof(state->response), response_main_sent, state);
517}
518
519void request_error(struct winbindd_cli_state *state)
520{
521 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
522 state->response.result = WINBINDD_ERROR;
523 request_finished(state);
524}
525
526void request_ok(struct winbindd_cli_state *state)
527{
528 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
529 state->response.result = WINBINDD_OK;
530 request_finished(state);
531}
532
533void request_finished_cont(void *private_data, BOOL success)
534{
535 struct winbindd_cli_state *state =
536 talloc_get_type_abort(private_data, struct winbindd_cli_state);
537
538 if (success)
539 request_ok(state);
540 else
541 request_error(state);
542}
543
544static void request_len_recv(void *private_data, BOOL success)
545{
546 struct winbindd_cli_state *state =
547 talloc_get_type_abort(private_data, struct winbindd_cli_state);
548
549 if (!success) {
550 state->finished = True;
551 return;
552 }
553
554 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
555 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %d)\n",
556 *(uint32 *)(&state->request), (uint32)sizeof(state->request)));
557 state->finished = True;
558 return;
559 }
560
561 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
562 sizeof(state->request) - sizeof(uint32),
563 request_main_recv, state);
564}
565
566static void request_main_recv(void *private_data, BOOL success)
567{
568 struct winbindd_cli_state *state =
569 talloc_get_type_abort(private_data, struct winbindd_cli_state);
570
571 if (!success) {
572 state->finished = True;
573 return;
574 }
575
576 if (state->request.extra_len == 0) {
577 state->request.extra_data.data = NULL;
578 request_recv(state, True);
579 return;
580 }
581
582 if ((!state->privileged) &&
583 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
584 DEBUG(3, ("Got request with %d bytes extra data on "
585 "unprivileged socket\n", (int)state->request.extra_len));
586 state->request.extra_data.data = NULL;
587 state->finished = True;
588 return;
589 }
590
591 state->request.extra_data.data =
592 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
593
594 if (state->request.extra_data.data == NULL) {
595 DEBUG(0, ("malloc failed\n"));
596 state->finished = True;
597 return;
598 }
599
600 /* Ensure null termination */
601 state->request.extra_data.data[state->request.extra_len] = '\0';
602
603 setup_async_read(&state->fd_event, state->request.extra_data.data,
604 state->request.extra_len, request_recv, state);
605}
606
607static void request_recv(void *private_data, BOOL success)
608{
609 struct winbindd_cli_state *state =
610 talloc_get_type_abort(private_data, struct winbindd_cli_state);
611
612 if (!success) {
613 state->finished = True;
614 return;
615 }
616
617 process_request(state);
618}
619
620/* Process a new connection by adding it to the client connection list */
621
622static void new_connection(int listen_sock, BOOL privileged)
623{
624 struct sockaddr_un sunaddr;
625 struct winbindd_cli_state *state;
626 socklen_t len;
627 int sock;
628
629 /* Accept connection */
630
631 len = sizeof(sunaddr);
632
633 do {
634 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
635 } while (sock == -1 && errno == EINTR);
636
637 if (sock == -1)
638 return;
639
640 DEBUG(6,("accepted socket %d\n", sock));
641
642 /* Create new connection structure */
643
644 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
645 close(sock);
646 return;
647 }
648
649 state->sock = sock;
650
651 state->last_access = time(NULL);
652
653 state->privileged = privileged;
654
655 state->fd_event.fd = state->sock;
656 state->fd_event.flags = 0;
657 add_fd_event(&state->fd_event);
658
659 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
660 request_len_recv, state);
661
662 /* Add to connection list */
663
664 winbindd_add_client(state);
665}
666
667/* Remove a client connection from client connection list */
668
669static void remove_client(struct winbindd_cli_state *state)
670{
671 /* It's a dead client - hold a funeral */
672
673 if (state == NULL) {
674 return;
675 }
676
677 /* Close socket */
678
679 close(state->sock);
680
681 /* Free any getent state */
682
683 free_getent_state(state->getpwent_state);
684 free_getent_state(state->getgrent_state);
685
686 /* We may have some extra data that was not freed if the client was
687 killed unexpectedly */
688
689 SAFE_FREE(state->response.extra_data.data);
690
691 if (state->mem_ctx != NULL) {
692 talloc_destroy(state->mem_ctx);
693 state->mem_ctx = NULL;
694 }
695
696 remove_fd_event(&state->fd_event);
697
698 /* Remove from list and free */
699
700 winbindd_remove_client(state);
701 TALLOC_FREE(state);
702}
703
704/* Shutdown client connection which has been idle for the longest time */
705
706static BOOL remove_idle_client(void)
707{
708 struct winbindd_cli_state *state, *remove_state = NULL;
709 time_t last_access = 0;
710 int nidle = 0;
711
712 for (state = winbindd_client_list(); state; state = state->next) {
713 if (state->response.result != WINBINDD_PENDING &&
714 !state->getpwent_state && !state->getgrent_state) {
715 nidle++;
716 if (!last_access || state->last_access < last_access) {
717 last_access = state->last_access;
718 remove_state = state;
719 }
720 }
721 }
722
723 if (remove_state) {
724 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
725 nidle, remove_state->sock, (unsigned int)remove_state->pid));
726 remove_client(remove_state);
727 return True;
728 }
729
730 return False;
731}
732
733/* check if HUP has been received and reload files */
734void winbind_check_sighup(const char *logfile)
735{
736 if (do_sighup) {
737
738 DEBUG(3, ("got SIGHUP\n"));
739
740 msg_reload_services(MSG_SMB_CONF_UPDATED, pid_to_procid(0), NULL, 0, logfile);
741 do_sighup = False;
742 }
743}
744
745/* check if TERM has been received */
746void winbind_check_sigterm(bool in_parent)
747{
748 if (do_sigterm)
749 terminate(in_parent);
750}
751
752/* Process incoming clients on listen_sock. We use a tricky non-blocking,
753 non-forking, non-threaded model which allows us to handle many
754 simultaneous connections while remaining impervious to many denial of
755 service attacks. */
756
757static void process_loop(void)
758{
759 struct winbindd_cli_state *state;
760 struct fd_event *ev;
761 fd_set r_fds, w_fds;
762 int maxfd, listen_sock, listen_priv_sock, selret;
763 struct timeval timeout, ev_timeout;
764
765 /* Open Sockets here to get stuff going ASAP */
766 listen_sock = open_winbindd_socket();
767 listen_priv_sock = open_winbindd_priv_socket();
768
769 if (listen_sock == -1 || listen_priv_sock == -1) {
770 perror("open_winbind_socket");
771 exit(1);
772 }
773
774 /* We'll be doing this a lot */
775
776 /* Handle messages */
777
778 message_dispatch();
779
780 run_events(winbind_event_context(), 0, NULL, NULL);
781
782 /* refresh the trusted domain cache */
783
784 rescan_trusted_domains();
785
786 /* Free up temporary memory */
787
788 lp_TALLOC_FREE();
789 main_loop_TALLOC_FREE();
790
791 /* Initialise fd lists for select() */
792
793 maxfd = MAX(listen_sock, listen_priv_sock);
794
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);
799
800 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
801 timeout.tv_usec = 0;
802
803 /* Check for any event timeouts. */
804 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
805 timeout = timeval_min(&timeout, &ev_timeout);
806 }
807
808 /* Set up client readers and writers */
809
810 state = winbindd_client_list();
811
812 while (state) {
813
814 struct winbindd_cli_state *next = state->next;
815
816 /* Dispose of client connection if it is marked as
817 finished */
818
819 if (state->finished)
820 remove_client(state);
821
822 state = next;
823 }
824
825 for (ev = fd_events; ev; ev = ev->next) {
826 if (ev->flags & EVENT_FD_READ) {
827 FD_SET(ev->fd, &r_fds);
828 maxfd = MAX(ev->fd, maxfd);
829 }
830 if (ev->flags & EVENT_FD_WRITE) {
831 FD_SET(ev->fd, &w_fds);
832 maxfd = MAX(ev->fd, maxfd);
833 }
834 }
835
836 /* Call select */
837
838 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
839
840 if (selret == 0) {
841 goto no_fds_ready;
842 }
843
844 if (selret == -1) {
845 if (errno == EINTR) {
846 goto no_fds_ready;
847 }
848
849 /* Select error, something is badly wrong */
850
851 perror("select");
852 exit(1);
853 }
854
855 /* selret > 0 */
856
857 ev = fd_events;
858 while (ev != NULL) {
859 struct fd_event *next = ev->next;
860 int flags = 0;
861 if (FD_ISSET(ev->fd, &r_fds))
862 flags |= EVENT_FD_READ;
863 if (FD_ISSET(ev->fd, &w_fds))
864 flags |= EVENT_FD_WRITE;
865 if (flags)
866 ev->handler(ev, flags);
867 ev = next;
868 }
869
870 if (FD_ISSET(listen_sock, &r_fds)) {
871 while (winbindd_num_clients() >
872 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
873 DEBUG(5,("winbindd: Exceeding %d client "
874 "connections, removing idle "
875 "connection.\n",
876 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
877 if (!remove_idle_client()) {
878 DEBUG(0,("winbindd: Exceeding %d "
879 "client connections, no idle "
880 "connection found\n",
881 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
882 break;
883 }
884 }
885 /* new, non-privileged connection */
886 new_connection(listen_sock, False);
887 }
888
889 if (FD_ISSET(listen_priv_sock, &r_fds)) {
890 while (winbindd_num_clients() >
891 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
892 DEBUG(5,("winbindd: Exceeding %d client "
893 "connections, removing idle "
894 "connection.\n",
895 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
896 if (!remove_idle_client()) {
897 DEBUG(0,("winbindd: Exceeding %d "
898 "client connections, no idle "
899 "connection found\n",
900 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
901 break;
902 }
903 }
904 /* new, privileged connection */
905 new_connection(listen_priv_sock, True);
906 }
907
908 no_fds_ready:
909
910#if 0
911 winbindd_check_cache_size(time(NULL));
912#endif
913
914 /* Check signal handling things */
915
916 winbind_check_sigterm(true);
917 winbind_check_sighup(NULL);
918
919 if (do_sigusr2) {
920 print_winbindd_status();
921 do_sigusr2 = False;
922 }
923
924 if (do_sigchld) {
925 pid_t pid;
926
927 do_sigchld = False;
928
929 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
930 winbind_child_died(pid);
931 }
932 }
933}
934
935/* Main function */
936
937int main(int argc, char **argv, char **envp)
938{
939 pstring logfile;
940 static BOOL Fork = True;
941 static BOOL log_stdout = False;
942 static BOOL no_process_group = 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 { "no-process-group", 0, POPT_ARG_VAL, &no_process_group, True, "Don't create a new process group" },
948 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
949 { "no-caching", 'n', POPT_ARG_VAL, &opt_nocache, True, "Disable caching" },
950 POPT_COMMON_SAMBA
951 POPT_TABLEEND
952 };
953 poptContext pc;
954 int opt;
955
956 /* glibc (?) likes to print "User defined signal 1" and exit if a
957 SIGUSR[12] is received before a handler is installed */
958
959 CatchSignal(SIGUSR1, SIG_IGN);
960 CatchSignal(SIGUSR2, SIG_IGN);
961
962 fault_setup((void (*)(void *))fault_quit );
963 dump_core_setup("winbindd");
964
965 load_case_tables();
966
967 /* Initialise for running in non-root mode */
968
969 sec_init();
970
971 set_remote_machine_name("winbindd", False);
972
973 /* Set environment variable so we don't recursively call ourselves.
974 This may also be useful interactively. */
975
976 if ( !winbind_off() ) {
977 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
978 exit(1);
979 }
980
981 /* Initialise samba/rpc client stuff */
982
983 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options,
984 POPT_CONTEXT_KEEP_FIRST);
985
986 while ((opt = poptGetNextOpt(pc)) != -1) {
987 switch (opt) {
988 /* Don't become a daemon */
989 case 'i':
990 interactive = True;
991 log_stdout = True;
992 Fork = False;
993 break;
994 }
995 }
996
997
998 if (log_stdout && Fork) {
999 printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
1000 poptPrintUsage(pc, stderr, 0);
1001 exit(1);
1002 }
1003
1004 if (!override_logfile) {
1005 pstr_sprintf(logfile, "%s/log.winbindd", dyn_LOGFILEBASE);
1006 lp_set_logfile(logfile);
1007 }
1008 setup_logging("winbindd", log_stdout);
1009 reopen_logs();
1010
1011 DEBUG(1, ("winbindd version %s started.\n%s\n",
1012 SAMBA_VERSION_STRING,
1013 COPYRIGHT_STARTUP_MESSAGE) );
1014
1015 if (!reload_services_file(NULL)) {
1016 DEBUG(0, ("error opening config file\n"));
1017 exit(1);
1018 }
1019
1020 if (!directory_exist(lp_lockdir(), NULL)) {
1021 mkdir(lp_lockdir(), 0755);
1022 }
1023
1024 /* Setup names. */
1025
1026 if (!init_names())
1027 exit(1);
1028
1029 load_interfaces();
1030
1031 if (!secrets_init()) {
1032
1033 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1034 return False;
1035 }
1036
1037 /* Enable netbios namecache */
1038
1039 namecache_enable();
1040
1041 /* Winbind daemon initialisation */
1042
1043 if ( ! NT_STATUS_IS_OK(idmap_init_cache()) ) {
1044 DEBUG(1, ("Could not init idmap cache!\n"));
1045 }
1046
1047 /* Unblock all signals we are interested in as they may have been
1048 blocked by the parent process. */
1049
1050 BlockSignals(False, SIGINT);
1051 BlockSignals(False, SIGQUIT);
1052 BlockSignals(False, SIGTERM);
1053 BlockSignals(False, SIGUSR1);
1054 BlockSignals(False, SIGUSR2);
1055 BlockSignals(False, SIGHUP);
1056 BlockSignals(False, SIGCHLD);
1057
1058 /* Setup signal handlers */
1059
1060 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
1061 CatchSignal(SIGQUIT, termination_handler);
1062 CatchSignal(SIGTERM, termination_handler);
1063 CatchSignal(SIGCHLD, sigchld_handler);
1064
1065 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1066
1067 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
1068 CatchSignal(SIGHUP, sighup_handler);
1069
1070 if (!interactive)
1071 become_daemon(Fork, no_process_group);
1072
1073 pidfile_create("winbindd");
1074
1075#if HAVE_SETPGID
1076 /*
1077 * If we're interactive we want to set our own process group for
1078 * signal management.
1079 */
1080 if (interactive && !no_process_group)
1081 setpgid( (pid_t)0, (pid_t)0);
1082#endif
1083
1084 TimeInit();
1085
1086 /* Initialise messaging system */
1087
1088 if (!message_init()) {
1089 DEBUG(0, ("unable to initialize messaging system\n"));
1090 exit(1);
1091 }
1092
1093 /* Initialize cache (ensure version is correct). */
1094 if (!initialize_winbindd_cache()) {
1095 exit(1);
1096 }
1097
1098 /* React on 'smbcontrol winbindd reload-config' in the same way
1099 as to SIGHUP signal */
1100 message_register(MSG_SMB_CONF_UPDATED, msg_reload_services, NULL);
1101 message_register(MSG_SHUTDOWN, msg_shutdown, NULL);
1102
1103 /* Handle online/offline messages. */
1104 message_register(MSG_WINBIND_OFFLINE, winbind_msg_offline, NULL);
1105 message_register(MSG_WINBIND_ONLINE, winbind_msg_online, NULL);
1106 message_register(MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus,
1107 NULL);
1108
1109 poptFreeContext(pc);
1110
1111 netsamlogon_cache_init(); /* Non-critical */
1112
1113 if (!init_domain_list()) {
1114 DEBUG(0,("unable to initalize domain list\n"));
1115 exit(1);
1116 }
1117
1118 init_idmap_child();
1119
1120 smb_nscd_flush_user_cache();
1121 smb_nscd_flush_group_cache();
1122
1123 /* Loop waiting for requests */
1124
1125 while (1)
1126 process_loop();
1127
1128 return 0;
1129}
Note: See TracBrowser for help on using the repository browser.