vs_main.c revision 911106dfb16696472af8c1b7b4c554a829354fa8
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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
* or http://www.opensolaris.org/os/licensing.
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* vscand Daemon Program
*/
#include <stdio.h>
#include <sys/stat.h>
#include <sys/filio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <zone.h>
#include <tsol/label.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <wait.h>
#include <unistd.h>
#include <getopt.h>
#include <stdarg.h>
#include <libscf.h>
#include <signal.h>
#include <libintl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <pthread.h>
#include <syslog.h>
#include <locale.h>
#include <pwd.h>
#include <grp.h>
#include <priv_utils.h>
#include "vs_incl.h"
static int vscand_fg = 0; /* daemon by default */
static vs_daemon_state_t vscand_state = VS_STATE_INIT;
static int vscand_sigval = 0;
static int vscand_kdrv_fd = -1;
static pthread_mutex_t vscand_cfg_mutex = PTHREAD_MUTEX_INITIALIZER;
/* virus log path */
static char vscand_vlog[MAXPATHLEN];
/* user and group ids - default to 0 */
static uid_t root_uid = 0, daemon_uid = 0;
static gid_t sys_gid = 0;
/* local function prototypes */
static void vscand_sig_handler(int);
static int vscand_parse_args(int, char **);
static void vscand_get_uid_gid();
static int vscand_init_file(char *, uid_t, gid_t, mode_t);
static void vscand_usage(char *);
static int vscand_daemonize_init(void);
static void vscand_daemonize_fini(int, int);
static int vscand_init(void);
static void vscand_fini(void);
static int vscand_configure(void);
static int vscand_kernel_bind(void);
static void vscand_kernel_unbind(void);
static int vscand_kernel_enable(int);
static void vscand_kernel_disable(void);
static int vscand_kernel_config(vs_config_t *);
static void vscand_error(const char *);
static int vscand_get_viruslog(void);
/*
* Enable libumem debugging by default on DEBUG builds.
*/
#ifdef DEBUG
const char *
_umem_debug_init(void)
{
return ("default,verbose"); /* $UMEM_DEBUG setting */
}
const char *
_umem_logging_init(void)
{
return ("fail,contents"); /* $UMEM_LOGGING setting */
}
#endif
/*
* vs_sig_handler
*/
static void
vscand_sig_handler(int sig)
{
if (vscand_sigval == 0)
vscand_sigval = sig;
}
/*
* main
*
* main must return SMF return code (see smf_method (5)) if vscand
* is invoked directly by smf (see manifest: vscan.xml)
* Exit codes: SMF_EXIT_ERR_CONFIG - error
* SMF_EXIT_ERR_FATAL - fatal error
* SMF_EXIT_OK - success
*/
int
main(int argc, char **argv)
{
int err_stat = 0, pfd = -1;
sigset_t set;
struct sigaction act;
mode_t log_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
mode_t door_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
(void) setlocale(LC_ALL, "");
openlog("vscand", 0, LOG_DAEMON);
/* check if running in global zone; other zones not supported */
if (getzoneid() != GLOBAL_ZONEID) {
vscand_error(gettext("non-global zone not supported"));
exit(SMF_EXIT_ERR_FATAL);
}
/* check for a Trusted Solaris environment; not supported */
if (is_system_labeled()) {
vscand_error(gettext("Trusted Extensions not supported"));
exit(SMF_EXIT_ERR_FATAL);
}
/* Parse arguments */
if (vscand_parse_args(argc, argv) != 0)
exit(SMF_EXIT_ERR_CONFIG);
vscand_get_uid_gid();
/*
* Initializetion of virus log and statistic door file
* MUST be done BEFORE vscand_daemonize_init resets uid/gid.
* Only root can create the files in /var/log and /var/run.
*/
if ((vscand_get_viruslog() != 0) ||
(vscand_vlog[0] == '\0') ||
(vscand_init_file(vscand_vlog, root_uid, sys_gid, log_mode) != 0)) {
*vscand_vlog = 0;
}
(void) unlink(VS_STATS_DOOR_NAME);
(void) vscand_init_file(VS_STATS_DOOR_NAME,
daemon_uid, sys_gid, door_mode);
/*
* Once we're done setting our global state up, set up signal handlers
* for ensuring orderly termination on SIGTERM. If we are starting in
* the foreground, we also use the same handler for SIGINT and SIGHUP.
*/
(void) sigfillset(&set);
(void) sigdelset(&set, SIGABRT); /* always unblocked for ASSERT() */
(void) sigfillset(&act.sa_mask);
act.sa_handler = vscand_sig_handler;
act.sa_flags = 0;
(void) sigaction(SIGTERM, &act, NULL);
(void) sigaction(SIGHUP, &act, NULL); /* Refresh config */
(void) sigaction(SIGINT, &act, NULL);
(void) sigaction(SIGUSR1, &act, NULL);
(void) sigdelset(&set, SIGTERM);
(void) sigdelset(&set, SIGHUP);
(void) sigdelset(&set, SIGINT);
(void) sigdelset(&set, SIGUSR1);
if (vscand_fg) {
(void) sigdelset(&set, SIGTSTP);
(void) sigdelset(&set, SIGTTIN);
(void) sigdelset(&set, SIGTTOU);
if (vscand_init() != 0) {
vscand_error(gettext("failed to initialize service"));
exit(SMF_EXIT_ERR_CONFIG);
}
} else {
/*
* "pfd" is a pipe descriptor -- any fatal errors
* during subsequent initialization of the child
* process should be written to this pipe and the
* parent will report this error as the exit status.
*/
pfd = vscand_daemonize_init();
if (vscand_init() != 0) {
vscand_error(gettext("failed to initialize service"));
exit(SMF_EXIT_ERR_CONFIG);
}
vscand_daemonize_fini(pfd, err_stat);
}
vscand_state = VS_STATE_RUNNING;
/* Wait here until shutdown */
while (vscand_state == VS_STATE_RUNNING) {
(void) sigsuspend(&set);
switch (vscand_sigval) {
case 0:
case SIGUSR1:
break;
case SIGHUP:
if (vscand_configure() != 0)
vscand_state = VS_STATE_SHUTDOWN;
break;
default:
vscand_state = VS_STATE_SHUTDOWN;
break;
}
vscand_sigval = 0;
}
vscand_fini();
return (SMF_EXIT_OK);
}
/*
* vscand_parse_args -
* Routine to parse the arguments to the daemon program
* 'f' argument runs process in the foreground instead of as a daemon
*/
int
vscand_parse_args(int argc, char **argv)
{
int optchar;
while ((optchar = getopt(argc, argv, "f?")) != EOF) {
switch (optchar) {
case 'f':
vscand_fg = 1;
break;
default:
vscand_usage(argv[0]);
return (-1);
}
}
return (0);
}
/*
* vscand_usage
*/
static void
vscand_usage(char *progname)
{
char buf[128];
(void) snprintf(buf, sizeof (buf), "%s %s [-f]",
gettext("Usage"), progname);
vscand_error(buf);
(void) snprintf(buf, sizeof (buf), "\t-f %s\n",
gettext("run program in foreground"));
vscand_error(buf);
}
/*
* vscand_get_uid_gid
*
* failure to access a uid/gid results in the default (0) being used.
*/
static void
vscand_get_uid_gid()
{
struct passwd *pwd;
struct group *grp;
if ((pwd = getpwnam("root")) != NULL)
root_uid = pwd->pw_uid;
if ((pwd = getpwnam("daemon")) != NULL)
daemon_uid = pwd->pw_uid;
if ((grp = getgrnam("sys")) != NULL)
sys_gid = grp->gr_gid;
}
/*
* vscand_daemonize_init
*
* This function will fork off a child process, from which
* only the child will return.
*/
static int
vscand_daemonize_init(void)
{
int status, pfds[2];
sigset_t set, oset;
pid_t pid;
/*
* Reset process owner/group to daemon/sys. Root ownership is only
* required to initialize virus log file in /var/log
*/
if (__init_daemon_priv(PU_RESETGROUPS | PU_LIMITPRIVS,
daemon_uid, sys_gid,
PRIV_PROC_AUDIT, PRIV_FILE_DAC_SEARCH, PRIV_FILE_DAC_READ,
PRIV_FILE_FLAG_SET, NULL) != 0) {
vscand_error(gettext("failed to initialize privileges"));
_exit(SMF_EXIT_ERR_FATAL);
}
/*
* Block all signals prior to the fork and leave them blocked in the
* parent so we don't get in a situation where the parent gets SIGINT
* and returns non-zero exit status and the child is actually running.
* In the child, restore the signal mask once we've done our setsid().
*/
(void) sigfillset(&set);
(void) sigdelset(&set, SIGABRT);
(void) sigprocmask(SIG_BLOCK, &set, &oset);
if (pipe(pfds) == -1) {
vscand_error(gettext("failed to create pipe for daemonize"));
_exit(SMF_EXIT_ERR_FATAL);
}
if ((pid = fork()) == -1) {
vscand_error(gettext("failed to fork for daemonize"));
_exit(SMF_EXIT_ERR_FATAL);
}
/*
* If we're the parent process, wait for either the child to send us
* the appropriate exit status over the pipe or for the read to fail
* (presumably with 0 for EOF if our child terminated abnormally).
* If the read fails, exit with either the child's exit status if it
* exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
*/
if (pid != 0) {
(void) close(pfds[1]);
if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
_exit(status);
if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
_exit(WEXITSTATUS(status));
vscand_error(gettext("failed to daemonize"));
_exit(SMF_EXIT_ERR_FATAL);
}
(void) setsid();
(void) sigprocmask(SIG_SETMASK, &oset, NULL);
(void) chdir("/");
(void) umask(022);
(void) close(pfds[0]);
return (pfds[1]);
}
/*
* vscand_daemonize_fini
* Now that we're running, if a pipe fd was specified, write an exit
* status to it to indicate that our parent process can safely detach.
*/
static void
vscand_daemonize_fini(int fd, int err_status)
{
if (fd >= 0)
(void) write(fd, &err_status, sizeof (err_status));
(void) close(fd);
/* Restore standard file descriptors */
if ((fd = open("/dev/null", O_RDWR)) >= 0) {
(void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
(void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
(void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
(void) close(fd);
}
/* clear basic privileges not required by vscand */
__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
}
/*
* vscand_init_file
*
* create specified file and set its uid, gid and mode
*/
static int
vscand_init_file(char *filepath, uid_t uid, gid_t gid, mode_t mode)
{
int fd, rc = 0;
struct stat stat_buf;
char buf[MAXPATHLEN];
if ((fd = open(filepath, O_RDONLY | O_CREAT, mode)) == -1)
rc = -1;
else {
if (fstat(fd, &stat_buf) != 0)
rc = -1;
else {
if (stat_buf.st_mode != mode) {
if (fchmod(fd, mode) != 0)
rc = -1;
}
if ((stat_buf.st_uid != uid) ||
(stat_buf.st_gid != gid)) {
if (fchown(fd, uid, gid) != 0)
rc = -1;
}
}
(void) close(fd);
}
if (rc == -1) {
(void) snprintf(buf, MAXPATHLEN, "%s %s",
gettext("Failed to initialize"), filepath);
vscand_error(buf);
}
return (rc);
}
/*
* vscand_init
*
* There are some requirements on the order in which the daemon
* initialization functions are called.
*
* - vscand_kernel_bind - bind to kernel module
* - vs_eng_init populates vs_icap data and thus vs_icap_init MUST be
* called before vs_eng_init
* - vscand_configure - load the configuration
* - vs_door_init - start vscan door server
* - vscand_kernel_enable - enable scan requests from kernel
*/
static int
vscand_init(void)
{
int door_fd = -1;
if (vscand_kernel_bind() < 0)
return (-1);
if (vs_stats_init() != 0)
vscand_error(
gettext("failed to initialize statistics interface"));
vs_svc_init();
vs_icap_init();
vs_eng_init();
if (vscand_configure() != 0) {
vscand_error(gettext("failed to initialize configuration"));
return (-1);
}
if (((door_fd = vs_door_init()) < 0) ||
(vscand_kernel_enable(door_fd) < 0)) {
vscand_fini();
return (-1);
}
return (0);
}
/*
* vscand_fini
*
* vscand_kernel_disable - should be called first to ensure that no
* more scan requests are initiated from the kernel module
* vs_door_fini - shouldn't be called until after the in-progress
* scans complete (vs_eng_fini waits for in progress scans)
* vscand_kernel_unbind - should be called last to tell the kernel module
* that vscand is shutdown.
*/
static void
vscand_fini(void)
{
vscand_kernel_disable();
vs_svc_fini();
vs_eng_fini();
vs_icap_fini();
vs_door_fini();
vs_stats_fini();
vscand_kernel_unbind();
}
/*
* vscand_configure
*/
static int
vscand_configure(void)
{
uint32_t len;
vs_config_t kconfig;
vs_props_all_t config;
(void) pthread_mutex_lock(&vscand_cfg_mutex);
(void) memset(&config, 0, sizeof (vs_props_all_t));
if (vs_props_get_all(&config) != VS_ERR_NONE) {
vscand_error(gettext("configuration data error"));
(void) pthread_mutex_unlock(&vscand_cfg_mutex);
return (-1);
}
(void) memset(&kconfig, 0, sizeof (vs_config_t));
len = sizeof (kconfig.vsc_types);
if (vs_parse_types(config.va_props.vp_types,
kconfig.vsc_types, &len) != 0) {
vscand_error(gettext("configuration data error - types"));
(void) pthread_mutex_unlock(&vscand_cfg_mutex);
return (-1);
}
kconfig.vsc_types_len = len;
/* Convert the maxfsize string from the configuration into bytes */
if (vs_strtonum(config.va_props.vp_maxsize,
&kconfig.vsc_max_size) != 0) {
vscand_error(gettext("configuration data error - max-size"));
(void) pthread_mutex_unlock(&vscand_cfg_mutex);
return (-1);
}
kconfig.vsc_allow = config.va_props.vp_maxsize_action ? 1LL : 0LL;
/* Send configuration update to kernel */
if (vscand_kernel_config(&kconfig) != 0) {
(void) pthread_mutex_unlock(&vscand_cfg_mutex);
return (-1);
}
/* Tell vs_eng things have changed. */
vs_eng_config(&config);
/* Tell vs_stats things have changed */
vs_stats_config(&config);
(void) pthread_mutex_unlock(&vscand_cfg_mutex);
return (0);
}
/*
* vscand_get_state
*/
vs_daemon_state_t
vscand_get_state(void)
{
return (vscand_state);
}
/*
* vscand_get_viruslog
*/
static int
vscand_get_viruslog()
{
vs_props_t props;
uint64_t propids;
int rc;
propids = VS_PROPID_VLOG;
if ((rc = vs_props_get(&props, propids)) != VS_ERR_NONE) {
vscand_error(vs_strerror(rc));
return (-1);
}
(void) strlcpy(vscand_vlog, props.vp_vlog, sizeof (vscand_vlog));
return (0);
}
/*
* vscand_viruslog
*/
char *
vscand_viruslog(void)
{
if (vscand_vlog[0] == '\0')
return (NULL);
return (vscand_vlog);
}
/*
* vscand_kernel_bind
*/
static int
vscand_kernel_bind(void)
{
char devname[MAXPATHLEN];
int inst = 0;
(void) snprintf(devname, MAXPATHLEN, "%s%d", VS_DRV_PATH, inst);
if ((vscand_kdrv_fd = open(devname, O_RDONLY)) < 0) {
vscand_error(gettext("failed to bind to kernel"));
return (-1);
}
return (0);
}
/*
* vscand_kernel_unbind
*/
static void
vscand_kernel_unbind(void)
{
if (vscand_kdrv_fd >= 0)
(void) close(vscand_kdrv_fd);
}
/*
* vscand_kernel_enable
*/
static int
vscand_kernel_enable(int door_fd)
{
if (ioctl(vscand_kdrv_fd, VS_DRV_IOCTL_ENABLE, door_fd) < 0) {
vscand_error(gettext("failed to bind to kernel"));
(void) close(vscand_kdrv_fd);
vscand_kdrv_fd = -1;
return (-1);
}
return (0);
}
/*
* vscand_kernel_disable
*/
static void
vscand_kernel_disable()
{
if (vscand_kdrv_fd >= 0)
(void) ioctl(vscand_kdrv_fd, VS_DRV_IOCTL_DISABLE);
}
/*
* vscand_kernel_config
*/
int
vscand_kernel_config(vs_config_t *conf)
{
if (vscand_kdrv_fd < 0)
return (-1);
if (ioctl(vscand_kdrv_fd, VS_DRV_IOCTL_CONFIG, conf) < 0) {
vscand_error(gettext("failed to send config to kernel"));
return (-1);
}
return (0);
}
/*
* vscand_error
*/
static void
vscand_error(const char *errmsg)
{
(void) fprintf(stderr, "vscand: %s", errmsg);
syslog(LOG_ERR, "%s\n", errmsg);
}