/*1* Copyright (c) 1997-2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "kdc_locl.h"36#ifdef HAVE_UTIL_H37#include <util.h>38#endif3940#ifdef HAVE_CAPNG41#include <cap-ng.h>42#endif4344sig_atomic_t exit_flag = 0;4546#ifdef SUPPORT_DETACH47int detach_from_console = -1;48#endif4950static RETSIGTYPE51sigterm(int sig)52{53exit_flag = sig;54}5556/*57* Allow dropping root bit, since heimdal reopens the database all the58* time the database needs to be owned by the user you are switched59* too. A better solution is to split the kdc in to more processes and60* run the network facing part with very low privilege.61*/6263static void64switch_environment(void)65{66#ifdef HAVE_GETEUID67if ((runas_string || chroot_string) && geteuid() != 0)68errx(1, "no running as root, can't switch user/chroot");6970if (chroot_string && chroot(chroot_string) != 0)71errx(1, "chroot(%s)", "chroot_string failed");7273if (runas_string) {74struct passwd *pw;7576pw = getpwnam(runas_string);77if (pw == NULL)78errx(1, "unknown user %s", runas_string);7980if (initgroups(pw->pw_name, pw->pw_gid) < 0)81err(1, "initgroups failed");8283#ifndef HAVE_CAPNG84if (setgid(pw->pw_gid) < 0)85err(1, "setgid(%s) failed", runas_string);8687if (setuid(pw->pw_uid) < 0)88err(1, "setuid(%s)", runas_string);89#else90capng_clear (CAPNG_EFFECTIVE | CAPNG_PERMITTED);91if (capng_updatev (CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,92CAP_NET_BIND_SERVICE, CAP_SETPCAP, -1) < 0)93err(1, "capng_updateev");9495if (capng_change_id(pw->pw_uid, pw->pw_gid,96CAPNG_CLEAR_BOUNDING) < 0)97err(1, "capng_change_id(%s)", runas_string);98#endif99}100#endif101}102103104int105main(int argc, char **argv)106{107krb5_error_code ret;108krb5_context context;109krb5_kdc_configuration *config;110111setprogname(argv[0]);112113ret = krb5_init_context(&context);114if (ret == KRB5_CONFIG_BADFORMAT)115errx (1, "krb5_init_context failed to parse configuration file");116else if (ret)117errx (1, "krb5_init_context failed: %d", ret);118119ret = krb5_kt_register(context, &hdb_kt_ops);120if (ret)121errx (1, "krb5_kt_register(HDB) failed: %d", ret);122123config = configure(context, argc, argv);124125#ifdef HAVE_SIGACTION126{127struct sigaction sa;128129sa.sa_flags = 0;130sa.sa_handler = sigterm;131sigemptyset(&sa.sa_mask);132133sigaction(SIGINT, &sa, NULL);134sigaction(SIGTERM, &sa, NULL);135#ifdef SIGXCPU136sigaction(SIGXCPU, &sa, NULL);137#endif138139sa.sa_handler = SIG_IGN;140#ifdef SIGPIPE141sigaction(SIGPIPE, &sa, NULL);142#endif143}144#else145signal(SIGINT, sigterm);146signal(SIGTERM, sigterm);147#ifdef SIGXCPU148signal(SIGXCPU, sigterm);149#endif150#ifdef SIGPIPE151signal(SIGPIPE, SIG_IGN);152#endif153#endif154#ifdef SUPPORT_DETACH155if (detach_from_console)156daemon(0, 0);157#endif158#ifdef __APPLE__159bonjour_announce(context, config);160#endif161pidfile(NULL);162163switch_environment();164165loop(context, config);166krb5_free_context(context);167return 0;168}169170171