#include <assert.h>
#include <door.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <priv.h>
#include <procfs.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/corectl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ucontext.h>
#include <unistd.h>
#include "configd.h"
static pthread_key_t thread_info_key;
static pthread_attr_t thread_attr;
static pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER;
int num_started;
int num_servers;
static uu_list_pool_t *thread_pool;
uu_list_t *thread_list;
static thread_info_t main_thread_info;
static int finished;
static pid_t privileged_pid = 0;
static int privileged_psinfo_fd = -1;
static int privileged_user = 0;
static priv_set_t *privileged_privs;
static int log_to_syslog = 0;
int is_main_repository = 1;
int max_repository_backups = 4;
#define CONFIGD_MAX_FDS 262144
const char *
_umem_options_init(void)
{
return ("nomagazines");
}
void
abort_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
{
struct sigaction act;
(void) sigemptyset(&act.sa_mask);
act.sa_handler = SIG_DFL;
act.sa_flags = 0;
(void) sigaction(sig, &act, NULL);
(void) printstack(2);
if (sip != NULL && SI_FROMUSER(sip))
(void) pthread_kill(pthread_self(), sig);
(void) sigfillset(&ucp->uc_sigmask);
(void) sigdelset(&ucp->uc_sigmask, sig);
ucp->uc_flags |= UC_SIGMASK;
(void) setcontext(ucp);
}
static int
reserve_new_thread(void)
{
(void) pthread_mutex_lock(&thread_lock);
assert(num_started >= 0);
if (num_servers > num_started + 1) {
(void) pthread_mutex_unlock(&thread_lock);
return (0);
}
++num_servers;
(void) pthread_mutex_unlock(&thread_lock);
return (1);
}
static void
thread_info_free(thread_info_t *ti)
{
uu_list_node_fini(ti, &ti->ti_node, thread_pool);
if (ti->ti_ucred != NULL)
uu_free(ti->ti_ucred);
uu_free(ti);
}
static void
thread_exiting(void *arg)
{
thread_info_t *ti = arg;
if (ti != NULL)
log_enter(&ti->ti_log);
(void) pthread_mutex_lock(&thread_lock);
if (ti != NULL) {
num_started--;
uu_list_remove(thread_list, ti);
}
assert(num_servers > 0);
--num_servers;
if (num_servers == 0) {
configd_critical("no door server threads\n");
abort();
}
(void) pthread_mutex_unlock(&thread_lock);
if (ti != NULL && ti != &main_thread_info)
thread_info_free(ti);
}
void
thread_newstate(thread_info_t *ti, thread_state_t newstate)
{
ti->ti_ucred_read = 0;
if (newstate != ti->ti_state) {
ti->ti_prev_state = ti->ti_state;
ti->ti_state = newstate;
ti->ti_lastchange = gethrtime();
}
}
thread_info_t *
thread_self(void)
{
return (pthread_getspecific(thread_info_key));
}
ucred_t *
get_ucred(void)
{
thread_info_t *ti = thread_self();
ucred_t **ret = &ti->ti_ucred;
if (ti->ti_ucred_read)
return (*ret);
if (door_ucred(ret) != 0)
return (NULL);
ti->ti_ucred_read = 1;
return (*ret);
}
int
ucred_is_privileged(ucred_t *uc)
{
const priv_set_t *ps;
if ((ps = ucred_getprivset(uc, PRIV_EFFECTIVE)) != NULL) {
if (priv_isfullset(ps))
return (1);
if (privileged_privs != NULL &&
priv_issubset(privileged_privs, ps))
return (1);
}
return (0);
}
adt_session_data_t *
get_audit_session(void)
{
thread_info_t *ti = thread_self();
return (ti->ti_active_client->rc_adt_session);
}
static void *
thread_start(void *arg)
{
thread_info_t *ti = arg;
(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
(void) pthread_mutex_lock(&thread_lock);
num_started++;
(void) uu_list_insert_after(thread_list, uu_list_last(thread_list),
ti);
(void) pthread_mutex_unlock(&thread_lock);
(void) pthread_setspecific(thread_info_key, ti);
thread_newstate(ti, TI_DOOR_RETURN);
(void) door_return(NULL, 0, NULL, 0);
return (arg);
}
static void
new_thread_needed(door_info_t *dip)
{
thread_info_t *ti;
sigset_t new, old;
assert(dip == NULL);
if (!reserve_new_thread())
return;
if ((ti = uu_zalloc(sizeof (*ti))) == NULL)
goto fail;
uu_list_node_init(ti, &ti->ti_node, thread_pool);
ti->ti_state = TI_CREATED;
ti->ti_prev_state = TI_CREATED;
if ((ti->ti_ucred = uu_zalloc(ucred_size())) == NULL)
goto fail;
(void) sigfillset(&new);
(void) pthread_sigmask(SIG_SETMASK, &new, &old);
if ((errno = pthread_create(&ti->ti_thread, &thread_attr, thread_start,
ti)) != 0) {
(void) pthread_sigmask(SIG_SETMASK, &old, NULL);
goto fail;
}
(void) pthread_sigmask(SIG_SETMASK, &old, NULL);
return;
fail:
thread_exiting(NULL);
if (ti != NULL)
thread_info_free(ti);
}
int
create_connection(ucred_t *uc, repository_door_request_t *rp,
size_t rp_size, int *out_fd)
{
int flags;
int privileged = 0;
uint32_t debugflags = 0;
psinfo_t info;
if (privileged_pid != 0) {
if (ucred_getpid(uc) != privileged_pid ||
read(privileged_psinfo_fd, &info, sizeof (info)) !=
sizeof (info))
return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
privileged = 1;
} else if (privileged_user != 0) {
if (ucred_geteuid(uc) != privileged_user)
return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
privileged = 1;
}
#define GET_ARG(rp, rp_size, f, out) \
(((rp_size) >= offsetofend(repository_door_request_t, f)) ? \
((*(out) = (rp)->f), 1) : 0)
if (!GET_ARG(rp, rp_size, rdr_flags, &flags))
return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
#if (REPOSITORY_DOOR_FLAG_ALL != REPOSITORY_DOOR_FLAG_DEBUG)
#error Need to update flag checks
#endif
if (flags & ~REPOSITORY_DOOR_FLAG_ALL)
return (REPOSITORY_DOOR_FAIL_BAD_FLAG);
if (flags & REPOSITORY_DOOR_FLAG_DEBUG)
if (!GET_ARG(rp, rp_size, rdr_debug, &debugflags))
return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
#undef GET_ARG
return (create_client(ucred_getpid(uc), debugflags, privileged,
out_fd));
}
void
configd_vlog(int severity, const char *prefix, const char *message,
va_list args)
{
if (log_to_syslog)
vsyslog(severity, message, args);
else {
flockfile(stderr);
if (prefix != NULL)
(void) fprintf(stderr, "%s", prefix);
(void) vfprintf(stderr, message, args);
if (message[0] == 0 || message[strlen(message) - 1] != '\n')
(void) fprintf(stderr, "\n");
funlockfile(stderr);
}
}
void
configd_vcritical(const char *message, va_list args)
{
configd_vlog(LOG_CRIT, "svc.configd: Fatal error: ", message, args);
}
void
configd_critical(const char *message, ...)
{
va_list args;
va_start(args, message);
configd_vcritical(message, args);
va_end(args);
}
void
configd_info(const char *message, ...)
{
va_list args;
va_start(args, message);
configd_vlog(LOG_INFO, "svc.configd: ", message, args);
va_end(args);
}
static void
usage(const char *prog, int ret)
{
(void) fprintf(stderr,
"usage: %s [-np] [-d door_path] [-r repository_path]\n"
" [-t nonpersist_repository]\n", prog);
exit(ret);
}
static void
handler(int sig, siginfo_t *info, void *data)
{
finished = 1;
}
static int pipe_fd = -1;
static int
daemonize_start(void)
{
char data;
int status;
int filedes[2];
pid_t pid;
(void) close(0);
(void) dup2(2, 1);
if (pipe(filedes) < 0)
return (-1);
if ((pid = fork1()) < 0)
return (-1);
if (pid != 0) {
struct sigaction act;
act.sa_handler = SIG_DFL;
(void) sigemptyset(&act.sa_mask);
act.sa_flags = 0;
(void) sigaction(SIGPIPE, &act, NULL);
(void) close(filedes[1]);
if (read(filedes[0], &data, 1) == 1) {
_exit(CONFIGD_EXIT_OKAY);
}
status = -1;
(void) wait4(pid, &status, 0, NULL);
if (WIFEXITED(status))
_exit(WEXITSTATUS(status));
else
_exit(-1);
}
pipe_fd = filedes[1];
(void) close(filedes[0]);
(void) setsid();
(void) umask(0077);
return (0);
}
static void
daemonize_ready(void)
{
char data = '\0';
(void) write(pipe_fd, &data, 1);
(void) close(pipe_fd);
}
const char *
regularize_path(const char *dir, const char *base, char *tmpbuf)
{
if (base == NULL)
return (NULL);
if (base[0] == '/')
return (base);
if (snprintf(tmpbuf, PATH_MAX, "%s/%s", dir, base) >= PATH_MAX) {
(void) fprintf(stderr, "svc.configd: %s/%s: path too long\n",
dir, base);
exit(CONFIGD_EXIT_BAD_ARGS);
}
return (tmpbuf);
}
int
main(int argc, char *argv[])
{
thread_info_t *ti = &main_thread_info;
char pidpath[sizeof ("/proc/" "/psinfo") + 10];
struct rlimit fd_new;
const char *endptr;
sigset_t myset;
int c;
int ret;
int fd;
char curdir[PATH_MAX];
char dbtmp[PATH_MAX];
char npdbtmp[PATH_MAX];
char doortmp[PATH_MAX];
const char *dbpath = NULL;
const char *npdbpath = NULL;
const char *doorpath = REPOSITORY_DOOR_NAME;
struct sigaction act;
int daemonize = 1;
int have_npdb = 1;
closefrom(3);
if (getcwd(curdir, sizeof (curdir)) == NULL) {
(void) fprintf(stderr,
"%s: unable to get current directory: %s\n",
argv[0], strerror(errno));
exit(CONFIGD_EXIT_INIT_FAILED);
}
while ((c = getopt(argc, argv, "Dnpd:r:t:")) != -1) {
switch (c) {
case 'n':
daemonize = 0;
break;
case 'd':
doorpath = regularize_path(curdir, optarg, doortmp);
have_npdb = 0;
break;
case 'p':
log_to_syslog = 0;
privileged_pid = getppid();
(void) snprintf(pidpath, sizeof (pidpath),
"/proc/%" _PRIdID "/psinfo", privileged_pid);
if ((fd = open(pidpath, O_RDONLY)) < 0 ||
getppid() != privileged_pid) {
(void) fprintf(stderr,
"%s: unable to get parent info\n", argv[0]);
exit(CONFIGD_EXIT_BAD_ARGS);
}
privileged_psinfo_fd = fd;
break;
case 'r':
dbpath = regularize_path(curdir, optarg, dbtmp);
is_main_repository = 0;
break;
case 't':
npdbpath = regularize_path(curdir, optarg, npdbtmp);
is_main_repository = 0;
break;
default:
usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
break;
}
}
if (privileged_pid == 0 && geteuid() != 0) {
privileged_user = geteuid();
}
privileged_privs = priv_str_to_set("zone", "", &endptr);
if (endptr != NULL && privileged_privs != NULL) {
priv_freeset(privileged_privs);
privileged_privs = NULL;
}
openlog("svc.configd", LOG_PID | LOG_CONS, LOG_DAEMON);
(void) setlogmask(LOG_UPTO(LOG_NOTICE));
if (npdbpath)
have_npdb = 1;
if (optind != argc)
usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
if (daemonize) {
if (getuid() == 0)
(void) chdir("/");
if (daemonize_start() < 0) {
(void) perror("unable to daemonize");
exit(CONFIGD_EXIT_INIT_FAILED);
}
}
if (getuid() == 0)
(void) core_set_process_path(CONFIGD_CORE,
strlen(CONFIGD_CORE) + 1, getpid());
#if 0
(void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_FILE_LINK_ANY,
PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_PROC_SESSION, NULL);
#endif
(void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL);
(void) sigfillset(&act.sa_mask);
act.sa_handler = SIG_IGN;
act.sa_flags = 0;
(void) sigaction(SIGPIPE, &act, NULL);
(void) sigaction(SIGALRM, &act, NULL);
(void) sigaction(SIGUSR1, &act, NULL);
(void) sigaction(SIGUSR2, &act, NULL);
(void) sigaction(SIGPOLL, &act, NULL);
act.sa_sigaction = (void (*)(int, siginfo_t *, void *))&abort_handler;
act.sa_flags = SA_SIGINFO;
(void) sigaction(SIGABRT, &act, NULL);
act.sa_sigaction = &handler;
act.sa_flags = SA_SIGINFO;
(void) sigaction(SIGHUP, &act, NULL);
(void) sigaction(SIGINT, &act, NULL);
(void) sigaction(SIGTERM, &act, NULL);
(void) sigemptyset(&myset);
(void) sigaddset(&myset, SIGHUP);
(void) sigaddset(&myset, SIGINT);
(void) sigaddset(&myset, SIGTERM);
if ((errno = pthread_attr_init(&thread_attr)) != 0) {
(void) perror("initializing");
exit(CONFIGD_EXIT_INIT_FAILED);
}
fd_new.rlim_max = fd_new.rlim_cur = CONFIGD_MAX_FDS;
(void) setrlimit(RLIMIT_NOFILE, &fd_new);
#ifndef NATIVE_BUILD
(void) enable_extended_FILE_stdio(-1, -1);
#endif
if ((ret = backend_init(dbpath, npdbpath, have_npdb)) !=
CONFIGD_EXIT_OKAY)
exit(ret);
if (!client_init())
exit(CONFIGD_EXIT_INIT_FAILED);
if (!rc_node_init())
exit(CONFIGD_EXIT_INIT_FAILED);
(void) pthread_attr_setdetachstate(&thread_attr,
PTHREAD_CREATE_DETACHED);
(void) pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);
if ((errno = pthread_key_create(&thread_info_key,
thread_exiting)) != 0) {
perror("pthread_key_create");
exit(CONFIGD_EXIT_INIT_FAILED);
}
if ((thread_pool = uu_list_pool_create("thread_pool",
sizeof (thread_info_t), offsetof(thread_info_t, ti_node),
NULL, UU_LIST_POOL_DEBUG)) == NULL) {
configd_critical("uu_list_pool_create: %s\n",
uu_strerror(uu_error()));
exit(CONFIGD_EXIT_INIT_FAILED);
}
if ((thread_list = uu_list_create(thread_pool, NULL, 0)) == NULL) {
configd_critical("uu_list_create: %s\n",
uu_strerror(uu_error()));
exit(CONFIGD_EXIT_INIT_FAILED);
}
(void) memset(ti, '\0', sizeof (*ti));
uu_list_node_init(ti, &ti->ti_node, thread_pool);
(void) uu_list_insert_before(thread_list, uu_list_first(thread_list),
ti);
ti->ti_thread = pthread_self();
ti->ti_state = TI_SIGNAL_WAIT;
ti->ti_prev_state = TI_SIGNAL_WAIT;
(void) pthread_setspecific(thread_info_key, ti);
(void) door_server_create(new_thread_needed);
if (!setup_main_door(doorpath)) {
configd_critical("Setting up main door failed.\n");
exit(CONFIGD_EXIT_DOOR_INIT_FAILED);
}
if (daemonize)
daemonize_ready();
(void) pthread_sigmask(SIG_BLOCK, &myset, NULL);
while (!finished) {
int sig = sigwait(&myset);
if (sig > 0) {
break;
}
}
backend_fini();
return (CONFIGD_EXIT_OKAY);
}