1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6*ae771770SStanislav Sedov * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7*ae771770SStanislav Sedov *
8b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
9b528cefcSMark Murray * modification, are permitted provided that the following conditions
10b528cefcSMark Murray * are met:
11b528cefcSMark Murray *
12b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
13b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
14b528cefcSMark Murray *
15b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
16b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
17b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
18b528cefcSMark Murray *
19b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
20b528cefcSMark Murray * may be used to endorse or promote products derived from this software
21b528cefcSMark Murray * without specific prior written permission.
22b528cefcSMark Murray *
23b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33b528cefcSMark Murray * SUCH DAMAGE.
34b528cefcSMark Murray */
35b528cefcSMark Murray
36b528cefcSMark Murray #include "kdc_locl.h"
378373020dSJacques Vidrine #ifdef HAVE_UTIL_H
388373020dSJacques Vidrine #include <util.h>
398373020dSJacques Vidrine #endif
40b528cefcSMark Murray
41*ae771770SStanislav Sedov #ifdef HAVE_CAPNG
42*ae771770SStanislav Sedov #include <cap-ng.h>
43*ae771770SStanislav Sedov #endif
44b528cefcSMark Murray
45b528cefcSMark Murray sig_atomic_t exit_flag = 0;
46b528cefcSMark Murray
47*ae771770SStanislav Sedov #ifdef SUPPORT_DETACH
48c19800e8SDoug Rabson int detach_from_console = -1;
49*ae771770SStanislav Sedov #endif
508373020dSJacques Vidrine
51b528cefcSMark Murray static RETSIGTYPE
sigterm(int sig)52b528cefcSMark Murray sigterm(int sig)
53b528cefcSMark Murray {
54c19800e8SDoug Rabson exit_flag = sig;
55b528cefcSMark Murray }
56b528cefcSMark Murray
57*ae771770SStanislav Sedov /*
58*ae771770SStanislav Sedov * Allow dropping root bit, since heimdal reopens the database all the
59*ae771770SStanislav Sedov * time the database needs to be owned by the user you are switched
60*ae771770SStanislav Sedov * too. A better solution is to split the kdc in to more processes and
61*ae771770SStanislav Sedov * run the network facing part with very low privilege.
62*ae771770SStanislav Sedov */
63*ae771770SStanislav Sedov
64*ae771770SStanislav Sedov static void
switch_environment(void)65*ae771770SStanislav Sedov switch_environment(void)
66*ae771770SStanislav Sedov {
67*ae771770SStanislav Sedov #ifdef HAVE_GETEUID
68*ae771770SStanislav Sedov if ((runas_string || chroot_string) && geteuid() != 0)
69*ae771770SStanislav Sedov errx(1, "no running as root, can't switch user/chroot");
70*ae771770SStanislav Sedov
71*ae771770SStanislav Sedov if (chroot_string && chroot(chroot_string) != 0)
72*ae771770SStanislav Sedov errx(1, "chroot(%s)", "chroot_string failed");
73*ae771770SStanislav Sedov
74*ae771770SStanislav Sedov if (runas_string) {
75*ae771770SStanislav Sedov struct passwd *pw;
76*ae771770SStanislav Sedov
77*ae771770SStanislav Sedov pw = getpwnam(runas_string);
78*ae771770SStanislav Sedov if (pw == NULL)
79*ae771770SStanislav Sedov errx(1, "unknown user %s", runas_string);
80*ae771770SStanislav Sedov
81*ae771770SStanislav Sedov if (initgroups(pw->pw_name, pw->pw_gid) < 0)
82*ae771770SStanislav Sedov err(1, "initgroups failed");
83*ae771770SStanislav Sedov
84*ae771770SStanislav Sedov #ifndef HAVE_CAPNG
85*ae771770SStanislav Sedov if (setgid(pw->pw_gid) < 0)
86*ae771770SStanislav Sedov err(1, "setgid(%s) failed", runas_string);
87*ae771770SStanislav Sedov
88*ae771770SStanislav Sedov if (setuid(pw->pw_uid) < 0)
89*ae771770SStanislav Sedov err(1, "setuid(%s)", runas_string);
90*ae771770SStanislav Sedov #else
91*ae771770SStanislav Sedov capng_clear (CAPNG_EFFECTIVE | CAPNG_PERMITTED);
92*ae771770SStanislav Sedov if (capng_updatev (CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
93*ae771770SStanislav Sedov CAP_NET_BIND_SERVICE, CAP_SETPCAP, -1) < 0)
94*ae771770SStanislav Sedov err(1, "capng_updateev");
95*ae771770SStanislav Sedov
96*ae771770SStanislav Sedov if (capng_change_id(pw->pw_uid, pw->pw_gid,
97*ae771770SStanislav Sedov CAPNG_CLEAR_BOUNDING) < 0)
98*ae771770SStanislav Sedov err(1, "capng_change_id(%s)", runas_string);
99*ae771770SStanislav Sedov #endif
100*ae771770SStanislav Sedov }
101*ae771770SStanislav Sedov #endif
102*ae771770SStanislav Sedov }
103*ae771770SStanislav Sedov
104*ae771770SStanislav Sedov
105b528cefcSMark Murray int
main(int argc,char ** argv)106b528cefcSMark Murray main(int argc, char **argv)
107b528cefcSMark Murray {
108b528cefcSMark Murray krb5_error_code ret;
109c19800e8SDoug Rabson krb5_context context;
110c19800e8SDoug Rabson krb5_kdc_configuration *config;
111c19800e8SDoug Rabson
112adb0ddaeSAssar Westerlund setprogname(argv[0]);
113b528cefcSMark Murray
1145e9cd1aeSAssar Westerlund ret = krb5_init_context(&context);
115c19800e8SDoug Rabson if (ret == KRB5_CONFIG_BADFORMAT)
116c19800e8SDoug Rabson errx (1, "krb5_init_context failed to parse configuration file");
117c19800e8SDoug Rabson else if (ret)
1185e9cd1aeSAssar Westerlund errx (1, "krb5_init_context failed: %d", ret);
119b528cefcSMark Murray
120c19800e8SDoug Rabson ret = krb5_kt_register(context, &hdb_kt_ops);
121c19800e8SDoug Rabson if (ret)
122c19800e8SDoug Rabson errx (1, "krb5_kt_register(HDB) failed: %d", ret);
123b528cefcSMark Murray
124c19800e8SDoug Rabson config = configure(context, argc, argv);
125b528cefcSMark Murray
126b528cefcSMark Murray #ifdef HAVE_SIGACTION
127b528cefcSMark Murray {
128b528cefcSMark Murray struct sigaction sa;
129b528cefcSMark Murray
130b528cefcSMark Murray sa.sa_flags = 0;
131b528cefcSMark Murray sa.sa_handler = sigterm;
132b528cefcSMark Murray sigemptyset(&sa.sa_mask);
133b528cefcSMark Murray
134b528cefcSMark Murray sigaction(SIGINT, &sa, NULL);
1355e9cd1aeSAssar Westerlund sigaction(SIGTERM, &sa, NULL);
136*ae771770SStanislav Sedov #ifdef SIGXCPU
137c19800e8SDoug Rabson sigaction(SIGXCPU, &sa, NULL);
138*ae771770SStanislav Sedov #endif
139c19800e8SDoug Rabson
140c19800e8SDoug Rabson sa.sa_handler = SIG_IGN;
141*ae771770SStanislav Sedov #ifdef SIGPIPE
142c19800e8SDoug Rabson sigaction(SIGPIPE, &sa, NULL);
143*ae771770SStanislav Sedov #endif
144b528cefcSMark Murray }
145b528cefcSMark Murray #else
146b528cefcSMark Murray signal(SIGINT, sigterm);
1475e9cd1aeSAssar Westerlund signal(SIGTERM, sigterm);
148*ae771770SStanislav Sedov #ifdef SIGXCPU
149c19800e8SDoug Rabson signal(SIGXCPU, sigterm);
150*ae771770SStanislav Sedov #endif
151*ae771770SStanislav Sedov #ifdef SIGPIPE
152c19800e8SDoug Rabson signal(SIGPIPE, SIG_IGN);
153b528cefcSMark Murray #endif
154*ae771770SStanislav Sedov #endif
155*ae771770SStanislav Sedov #ifdef SUPPORT_DETACH
1568373020dSJacques Vidrine if (detach_from_console)
1578373020dSJacques Vidrine daemon(0, 0);
158*ae771770SStanislav Sedov #endif
159*ae771770SStanislav Sedov #ifdef __APPLE__
160*ae771770SStanislav Sedov bonjour_announce(context, config);
161*ae771770SStanislav Sedov #endif
1625e9cd1aeSAssar Westerlund pidfile(NULL);
163*ae771770SStanislav Sedov
164*ae771770SStanislav Sedov switch_environment();
165*ae771770SStanislav Sedov
166c19800e8SDoug Rabson loop(context, config);
167b528cefcSMark Murray krb5_free_context(context);
168b528cefcSMark Murray return 0;
169b528cefcSMark Murray }
170