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