xref: /onnv-gate/usr/src/cmd/smbsrv/smbd/smbd_main.c (revision 6030:6bebab7d43d5)
15331Samw /*
25331Samw  * CDDL HEADER START
35331Samw  *
45331Samw  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
75331Samw  *
85331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw  * or http://www.opensolaris.org/os/licensing.
105331Samw  * See the License for the specific language governing permissions
115331Samw  * and limitations under the License.
125331Samw  *
135331Samw  * When distributing Covered Code, include this CDDL HEADER in each
145331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw  * If applicable, add the following below this CDDL HEADER, with the
165331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw  *
195331Samw  * CDDL HEADER END
205331Samw  */
215331Samw /*
225772Sas200622  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235331Samw  * Use is subject to license terms.
245331Samw  */
255331Samw 
265331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
275331Samw 
285331Samw #include <sys/types.h>
295331Samw #include <sys/stat.h>
305331Samw #include <sys/ioccom.h>
315331Samw #include <stdio.h>
325331Samw #include <string.h>
335331Samw #include <strings.h>
345331Samw #include <stdlib.h>
355331Samw #include <unistd.h>
365331Samw #include <stdarg.h>
375331Samw #include <fcntl.h>
385331Samw #include <wait.h>
395331Samw #include <signal.h>
405331Samw #include <libscf.h>
415331Samw #include <limits.h>
425331Samw #include <priv_utils.h>
435331Samw #include <door.h>
445331Samw #include <errno.h>
455331Samw #include <syslog.h>
465331Samw #include <pthread.h>
475331Samw #include <time.h>
485331Samw #include <libscf.h>
495331Samw #include <zone.h>
505331Samw #include <tzfile.h>
515331Samw #include <libgen.h>
525331Samw #include <pwd.h>
535331Samw #include <grp.h>
545331Samw 
555331Samw #include <smbsrv/smb_door_svc.h>
565331Samw #include <smbsrv/smb_ioctl.h>
575331Samw #include <smbsrv/libsmb.h>
585331Samw #include <smbsrv/libsmbns.h>
595331Samw #include <smbsrv/libsmbrdr.h>
605331Samw #include <smbsrv/libmlsvc.h>
615331Samw 
625331Samw #include "smbd.h"
635331Samw 
645331Samw #define	DRV_DEVICE_PATH	"/devices/pseudo/smbsrv@0:smbsrv"
655521Sas200622 #define	SMB_VARRUN_DIR "/var/run/smb"
665521Sas200622 #define	SMB_CCACHE SMB_VARRUN_DIR "/ccache"
675331Samw #define	SMB_DBDIR "/var/smb"
685331Samw 
695331Samw static int smbd_daemonize_init(void);
705331Samw static void smbd_daemonize_fini(int, int);
715331Samw 
725331Samw static int smbd_kernel_bind(void);
735331Samw static void smbd_kernel_unbind(void);
745331Samw static int smbd_already_running(void);
755331Samw 
765521Sas200622 static void smbd_remove_ccache(void);
775331Samw static int smbd_service_init(void);
785331Samw static void smbd_service_fini(void);
795331Samw 
805331Samw static int smbd_setup_options(int argc, char *argv[]);
815331Samw static void smbd_usage(FILE *fp);
825331Samw static void smbd_report(const char *fmt, ...);
835331Samw 
845331Samw static void smbd_sig_handler(int sig);
855331Samw 
865331Samw static int smbd_localtime_init(void);
875331Samw static void *smbd_localtime_monitor(void *arg);
885331Samw 
895331Samw extern time_t altzone;
905331Samw 
915331Samw static pthread_t localtime_thr;
925331Samw 
935331Samw static int smbd_refresh_init(void);
945331Samw static void smbd_refresh_fini(void);
955331Samw static void *smbd_refresh_monitor(void *);
965331Samw static pthread_t refresh_thr;
975331Samw static pthread_cond_t refresh_cond;
985331Samw static pthread_mutex_t refresh_mutex;
995331Samw 
1005331Samw static smbd_t smbd;
1015331Samw 
1025331Samw /*
1035331Samw  * smbd user land daemon
1045331Samw  *
1055331Samw  * Use SMF error codes only on return or exit.
1065331Samw  */
1075331Samw int
1085331Samw main(int argc, char *argv[])
1095331Samw {
1105331Samw 	struct sigaction act;
1115331Samw 	sigset_t set;
1125331Samw 	uid_t uid;
1135331Samw 	int pfd = -1;
1145331Samw 
1155331Samw 	smbd.s_pname = basename(argv[0]);
1165331Samw 	openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
1175331Samw 
1185331Samw 	if (smbd_setup_options(argc, argv) != 0)
1195331Samw 		return (SMF_EXIT_ERR_FATAL);
1205331Samw 
1215331Samw 	if ((uid = getuid()) != smbd.s_uid) {
1225331Samw 		smbd_report("user %d: %s", uid, strerror(EPERM));
1235331Samw 		return (SMF_EXIT_ERR_FATAL);
1245331Samw 	}
1255331Samw 
1265331Samw 	if (getzoneid() != GLOBAL_ZONEID) {
1275331Samw 		smbd_report("non-global zones are not supported");
1285331Samw 		return (SMF_EXIT_ERR_FATAL);
1295331Samw 	}
1305331Samw 
1315331Samw 	if (is_system_labeled()) {
1325331Samw 		smbd_report("Trusted Extensions not supported");
1335331Samw 		return (SMF_EXIT_ERR_FATAL);
1345331Samw 	}
1355331Samw 
1365331Samw 	if (smbd_already_running())
1375331Samw 		return (SMF_EXIT_OK);
1385331Samw 
1395331Samw 	(void) sigfillset(&set);
1405331Samw 	(void) sigdelset(&set, SIGABRT);
1415331Samw 
1425331Samw 	(void) sigfillset(&act.sa_mask);
1435331Samw 	act.sa_handler = smbd_sig_handler;
1445331Samw 	act.sa_flags = 0;
1455331Samw 
1465331Samw 	(void) sigaction(SIGTERM, &act, NULL);
1475331Samw 	(void) sigaction(SIGHUP, &act, NULL);
1485331Samw 	(void) sigaction(SIGINT, &act, NULL);
1495331Samw 	(void) sigaction(SIGPIPE, &act, NULL);
1505331Samw 
1515331Samw 	(void) sigdelset(&set, SIGTERM);
1525331Samw 	(void) sigdelset(&set, SIGHUP);
1535331Samw 	(void) sigdelset(&set, SIGINT);
1545331Samw 	(void) sigdelset(&set, SIGPIPE);
1555331Samw 
1565331Samw 	if (smbd.s_fg) {
1575331Samw 		(void) sigdelset(&set, SIGTSTP);
1585331Samw 		(void) sigdelset(&set, SIGTTIN);
1595331Samw 		(void) sigdelset(&set, SIGTTOU);
1605331Samw 
1615331Samw 		if (smbd_service_init() != 0) {
1625331Samw 			smbd_report("service initialization failed");
1635331Samw 			exit(SMF_EXIT_ERR_FATAL);
1645331Samw 		}
1655331Samw 	} else {
1665331Samw 		/*
1675331Samw 		 * "pfd" is a pipe descriptor -- any fatal errors
1685331Samw 		 * during subsequent initialization of the child
1695331Samw 		 * process should be written to this pipe and the
1705331Samw 		 * parent will report this error as the exit status.
1715331Samw 		 */
1725331Samw 		pfd = smbd_daemonize_init();
1735331Samw 
1745331Samw 		if (smbd_service_init() != 0) {
1755331Samw 			smbd_report("daemon initialization failed");
1765331Samw 			exit(SMF_EXIT_ERR_FATAL);
1775331Samw 		}
1785331Samw 
1795331Samw 		smbd_daemonize_fini(pfd, SMF_EXIT_OK);
1805331Samw 	}
1815331Samw 
1825331Samw 	(void) atexit(smbd_service_fini);
1835331Samw 
1845331Samw 	while (!smbd.s_shutdown_flag) {
1855331Samw 		(void) sigsuspend(&set);
1865331Samw 
1875331Samw 		switch (smbd.s_sigval) {
1885331Samw 		case 0:
1895331Samw 			break;
1905331Samw 
1915331Samw 		case SIGPIPE:
1925331Samw 			break;
1935331Samw 
1945331Samw 		case SIGHUP:
1955331Samw 			/* Refresh config was triggered */
1965331Samw 			if (smbd.s_fg)
1975331Samw 				smbd_report("reconfiguration requested");
1985331Samw 			(void) pthread_cond_signal(&refresh_cond);
1995331Samw 			break;
2005331Samw 
2015331Samw 		default:
2025331Samw 			/*
2035331Samw 			 * Typically SIGINT or SIGTERM.
2045331Samw 			 */
2055331Samw 			smbd.s_shutdown_flag = 1;
2065331Samw 			break;
2075331Samw 		}
2085331Samw 
2095331Samw 		smbd.s_sigval = 0;
2105331Samw 	}
2115331Samw 
2125331Samw 	smbd_service_fini();
2135331Samw 	closelog();
2145331Samw 	return (SMF_EXIT_OK);
2155331Samw }
2165331Samw 
2175331Samw /*
2185331Samw  * This function will fork off a child process,
2195331Samw  * from which only the child will return.
2205331Samw  *
2215331Samw  * Use SMF error codes only on exit.
2225331Samw  */
2235331Samw static int
2245331Samw smbd_daemonize_init(void)
2255331Samw {
2265331Samw 	int status, pfds[2];
2275331Samw 	sigset_t set, oset;
2285331Samw 	pid_t pid;
2295331Samw 	int rc;
2305331Samw 
2315331Samw 	/*
2325331Samw 	 * Reset privileges to the minimum set required. We continue
2335331Samw 	 * to run as root to create and access files in /var.
2345331Samw 	 */
2355331Samw 	rc = __init_daemon_priv(PU_RESETGROUPS | PU_LIMITPRIVS,
2365331Samw 	    smbd.s_uid, smbd.s_gid,
2375331Samw 	    PRIV_NET_MAC_AWARE, PRIV_NET_PRIVADDR, PRIV_PROC_AUDIT,
2385331Samw 	    PRIV_SYS_DEVICES, PRIV_SYS_SMB, NULL);
2395331Samw 
2405331Samw 	if (rc != 0) {
2415331Samw 		smbd_report("insufficient privileges");
2425331Samw 		exit(SMF_EXIT_ERR_FATAL);
2435331Samw 	}
2445331Samw 
2455331Samw 	/*
2465331Samw 	 * Block all signals prior to the fork and leave them blocked in the
2475331Samw 	 * parent so we don't get in a situation where the parent gets SIGINT
2485331Samw 	 * and returns non-zero exit status and the child is actually running.
2495331Samw 	 * In the child, restore the signal mask once we've done our setsid().
2505331Samw 	 */
2515331Samw 	(void) sigfillset(&set);
2525331Samw 	(void) sigdelset(&set, SIGABRT);
2535331Samw 	(void) sigprocmask(SIG_BLOCK, &set, &oset);
2545331Samw 
2555331Samw 	if (pipe(pfds) == -1) {
2565331Samw 		smbd_report("unable to create pipe");
2575331Samw 		exit(SMF_EXIT_ERR_FATAL);
2585331Samw 	}
2595331Samw 
2605331Samw 	closelog();
2615331Samw 
2625331Samw 	if ((pid = fork()) == -1) {
2635331Samw 		openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
2645331Samw 		smbd_report("unable to fork");
2655331Samw 		closelog();
2665331Samw 		exit(SMF_EXIT_ERR_FATAL);
2675331Samw 	}
2685331Samw 
2695331Samw 	/*
2705331Samw 	 * If we're the parent process, wait for either the child to send us
2715331Samw 	 * the appropriate exit status over the pipe or for the read to fail
2725331Samw 	 * (presumably with 0 for EOF if our child terminated abnormally).
2735331Samw 	 * If the read fails, exit with either the child's exit status if it
2745331Samw 	 * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
2755331Samw 	 */
2765331Samw 	if (pid != 0) {
2775331Samw 		(void) close(pfds[1]);
2785331Samw 
2795331Samw 		if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
2805331Samw 			_exit(status);
2815331Samw 
2825331Samw 		if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
2835331Samw 			_exit(WEXITSTATUS(status));
2845331Samw 
2855331Samw 		_exit(SMF_EXIT_ERR_FATAL);
2865331Samw 	}
2875331Samw 
2885331Samw 	openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
2895331Samw 	smbd.s_pid = getpid();
2905331Samw 	(void) setsid();
2915331Samw 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
2925331Samw 	(void) chdir("/");
2935331Samw 	(void) umask(022);
2945331Samw 	(void) close(pfds[0]);
2955331Samw 
2965331Samw 	return (pfds[1]);
2975331Samw }
2985331Samw 
2995331Samw static void
3005331Samw smbd_daemonize_fini(int fd, int exit_status)
3015331Samw {
3025331Samw 	/*
3035331Samw 	 * Now that we're running, if a pipe fd was specified, write an exit
3045331Samw 	 * status to it to indicate that our parent process can safely detach.
3055331Samw 	 * Then proceed to loading the remaining non-built-in modules.
3065331Samw 	 */
3075331Samw 	if (fd >= 0)
3085331Samw 		(void) write(fd, &exit_status, sizeof (exit_status));
3095331Samw 
3105331Samw 	(void) close(fd);
3115331Samw 
3125331Samw 	if ((fd = open("/dev/null", O_RDWR)) >= 0) {
3135331Samw 		(void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
3145331Samw 		(void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
3155331Samw 		(void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
3165331Samw 		(void) close(fd);
3175331Samw 	}
3185331Samw 
3195331Samw 	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
3205331Samw 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, NULL);
3215331Samw }
3225331Samw 
3235331Samw static int
3245331Samw smbd_service_init(void)
3255331Samw {
3265331Samw 	static char *dir[] = {
3275331Samw 		SMB_DBDIR,		/* smbpasswd */
3285521Sas200622 		SMB_VARRUN_DIR		/* KRB credential cache */
3295331Samw 	};
3305331Samw 
3315521Sas200622 	int door_id;
3325331Samw 	int rc;
3335331Samw 	uint32_t mode;
3345331Samw 	char resource_domain[SMB_PI_MAX_DOMAIN];
3355331Samw 	int i;
3365331Samw 
3375331Samw 	smbd.s_drv_fd = -1;
3385331Samw 
3395331Samw 	for (i = 0; i < (sizeof (dir)/sizeof (dir[0])); ++i) {
3405331Samw 		errno = 0;
3415331Samw 
3425331Samw 		if ((mkdir(dir[i], 0700) < 0) && (errno != EEXIST)) {
3435331Samw 			smbd_report("mkdir %s: %s", dir[i], strerror(errno));
3445331Samw 			return (1);
3455331Samw 		}
3465331Samw 	}
3475331Samw 
3485331Samw 	/*
3495331Samw 	 * Set KRB5CCNAME (for the SMB credential cache) in the environment.
3505331Samw 	 */
3515521Sas200622 	if (putenv("KRB5CCNAME=" SMB_CCACHE) != 0) {
3525331Samw 		smbd_report("unable to set KRB5CCNAME");
3535331Samw 		return (1);
3545331Samw 	}
3555331Samw 
3565331Samw 	(void) oem_language_set("english");
3575331Samw 
3585331Samw 	if (!nt_builtin_init()) {
3595331Samw 		smbd_report("out of memory");
3605331Samw 		return (1);
3615331Samw 	}
3625331Samw 
3635772Sas200622 	(void) smb_nicmon_start();
3645772Sas200622 	if (dns_msgid_init() != 0) {
3655772Sas200622 		smbd_report("DNS message id initialization failed");
3665331Samw 		return (1);
3675331Samw 	}
3685331Samw 
3695331Samw 	smbrdr_init();
3705331Samw 
3715772Sas200622 	if (smb_netbios_start() != 0)
3725772Sas200622 		smbd_report("NetBIOS services failed to start");
3735772Sas200622 	else
3745772Sas200622 		smbd_report("NetBIOS services started");
3755772Sas200622 
3765331Samw 	if (smb_netlogon_init() != 0) {
3775331Samw 		smbd_report("netlogon initialization failed");
3785331Samw 		return (1);
3795331Samw 	}
3805331Samw 
3815772Sas200622 	(void) smb_getdomainname(resource_domain, SMB_PI_MAX_DOMAIN);
3825331Samw 	(void) utf8_strupr(resource_domain);
3835331Samw 
3845772Sas200622 	mode = smb_config_get_secmode();
3855331Samw 	if (mode == SMB_SECMODE_DOMAIN)
3865331Samw 		(void) locate_resource_pdc(resource_domain);
3875331Samw 
3885331Samw 	/* Get the ID map client handle */
3895331Samw 	if ((rc = smb_idmap_start()) != 0) {
3905331Samw 		smbd_report("no idmap handle");
3915331Samw 		return (rc);
3925331Samw 	}
3935331Samw 
3945331Samw 	if ((rc = nt_domain_init(resource_domain, mode)) != 0) {
3955772Sas200622 		if (rc == SMB_DOMAIN_NOMACHINE_SID) {
3965772Sas200622 			smbd_report(
3975772Sas200622 			    "no machine SID: check idmap configuration");
3985772Sas200622 			return (rc);
3995772Sas200622 		}
4005331Samw 	}
4015331Samw 
4025772Sas200622 	ads_init();
4035331Samw 	if ((rc = mlsvc_init()) != 0) {
4045331Samw 		smbd_report("msrpc initialization failed");
4055331Samw 		return (rc);
4065331Samw 	}
4075331Samw 
4085331Samw 	if (smb_lmshrd_srv_start() != 0) {
4095331Samw 		smbd_report("share initialization failed");
4105331Samw 	}
4115331Samw 
4125331Samw 	/* XXX following will get removed */
4135331Samw 	(void) smb_doorsrv_start();
4145331Samw 	if ((rc = smb_door_srv_start()) != 0)
4155331Samw 		return (rc);
4165331Samw 
4175331Samw 	if ((rc = smbd_refresh_init()) != 0)
4185331Samw 		return (rc);
4195331Samw 
4205331Samw 	/* Call dyndns update - Just in case its configured to refresh DNS */
4215772Sas200622 	if (smb_config_getbool(SMB_CI_DYNDNS_ENABLE))
4225331Samw 		(void) dyndns_update();
4235331Samw 
4245331Samw 	if ((rc = smbd_kernel_bind()) != 0)
4255331Samw 		smbd_report("kernel bind error: %s", strerror(errno));
4265331Samw 
4275331Samw 	(void) smbd_localtime_init();
4285331Samw 
4295521Sas200622 	if ((door_id = smb_winpipe_doorsvc_start()) == -1) {
4305521Sas200622 		smbd_report("winpipe initialization failed %s",
4315521Sas200622 		    strerror(errno));
4325521Sas200622 		return (rc);
4335521Sas200622 	} else {
4345521Sas200622 		if (ioctl(smbd.s_drv_fd, SMB_IOC_WINPIPE, &door_id) < 0)
4355521Sas200622 			smbd_report("winpipe ioctl: %s", strerror(errno));
4365521Sas200622 	}
4375521Sas200622 
4385772Sas200622 	(void) smb_lgrp_start();
439*6030Sjb150015 
440*6030Sjb150015 	(void) smb_pwd_init();
441*6030Sjb150015 
4425331Samw 	return (lmshare_start());
4435331Samw }
4445331Samw 
4455521Sas200622 static void
4465521Sas200622 smbd_remove_ccache(void)
4475521Sas200622 {
4485521Sas200622 	if ((remove(SMB_CCACHE) < 0) && (errno != ENOENT))
4495521Sas200622 		smbd_report("failed to remove SMB ccache");
4505521Sas200622 }
4515521Sas200622 
4525331Samw /*
4535331Samw  * Close the kernel service and shutdown smbd services.
4545331Samw  * This function is registered with atexit(): ensure that anything
4555331Samw  * called from here is safe to be called multiple times.
4565331Samw  */
4575331Samw static void
4585331Samw smbd_service_fini(void)
4595331Samw {
4605521Sas200622 	smb_winpipe_doorsvc_stop();
4615331Samw 	nt_builtin_fini();
4625331Samw 	smbd_refresh_fini();
4635331Samw 	smbd_kernel_unbind();
4645331Samw 	smb_door_srv_stop();
4655331Samw 	smb_doorsrv_stop();
4665331Samw 	smb_lmshrd_srv_stop();
4675331Samw 	lmshare_stop();
4685331Samw 	smb_nicmon_stop();
4695331Samw 	smb_idmap_stop();
4705772Sas200622 	smb_lgrp_stop();
4715521Sas200622 	smbd_remove_ccache();
472*6030Sjb150015 	smb_pwd_fini();
4735521Sas200622 
4745331Samw }
4755331Samw 
4765331Samw /*
4775331Samw  * smbd_refresh_init()
4785331Samw  *
4795331Samw  * SMB service refresh thread initialization.  This thread waits for a
4805331Samw  * refresh event and updates the daemon's view of the configuration
4815331Samw  * before going back to sleep.
4825331Samw  */
4835331Samw static int
4845331Samw smbd_refresh_init()
4855331Samw {
4865331Samw 	pthread_attr_t tattr;
4875331Samw 	pthread_condattr_t cattr;
4885331Samw 	int rc;
4895331Samw 
4905331Samw 	(void) pthread_condattr_init(&cattr);
4915331Samw 	(void) pthread_cond_init(&refresh_cond, &cattr);
4925331Samw 	(void) pthread_condattr_destroy(&cattr);
4935331Samw 
4945331Samw 	(void) pthread_mutex_init(&refresh_mutex, NULL);
4955331Samw 
4965331Samw 	(void) pthread_attr_init(&tattr);
4975331Samw 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
4985331Samw 	rc = pthread_create(&refresh_thr, &tattr, smbd_refresh_monitor, 0);
4995331Samw 	(void) pthread_attr_destroy(&tattr);
5005331Samw 	return (rc);
5015331Samw 
5025331Samw }
5035331Samw 
5045331Samw /*
5055331Samw  * smbd_refresh_fini()
5065331Samw  *
5075331Samw  * Stop the refresh thread.
5085331Samw  */
5095331Samw static void
5105331Samw smbd_refresh_fini()
5115331Samw {
5125331Samw 	(void) pthread_cancel(refresh_thr);
5135331Samw 
5145331Samw 	(void) pthread_cond_destroy(&refresh_cond);
5155331Samw 	(void) pthread_mutex_destroy(&refresh_mutex);
5165331Samw }
5175331Samw 
5185331Samw /*
5195331Samw  * smbd_refresh_monitor()
5205331Samw  *
5215331Samw  * Wait for a refresh event. When this thread wakes up, update the
5225331Samw  * smbd configuration from the SMF config information then go back to
5235331Samw  * wait for the next refresh.
5245331Samw  */
5255331Samw /*ARGSUSED*/
5265331Samw static void *
5275331Samw smbd_refresh_monitor(void *arg)
5285331Samw {
5295331Samw 	int dummy = 0;
5305331Samw 
5315331Samw 	(void) pthread_mutex_lock(&refresh_mutex);
5325331Samw 	while (pthread_cond_wait(&refresh_cond, &refresh_mutex) == 0) {
5335331Samw 		/*
5345331Samw 		 * We've been woken up by a refresh event so go do
5355331Samw 		 * what is necessary.
5365331Samw 		 */
5375772Sas200622 		ads_refresh();
538*6030Sjb150015 		smb_nicmon_reconfig();
5395521Sas200622 		smbd_remove_ccache();
5405521Sas200622 		if (ioctl(smbd.s_drv_fd, SMB_IOC_CONFIG, &dummy) < 0) {
5415331Samw 			smbd_report("configuration update ioctl: %s",
5425331Samw 			    strerror(errno));
5435331Samw 		}
5445331Samw 	}
5455331Samw 	return (NULL);
5465331Samw }
5475331Samw 
5485331Samw 
5495331Samw /*
5505331Samw  * If the door has already been opened by another process (non-zero pid
5515331Samw  * in target), we assume that another smbd is already running.  If there
5525331Samw  * is a race here, it will be caught later when smbsrv is opened because
5535331Samw  * only one process is allowed to open the device at a time.
5545331Samw  */
5555331Samw static int
5565331Samw smbd_already_running(void)
5575331Samw {
5585331Samw 	door_info_t info;
5595331Samw 	int door;
5605331Samw 
5615331Samw 	if ((door = open(SMBD_DOOR_NAME, O_RDONLY)) < 0)
5625331Samw 		return (0);
5635331Samw 
5645331Samw 	if (door_info(door, &info) < 0)
5655331Samw 		return (0);
5665331Samw 
5675331Samw 	if (info.di_target > 0) {
5685331Samw 		smbd_report("already running: pid %ld\n", info.di_target);
5695331Samw 		(void) close(door);
5705331Samw 		return (1);
5715331Samw 	}
5725331Samw 
5735331Samw 	(void) close(door);
5745331Samw 	return (0);
5755331Samw }
5765331Samw 
5775331Samw static int
5785331Samw smbd_kernel_bind(void)
5795331Samw {
5805331Samw 	if (smbd.s_drv_fd != -1)
5815331Samw 		(void) close(smbd.s_drv_fd);
5825331Samw 
5835331Samw 	if ((smbd.s_drv_fd = open(DRV_DEVICE_PATH, 0)) < 0) {
5845331Samw 		smbd.s_drv_fd = -1;
5855331Samw 		return (1);
5865331Samw 	}
5875331Samw 	return (0);
5885331Samw }
5895331Samw 
5905331Samw 
5915331Samw /*
5925331Samw  * Initialization of the localtime thread.
5935331Samw  * Returns 0 on success, an error number if thread creation fails.
5945331Samw  */
5955331Samw 
5965331Samw int
5975331Samw smbd_localtime_init(void)
5985331Samw {
5995331Samw 	pthread_attr_t tattr;
6005331Samw 	int rc;
6015331Samw 
6025331Samw 	(void) pthread_attr_init(&tattr);
6035331Samw 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
6045331Samw 	rc = pthread_create(&localtime_thr, &tattr, smbd_localtime_monitor, 0);
6055331Samw 	(void) pthread_attr_destroy(&tattr);
6065331Samw 	return (rc);
6075331Samw }
6085331Samw 
6095331Samw /*
6105331Samw  * Local time thread to kernel land.
6115331Samw  * Send local gmtoff to kernel module one time at startup
6125331Samw  * and each time it changes (up to twice a year).
6135331Samw  * Local gmtoff is checked once every 15 minutes and
6145331Samw  * since some timezones are aligned on half and qtr hour boundaries,
6155331Samw  * once an hour would likely suffice.
6165331Samw  */
6175331Samw 
6185331Samw /*ARGSUSED*/
6195331Samw static void *
6205331Samw smbd_localtime_monitor(void *arg)
6215331Samw {
6225331Samw 	struct tm local_tm;
6235331Samw 	time_t secs, gmtoff;
6245331Samw 	time_t last_gmtoff = -1;
6255331Samw 	int timeout;
6265331Samw 
6275331Samw 	for (;;) {
6285331Samw 		gmtoff = -altzone;
6295331Samw 
6305331Samw 		if ((last_gmtoff != gmtoff) && (smbd.s_drv_fd != -1)) {
6315331Samw 			if (ioctl(smbd.s_drv_fd, SMB_IOC_GMTOFF, &gmtoff) < 0) {
6325331Samw 				smbd_report("localtime ioctl: %s",
6335331Samw 				    strerror(errno));
6345331Samw 			}
6355331Samw 		}
6365331Samw 
6375331Samw 		/*
6385331Samw 		 * Align the next iteration on a fifteen minute boundary.
6395331Samw 		 */
6405331Samw 		secs = time(0);
6415331Samw 		(void) localtime_r(&secs, &local_tm);
6425331Samw 		timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN);
6435331Samw 		(void) sleep(timeout);
6445331Samw 
6455331Samw 		last_gmtoff = gmtoff;
6465331Samw 	}
6475331Samw 
6485331Samw 	/*NOTREACHED*/
6495331Samw 	return (NULL);
6505331Samw }
6515331Samw 
6525331Samw 
6535331Samw static void
6545331Samw smbd_kernel_unbind(void)
6555331Samw {
6565331Samw 	if (smbd.s_drv_fd != -1) {
6575331Samw 		(void) close(smbd.s_drv_fd);
6585331Samw 		smbd.s_drv_fd = -1;
6595331Samw 	}
6605331Samw }
6615331Samw 
6625331Samw static void
6635331Samw smbd_sig_handler(int sigval)
6645331Samw {
6655331Samw 	if (smbd.s_sigval == 0)
6665331Samw 		smbd.s_sigval = sigval;
6675331Samw }
6685331Samw 
6695331Samw /*
6705331Samw  * Set up configuration options and parse the command line.
6715331Samw  * This function will determine if we will run as a daemon
6725331Samw  * or in the foreground.
6735331Samw  *
6745331Samw  * Failure to find a uid or gid results in using the default (0).
6755331Samw  */
6765331Samw static int
6775331Samw smbd_setup_options(int argc, char *argv[])
6785331Samw {
6795331Samw 	struct passwd *pwd;
6805331Samw 	struct group *grp;
6815331Samw 	int c;
6825331Samw 
6835331Samw 	if ((pwd = getpwnam("root")) != NULL)
6845331Samw 		smbd.s_uid = pwd->pw_uid;
6855331Samw 
6865331Samw 	if ((grp = getgrnam("sys")) != NULL)
6875331Samw 		smbd.s_gid = grp->gr_gid;
6885331Samw 
6895772Sas200622 	smbd.s_fg = smb_config_get_fg_flag();
6905331Samw 
6915331Samw 	while ((c = getopt(argc, argv, ":f")) != -1) {
6925331Samw 		switch (c) {
6935331Samw 		case 'f':
6945331Samw 			smbd.s_fg = 1;
6955331Samw 			break;
6965331Samw 
6975331Samw 		case ':':
6985331Samw 		case '?':
6995331Samw 		default:
7005331Samw 			smbd_usage(stderr);
7015331Samw 			return (-1);
7025331Samw 		}
7035331Samw 	}
7045331Samw 
7055331Samw 	return (0);
7065331Samw }
7075331Samw 
7085331Samw static void
7095331Samw smbd_usage(FILE *fp)
7105331Samw {
7115331Samw 	static char *help[] = {
7125331Samw 		"-f  run program in foreground"
7135331Samw 	};
7145331Samw 
7155331Samw 	int i;
7165331Samw 
7175331Samw 	(void) fprintf(fp, "Usage: %s [-f]\n", smbd.s_pname);
7185331Samw 
7195331Samw 	for (i = 0; i < sizeof (help)/sizeof (help[0]); ++i)
7205331Samw 		(void) fprintf(fp, "    %s\n", help[i]);
7215331Samw }
7225331Samw 
7235331Samw static void
7245331Samw smbd_report(const char *fmt, ...)
7255331Samw {
7265331Samw 	char buf[128];
7275331Samw 	va_list ap;
7285331Samw 
7295331Samw 	if (fmt == NULL)
7305331Samw 		return;
7315331Samw 
7325331Samw 	va_start(ap, fmt);
7335331Samw 	(void) vsnprintf(buf, 128, fmt, ap);
7345331Samw 	va_end(ap);
7355331Samw 
7365331Samw 	(void) fprintf(stderr, "smbd: %s\n", buf);
7375331Samw }
7385331Samw 
7395331Samw /*
7405331Samw  * Enable libumem debugging by default on DEBUG builds.
7415331Samw  */
7425331Samw #ifdef DEBUG
7435331Samw const char *
7445331Samw _umem_debug_init(void)
7455331Samw {
7465331Samw 	return ("default,verbose"); /* $UMEM_DEBUG setting */
7475331Samw }
7485331Samw 
7495331Samw const char *
7505331Samw _umem_logging_init(void)
7515331Samw {
7525331Samw 	return ("fail,contents"); /* $UMEM_LOGGING setting */
7535331Samw }
7545331Samw #endif
755