smbd_main.c revision 7f667e74610492ddbce8ce60f52ece95d2401949
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/*
89dc44ce9705974a8bc4a39f1e878a0491a5be61jose borrego * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Use is subject to license terms.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <sys/types.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <sys/stat.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <sys/ioccom.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <stdio.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <string.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <strings.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <stdlib.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <unistd.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <stdarg.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <fcntl.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <wait.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <signal.h>
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>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/smb_door_svc.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/smb_ioctl.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libsmb.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libsmbns.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libsmbrdr.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libmlsvc.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include "smbd.h"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#define DRV_DEVICE_PATH "/devices/pseudo/smbsrv@0:smbsrv"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#define SMB_DBDIR "/var/smb"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbextern void *smbd_nbt_listener(void *);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbextern void *smbd_tcp_listener(void *);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_daemonize_init(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_daemonize_fini(int, int);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2easstatic int smbd_kernel_bind(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_kernel_unbind(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_already_running(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_service_init(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_service_fini(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_setup_options(int argc, char *argv[]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_usage(FILE *fp);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_report(const char *fmt, ...);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_sig_handler(int sig);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borregostatic int32_t smbd_gmtoff(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_localtime_init(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void *smbd_localtime_monitor(void *arg);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_t localtime_thr;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int smbd_refresh_init(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void smbd_refresh_fini(void);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void *smbd_refresh_monitor(void *);
7f667e74610492ddbce8ce60f52ece95d2401949jose borregostatic void smbd_refresh_dc(void);
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbstatic pthread_t nbt_listener;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbstatic pthread_t tcp_listener;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_t refresh_thr;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_cond_t refresh_cond;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic pthread_mutex_t refresh_mutex;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbsmbd_t smbd;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smbd user land daemon
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Use SMF error codes only on return or exit.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwmain(int argc, char *argv[])
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas struct sigaction act;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas sigset_t set;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas uid_t uid;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas int pfd = -1;
8d7e41661dc4633488e93b13363137523ce59977jose borrego uint_t sigval;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_pname = basename(argv[0]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd_setup_options(argc, argv) != 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((uid = getuid()) != smbd.s_uid) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("user %d: %s", uid, strerror(EPERM));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (getzoneid() != GLOBAL_ZONEID) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("non-global zones are not supported");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (is_system_labeled()) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("Trusted Extensions not supported");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd_already_running())
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_OK);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigfillset(&set);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGABRT);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigfillset(&act.sa_mask);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw act.sa_handler = smbd_sig_handler;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw act.sa_flags = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGTERM, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGHUP, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGINT, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigaction(SIGPIPE, &act, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGTERM);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGHUP);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGINT);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGPIPE);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd.s_fg) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGTSTP);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGTTIN);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGTTOU);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd_service_init() != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("service initialization failed");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * "pfd" is a pipe descriptor -- any fatal errors
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * during subsequent initialization of the child
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * process should be written to this pipe and the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * parent will report this error as the exit status.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw pfd = smbd_daemonize_init();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd_service_init() != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("daemon initialization failed");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_daemonize_fini(pfd, SMF_EXIT_OK);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) atexit(smbd_service_fini);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
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:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SIGHUP:
8d7e41661dc4633488e93b13363137523ce59977jose borrego syslog(LOG_DEBUG, "refresh requested");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_cond_signal(&refresh_cond);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
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();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (SMF_EXIT_OK);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This function will fork off a child process,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * from which only the child will return.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Use SMF error codes only on exit.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_daemonize_init(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int status, pfds[2];
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw sigset_t set, oset;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw pid_t pid;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int rc;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Reset privileges to the minimum set required. We continue
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * to run as root to create and access files in /var.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw rc = __init_daemon_priv(PU_RESETGROUPS | PU_LIMITPRIVS,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_uid, smbd.s_gid,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw PRIV_NET_MAC_AWARE, PRIV_NET_PRIVADDR, PRIV_PROC_AUDIT,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw PRIV_SYS_DEVICES, PRIV_SYS_SMB, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (rc != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("insufficient privileges");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Block all signals prior to the fork and leave them blocked in the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * parent so we don't get in a situation where the parent gets SIGINT
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and returns non-zero exit status and the child is actually running.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * In the child, restore the signal mask once we've done our setsid().
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigfillset(&set);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigdelset(&set, SIGABRT);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigprocmask(SIG_BLOCK, &set, &oset);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (pipe(pfds) == -1) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("unable to create pipe");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw closelog();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((pid = fork()) == -1) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("unable to fork");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw closelog();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If we're the parent process, wait for either the child to send us
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the appropriate exit status over the pipe or for the read to fail
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * (presumably with 0 for EOF if our child terminated abnormally).
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If the read fails, exit with either the child's exit status if it
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (pid != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(pfds[1]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw _exit(status);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw _exit(WEXITSTATUS(status));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw _exit(SMF_EXIT_ERR_FATAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_pid = getpid();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) setsid();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sigprocmask(SIG_SETMASK, &oset, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) chdir("/");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) umask(022);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(pfds[0]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (pfds[1]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_daemonize_fini(int fd, int exit_status)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Now that we're running, if a pipe fd was specified, write an exit
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * status to it to indicate that our parent process can safely detach.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Then proceed to loading the remaining non-built-in modules.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (fd >= 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) write(fd, &exit_status, sizeof (exit_status));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fd);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((fd = open("/dev/null", O_RDWR)) >= 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fd);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw __fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb/*
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb * smbd_service_init
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_service_init(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas int rc;
8d7e41661dc4633488e93b13363137523ce59977jose borrego char nb_domain[NETBIOS_NAME_SZ];
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_drv_fd = -1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if ((mkdir(SMB_DBDIR, 0700) < 0) && (errno != EEXIST)) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd_report("mkdir %s: %s", SMB_DBDIR, strerror(errno));
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if ((rc = smb_ccache_init(SMB_VARRUN_DIR, SMB_CCACHE_FILE)) != 0) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if (rc == -1)
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd_report("mkdir %s: %s", SMB_VARRUN_DIR,
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb strerror(errno));
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb else
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd_report("unable to set KRB5CCNAME");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) oem_language_set("english");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (!smb_wka_init()) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("out of memory");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (smb_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI) != 0)
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd_report("NIC monitoring failed to start");
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) dyndns_start();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbrdr_init();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as if (smb_netbios_start() != 0)
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smbd_report("NetBIOS services failed to start");
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as else
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smbd_report("NetBIOS services started");
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) smb_getdomainname(nb_domain, NETBIOS_NAME_SZ);
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) utf8_strupr(nb_domain);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /* Get the ID map client handle */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((rc = smb_idmap_start()) != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("no idmap handle");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_secmode = smb_config_get_secmode();
8d7e41661dc4633488e93b13363137523ce59977jose borrego if ((rc = nt_domain_init(nb_domain, smbd.s_secmode)) != 0) {
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as if (rc == SMB_DOMAIN_NOMACHINE_SID) {
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smbd_report(
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as "no machine SID: check idmap configuration");
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as return (rc);
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_ads_init();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((rc = mlsvc_init()) != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("msrpc initialization failed");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego if (smbd.s_secmode == SMB_SECMODE_DOMAIN)
8d7e41661dc4633488e93b13363137523ce59977jose borrego if (smbd_locate_dc_start() != 0)
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego smbd_report("dc discovery failed %s", strerror(errno));
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_door_srv = smb_door_srv_start();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (smbd.s_door_srv < 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((rc = smbd_refresh_init()) != 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
8d7e41661dc4633488e93b13363137523ce59977jose borrego dyndns_update_zones();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) smbd_localtime_init();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
3db3f65c6274eb042354801a308c8e9bc4994553amw smbd.s_door_opipe = smbd_opipe_dsrv_start();
3db3f65c6274eb042354801a308c8e9bc4994553amw if (smbd.s_door_opipe < 0) {
3db3f65c6274eb042354801a308c8e9bc4994553amw smbd_report("opipe initialization failed %s",
55bf511df53aad0fdb7eb3fa349f0308cc05234cas strerror(errno));
55bf511df53aad0fdb7eb3fa349f0308cc05234cas return (rc);
55bf511df53aad0fdb7eb3fa349f0308cc05234cas }
55bf511df53aad0fdb7eb3fa349f0308cc05234cas
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as (void) smb_lgrp_start();
7b59d02d2a384be9a08087b14defadd214b3c1ddjb
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_pwd_init(B_TRUE);
7b59d02d2a384be9a08087b14defadd214b3c1ddjb
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States if ((rc = smb_shr_start()) != 0) {
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States smbd_report("share initialization failed: %s", strerror(errno));
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas return (rc);
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States smbd.s_door_lmshr = smb_share_dsrv_start();
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - 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 }
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States
8d7e41661dc4633488e93b13363137523ce59977jose borrego if ((rc = smbd_kernel_bind()) != 0) {
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States smbd_report("kernel bind error: %s", strerror(errno));
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego return (rc);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego }
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
8d7e41661dc4633488e93b13363137523ce59977jose borrego if ((rc = smb_shr_load()) != 0) {
8d7e41661dc4633488e93b13363137523ce59977jose borrego smbd_report("failed to start loading shares: %s",
8d7e41661dc4633488e93b13363137523ce59977jose borrego strerror(errno));
8d7e41661dc4633488e93b13363137523ce59977jose borrego return (rc);
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
8d7e41661dc4633488e93b13363137523ce59977jose borrego
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego return (0);
55bf511df53aad0fdb7eb3fa349f0308cc05234cas}
55bf511df53aad0fdb7eb3fa349f0308cc05234cas
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Close the kernel service and shutdown smbd services.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This function is registered with atexit(): ensure that anything
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * called from here is safe to be called multiple times.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_service_fini(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
3db3f65c6274eb042354801a308c8e9bc4994553amw smbd_opipe_dsrv_stop();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_wka_fini();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_refresh_fini();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_kernel_unbind();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_door_srv_stop();
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb smb_share_dsrv_stop();
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_shr_stop();
8d7e41661dc4633488e93b13363137523ce59977jose borrego dyndns_stop();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_nicmon_stop();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_idmap_stop();
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smb_lgrp_stop();
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smb_ccache_remove(SMB_CCACHE_PATH);
7b59d02d2a384be9a08087b14defadd214b3c1ddjb smb_pwd_fini();
89dc44ce9705974a8bc4a39f1e878a0491a5be61jose borrego nt_domain_unlink();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smbd_refresh_init()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB service refresh thread initialization. This thread waits for a
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * refresh event and updates the daemon's view of the configuration
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * before going back to sleep.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_refresh_init()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas pthread_attr_t tattr;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas pthread_condattr_t cattr;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas int rc;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_condattr_init(&cattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_cond_init(&refresh_cond, &cattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_condattr_destroy(&cattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_mutex_init(&refresh_mutex, NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_attr_init(&tattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw rc = pthread_create(&refresh_thr, &tattr, smbd_refresh_monitor, 0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_attr_destroy(&tattr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smbd_refresh_fini()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Stop the refresh thread.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_refresh_fini()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_cancel(refresh_thr);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_cond_destroy(&refresh_cond);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) pthread_mutex_destroy(&refresh_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smbd_refresh_monitor()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Wait for a refresh event. When this thread wakes up, update the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smbd configuration from the SMF config information then go back to
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * wait for the next refresh.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*ARGSUSED*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_refresh_monitor(void *arg)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_io_t smb_io;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb bzero(&smb_io, sizeof (smb_io));
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) {
8d7e41661dc4633488e93b13363137523ce59977jose borrego syslog(LOG_DEBUG, "shutting down");
8d7e41661dc4633488e93b13363137523ce59977jose borrego exit(SMF_EXIT_OK);
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
8d7e41661dc4633488e93b13363137523ce59977jose borrego
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
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas /* re-initialize NIC table */
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (smb_nic_init() != 0)
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd_report("failed to get NIC information");
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_netbios_name_reconfig();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_browser_reconfig();
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego smbd_refresh_dc();
8d7e41661dc4633488e93b13363137523ce59977jose borrego dyndns_update_zones();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
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,
8d7e41661dc4633488e93b13363137523ce59977jose borrego "unable to restart smb service. "
8d7e41661dc4633488e93b13363137523ce59977jose borrego "Run 'svcs -xv smb/server' for more "
8d7e41661dc4633488e93b13363137523ce59977jose borrego "information.");
8d7e41661dc4633488e93b13363137523ce59977jose borrego smbd.s_shutting_down = B_TRUE;
8d7e41661dc4633488e93b13363137523ce59977jose borrego exit(SMF_EXIT_OK);
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
8d7e41661dc4633488e93b13363137523ce59977jose borrego
8d7e41661dc4633488e93b13363137523ce59977jose borrego break;
8d7e41661dc4633488e93b13363137523ce59977jose borrego }
8d7e41661dc4633488e93b13363137523ce59977jose borrego
8d7e41661dc4633488e93b13363137523ce59977jose borrego if (smbd.s_drv_fd == -1) {
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (smbd_kernel_bind()) {
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd_report("kernel bind error: %s",
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas strerror(errno));
8d7e41661dc4633488e93b13363137523ce59977jose borrego } else {
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) smb_shr_load();
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas continue;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas }
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
8d7e41661dc4633488e93b13363137523ce59977jose borrego (void) smb_shr_load();
8d7e41661dc4633488e93b13363137523ce59977jose borrego
8d7e41661dc4633488e93b13363137523ce59977jose borrego smb_load_kconfig(&smb_io.sio_data.cfg);
8d7e41661dc4633488e93b13363137523ce59977jose borrego
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright if (smbd_ioctl(SMB_IOC_CONFIG, &smb_io) < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("configuration update ioctl: %s",
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw strerror(errno));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
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
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego if (smb_locate_dc(fqdomain, "", NULL))
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego smbd_report("DC discovery 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
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If the door has already been opened by another process (non-zero pid
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * in target), we assume that another smbd is already running. If there
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * is a race here, it will be caught later when smbsrv is opened because
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * only one process is allowed to open the device at a time.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_already_running(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw door_info_t info;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int door;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if ((door = open(SMB_DR_SVC_NAME, O_RDONLY)) < 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (door_info(door, &info) < 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (info.di_target > 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("already running: pid %ld\n", info.di_target);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(door);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(door);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb/*
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb * smbd_kernel_bind
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas *
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas * This function open the smbsrv device and start the kernel service.
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic int
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eassmbd_kernel_bind(void)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego pthread_attr_t tattr;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smb_io_t smb_io;
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego int rc1;
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego int rc2;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb int rc;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb bzero(&smb_io, sizeof (smb_io));
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
8d7e41661dc4633488e93b13363137523ce59977jose borrego smbd_kernel_unbind();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((smbd.s_drv_fd = open(DRV_DEVICE_PATH, 0)) < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd.s_drv_fd = -1;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (errno);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright
8d7e41661dc4633488e93b13363137523ce59977jose borrego smb_load_kconfig(&smb_io.sio_data.cfg);
8d7e41661dc4633488e93b13363137523ce59977jose borrego
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright if (smbd_ioctl(SMB_IOC_CONFIG, &smb_io) < 0) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb (void) close(smbd.s_drv_fd);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd.s_drv_fd = -1;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (errno);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb }
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego smb_io.sio_data.gmtoff = smbd_gmtoff();
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright if (smbd_ioctl(SMB_IOC_GMTOFF, &smb_io) < 0) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb (void) close(smbd.s_drv_fd);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd.s_drv_fd = -1;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (errno);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb }
3db3f65c6274eb042354801a308c8e9bc4994553amw smb_io.sio_data.start.opipe = smbd.s_door_opipe;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_io.sio_data.start.lmshrd = smbd.s_door_lmshr;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smb_io.sio_data.start.udoor = smbd.s_door_srv;
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright if (smbd_ioctl(SMB_IOC_START, &smb_io) < 0) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb (void) close(smbd.s_drv_fd);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd.s_drv_fd = -1;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (errno);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb }
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego (void) pthread_attr_init(&tattr);
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego rc1 = pthread_create(&nbt_listener, &tattr, smbd_nbt_listener, NULL);
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego if (rc1 != 0)
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego smbd_report("unable to start NBT service");
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego rc2 = pthread_create(&tcp_listener, &tattr, smbd_tcp_listener, NULL);
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego if (rc2 != 0)
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego smbd_report("unable to start TCP service");
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego (void) pthread_attr_destroy(&tattr);
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego rc = rc1;
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego if (rc == 0)
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego rc = rc2;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas if (rc == 0) {
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego smbd.s_kbound = B_TRUE;
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego return (0);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb }
b2f61ddbd28af365ab5ddf06bec0326ac7dff672jose borrego
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb (void) close(smbd.s_drv_fd);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd.s_drv_fd = -1;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb/*
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb * smbd_kernel_unbind
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb */
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbstatic void
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbsmbd_kernel_unbind(void)
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb{
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb if (smbd.s_drv_fd != -1) {
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb (void) close(smbd.s_drv_fd);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb smbd.s_drv_fd = -1;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas smbd.s_kbound = B_FALSE;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb }
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wrightint
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wrightsmbd_ioctl(int cmd, smb_io_t *smb_io)
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright{
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright smb_io->sio_version = SMB_IOC_VERSION;
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright smb_io->sio_crc = 0;
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright smb_io->sio_crc = smb_crc_gen((uint8_t *)smb_io, sizeof (smb_io_t));
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright return (ioctl(smbd.s_drv_fd, cmd, smb_io));
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright}
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright
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{
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright smb_io_t smb_io;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw struct tm local_tm;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego time_t secs;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego int32_t gmtoff, last_gmtoff = -1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int timeout;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright bzero(&smb_io, sizeof (smb_io));
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (;;) {
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego gmtoff = smbd_gmtoff();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((last_gmtoff != gmtoff) && (smbd.s_drv_fd != -1)) {
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright smb_io.sio_data.gmtoff = gmtoff;
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright if (smbd_ioctl(SMB_IOC_GMTOFF, &smb_io) < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smbd_report("localtime ioctl: %s",
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw strerror(errno));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Align the next iteration on a fifteen minute boundary.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw secs = time(0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) localtime_r(&secs, &local_tm);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) sleep(timeout);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw last_gmtoff = gmtoff;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*NOTREACHED*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego/*
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego * smbd_gmtoff
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego *
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego * Determine offset from GMT. If daylight saving time use altzone,
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego * otherwise use timezone.
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego */
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borregostatic int32_t
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borregosmbd_gmtoff(void)
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego{
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego time_t clock_val;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego struct tm *atm;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego int32_t gmtoff;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego (void) time(&clock_val);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego atm = localtime(&clock_val);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego gmtoff = (atm->tm_isdst) ? altzone : timezone;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego return (gmtoff);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego}
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmbd_sig_handler(int sigval)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smbd.s_sigval == 0)
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
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