~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source3/winbindd/winbindd.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 3 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, see <http://www.gnu.org/licenses/>.
 
23
*/
 
24
 
 
25
#include "includes.h"
 
26
#include "winbindd.h"
 
27
 
 
28
#undef DBGC_CLASS
 
29
#define DBGC_CLASS DBGC_WINBIND
 
30
 
 
31
static bool opt_nocache = False;
 
32
static bool interactive = False;
 
33
 
 
34
extern bool override_logfile;
 
35
 
 
36
struct event_context *winbind_event_context(void)
 
37
{
 
38
        static struct event_context *ctx;
 
39
 
 
40
        if (!ctx && !(ctx = event_context_init(NULL))) {
 
41
                smb_panic("Could not init winbind event context");
 
42
        }
 
43
        return ctx;
 
44
}
 
45
 
 
46
struct messaging_context *winbind_messaging_context(void)
 
47
{
 
48
        static struct messaging_context *ctx;
 
49
 
 
50
        if (ctx == NULL) {
 
51
                ctx = messaging_init(NULL, server_id_self(),
 
52
                                     winbind_event_context());
 
53
        }
 
54
        if (ctx == NULL) {
 
55
                DEBUG(0, ("Could not init winbind messaging context.\n"));
 
56
        }
 
57
        return ctx;
 
58
}
 
59
 
 
60
/* Reload configuration */
 
61
 
 
62
static bool reload_services_file(const char *lfile)
 
63
{
 
64
        bool ret;
 
65
 
 
66
        if (lp_loaded()) {
 
67
                const char *fname = lp_configfile();
 
68
 
 
69
                if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
 
70
                        set_dyn_CONFIGFILE(fname);
 
71
                }
 
72
        }
 
73
 
 
74
        /* if this is a child, restore the logfile to the special
 
75
           name - <domain>, idmap, etc. */
 
76
        if (lfile && *lfile) {
 
77
                lp_set_logfile(lfile);
 
78
        }
 
79
 
 
80
        reopen_logs();
 
81
        ret = lp_load(get_dyn_CONFIGFILE(),False,False,True,True);
 
82
 
 
83
        reopen_logs();
 
84
        load_interfaces();
 
85
 
 
86
        return(ret);
 
87
}
 
88
 
 
89
 
 
90
/**************************************************************************** **
 
91
 Handle a fault..
 
92
 **************************************************************************** */
 
93
 
 
94
static void fault_quit(void)
 
95
{
 
96
        dump_core();
 
97
}
 
98
 
 
99
static void winbindd_status(void)
 
100
{
 
101
        struct winbindd_cli_state *tmp;
 
102
 
 
103
        DEBUG(0, ("winbindd status:\n"));
 
104
 
 
105
        /* Print client state information */
 
106
 
 
107
        DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
 
108
 
 
109
        if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
 
110
                DEBUG(2, ("\tclient list:\n"));
 
111
                for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
 
112
                        DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
 
113
                                  (unsigned long)tmp->pid, tmp->sock));
 
114
                }
 
115
        }
 
116
}
 
117
 
 
118
/* Print winbindd status to log file */
 
119
 
 
120
static void print_winbindd_status(void)
 
121
{
 
122
        winbindd_status();
 
123
}
 
124
 
 
125
/* Flush client cache */
 
126
 
 
127
static void flush_caches(void)
 
128
{
 
129
        /* We need to invalidate cached user list entries on a SIGHUP 
 
130
           otherwise cached access denied errors due to restrict anonymous
 
131
           hang around until the sequence number changes. */
 
132
 
 
133
        if (!wcache_invalidate_cache()) {
 
134
                DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
 
135
                if (!winbindd_cache_validate_and_initialize()) {
 
136
                        exit(1);
 
137
                }
 
138
        }
 
139
}
 
140
 
 
141
/* Handle the signal by unlinking socket and exiting */
 
142
 
 
143
static void terminate(bool is_parent)
 
144
{
 
145
        if (is_parent) {
 
146
                /* When parent goes away we should
 
147
                 * remove the socket file. Not so
 
148
                 * when children terminate.
 
149
                 */ 
 
150
                char *path = NULL;
 
151
 
 
152
                if (asprintf(&path, "%s/%s",
 
153
                        get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME) > 0) {
 
154
                        unlink(path);
 
155
                        SAFE_FREE(path);
 
156
                }
 
157
        }
 
158
 
 
159
        idmap_close();
 
160
 
 
161
        trustdom_cache_shutdown();
 
162
 
 
163
#if 0
 
164
        if (interactive) {
 
165
                TALLOC_CTX *mem_ctx = talloc_init("end_description");
 
166
                char *description = talloc_describe_all(mem_ctx);
 
167
 
 
168
                DEBUG(3, ("tallocs left:\n%s\n", description));
 
169
                talloc_destroy(mem_ctx);
 
170
        }
 
171
#endif
 
172
 
 
173
        if (is_parent) {
 
174
                pidfile_unlink();
 
175
        }
 
176
 
 
177
        exit(0);
 
178
}
 
179
 
 
180
static void winbindd_sig_term_handler(struct tevent_context *ev,
 
181
                                      struct tevent_signal *se,
 
182
                                      int signum,
 
183
                                      int count,
 
184
                                      void *siginfo,
 
185
                                      void *private_data)
 
186
{
 
187
        bool *is_parent = talloc_get_type_abort(private_data, bool);
 
188
 
 
189
        DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
 
190
                 signum, (int)*is_parent));
 
191
        terminate(*is_parent);
 
192
}
 
193
 
 
194
bool winbindd_setup_sig_term_handler(bool parent)
 
