17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 545916cd2Sjpk * Common Development and Distribution License (the "License"). 645916cd2Sjpk * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*bf1e3beeSmichen * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Simple doors name server cache daemon 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <stdio.h> 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <fcntl.h> 35cb5caa98Sdjl #include <string.h> 36cb5caa98Sdjl #include <errno.h> 37cb5caa98Sdjl #include <stdarg.h> 38cb5caa98Sdjl #include <locale.h> 39*bf1e3beeSmichen #include <sys/stat.h> 4045916cd2Sjpk #include <tsol/label.h> 4145916cd2Sjpk #include <zone.h> 42cb5caa98Sdjl #include "cache.h" 43cb5caa98Sdjl #include "nscd_log.h" 44cb5caa98Sdjl #include "nscd_selfcred.h" 45cb5caa98Sdjl #include "nscd_frontend.h" 46cb5caa98Sdjl #include "nscd_common.h" 47cb5caa98Sdjl #include "nscd_admin.h" 48cb5caa98Sdjl #include "nscd_door.h" 49cb5caa98Sdjl #include "nscd_switch.h" 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate extern int optind; 527c478bd9Sstevel@tonic-gate extern int opterr; 537c478bd9Sstevel@tonic-gate extern int optopt; 547c478bd9Sstevel@tonic-gate extern char *optarg; 557c478bd9Sstevel@tonic-gate 56cb5caa98Sdjl #define NSCDOPT "S:Kf:c:ge:p:n:i:l:d:s:h:o:GFR" 57cb5caa98Sdjl 58cb5caa98Sdjl /* assume this is a single nscd or, if multiple, the main nscd */ 59cb5caa98Sdjl int _whoami = NSCD_MAIN; 60cb5caa98Sdjl int _doorfd = -1; 61cb5caa98Sdjl extern int _logfd; 62cb5caa98Sdjl static char *cfgfile = NULL; 63cb5caa98Sdjl 64cb5caa98Sdjl extern nsc_ctx_t *cache_ctx_p[]; 65cb5caa98Sdjl 667c478bd9Sstevel@tonic-gate static void usage(char *); 677c478bd9Sstevel@tonic-gate static void detachfromtty(void); 687c478bd9Sstevel@tonic-gate 69cb5caa98Sdjl static int debug_level = 0; 70cb5caa98Sdjl static char logfile[128] = { 0 }; 717c478bd9Sstevel@tonic-gate static int will_become_server; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate static char * 747c478bd9Sstevel@tonic-gate getcacheopt(char *s) 757c478bd9Sstevel@tonic-gate { 767c478bd9Sstevel@tonic-gate while (*s && *s != ',') 777c478bd9Sstevel@tonic-gate s++; 787c478bd9Sstevel@tonic-gate return ((*s == ',') ? (s + 1) : NULL); 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* 827c478bd9Sstevel@tonic-gate * declaring this causes the files backend to use hashing 837c478bd9Sstevel@tonic-gate * this is of course an utter hack, but provides a nice 847c478bd9Sstevel@tonic-gate * quiet back door to enable this feature for only the nscd. 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate void 877c478bd9Sstevel@tonic-gate __nss_use_files_hash(void) 887c478bd9Sstevel@tonic-gate { 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate 91cb5caa98Sdjl static int saved_argc = 0; 92cb5caa98Sdjl static char **saved_argv = NULL; 937c478bd9Sstevel@tonic-gate static char saved_execname[MAXPATHLEN]; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate static void 967c478bd9Sstevel@tonic-gate save_execname() 977c478bd9Sstevel@tonic-gate { 987c478bd9Sstevel@tonic-gate const char *name = getexecname(); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate saved_execname[0] = 0; 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate if (name[0] != '/') { /* started w/ relative path */ 1037c478bd9Sstevel@tonic-gate (void) getcwd(saved_execname, MAXPATHLEN); 104cb5caa98Sdjl (void) strlcat(saved_execname, "/", MAXPATHLEN); 1057c478bd9Sstevel@tonic-gate } 106cb5caa98Sdjl (void) strlcat(saved_execname, name, MAXPATHLEN); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 109f166393fSesolom int 1107c478bd9Sstevel@tonic-gate main(int argc, char ** argv) 1117c478bd9Sstevel@tonic-gate { 1127c478bd9Sstevel@tonic-gate int opt; 1137c478bd9Sstevel@tonic-gate int errflg = 0; 1147c478bd9Sstevel@tonic-gate int showstats = 0; 1157c478bd9Sstevel@tonic-gate int doset = 0; 116cb5caa98Sdjl nscd_rc_t rc; 117cb5caa98Sdjl char *me = "main()"; 118cb5caa98Sdjl char *ret_locale; 119cb5caa98Sdjl char *ret_textdomain; 120cb5caa98Sdjl char msg[128]; 121cb5caa98Sdjl 122cb5caa98Sdjl ret_locale = setlocale(LC_ALL, ""); 123cb5caa98Sdjl if (ret_locale == NULL) 124cb5caa98Sdjl (void) fprintf(stderr, gettext("Unable to set locale\n")); 125cb5caa98Sdjl 126cb5caa98Sdjl ret_textdomain = textdomain(TEXT_DOMAIN); 127cb5caa98Sdjl if (ret_textdomain == NULL) 128cb5caa98Sdjl (void) fprintf(stderr, gettext("Unable to set textdomain\n")); 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate /* 13145916cd2Sjpk * The admin model for TX is that labeled zones are managed 13245916cd2Sjpk * in global zone where most trusted configuration database 133*bf1e3beeSmichen * resides. However, nscd will run in any labeled zone if 134*bf1e3beeSmichen * file /var/tsol/doors/nscd_per_label exists. 13545916cd2Sjpk */ 13645916cd2Sjpk if (is_system_labeled() && (getzoneid() != GLOBAL_ZONEID)) { 137*bf1e3beeSmichen struct stat sbuf; 138*bf1e3beeSmichen if (stat(TSOL_NSCD_PER_LABEL_FILE, &sbuf) < 0) { 13945916cd2Sjpk (void) fprintf(stderr, 140*bf1e3beeSmichen gettext("With Trusted Extensions nscd runs only in the " 141*bf1e3beeSmichen "global zone (if nscd_per_label flag not set)\n")); 14245916cd2Sjpk exit(1); 14345916cd2Sjpk } 144*bf1e3beeSmichen } 14545916cd2Sjpk 14645916cd2Sjpk /* 1477c478bd9Sstevel@tonic-gate * Special case non-root user here - he can just print stats 1487c478bd9Sstevel@tonic-gate */ 1497c478bd9Sstevel@tonic-gate if (geteuid()) { 150cb5caa98Sdjl if (argc != 2 || 151cb5caa98Sdjl (strcmp(argv[1], "-g") && strcmp(argv[1], "-G"))) { 1527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 153cb5caa98Sdjl gettext("Must be root to use any option other than -g\n\n")); 1547c478bd9Sstevel@tonic-gate usage(argv[0]); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 157cb5caa98Sdjl if (_nscd_doorcall(NSCD_PING) != NSS_SUCCESS) { 1587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 159cb5caa98Sdjl gettext("%s doesn't appear to be running.\n"), 160cb5caa98Sdjl argv[0]); 1617c478bd9Sstevel@tonic-gate exit(1); 1627c478bd9Sstevel@tonic-gate } 163cb5caa98Sdjl if (_nscd_client_getadmin(argv[1][1]) != 0) { 164cb5caa98Sdjl (void) fprintf(stderr, 165cb5caa98Sdjl gettext("unable to get configuration and statistics data\n")); 166cb5caa98Sdjl exit(1); 167cb5caa98Sdjl } 168cb5caa98Sdjl 169cb5caa98Sdjl _nscd_client_showstats(); 1707c478bd9Sstevel@tonic-gate exit(0); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 173cb5caa98Sdjl /* 174cb5caa98Sdjl * Determine if there is already a daemon (main nscd) running. 175cb5caa98Sdjl * If not, will start it. Forker NSCD will always become a 176cb5caa98Sdjl * daemon. 177cb5caa98Sdjl */ 178cb5caa98Sdjl will_become_server = (_nscd_doorcall(NSCD_PING) != NSS_SUCCESS); 179cb5caa98Sdjl if (argc >= 2 && strcmp(argv[1], "-F") == 0) { 180cb5caa98Sdjl will_become_server = 1; 181cb5caa98Sdjl _whoami = NSCD_FORKER; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* 184cb5caa98Sdjl * allow time for the main nscd to get ready 185cb5caa98Sdjl * to receive the IMHERE door request this 186cb5caa98Sdjl * process will send later 1877c478bd9Sstevel@tonic-gate */ 188cb5caa98Sdjl (void) usleep(100000); 189cb5caa98Sdjl } 1907c478bd9Sstevel@tonic-gate 191cb5caa98Sdjl /* 192cb5caa98Sdjl * first get the config file path. Also detect 193cb5caa98Sdjl * invalid option as soon as possible. 194cb5caa98Sdjl */ 195cb5caa98Sdjl while ((opt = getopt(argc, argv, NSCDOPT)) != EOF) { 196cb5caa98Sdjl switch (opt) { 197cb5caa98Sdjl 198cb5caa98Sdjl case 'f': 199cb5caa98Sdjl if ((cfgfile = strdup(optarg)) == NULL) 200cb5caa98Sdjl exit(1); 201cb5caa98Sdjl break; 202cb5caa98Sdjl case 'g': 203cb5caa98Sdjl if (will_become_server) { 204cb5caa98Sdjl (void) fprintf(stderr, 205cb5caa98Sdjl gettext("nscd not running, no statistics to show\n\n")); 206cb5caa98Sdjl errflg++; 207cb5caa98Sdjl } 208cb5caa98Sdjl break; 209cb5caa98Sdjl case 'i': 210cb5caa98Sdjl if (will_become_server) { 211cb5caa98Sdjl (void) fprintf(stderr, 212cb5caa98Sdjl gettext("nscd not running, no cache to invalidate\n\n")); 213cb5caa98Sdjl errflg++; 214cb5caa98Sdjl } 215cb5caa98Sdjl break; 216cb5caa98Sdjl 217cb5caa98Sdjl case '?': 218cb5caa98Sdjl errflg++; 219cb5caa98Sdjl break; 220cb5caa98Sdjl } 221cb5caa98Sdjl 222cb5caa98Sdjl } 223cb5caa98Sdjl if (errflg) 224cb5caa98Sdjl usage(argv[0]); 225cb5caa98Sdjl 226cb5caa98Sdjl /* 227cb5caa98Sdjl * perform more initialization and load configuration 228cb5caa98Sdjl * if to become server 229cb5caa98Sdjl */ 230cb5caa98Sdjl if (will_become_server) { 231cb5caa98Sdjl 232cb5caa98Sdjl /* initialize switch engine and config/stats management */ 233cb5caa98Sdjl if ((rc = _nscd_init(cfgfile)) != NSCD_SUCCESS) { 234cb5caa98Sdjl (void) fprintf(stderr, 235cb5caa98Sdjl gettext("initialization of switch failed (rc = %d)\n"), rc); 236cb5caa98Sdjl exit(1); 237cb5caa98Sdjl } 238cb5caa98Sdjl 239cb5caa98Sdjl /* 240cb5caa98Sdjl * initialize cache store 241cb5caa98Sdjl */ 242cb5caa98Sdjl if ((rc = init_cache(0)) != NSCD_SUCCESS) { 243cb5caa98Sdjl (void) fprintf(stderr, 244cb5caa98Sdjl gettext("initialization of cache store failed (rc = %d)\n"), rc); 245cb5caa98Sdjl exit(1); 246cb5caa98Sdjl } 247cb5caa98Sdjl } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate /* 2507c478bd9Sstevel@tonic-gate * process usual options 2517c478bd9Sstevel@tonic-gate */ 252cb5caa98Sdjl optind = 1; /* this is a rescan */ 253cb5caa98Sdjl *msg = '\0'; 254cb5caa98Sdjl while ((opt = getopt(argc, argv, NSCDOPT)) != EOF) { 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate switch (opt) { 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate case 'K': /* undocumented feature */ 259cb5caa98Sdjl (void) _nscd_doorcall(NSCD_KILLSERVER); 2607c478bd9Sstevel@tonic-gate exit(0); 2617c478bd9Sstevel@tonic-gate break; 2627c478bd9Sstevel@tonic-gate 263cb5caa98Sdjl case 'G': 2647c478bd9Sstevel@tonic-gate case 'g': 2657c478bd9Sstevel@tonic-gate showstats++; 2667c478bd9Sstevel@tonic-gate break; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate case 'p': 2697c478bd9Sstevel@tonic-gate doset++; 270cb5caa98Sdjl if (_nscd_add_admin_mod(optarg, 'p', 271cb5caa98Sdjl getcacheopt(optarg), 272cb5caa98Sdjl msg, sizeof (msg)) == -1) 2737c478bd9Sstevel@tonic-gate errflg++; 2747c478bd9Sstevel@tonic-gate break; 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate case 'n': 2777c478bd9Sstevel@tonic-gate doset++; 278cb5caa98Sdjl if (_nscd_add_admin_mod(optarg, 'n', 279cb5caa98Sdjl getcacheopt(optarg), 280cb5caa98Sdjl msg, sizeof (msg)) == -1) 2817c478bd9Sstevel@tonic-gate errflg++; 2827c478bd9Sstevel@tonic-gate break; 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate case 'c': 2857c478bd9Sstevel@tonic-gate doset++; 286cb5caa98Sdjl if (_nscd_add_admin_mod(optarg, 'c', 287cb5caa98Sdjl getcacheopt(optarg), 288cb5caa98Sdjl msg, sizeof (msg)) == -1) 2897c478bd9Sstevel@tonic-gate errflg++; 2907c478bd9Sstevel@tonic-gate break; 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate case 'i': 2937c478bd9Sstevel@tonic-gate doset++; 294cb5caa98Sdjl if (_nscd_add_admin_mod(optarg, 'i', NULL, 295cb5caa98Sdjl msg, sizeof (msg)) == -1) 2967c478bd9Sstevel@tonic-gate errflg++; 2977c478bd9Sstevel@tonic-gate break; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate case 'l': 3007c478bd9Sstevel@tonic-gate doset++; 301cb5caa98Sdjl (void) strlcpy(logfile, optarg, 128); 302cb5caa98Sdjl (void) _nscd_add_admin_mod(NULL, 'l', optarg, 303cb5caa98Sdjl msg, sizeof (msg)); 3047c478bd9Sstevel@tonic-gate break; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate case 'd': 3077c478bd9Sstevel@tonic-gate doset++; 308cb5caa98Sdjl debug_level = atoi(optarg); 309cb5caa98Sdjl (void) _nscd_add_admin_mod(NULL, 'd', optarg, 310cb5caa98Sdjl msg, sizeof (msg)); 311cb5caa98Sdjl break; 312cb5caa98Sdjl 313cb5caa98Sdjl case 'S': 314cb5caa98Sdjl /* silently ignore secure-mode */ 3157c478bd9Sstevel@tonic-gate break; 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate case 's': 318cb5caa98Sdjl /* silently ignore suggested-size */ 3197c478bd9Sstevel@tonic-gate break; 3207c478bd9Sstevel@tonic-gate 321cb5caa98Sdjl case 'o': 322cb5caa98Sdjl /* silently ignore old-data-ok */ 3237c478bd9Sstevel@tonic-gate break; 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate case 'h': 3267c478bd9Sstevel@tonic-gate doset++; 327cb5caa98Sdjl if (_nscd_add_admin_mod(optarg, 'h', 328cb5caa98Sdjl getcacheopt(optarg), 329cb5caa98Sdjl msg, sizeof (msg)) == -1) 3307c478bd9Sstevel@tonic-gate errflg++; 3317c478bd9Sstevel@tonic-gate break; 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate case 'e': 3347c478bd9Sstevel@tonic-gate doset++; 335cb5caa98Sdjl if (_nscd_add_admin_mod(optarg, 'e', 336cb5caa98Sdjl getcacheopt(optarg), 337cb5caa98Sdjl msg, sizeof (msg)) == -1) 3387c478bd9Sstevel@tonic-gate errflg++; 3397c478bd9Sstevel@tonic-gate break; 340cb5caa98Sdjl 341cb5caa98Sdjl case 'F': 342cb5caa98Sdjl _whoami = NSCD_FORKER; 3437c478bd9Sstevel@tonic-gate break; 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate default: 3467c478bd9Sstevel@tonic-gate errflg++; 3477c478bd9Sstevel@tonic-gate break; 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate 352cb5caa98Sdjl if (errflg) { 353cb5caa98Sdjl if (*msg != '\0') 354cb5caa98Sdjl (void) fprintf(stderr, "\n%s: %s\n\n", argv[0], msg); 3557c478bd9Sstevel@tonic-gate usage(argv[0]); 356cb5caa98Sdjl } 3577c478bd9Sstevel@tonic-gate 358cb5caa98Sdjl /* 359cb5caa98Sdjl * if main nscd already running and not forker nscd, 360cb5caa98Sdjl * can only do admin work 361cb5caa98Sdjl */ 362cb5caa98Sdjl if (_whoami == NSCD_MAIN) { 3637c478bd9Sstevel@tonic-gate if (!will_become_server) { 3647c478bd9Sstevel@tonic-gate if (showstats) { 365cb5caa98Sdjl if (_nscd_client_getadmin('g')) { 366cb5caa98Sdjl (void) fprintf(stderr, 367cb5caa98Sdjl gettext("Cannot contact nscd properly(?)\n")); 368cb5caa98Sdjl exit(1); 369cb5caa98Sdjl } 370cb5caa98Sdjl _nscd_client_showstats(); 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate if (doset) { 374cb5caa98Sdjl if (_nscd_client_setadmin() < 0) { 3757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 376cb5caa98Sdjl gettext("Error during admin call\n")); 3777c478bd9Sstevel@tonic-gate exit(1); 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate if (!showstats && !doset) { 3817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 382cb5caa98Sdjl gettext("%s already running.... no administration option specified\n"), 3837c478bd9Sstevel@tonic-gate argv[0]); 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate exit(0); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate 389cb5caa98Sdjl /* 390cb5caa98Sdjl * daemon from here on 391cb5caa98Sdjl */ 392cb5caa98Sdjl 393cb5caa98Sdjl if (_whoami == NSCD_MAIN) { 394cb5caa98Sdjl 395cb5caa98Sdjl /* save enough info in case need to restart or fork */ 396cb5caa98Sdjl saved_argc = argc; 3977c478bd9Sstevel@tonic-gate saved_argv = argv; 3987c478bd9Sstevel@tonic-gate save_execname(); 3997c478bd9Sstevel@tonic-gate 400cb5caa98Sdjl /* 401cb5caa98Sdjl * if a log file is not specified, set it to 402cb5caa98Sdjl * "stderr" or "/dev/null" based on debug level 403cb5caa98Sdjl */ 404cb5caa98Sdjl if (_logfd < 0 && *logfile == '\0') { 405cb5caa98Sdjl if (debug_level != 0) 4067c478bd9Sstevel@tonic-gate /* we're debugging... */ 407cb5caa98Sdjl (void) strcpy(logfile, "stderr"); 4087c478bd9Sstevel@tonic-gate else 409cb5caa98Sdjl (void) strcpy(logfile, "/dev/null"); 410cb5caa98Sdjl 411cb5caa98Sdjl (void) _nscd_add_admin_mod(NULL, 'l', logfile, 412cb5caa98Sdjl msg, sizeof (msg)); 413cb5caa98Sdjl } 414cb5caa98Sdjl 415cb5caa98Sdjl /* activate command options */ 416cb5caa98Sdjl if (_nscd_server_setadmin(NULL) != NSCD_SUCCESS) { 417cb5caa98Sdjl (void) fprintf(stderr, 418cb5caa98Sdjl gettext("unable to set command line options\n")); 419cb5caa98Sdjl exit(1); 420cb5caa98Sdjl } 421cb5caa98Sdjl 422cb5caa98Sdjl if (debug_level) { 423cb5caa98Sdjl /* we're debugging, no forking of nscd */ 424cb5caa98Sdjl 425cb5caa98Sdjl /* 426cb5caa98Sdjl * forker nscd will be started if self credential 427cb5caa98Sdjl * is configured 428cb5caa98Sdjl */ 429cb5caa98Sdjl _nscd_start_forker(saved_execname, saved_argc, 430cb5caa98Sdjl saved_argv); 4317c478bd9Sstevel@tonic-gate } else { 432cb5caa98Sdjl /* 433cb5caa98Sdjl * daemonize the nscd (forker nscd will also 434cb5caa98Sdjl * be started if self credential is configured) 435cb5caa98Sdjl */ 4367c478bd9Sstevel@tonic-gate detachfromtty(); 4377c478bd9Sstevel@tonic-gate } 438cb5caa98Sdjl } else { /* NSCD_FORKER */ 439cb5caa98Sdjl (void) open("/dev/null", O_RDWR, 0); 440cb5caa98Sdjl (void) dup(0); 441cb5caa98Sdjl if (_logfd != 2) 442cb5caa98Sdjl (void) dup(0); 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate 445cb5caa98Sdjl /* set up door and establish our own server thread pool */ 446cb5caa98Sdjl if ((_doorfd = _nscd_setup_server(saved_execname, saved_argv)) == -1) { 447cb5caa98Sdjl _NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR) 448cb5caa98Sdjl (me, "unable to set up door\n"); 4497c478bd9Sstevel@tonic-gate exit(1); 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate 452cb5caa98Sdjl /* inform the main nscd that this forker is ready */ 453cb5caa98Sdjl if (_whoami == NSCD_FORKER) { 454cb5caa98Sdjl int ret; 4557c478bd9Sstevel@tonic-gate 456cb5caa98Sdjl for (ret = NSS_ALTRETRY; ret == NSS_ALTRETRY; ) 457cb5caa98Sdjl ret = _nscd_doorcall_sendfd(_doorfd, 458cb5caa98Sdjl NSCD_IMHERE | (NSCD_FORKER & NSCD_WHOAMI), 459cb5caa98Sdjl NULL, 0, NULL); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate for (;;) { 4637c478bd9Sstevel@tonic-gate (void) pause(); 464cb5caa98Sdjl (void) _nscd_doorcall(NSCD_REFRESH); 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate 467cb5caa98Sdjl /* NOTREACHED */ 468cb5caa98Sdjl /*LINTED E_FUNC_HAS_NO_RETURN_STMT*/ 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate static void 4727c478bd9Sstevel@tonic-gate usage(char *s) 4737c478bd9Sstevel@tonic-gate { 4747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4757c478bd9Sstevel@tonic-gate "Usage: %s [-d debug_level] [-l logfilename]\n", s); 4767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4777c478bd9Sstevel@tonic-gate " [-p cachename,positive_time_to_live]\n"); 4787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4797c478bd9Sstevel@tonic-gate " [-n cachename,negative_time_to_live]\n"); 4807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 481cb5caa98Sdjl " [-i cachename]\n"); 4827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 483cb5caa98Sdjl " [-h cachename,keep_hot_count]\n"); 4847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4857c478bd9Sstevel@tonic-gate " [-e cachename,\"yes\"|\"no\"] [-g] " \ 4867c478bd9Sstevel@tonic-gate "[-c cachename,\"yes\"|\"no\"]\n"); 4877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4887c478bd9Sstevel@tonic-gate " [-f configfilename] \n"); 4897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 490cb5caa98Sdjl "\n Supported caches:\n"); 4917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 492cb5caa98Sdjl " audit_user, auth_attr, bootparams, ethers\n"); 493cb5caa98Sdjl (void) fprintf(stderr, 494cb5caa98Sdjl " exec_attr, group, hosts, ipnodes, netmasks\n"); 495cb5caa98Sdjl (void) fprintf(stderr, 496cb5caa98Sdjl " networks, passwd, printers, prof_attr, project\n"); 497cb5caa98Sdjl (void) fprintf(stderr, 498cb5caa98Sdjl " protocols, rpc, services, tnrhtp, tnrhdb\n"); 499cb5caa98Sdjl (void) fprintf(stderr, 500cb5caa98Sdjl " user_attr\n"); 5017c478bd9Sstevel@tonic-gate exit(1); 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate /* 5057c478bd9Sstevel@tonic-gate * detach from tty 5067c478bd9Sstevel@tonic-gate */ 5077c478bd9Sstevel@tonic-gate static void 5087c478bd9Sstevel@tonic-gate detachfromtty(void) 5097c478bd9Sstevel@tonic-gate { 510cb5caa98Sdjl nscd_rc_t rc; 511cb5caa98Sdjl char *me = "detachfromtty"; 512cb5caa98Sdjl 513cb5caa98Sdjl if (_logfd > 0) { 5147c478bd9Sstevel@tonic-gate int i; 515cb5caa98Sdjl for (i = 0; i < _logfd; i++) 5167c478bd9Sstevel@tonic-gate (void) close(i); 517cb5caa98Sdjl closefrom(_logfd + 1); 5187c478bd9Sstevel@tonic-gate } else 5197c478bd9Sstevel@tonic-gate closefrom(0); 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate (void) chdir("/"); 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate switch (fork1()) { 5247c478bd9Sstevel@tonic-gate case (pid_t)-1: 525cb5caa98Sdjl 526cb5caa98Sdjl _NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR) 527cb5caa98Sdjl (me, "unable to fork: pid = %d, %s\n", 528cb5caa98Sdjl getpid(), strerror(errno)); 529cb5caa98Sdjl 5307c478bd9Sstevel@tonic-gate exit(1); 5317c478bd9Sstevel@tonic-gate break; 5327c478bd9Sstevel@tonic-gate case 0: 533cb5caa98Sdjl /* start the forker nscd if so configured */ 534cb5caa98Sdjl _nscd_start_forker(saved_execname, saved_argc, saved_argv); 5357c478bd9Sstevel@tonic-gate break; 5367c478bd9Sstevel@tonic-gate default: 5377c478bd9Sstevel@tonic-gate exit(0); 5387c478bd9Sstevel@tonic-gate } 5397c478bd9Sstevel@tonic-gate (void) setsid(); 5407c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_RDWR, 0); 5417c478bd9Sstevel@tonic-gate (void) dup(0); 542cb5caa98Sdjl if (_logfd != 2) 5437c478bd9Sstevel@tonic-gate (void) dup(0); 544cb5caa98Sdjl 545cb5caa98Sdjl /* 546cb5caa98Sdjl * start monitoring the states of the name service clients 547cb5caa98Sdjl */ 548cb5caa98Sdjl rc = _nscd_init_smf_monitor(); 549cb5caa98Sdjl if (rc != NSCD_SUCCESS) { 550cb5caa98Sdjl _NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR) 551cb5caa98Sdjl (me, "unable to start the SMF monitor (rc = %d)\n", rc); 552cb5caa98Sdjl 553cb5caa98Sdjl exit(-1); 554cb5caa98Sdjl } 5557c478bd9Sstevel@tonic-gate } 556