1/*
2 * Copyright (c) 1997-2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "kcm_locl.h"
35
36#ifdef __APPLE__
37#include <sandbox.h>
38#endif
39
40sig_atomic_t exit_flag = 0;
41
42krb5_context kcm_context = NULL;
43
44const char *service_name = "org.h5l.kcm";
45
46static void
47terminated(void *ctx)
48{
49    exit(0);
50}
51
52static void
53sigusr1(void *ctx)
54{
55    kcm_debug_ccache(kcm_context);
56}
57
58static void
59sigusr2(void *ctx)
60{
61    kcm_debug_events(kcm_context);
62}
63
64static void
65timeout_handler(void)
66{
67    kcm_write_dump(kcm_context);
68    exit(0);
69}
70
71int
72main(int argc, char **argv)
73{
74    krb5_error_code ret;
75    setprogname(argv[0]);
76
77    ret = krb5_init_context(&kcm_context);
78    if (ret) {
79	errx (1, "krb5_init_context failed: %d", ret);
80	return ret;
81    }
82
83    kcm_configure(argc, argv);
84
85#ifdef HAVE_SIGACTION
86    {
87	struct sigaction sa;
88
89	sa.sa_flags = 0;
90	sa.sa_handler = SIG_IGN;
91	sigemptyset(&sa.sa_mask);
92	sigaction(SIGPIPE, &sa, NULL);
93    }
94#else
95    signal(SIGPIPE, SIG_IGN);
96#endif
97
98    heim_sipc_signal_handler(SIGINT, terminated, "SIGINT");
99    heim_sipc_signal_handler(SIGTERM, terminated, "SIGTERM");
100    heim_sipc_signal_handler(SIGUSR1, sigusr1, NULL);
101    heim_sipc_signal_handler(SIGUSR2, sigusr2, NULL);
102#ifdef SIGXCPU
103    heim_sipc_signal_handler(SIGXCPU, terminated, "CPU time limit exceeded");
104#endif
105
106
107#ifdef SUPPORT_DETACH
108    if (detach_from_console)
109	daemon(0, 0);
110#endif
111    kcm_session_setup_handler();
112
113    kcm_read_dump(kcm_context);
114
115    if (launchd_flag) {
116	heim_sipc mach;
117	heim_sipc_launchd_mach_init(service_name, kcm_service, NULL, &mach);
118    } else {
119	heim_sipc un;
120	heim_sipc_service_unix(service_name, kcm_service, NULL, &un);
121    }
122
123#ifdef __APPLE__
124    {
125	char *errorstring;
126	ret = sandbox_init("kcm", SANDBOX_NAMED, &errorstring);
127	if (ret)
128	    errx(1, "sandbox_init failed: %d: %s", ret, errorstring);
129    }
130#endif /* __APPLE__ */
131
132    heim_sipc_set_timeout_handler(timeout_handler);
133
134    /*
135     * If we have didn't get a time or, and it was non zero,
136     * lets set the timeout
137     */
138    if (kcm_timeout < 0)
139	kcm_timeout = 15;
140    if (kcm_timeout != 0)
141	heim_sipc_timeout(kcm_timeout);
142
143    heim_ipc_main();
144
145    krb5_free_context(kcm_context);
146    return 0;
147}
148