smbd_main.c revision cb174861876aea6950a7ab4ce944aff84b1914cd
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/*
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <sys/types.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <sys/stat.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <sys/ioccom.h>
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright#include <sys/corectl.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>
8d7e41661dc4633488e93b13363137523ce59977jose borrego#include <atomic.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <libscf.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <limits.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <priv_utils.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <door.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <errno.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <pthread.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <time.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <libscf.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <zone.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <libgen.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <pwd.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <grp.h>
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh#include <cups/cups.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States#include <smbsrv/smb_door.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/smb_ioctl.h>
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brown#include <smbsrv/string.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libsmb.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libsmbns.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libmlsvc.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include "smbd.h"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh#define SMB_CUPS_DOCNAME "generic_doc"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#define DRV_DEVICE_PATH "/devices/pseudo/smbsrv@0:smbsrv"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#define SMB_DBDIR "/var/smb"
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh#define SMB_SPOOL_WAIT 2
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *smbd_nbt_listener(void *);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *smbd_tcp_listener(void *);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *smbd_nbt_receiver(void *);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *smbd_tcp_receiver(void *);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_daemonize_init(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_daemonize_fini(int, int);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic int smb_init_daemon_priv(int, uid_t, gid_t);
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
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshstatic int smbd_spool_init(void);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshstatic void *smbd_spool_monitor(void *arg);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshstatic pthread_t smbd_spool_thr;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_refresh_init(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_refresh_fini(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void *smbd_refresh_monitor(void *);
7f667e74610492ddbce8ce60f52ece95d2401949jose borregostatic void smbd_refresh_dc(void);
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *smbd_nbt_receiver(void *);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *smbd_nbt_listener(void *);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *smbd_tcp_receiver(void *);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *smbd_tcp_listener(void *);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borregostatic int smbd_start_listeners(void);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borregostatic void smbd_stop_listeners(void);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic int smbd_kernel_start(void);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void smbd_fatal_error(const char *);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_t refresh_thr;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_cond_t refresh_cond;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_mutex_t refresh_mutex;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statesstatic cond_t listener_cv;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statesstatic mutex_t listener_mutex;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States/*
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Mutex to ensure that smbd_service_fini() and smbd_service_init()
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * are atomic w.r.t. one another. Otherwise, if a shutdown begins
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * before initialization is complete, resources can get deallocated
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * while initialization threads are still using them.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States */
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statesstatic mutex_t smbd_service_mutex;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statesstatic cond_t smbd_service_cv;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbsmbd_t smbd;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
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;
8d7e41661dc4633488e93b13363137523ce59977jose borrego uint_t sigval;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh struct rlimit rl;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh int orig_limit;
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
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh /*
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * Raise the file descriptor limit to accommodate simultaneous user
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * authentications/file access.
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh */
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh if ((getrlimit(RLIMIT_NOFILE, &rl) == 0) &&
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh (rl.rlim_cur < rl.rlim_max)) {
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh orig_limit = rl.rlim_cur;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh rl.rlim_cur = rl.rlim_max;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh smbd_report("Failed to raise file descriptor limit"
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh " from %d to %d", orig_limit, rl.rlim_cur);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh }
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
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
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) sigaction(SIGABRT, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGTERM, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGHUP, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGINT, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGPIPE, &act, NULL);
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright (void) sigaction(SIGUSR1, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGTERM);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGHUP);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGINT);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGPIPE);
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright (void) sigdelset(&set, SIGUSR1);
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
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) atexit(smb_kmod_stop);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
8d7e41661dc4633488e93b13363137523ce59977jose borrego while (!smbd.s_shutting_down) {
8d7e41661dc4633488e93b13363137523ce59977jose borrego if (smbd.s_sigval == 0 && smbd.s_refreshes == 0)
3db3f65c6274eb042354801a308c8e9bc4994553amw (void) sigsuspend(&set);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
8d7e41661dc4633488e93b13363137523ce59977jose borrego sigval = atomic_swap_uint(&smbd.s_sigval, 0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
3db3f65c6274eb042354801a308c8e9bc4994553amw switch (sigval) {
3db3f65c6274eb042354801a308c8e9bc4994553amw case 0:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SIGPIPE:
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States case SIGABRT:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SIGHUP:
8d7e41661dc4633488e93b13363137523ce59977jose borrego syslog(LOG_DEBUG, "refresh requested");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_cond_signal(&refresh_cond);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright case SIGUSR1:
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright smb_log_dumpall();
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright break;
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw default:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Typically SIGINT or SIGTERM.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
8d7e41661dc4633488e93b13363137523ce59977jose borrego smbd.s_shutting_down = B_TRUE;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_service_fini();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw closelog();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return ((smbd.s_fatal_error) ? SMF_EXIT_ERR_FATAL : 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 */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright rc = smb_init_daemon_priv(PU_RESETGROUPS, smbd.s_uid, smbd.s_gid);
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 (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
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright/*
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * This function is based on __init_daemon_priv() and replaces
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * __init_daemon_priv() since we want smbd to have all privileges so that it
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * can execute map/unmap commands with all privileges during share
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * connection/disconnection. Unused privileges are disabled until command
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * execution. The permitted and the limit set contains all privileges. The
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * inheritable set contains no privileges.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic const char root_cp[] = "/core.%f.%t";
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic const char daemon_cp[] = "/var/tmp/core.%f.%t";
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic int
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightsmb_init_daemon_priv(int flags, uid_t uid, gid_t gid)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright priv_set_t *perm = NULL;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright int ret = -1;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright char buf[1024];
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /*
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * This is not a significant failure: it allows us to start programs
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * with sufficient privileges and with the proper uid. We don't
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * care enough about the extra groups in that case.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (flags & PU_RESETGROUPS)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) setgroups(0, NULL);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (gid != (gid_t)-1 && setgid(gid) != 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright goto end;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright perm = priv_allocset();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (perm == NULL)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright goto end;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /* E = P */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) getppriv(PRIV_PERMITTED, perm);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) setppriv(PRIV_SET, PRIV_EFFECTIVE, perm);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /* Now reset suid and euid */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (uid != (uid_t)-1 && setreuid(uid, uid) != 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright goto end;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /* I = 0 */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright priv_emptyset(perm);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright ret = setppriv(PRIV_SET, PRIV_INHERITABLE, perm);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightend:
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright priv_freeset(perm);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (core_get_process_path(buf, sizeof (buf), getpid()) == 0 &&
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright strcmp(buf, "core") == 0) {
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if ((uid == (uid_t)-1 ? geteuid() : uid) == 0) {
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) core_set_process_path(root_cp, sizeof (root_cp),
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright getpid());
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright } else {
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) core_set_process_path(daemon_cp,
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright sizeof (daemon_cp), getpid());
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright }
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright }
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) setpflags(__PROC_PROTECT, 0);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (ret);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright}
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright/*
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Most privileges, except the ones that are required for smbd, are turn off
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * in the effective set. They will be turn on when needed for command
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * execution during share connection/disconnection.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_daemonize_fini(int fd, int exit_status)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright priv_set_t *pset;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
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
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright pset = priv_allocset();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (pset == NULL)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
634e26ec75c89095090605284938356a3145f2b8Casper H.S. Dik priv_basicset(pset);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /* list of privileges for smbd */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) priv_addset(pset, PRIV_NET_MAC_AWARE);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) priv_addset(pset, PRIV_NET_PRIVADDR);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) priv_addset(pset, PRIV_PROC_AUDIT);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) priv_addset(pset, PRIV_SYS_DEVICES);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) priv_addset(pset, PRIV_SYS_SMB);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) priv_addset(pset, PRIV_SYS_MOUNT);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright priv_inverse(pset);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /* turn off unneeded privileges */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) setppriv(PRIV_OFF, PRIV_EFFECTIVE, pset);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright priv_freeset(pset);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /* reenable core dumps */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright __fini_daemon_priv(NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb/*
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb * smbd_service_init
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_service_init(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States static struct dir {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States char *name;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States int perm;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States } dir[] = {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States { SMB_DBDIR, 0700 },
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States { SMB_CVOL, 0755 },
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States { SMB_SYSROOT, 0755 },
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States { SMB_SYSTEM32, 0755 },
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States { SMB_VSS, 0755 }
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States };
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States int rc, i;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_lock(&smbd_service_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd.s_pid = getpid();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States for (i = 0; i < sizeof (dir)/sizeof (dir[0]); ++i) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if ((mkdir(dir[i].name, dir[i].perm) < 0) &&
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (errno != EEXIST)) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_report("mkdir %s: %s", dir[i].name,
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States strerror(errno));
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (-1);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States }
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");
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright smbd.s_loghd = smb_log_create(SMBD_LOGSIZE, SMBD_LOGNAME);
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brown smb_codepage_init();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright if (smbd_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI) != 0)
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright smbd_report("NIC monitor failed to start");
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) dyndns_start();
a0aa776e20803c84edd153d9cb584fd67163aef3Alan Wright smb_ipc_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
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_secmode = smb_config_get_secmode();
a0aa776e20803c84edd153d9cb584fd67163aef3Alan Wright if ((rc = smb_domain_init(smbd.s_secmode)) != 0) {
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as if (rc == SMB_DOMAIN_NOMACHINE_SID) {
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smbd_report(
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as "no machine SID: check idmap configuration");
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (-1);
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_ads_init();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (mlsvc_init() != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("msrpc initialization failed");
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego if (smbd.s_secmode == SMB_SECMODE_DOMAIN)
8d7e41661dc4633488e93b13363137523ce59977jose borrego if (smbd_locate_dc_start() != 0)
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego smbd_report("dc discovery failed %s", strerror(errno));
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd.s_door_srv = smbd_door_start();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd.s_door_opipe = smbd_opipe_start();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (smbd.s_door_srv < 0 || smbd.s_door_opipe < 0) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_report("door initialization failed %s", strerror(errno));
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (-1);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (smbd_refresh_init() != 0) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (-1);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
8d7e41661dc4633488e93b13363137523ce59977jose borrego dyndns_update_zones();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) smbd_localtime_init();
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as (void) smb_lgrp_start();
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_pwd_init(B_TRUE);
7b59d02d2a384be9a08087b14defadd214b3c1ddjb
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (smb_shr_start() != 0) {
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States smbd_report("share initialization failed: %s", strerror(errno));
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (-1);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd.s_door_lmshr = smbd_share_start();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (smbd.s_door_lmshr < 0)
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States smbd_report("share initialization failed");
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (smbd_kernel_bind() != 0) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (-1);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego }
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (smb_shr_load() != 0) {
8d7e41661dc4633488e93b13363137523ce59977jose borrego smbd_report("failed to start loading shares: %s",
8d7e41661dc4633488e93b13363137523ce59977jose borrego strerror(errno));
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (-1);
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
8d7e41661dc4633488e93b13363137523ce59977jose borrego
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh if (smbd_spool_init() != 0) {
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh smbd_report("failed to start print monitor: %s",
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh strerror(errno));
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh (void) mutex_unlock(&smbd_service_mutex);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh return (-1);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh }
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd.s_initialized = B_TRUE;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_report("service initialized");
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) cond_signal(&smbd_service_cv);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego return (0);
55bf511df53aad0fdb7eb3fa349f0308cc05234cas}
55bf511df53aad0fdb7eb3fa349f0308cc05234cas
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Shutdown smbd and smbsrv kernel services.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States *
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Shutdown will not begin until initialization has completed.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Only one thread is allowed to perform the shutdown. Other
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * threads will be blocked on fini_in_progress until the process
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * has exited.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_service_fini(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States static uint_t fini_in_progress;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_lock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States while (!smbd.s_initialized)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) cond_wait(&smbd_service_cv, &smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (atomic_swap_uint(&fini_in_progress, 1) != 0) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States while (fini_in_progress)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) cond_wait(&smbd_service_cv, &smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States /*NOTREACHED*/
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States }
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd.s_shutting_down = B_TRUE;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_report("service shutting down");
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_kmod_stop();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_logon_abort();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_lgrp_stop();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_opipe_stop();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_door_stop();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_refresh_fini();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_kernel_unbind();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_share_stop();
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_shr_stop();
8d7e41661dc4633488e93b13363137523ce59977jose borrego dyndns_stop();
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright smbd_nicmon_stop();
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smb_ccache_remove(SMB_CCACHE_PATH);
7b59d02d2a384be9a08087b14defadd214b3c1ddjb smb_pwd_fini();
a0aa776e20803c84edd153d9cb584fd67163aef3Alan Wright smb_domain_fini();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright mlsvc_fini();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smb_ads_fini();
a0aa776e20803c84edd153d9cb584fd67163aef3Alan Wright smb_netbios_stop();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd.s_initialized = B_FALSE;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_report("service terminated");
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States exit((smbd.s_fatal_error) ? SMF_EXIT_ERR_FATAL : SMF_EXIT_OK);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States}
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{
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (pthread_self() != refresh_thr) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) pthread_cancel(refresh_thr);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) pthread_cond_destroy(&refresh_cond);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) pthread_mutex_destroy(&refresh_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States }
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{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smb_kmod_cfg_t cfg;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright int error;
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
8d7e41661dc4633488e93b13363137523ce59977jose borrego while (!smbd.s_shutting_down) {
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) pthread_mutex_lock(&refresh_mutex);
8d7e41661dc4633488e93b13363137523ce59977jose borrego while ((atomic_swap_uint(&smbd.s_refreshes, 0) == 0) &&
8d7e41661dc4633488e93b13363137523ce59977jose borrego (!smbd.s_shutting_down))
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) pthread_cond_wait(&refresh_cond, &refresh_mutex);
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) pthread_mutex_unlock(&refresh_mutex);
8d7e41661dc4633488e93b13363137523ce59977jose borrego
8d7e41661dc4633488e93b13363137523ce59977jose borrego if (smbd.s_shutting_down) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_service_fini();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States /*NOTREACHED*/
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
8d7e41661dc4633488e93b13363137523ce59977jose borrego
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_lock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
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
8d7e41661dc4633488e93b13363137523ce59977jose borrego /*
8d7e41661dc4633488e93b13363137523ce59977jose borrego * Start the dyndns thread, if required.
8d7e41661dc4633488e93b13363137523ce59977jose borrego * Clear the DNS zones for the existing interfaces
8d7e41661dc4633488e93b13363137523ce59977jose borrego * before updating the NIC interface list.
8d7e41661dc4633488e93b13363137523ce59977jose borrego */
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) dyndns_start();
8d7e41661dc4633488e93b13363137523ce59977jose borrego dyndns_clear_zones();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright if (smbd_nicmon_refresh() != 0)
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright smbd_report("NIC monitor refresh failed");
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_netbios_name_reconfig();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_browser_reconfig();
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego smbd_refresh_dc();
8d7e41661dc4633488e93b13363137523ce59977jose borrego dyndns_update_zones();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&smbd_service_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
8d7e41661dc4633488e93b13363137523ce59977jose borrego if (smbd_set_netlogon_cred()) {
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas /*
8d7e41661dc4633488e93b13363137523ce59977jose borrego * Restart required because the domain changed
8d7e41661dc4633488e93b13363137523ce59977jose borrego * or the credential chain setup failed.
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas */
8d7e41661dc4633488e93b13363137523ce59977jose borrego if (smb_smf_restart_service() != 0) {
8d7e41661dc4633488e93b13363137523ce59977jose borrego syslog(LOG_ERR,
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States "unable to restart smb/server. "
8d7e41661dc4633488e93b13363137523ce59977jose borrego "Run 'svcs -xv smb/server' for more "
8d7e41661dc4633488e93b13363137523ce59977jose borrego "information.");
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_service_fini();
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States /*NOTREACHED*/
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
8d7e41661dc4633488e93b13363137523ce59977jose borrego
8d7e41661dc4633488e93b13363137523ce59977jose borrego break;
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
8d7e41661dc4633488e93b13363137523ce59977jose borrego
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (!smbd.s_kbound) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if ((error = smbd_kernel_bind()) == 0)
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) smb_shr_load();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas continue;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) smb_shr_load();
8d7e41661dc4633488e93b13363137523ce59977jose borrego
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smb_load_kconfig(&cfg);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright error = smb_kmod_setcfg(&cfg);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (error < 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd_report("configuration update failed: %s",
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright strerror(error));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
8d7e41661dc4633488e93b13363137523ce59977jose borrego
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego/*
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * Update DC information on a refresh.
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego */
7f667e74610492ddbce8ce60f52ece95d2401949jose borregostatic void
7f667e74610492ddbce8ce60f52ece95d2401949jose borregosmbd_refresh_dc(void)
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego{
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego char fqdomain[MAXHOSTNAMELEN];
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN)
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego return;
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego if (smb_getfqdomainname(fqdomain, MAXHOSTNAMELEN))
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego return;
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright if (!smb_locate_dc(fqdomain, "", NULL))
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright smbd_report("DC refresh failed");
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego}
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego
8d7e41661dc4633488e93b13363137523ce59977jose borregovoid
8d7e41661dc4633488e93b13363137523ce59977jose borregosmbd_set_secmode(int secmode)
8d7e41661dc4633488e93b13363137523ce59977jose borrego{
8d7e41661dc4633488e93b13363137523ce59977jose borrego switch (secmode) {
8d7e41661dc4633488e93b13363137523ce59977jose borrego case SMB_SECMODE_WORKGRP:
8d7e41661dc4633488e93b13363137523ce59977jose borrego case SMB_SECMODE_DOMAIN:
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) smb_config_set_secmode(secmode);
8d7e41661dc4633488e93b13363137523ce59977jose borrego smbd.s_secmode = secmode;
8d7e41661dc4633488e93b13363137523ce59977jose borrego break;
8d7e41661dc4633488e93b13363137523ce59977jose borrego
8d7e41661dc4633488e93b13363137523ce59977jose borrego default:
8d7e41661dc4633488e93b13363137523ce59977jose borrego syslog(LOG_ERR, "invalid security mode: %d", secmode);
8d7e41661dc4633488e93b13363137523ce59977jose borrego syslog(LOG_ERR, "entering maintenance mode");
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) smb_smf_maintenance_mode();
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
8d7e41661dc4633488e93b13363137523ce59977jose borrego}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States/*
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * The service is online if initialization is complete and shutdown
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * has not begun.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States */
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statesboolean_t
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statessmbd_online(void)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States{
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (smbd.s_initialized && !smbd.s_shutting_down);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States}
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
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
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if ((door = open(SMBD_DOOR_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{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright int rc;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
8d7e41661dc4633488e93b13363137523ce59977jose borrego smbd_kernel_unbind();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if ((rc = smb_kmod_bind()) == 0) {
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright rc = smbd_kernel_start();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (rc != 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smb_kmod_unbind();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright else
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd.s_kbound = B_TRUE;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (rc != 0)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd_report("kernel bind error: %s", strerror(errno));
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (rc);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright}
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic int
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightsmbd_kernel_start(void)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smb_kmod_cfg_t cfg;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright int rc;
8d7e41661dc4633488e93b13363137523ce59977jose borrego
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smb_load_kconfig(&cfg);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright rc = smb_kmod_setcfg(&cfg);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (rc != 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (rc);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright rc = smb_kmod_setgmtoff(smbd_gmtoff());
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (rc != 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (rc);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright rc = smb_kmod_start(smbd.s_door_opipe, smbd.s_door_lmshr,
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd.s_door_srv);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (rc != 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (rc);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego rc = smbd_start_listeners();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (rc != 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (rc);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb/*
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb * smbd_kernel_unbind
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb */
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbstatic void
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbsmbd_kernel_unbind(void)
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd_stop_listeners();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smb_kmod_unbind();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd.s_kbound = B_FALSE;
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright}
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh/*
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * Initialization of the spool thread.
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * Returns 0 on success, an error number if thread creation fails.
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh */
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshstatic int
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshsmbd_spool_init(void)
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh{
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh pthread_attr_t tattr;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh int rc;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh (void) pthread_attr_init(&tattr);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh rc = pthread_create(&smbd_spool_thr, &tattr, smbd_spool_monitor, 0);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh (void) pthread_attr_destroy(&tattr);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh return (rc);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh}
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh/*
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * This user thread blocks waiting for close print file
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * in the kernel. It then uses the data returned from the ioctl
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * to copy the spool file into the cups spooler.
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * This function is really only used by Vista and Win7 clients,
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * other versions of windows create only a zero size file and this
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * be removed by spoolss_copy_spool_file function.
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh */
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh/*ARGSUSED*/
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshstatic void *
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshsmbd_spool_monitor(void *arg)
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh{
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh uint32_t spool_num;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh char username[MAXNAMELEN];
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh char path[MAXPATHLEN];
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh smb_inaddr_t ipaddr;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh int error_retry_cnt = 5;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh while (!smbd.s_shutting_down && (error_retry_cnt > 0)) {
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh errno = 0;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh if (smb_kmod_get_spool_doc(&spool_num, username,
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh path, &ipaddr) == 0) {
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh spoolss_copy_spool_file(&ipaddr,
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh username, path, SMB_CUPS_DOCNAME);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh error_retry_cnt = 5;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh } else {
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh if (errno == ECANCELED)
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh break;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh (void) sleep(SMB_SPOOL_WAIT);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh error_retry_cnt--;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh }
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh }
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh return (NULL);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh}
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
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;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright int error;
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (;;) {
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego gmtoff = smbd_gmtoff();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if ((last_gmtoff != gmtoff) && smbd.s_kbound) {
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright error = smb_kmod_setgmtoff(gmtoff);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (error != 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd_report("localtime set failed: %s",
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright strerror(error));
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)
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) atomic_swap_uint(&smbd.s_sigval, sigval);
8d7e41661dc4633488e93b13363137523ce59977jose borrego
8d7e41661dc4633488e93b13363137523ce59977jose borrego if (sigval == SIGHUP) {
8d7e41661dc4633488e93b13363137523ce59977jose borrego atomic_inc_uint(&smbd.s_refreshes);
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) pthread_cond_signal(&refresh_cond);
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
8d7e41661dc4633488e93b13363137523ce59977jose borrego
8d7e41661dc4633488e93b13363137523ce59977jose borrego if (sigval == SIGINT || sigval == SIGTERM) {
8d7e41661dc4633488e93b13363137523ce59977jose borrego smbd.s_shutting_down = B_TRUE;
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) pthread_cond_signal(&refresh_cond);
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
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
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borregostatic int
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borregosmbd_start_listeners(void)
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego{
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego int rc1;
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego int rc2;
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego pthread_attr_t tattr;
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego (void) pthread_attr_init(&tattr);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego if (!smbd.s_nbt_listener_running) {
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego rc1 = pthread_create(&smbd.s_nbt_listener_id, &tattr,
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego smbd_nbt_listener, NULL);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego if (rc1 != 0)
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego smbd_report("unable to start NBT service");
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego else
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego smbd.s_nbt_listener_running = B_TRUE;
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego }
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego if (!smbd.s_tcp_listener_running) {
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego rc2 = pthread_create(&smbd.s_tcp_listener_id, &tattr,
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego smbd_tcp_listener, NULL);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego if (rc2 != 0)
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego smbd_report("unable to start TCP service");
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego else
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego smbd.s_tcp_listener_running = B_TRUE;
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego }
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego (void) pthread_attr_destroy(&tattr);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego if (rc1 != 0)
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego return (rc1);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego return (rc2);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego}
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States/*
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Stop the listener threads. In an attempt to ensure that the listener
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * threads get the signal, we use the timed wait loop to harass the
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * threads into terminating. Then, if they are still running, we make
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * one final attempt to deliver the signal before calling thread join
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * to wait for them. Note: if these threads don't terminate, smbd will
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * hang here and SMF will probably end up killing the contract.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States */
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borregostatic void
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borregosmbd_stop_listeners(void)
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego{
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States void *status;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States timestruc_t delay;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States int rc = 0;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_lock(&listener_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States while ((smbd.s_nbt_listener_running || smbd.s_tcp_listener_running) &&
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (rc != ETIME)) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (smbd.s_nbt_listener_running)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) pthread_kill(smbd.s_nbt_listener_id, SIGTERM);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (smbd.s_tcp_listener_running)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) pthread_kill(smbd.s_tcp_listener_id, SIGTERM);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States delay.tv_sec = 3;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States delay.tv_nsec = 0;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States rc = cond_reltimedwait(&listener_cv, &listener_mutex, &delay);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States }
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&listener_mutex);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego if (smbd.s_nbt_listener_running) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States syslog(LOG_WARNING, "NBT listener still running");
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego (void) pthread_kill(smbd.s_nbt_listener_id, SIGTERM);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego (void) pthread_join(smbd.s_nbt_listener_id, &status);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego smbd.s_nbt_listener_running = B_FALSE;
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego }
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego if (smbd.s_tcp_listener_running) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States syslog(LOG_WARNING, "TCP listener still running");
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego (void) pthread_kill(smbd.s_tcp_listener_id, SIGTERM);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego (void) pthread_join(smbd.s_tcp_listener_id, &status);
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego smbd.s_tcp_listener_running = B_FALSE;
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego }
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego}
a4b239dfde5f1d873c730c047e87e5714ebddbb7jose borrego
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright/*
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Perform fatal error exit.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightsmbd_fatal_error(const char *msg)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (msg == NULL)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright msg = "Fatal error";
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd_report("%s", msg);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd.s_fatal_error = B_TRUE;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) kill(smbd.s_pid, SIGTERM);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright}
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright/*ARGSUSED*/
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightsmbd_nbt_receiver(void *arg)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) smb_kmod_nbtreceive();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (NULL);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright}
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright/*ARGSUSED*/
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightsmbd_nbt_listener(void *arg)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright pthread_attr_t tattr;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright sigset_t set;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright sigset_t oset;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright pthread_t tid;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright int error = 0;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) sigfillset(&set);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) sigdelset(&set, SIGTERM);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) sigdelset(&set, SIGINT);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) pthread_sigmask(SIG_SETMASK, &set, &oset);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) pthread_attr_init(&tattr);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright while (smb_kmod_nbtlisten(error) == 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright error = pthread_create(&tid, &tattr, smbd_nbt_receiver, NULL);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) pthread_attr_destroy(&tattr);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (!smbd.s_shutting_down)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd_fatal_error("NBT listener thread terminated unexpectedly");
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_lock(&listener_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd.s_nbt_listener_running = B_FALSE;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) cond_broadcast(&listener_cv);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&listener_mutex);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (NULL);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright}
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright/*ARGSUSED*/
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightsmbd_tcp_receiver(void *arg)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) smb_kmod_tcpreceive();
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (NULL);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright}
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright/*ARGSUSED*/
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightstatic void *
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightsmbd_tcp_listener(void *arg)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright{
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright pthread_attr_t tattr;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright sigset_t set;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright sigset_t oset;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright pthread_t tid;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright int error = 0;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) sigfillset(&set);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) sigdelset(&set, SIGTERM);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) sigdelset(&set, SIGINT);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) pthread_sigmask(SIG_SETMASK, &set, &oset);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) pthread_attr_init(&tattr);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright while (smb_kmod_tcplisten(error) == 0)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright error = pthread_create(&tid, &tattr, smbd_tcp_receiver, NULL);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright (void) pthread_attr_destroy(&tattr);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if (!smbd.s_shutting_down)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright smbd_fatal_error("TCP listener thread terminated unexpectedly");
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_lock(&listener_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smbd.s_tcp_listener_running = B_FALSE;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) cond_broadcast(&listener_cv);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) mutex_unlock(&listener_mutex);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (NULL);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright}
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright
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