smbd_main.c revision b7301bf5522d8b9141fe432333ded586218327f2
/*
* 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
* 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 2011 Nexenta Systems, Inc. All rights reserved.
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <fcntl.h>
#include <wait.h>
#include <signal.h>
#include <atomic.h>
#include <libscf.h>
#include <limits.h>
#include <priv_utils.h>
#include <door.h>
#include <errno.h>
#include <pthread.h>
#include <time.h>
#include <libscf.h>
#include <zone.h>
#include <libgen.h>
#include <pwd.h>
#include <grp.h>
#include <smbsrv/smb_door.h>
#include <smbsrv/smb_ioctl.h>
#include <smbsrv/libsmbns.h>
#include <smbsrv/libmlsvc.h>
#include "smbd.h"
#define SMBD_ONLINE_WAIT_INTERVAL 10
#define SMBD_REFRESH_INTERVAL 10
static int smbd_daemonize_init(void);
static void smbd_daemonize_fini(int, int);
static int smbd_kernel_bind(void);
static void smbd_kernel_unbind(void);
static int smbd_already_running(void);
static int smbd_service_init(void);
static void smbd_service_fini(void);
static void smbd_report(const char *fmt, ...);
static void smbd_sig_handler(int sig);
static int32_t smbd_gmtoff(void);
static void smbd_localtime_init(void);
static void *smbd_localtime_monitor(void *arg);
static void smbd_dyndns_init(void);
static void smbd_load_shares(void);
static int smbd_refresh_init(void);
static void smbd_refresh_fini(void);
static void *smbd_refresh_monitor(void *);
static int smbd_kernel_start(void);
static pthread_cond_t refresh_cond;
static pthread_mutex_t refresh_mutex;
/*
* Mutex to ensure that smbd_service_fini() and smbd_service_init()
* are atomic w.r.t. one another. Otherwise, if a shutdown begins
* before initialization is complete, resources can get deallocated
* while initialization threads are still using them.
*/
static mutex_t smbd_service_mutex;
static cond_t smbd_service_cv;
/*
* Use SMF error codes only on return or exit.
*/
int
{
int pfd = -1;
int orig_limit;
return (SMF_EXIT_ERR_FATAL);
return (SMF_EXIT_ERR_FATAL);
}
if (getzoneid() != GLOBAL_ZONEID) {
smbd_report("non-global zones are not supported");
return (SMF_EXIT_ERR_FATAL);
}
if (is_system_labeled()) {
smbd_report("Trusted Extensions not supported");
return (SMF_EXIT_ERR_FATAL);
}
if (smbd_already_running())
return (SMF_EXIT_OK);
/*
* Raise the file descriptor limit to accommodate simultaneous user
* authentications/file access.
*/
smbd_report("Failed to raise file descriptor limit"
}
(void) sigfillset(&set);
if (smbd_service_init() != 0) {
smbd_report("service initialization failed");
}
} 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 = smbd_daemonize_init();
if (smbd_service_init() != 0) {
smbd_report("daemon initialization failed");
}
}
(void) atexit(smb_kmod_stop);
while (!smbd.s_shutting_down) {
(void) sigsuspend(&set);
switch (sigval) {
case 0:
case SIGPIPE:
case SIGABRT:
break;
case SIGHUP:
(void) pthread_cond_signal(&refresh_cond);
break;
case SIGUSR1:
break;
default:
/*
* Typically SIGINT or SIGTERM.
*/
break;
}
}
closelog();
}
/*
* This function will fork off a child process,
* from which only the child will return.
*
* Use SMF error codes only on exit.
*/
static int
smbd_daemonize_init(void)
{
int rc;
/*
* Reset privileges to the minimum set required. We continue
* to run as root to create and access files in /var.
*/
if (rc != 0) {
smbd_report("insufficient privileges");
}
/*
* 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);
smbd_report("unable to create pipe");
}
closelog();
smbd_report("unable to fork");
closelog();
}
/*
* 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) setsid();
(void) chdir("/");
(void) umask(022);
return (pfds[1]);
}
/*
* This function is based on __init_daemon_priv() and replaces
* __init_daemon_priv() since we want smbd to have all privileges so that it
* connection/disconnection. Unused privileges are disabled until command
* execution. The permitted and the limit set contains all privileges. The
* inheritable set contains no privileges.
*/
static const char root_cp[] = "/core.%f.%t";
static int
{
int ret = -1;
char buf[1024];
/*
* This is not a significant failure: it allows us to start programs
* with sufficient privileges and with the proper uid. We don't
* care enough about the extra groups in that case.
*/
if (flags & PU_RESETGROUPS)
goto end;
perm = priv_allocset();
goto end;
/* E = P */
/* Now reset suid and euid */
goto end;
/* I = 0 */
end:
getpid());
} else {
(void) core_set_process_path(daemon_cp,
}
}
(void) setpflags(__PROC_PROTECT, 0);
return (ret);
}
/*
* Most privileges, except the ones that are required for smbd, are turn off
* in the effective set. They will be turn on when needed for command
* execution during share connection/disconnection.
*/
static void
{
/*
* 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.
* Then proceed to loading the remaining non-built-in modules.
*/
if (fd >= 0)
pset = priv_allocset();
return;
/* list of privileges for smbd */
/* turn off unneeded privileges */
/* reenable core dumps */
}
/*
* smbd_service_init
*/
static int
smbd_service_init(void)
{
static struct dir {
char *name;
int perm;
} dir[] = {
{ SMB_DBDIR, 0700 },
{ SMB_CVOL, 0755 },
{ SMB_SYSROOT, 0755 },
{ SMB_SYSTEM32, 0755 },
{ SMB_VSS, 0755 }
};
int rc, i;
(void) mutex_lock(&smbd_service_mutex);
(void) mutex_unlock(&smbd_service_mutex);
return (-1);
}
}
if (rc == -1)
else
smbd_report("unable to set KRB5CCNAME");
(void) mutex_unlock(&smbd_service_mutex);
return (-1);
}
rc = smbd_cups_init();
if (smbd_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI) != 0)
smbd_report("NIC monitor failed to start");
smb_ipc_init();
if (smb_netbios_start() != 0)
smbd_report("NetBIOS services failed to start");
else
smbd_report("NetBIOS services started");
if (rc == SMB_DOMAIN_NOMACHINE_SID) {
"no machine SID: check idmap configuration");
(void) mutex_unlock(&smbd_service_mutex);
return (-1);
}
}
if (smbd_dc_monitor_init() != 0)
smbd_report("DC monitor initialization failed %s",
if (mlsvc_init() != 0) {
smbd_report("msrpc initialization failed");
(void) mutex_unlock(&smbd_service_mutex);
return (-1);
}
(void) mutex_unlock(&smbd_service_mutex);
return (-1);
}
if (smbd_refresh_init() != 0) {
(void) mutex_unlock(&smbd_service_mutex);
return (-1);
}
(void) smb_lgrp_start();
if (smb_shr_start() != 0) {
(void) mutex_unlock(&smbd_service_mutex);
return (-1);
}
if (smbd.s_door_lmshr < 0)
smbd_report("share initialization failed");
/* This reloads the kernel config info. */
if (smbd_kernel_bind() != 0) {
(void) mutex_unlock(&smbd_service_mutex);
return (-1);
}
smbd_report("service initialized");
(void) cond_signal(&smbd_service_cv);
(void) mutex_unlock(&smbd_service_mutex);
return (0);
}
/*
* Shutdown smbd and smbsrv kernel services.
*
* Shutdown will not begin until initialization has completed.
* Only one thread is allowed to perform the shutdown. Other
* threads will be blocked on fini_in_progress until the process
* has exited.
*/
static void
smbd_service_fini(void)
{
static uint_t fini_in_progress;
(void) mutex_lock(&smbd_service_mutex);
while (!smbd.s_initialized)
while (fini_in_progress)
/*NOTREACHED*/
}
smbd_report("service shutting down");
smb_shr_stop();
dyndns_stop();
smb_pwd_fini();
mlsvc_fini();
smbd_report("service terminated");
(void) mutex_unlock(&smbd_service_mutex);
}
/*
* smbd_refresh_init()
*
* SMB service refresh thread initialization. This thread waits for a
* refresh event and updates the daemon's view of the configuration
* before going back to sleep.
*/
static int
{
int rc;
(void) pthread_condattr_init(&cattr);
(void) pthread_condattr_destroy(&cattr);
(void) pthread_attr_init(&tattr);
NULL);
(void) pthread_attr_destroy(&tattr);
if (rc != 0)
smbd_report("unable to start refresh monitor: %s",
return (rc);
}
/*
* smbd_refresh_fini()
*
* Stop the refresh thread.
*/
static void
{
(smbd.s_refresh_tid != 0)) {
(void) pthread_cond_destroy(&refresh_cond);
(void) pthread_mutex_destroy(&refresh_mutex);
}
}
/*
* Wait for refresh events. When woken up, update the smbd configuration
* from SMF and check for changes that require service reconfiguration.
* Throttling is applied to coallesce multiple refresh events when the
* service is being refreshed repeatedly.
*/
/*ARGSUSED*/
static void *
smbd_refresh_monitor(void *arg)
{
smbd_online_wait("smbd_refresh_monitor");
while (!smbd.s_shutting_down) {
(void) sleep(SMBD_REFRESH_INTERVAL);
(void) pthread_mutex_lock(&refresh_mutex);
(!smbd.s_shutting_down))
(void) pthread_mutex_unlock(&refresh_mutex);
if (smbd.s_shutting_down) {
/*NOTREACHED*/
}
(void) mutex_lock(&smbd_service_mutex);
/*
* Clear the DNS zones for the existing interfaces
* before updating the NIC interface list.
*/
if (smbd_nicmon_refresh() != 0)
smbd_report("NIC monitor refresh failed");
(void) smbd_kernel_bind();
(void) mutex_unlock(&smbd_service_mutex);
}
smbd.s_refresh_tid = 0;
return (NULL);
}
void
smbd_set_secmode(int secmode)
{
switch (secmode) {
case SMB_SECMODE_WORKGRP:
case SMB_SECMODE_DOMAIN:
(void) smb_config_set_secmode(secmode);
break;
default:
(void) smb_smf_maintenance_mode();
}
}
/*
* The service is online if initialization is complete and shutdown
* has not begun.
*/
smbd_online(void)
{
}
/*
* Wait until the service is online. Provided for threads that
* should wait until the service has been fully initialized before
* they start performing operations.
*/
void
smbd_online_wait(const char *text)
{
while (!smbd_online())
(void) sleep(SMBD_ONLINE_WAIT_INTERVAL);
}
}
/*
* If the door has already been opened by another process (non-zero pid
* in target), we assume that another smbd is already running. If there
* is a race here, it will be caught later when smbsrv is opened because
* only one process is allowed to open the device at a time.
*/
static int
smbd_already_running(void)
{
int door;
return (0);
return (0);
return (1);
}
return (0);
}
/*
* smbd_kernel_bind
*
* If smbsrv is already bound, reload the configuration and update smbsrv.
* Otherwise, open the smbsrv device and start the kernel service.
*/
static int
smbd_kernel_bind(void)
{
int rc;
if (rc < 0)
smbd_report("kernel configuration update failed: %s",
return (rc);
}
if (smb_kmod_isbound())
if ((rc = smb_kmod_bind()) == 0) {
rc = smbd_kernel_start();
if (rc != 0)
else
}
if (rc != 0)
return (rc);
}
static int
smbd_kernel_start(void)
{
int rc;
if (rc != 0)
return (rc);
if (rc != 0)
return (rc);
if (rc != 0)
return (rc);
return (0);
}
/*
* smbd_kernel_unbind
*/
static void
smbd_kernel_unbind(void)
{
}
/*
* Create the Dynamic DNS publisher thread.
*/
static void
smbd_dyndns_init(void)
{
int rc;
dyndns_start();
(void) pthread_attr_init(&attr);
(void) pthread_attr_destroy(&attr);
if (rc != 0)
smbd_report("unable to start dyndns publisher: %s",
}
/*
* Launches a thread to populate the share cache by share information
* stored in sharemgr
*/
static void
smbd_load_shares(void)
{
int rc;
(void) pthread_attr_init(&attr);
(void) pthread_attr_destroy(&attr);
if (rc != 0)
}
/*
* Initialization of the localtime thread.
* Returns 0 on success, an error number if thread creation fails.
*/
static void
smbd_localtime_init(void)
{
int rc;
(void) pthread_attr_init(&attr);
(void) pthread_attr_destroy(&attr);
if (rc != 0)
}
/*
* Send local gmtoff to the kernel module one time at startup and each
* time it changes (up to twice a year).
* Local gmtoff is checked once every 15 minutes since some timezones
* are aligned on half and quarter hour boundaries.
*/
/*ARGSUSED*/
static void *
smbd_localtime_monitor(void *arg)
{
int timeout;
int error;
smbd_online_wait("smbd_localtime_monitor");
for (;;) {
gmtoff = smbd_gmtoff();
if (error != 0)
smbd_report("localtime set failed: %s",
}
/*
* Align the next iteration on a fifteen minute boundary.
*/
}
/*NOTREACHED*/
return (NULL);
}
/*
* smbd_gmtoff
*
* Determine offset from GMT. If daylight saving time use altzone,
* otherwise use timezone.
*/
static int32_t
smbd_gmtoff(void)
{
return (gmtoff);
}
static void
smbd_sig_handler(int sigval)
{
(void) pthread_cond_signal(&refresh_cond);
}
(void) pthread_cond_signal(&refresh_cond);
}
}
/*
* Set up configuration options and parse the command line.
* This function will determine if we will run as a daemon
* or in the foreground.
*
* Failure to find a uid or gid results in using the default (0).
*/
static int
{
int c;
switch (c) {
case 'f':
break;
case ':':
case '?':
default:
return (-1);
}
}
return (0);
}
static void
{
static char *help[] = {
"-f run program in foreground"
};
int i;
}
static void
smbd_report(const char *fmt, ...)
{
char buf[128];
return;
}
/*
* 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