smbd_main.c revision c8ec8eea9849cac239663c46be8a7f5d2ba7ca00
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER START
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The contents of this file are subject to the terms of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Common Development and Distribution License (the "License").
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You may not use this file except in compliance with the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * or http://www.opensolaris.org/os/licensing.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * See the License for the specific language governing permissions
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and limitations under the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * When distributing Covered Code, include this CDDL HEADER in each
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If applicable, add the following below this CDDL HEADER, with the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * fields enclosed by brackets "[]" replaced with your own identifying
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * information: Portions Copyright [yyyy] [name of copyright owner]
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER END
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Use is subject to license terms.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego#pragma ident "@(#)smbd_main.c 1.13 08/08/05 SMI"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <sys/types.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <sys/stat.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <sys/ioccom.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <stdio.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <string.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <strings.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <stdlib.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <unistd.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <stdarg.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <fcntl.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <wait.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <signal.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <libscf.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <limits.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <priv_utils.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <door.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <errno.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <syslog.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <pthread.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <time.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <libscf.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <zone.h>
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego#include <time.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <tzfile.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <libgen.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <pwd.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <grp.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/smb_door_svc.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/smb_ioctl.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libsmb.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libsmbns.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libsmbrdr.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libmlsvc.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include "smbd.h"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#define DRV_DEVICE_PATH "/devices/pseudo/smbsrv@0:smbsrv"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#define SMB_DBDIR "/var/smb"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbextern void *smbd_nbt_listener(void *);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbextern void *smbd_tcp_listener(void *);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_daemonize_init(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_daemonize_fini(int, int);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2easstatic int smbd_kernel_bind(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_kernel_unbind(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_already_running(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_service_init(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_service_fini(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_setup_options(int argc, char *argv[]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_usage(FILE *fp);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_report(const char *fmt, ...);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_sig_handler(int sig);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borregostatic int32_t smbd_gmtoff(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_localtime_init(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void *smbd_localtime_monitor(void *arg);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_t localtime_thr;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_refresh_init(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_refresh_fini(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void *smbd_refresh_monitor(void *);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbstatic pthread_t nbt_listener;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbstatic pthread_t tcp_listener;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_t refresh_thr;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_cond_t refresh_cond;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_mutex_t refresh_mutex;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbsmbd_t smbd;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smbd user land daemon
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Use SMF error codes only on return or exit.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwmain(int argc, char *argv[])
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas struct sigaction act;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas sigset_t set;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas uid_t uid;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas int pfd = -1;
3db3f65c6274eb042354801a308c8e9bc4994553amw int sigval;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_pname = basename(argv[0]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd_setup_options(argc, argv) != 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((uid = getuid()) != smbd.s_uid) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("user %d: %s", uid, strerror(EPERM));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (getzoneid() != GLOBAL_ZONEID) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("non-global zones are not supported");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (is_system_labeled()) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("Trusted Extensions not supported");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd_already_running())
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_OK);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigfillset(&set);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGABRT);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigfillset(&act.sa_mask);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw act.sa_handler = smbd_sig_handler;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw act.sa_flags = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGTERM, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGHUP, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGINT, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGPIPE, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGTERM);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGHUP);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGINT);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGPIPE);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd.s_fg) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGTSTP);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGTTIN);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGTTOU);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd_service_init() != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("service initialization failed");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * "pfd" is a pipe descriptor -- any fatal errors
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * during subsequent initialization of the child
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * process should be written to this pipe and the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * parent will report this error as the exit status.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw pfd = smbd_daemonize_init();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd_service_init() != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("daemon initialization failed");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_daemonize_fini(pfd, SMF_EXIT_OK);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) atexit(smbd_service_fini);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw while (!smbd.s_shutdown_flag) {
3db3f65c6274eb042354801a308c8e9bc4994553amw if (smbd.s_sigval == 0)
3db3f65c6274eb042354801a308c8e9bc4994553amw (void) sigsuspend(&set);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
3db3f65c6274eb042354801a308c8e9bc4994553amw sigval = smbd.s_sigval;
3db3f65c6274eb042354801a308c8e9bc4994553amw smbd.s_sigval = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
3db3f65c6274eb042354801a308c8e9bc4994553amw switch (sigval) {
3db3f65c6274eb042354801a308c8e9bc4994553amw case 0:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SIGPIPE:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SIGHUP:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /* Refresh config was triggered */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd.s_fg)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("reconfiguration requested");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_cond_signal(&refresh_cond);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw default:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Typically SIGINT or SIGTERM.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_shutdown_flag = 1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_service_fini();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw closelog();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_OK);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This function will fork off a child process,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * from which only the child will return.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Use SMF error codes only on exit.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_daemonize_init(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int status, pfds[2];
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw sigset_t set, oset;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw pid_t pid;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int rc;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Reset privileges to the minimum set required. We continue
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * to run as root to create and access files in /var.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw rc = __init_daemon_priv(PU_RESETGROUPS | PU_LIMITPRIVS,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_uid, smbd.s_gid,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw PRIV_NET_MAC_AWARE, PRIV_NET_PRIVADDR, PRIV_PROC_AUDIT,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw PRIV_SYS_DEVICES, PRIV_SYS_SMB, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (rc != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("insufficient privileges");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Block all signals prior to the fork and leave them blocked in the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * parent so we don't get in a situation where the parent gets SIGINT
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and returns non-zero exit status and the child is actually running.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * In the child, restore the signal mask once we've done our setsid().
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigfillset(&set);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGABRT);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigprocmask(SIG_BLOCK, &set, &oset);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (pipe(pfds) == -1) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("unable to create pipe");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw closelog();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((pid = fork()) == -1) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("unable to fork");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw closelog();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If we're the parent process, wait for either the child to send us
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the appropriate exit status over the pipe or for the read to fail
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * (presumably with 0 for EOF if our child terminated abnormally).
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If the read fails, exit with either the child's exit status if it
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (pid != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(pfds[1]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw _exit(status);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw _exit(WEXITSTATUS(status));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw _exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_pid = getpid();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) setsid();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigprocmask(SIG_SETMASK, &oset, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) chdir("/");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) umask(022);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(pfds[0]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (pfds[1]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_daemonize_fini(int fd, int exit_status)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Now that we're running, if a pipe fd was specified, write an exit
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * status to it to indicate that our parent process can safely detach.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Then proceed to loading the remaining non-built-in modules.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (fd >= 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) write(fd, &exit_status, sizeof (exit_status));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fd);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((fd = open("/dev/null", O_RDWR)) >= 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fd);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw __fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb/*
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb * smbd_service_init
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_service_init(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas int rc;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas char resource_domain[SMB_PI_MAX_DOMAIN];
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas char fqdn[MAXHOSTNAMELEN];
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_drv_fd = -1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if ((mkdir(SMB_DBDIR, 0700) < 0) && (errno != EEXIST)) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd_report("mkdir %s: %s", SMB_DBDIR, strerror(errno));
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if ((rc = smb_ccache_init(SMB_VARRUN_DIR, SMB_CCACHE_FILE)) != 0) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if (rc == -1)
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd_report("mkdir %s: %s", SMB_VARRUN_DIR,
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb strerror(errno));
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb else
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd_report("unable to set KRB5CCNAME");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) oem_language_set("english");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (!smb_wka_init()) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("out of memory");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (smb_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI) != 0)
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd_report("NIC monitoring failed to start");
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego dns_msgid_init();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbrdr_init();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as if (smb_netbios_start() != 0)
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smbd_report("NetBIOS services failed to start");
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as else
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smbd_report("NetBIOS services started");
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smb_netlogon_init() != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("netlogon initialization failed");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as (void) smb_getdomainname(resource_domain, SMB_PI_MAX_DOMAIN);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) utf8_strupr(resource_domain);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /* Get the ID map client handle */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((rc = smb_idmap_start()) != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("no idmap handle");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_secmode = smb_config_get_secmode();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if ((rc = nt_domain_init(resource_domain, smbd.s_secmode)) != 0) {
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as if (rc == SMB_DOMAIN_NOMACHINE_SID) {
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smbd_report(
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as "no machine SID: check idmap configuration");
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as return (rc);
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_ads_init();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((rc = mlsvc_init()) != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("msrpc initialization failed");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego if (smbd.s_secmode == SMB_SECMODE_DOMAIN)
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego if (smbd_locate_dc_start(resource_domain) != 0)
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego smbd_report("dc discovery failed %s", strerror(errno));
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb smbd.s_door_lmshr = smb_share_dsrv_start();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (smbd.s_door_lmshr < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("share initialization failed");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_door_srv = smb_door_srv_start();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (smbd.s_door_srv < 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((rc = smbd_refresh_init()) != 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if (smb_getfqdomainname(fqdn, MAXHOSTNAMELEN) == 0)
8c10a8659ac31335ed870a1711c0182623f72fd6as (void) dyndns_update_core(fqdn);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) smbd_localtime_init();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
3db3f65c6274eb042354801a308c8e9bc4994553amw smbd.s_door_opipe = smbd_opipe_dsrv_start();
3db3f65c6274eb042354801a308c8e9bc4994553amw if (smbd.s_door_opipe < 0) {
3db3f65c6274eb042354801a308c8e9bc4994553amw smbd_report("opipe initialization failed %s",
55bf511df53aad0fdb7eb3fa349f0308cc05234cas strerror(errno));
55bf511df53aad0fdb7eb3fa349f0308cc05234cas return (rc);
55bf511df53aad0fdb7eb3fa349f0308cc05234cas }
55bf511df53aad0fdb7eb3fa349f0308cc05234cas
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as (void) smb_lgrp_start();
7b59d02d2a384be9a08087b14defadd214b3c1ddjb
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_pwd_init(B_TRUE);
7b59d02d2a384be9a08087b14defadd214b3c1ddjb
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas rc = smbd_kernel_bind();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (rc != 0) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd_report("kernel bind error: %s", strerror(errno));
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas return (rc);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego if ((rc = smb_shr_start()) != 0) {
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego smbd_report("share initialization failed: %s", strerror(errno));
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego return (rc);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego }
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego return (0);
55bf511df53aad0fdb7eb3fa349f0308cc05234cas}
55bf511df53aad0fdb7eb3fa349f0308cc05234cas
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Close the kernel service and shutdown smbd services.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This function is registered with atexit(): ensure that anything
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * called from here is safe to be called multiple times.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_service_fini(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
3db3f65c6274eb042354801a308c8e9bc4994553amw smbd_opipe_dsrv_stop();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_wka_fini();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_refresh_fini();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_kernel_unbind();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_door_srv_stop();
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb smb_share_dsrv_stop();
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_shr_stop();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_nicmon_stop();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_idmap_stop();
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smb_lgrp_stop();
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smb_ccache_remove(SMB_CCACHE_PATH);
7b59d02d2a384be9a08087b14defadd214b3c1ddjb smb_pwd_fini();
55bf511df53aad0fdb7eb3fa349f0308cc05234cas
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smbd_refresh_init()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB service refresh thread initialization. This thread waits for a
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * refresh event and updates the daemon's view of the configuration
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * before going back to sleep.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_refresh_init()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas pthread_attr_t tattr;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas pthread_condattr_t cattr;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas int rc;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_condattr_init(&cattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_cond_init(&refresh_cond, &cattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_condattr_destroy(&cattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_mutex_init(&refresh_mutex, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_attr_init(&tattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw rc = pthread_create(&refresh_thr, &tattr, smbd_refresh_monitor, 0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_attr_destroy(&tattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smbd_refresh_fini()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Stop the refresh thread.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_refresh_fini()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_cancel(refresh_thr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_cond_destroy(&refresh_cond);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_mutex_destroy(&refresh_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smbd_refresh_monitor()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Wait for a refresh event. When this thread wakes up, update the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smbd configuration from the SMF config information then go back to
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * wait for the next refresh.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*ARGSUSED*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_refresh_monitor(void *arg)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_io_t smb_io;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas size_t len;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas char *new_dom;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas int new_secmod;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas char *old_dom;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas char fqdn[MAXHOSTNAMELEN];
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas int rc = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb bzero(&smb_io, sizeof (smb_io));
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb smb_io.sio_version = SMB_IOC_VERSION;
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_mutex_lock(&refresh_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw while (pthread_cond_wait(&refresh_cond, &refresh_mutex) == 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * We've been woken up by a refresh event so go do
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * what is necessary.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_ads_refresh();
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smb_ccache_remove(SMB_CCACHE_PATH);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if ((rc = smb_getfqdomainname(fqdn, MAXHOSTNAMELEN)) != 0)
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd_report("failed to get fully qualified domainname");
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (rc == 0)
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas /* Clear rev zone before creating if list */
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (dyndns_clear_rev_zone(fqdn) != 0)
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd_report("failed to clear DNS reverse "
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas "lookup zone");
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas /* re-initialize NIC table */
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (smb_nic_init() != 0)
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd_report("failed to get NIC information");
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_netbios_name_reconfig();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_browser_reconfig();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (rc == 0)
8c10a8659ac31335ed870a1711c0182623f72fd6as if (dyndns_update_core(fqdn) != 0)
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd_report("failed to update dynamic DNS");
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smb_set_netlogon_cred();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_load_kconfig(&smb_io.sio_data.cfg);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas new_dom = smb_io.sio_data.cfg.skc_resource_domain;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas old_dom = smbd.s_kcfg.skc_resource_domain;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas len = strlen(old_dom);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas new_secmod = smb_config_get_secmode();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if ((len != strlen(new_dom)) ||
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas (strncasecmp(new_dom, old_dom, len)) ||
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas (new_secmod != smbd.s_secmode) ||
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas (smbd.s_drv_fd == -1)) {
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas /*
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas * The active sessions have to be disconnected.
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas */
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd_kernel_unbind();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (smbd_kernel_bind()) {
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd_report("kernel bind error: %s",
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas strerror(errno));
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas continue;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas bcopy(&smb_io.sio_data.cfg, &smbd.s_kcfg, sizeof (smbd.s_kcfg));
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (ioctl(smbd.s_drv_fd, SMB_IOC_CONFIG, &smb_io) < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("configuration update ioctl: %s",
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw strerror(errno));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If the door has already been opened by another process (non-zero pid
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * in target), we assume that another smbd is already running. If there
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * is a race here, it will be caught later when smbsrv is opened because
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * only one process is allowed to open the device at a time.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_already_running(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw door_info_t info;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int door;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if ((door = open(SMB_DR_SVC_NAME, O_RDONLY)) < 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (door_info(door, &info) < 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (info.di_target > 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("already running: pid %ld\n", info.di_target);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(door);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(door);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb/*
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb * smbd_kernel_bind
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas *
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas * This function open the smbsrv device and start the kernel service.
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eassmbd_kernel_bind(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smb_io_t smb_io;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb int rc;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb bzero(&smb_io, sizeof (smb_io));
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smb_io.sio_version = SMB_IOC_VERSION;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd.s_drv_fd != -1)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(smbd.s_drv_fd);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((smbd.s_drv_fd = open(DRV_DEVICE_PATH, 0)) < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_drv_fd = -1;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (errno);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_load_kconfig(&smbd.s_kcfg);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas bcopy(&smbd.s_kcfg, &smb_io.sio_data.cfg, sizeof (smb_io.sio_data.cfg));
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if (ioctl(smbd.s_drv_fd, SMB_IOC_CONFIG, &smb_io) < 0) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb (void) close(smbd.s_drv_fd);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd.s_drv_fd = -1;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (errno);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb }
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego smb_io.sio_data.gmtoff = smbd_gmtoff();
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if (ioctl(smbd.s_drv_fd, SMB_IOC_GMTOFF, &smb_io) < 0) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb (void) close(smbd.s_drv_fd);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd.s_drv_fd = -1;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (errno);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb }
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_io.sio_data.start.opipe = smbd.s_door_opipe;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_io.sio_data.start.lmshrd = smbd.s_door_lmshr;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_io.sio_data.start.udoor = smbd.s_door_srv;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if (ioctl(smbd.s_drv_fd, SMB_IOC_START, &smb_io) < 0) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb (void) close(smbd.s_drv_fd);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd.s_drv_fd = -1;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (errno);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb }
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb rc = pthread_create(&nbt_listener, NULL, smbd_nbt_listener, NULL);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if (rc == 0) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb rc = pthread_create(&tcp_listener, NULL, smbd_tcp_listener,
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb NULL);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (rc == 0) {
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_kbound = B_TRUE;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (0);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas rc = pthread_create(&nbt_listener, NULL, smbd_nbt_listener, NULL);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (rc == 0) {
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas rc = pthread_create(&tcp_listener, NULL, smbd_tcp_listener,
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas NULL);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (rc == 0) {
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_kbound = B_TRUE;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas return (0);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb }
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb (void) close(smbd.s_drv_fd);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd.s_drv_fd = -1;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb/*
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb * smbd_kernel_unbind
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb */
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbstatic void
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbsmbd_kernel_unbind(void)
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb{
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if (smbd.s_drv_fd != -1) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb (void) close(smbd.s_drv_fd);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd.s_drv_fd = -1;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_kbound = B_FALSE;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb }
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Initialization of the localtime thread.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns 0 on success, an error number if thread creation fails.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_localtime_init(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw pthread_attr_t tattr;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int rc;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_attr_init(&tattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw rc = pthread_create(&localtime_thr, &tattr, smbd_localtime_monitor, 0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_attr_destroy(&tattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Local time thread to kernel land.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Send local gmtoff to kernel module one time at startup
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and each time it changes (up to twice a year).
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Local gmtoff is checked once every 15 minutes and
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * since some timezones are aligned on half and qtr hour boundaries,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * once an hour would likely suffice.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*ARGSUSED*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_localtime_monitor(void *arg)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw struct tm local_tm;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego time_t secs;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego int32_t gmtoff, last_gmtoff = -1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int timeout;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (;;) {
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego gmtoff = smbd_gmtoff();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((last_gmtoff != gmtoff) && (smbd.s_drv_fd != -1)) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ioctl(smbd.s_drv_fd, SMB_IOC_GMTOFF, &gmtoff) < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("localtime ioctl: %s",
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw strerror(errno));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Align the next iteration on a fifteen minute boundary.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw secs = time(0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) localtime_r(&secs, &local_tm);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sleep(timeout);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw last_gmtoff = gmtoff;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*NOTREACHED*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego/*
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego * smbd_gmtoff
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego *
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego * Determine offset from GMT. If daylight saving time use altzone,
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego * otherwise use timezone.
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego */
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borregostatic int32_t
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borregosmbd_gmtoff(void)
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego{
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego time_t clock_val;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego struct tm *atm;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego int32_t gmtoff;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego (void) time(&clock_val);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego atm = localtime(&clock_val);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego gmtoff = (atm->tm_isdst) ? altzone : timezone;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego return (gmtoff);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego}
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_sig_handler(int sigval)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd.s_sigval == 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_sigval = sigval;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Set up configuration options and parse the command line.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This function will determine if we will run as a daemon
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * or in the foreground.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Failure to find a uid or gid results in using the default (0).
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_setup_options(int argc, char *argv[])
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw struct passwd *pwd;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw struct group *grp;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int c;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((pwd = getpwnam("root")) != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_uid = pwd->pw_uid;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((grp = getgrnam("sys")) != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_gid = grp->gr_gid;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smbd.s_fg = smb_config_get_fg_flag();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw while ((c = getopt(argc, argv, ":f")) != -1) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw switch (c) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case 'f':
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_fg = 1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case ':':
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case '?':
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw default:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_usage(stderr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_usage(FILE *fp)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw static char *help[] = {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw "-f run program in foreground"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw };
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int i;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fprintf(fp, "Usage: %s [-f]\n", smbd.s_pname);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (i = 0; i < sizeof (help)/sizeof (help[0]); ++i)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fprintf(fp, " %s\n", help[i]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_report(const char *fmt, ...)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw char buf[128];
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw va_list ap;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (fmt == NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw va_start(ap, fmt);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) vsnprintf(buf, 128, fmt, ap);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw va_end(ap);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fprintf(stderr, "smbd: %s\n", buf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Enable libumem debugging by default on DEBUG builds.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#ifdef DEBUG
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwconst char *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw_umem_debug_init(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return ("default,verbose"); /* $UMEM_DEBUG setting */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwconst char *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw_umem_logging_init(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return ("fail,contents"); /* $UMEM_LOGGING setting */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#endif