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

Last change on this file since 293 was 165, checked in by Paul Smedley, 16 years ago

Add 'missing' 3.0.34 diffs

File size: 27.5 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->response.extra_data.data);
481
482 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
483 request_len_recv, state);
484}
485
486static void response_main_sent(void *private_data, BOOL success)
487{
488 struct winbindd_cli_state *state =
489 talloc_get_type_abort(private_data, struct winbindd_cli_state);
490
491 if (!success) {
492 state->finished = True;
493 return;
494 }
495
496 if (state->response.length == sizeof(state->response)) {
497 if (state->mem_ctx != NULL) {
498 talloc_destroy(state->mem_ctx);
499 state->mem_ctx = NULL;
500 }
501
502 setup_async_read(&state->fd_event, &state->request,
503 sizeof(uint32), request_len_recv, state);
504 return;
505 }
506
507 setup_async_write(&state->fd_event, state->response.extra_data.data,
508 state->response.length - sizeof(state->response),
509 response_extra_sent, state);
510}
511
512static void request_finished(struct winbindd_cli_state *state)
513{
514 /* Make sure request.extra_data is freed when finish processing a request */
515 SAFE_FREE(state->request.extra_data.data);
516 setup_async_write(&state->fd_event, &state->response,
517 sizeof(state->response), response_main_sent, state);
518}
519
520void request_error(struct winbindd_cli_state *state)
521{
522 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
523 state->response.result = WINBINDD_ERROR;
524 request_finished(state);
525}
526
527void request_ok(struct winbindd_cli_state *state)
528{
529 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
530 state->response.result = WINBINDD_OK;
531 request_finished(state);
532}
533
534void request_finished_cont(void *private_data, BOOL success)
535{
536 struct winbindd_cli_state *state =
537 talloc_get_type_abort(private_data, struct winbindd_cli_state);
538
539 if (success)
540 request_ok(state);
541 else
542 request_error(state);
543}
544
545static void request_len_recv(void *private_data, BOOL success)
546{
547 struct winbindd_cli_state *state =
548 talloc_get_type_abort(private_data, struct winbindd_cli_state);
549
550 if (!success) {
551 state->finished = True;
552 return;
553 }
554
555 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
556 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %d)\n",
557 *(uint32 *)(&state->request), (uint32)sizeof(state->request)));
558 state->finished = True;
559 return;
560 }
561
562 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
563 sizeof(state->request) - sizeof(uint32),
564 request_main_recv, state);
565}
566
567static void request_main_recv(void *private_data, BOOL success)
568{
569 struct winbindd_cli_state *state =
570 talloc_get_type_abort(private_data, struct winbindd_cli_state);
571
572 if (!success) {
573 state->finished = True;
574 return;
575 }
576
577 if (state->request.extra_len == 0) {
578 state->request.extra_data.data = NULL;
579 request_recv(state, True);
580 return;
581 }
582
583 if ((!state->privileged) &&
584 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
585 DEBUG(3, ("Got request with %d bytes extra data on "
586 "unprivileged socket\n", (int)state->request.extra_len));
587 state->request.extra_data.data = NULL;
588 state->finished = True;
589 return;
590 }
591
592 state->request.extra_data.data =
593 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
594
595 if (state->request.extra_data.data == NULL) {
596 DEBUG(0, ("malloc failed\n"));
597 state->finished = True;
598 return;
599 }
600
601 /* Ensure null termination */
602 state->request.extra_data.data[state->request.extra_len] = '\0';
603
604 setup_async_read(&state->fd_event, state->request.extra_data.data,
605 state->request.extra_len, request_recv, state);
606}
607
608static void request_recv(void *private_data, BOOL success)
609{
610 struct winbindd_cli_state *state =
611 talloc_get_type_abort(private_data, struct winbindd_cli_state);
612
613 if (!success) {
614 state->finished = True;
615 return;
616 }
617
618 process_request(state);
619}
620
621/* Process a new connection by adding it to the client connection list */
622
623static void new_connection(int listen_sock, BOOL privileged)
624{
625 struct sockaddr_un sunaddr;
626 struct winbindd_cli_state *state;
627 socklen_t len;
628 int sock;
629
630 /* Accept connection */
631
632 len = sizeof(sunaddr);
633
634 do {
635 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
636 } while (sock == -1 && errno == EINTR);
637
638 if (sock == -1)
639 return;
640
641 DEBUG(6,("accepted socket %d\n", sock));
642
643 /* Create new connection structure */
644
645 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
646 close(sock);
647 return;
648 }
649
650 state->sock = sock;
651
652 state->last_access = time(NULL);
653
654 state->privileged = privileged;
655
656 state->fd_event.fd = state->sock;
657 state->fd_event.flags = 0;
658 add_fd_event(&state->fd_event);
659
660 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
661 request_len_recv, state);
662
663 /* Add to connection list */
664
665 winbindd_add_client(state);
666}
667
668/* Remove a client connection from client connection list */
669
670static void remove_client(struct winbindd_cli_state *state)
671{
672 char c = 0;
673
674 /* It's a dead client - hold a funeral */
675
676 if (state == NULL) {
677 return;
678 }
679
680 /* tell client, we are closing ... */
681 write(state->sock, &c, sizeof(c));
682
683 /* Close socket */
684
685 close(state->sock);
686
687 /* Free any getent state */
688
689 free_getent_state(state->getpwent_state);
690 free_getent_state(state->getgrent_state);
691
692 /* We may have some extra data that was not freed if the client was
693 killed unexpectedly */
694
695 SAFE_FREE(state->response.extra_data.data);
696
697 if (state->mem_ctx != NULL) {
698 talloc_destroy(state->mem_ctx);
699 state->mem_ctx = NULL;
700 }
701
702 remove_fd_event(&state->fd_event);
703
704 /* Remove from list and free */
705
706 winbindd_remove_client(state);
707 TALLOC_FREE(state);
708}
709
710/* Shutdown client connection which has been idle for the longest time */
711
712static BOOL remove_idle_client(void)
713{
714 struct winbindd_cli_state *state, *remove_state = NULL;
715 time_t last_access = 0;
716 int nidle = 0;
717
718 for (state = winbindd_client_list(); state; state = state->next) {
719 if (state->response.result != WINBINDD_PENDING &&
720 !state->getpwent_state && !state->getgrent_state) {
721 nidle++;
722 if (!last_access || state->last_access < last_access) {
723 last_access = state->last_access;
724 remove_state = state;
725 }
726 }
727 }
728
729 if (remove_state) {
730 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
731 nidle, remove_state->sock, (unsigned int)remove_state->pid));
732 remove_client(remove_state);
733 return True;
734 }
735
736 return False;
737}
738
739/* check if HUP has been received and reload files */
740void winbind_check_sighup(const char *logfile)
741{
742 if (do_sighup) {
743
744 DEBUG(3, ("got SIGHUP\n"));
745
746 msg_reload_services(MSG_SMB_CONF_UPDATED, pid_to_procid(0), NULL, 0, logfile);
747 do_sighup = False;
748 }
749}
750
751/* check if TERM has been received */
752void winbind_check_sigterm(bool in_parent)
753{
754 if (do_sigterm)
755 terminate(in_parent);
756}
757
758/* Process incoming clients on listen_sock. We use a tricky non-blocking,
759 non-forking, non-threaded model which allows us to handle many
760 simultaneous connections while remaining impervious to many denial of
761 service attacks. */
762
763static void process_loop(void)
764{
765 struct winbindd_cli_state *state;
766 struct fd_event *ev;
767 fd_set r_fds, w_fds;
768 int maxfd, listen_sock, listen_priv_sock, selret;
769 struct timeval timeout, ev_timeout;
770
771 /* Open Sockets here to get stuff going ASAP */
772 listen_sock = open_winbindd_socket();
773 listen_priv_sock = open_winbindd_priv_socket();
774
775 if (listen_sock == -1 || listen_priv_sock == -1) {
776 perror("open_winbind_socket");
777 exit(1);
778 }
779
780 /* We'll be doing this a lot */
781
782 /* Handle messages */
783
784 message_dispatch();
785
786 run_events(winbind_event_context(), 0, NULL, NULL);
787
788 /* refresh the trusted domain cache */
789
790 rescan_trusted_domains();
791
792 /* Free up temporary memory */
793
794 lp_TALLOC_FREE();
795 main_loop_TALLOC_FREE();
796
797 /* Initialise fd lists for select() */
798
799 maxfd = MAX(listen_sock, listen_priv_sock);
800
801 FD_ZERO(&r_fds);
802 FD_ZERO(&w_fds);
803 FD_SET(listen_sock, &r_fds);
804 FD_SET(listen_priv_sock, &r_fds);
805
806 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
807 timeout.tv_usec = 0;
808
809 /* Check for any event timeouts. */
810 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
811 timeout = timeval_min(&timeout, &ev_timeout);
812 }
813
814 /* Set up client readers and writers */
815
816 state = winbindd_client_list();
817
818 while (state) {
819
820 struct winbindd_cli_state *next = state->next;
821
822 /* Dispose of client connection if it is marked as
823 finished */
824
825 if (state->finished)
826 remove_client(state);
827
828 state = next;
829 }
830
831 for (ev = fd_events; ev; ev = ev->next) {
832 if (ev->flags & EVENT_FD_READ) {
833 FD_SET(ev->fd, &r_fds);
834 maxfd = MAX(ev->fd, maxfd);
835 }
836 if (ev->flags & EVENT_FD_WRITE) {
837 FD_SET(ev->fd, &w_fds);
838 maxfd = MAX(ev->fd, maxfd);
839 }
840 }
841
842 /* Call select */
843
844 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
845
846 if (selret == 0) {
847 goto no_fds_ready;
848 }
849
850 if (selret == -1) {
851 if (errno == EINTR) {
852 goto no_fds_ready;
853 }
854
855 /* Select error, something is badly wrong */
856
857 perror("select");
858 exit(1);
859 }
860
861 /* selret > 0 */
862
863 ev = fd_events;
864 while (ev != NULL) {
865 struct fd_event *next = ev->next;
866 int flags = 0;
867 if (FD_ISSET(ev->fd, &r_fds))
868 flags |= EVENT_FD_READ;
869 if (FD_ISSET(ev->fd, &w_fds))
870 flags |= EVENT_FD_WRITE;
871 if (flags)
872 ev->handler(ev, flags);
873 ev = next;
874 }
875
876 if (FD_ISSET(listen_sock, &r_fds)) {
877 while (winbindd_num_clients() >
878 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
879 DEBUG(5,("winbindd: Exceeding %d client "
880 "connections, removing idle "
881 "connection.\n",
882 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
883 if (!remove_idle_client()) {
884 DEBUG(0,("winbindd: Exceeding %d "
885 "client connections, no idle "
886 "connection found\n",
887 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
888 break;
889 }
890 }
891 /* new, non-privileged connection */
892 new_connection(listen_sock, False);
893 }
894
895 if (FD_ISSET(listen_priv_sock, &r_fds)) {
896 while (winbindd_num_clients() >
897 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
898 DEBUG(5,("winbindd: Exceeding %d client "
899 "connections, removing idle "
900 "connection.\n",
901 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
902 if (!remove_idle_client()) {
903 DEBUG(0,("winbindd: Exceeding %d "
904 "client connections, no idle "
905 "connection found\n",
906 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
907 break;
908 }
909 }
910 /* new, privileged connection */
911 new_connection(listen_priv_sock, True);
912 }
913
914 no_fds_ready:
915
916#if 0
917 winbindd_check_cache_size(time(NULL));
918#endif
919
920 /* Check signal handling things */
921
922 winbind_check_sigterm(true);
923 winbind_check_sighup(NULL);
924
925 if (do_sigusr2) {
926 print_winbindd_status();
927 do_sigusr2 = False;
928 }
929
930 if (do_sigchld) {
931 pid_t pid;
932
933 do_sigchld = False;
934
935 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
936 winbind_child_died(pid);
937 }
938 }
939}
940
941/* Main function */
942
943int main(int argc, char **argv, char **envp)
944{
945 pstring logfile;
946 static BOOL Fork = True;
947 static BOOL log_stdout = False;
948 static BOOL no_process_group = False;
949 struct poptOption long_options[] = {
950 POPT_AUTOHELP
951 { "stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
952 { "foreground", 'F', POPT_ARG_VAL, &Fork, False, "Daemon in foreground mode" },
953 { "no-process-group", 0, POPT_ARG_VAL, &no_process_group, True, "Don't create a new process group" },
954 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
955 { "no-caching", 'n', POPT_ARG_VAL, &opt_nocache, True, "Disable caching" },
956 POPT_COMMON_SAMBA
957 POPT_TABLEEND
958 };
959 poptContext pc;
960 int opt;
961
962 /* glibc (?) likes to print "User defined signal 1" and exit if a
963 SIGUSR[12] is received before a handler is installed */
964
965 CatchSignal(SIGUSR1, SIG_IGN);
966 CatchSignal(SIGUSR2, SIG_IGN);
967
968 fault_setup((void (*)(void *))fault_quit );
969 dump_core_setup("winbindd");
970
971 load_case_tables();
972
973 /* Initialise for running in non-root mode */
974
975 sec_init();
976
977 set_remote_machine_name("winbindd", False);
978
979 /* Set environment variable so we don't recursively call ourselves.
980 This may also be useful interactively. */
981
982 if ( !winbind_off() ) {
983 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
984 exit(1);
985 }
986
987 /* Initialise samba/rpc client stuff */
988
989 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options,
990 POPT_CONTEXT_KEEP_FIRST);
991
992 while ((opt = poptGetNextOpt(pc)) != -1) {
993 switch (opt) {
994 /* Don't become a daemon */
995 case 'i':
996 interactive = True;
997 log_stdout = True;
998 Fork = False;
999 break;
1000 }
1001 }
1002
1003
1004 if (log_stdout && Fork) {
1005 printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
1006 poptPrintUsage(pc, stderr, 0);
1007 exit(1);
1008 }
1009
1010 if (!override_logfile) {
1011 pstr_sprintf(logfile, "%s/log.winbindd", dyn_LOGFILEBASE);
1012 lp_set_logfile(logfile);
1013 }
1014 setup_logging("winbindd", log_stdout);
1015 reopen_logs();
1016
1017 DEBUG(1, ("winbindd version %s started.\n%s\n",
1018 SAMBA_VERSION_STRING,
1019 COPYRIGHT_STARTUP_MESSAGE) );
1020
1021 if (!reload_services_file(NULL)) {
1022 DEBUG(0, ("error opening config file\n"));
1023 exit(1);
1024 }
1025
1026 if (!directory_exist(lp_lockdir(), NULL)) {
1027 mkdir(lp_lockdir(), 0755);
1028 }
1029
1030 /* Setup names. */
1031
1032 if (!init_names())
1033 exit(1);
1034
1035 load_interfaces();
1036
1037 if (!secrets_init()) {
1038
1039 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1040 return False;
1041 }
1042
1043 /* Enable netbios namecache */
1044
1045 namecache_enable();
1046
1047 /* Winbind daemon initialisation */
1048
1049 if ( ! NT_STATUS_IS_OK(idmap_init_cache()) ) {
1050 DEBUG(1, ("Could not init idmap cache!\n"));
1051 }
1052
1053 /* Unblock all signals we are interested in as they may have been
1054 blocked by the parent process. */
1055
1056 BlockSignals(False, SIGINT);
1057 BlockSignals(False, SIGQUIT);
1058 BlockSignals(False, SIGTERM);
1059 BlockSignals(False, SIGUSR1);
1060 BlockSignals(False, SIGUSR2);
1061 BlockSignals(False, SIGHUP);
1062 BlockSignals(False, SIGCHLD);
1063
1064 /* Setup signal handlers */
1065
1066 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
1067 CatchSignal(SIGQUIT, termination_handler);
1068 CatchSignal(SIGTERM, termination_handler);
1069 CatchSignal(SIGCHLD, sigchld_handler);
1070
1071 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1072
1073 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
1074 CatchSignal(SIGHUP, sighup_handler);
1075
1076 if (!interactive)
1077 become_daemon(Fork, no_process_group);
1078
1079 pidfile_create("winbindd");
1080
1081#if HAVE_SETPGID
1082 /*
1083 * If we're interactive we want to set our own process group for
1084 * signal management.
1085 */
1086 if (interactive && !no_process_group)
1087 setpgid( (pid_t)0, (pid_t)0);
1088#endif
1089
1090 TimeInit();
1091
1092 /* Initialise messaging system */
1093
1094 if (!message_init()) {
1095 DEBUG(0, ("unable to initialize messaging system\n"));
1096 exit(1);
1097 }
1098
1099 /* Initialize cache (ensure version is correct). */
1100 if (!initialize_winbindd_cache()) {
1101 exit(1);
1102 }
1103
1104 /* React on 'smbcontrol winbindd reload-config' in the same way
1105 as to SIGHUP signal */
1106 message_register(MSG_SMB_CONF_UPDATED, msg_reload_services, NULL);
1107 message_register(MSG_SHUTDOWN, msg_shutdown, NULL);
1108
1109 /* Handle online/offline messages. */
1110 message_register(MSG_WINBIND_OFFLINE, winbind_msg_offline, NULL);
1111 message_register(MSG_WINBIND_ONLINE, winbind_msg_online, NULL);
1112 message_register(MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus,
1113 NULL);
1114
1115 poptFreeContext(pc);
1116
1117 netsamlogon_cache_init(); /* Non-critical */
1118
1119 if (!init_domain_list()) {
1120 DEBUG(0,("unable to initalize domain list\n"));
1121 exit(1);
1122 }
1123
1124 init_idmap_child();
1125
1126 smb_nscd_flush_user_cache();
1127 smb_nscd_flush_group_cache();
1128
1129 /* Loop waiting for requests */
1130
1131 while (1)
1132 process_loop();
1133
1134 return 0;
1135}
Note: See TracBrowser for help on using the repository browser.