server.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Simple doors name server cache daemon
*/
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <thread.h>
#include <stdarg.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <memory.h>
#include <resolv.h>
#include <door.h>
#include "getxby_door.h"
#include "server_door.h"
#include "nscd.h"
/* Includes for filenames of databases */
#include <shadow.h>
#include <userdefs.h>
#include <netdb.h>
#include <nss_dbdefs.h>
#include <exec_attr.h>
#include <prof_attr.h>
#include <user_attr.h>
#include <ucred.h>
#include <priv.h>
#include <libscf.h>
extern int optind;
extern int opterr;
extern int optopt;
extern char *optarg;
static void rts_mon(void);
static void usage(char *);
static int nsc_calllen(nsc_call_t *);
static int client_getadmin(admin_t *);
static void client_killserver(void);
static int client_setadmin(admin_t *);
static void client_showstats(admin_t *);
static void detachfromtty(void);
static int will_become_server;
void
{
while (1) {
logit("reaper_%s: %d entries in cache\n",
}
if (admin_ptr->nsc_entries > 0) {
logit("reaper_%s: reaped %d entries\n",
}
} else {
/*
* We set a minimum wait of 60 before checking again;
* we don't want to sleep for no time at all.
* We don't clamp it for the reaping itself, that is
* done in reap_hash, and with a different minimum.
*/
"reaper_%s: Nothing to reap, sleep %d\n",
}
}
}
}
getcacheptr(char *s)
{
"exec_attr", "prof_attr", "user_attr" };
return (¤t_admin.passwd);
return (¤t_admin.group);
return (¤t_admin.host);
return (¤t_admin.node);
return (¤t_admin.exec);
return (¤t_admin.prof);
return (¤t_admin.user);
return (NULL);
}
static char *
getcacheopt(char *s)
{
while (*s && *s != ',')
s++;
}
/*
* routine to check if server is already running
*/
static int
nsc_ping(void)
{
int ndata;
int adata;
}
static void
dozip(void)
{
/* not much here */
}
static void
keep_open_dns_socket(void)
{
}
/*
* declaring this causes the files backend to use hashing
* this is of course an utter hack, but provides a nice
* quiet back door to enable this feature for only the nscd.
*/
void
__nss_use_files_hash(void)
{
}
/*
*
* The allocation of resources for cache lookups is an interesting
* problem, and one that has caused several bugs in the beta release
* of 2.5. In particular, the introduction of a thottle to prevent
* the creation of excessive numbers of LWPs in the case of a failed
* name service has led to a denial of service problem when the
* name service request rate exceeds the name service's ability
* to respond. As a result, I'm implementing the following
* algorithm:
*
* 1) We cap the number of total threads.
* 2) We save CACHE_THREADS of those for cache lookups only.
* 3) We use a common pool of 2/3 of the remain threads that are used first
* 4) We save the remainder and allocate 1/3 of it for table specific lookups
*
* The intent is to prevent the failure of a single name service from
* causing denial of service, and to always have threads available for
* cached lookups. If a request comes in and the answer isn't in the
* cache and we cannot get a thread, we simply return NOSERVER, forcing
* the client to lookup the
* data itself. This will prevent the types of starvation seen
* at UNC due to a single threaded DNS backend, and allows the cache
* to eventually become filled.
*
*/
/* 7 tables: passwd, group, hosts, ipnodes, exec_attr, prof_attr, user_attr */
#define NSCD_TABLES 7
#define TABLE_THREADS 10
#define COMMON_THREADS 20
#define CACHE_HIT_THREADS 20
static sema_t common_sema;
static sema_t passwd_sema;
static sema_t hosts_sema;
static sema_t nodes_sema;
static sema_t group_sema;
static thread_key_t lookup_state_key;
static void
{
}
int
get_clearance(int callnumber)
{
char *tab;
if (sema_trywait(&common_sema) == 0) {
return (0);
}
switch (MASKUPDATEBIT(callnumber)) {
case GETPWUID:
case GETPWNAM:
tab = "passwd";
break;
case GETGRNAM:
case GETGRGID:
tab = "group";
table_sema = &group_sema;
break;
case GETHOSTBYNAME:
case GETHOSTBYADDR:
tab = "hosts";
table_sema = &hosts_sema;
break;
case GETIPNODEBYNAME:
case GETIPNODEBYADDR:
tab = "ipnodes";
table_sema = &nodes_sema;
break;
case GETEXECID:
tab = "exec_attr";
table_sema = &exec_sema;
break;
case GETPROFNAM:
tab = "prof_attr";
table_sema = &prof_sema;
break;
case GETUSERNAM:
tab = "user_attr";
table_sema = &user_sema;
break;
}
if (sema_trywait(table_sema) == 0) {
return (0);
}
}
return (-1);
}
int
{
int which;
if (which == 0) /* from common pool */ {
(void) sema_post(&common_sema);
return (0);
}
switch (MASKUPDATEBIT(callnumber)) {
case GETPWUID:
case GETPWNAM:
break;
case GETGRNAM:
case GETGRGID:
table_sema = &group_sema;
break;
case GETHOSTBYNAME:
case GETHOSTBYADDR:
table_sema = &hosts_sema;
break;
case GETIPNODEBYNAME:
case GETIPNODEBYADDR:
table_sema = &nodes_sema;
break;
case GETEXECID:
table_sema = &exec_sema;
break;
case GETPROFNAM:
table_sema = &prof_sema;
break;
case GETUSERNAM:
table_sema = &user_sema;
break;
}
(void) sema_post(table_sema);
return (0);
}
static mutex_t create_lock;
static int nscd_max_servers = MAX_SERVER_THREADS;
static int num_servers = 0;
static thread_key_t server_key;
/*
* Bind a TSD value to a server thread. This enables the destructor to
* but better safe than sorry.
*/
/*ARGSUSED*/
static void *
server_tsd_bind(void *arg)
{
static void *value = 0;
/* disable cancellation to avoid hangs if server threads disappear */
/* make lint happy */
return (NULL);
}
/*
* Server threads are created here.
*/
/*ARGSUSED*/
static void
{
(void) mutex_lock(&create_lock);
if (++num_servers > nscd_max_servers) {
num_servers--;
(void) mutex_unlock(&create_lock);
return;
}
(void) mutex_unlock(&create_lock);
NULL);
}
/*
* Server thread are destroyed here
*/
/*ARGSUSED*/
static void
server_destroy(void *arg)
{
(void) mutex_lock(&create_lock);
num_servers--;
(void) mutex_unlock(&create_lock);
}
static char **saved_argv;
static char saved_execname[MAXPATHLEN];
static void
{
const char *name = getexecname();
saved_execname[0] = 0;
}
}
void
{
int did;
int opt;
int errflg = 0;
int showstats = 0;
int doset = 0;
int loaded_config_file = 0;
/*
* Special case non-root user here - he can just print stats
*/
if (geteuid()) {
"Must be root to use any option other than "\
"-g.\n\n");
}
(client_getadmin(¤t_admin) != 0)) {
"%s doesn't appear to be running.\n", argv[0]);
exit(1);
}
exit(0);
}
/*
* Determine if there is already a daemon running
*/
/*
* process usual options
*/
/*
* load normal config file
*/
if (will_become_server) {
static const nsc_stat_t defaults = {
0, /* stats */
0, /* stats */
0, /* stats */
0, /* stats */
0, /* stats */
0, /* stats */
0, /* stats */
211, /* suggested size */
1, /* enabled */
0, /* invalidate cmd */
600, /* positive ttl */
10, /* netative ttl */
20, /* keep hot */
0, /* old data not ok */
1 }; /* check files */
exit(1);
}
}
}
else {
if (client_getadmin(¤t_admin)) {
"Cannot contact nscd properly(?)\n");
exit(1);
}
}
"S:Kf:c:ge:p:n:i:l:d:s:h:o:")) != EOF) {
char *cacheopt;
switch (opt) {
case 'S': /* undocumented feature */
doset++;
errflg++;
break;
}
cache->nsc_secure_mode = 0;
else
errflg++;
break;
case 'K': /* undocumented feature */
exit(0);
break;
case 'f':
doset++;
exit(1);
}
break;
case 'g':
showstats++;
break;
case 'p':
doset++;
errflg++;
break;
}
break;
case 'n':
doset++;
errflg++;
break;
}
break;
case 'c':
doset++;
errflg++;
break;
}
cache->nsc_check_files = 0;
else
errflg++;
break;
case 'i':
doset++;
if (!cache) {
errflg++;
break;
}
break;
case 'l':
doset++;
break;
case 'd':
doset++;
break;
case 's':
doset++;
errflg++;
break;
}
break;
case 'h':
doset++;
errflg++;
break;
}
break;
case 'o':
doset++;
errflg++;
break;
}
cache->nsc_old_data_ok = 0;
else
errflg++;
break;
case 'e':
doset++;
errflg++;
break;
}
cache->nsc_enabled = 0;
else
errflg++;
break;
default:
errflg++;
break;
}
}
if (errflg)
if (!will_become_server) {
if (showstats) {
}
if (doset) {
if (client_setadmin(¤t_admin) < 0) {
"Error during admin call\n");
exit(1);
}
}
"%s already running.... no admin specified\n",
argv[0]);
}
exit(0);
}
/*
* daemon from here ou
*/
if (!loaded_config_file) {
"not present\n");
exit(1);
}
saved_argv = argv;
if (current_admin.debug_level) {
/* we're debugging... */
/* no specified log file */
else
(void) nscd_set_lf(¤t_admin,
} else {
}
/* perform some initialization */
getpw_init();
getgr_init();
gethost_init();
getnode_init();
getexec_init();
getprof_init();
getuser_init();
/* Establish our own server thread pool */
perror("thr_keycreate");
exit(-1);
}
/* Create a door */
perror("door_create");
exit(-1);
}
/* bind to file system */
int newfd;
logit("Cannot create %s:%s\n",
exit(1);
}
}
(fdetach(NAME_SERVICE_DOOR) < 0) ||
perror("door_attach");
exit(2);
}
}
(void) sigemptyset(&myset);
perror("sigaction");
exit(1);
}
perror("thr_sigsetmask");
exit(1);
}
/*
* kick off revalidate threads
*/
(void *(*)(void *))getpw_revalidate, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))gethost_revalidate, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void*))getnode_revalidate, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void*))getgr_revalidate, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void*))getexec_revalidate, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void*))getprof_revalidate, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void*))getuser_revalidate, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
/*
* kick off reaper threads
*/
(void *(*)(void *))getpw_uid_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))getpw_nam_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))getgr_uid_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))getgr_nam_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))gethost_nam_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))gethost_addr_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))getnode_nam_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))getnode_addr_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))getexec_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))getprof_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
(void *(*)(void *))getuser_reaper, 0, 0, NULL) != 0) {
perror("thr_create");
exit(1);
}
/*
* kick off routing socket monitor thread
*/
perror("thr_create");
exit(1);
}
perror("thr_sigsetmask");
exit(1);
}
for (;;) {
(void) pause();
}
}
/*ARGSUSED*/
static void
{
union {
char space[8192];
} u;
static time_t last_nsswitch_check;
static time_t last_nsswitch_modified;
static time_t last_resolv_modified;
static mutex_t nsswitch_lock;
if (argp == DOOR_UNREF_DATA) {
(void) printf("Door Slam... exiting\n");
exit(0);
}
}
/*
* just in case check
*/
(void) mutex_lock(&nsswitch_lock);
/*
* This code keeps us from statting resolv.conf
* if it doesn't exist, yet prevents us from ignoring
* it if it happens to disappear later on for a bit.
*/
if (last_resolv_modified >= 0) {
if (last_resolv_modified == 0)
last_resolv_modified = -1;
else
} else if (last_resolv_modified == 0) {
}
}
/*EMPTY*/;
} else if (last_nsswitch_modified == 0) {
((last_resolv_modified > 0) &&
char *fmri;
/*
* time for restart
*/
logit("nscd restart due to /etc/nsswitch.conf or "\
"resolv.conf change\n");
/*
* try to restart under smf
*/
/* not running under smf - reexec */
}
if (smf_restart_instance(fmri) == 0)
}
} else
(void) mutex_unlock(&nsswitch_lock);
switch (ptr->nsc_callnumber) {
case NULLCALL:
break;
case GETPWNAM:
case GETPWUID:
break;
case GETGRNAM:
case GETGRGID:
break;
case GETHOSTBYNAME:
case GETHOSTBYADDR:
break;
case GETIPNODEBYNAME:
case GETIPNODEBYADDR:
break;
case GETEXECID:
break;
case GETPROFNAM:
break;
case GETUSERNAM:
break;
case GETADMIN:
break;
case SETADMIN:
case KILLSERVER: {
const priv_set_t *eset;
if (door_ucred(&uc) != 0) {
perror("door_ucred");
break;
}
ucred_geteuid(uc) != 0) {
logit("SETADMIN call failed(cred): caller pid %d, "
ucred_free(uc);
break;
}
logit("Nscd received KILLSERVER cmd from pid %d, "
exit(0);
} else {
logit("SETADMIN call failed\n");
}
ucred_free(uc);
break;
}
default:
logit("Unknown name service door call op %d\n",
break;
}
NULL, 0);
}
/*
* Monitor the routing socket. Address lists stored in the ipnodes
* cache are sorted based on destination address selection rules,
* so when things change that could affect that sorting (interfaces
* go up or down, flags change, etc.), we clear that cache so the
* list will be re-ordered the next time the hostname is resolved.
*/
static void
rts_mon(void)
{
union {
struct {
} r;
struct ifa_msghdr ifam;
} mbuf;
if (rt_sock < 0) {
thr_exit(0);
}
for (;;) {
if (rdlen <= 0) {
logit("routing socket read: %s\n",
thr_exit(0);
}
continue;
}
logit("rx unknown version (%d) on routing socket.\n",
ifam->ifam_version);
continue;
}
case RTM_NEWADDR:
case RTM_DELADDR:
break;
case RTM_ADD:
case RTM_DELETE:
case RTM_CHANGE:
case RTM_GET:
case RTM_LOSING:
case RTM_REDIRECT:
case RTM_MISS:
case RTM_LOCK:
case RTM_OLDADD:
case RTM_OLDDEL:
case RTM_RESOLVE:
case RTM_IFINFO:
break;
default:
logit("rx unknown msg type (%d) on routing socket.\n",
break;
}
}
}
static void
usage(char *s)
{
"Usage: %s [-d debug_level] [-l logfilename]\n", s);
" [-p cachename,positive_time_to_live]\n");
" [-n cachename,negative_time_to_live]\n");
" [-i cachename] [-s cachename,suggestedsize]\n");
" [-h cachename,keep_hot_count] "\
"[-o cachename,\"yes\"|\"no\"]\n");
" [-e cachename,\"yes\"|\"no\"] [-g] " \
"[-c cachename,\"yes\"|\"no\"]\n");
" [-f configfilename] \n");
"\n Supported caches: passwd, group, hosts, ipnodes\n");
" exec_attr, prof_attr, and user_attr.\n");
exit(1);
}
static int logfd = 2;
int
{
int newlogfd;
/*
* we don't really want to try and open the log file
*/
if (*s == 0) {
/* ignore empty log file specs */
/*EMPTY*/;
logfd = -1;
} else {
/*
* In order to open this file securely, we'll try a few tricks
*/
/*
* File already exists... now we need to get cute
* since opening a file in a world-writeable directory
* safely is hard = it could be a hard link or a
* symbolic link to a system file.
*/
logit("Cannot open new logfile \"%s\": %sn",
return (-1);
}
if ((newlogfd =
logit("Cannot open new "\
"logfile \"%s\": %s\n", s,
return (-1);
}
} else {
logit("Cannot use specified logfile \"%s\": "\
"root\n", s);
return (-1);
}
}
logit("Start of new logfile %s\n", s);
}
return (0);
}
void
{
#define LOGBUFLEN 1024
if (logfd >= 0) {
"<time conversion failed>\t");
} else {
/*
* ctime_r() includes some stuff we don't want;
* adjust length to overwrite " YYYY\n".
*/
}
safechars) {
}
(void) mutex_lock(&loglock);
(void) mutex_unlock(&loglock);
}
}
static void
{
union {
char space[8192];
} u;
case GETPWUID:
case GETPWNAM:
break;
case GETGRNAM:
case GETGRGID:
break;
case GETHOSTBYNAME:
case GETHOSTBYADDR:
break;
case GETIPNODEBYNAME:
case GETIPNODEBYADDR:
break;
case GETEXECID:
break;
case GETPROFNAM:
break;
case GETUSERNAM:
break;
default:
assert(0);
break;
}
}
int
{
nsc_call_t *c;
int l = nsc_calllen(in);
exit(1);
}
logit("launching update\n");
}
if (thr_create(NULL,
NULL,
(void *(*)(void*))do_update,
c,
0|THR_DETACHED, NULL) != 0) {
logit("thread create failed\n");
exit(1);
}
return (0);
}
static int
{
case GETPWUID:
case GETGRGID:
case NULLCALL:
return (sizeof (*in));
case GETPWNAM:
case GETGRNAM:
case GETHOSTBYNAME:
case GETIPNODEBYNAME:
case GETHOSTBYADDR:
case GETIPNODEBYADDR:
case GETEXECID:
case GETPROFNAM:
case GETUSERNAM:
}
return (0);
}
static int
{
union {
char space[8192];
} u;
int ndata;
int adata;
ndata = sizeof (u);
return (-1);
}
return (0);
}
/*ARGSUSED*/
static void
{
}
static int
{
int i;
void (*invalidate_func)(void);
for (i = 1; i <= 3; i++) {
/*
* Three of the RBAC databases are cached.
*/
switch (i) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
if (invalidate) {
if (new->nsc_invalidate) {
(*invalidate_func)();
}
} else {
new->nsc_pos_ttl) < 0 ||
new->nsc_neg_ttl) < 0 ||
new->nsc_old_data_ok) < 0 ||
new->nsc_suggestedsize) < 0)
return (-1);
}
}
return (0);
}
/*ARGSUSED*/
static int
{
/*
* global admin stuff
*/
return (-1);
}
/*
* per cache items
*/
logit("Invalidating passwd cache\n");
}
logit("Invalidating group cache\n");
}
logit("Invalidating host cache\n");
}
logit("Invalidating ipnodes cache\n");
}
"passwd",
"passwd",
"passwd",
"passwd",
"passwd",
"passwd",
"group",
"group",
"group",
"group",
"group",
"group",
"ipnodes",
"ipnodes",
"ipnodes",
"ipnodes",
"ipnodes",
"ipnodes",
"hosts",
"hosts",
"hosts",
"hosts",
"hosts",
"hosts",
nscd_set_rbac(new, 0) < 0) {
return (-1);
}
return (0);
}
void
client_killserver(void)
{
union {
char space[8192];
} u;
int ndata;
int adata;
ndata = sizeof (u);
adata = sizeof (nsc_call_t);
}
static int
{
union {
char space[8192];
} u;
int ndata;
int adata;
ndata = sizeof (u);
return (-1);
}
return (0);
}
static void
{
double hitrate;
(void) printf("%10s cache is enabled\n",
(void) printf("%10d cache hits on positive entries\n",
(void) printf("%10d cache hits on negative entries\n",
(void) printf("%10d cache misses on positive entries\n",
(void) printf("%10d cache misses on negative entries\n",
if (hitrate > 0.0)
(void) printf("%10d complete cache invalidations\n",
(void) printf("%10d seconds time to live for positive entries\n",
ptr->nsc_pos_ttl);
(void) printf("%10d seconds time to live for negative entries\n",
ptr->nsc_neg_ttl);
(void) printf("%10d most active entries to be kept valid\n",
ptr->nsc_keephot);
"file for changes\n",
(void) printf("%10s use possibly stale data rather than waiting for "
"refresh\n",
}
static void
{
(void) printf("nscd configuration:\n\n");
(void) printf("\npasswd cache:\n\n");
(void) printf("\ngroup cache:\n\n");
(void) printf("\nhosts cache:\n\n");
(void) printf("\nipnodes cache:\n\n");
(void) printf("\nexec_attr cache:\n\n");
(void) printf("\nprof_attr cache:\n\n");
(void) printf("\nuser_attr cache:\n\n");
}
/*
* detach from tty
*/
static void
detachfromtty(void)
{
if (logfd > 0) {
int i;
for (i = 0; i < logfd; i++)
(void) close(i);
} else
closefrom(0);
(void) chdir("/");
switch (fork1()) {
case (pid_t)-1:
exit(1);
break;
case 0:
break;
default:
exit(0);
}
(void) setsid();
(void) dup(0);
(void) dup(0);
}