195
{
 
196
        struct tevent_signal *se;
 
197
        bool *is_parent;
 
198
 
 
199
        is_parent = talloc(winbind_event_context(), bool);
 
200
        if (!is_parent) {
 
201
                return false;
 
202
        }
 
203
 
 
204
        *is_parent = parent;
 
205
 
 
206
        se = tevent_add_signal(winbind_event_context(),
 
207
                               is_parent,
 
208
                               SIGTERM, 0,
 
209
                               winbindd_sig_term_handler,
 
210
                               is_parent);
 
211
        if (!se) {
 
212
                DEBUG(0,("failed to setup SIGTERM handler"));
 
213
                talloc_free(is_parent);
 
214
                return false;
 
215
        }
 
216
 
 
217
        se = tevent_add_signal(winbind_event_context(),
 
218
                               is_parent,
 
219
                               SIGINT, 0,
 
220
                               winbindd_sig_term_handler,
 
221
                               is_parent);
 
222
        if (!se) {
 
223
                DEBUG(0,("failed to setup SIGINT handler"));
 
224
                talloc_free(is_parent);
 
225
                return false;
 
226
        }
 
227
 
 
228
        se = tevent_add_signal(winbind_event_context(),
 
229
                               is_parent,
 
230
                               SIGQUIT, 0,
 
231
                               winbindd_sig_term_handler,
 
232
                               is_parent);
 
233
        if (!se) {
 
234
                DEBUG(0,("failed to setup SIGINT handler"));
 
235
                talloc_free(is_parent);
 
236
                return false;
 
237
        }
 
238
 
 
239
        return true;
 
240
}
 
241
 
 
242
static void winbindd_sig_hup_handler(struct tevent_context *ev,
 
243
                                     struct tevent_signal *se,
 
244
                                     int signum,
 
245
                                     int count,
 
246
                                     void *siginfo,
 
247
                                     void *private_data)
 
248
{
 
249
        const char *file = (const char *)private_data;
 
250
 
 
251
        DEBUG(1,("Reloading services after SIGHUP\n"));
 
252
        flush_caches();
 
253
        reload_services_file(file);
 
254
}
 
255
 
 
256
bool winbindd_setup_sig_hup_handler(const char *lfile)
 
257
{
 
258
        struct tevent_signal *se;
 
259
        char *file = NULL;
 
260
 
 
261
        if (lfile) {
 
262
                file = talloc_strdup(winbind_event_context(),
 
263
                                     lfile);
 
264
                if (!file) {
 
265
                        return false;
 
266
                }
 
267
        }
 
268
 
 
269
        se = tevent_add_signal(winbind_event_context(),
 
270
                               winbind_event_context(),
 
271
                               SIGHUP, 0,
 
272
                               winbindd_sig_hup_handler,
 
273
                               file);
 
274
        if (!se) {
 
275
                return false;
 
276
        }
 
277
 
 
278
        return true;
 
279
}
 
280
 
 
281
static void winbindd_sig_chld_handler(struct tevent_context *ev,
 
282
                                      struct tevent_signal *se,
 
283
                                      int signum,
 
284
                                      int count,
 
285
                                      void *siginfo,
 
286
                                      void *private_data)
 
287
{
 
288
        pid_t pid;
 
289
 
 
290
        while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
 
291
                winbind_child_died(pid);
 
292
        }
 
293
}
 
294
 
 
295
static bool winbindd_setup_sig_chld_handler(void)
 
296
{
 
297
        struct tevent_signal *se;
 
298
 
 
299
        se = tevent_add_signal(winbind_event_context(),
 
300
                               winbind_event_context(),
 
301
                               SIGCHLD, 0,
 
302
                               winbindd_sig_chld_handler,
 
303
                               NULL);
 
304
        if (!se) {
 
305
                return false;
 
306
        }
 
307
 
 
308
        return true;
 
309
}
 
310
 
 
311
static void winbindd_sig_usr2_handler(struct tevent_context *ev,
 
312
                                      struct tevent_signal *se,
 
313
                                      int signum,
 
314
                                      int count,
 
315
                                      void *siginfo,
 
316
                                      void *private_data)
 
317
{
 
318
        print_winbindd_status();
 
319
}
 
320
 
 
321
static bool winbindd_setup_sig_usr2_handler(void)
 
322
{
 
323
        struct tevent_signal *se;
 
324
 
 
325
        se = tevent_add_signal(winbind_event_context(),
 
326
                               winbind_event_context(),
 
327
                               SIGUSR2, 0,
 
328
                               winbindd_sig_usr2_handler,
 
329
                               NULL);
 
330
        if (!se) {
 
331
                return false;
 
332
        }
 
333
 
 
334
        return true;
 
335
}
 
336
 
 
337
/* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
 
338
static void msg_reload_services(struct messaging_context *msg,
 
339
                                void *private_data,
 
340
                                uint32_t msg_type,
 
341
                                struct server_id server_id,
 
342
                                DATA_BLOB *data)
 
343
{
 
344
        /* Flush various caches */
 
345
        flush_caches();
 
346
        reload_services_file((const char *) private_data);
 
347
}
 
348
 
 
349
/* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
 
350
static void msg_shutdown(struct messaging_context *msg,
 
351
                         void *private_data,
 
352
                         uint32_t msg_type,
 
353
                         struct server_id server_id,
 
354
                         DATA_BLOB *data)
 
355
{
 
356
        /* only the parent waits for this message */
 
357
        DEBUG(0,("Got shutdown message\n"));
 
358
        terminate(true);
 
359
}
 
360
 
 
361
 
 
362
static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
 
363
                                       void *private_data,
 
364
                                       uint32_t msg_type,
 
365
                                       struct server_id server_id,
 
366
                                       DATA_BLOB *data)
 
367
{
 
368
        uint8 ret;
 
369
        pid_t child_pid;
 
370
 
 
371
        DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
 
372
                   "message.\n"));
 
373
 
 
374
        /*
 
375
         * call the validation code from a child:
 
376
         * so we don't block the main winbindd and the validation
 
377
         * code can safely use fork/waitpid...
 
378
         */
 
379
        child_pid = sys_fork();
 
380
 
 
381
        if (child_pid == -1) {
 
382
                DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
 
383
                          strerror(errno)));
 
384
                return;
 
385
        }
 
386
 
 
387
        if (child_pid != 0) {
 
388
                /* parent */
 
389
                DEBUG(5, ("winbind_msg_validate_cache: child created with "
 
390
                          "pid %d.\n", (int)child_pid));
 
391
                return;
 
392
        }
 
393
 
 
394
        /* child */
 
395
 
 
396
        if (!winbindd_reinit_after_fork(NULL)) {
 
397
                _exit(0);
 
398
        }
 
399
 
 
400
        /* install default SIGCHLD handler: validation code uses fork/waitpid */
 
401
        CatchSignal(SIGCHLD, SIG_DFL);
 
402
 
 
403
        ret = (uint8)winbindd_validate_cache_nobackup();
 
404
        DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
 
405
        messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
 
406
                           (size_t)1);
 
407
        _exit(0);
 
408
}
 
