main.c revision 178826
175899Sjoe/*
275899Sjoe * Copyright (c) 1997-2005 Kungliga Tekniska H�gskolan
375899Sjoe * (Royal Institute of Technology, Stockholm, Sweden).
4190421Sluigi * All rights reserved.
575899Sjoe *
6190421Sluigi * Redistribution and use in source and binary forms, with or without
7190421Sluigi * modification, are permitted provided that the following conditions
8190421Sluigi * are met:
9190421Sluigi *
1075899Sjoe * 1. Redistributions of source code must retain the above copyright
11190421Sluigi *    notice, this list of conditions and the following disclaimer.
12190421Sluigi *
13190421Sluigi * 2. Redistributions in binary form must reproduce the above copyright
14190421Sluigi *    notice, this list of conditions and the following disclaimer in the
15190421Sluigi *    documentation and/or other materials provided with the distribution.
16190421Sluigi *
17190421Sluigi * 3. Neither the name of the Institute nor the names of its contributors
18190421Sluigi *    may be used to endorse or promote products derived from this software
19190421Sluigi *    without specific prior written permission.
2075899Sjoe *
21190421Sluigi * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22190421Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23190421Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24190421Sluigi * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25190421Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26190421Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27190421Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28190421Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29190421Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3075899Sjoe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31190421Sluigi * SUCH DAMAGE.
32190421Sluigi */
33190421Sluigi
34156905Sru#include "kdc_locl.h"
3575899Sjoe#ifdef HAVE_UTIL_H
36190421Sluigi#include <util.h>
37102344Sluigi#endif
38190421Sluigi
39190421SluigiRCSID("$Id: main.c 20454 2007-04-19 20:21:51Z lha $");
40190421Sluigi
41190421Sluigisig_atomic_t exit_flag = 0;
42102344Sluigi
43102344Sluigiint detach_from_console = -1;
44190421Sluigi
45190421Sluigistatic RETSIGTYPE
46190421Sluigisigterm(int sig)
47190421Sluigi{
4884312Sluigi    exit_flag = sig;
49102344Sluigi}
50190421Sluigi
51190421Sluigiint
52190421Sluigimain(int argc, char **argv)
5375899Sjoe{
5475899Sjoe    krb5_error_code ret;
5575899Sjoe    krb5_context context;
5675899Sjoe    krb5_kdc_configuration *config;
5775899Sjoe
5875899Sjoe    setprogname(argv[0]);
5975899Sjoe
6075899Sjoe    ret = krb5_init_context(&context);
61190421Sluigi    if (ret == KRB5_CONFIG_BADFORMAT)
62190421Sluigi	errx (1, "krb5_init_context failed to parse configuration file");
63190421Sluigi    else if (ret)
64190421Sluigi	errx (1, "krb5_init_context failed: %d", ret);
65190421Sluigi
66190421Sluigi    ret = krb5_kt_register(context, &hdb_kt_ops);
67190421Sluigi    if (ret)
68190421Sluigi	errx (1, "krb5_kt_register(HDB) failed: %d", ret);
69190385Sluigi
70190421Sluigi    config = configure(context, argc, argv);
7175899Sjoe
7275899Sjoe#ifdef HAVE_SIGACTION
7375899Sjoe    {
74190421Sluigi	struct sigaction sa;
75190385Sluigi
76190421Sluigi	sa.sa_flags = 0;
77190421Sluigi	sa.sa_handler = sigterm;
78190385Sluigi	sigemptyset(&sa.sa_mask);
79190421Sluigi
8075899Sjoe	sigaction(SIGINT, &sa, NULL);
8175899Sjoe	sigaction(SIGTERM, &sa, NULL);
8275899Sjoe	sigaction(SIGXCPU, &sa, NULL);
83190421Sluigi
84190421Sluigi	sa.sa_handler = SIG_IGN;
85190421Sluigi	sigaction(SIGPIPE, &sa, NULL);
86190421Sluigi    }
87190421Sluigi#else
88190385Sluigi    signal(SIGINT, sigterm);
8975899Sjoe    signal(SIGTERM, sigterm);
90190421Sluigi    signal(SIGXCPU, sigterm);
91190421Sluigi    signal(SIGPIPE, SIG_IGN);
92190385Sluigi#endif
93190421Sluigi    if (detach_from_console)
9475899Sjoe	daemon(0, 0);
95190421Sluigi    pidfile(NULL);
96190421Sluigi    loop(context, config);
97190421Sluigi    krb5_free_context(context);
98190421Sluigi    return 0;
99190421Sluigi}
100190421Sluigi