1c5c4113dSnw141292 /* 2c5c4113dSnw141292 * CDDL HEADER START 3c5c4113dSnw141292 * 4c5c4113dSnw141292 * The contents of this file are subject to the terms of the 5c5c4113dSnw141292 * Common Development and Distribution License (the "License"). 6c5c4113dSnw141292 * You may not use this file except in compliance with the License. 7c5c4113dSnw141292 * 8c5c4113dSnw141292 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9c5c4113dSnw141292 * or http://www.opensolaris.org/os/licensing. 10c5c4113dSnw141292 * See the License for the specific language governing permissions 11c5c4113dSnw141292 * and limitations under the License. 12c5c4113dSnw141292 * 13c5c4113dSnw141292 * When distributing Covered Code, include this CDDL HEADER in each 14c5c4113dSnw141292 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15c5c4113dSnw141292 * If applicable, add the following below this CDDL HEADER, with the 16c5c4113dSnw141292 * fields enclosed by brackets "[]" replaced with your own identifying 17c5c4113dSnw141292 * information: Portions Copyright [yyyy] [name of copyright owner] 18c5c4113dSnw141292 * 19c5c4113dSnw141292 * CDDL HEADER END 20c5c4113dSnw141292 */ 21c5c4113dSnw141292 /* 22c5c4113dSnw141292 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23c5c4113dSnw141292 * Use is subject to license terms. 24c5c4113dSnw141292 */ 25c5c4113dSnw141292 26c5c4113dSnw141292 #pragma ident "%Z%%M% %I% %E% SMI" 27c5c4113dSnw141292 28c5c4113dSnw141292 /* 29c5c4113dSnw141292 * main() of idmapd(1M) 30c5c4113dSnw141292 */ 31c5c4113dSnw141292 32c5c4113dSnw141292 #include "idmapd.h" 33c5c4113dSnw141292 #include <signal.h> 34c5c4113dSnw141292 #include <rpc/pmap_clnt.h> /* for pmap_unset */ 35c5c4113dSnw141292 #include <string.h> /* strcmp */ 36c5c4113dSnw141292 #include <unistd.h> /* setsid */ 37c5c4113dSnw141292 #include <sys/types.h> 38c5c4113dSnw141292 #include <memory.h> 39c5c4113dSnw141292 #include <stropts.h> 40c5c4113dSnw141292 #include <netconfig.h> 41c5c4113dSnw141292 #include <sys/resource.h> /* rlimit */ 42c5c4113dSnw141292 #include <syslog.h> 43c5c4113dSnw141292 #include <rpcsvc/daemon_utils.h> /* DAEMON_UID and DAEMON_GID */ 44c5c4113dSnw141292 #include <priv_utils.h> /* privileges */ 45c5c4113dSnw141292 #include <locale.h> 46c5c4113dSnw141292 #include <sys/systeminfo.h> 47c5c4113dSnw141292 #include <errno.h> 48c5c4113dSnw141292 #include <sys/wait.h> 49c5c4113dSnw141292 #include <sys/time.h> 50c5c4113dSnw141292 #include <zone.h> 51c5c4113dSnw141292 #include <door.h> 52c8e26105Sjp151216 #include <port.h> 53c5c4113dSnw141292 #include <tsol/label.h> 54c5c4113dSnw141292 #include <sys/resource.h> 55c5c4113dSnw141292 #include <sys/sid.h> 56c5c4113dSnw141292 #include <sys/idmap.h> 57c5c4113dSnw141292 58c5c4113dSnw141292 static void hup_handler(int); 59c5c4113dSnw141292 static void term_handler(int); 60c5c4113dSnw141292 static void init_idmapd(); 61c5c4113dSnw141292 static void fini_idmapd(); 62c5c4113dSnw141292 63c5c4113dSnw141292 #ifndef SIG_PF 64c5c4113dSnw141292 #define SIG_PF void(*)(int) 65c5c4113dSnw141292 #endif 66c5c4113dSnw141292 67c5c4113dSnw141292 #define _RPCSVC_CLOSEDOWN 120 68c5c4113dSnw141292 69c5c4113dSnw141292 int _rpcsvcstate = _IDLE; /* Set when a request is serviced */ 70c5c4113dSnw141292 int _rpcsvccount = 0; /* Number of requests being serviced */ 71c5c4113dSnw141292 mutex_t _svcstate_lock; /* lock for _rpcsvcstate, _rpcsvccount */ 72c5c4113dSnw141292 idmapd_state_t _idmapdstate; 73c5c4113dSnw141292 74c8e26105Sjp151216 int hupped; 75c8e26105Sjp151216 extern int hup_ev_port; 76c8e26105Sjp151216 77c5c4113dSnw141292 SVCXPRT *xprt = NULL; 78c5c4113dSnw141292 79c5c4113dSnw141292 static int dfd = -1; /* our door server fildes, for unregistration */ 80c5c4113dSnw141292 81c5c4113dSnw141292 #ifdef DEBUG 82c5c4113dSnw141292 #define RPC_SVC_FG 83c5c4113dSnw141292 #endif 84c5c4113dSnw141292 85c5c4113dSnw141292 /* 86c5c4113dSnw141292 * This is needed for mech_krb5 -- we run as daemon, yes, but we want 8778b2cb9aSnw141292 * mech_krb5 to think we're root so it can get host/nodename.fqdn 8878b2cb9aSnw141292 * tickets for us so we can authenticate to AD as the machine account 8978b2cb9aSnw141292 * that we are. For more details look at the entry point in mech_krb5 9078b2cb9aSnw141292 * corresponding to gss_init_sec_context(). 9178b2cb9aSnw141292 * 9278b2cb9aSnw141292 * As a side effect of faking our effective UID to mech_krb5 we will use 9378b2cb9aSnw141292 * root's default ccache (/tmp/krb5cc_0). But if that's created by 9478b2cb9aSnw141292 * another process then we won't have access to it: we run as daemon and 9578b2cb9aSnw141292 * keep PRIV_FILE_DAC_READ, which is insufficient to share the ccache 9678b2cb9aSnw141292 * with others. We putenv("KRB5CCNAME=/var/run/idmap/ccache") in main() 9778b2cb9aSnw141292 * to avoid this issue; see main(). 98c5c4113dSnw141292 * 99c5c4113dSnw141292 * Someday we'll have gss/mech_krb5 extensions for acquiring initiator 100c5c4113dSnw141292 * creds with keytabs/raw keys, and someday we'll have extensions to 101c5c4113dSnw141292 * libsasl to specify creds/name to use on the initiator side, and 102c5c4113dSnw141292 * someday we'll have extensions to libldap to pass those through to 103c5c4113dSnw141292 * libsasl. Until then this interposer will have to do. 104c5c4113dSnw141292 * 105c5c4113dSnw141292 * Also, we have to tell lint to shut up: it thinks app_krb5_user_uid() 106c5c4113dSnw141292 * is defined but not used. 107c5c4113dSnw141292 */ 108c5c4113dSnw141292 /*LINTLIBRARY*/ 109c5c4113dSnw141292 uid_t 110c5c4113dSnw141292 app_krb5_user_uid(void) 111c5c4113dSnw141292 { 112c5c4113dSnw141292 return (0); 113c5c4113dSnw141292 } 114c5c4113dSnw141292 115c5c4113dSnw141292 /*ARGSUSED*/ 116c5c4113dSnw141292 static void 117c5c4113dSnw141292 hup_handler(int sig) { 118c8e26105Sjp151216 hupped = 1; 119c8e26105Sjp151216 if (hup_ev_port >= 0) 120c8e26105Sjp151216 (void) port_send(hup_ev_port, 1, &sig /* any ptr will do */); 121c5c4113dSnw141292 } 122c8e26105Sjp151216 123c5c4113dSnw141292 124c5c4113dSnw141292 /*ARGSUSED*/ 125c5c4113dSnw141292 static void 126c5c4113dSnw141292 term_handler(int sig) { 127c5c4113dSnw141292 (void) idmapdlog(LOG_INFO, "idmapd: Terminating."); 128c5c4113dSnw141292 fini_idmapd(); 129c5c4113dSnw141292 _exit(0); 130c5c4113dSnw141292 } 131c5c4113dSnw141292 132c5c4113dSnw141292 static int pipe_fd = -1; 133c5c4113dSnw141292 134c5c4113dSnw141292 static void 135c5c4113dSnw141292 daemonize_ready(void) { 136c5c4113dSnw141292 char data = '\0'; 137c5c4113dSnw141292 /* 138c5c4113dSnw141292 * wake the parent 139c5c4113dSnw141292 */ 140c5c4113dSnw141292 (void) write(pipe_fd, &data, 1); 141c5c4113dSnw141292 (void) close(pipe_fd); 142c5c4113dSnw141292 } 143c5c4113dSnw141292 144c5c4113dSnw141292 static int 145c5c4113dSnw141292 daemonize_start(void) { 146c5c4113dSnw141292 char data; 147c5c4113dSnw141292 int status; 148c5c4113dSnw141292 int devnull; 149c5c4113dSnw141292 int filedes[2]; 150c5c4113dSnw141292 pid_t pid; 151c5c4113dSnw141292 15278b2cb9aSnw141292 (void) sigset(SIGPIPE, SIG_IGN); 153c5c4113dSnw141292 devnull = open("/dev/null", O_RDONLY); 154c5c4113dSnw141292 if (devnull < 0) 155c5c4113dSnw141292 return (-1); 156c5c4113dSnw141292 (void) dup2(devnull, 0); 157c5c4113dSnw141292 (void) dup2(2, 1); /* stderr only */ 158c5c4113dSnw141292 if (pipe(filedes) < 0) 159c5c4113dSnw141292 return (-1); 160c5c4113dSnw141292 if ((pid = fork1()) < 0) 161c5c4113dSnw141292 return (-1); 162c5c4113dSnw141292 if (pid != 0) { 163c5c4113dSnw141292 /* 164c5c4113dSnw141292 * parent 165c5c4113dSnw141292 */ 166c5c4113dSnw141292 (void) close(filedes[1]); 167c5c4113dSnw141292 if (read(filedes[0], &data, 1) == 1) { 168c5c4113dSnw141292 /* presume success */ 169c5c4113dSnw141292 _exit(0); 170c5c4113dSnw141292 } 171c5c4113dSnw141292 status = -1; 172c5c4113dSnw141292 (void) wait4(pid, &status, 0, NULL); 173c5c4113dSnw141292 if (WIFEXITED(status)) 174c5c4113dSnw141292 _exit(WEXITSTATUS(status)); 175c5c4113dSnw141292 else 176c5c4113dSnw141292 _exit(-1); 177c5c4113dSnw141292 } 178c5c4113dSnw141292 179c5c4113dSnw141292 /* 180c5c4113dSnw141292 * child 181c5c4113dSnw141292 */ 182c5c4113dSnw141292 pipe_fd = filedes[1]; 183c5c4113dSnw141292 (void) close(filedes[0]); 184c5c4113dSnw141292 (void) setsid(); 185c5c4113dSnw141292 (void) umask(0077); 186c5c4113dSnw141292 openlog("idmap", LOG_PID, LOG_DAEMON); 187c5c4113dSnw141292 _idmapdstate.daemon_mode = TRUE; 188c5c4113dSnw141292 return (0); 189c5c4113dSnw141292 } 190c5c4113dSnw141292 191c5c4113dSnw141292 192c5c4113dSnw141292 int 193c5c4113dSnw141292 main(int argc, char **argv) 194c5c4113dSnw141292 { 195c5c4113dSnw141292 int c; 196c5c4113dSnw141292 #ifdef RPC_SVC_FG 197c5c4113dSnw141292 bool_t daemonize = FALSE; 198c5c4113dSnw141292 #else 199c5c4113dSnw141292 bool_t daemonize = TRUE; 200c5c4113dSnw141292 #endif 201c5c4113dSnw141292 202c5c4113dSnw141292 while ((c = getopt(argc, argv, "d")) != EOF) { 203c5c4113dSnw141292 switch (c) { 204c5c4113dSnw141292 case 'd': 205c5c4113dSnw141292 daemonize = FALSE; 206c5c4113dSnw141292 break; 207c5c4113dSnw141292 default: 208c5c4113dSnw141292 break; 209c5c4113dSnw141292 } 210c5c4113dSnw141292 } 211c5c4113dSnw141292 212c5c4113dSnw141292 /* set locale and domain for internationalization */ 213c5c4113dSnw141292 (void) setlocale(LC_ALL, ""); 214c5c4113dSnw141292 (void) textdomain(TEXT_DOMAIN); 215c5c4113dSnw141292 21678b2cb9aSnw141292 if (getzoneid() != GLOBAL_ZONEID) { 217c5c4113dSnw141292 (void) idmapdlog(LOG_ERR, 21878b2cb9aSnw141292 "idmapd: idmapd runs only in the global zone"); 219c5c4113dSnw141292 exit(1); 220c5c4113dSnw141292 } 221c5c4113dSnw141292 222c5c4113dSnw141292 (void) mutex_init(&_svcstate_lock, USYNC_THREAD, NULL); 223c5c4113dSnw141292 224c5c4113dSnw141292 if (daemonize == TRUE) { 225c5c4113dSnw141292 if (daemonize_start() < 0) { 226c5c4113dSnw141292 (void) perror("idmapd: unable to daemonize"); 227c5c4113dSnw141292 exit(-1); 228c5c4113dSnw141292 } 229c5c4113dSnw141292 } else 230c5c4113dSnw141292 (void) umask(0077); 231c5c4113dSnw141292 23284decf41Sjp151216 idmap_init_tsd_key(); 23384decf41Sjp151216 234c5c4113dSnw141292 init_idmapd(); 235c5c4113dSnw141292 23678b2cb9aSnw141292 /* signal handlers that should run only after we're initialized */ 23778b2cb9aSnw141292 (void) sigset(SIGTERM, term_handler); 23878b2cb9aSnw141292 (void) sigset(SIGHUP, hup_handler); 23978b2cb9aSnw141292 240c5c4113dSnw141292 if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, 241c5c4113dSnw141292 DAEMON_UID, DAEMON_GID, 242c5c4113dSnw141292 PRIV_PROC_AUDIT, PRIV_FILE_DAC_READ, 243c5c4113dSnw141292 (char *)NULL) == -1) { 244651c0131Sbaban (void) idmapdlog(LOG_ERR, "idmapd: unable to drop privileges"); 245c5c4113dSnw141292 exit(1); 246c5c4113dSnw141292 } 247c5c4113dSnw141292 248c5c4113dSnw141292 __fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION, 249c5c4113dSnw141292 PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL); 250c5c4113dSnw141292 251c5c4113dSnw141292 if (daemonize == TRUE) 252c5c4113dSnw141292 daemonize_ready(); 253c5c4113dSnw141292 254c5c4113dSnw141292 /* With doors RPC this just wastes this thread, oh well */ 255c5c4113dSnw141292 svc_run(); 256c5c4113dSnw141292 return (0); 257c5c4113dSnw141292 } 258c5c4113dSnw141292 259c5c4113dSnw141292 static void 260c5c4113dSnw141292 init_idmapd() { 261c5c4113dSnw141292 int error; 262d3a612caSnw141292 int connmaxrec = IDMAP_MAX_DOOR_RPC; 263c5c4113dSnw141292 26478b2cb9aSnw141292 /* create directories as root and chown to daemon uid */ 26578b2cb9aSnw141292 if (create_directory(IDMAP_DBDIR, DAEMON_UID, DAEMON_GID) < 0) 26678b2cb9aSnw141292 exit(1); 26778b2cb9aSnw141292 if (create_directory(IDMAP_CACHEDIR, DAEMON_UID, DAEMON_GID) < 0) 26878b2cb9aSnw141292 exit(1); 26978b2cb9aSnw141292 27078b2cb9aSnw141292 /* 27178b2cb9aSnw141292 * Set KRB5CCNAME in the environment. See app_krb5_user_uid() 272e3c2d6aaSnw141292 * for more details. We blow away the existing one, if there is 273e3c2d6aaSnw141292 * one. 27478b2cb9aSnw141292 */ 275e3c2d6aaSnw141292 (void) unlink(IDMAP_CACHEDIR "/ccache"); 27678b2cb9aSnw141292 putenv("KRB5CCNAME=" IDMAP_CACHEDIR "/ccache"); 27778b2cb9aSnw141292 278c5c4113dSnw141292 memset(&_idmapdstate, 0, sizeof (_idmapdstate)); 279c5c4113dSnw141292 280c5c4113dSnw141292 if (sysinfo(SI_HOSTNAME, _idmapdstate.hostname, 281c5c4113dSnw141292 sizeof (_idmapdstate.hostname)) == -1) { 282c5c4113dSnw141292 error = errno; 283c5c4113dSnw141292 idmapdlog(LOG_ERR, 284c5c4113dSnw141292 "idmapd: unable to determine hostname, error: %d", 285c5c4113dSnw141292 error); 286c5c4113dSnw141292 exit(1); 287c5c4113dSnw141292 } 288c5c4113dSnw141292 289*e8c27ec8Sbaban if ((error = init_mapping_system()) < 0) { 290c5c4113dSnw141292 idmapdlog(LOG_ERR, 291c5c4113dSnw141292 "idmapd: unable to initialize mapping system"); 292*e8c27ec8Sbaban exit(error < -2 ? SMF_EXIT_ERR_CONFIG : 1); 293c5c4113dSnw141292 } 294c5c4113dSnw141292 295d3a612caSnw141292 xprt = svc_door_create(idmap_prog_1, IDMAP_PROG, IDMAP_V1, connmaxrec); 296c5c4113dSnw141292 if (xprt == NULL) { 297c5c4113dSnw141292 idmapdlog(LOG_ERR, 298c5c4113dSnw141292 "idmapd: unable to create door RPC service"); 299c5c4113dSnw141292 goto errout; 300c5c4113dSnw141292 } 301c5c4113dSnw141292 3028e228215Sdm199847 if (!svc_control(xprt, SVCSET_CONNMAXREC, &connmaxrec)) { 3038e228215Sdm199847 idmapdlog(LOG_ERR, 3048e228215Sdm199847 "idmapd: unable to limit RPC request size"); 3058e228215Sdm199847 goto errout; 3068e228215Sdm199847 } 3078e228215Sdm199847 308c5c4113dSnw141292 dfd = xprt->xp_fd; 309c5c4113dSnw141292 310c5c4113dSnw141292 if (dfd == -1) { 311c5c4113dSnw141292 idmapdlog(LOG_ERR, "idmapd: unable to register door"); 312c5c4113dSnw141292 goto errout; 313c5c4113dSnw141292 } 314c5c4113dSnw141292 if ((error = idmap_reg(dfd)) != 0) { 315c5c4113dSnw141292 idmapdlog(LOG_ERR, "idmapd: unable to register door (%s)", 316c5c4113dSnw141292 strerror(error)); 317c5c4113dSnw141292 goto errout; 318c5c4113dSnw141292 } 319c5c4113dSnw141292 320c5c4113dSnw141292 if ((error = allocids(_idmapdstate.new_eph_db, 321c5c4113dSnw141292 8192, &_idmapdstate.next_uid, 322c5c4113dSnw141292 8192, &_idmapdstate.next_gid)) != 0) { 323c5c4113dSnw141292 idmapdlog(LOG_ERR, "idmapd: unable to allocate ephemeral IDs " 324c5c4113dSnw141292 "(%s)", strerror(error)); 325c5c4113dSnw141292 _idmapdstate.next_uid = _idmapdstate.limit_uid = SENTINEL_PID; 326c5c4113dSnw141292 _idmapdstate.next_gid = _idmapdstate.limit_gid = SENTINEL_PID; 327c5c4113dSnw141292 } else { 328c5c4113dSnw141292 _idmapdstate.limit_uid = _idmapdstate.next_uid + 8192; 329c5c4113dSnw141292 _idmapdstate.limit_gid = _idmapdstate.next_gid + 8192; 330c5c4113dSnw141292 } 331c5c4113dSnw141292 332c5c4113dSnw141292 print_idmapdstate(); 333c5c4113dSnw141292 334c5c4113dSnw141292 return; 335c5c4113dSnw141292 336c5c4113dSnw141292 errout: 337c5c4113dSnw141292 fini_idmapd(); 338c5c4113dSnw141292 exit(1); 339c5c4113dSnw141292 } 340c5c4113dSnw141292 341c5c4113dSnw141292 static void 342c5c4113dSnw141292 fini_idmapd() { 343c5c4113dSnw141292 idmap_unreg(dfd); 344c5c4113dSnw141292 fini_mapping_system(); 345c5c4113dSnw141292 if (xprt != NULL) 346c5c4113dSnw141292 svc_destroy(xprt); 347c5c4113dSnw141292 } 348c5c4113dSnw141292 349c5c4113dSnw141292 void 350c5c4113dSnw141292 idmapdlog(int pri, const char *format, ...) { 351c5c4113dSnw141292 va_list args; 352c5c4113dSnw141292 353c5c4113dSnw141292 va_start(args, format); 354c5c4113dSnw141292 if (_idmapdstate.daemon_mode == FALSE) { 355c5c4113dSnw141292 (void) vfprintf(stderr, format, args); 356c5c4113dSnw141292 (void) fprintf(stderr, "\n"); 357c5c4113dSnw141292 } 358c5c4113dSnw141292 (void) vsyslog(pri, format, args); 359c5c4113dSnw141292 va_end(args); 360c5c4113dSnw141292 } 361