409
 
 
410
static struct winbindd_dispatch_table {
 
411
        enum winbindd_cmd cmd;
 
412
        void (*fn)(struct winbindd_cli_state *state);
 
413
        const char *winbindd_cmd_name;
 
414
} dispatch_table[] = {
 
415
 
 
416
        /* User functions */
 
417
 
 
418
        { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
 
419
        { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
 
420
        { WINBINDD_GETPWSID, winbindd_getpwsid, "GETPWSID" },
 
421
 
 
422
        { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
 
423
        { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
 
424
        { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
 
425
 
 
426
        { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
 
427
        { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
 
428
        { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
 
429
          "GETUSERDOMGROUPS" },
 
430
        { WINBINDD_GETSIDALIASES, winbindd_getsidaliases,
 
431
           "LOOKUPUSERALIASES" },
 
432
 
 
433
        /* Group functions */
 
434
 
 
435
        { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
 
436
        { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
 
437
        { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
 
438
        { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
 
439
        { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
 
440
        { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
 
441
 
 
442
        /* PAM auth functions */
 
443
 
 
444
        { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
 
445
        { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
 
446
        { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
 
447
        { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
 
448
        { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
 
449
 
 
450
        /* Enumeration functions */
 
451
 
 
452
        { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
 
453
        { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
 
454
        { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
 
455
          "LIST_TRUSTDOM" },
 
456
        { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
 
457
 
 
458
        /* SID related functions */
 
459
 
 
460
        { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
 
461
        { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
 
462
        { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
 
463
 
 
464
        /* Lookup related functions */
 
465
 
 
466
        { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
 
467
        { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
 
468
        { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
 
469
        { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
 
470
        { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
 
471
        { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
 
472
        { WINBINDD_SET_MAPPING, winbindd_set_mapping, "SET_MAPPING" },
 
473
        { WINBINDD_REMOVE_MAPPING, winbindd_remove_mapping, "REMOVE_MAPPING" },
 
474
        { WINBINDD_SET_HWM, winbindd_set_hwm, "SET_HWMS" },
 
475
 
 
476
        /* Miscellaneous */
 
477
 
 
478
        { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
 
479
        { WINBINDD_PING, winbindd_ping, "PING" },
 
480
        { WINBINDD_INFO, winbindd_info, "INFO" },
 
481
        { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
 
482
          "INTERFACE_VERSION" },
 
483
        { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
 
484
        { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
 
485
        { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
 
486
        { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
 
487
          "WINBINDD_PRIV_PIPE_DIR" },
 
488
        { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
 
489
        { WINBINDD_DSGETDCNAME, winbindd_dsgetdcname, "DSGETDCNAME" },
 
490
 
 
491
        /* Credential cache access */
 
492
        { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
 
493
 
 
494
        /* WINS functions */
 
495
 
 
496
        { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
 
497
        { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
 
498
 
 
499
        /* End of list */
 
500
 
 
501
        { WINBINDD_NUM_CMDS, NULL, "NONE" }
 
502
};
 
503
 
 
504
static void process_request(struct winbindd_cli_state *state)
 
505
{
 
506
        struct winbindd_dispatch_table *table = dispatch_table;
 
507
 
 
508
        /* Free response data - we may be interrupted and receive another
 
509
           command before being able to send this data off. */
 
510
 
 
511
        SAFE_FREE(state->response.extra_data.data);  
 
512
 
 
513
        ZERO_STRUCT(state->response);
 
514
 
 
515
        state->response.result = WINBINDD_PENDING;
 
516
        state->response.length = sizeof(struct winbindd_response);
 
517
 
 
518
        state->mem_ctx = talloc_init("winbind request");
 
519
        if (state->mem_ctx == NULL)
 
520
                return;
 
521
 
 
522
        /* Remember who asked us. */
 
523
        state->pid = state->request.pid;
 
524
 
 
525
        /* Process command */
 
526
 
 
527
        for (table = dispatch_table; table->fn; table++) {
 
528
                if (state->request.cmd == table->cmd) {
 
529
                        DEBUG(10,("process_request: request fn %s\n",
 
530
                                  table->winbindd_cmd_name ));
 
531
                        table->fn(state);
 
532
                        break;
 
533
                }
 
534
        }
 
535
 
 
536
        if (!table->fn) {
 
537
                DEBUG(10,("process_request: unknown request fn number %d\n",
 
538
                          (int)state->request.cmd ));
 
539
                request_error(state);
 
540
        }
 
541
}
 
542
 
 
543
/*
 
544
 * A list of file descriptors being monitored by select in the main processing
 
545
 * loop. winbindd_fd_event->handler is called whenever the socket is readable/writable.
 
546
 */
 
547
 
 
548
static struct winbindd_fd_event *fd_events = NULL;
 
549
 
 
550
void add_fd_event(struct winbindd_fd_event *ev)
 
551
{
 
552
        struct winbindd_fd_event *match;
 
553
 
 
554
        /* only add unique winbindd_fd_event structs */
 
555
 
 
556
        for (match=fd_events; match; match=match->next ) {
 
557
#ifdef DEVELOPER
 
558
                SMB_ASSERT( match != ev );
 
559
#else
 
560
                if ( match == ev )
 
561
                        return;
 
562
#endif
 
563
        }
 
564
 
 
565
        DLIST_ADD(fd_events, ev);
 
566
}
 
567
 
 
568
void remove_fd_event(struct winbindd_fd_event *ev)
 
569
{
 
570
        DLIST_REMOVE(fd_events, ev);
 
571
}
 
572
 
 
573
/*
 
574
 * Handler for winbindd_fd_events to complete a read/write request, set up by
 
575
 * setup_async_read/setup_async_write.
 
576
 */
 
577
 
 
578
static void rw_callback(struct winbindd_fd_event *event, int flags)
 
579
{
 
580
        size_t todo;
 
581
        ssize_t done = 0;
 
582
 
 
583
        todo = event->length - event->done;
 
584
 
 
585
        if (event->flags & EVENT_FD_WRITE) {
 
586
                SMB_ASSERT(flags == EVENT_FD_WRITE);
 
587
                done = sys_write(event->fd,
 
588
                                 &((char *)event->data)[event->done],
 
589
                                 todo);
 
590
 
 
591
                if (done <= 0) {
 
592
                        event->flags = 0;
 
593
                        event->finished(event->private_data, False);
 
594
                        return;
 
595
                }
 
596
        }
 
597
 
 
598
        if (event->flags & EVENT_FD_READ) {
 
599
                SMB_ASSERT(flags == EVENT_FD_READ);
 
600
                done = sys_read(event->fd, &((char *)event->data)[event->done],
 
601
                                todo);
 
602
 
 
603
                if (done <= 0) {
 
604
                        event->flags = 0;
 
605
                        event->finished(event->private_data, False);
 
606
                        return;
 
607
                }
 
608
        }
 
609
 
 
610
        event->done += done;
 
611
 
 
612
        if (event->done == event->length) {
 
613
                event->flags = 0;
 
614
                event->finished(event->private_data, True);
 
615
        }
 
616
}
 
617
 
 
618
/*
 
619
 * Request an async read/write on a winbindd_fd_event structure. (*finished) is called
 
620
 * when the request is completed or an error had occurred.
 
621
 */
 
622
 
 
623
void setup_async_read(struct winbindd_fd_event *event, void *data, size_t length,
 
624
                      void (*finished)(void *private_data, bool success),
 
625
                      void *private_data)
 
626
{
 
627
        SMB_ASSERT(event->flags == 0);
 
628
        event->data = data;
 
629
        event->length = length;
 
630
        event->done = 0;
 
631
        event->handler = rw_callback;
 
632
        event->finished = finished;
 
633
        event->private_data = private_data;
 
634
        event->flags = EVENT_FD_READ;
 
635
}
 
636
 
 
637
void setup_async_write(struct winbindd_fd_event *event, void *data, size_t length,
 
638
                       void (*finished)(void *private_data, bool success),
 
639
                       void *private_data)
 
640
{
 
641
        SMB_ASSERT(event->flags == 0);
 
642
        event->data = data;
 
643
        event->length = length;
 
644
        event->done = 0;
 
645
        event->handler = rw_callback;
 
646
        event->finished = finished;
 
647
        event->private_data = private_data;
 
648
        event->flags = EVENT_FD_WRITE;
 
649
}
 
650
 
 
651
/*
 
652
 * This is the main event loop of winbind requests. It goes through a
 
653
 * state-machine of 3 read/write requests, 4 if you have extra data to send.
 
654
 *
 
655
 * An idle winbind client has a read request of 4 bytes outstanding,
 
656
 * finalizing function is request_len_recv, checking the length. request_recv
 
657
 * then processes the packet. The processing function then at some point has
 
658
 * to call request_finished which schedules sending the response.
 
659
 */
 
660
 
 
661
static void request_len_recv(void *private_data, bool success);
 
662
static void request_recv(void *private_data, bool success);
 
663
static void request_main_recv(void *private_data, bool success);
 
664
static void request_finished(struct winbindd_cli_state *state);
 
665
static void response_main_sent(void *private_data, bool success);
 
666
static void response_extra_sent(void *private_data, bool success);
 
667
 
 
668
static void response_extra_sent(void *private_data, bool success)
 
669
{
 
670
        struct winbindd_cli_state *state =
 
671
                talloc_get_type_abort(private_data, struct winbindd_cli_state);
 
672
 
 
673
        TALLOC_FREE(state->mem_ctx);
 
674
 
 
675
        if (!success) {
 
676
                state->finished = True;
 
677
                return;
 
678
        }
 
679
 
 
680
        SAFE_FREE(state->response.extra_data.data);
 
681
 
 
682
        setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
 
683
                         request_len_recv, state);
 
684
}
 
685
 
 
686
static void response_main_sent(void *private_data, bool success)
 
687
{
 
688
        struct winbindd_cli_state *state =
 
689
                talloc_get_type_abort(private_data, struct winbindd_cli_state);
 
690
 
 
691
        if (!success) {
 
692
                state->finished = True;
 
693
                return;
 
694
        }
 
695
 
 
696
        if (state->response.length == sizeof(state->response)) {
 
697
                TALLOC_FREE(state->mem_ctx);
 
698
 
 
699
                setup_async_read(&state->fd_event, &state->request,
 
700
                                 sizeof(uint32), request_len_recv, state);
 
701
                return;
 
702
        }
 
703
 
 
704
        setup_async_write(&state->fd_event, state->response.extra_data.data,
 
705
                          state->response.length - sizeof(state->response),
 
706
                          response_extra_sent, state);
 
707
}
 
708
 
 
709
static void request_finished(struct winbindd_cli_state *state)
 
710
{
 
711
        /* Make sure request.extra_data is freed when finish processing a request */
 
712
        SAFE_FREE(state->request.extra_data.data);
 
713
        setup_async_write(&state->fd_event, &state->response,
 
714
                          sizeof(state->response), response_main_sent, state);
 
715
}
 
716
 
 
717
void request_error(struct winbindd_cli_state *state)
 
718
{
 
719
        SMB_ASSERT(state->response.result == WINBINDD_PENDING);
 
720
        state->response.result = WINBINDD_ERROR;
 
721
        request_finished(state);
 
722
}
 
723
 
 
724
void request_ok(struct winbindd_cli_state *state)
 
725
{
 
726
        SMB_ASSERT(state->response.result == WINBINDD_PENDING);
 
727
        state->response.result = WINBINDD_OK;
 
728
        request_finished(state);
 
729
}
 
730
 
 
731
static void request_len_recv(void *private_data, bool success)
 
732
{
 
733
        struct winbindd_cli_state *state =
 
734
                talloc_get_type_abort(private_data, struct winbindd_cli_state);
 
735
 
 
736
        if (!success) {
 
737
                state->finished = True;
 
738
                return;
 
739
        }
 
740
 
 
741
        if (*(uint32 *)(&state->request) != sizeof(state->request)) {
 
742
                DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %u)\n",
 
743
                         *(uint32_t *)(&state->request), (uint32_t)sizeof(state->request)));
 
744
                state->finished = True;
 
745
                return;
 
746
        }
 
747
 
 
748
        setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
 
749
                         sizeof(state->request) - sizeof(uint32),
 
750
                         request_main_recv, state);
 
751
}
 
752
 
 
753
static void request_main_recv(void *private_data, bool success)
 
754
{
 
755
        struct winbindd_cli_state *state =
 
756
                talloc_get_type_abort(private_data, struct winbindd_cli_state);
 
757
 
 
758
        if (!success) {
 
759
                state->finished = True;
 
760
                return;
 
761
        }
 
762
 
 
763
        if (state->request.extra_len == 0) {
 
764
                state->request.extra_data.data = NULL;
 
765
                request_recv(state, True);
 
766
                return;
 
767
        }
 
768
 
 
769
        if ((!state->privileged) &&
 
770
            (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
 
771
                DEBUG(3, ("Got request with %d bytes extra data on "
 
772
                          "unprivileged socket\n", (int)state->request.extra_len));
 
773
                state->request.extra_data.data = NULL;
 
774
                state->finished = True;
 
775
                return;
 
776
        }
 
777
 
 
778
        state->request.extra_data.data =
 
779
                SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
 
780
 
 
781
        if (state->request.extra_data.data == NULL) {
 
782
                DEBUG(0, ("malloc failed\n"));
 
783
                state->finished = True;
 
784
                return;
 
785
        }
 
786
 
 
787
        /* Ensure null termination */
 
788
        state->request.extra_data.data[state->request.extra_len] = '\0';
 
789
 
 
790
        setup_async_read(&state->fd_event, state->request.extra_data.data,
 
791
                         state->request.extra_len, request_recv, state);
 
792
}
 
793
 
 
794
static void request_recv(void *private_data, bool success)
 
795
{
 
796
        struct winbindd_cli_state *state =
 
797
                talloc_get_type_abort(private_data, struct winbindd_cli_state);
 
798
 
 
799
        if (!success) {
 
800
                state->finished = True;
 
801
                return;
 
802
        }
 
803
 
 
804
        process_request(state);
 
805
}
 
806
 
 
807
/* Process a new connection by adding it to the client connection list */
 
808
 
 
809
static void new_connection(int listen_sock, bool privileged)
 
810
{
 
811
        struct sockaddr_un sunaddr;
 
812
        struct winbindd_cli_state *state;
 
813
        socklen_t len;
 
814
        int sock;
 
815
 
 
816
        /* Accept connection */
 
817
 
 
818
        len = sizeof(sunaddr);
 
819
 
 
820
        do {
 
821
                sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
 
822
        } while (sock == -1 && errno == EINTR);
 
823
 
 
824
        if (sock == -1)
 
825
                return;
 
826
 
 
827
        DEBUG(6,("accepted socket %d\n", sock));
 
828
 
 
829
        /* Create new connection structure */
 
830
 
 
831
        if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
 
832
                close(sock);
 
833
                return;
 
834
        }
 
835
 
 
836
        state->sock = sock;
 
837
 
 
838
        state->last_access = time(NULL);        
 
839
 
 
840
        state->privileged = privileged;
 
841
 
 
842
        state->fd_event.fd = state->sock;
 
843
        state->fd_event.flags = 0;
 
844
        add_fd_event(&state->fd_event);
 
845
 
 
846
        setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
 
847
                         request_len_recv, state);
 
848
 
 
849
        /* Add to connection list */
 
850
 
 
851
        winbindd_add_client(state);
 
852
}
 
853
 
 
854
/* Remove a client connection from client connection list */
 
855
 
 
856
static void remove_client(struct winbindd_cli_state *state)
 
857
{
 
858
        char c = 0;
 
859
        int nwritten;
 
860
 
 
861
        /* It's a dead client - hold a funeral */
 
862
 
 
863
        if (state == NULL) {
 
864
                return;
 
865
        }
 
866
 
 
867
        if (!state->finished) {
 
868
                /* tell client, we are closing ... */
 
869
                nwritten = write(state->sock, &c, sizeof(c));
 
870
                if (nwritten == -1) {
 
871
                        DEBUG(2, ("final write to client failed: %s\n",
 
872
                                  strerror(errno)));
 
873
                }
 
874
        }
 
875
 
 
876
        /* Close socket */
 
877
 
 
878
        close(state->sock);
 
879
 
 
880
        /* Free any getent state */
 
881
 
 
882
        free_getent_state(state->getpwent_state);
 
883
        free_getent_state(state->getgrent_state);
 
884
 
 
885
        /* We may have some extra data that was not freed if the client was
 
886
           killed unexpectedly */
 
887
 
 
888
        SAFE_FREE(state->response.extra_data.data);
 
889
 
 
890
        TALLOC_FREE(state->mem_ctx);
 
891
 
 
892
        remove_fd_event(&state->fd_event);
 
893
 
 
894
        /* Remove from list and free */
 
895
 
 
896
        winbindd_remove_client(state);
 
897
        TALLOC_FREE(state);
 
898
}
 
899
 
 
900
/* Shutdown client connection which has been idle for the longest time */
 
901
 
 
902
static bool remove_idle_client(void)
 
903
{
 
904
        struct winbindd_cli_state *state, *remove_state = NULL;
 
905
        time_t last_access = 0;
 
906
        int nidle = 0;
 
907
 
 
908
        for (state = winbindd_client_list(); state; state = state->next) {
 
909
                if (state->response.result != WINBINDD_PENDING &&
 
910
                    state->fd_event.flags == EVENT_FD_READ &&
 
911
                    !state->getpwent_state && !state->getgrent_state) {
 
912
                        nidle++;
 
913
                        if (!last_access || state->last_access < last_access) {
 
914
                                last_access = state->last_access;
 
915
                                remove_state = state;
 
916
                        }
 
917
                }
 
918
        }
 
919
 
 
920
        if (remove_state) {
 
921
                DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
 
922
                        nidle, remove_state->sock, (unsigned int)remove_state->pid));
 
923
                remove_client(remove_state);
 
924
                return True;
 
925
        }
 
926
 
 
927
        return False;
 
928
}
 
929
 
 
930
struct winbindd_listen_state {
 
931
        bool privileged;
 
932
        int fd;
 
933
        struct tevent_fd *fde;
 
934
};
 
935
 
 
936
static void winbindd_listen_fde_handler(struct tevent_context *ev,
 
937
                                        struct tevent_fd *fde,
 
938
                                        uint16_t flags,
 
939
                                        void *private_data)
 
940
{
 
941
        struct winbindd_listen_state *s = talloc_get_type_abort(private_data,
 
942
                                          struct winbindd_listen_state);
 
943
 
 
944
        while (winbindd_num_clients() >
 
945
               WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
 
946
                DEBUG(5,("winbindd: Exceeding %d client "
 
947
                         "connections, removing idle "
 
948
                         "connection.\n",
 
949
                         WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
 
950
                if (!remove_idle_client()) {
 
951
                        DEBUG(0,("winbindd: Exceeding %d "
 
952
                                 "client connections, no idle "
 
953
                                 "connection found\n",
 
954
                                 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
 
955
                        break;
 
956
                }
 
957
        }
 
958
 
 
959
        /* new, non-privileged connection */
 
960
        new_connection(s->fd, s->privileged);
 
961
}
 
962
 
 
963
static bool winbindd_setup_listeners(void)
 
964
{
 
965
        struct winbindd_listen_state *pub_state = NULL;
 
966
        struct winbindd_listen_state *priv_state = NULL;
 
967
 
 
968
        pub_state = talloc(winbind_event_context(),
 
969
                           struct winbindd_listen_state);
 
970
        if (!pub_state) {
 
971
                goto failed;
 
972
        }
 
973
 
 
974
        pub_state->privileged = false;
 
975
        pub_state->fd = open_winbindd_socket();
 
976
        if (pub_state->fd == -1) {
 
977
                goto failed;
 
978
        }
 
979
 
 
980
        pub_state->fde = tevent_add_fd(winbind_event_context(),
 
981
                                       pub_state, pub_state->fd,
 
982
                                       TEVENT_FD_READ,
 
983
                                       winbindd_listen_fde_handler,
 
984
                                       pub_state);
 
985
        if (!pub_state->fde) {
 
986
                close(pub_state->fd);
 
987
                goto failed;
 
988
        }
 
989
        tevent_fd_set_auto_close(pub_state->fde);
 
990
 
 
991
        priv_state = talloc(winbind_event_context(),
 
992
                            struct winbindd_listen_state);
 
993
        if (!priv_state) {
 
994
                goto failed;
 
995
        }
 
996
 
 
997
        priv_state->privileged = true;
 
998
        priv_state->fd = open_winbindd_priv_socket();
 
999
        if (priv_state->fd == -1) {
 
1000
                goto failed;
 
1001
        }
 
1002
 
 
1003
        priv_state->fde = tevent_add_fd(winbind_event_context(),
 
1004
                                        priv_state, priv_state->fd,
 
1005
                                        TEVENT_FD_READ,
 
1006
                                        winbindd_listen_fde_handler,
 
1007
                                        priv_state);
 
1008
        if (!priv_state->fde) {
 
1009
                close(priv_state->fd);
 
1010
                goto failed;
 
1011
        }
 
1012
        tevent_fd_set_auto_close(priv_state->fde);
 
1013
 
 
1014
        return true;
 
1015
failed:
 
1016
        TALLOC_FREE(pub_state);
 
1017
        TALLOC_FREE(priv_state);
 
1018
        return false;
 
1019
}
 
1020
 
 
1021
/* Process incoming clients on listen_sock.  We use a tricky non-blocking,
 
1022
   non-forking, non-threaded model which allows us to handle many
 
1023
   simultaneous connections while remaining impervious to many denial of
 
1024
   service attacks. */
 
1025
 
 
1026
static void process_loop(void)
 
1027
{
 
1028
        struct winbindd_fd_event *ev;
 
1029
        fd_set r_fds, w_fds;
 
1030
        int maxfd = 0, selret;
 
1031
        struct timeval timeout, ev_timeout;
 
1032
 
 
1033
        if (run_events(winbind_event_context(), 0, NULL, NULL)) {
 
1034
                return;
 
1035
        }
 
1036
 
 
1037
        /* Initialise fd lists for select() */
 
1038
 
 
1039
        FD_ZERO(&r_fds);
 
1040
        FD_ZERO(&w_fds);
 
1041
 
 
1042
        timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
 
1043
        timeout.tv_usec = 0;
 
1044
 
 
1045
        /* Check for any event timeouts. */
 
1046
        {
 
1047
                struct timeval now;
 
1048
                GetTimeOfDay(&now);
 
1049
 
 
1050
                /*
 
1051
                 * Initialize this high as event_add_to_select_args()
 
1052
                 * uses a timeval_min() on this and next_event. Fix
 
1053
                 * from Roel van Meer <rolek@alt001.com>.
 
1054
                 */
 
1055
 
 
1056
                ev_timeout.tv_sec = 999999;
 
1057
                ev_timeout.tv_usec = 0;
 
1058
 
 
1059
                event_add_to_select_args(winbind_event_context(), &now,
 
1060
                                         &r_fds, &w_fds, &ev_timeout, &maxfd);
 
1061
        }
 
1062
        if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
 
1063
                timeout = timeval_min(&timeout, &ev_timeout);
 
1064
        }
 
1065
 
 
1066
        for (ev = fd_events; ev; ev = ev->next) {
 
1067
                if (ev->flags & EVENT_FD_READ) {
 
1068
                        FD_SET(ev->fd, &r_fds);
 
1069
                        maxfd = MAX(ev->fd, maxfd);
 
1070
                }
 
1071
                if (ev->flags & EVENT_FD_WRITE) {
 
1072
                        FD_SET(ev->fd, &w_fds);
 
1073
                        maxfd = MAX(ev->fd, maxfd);
 
1074
                }
 
1075
        }
 
1076
 
 
1077
        /* Call select */
 
1078
 
 
1079
        selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
 
1080
 
 
1081
        if (selret == 0) {
 
1082
                goto no_fds_ready;
 
1083
        }
 
1084
 
 
1085
        if (selret == -1) {
 
1086
                if (errno == EINTR) {
 
1087
                        goto no_fds_ready;
 
1088
                }
 
1089
 
 
1090
                /* Select error, something is badly wrong */
 
1091
 
 
1092
                perror("select");
 
1093
                exit(1);
 
1094
        }
 
1095
 
 
1096
        /* selret > 0 */
 
1097
 
 
1098
        if (run_events(winbind_event_context(), selret, &r_fds, &w_fds)) {
 
1099
                return;
 
1100
        }
 
1101
 
 
1102
        ev = fd_events;
 
1103
        while (ev != NULL) {
 
1104
                struct winbindd_fd_event *next = ev->next;
 
1105
                int flags = 0;
 
1106
                if (FD_ISSET(ev->fd, &r_fds))
 
1107
                        flags |= EVENT_FD_READ;
 
1108
                if (FD_ISSET(ev->fd, &w_fds))
 
1109
                        flags |= EVENT_FD_WRITE;
 
1110
                if (flags) {
 
1111
                        ev->handler(ev, flags);
 
1112
                        return;
 
1113
                }
 
1114
                ev = next;
 
1115
        }
 
1116
 
 
1117
        return;
 
1118
 
 
1119
 no_fds_ready:
 
1120
 
 
1121
        run_events(winbind_event_context(), selret, &r_fds, &w_fds);
 
1122
 
 
1123
#if 0
 
1124
        winbindd_check_cache_size(time(NULL));
 
1125
#endif
 
1126
}
 
1127
 
 
1128
bool winbindd_use_idmap_cache(void)
 
1129
{
 
1130
        return !opt_nocache;
 
1131
}
 
1132
 
 
1133
bool winbindd_use_cache(void)
 
1134
{
 
1135
        return !opt_nocache;
 
1136
}
 
1137
 
 
1138
/* Main function */
 
1139
 
 
1140
int main(int argc, char **argv, char **envp)
 
1141
{
 
1142
        static bool is_daemon = False;
 
1143
        static bool Fork = True;
 
1144
        static bool log_stdout = False;
 
1145
        static bool no_process_group = False;
 
1146
        enum {
 
1147
                OPT_DAEMON = 1000,
 
1148
                OPT_FORK,
 
1149
                OPT_NO_PROCESS_GROUP,
 
1150
                OPT_LOG_STDOUT
 
1151
        };
 
1152
        struct poptOption long_options[] = {
 
1153
                POPT_AUTOHELP
 
1154
                { "stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
 
1155
                { "foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Daemon in foreground mode" },
 
1156
                { "no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
 
1157
                { "daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
 
1158
                { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
 
1159
                { "no-caching", 'n', POPT_ARG_NONE, NULL, 'n', "Disable caching" },
 
1160
                POPT_COMMON_SAMBA
 
1161
                POPT_TABLEEND
 
1162
        };
 
1163
        poptContext pc;
 
1164
        int opt;
 
1165
        TALLOC_CTX *frame = talloc_stackframe();
 
1166
 
 
1167
        /* glibc (?) likes to print "User defined signal 1" and exit if a
 
1168
           SIGUSR[12] is received before a handler is installed */
 
1169
 
 
1170
        CatchSignal(SIGUSR1, SIG_IGN);
 
1171
        CatchSignal(SIGUSR2, SIG_IGN);
 
1172
 
 
1173
        fault_setup((void (*)(void *))fault_quit );
 
1174
        dump_core_setup("winbindd");
 
1175
 
 
1176
        load_case_tables();
 
1177
 
 
1178
        /* Initialise for running in non-root mode */
 
1179
 
 
1180
        sec_init();
 
1181
 
 
1182
        set_remote_machine_name("winbindd", False);
 
1183
 
 
1184
        /* Set environment variable so we don't recursively call ourselves.
 
1185
           This may also be useful interactively. */
 
1186
 
 
1187
        if ( !winbind_off() ) {
 
1188
                DEBUG(0,("Failed to disable recusive winbindd calls.  Exiting.\n"));
 
1189
                exit(1);
 
1190
        }
 
1191
 
 
1192
        /* Initialise samba/rpc client stuff */
 
1193
 
 
1194
        pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
 
1195
 
 
1196
        while ((opt = poptGetNextOpt(pc)) != -1) {
 
1197
                switch (opt) {
 
1198
                        /* Don't become a daemon */
 
1199
                case OPT_DAEMON:
 
1200
                        is_daemon = True;
 
1201
                        break;
 
1202
                case 'i':
 
1203
                        interactive = True;
 
1204
                        log_stdout = True;
 
1205
                        Fork = False;
 
1206
                        break;
 
1207
                case OPT_FORK:
 
1208
                        Fork = false;
 
1209
                        break;
 
1210
                case OPT_NO_PROCESS_GROUP:
 
1211
                        no_process_group = true;
 
1212
                        break;
 
1213
                case OPT_LOG_STDOUT:
 
1214
                        log_stdout = true;
 
1215
                        break;
 
1216
                case 'n':
 
1217
                        opt_nocache = true;
 
1218
                        break;
 
1219
                default:
 
1220
                        d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
 
1221
                                  poptBadOption(pc, 0), poptStrerror(opt));
 
1222
                        poptPrintUsage(pc, stderr, 0);
 
1223
                        exit(1);
 
1224
                }
 
1225
        }
 
1226
 
 
1227
        if (is_daemon && interactive) {
 
1228
                d_fprintf(stderr,"\nERROR: "
 
1229
                          "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
 
1230
                poptPrintUsage(pc, stderr, 0);
 
1231
                exit(1);
 
1232
        }
 
1233
 
 
1234
        if (log_stdout && Fork) {
 
1235
                d_fprintf(stderr, "\nERROR: "
 
1236
                          "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
 
1237
                poptPrintUsage(pc, stderr, 0);
 
1238
                exit(1);
 
1239
        }
 
1240
 
 
1241
        poptFreeContext(pc);
 
1242
 
 
1243
        if (!override_logfile) {
 
1244
                char *lfile = NULL;
 
1245
                if (asprintf(&lfile,"%s/log.winbindd",
 
1246
                                get_dyn_LOGFILEBASE()) > 0) {
 
1247
                        lp_set_logfile(lfile);
 
1248
                        SAFE_FREE(lfile);
 
1249
                }
 
1250
        }
 
1251
        setup_logging("winbindd", log_stdout);
 
1252
        reopen_logs();
 
1253
 
 
1254
        DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
 
1255
        DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
 
1256
 
 
1257
        if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
 
1258
                DEBUG(0, ("error opening config file\n"));
 
1259
                exit(1);
 
1260
        }
 
1261
 
 
1262
        /* Initialise messaging system */
 
1263
 
 
1264
        if (winbind_messaging_context() == NULL) {
 
1265
                exit(1);
 
1266
        }
 
1267
 
 
1268
        if (!reload_services_file(NULL)) {
 
1269
                DEBUG(0, ("error opening config file\n"));
 
1270
                exit(1);
 
1271
        }
 
1272
 
 
1273
        if (!directory_exist(lp_lockdir())) {
 
1274
                mkdir(lp_lockdir(), 0755);
 
1275
        }
 
1276
 
 
1277
        /* Setup names. */
 
1278
 
 
1279
        if (!init_names())
 
1280
                exit(1);
 
1281
 
 
1282
        load_interfaces();
 
1283
 
 
1284
        if (!secrets_init()) {
 
1285
 
 
1286
                DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
 
1287
                return False;
 
1288
        }
 
1289
 
 
1290
        /* Enable netbios namecache */
 
1291
 
 
1292
        namecache_enable();
 
1293
 
 
1294
        /* Unblock all signals we are interested in as they may have been
 
1295
           blocked by the parent process. */
 
1296
 
 
1297
        BlockSignals(False, SIGINT);
 
1298
        BlockSignals(False, SIGQUIT);
 
1299
        BlockSignals(False, SIGTERM);
 
1300
        BlockSignals(False, SIGUSR1);
 
1301
        BlockSignals(False, SIGUSR2);
 
1302
        BlockSignals(False, SIGHUP);
 
1303
        BlockSignals(False, SIGCHLD);
 
1304
 
 
1305
        if (!interactive)
 
1306
                become_daemon(Fork, no_process_group);
 
1307
 
 
1308
        pidfile_create("winbindd");
 
1309
 
 
1310
#if HAVE_SETPGID
 
1311
        /*
 
1312
         * If we're interactive we want to set our own process group for
 
1313
         * signal management.
 
1314
         */
 
1315
        if (interactive && !no_process_group)
 
1316
                setpgid( (pid_t)0, (pid_t)0);
 
1317
#endif
 
1318
 
 
1319
        TimeInit();
 
1320
 
 
1321
        /* Don't use winbindd_reinit_after_fork here as
 
1322
         * we're just starting up and haven't created any
 
1323
         * winbindd-specific resources we must free yet. JRA.
 
1324
         */
 
1325
 
 
1326
        if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
 
1327
                                               winbind_event_context(),
 
1328
                                               false))) {
 
1329
                DEBUG(0,("reinit_after_fork() failed\n"));
 
1330
                exit(1);
 
1331
        }
 
1332
 
 
1333
        /* Setup signal handlers */
 
1334
 
 
1335
        if (!winbindd_setup_sig_term_handler(true))
 
1336
                exit(1);
 
1337
        if (!winbindd_setup_sig_hup_handler(NULL))
 
1338
                exit(1);
 
1339
        if (!winbindd_setup_sig_chld_handler())
 
1340
                exit(1);
 
1341
        if (!winbindd_setup_sig_usr2_handler())
 
1342
                exit(1);
 
1343
 
 
1344
        CatchSignal(SIGPIPE, SIG_IGN);                 /* Ignore sigpipe */
 
1345
 
 
1346
        /*
 
1347
         * Ensure all cache and idmap caches are consistent
 
1348
         * and initialized before we startup.
 
1349
         */
 
1350
        if (!winbindd_cache_validate_and_initialize()) {
 
1351
                exit(1);
 
1352
        }
 
1353
 
 
1354
        /* get broadcast messages */
 
1355
        claim_connection(NULL,"",FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP);
 
1356
 
 
1357
        /* React on 'smbcontrol winbindd reload-config' in the same way
 
1358
           as to SIGHUP signal */
 
1359
        messaging_register(winbind_messaging_context(), NULL,
 
1360
                           MSG_SMB_CONF_UPDATED, msg_reload_services);
 
1361
        messaging_register(winbind_messaging_context(), NULL,
 
1362
                           MSG_SHUTDOWN, msg_shutdown);
 
1363
 
 
1364
        /* Handle online/offline messages. */
 
1365
        messaging_register(winbind_messaging_context(), NULL,
 
1366
                           MSG_WINBIND_OFFLINE, winbind_msg_offline);
 
1367
        messaging_register(winbind_messaging_context(), NULL,
 
1368
                           MSG_WINBIND_ONLINE, winbind_msg_online);
 
1369
        messaging_register(winbind_messaging_context(), NULL,
 
1370
                           MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
 
1371
 
 
1372
        messaging_register(winbind_messaging_context(), NULL,
 
1373
                           MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
 
1374
 
 
1375
        messaging_register(winbind_messaging_context(), NULL,
 
1376
                           MSG_WINBIND_VALIDATE_CACHE,
 
1377
                           winbind_msg_validate_cache);
 
1378
 
 
1379
        messaging_register(winbind_messaging_context(), NULL,
 
1380
                           MSG_WINBIND_DUMP_DOMAIN_LIST,
 
1381
                           winbind_msg_dump_domain_list);
 
1382
 
 
1383
        /* Register handler for MSG_DEBUG. */
 
1384
        messaging_register(winbind_messaging_context(), NULL,
 
1385
                           MSG_DEBUG,
 
1386
                           winbind_msg_debug);
 
1387
 
 
1388
        netsamlogon_cache_init(); /* Non-critical */
 
1389
 
 
1390
        /* clear the cached list of trusted domains */
 
1391
 
 
1392
        wcache_tdc_clear();     
 
1393
 
 
1394
        if (!init_domain_list()) {
 
1395
                DEBUG(0,("unable to initialize domain list\n"));
 
1396
                exit(1);
 
1397
        }
 
1398
 
 
1399
        init_idmap_child();
 
1400
        init_locator_child();
 
1401
 
 
1402
        smb_nscd_flush_user_cache();
 
1403
        smb_nscd_flush_group_cache();
 
1404
 
 
1405
        /* setup listen sockets */
 
1406
 
 
1407
        if (!winbindd_setup_listeners()) {
 
1408
                DEBUG(0,("winbindd_setup_listeners() failed\n"));
 
1409
                exit(1);
 
1410
        }
 
1411
 
 
1412
        TALLOC_FREE(frame);
 
1413
        /* Loop waiting for requests */
 
1414
        while (1) {
 
1415
                struct winbindd_cli_state *state;
 
1416
 
 
1417
                frame = talloc_stackframe();
 
1418
 
 
1419
                /* refresh the trusted domain cache */
 
1420
 
 
1421
                rescan_trusted_domains();
 
1422
 
 
1423
                /* Dispose of client connection if it is marked as
 
1424
                   finished */
 
1425
                state = winbindd_client_list();
 
1426
                while (state) {
 
1427
                        struct winbindd_cli_state *next = state->next;
 
1428
 
 
1429
                        if (state->finished) {
 
1430
                                remove_client(state);
 
1431
                        }
 
1432
 
 
1433
                        state = next;
 
1434
                }
 
1435
 
 
1436
                process_loop();
 
1437
 
 
1438
                TALLOC_FREE(frame);
 
1439
        }
 
1440
 
 
1441
        return 0;
 
1442
}