15331Samw /*
25331Samw * CDDL HEADER START
35331Samw *
45331Samw * The contents of this file are subject to the terms of the
55331Samw * Common Development and Distribution License (the "License").
65331Samw * You may not use this file except in compliance with the License.
75331Samw *
85331Samw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw * or http://www.opensolaris.org/os/licensing.
105331Samw * See the License for the specific language governing permissions
115331Samw * and limitations under the License.
125331Samw *
135331Samw * When distributing Covered Code, include this CDDL HEADER in each
145331Samw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw * If applicable, add the following below this CDDL HEADER, with the
165331Samw * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw *
195331Samw * CDDL HEADER END
205331Samw */
215331Samw /*
2212508Samw@Sun.COM * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
235331Samw */
245331Samw
255331Samw #include <sys/types.h>
265331Samw #include <sys/stat.h>
275331Samw #include <sys/ioccom.h>
289832Samw@Sun.COM #include <sys/corectl.h>
295331Samw #include <stdio.h>
305331Samw #include <string.h>
315331Samw #include <strings.h>
325331Samw #include <stdlib.h>
335331Samw #include <unistd.h>
345331Samw #include <stdarg.h>
355331Samw #include <fcntl.h>
365331Samw #include <wait.h>
375331Samw #include <signal.h>
388334SJose.Borrego@Sun.COM #include <atomic.h>
395331Samw #include <libscf.h>
405331Samw #include <limits.h>
415331Samw #include <priv_utils.h>
425331Samw #include <door.h>
435331Samw #include <errno.h>
445331Samw #include <pthread.h>
455331Samw #include <time.h>
465331Samw #include <libscf.h>
475331Samw #include <zone.h>
485331Samw #include <libgen.h>
495331Samw #include <pwd.h>
505331Samw #include <grp.h>
5112890SJoyce.McIntosh@Sun.COM #include <cups/cups.h>
525331Samw
5311963SAfshin.Ardakani@Sun.COM #include <smbsrv/smb_door.h>
545331Samw #include <smbsrv/smb_ioctl.h>
5510966SJordan.Brown@Sun.COM #include <smbsrv/string.h>
565331Samw #include <smbsrv/libsmb.h>
575331Samw #include <smbsrv/libsmbns.h>
585331Samw #include <smbsrv/libmlsvc.h>
595331Samw #include "smbd.h"
605331Samw
6112914SJoyce.McIntosh@Sun.COM #define SMBD_ONLINE_WAIT_INTERVAL 10
62*13082SJoyce.McIntosh@Sun.COM #define SMBD_REFRESH_INTERVAL 10
635331Samw #define DRV_DEVICE_PATH "/devices/pseudo/smbsrv@0:smbsrv"
645331Samw #define SMB_DBDIR "/var/smb"
655331Samw
665331Samw static int smbd_daemonize_init(void);
675331Samw static void smbd_daemonize_fini(int, int);
689832Samw@Sun.COM static int smb_init_daemon_priv(int, uid_t, gid_t);
695331Samw
706432Sas200622 static int smbd_kernel_bind(void);
715331Samw static void smbd_kernel_unbind(void);
725331Samw static int smbd_already_running(void);
735331Samw
745331Samw static int smbd_service_init(void);
755331Samw static void smbd_service_fini(void);
765331Samw
775331Samw static int smbd_setup_options(int argc, char *argv[]);
785331Samw static void smbd_usage(FILE *fp);
795331Samw static void smbd_report(const char *fmt, ...);
805331Samw
815331Samw static void smbd_sig_handler(int sig);
825331Samw
837348SJose.Borrego@Sun.COM static int32_t smbd_gmtoff(void);
84*13082SJoyce.McIntosh@Sun.COM static void smbd_localtime_init(void);
855331Samw static void *smbd_localtime_monitor(void *arg);
865331Samw
87*13082SJoyce.McIntosh@Sun.COM static void smbd_dyndns_init(void);
88*13082SJoyce.McIntosh@Sun.COM static void smbd_load_shares(void);
8912890SJoyce.McIntosh@Sun.COM
905331Samw static int smbd_refresh_init(void);
915331Samw static void smbd_refresh_fini(void);
925331Samw static void *smbd_refresh_monitor(void *);
938670SJose.Borrego@Sun.COM
949832Samw@Sun.COM static int smbd_kernel_start(void);
959832Samw@Sun.COM
965331Samw static pthread_cond_t refresh_cond;
975331Samw static pthread_mutex_t refresh_mutex;
985331Samw
9911963SAfshin.Ardakani@Sun.COM /*
10011963SAfshin.Ardakani@Sun.COM * Mutex to ensure that smbd_service_fini() and smbd_service_init()
10111963SAfshin.Ardakani@Sun.COM * are atomic w.r.t. one another. Otherwise, if a shutdown begins
10211963SAfshin.Ardakani@Sun.COM * before initialization is complete, resources can get deallocated
10311963SAfshin.Ardakani@Sun.COM * while initialization threads are still using them.
10411963SAfshin.Ardakani@Sun.COM */
10511963SAfshin.Ardakani@Sun.COM static mutex_t smbd_service_mutex;
10611963SAfshin.Ardakani@Sun.COM static cond_t smbd_service_cv;
10711963SAfshin.Ardakani@Sun.COM
1086139Sjb150015 smbd_t smbd;
1095331Samw
1105331Samw /*
1115331Samw * Use SMF error codes only on return or exit.
1125331Samw */
1135331Samw int
main(int argc,char * argv[])1145331Samw main(int argc, char *argv[])
1155331Samw {
1166432Sas200622 struct sigaction act;
1176432Sas200622 sigset_t set;
1186432Sas200622 uid_t uid;
1196432Sas200622 int pfd = -1;
1208334SJose.Borrego@Sun.COM uint_t sigval;
12112890SJoyce.McIntosh@Sun.COM struct rlimit rl;
12212890SJoyce.McIntosh@Sun.COM int orig_limit;
1235331Samw
1245331Samw smbd.s_pname = basename(argv[0]);
1255331Samw openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
1265331Samw
1275331Samw if (smbd_setup_options(argc, argv) != 0)
1285331Samw return (SMF_EXIT_ERR_FATAL);
1295331Samw
1305331Samw if ((uid = getuid()) != smbd.s_uid) {
1315331Samw smbd_report("user %d: %s", uid, strerror(EPERM));
1325331Samw return (SMF_EXIT_ERR_FATAL);
1335331Samw }
1345331Samw
1355331Samw if (getzoneid() != GLOBAL_ZONEID) {
1365331Samw smbd_report("non-global zones are not supported");
1375331Samw return (SMF_EXIT_ERR_FATAL);
1385331Samw }
1395331Samw
1405331Samw if (is_system_labeled()) {
1415331Samw smbd_report("Trusted Extensions not supported");
1425331Samw return (SMF_EXIT_ERR_FATAL);
1435331Samw }
1445331Samw
1455331Samw if (smbd_already_running())
1465331Samw return (SMF_EXIT_OK);
1475331Samw
14812890SJoyce.McIntosh@Sun.COM /*
14912890SJoyce.McIntosh@Sun.COM * Raise the file descriptor limit to accommodate simultaneous user
15012890SJoyce.McIntosh@Sun.COM * authentications/file access.
15112890SJoyce.McIntosh@Sun.COM */
15212890SJoyce.McIntosh@Sun.COM if ((getrlimit(RLIMIT_NOFILE, &rl) == 0) &&
15312890SJoyce.McIntosh@Sun.COM (rl.rlim_cur < rl.rlim_max)) {
15412890SJoyce.McIntosh@Sun.COM orig_limit = rl.rlim_cur;
15512890SJoyce.McIntosh@Sun.COM rl.rlim_cur = rl.rlim_max;
15612890SJoyce.McIntosh@Sun.COM if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
15712890SJoyce.McIntosh@Sun.COM smbd_report("Failed to raise file descriptor limit"
15812890SJoyce.McIntosh@Sun.COM " from %d to %d", orig_limit, rl.rlim_cur);
15912890SJoyce.McIntosh@Sun.COM }
16012890SJoyce.McIntosh@Sun.COM
1615331Samw (void) sigfillset(&set);
1625331Samw (void) sigdelset(&set, SIGABRT);
1635331Samw
1645331Samw (void) sigfillset(&act.sa_mask);
1655331Samw act.sa_handler = smbd_sig_handler;
1665331Samw act.sa_flags = 0;
1675331Samw
16811963SAfshin.Ardakani@Sun.COM (void) sigaction(SIGABRT, &act, NULL);
1695331Samw (void) sigaction(SIGTERM, &act, NULL);
1705331Samw (void) sigaction(SIGHUP, &act, NULL);
1715331Samw (void) sigaction(SIGINT, &act, NULL);
1725331Samw (void) sigaction(SIGPIPE, &act, NULL);
17312508Samw@Sun.COM (void) sigaction(SIGUSR1, &act, NULL);
1745331Samw
1755331Samw (void) sigdelset(&set, SIGTERM);
1765331Samw (void) sigdelset(&set, SIGHUP);
1775331Samw (void) sigdelset(&set, SIGINT);
1785331Samw (void) sigdelset(&set, SIGPIPE);
17912508Samw@Sun.COM (void) sigdelset(&set, SIGUSR1);
1805331Samw
1815331Samw if (smbd.s_fg) {
1825331Samw (void) sigdelset(&set, SIGTSTP);
1835331Samw (void) sigdelset(&set, SIGTTIN);
1845331Samw (void) sigdelset(&set, SIGTTOU);
1855331Samw
1865331Samw if (smbd_service_init() != 0) {
1875331Samw smbd_report("service initialization failed");
1885331Samw exit(SMF_EXIT_ERR_FATAL);
1895331Samw }
1905331Samw } else {
1915331Samw /*
1925331Samw * "pfd" is a pipe descriptor -- any fatal errors
1935331Samw * during subsequent initialization of the child
1945331Samw * process should be written to this pipe and the
1955331Samw * parent will report this error as the exit status.
1965331Samw */
1975331Samw pfd = smbd_daemonize_init();
1985331Samw
1995331Samw if (smbd_service_init() != 0) {
2005331Samw smbd_report("daemon initialization failed");
2015331Samw exit(SMF_EXIT_ERR_FATAL);
2025331Samw }
2035331Samw
2045331Samw smbd_daemonize_fini(pfd, SMF_EXIT_OK);
2055331Samw }
2065331Samw
20711963SAfshin.Ardakani@Sun.COM (void) atexit(smb_kmod_stop);
2085331Samw
2098334SJose.Borrego@Sun.COM while (!smbd.s_shutting_down) {
2108334SJose.Borrego@Sun.COM if (smbd.s_sigval == 0 && smbd.s_refreshes == 0)
2117052Samw (void) sigsuspend(&set);
2125331Samw
2138334SJose.Borrego@Sun.COM sigval = atomic_swap_uint(&smbd.s_sigval, 0);
2147052Samw
2157052Samw switch (sigval) {
2165331Samw case 0:
2175331Samw case SIGPIPE:
21811963SAfshin.Ardakani@Sun.COM case SIGABRT:
2195331Samw break;
2205331Samw
2215331Samw case SIGHUP:
2228334SJose.Borrego@Sun.COM syslog(LOG_DEBUG, "refresh requested");
2235331Samw (void) pthread_cond_signal(&refresh_cond);
2245331Samw break;
2255331Samw
22612508Samw@Sun.COM case SIGUSR1:
22712508Samw@Sun.COM smb_log_dumpall();
22812508Samw@Sun.COM break;
22912508Samw@Sun.COM
2305331Samw default:
2315331Samw /*
2325331Samw * Typically SIGINT or SIGTERM.
2335331Samw */
2348334SJose.Borrego@Sun.COM smbd.s_shutting_down = B_TRUE;
2355331Samw break;
2365331Samw }
2375331Samw }
2385331Samw
2395331Samw smbd_service_fini();
2405331Samw closelog();
2419832Samw@Sun.COM return ((smbd.s_fatal_error) ? SMF_EXIT_ERR_FATAL : SMF_EXIT_OK);
2425331Samw }
2435331Samw
2445331Samw /*
2455331Samw * This function will fork off a child process,
2465331Samw * from which only the child will return.
2475331Samw *
2485331Samw * Use SMF error codes only on exit.
2495331Samw */
2505331Samw static int
smbd_daemonize_init(void)2515331Samw smbd_daemonize_init(void)
2525331Samw {
2535331Samw int status, pfds[2];
2545331Samw sigset_t set, oset;
2555331Samw pid_t pid;
2565331Samw int rc;
2575331Samw
2585331Samw /*
2595331Samw * Reset privileges to the minimum set required. We continue
2605331Samw * to run as root to create and access files in /var.
2615331Samw */
2629832Samw@Sun.COM rc = smb_init_daemon_priv(PU_RESETGROUPS, smbd.s_uid, smbd.s_gid);
2635331Samw
2645331Samw if (rc != 0) {
2655331Samw smbd_report("insufficient privileges");
2665331Samw exit(SMF_EXIT_ERR_FATAL);
2675331Samw }
2685331Samw
2695331Samw /*
2705331Samw * Block all signals prior to the fork and leave them blocked in the
2715331Samw * parent so we don't get in a situation where the parent gets SIGINT
2725331Samw * and returns non-zero exit status and the child is actually running.
2735331Samw * In the child, restore the signal mask once we've done our setsid().
2745331Samw */
2755331Samw (void) sigfillset(&set);
2765331Samw (void) sigdelset(&set, SIGABRT);
2775331Samw (void) sigprocmask(SIG_BLOCK, &set, &oset);
2785331Samw
2795331Samw if (pipe(pfds) == -1) {
2805331Samw smbd_report("unable to create pipe");
2815331Samw exit(SMF_EXIT_ERR_FATAL);
2825331Samw }
2835331Samw
2845331Samw closelog();
2855331Samw
2865331Samw if ((pid = fork()) == -1) {
2875331Samw openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
2885331Samw smbd_report("unable to fork");
2895331Samw closelog();
2905331Samw exit(SMF_EXIT_ERR_FATAL);
2915331Samw }
2925331Samw
2935331Samw /*
2945331Samw * If we're the parent process, wait for either the child to send us
2955331Samw * the appropriate exit status over the pipe or for the read to fail
2965331Samw * (presumably with 0 for EOF if our child terminated abnormally).
2975331Samw * If the read fails, exit with either the child's exit status if it
2985331Samw * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
2995331Samw */
3005331Samw if (pid != 0) {
3015331Samw (void) close(pfds[1]);
3025331Samw
3035331Samw if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
3045331Samw _exit(status);
3055331Samw
3065331Samw if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
3075331Samw _exit(WEXITSTATUS(status));
3085331Samw
3095331Samw _exit(SMF_EXIT_ERR_FATAL);
3105331Samw }
3115331Samw
3125331Samw openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
3135331Samw (void) setsid();
3145331Samw (void) sigprocmask(SIG_SETMASK, &oset, NULL);
3155331Samw (void) chdir("/");
3165331Samw (void) umask(022);
3175331Samw (void) close(pfds[0]);
3185331Samw
3195331Samw return (pfds[1]);
3205331Samw }
3215331Samw
3229832Samw@Sun.COM /*
3239832Samw@Sun.COM * This function is based on __init_daemon_priv() and replaces
3249832Samw@Sun.COM * __init_daemon_priv() since we want smbd to have all privileges so that it
3259832Samw@Sun.COM * can execute map/unmap commands with all privileges during share
3269832Samw@Sun.COM * connection/disconnection. Unused privileges are disabled until command
3279832Samw@Sun.COM * execution. The permitted and the limit set contains all privileges. The
3289832Samw@Sun.COM * inheritable set contains no privileges.
3299832Samw@Sun.COM */
3309832Samw@Sun.COM
3319832Samw@Sun.COM static const char root_cp[] = "/core.%f.%t";
3329832Samw@Sun.COM static const char daemon_cp[] = "/var/tmp/core.%f.%t";
3339832Samw@Sun.COM
3349832Samw@Sun.COM static int
smb_init_daemon_priv(int flags,uid_t uid,gid_t gid)3359832Samw@Sun.COM smb_init_daemon_priv(int flags, uid_t uid, gid_t gid)
3369832Samw@Sun.COM {
3379832Samw@Sun.COM priv_set_t *perm = NULL;
3389832Samw@Sun.COM int ret = -1;
3399832Samw@Sun.COM char buf[1024];
3409832Samw@Sun.COM
3419832Samw@Sun.COM /*
3429832Samw@Sun.COM * This is not a significant failure: it allows us to start programs
3439832Samw@Sun.COM * with sufficient privileges and with the proper uid. We don't
3449832Samw@Sun.COM * care enough about the extra groups in that case.
3459832Samw@Sun.COM */
3469832Samw@Sun.COM if (flags & PU_RESETGROUPS)
3479832Samw@Sun.COM (void) setgroups(0, NULL);
3489832Samw@Sun.COM
3499832Samw@Sun.COM if (gid != (gid_t)-1 && setgid(gid) != 0)
3509832Samw@Sun.COM goto end;
3519832Samw@Sun.COM
3529832Samw@Sun.COM perm = priv_allocset();
3539832Samw@Sun.COM if (perm == NULL)
3549832Samw@Sun.COM goto end;
3559832Samw@Sun.COM
3569832Samw@Sun.COM /* E = P */
3579832Samw@Sun.COM (void) getppriv(PRIV_PERMITTED, perm);
3589832Samw@Sun.COM (void) setppriv(PRIV_SET, PRIV_EFFECTIVE, perm);
3599832Samw@Sun.COM
3609832Samw@Sun.COM /* Now reset suid and euid */
3619832Samw@Sun.COM if (uid != (uid_t)-1 && setreuid(uid, uid) != 0)
3629832Samw@Sun.COM goto end;
3639832Samw@Sun.COM
3649832Samw@Sun.COM /* I = 0 */
3659832Samw@Sun.COM priv_emptyset(perm);
3669832Samw@Sun.COM ret = setppriv(PRIV_SET, PRIV_INHERITABLE, perm);
3679832Samw@Sun.COM end:
3689832Samw@Sun.COM priv_freeset(perm);
3699832Samw@Sun.COM
3709832Samw@Sun.COM if (core_get_process_path(buf, sizeof (buf), getpid()) == 0 &&
3719832Samw@Sun.COM strcmp(buf, "core") == 0) {
3729832Samw@Sun.COM
3739832Samw@Sun.COM if ((uid == (uid_t)-1 ? geteuid() : uid) == 0) {
3749832Samw@Sun.COM (void) core_set_process_path(root_cp, sizeof (root_cp),
3759832Samw@Sun.COM getpid());
3769832Samw@Sun.COM } else {
3779832Samw@Sun.COM (void) core_set_process_path(daemon_cp,
3789832Samw@Sun.COM sizeof (daemon_cp), getpid());
3799832Samw@Sun.COM }
3809832Samw@Sun.COM }
3819832Samw@Sun.COM (void) setpflags(__PROC_PROTECT, 0);
3829832Samw@Sun.COM
3839832Samw@Sun.COM return (ret);
3849832Samw@Sun.COM }
3859832Samw@Sun.COM
3869832Samw@Sun.COM /*
3879832Samw@Sun.COM * Most privileges, except the ones that are required for smbd, are turn off
3889832Samw@Sun.COM * in the effective set. They will be turn on when needed for command
3899832Samw@Sun.COM * execution during share connection/disconnection.
3909832Samw@Sun.COM */
3915331Samw static void
smbd_daemonize_fini(int fd,int exit_status)3925331Samw smbd_daemonize_fini(int fd, int exit_status)
3935331Samw {
3949832Samw@Sun.COM priv_set_t *pset;
3959832Samw@Sun.COM
3965331Samw /*
3975331Samw * Now that we're running, if a pipe fd was specified, write an exit
3985331Samw * status to it to indicate that our parent process can safely detach.
3995331Samw * Then proceed to loading the remaining non-built-in modules.
4005331Samw */
4015331Samw if (fd >= 0)
4025331Samw (void) write(fd, &exit_status, sizeof (exit_status));
4035331Samw
4045331Samw (void) close(fd);
4055331Samw
4069832Samw@Sun.COM pset = priv_allocset();
4079832Samw@Sun.COM if (pset == NULL)
4089832Samw@Sun.COM return;
4099832Samw@Sun.COM
41011537SCasper.Dik@Sun.COM priv_basicset(pset);
4119832Samw@Sun.COM
4129832Samw@Sun.COM /* list of privileges for smbd */
4139832Samw@Sun.COM (void) priv_addset(pset, PRIV_NET_MAC_AWARE);
4149832Samw@Sun.COM (void) priv_addset(pset, PRIV_NET_PRIVADDR);
4159832Samw@Sun.COM (void) priv_addset(pset, PRIV_PROC_AUDIT);
4169832Samw@Sun.COM (void) priv_addset(pset, PRIV_SYS_DEVICES);
4179832Samw@Sun.COM (void) priv_addset(pset, PRIV_SYS_SMB);
41811963SAfshin.Ardakani@Sun.COM (void) priv_addset(pset, PRIV_SYS_MOUNT);
4199832Samw@Sun.COM
4209832Samw@Sun.COM priv_inverse(pset);
4219832Samw@Sun.COM
4229832Samw@Sun.COM /* turn off unneeded privileges */
4239832Samw@Sun.COM (void) setppriv(PRIV_OFF, PRIV_EFFECTIVE, pset);
4249832Samw@Sun.COM
4259832Samw@Sun.COM priv_freeset(pset);
4269832Samw@Sun.COM
4279832Samw@Sun.COM /* reenable core dumps */
4289832Samw@Sun.COM __fini_daemon_priv(NULL);
4295331Samw }
4305331Samw
4316139Sjb150015 /*
4326139Sjb150015 * smbd_service_init
4336139Sjb150015 */
4345331Samw static int
smbd_service_init(void)4355331Samw smbd_service_init(void)
4365331Samw {
43711963SAfshin.Ardakani@Sun.COM static struct dir {
43811963SAfshin.Ardakani@Sun.COM char *name;
43911963SAfshin.Ardakani@Sun.COM int perm;
44011963SAfshin.Ardakani@Sun.COM } dir[] = {
44111963SAfshin.Ardakani@Sun.COM { SMB_DBDIR, 0700 },
44211963SAfshin.Ardakani@Sun.COM { SMB_CVOL, 0755 },
44311963SAfshin.Ardakani@Sun.COM { SMB_SYSROOT, 0755 },
44411963SAfshin.Ardakani@Sun.COM { SMB_SYSTEM32, 0755 },
44511963SAfshin.Ardakani@Sun.COM { SMB_VSS, 0755 }
44611963SAfshin.Ardakani@Sun.COM };
44711963SAfshin.Ardakani@Sun.COM int rc, i;
44811963SAfshin.Ardakani@Sun.COM
44911963SAfshin.Ardakani@Sun.COM (void) mutex_lock(&smbd_service_mutex);
4506432Sas200622
4519832Samw@Sun.COM smbd.s_pid = getpid();
45211963SAfshin.Ardakani@Sun.COM for (i = 0; i < sizeof (dir)/sizeof (dir[0]); ++i) {
45311963SAfshin.Ardakani@Sun.COM if ((mkdir(dir[i].name, dir[i].perm) < 0) &&
45411963SAfshin.Ardakani@Sun.COM (errno != EEXIST)) {
45511963SAfshin.Ardakani@Sun.COM smbd_report("mkdir %s: %s", dir[i].name,
45611963SAfshin.Ardakani@Sun.COM strerror(errno));
45711963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
45811963SAfshin.Ardakani@Sun.COM return (-1);
45911963SAfshin.Ardakani@Sun.COM }
4605331Samw }
4615331Samw
4626139Sjb150015 if ((rc = smb_ccache_init(SMB_VARRUN_DIR, SMB_CCACHE_FILE)) != 0) {
4636139Sjb150015 if (rc == -1)
4646139Sjb150015 smbd_report("mkdir %s: %s", SMB_VARRUN_DIR,
4656139Sjb150015 strerror(errno));
4666139Sjb150015 else
4676139Sjb150015 smbd_report("unable to set KRB5CCNAME");
46811963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
46911963SAfshin.Ardakani@Sun.COM return (-1);
4705331Samw }
4715331Samw
47212508Samw@Sun.COM smbd.s_loghd = smb_log_create(SMBD_LOGSIZE, SMBD_LOGNAME);
47310966SJordan.Brown@Sun.COM smb_codepage_init();
4745331Samw
475*13082SJoyce.McIntosh@Sun.COM rc = smbd_cups_init();
476*13082SJoyce.McIntosh@Sun.COM if (smb_config_getbool(SMB_CI_PRINT_ENABLE))
477*13082SJoyce.McIntosh@Sun.COM smbd_report("print service %savailable", (rc == 0) ? "" : "un");
478*13082SJoyce.McIntosh@Sun.COM
47912508Samw@Sun.COM if (smbd_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI) != 0)
48012508Samw@Sun.COM smbd_report("NIC monitor failed to start");
4816432Sas200622
482*13082SJoyce.McIntosh@Sun.COM smbd_dyndns_init();
48310717Samw@Sun.COM smb_ipc_init();
4845331Samw
4855772Sas200622 if (smb_netbios_start() != 0)
4865772Sas200622 smbd_report("NetBIOS services failed to start");
4875772Sas200622 else
4885772Sas200622 smbd_report("NetBIOS services started");
4895772Sas200622
4906432Sas200622 smbd.s_secmode = smb_config_get_secmode();
49110717Samw@Sun.COM if ((rc = smb_domain_init(smbd.s_secmode)) != 0) {
4925772Sas200622 if (rc == SMB_DOMAIN_NOMACHINE_SID) {
4935772Sas200622 smbd_report(
4945772Sas200622 "no machine SID: check idmap configuration");
49511963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
49611963SAfshin.Ardakani@Sun.COM return (-1);
4975772Sas200622 }
4985331Samw }
4995331Samw
50012914SJoyce.McIntosh@Sun.COM if (smbd_dc_monitor_init() != 0)
50112914SJoyce.McIntosh@Sun.COM smbd_report("DC monitor initialization failed %s",
50212914SJoyce.McIntosh@Sun.COM strerror(errno));
50312914SJoyce.McIntosh@Sun.COM
50411963SAfshin.Ardakani@Sun.COM if (mlsvc_init() != 0) {
5055331Samw smbd_report("msrpc initialization failed");
50611963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
50711963SAfshin.Ardakani@Sun.COM return (-1);
5085331Samw }
5095331Samw
51011963SAfshin.Ardakani@Sun.COM smbd.s_door_srv = smbd_door_start();
51111963SAfshin.Ardakani@Sun.COM smbd.s_door_opipe = smbd_opipe_start();
51211963SAfshin.Ardakani@Sun.COM if (smbd.s_door_srv < 0 || smbd.s_door_opipe < 0) {
51311963SAfshin.Ardakani@Sun.COM smbd_report("door initialization failed %s", strerror(errno));
51411963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
51511963SAfshin.Ardakani@Sun.COM return (-1);
51611963SAfshin.Ardakani@Sun.COM }
5175331Samw
51811963SAfshin.Ardakani@Sun.COM if (smbd_refresh_init() != 0) {
51911963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
52011963SAfshin.Ardakani@Sun.COM return (-1);
52111963SAfshin.Ardakani@Sun.COM }
5225331Samw
5238334SJose.Borrego@Sun.COM dyndns_update_zones();
524*13082SJoyce.McIntosh@Sun.COM smbd_localtime_init();
5255772Sas200622 (void) smb_lgrp_start();
5267052Samw smb_pwd_init(B_TRUE);
5276030Sjb150015
52811963SAfshin.Ardakani@Sun.COM if (smb_shr_start() != 0) {
5297961SNatalie.Li@Sun.COM smbd_report("share initialization failed: %s", strerror(errno));
53011963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
53111963SAfshin.Ardakani@Sun.COM return (-1);
5327961SNatalie.Li@Sun.COM }
5337961SNatalie.Li@Sun.COM
53411963SAfshin.Ardakani@Sun.COM smbd.s_door_lmshr = smbd_share_start();
53511963SAfshin.Ardakani@Sun.COM if (smbd.s_door_lmshr < 0)
53611963SAfshin.Ardakani@Sun.COM smbd_report("share initialization failed");
53711963SAfshin.Ardakani@Sun.COM
53811963SAfshin.Ardakani@Sun.COM if (smbd_kernel_bind() != 0) {
53911963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
54011963SAfshin.Ardakani@Sun.COM return (-1);
5416432Sas200622 }
5425331Samw
543*13082SJoyce.McIntosh@Sun.COM smbd_load_shares();
544*13082SJoyce.McIntosh@Sun.COM smbd_load_printers();
54512890SJoyce.McIntosh@Sun.COM
54611963SAfshin.Ardakani@Sun.COM smbd.s_initialized = B_TRUE;
54711963SAfshin.Ardakani@Sun.COM smbd_report("service initialized");
54811963SAfshin.Ardakani@Sun.COM (void) cond_signal(&smbd_service_cv);
54911963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
5507348SJose.Borrego@Sun.COM return (0);
5515521Sas200622 }
5525521Sas200622
5535331Samw /*
55411963SAfshin.Ardakani@Sun.COM * Shutdown smbd and smbsrv kernel services.
55511963SAfshin.Ardakani@Sun.COM *
55611963SAfshin.Ardakani@Sun.COM * Shutdown will not begin until initialization has completed.
55711963SAfshin.Ardakani@Sun.COM * Only one thread is allowed to perform the shutdown. Other
55811963SAfshin.Ardakani@Sun.COM * threads will be blocked on fini_in_progress until the process
55911963SAfshin.Ardakani@Sun.COM * has exited.
5605331Samw */
5615331Samw static void
smbd_service_fini(void)5625331Samw smbd_service_fini(void)
5635331Samw {
56411963SAfshin.Ardakani@Sun.COM static uint_t fini_in_progress;
56511963SAfshin.Ardakani@Sun.COM
56611963SAfshin.Ardakani@Sun.COM (void) mutex_lock(&smbd_service_mutex);
56711963SAfshin.Ardakani@Sun.COM
56811963SAfshin.Ardakani@Sun.COM while (!smbd.s_initialized)
56911963SAfshin.Ardakani@Sun.COM (void) cond_wait(&smbd_service_cv, &smbd_service_mutex);
57011963SAfshin.Ardakani@Sun.COM
57111963SAfshin.Ardakani@Sun.COM if (atomic_swap_uint(&fini_in_progress, 1) != 0) {
57211963SAfshin.Ardakani@Sun.COM while (fini_in_progress)
57311963SAfshin.Ardakani@Sun.COM (void) cond_wait(&smbd_service_cv, &smbd_service_mutex);
57411963SAfshin.Ardakani@Sun.COM /*NOTREACHED*/
57511963SAfshin.Ardakani@Sun.COM }
57611963SAfshin.Ardakani@Sun.COM
57711963SAfshin.Ardakani@Sun.COM smbd.s_shutting_down = B_TRUE;
57811963SAfshin.Ardakani@Sun.COM smbd_report("service shutting down");
57911963SAfshin.Ardakani@Sun.COM
58011963SAfshin.Ardakani@Sun.COM smb_kmod_stop();
58111963SAfshin.Ardakani@Sun.COM smb_logon_abort();
58211963SAfshin.Ardakani@Sun.COM smb_lgrp_stop();
58311963SAfshin.Ardakani@Sun.COM smbd_opipe_stop();
58411963SAfshin.Ardakani@Sun.COM smbd_door_stop();
5855331Samw smbd_refresh_fini();
5865331Samw smbd_kernel_unbind();
58711963SAfshin.Ardakani@Sun.COM smbd_share_stop();
5887052Samw smb_shr_stop();
5898334SJose.Borrego@Sun.COM dyndns_stop();
59012508Samw@Sun.COM smbd_nicmon_stop();
5916139Sjb150015 smb_ccache_remove(SMB_CCACHE_PATH);
5926030Sjb150015 smb_pwd_fini();
59310717Samw@Sun.COM smb_domain_fini();
5949832Samw@Sun.COM mlsvc_fini();
59510717Samw@Sun.COM smb_netbios_stop();
596*13082SJoyce.McIntosh@Sun.COM smbd_cups_fini();
59711963SAfshin.Ardakani@Sun.COM
59811963SAfshin.Ardakani@Sun.COM smbd.s_initialized = B_FALSE;
59911963SAfshin.Ardakani@Sun.COM smbd_report("service terminated");
60011963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
60111963SAfshin.Ardakani@Sun.COM exit((smbd.s_fatal_error) ? SMF_EXIT_ERR_FATAL : SMF_EXIT_OK);
6025331Samw }
6035331Samw
6045331Samw /*
6055331Samw * smbd_refresh_init()
6065331Samw *
6075331Samw * SMB service refresh thread initialization. This thread waits for a
6085331Samw * refresh event and updates the daemon's view of the configuration
6095331Samw * before going back to sleep.
6105331Samw */
6115331Samw static int
smbd_refresh_init()6125331Samw smbd_refresh_init()
6135331Samw {
6146432Sas200622 pthread_attr_t tattr;
6156432Sas200622 pthread_condattr_t cattr;
6166432Sas200622 int rc;
6175331Samw
6185331Samw (void) pthread_condattr_init(&cattr);
6195331Samw (void) pthread_cond_init(&refresh_cond, &cattr);
6205331Samw (void) pthread_condattr_destroy(&cattr);
6215331Samw
6225331Samw (void) pthread_mutex_init(&refresh_mutex, NULL);
6235331Samw
6245331Samw (void) pthread_attr_init(&tattr);
6255331Samw (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
62612914SJoyce.McIntosh@Sun.COM rc = pthread_create(&smbd.s_refresh_tid, &tattr, smbd_refresh_monitor,
62712914SJoyce.McIntosh@Sun.COM NULL);
6285331Samw (void) pthread_attr_destroy(&tattr);
6296432Sas200622
630*13082SJoyce.McIntosh@Sun.COM if (rc != 0)
631*13082SJoyce.McIntosh@Sun.COM smbd_report("unable to start refresh monitor: %s",
632*13082SJoyce.McIntosh@Sun.COM strerror(errno));
6335331Samw return (rc);
6345331Samw }
6355331Samw
6365331Samw /*
6375331Samw * smbd_refresh_fini()
6385331Samw *
6395331Samw * Stop the refresh thread.
6405331Samw */
6415331Samw static void
smbd_refresh_fini()6425331Samw smbd_refresh_fini()
6435331Samw {
644*13082SJoyce.McIntosh@Sun.COM if ((pthread_self() != smbd.s_refresh_tid) &&
645*13082SJoyce.McIntosh@Sun.COM (smbd.s_refresh_tid != 0)) {
64612914SJoyce.McIntosh@Sun.COM (void) pthread_cancel(smbd.s_refresh_tid);
64711963SAfshin.Ardakani@Sun.COM (void) pthread_cond_destroy(&refresh_cond);
64811963SAfshin.Ardakani@Sun.COM (void) pthread_mutex_destroy(&refresh_mutex);
64911963SAfshin.Ardakani@Sun.COM }
6505331Samw }
6515331Samw
6525331Samw /*
653*13082SJoyce.McIntosh@Sun.COM * Wait for refresh events. When woken up, update the smbd configuration
654*13082SJoyce.McIntosh@Sun.COM * from SMF and check for changes that require service reconfiguration.
655*13082SJoyce.McIntosh@Sun.COM * Throttling is applied to coallesce multiple refresh events when the
656*13082SJoyce.McIntosh@Sun.COM * service is being refreshed repeatedly.
6575331Samw */
6585331Samw /*ARGSUSED*/
6595331Samw static void *
smbd_refresh_monitor(void * arg)6605331Samw smbd_refresh_monitor(void *arg)
6615331Samw {
662*13082SJoyce.McIntosh@Sun.COM smbd_online_wait("smbd_refresh_monitor");
6636771Sjb150015
6648334SJose.Borrego@Sun.COM while (!smbd.s_shutting_down) {
665*13082SJoyce.McIntosh@Sun.COM (void) sleep(SMBD_REFRESH_INTERVAL);
666*13082SJoyce.McIntosh@Sun.COM
6678334SJose.Borrego@Sun.COM (void) pthread_mutex_lock(&refresh_mutex);
6688334SJose.Borrego@Sun.COM while ((atomic_swap_uint(&smbd.s_refreshes, 0) == 0) &&
6698334SJose.Borrego@Sun.COM (!smbd.s_shutting_down))
6708334SJose.Borrego@Sun.COM (void) pthread_cond_wait(&refresh_cond, &refresh_mutex);
6718334SJose.Borrego@Sun.COM (void) pthread_mutex_unlock(&refresh_mutex);
6728334SJose.Borrego@Sun.COM
6738334SJose.Borrego@Sun.COM if (smbd.s_shutting_down) {
67411963SAfshin.Ardakani@Sun.COM smbd_service_fini();
67511963SAfshin.Ardakani@Sun.COM /*NOTREACHED*/
6768334SJose.Borrego@Sun.COM }
6778334SJose.Borrego@Sun.COM
67811963SAfshin.Ardakani@Sun.COM (void) mutex_lock(&smbd_service_mutex);
67911963SAfshin.Ardakani@Sun.COM
680*13082SJoyce.McIntosh@Sun.COM smbd_dc_monitor_refresh();
6816139Sjb150015 smb_ccache_remove(SMB_CCACHE_PATH);
6826432Sas200622
6838334SJose.Borrego@Sun.COM /*
6848334SJose.Borrego@Sun.COM * Clear the DNS zones for the existing interfaces
6858334SJose.Borrego@Sun.COM * before updating the NIC interface list.
6868334SJose.Borrego@Sun.COM */
6878334SJose.Borrego@Sun.COM dyndns_clear_zones();
6886432Sas200622
68912508Samw@Sun.COM if (smbd_nicmon_refresh() != 0)
69012508Samw@Sun.COM smbd_report("NIC monitor refresh failed");
691*13082SJoyce.McIntosh@Sun.COM
6926432Sas200622 smb_netbios_name_reconfig();
6936432Sas200622 smb_browser_reconfig();
6948334SJose.Borrego@Sun.COM dyndns_update_zones();
695*13082SJoyce.McIntosh@Sun.COM (void) smbd_kernel_bind();
696*13082SJoyce.McIntosh@Sun.COM smbd_load_shares();
697*13082SJoyce.McIntosh@Sun.COM smbd_load_printers();
6986432Sas200622
69911963SAfshin.Ardakani@Sun.COM (void) mutex_unlock(&smbd_service_mutex);
7005331Samw }
7018334SJose.Borrego@Sun.COM
702*13082SJoyce.McIntosh@Sun.COM smbd.s_refresh_tid = 0;
7035331Samw return (NULL);
7045331Samw }
7055331Samw
7068334SJose.Borrego@Sun.COM void
smbd_set_secmode(int secmode)7078334SJose.Borrego@Sun.COM smbd_set_secmode(int secmode)
7088334SJose.Borrego@Sun.COM {
7098334SJose.Borrego@Sun.COM switch (secmode) {
7108334SJose.Borrego@Sun.COM case SMB_SECMODE_WORKGRP:
7118334SJose.Borrego@Sun.COM case SMB_SECMODE_DOMAIN:
7128334SJose.Borrego@Sun.COM (void) smb_config_set_secmode(secmode);
7138334SJose.Borrego@Sun.COM smbd.s_secmode = secmode;
7148334SJose.Borrego@Sun.COM break;
7158334SJose.Borrego@Sun.COM
7168334SJose.Borrego@Sun.COM default:
7178334SJose.Borrego@Sun.COM syslog(LOG_ERR, "invalid security mode: %d", secmode);
7188334SJose.Borrego@Sun.COM syslog(LOG_ERR, "entering maintenance mode");
7198334SJose.Borrego@Sun.COM (void) smb_smf_maintenance_mode();
7208334SJose.Borrego@Sun.COM }
7218334SJose.Borrego@Sun.COM }
7225331Samw
7235331Samw /*
72411963SAfshin.Ardakani@Sun.COM * The service is online if initialization is complete and shutdown
72511963SAfshin.Ardakani@Sun.COM * has not begun.
72611963SAfshin.Ardakani@Sun.COM */
72711963SAfshin.Ardakani@Sun.COM boolean_t
smbd_online(void)72811963SAfshin.Ardakani@Sun.COM smbd_online(void)
72911963SAfshin.Ardakani@Sun.COM {
73011963SAfshin.Ardakani@Sun.COM return (smbd.s_initialized && !smbd.s_shutting_down);
73111963SAfshin.Ardakani@Sun.COM }
73211963SAfshin.Ardakani@Sun.COM
73311963SAfshin.Ardakani@Sun.COM /*
73412914SJoyce.McIntosh@Sun.COM * Wait until the service is online. Provided for threads that
73512914SJoyce.McIntosh@Sun.COM * should wait until the service has been fully initialized before
73612914SJoyce.McIntosh@Sun.COM * they start performing operations.
73712914SJoyce.McIntosh@Sun.COM */
73812914SJoyce.McIntosh@Sun.COM void
smbd_online_wait(const char * text)73912914SJoyce.McIntosh@Sun.COM smbd_online_wait(const char *text)
74012914SJoyce.McIntosh@Sun.COM {
741*13082SJoyce.McIntosh@Sun.COM while (!smbd_online())
742*13082SJoyce.McIntosh@Sun.COM (void) sleep(SMBD_ONLINE_WAIT_INTERVAL);
74312914SJoyce.McIntosh@Sun.COM
744*13082SJoyce.McIntosh@Sun.COM if (text != NULL) {
745*13082SJoyce.McIntosh@Sun.COM smb_log(smbd.s_loghd, LOG_DEBUG, "%s: online", text);
746*13082SJoyce.McIntosh@Sun.COM (void) fprintf(stderr, "%s: online\n", text);
74712914SJoyce.McIntosh@Sun.COM }
74812914SJoyce.McIntosh@Sun.COM }
74912914SJoyce.McIntosh@Sun.COM
75012914SJoyce.McIntosh@Sun.COM /*
7515331Samw * If the door has already been opened by another process (non-zero pid
7525331Samw * in target), we assume that another smbd is already running. If there
7535331Samw * is a race here, it will be caught later when smbsrv is opened because
7545331Samw * only one process is allowed to open the device at a time.
7555331Samw */
7565331Samw static int
smbd_already_running(void)7575331Samw smbd_already_running(void)
7585331Samw {
7595331Samw door_info_t info;
7605331Samw int door;
7615331Samw
76211963SAfshin.Ardakani@Sun.COM if ((door = open(SMBD_DOOR_NAME, O_RDONLY)) < 0)
7635331Samw return (0);
7645331Samw
7655331Samw if (door_info(door, &info) < 0)
7665331Samw return (0);
7675331Samw
7685331Samw if (info.di_target > 0) {
7695331Samw smbd_report("already running: pid %ld\n", info.di_target);
7705331Samw (void) close(door);
7715331Samw return (1);
7725331Samw }
7735331Samw
7745331Samw (void) close(door);
7755331Samw return (0);
7765331Samw }
7775331Samw
7786139Sjb150015 /*
7796139Sjb150015 * smbd_kernel_bind
7806432Sas200622 *
781*13082SJoyce.McIntosh@Sun.COM * If smbsrv is already bound, reload the configuration and update smbsrv.
782*13082SJoyce.McIntosh@Sun.COM * Otherwise, open the smbsrv device and start the kernel service.
7836139Sjb150015 */
7845331Samw static int
smbd_kernel_bind(void)7856432Sas200622 smbd_kernel_bind(void)
7865331Samw {
787*13082SJoyce.McIntosh@Sun.COM smb_kmod_cfg_t cfg;
788*13082SJoyce.McIntosh@Sun.COM int rc;
7896139Sjb150015
790*13082SJoyce.McIntosh@Sun.COM if (smbd.s_kbound) {
791*13082SJoyce.McIntosh@Sun.COM smb_load_kconfig(&cfg);
792*13082SJoyce.McIntosh@Sun.COM rc = smb_kmod_setcfg(&cfg);
793*13082SJoyce.McIntosh@Sun.COM if (rc < 0)
794*13082SJoyce.McIntosh@Sun.COM smbd_report("kernel configuration update failed: %s",
795*13082SJoyce.McIntosh@Sun.COM strerror(errno));
796*13082SJoyce.McIntosh@Sun.COM return (rc);
797*13082SJoyce.McIntosh@Sun.COM }
798*13082SJoyce.McIntosh@Sun.COM
799*13082SJoyce.McIntosh@Sun.COM if (smb_kmod_isbound())
800*13082SJoyce.McIntosh@Sun.COM smbd_kernel_unbind();
8015331Samw
80211963SAfshin.Ardakani@Sun.COM if ((rc = smb_kmod_bind()) == 0) {
8039832Samw@Sun.COM rc = smbd_kernel_start();
8049832Samw@Sun.COM if (rc != 0)
8059832Samw@Sun.COM smb_kmod_unbind();
8069832Samw@Sun.COM else
8079832Samw@Sun.COM smbd.s_kbound = B_TRUE;
8086139Sjb150015 }
80911963SAfshin.Ardakani@Sun.COM
81011963SAfshin.Ardakani@Sun.COM if (rc != 0)
81111963SAfshin.Ardakani@Sun.COM smbd_report("kernel bind error: %s", strerror(errno));
8129832Samw@Sun.COM return (rc);
8139832Samw@Sun.COM }
8149832Samw@Sun.COM
8159832Samw@Sun.COM static int
smbd_kernel_start(void)8169832Samw@Sun.COM smbd_kernel_start(void)
8179832Samw@Sun.COM {
8189832Samw@Sun.COM smb_kmod_cfg_t cfg;
8199832Samw@Sun.COM int rc;
8209832Samw@Sun.COM
8219832Samw@Sun.COM smb_load_kconfig(&cfg);
8229832Samw@Sun.COM rc = smb_kmod_setcfg(&cfg);
8239832Samw@Sun.COM if (rc != 0)
8249832Samw@Sun.COM return (rc);
8259832Samw@Sun.COM
8269832Samw@Sun.COM rc = smb_kmod_setgmtoff(smbd_gmtoff());
8279832Samw@Sun.COM if (rc != 0)
8289832Samw@Sun.COM return (rc);
8299832Samw@Sun.COM
8309832Samw@Sun.COM rc = smb_kmod_start(smbd.s_door_opipe, smbd.s_door_lmshr,
8319832Samw@Sun.COM smbd.s_door_srv);
8326139Sjb150015
8339832Samw@Sun.COM if (rc != 0)
8349832Samw@Sun.COM return (rc);
8359832Samw@Sun.COM
836*13082SJoyce.McIntosh@Sun.COM smbd_spool_init();
8379832Samw@Sun.COM return (0);
8385331Samw }
8395331Samw
8406139Sjb150015 /*
8416139Sjb150015 * smbd_kernel_unbind
8426139Sjb150015 */
8436139Sjb150015 static void
smbd_kernel_unbind(void)8446139Sjb150015 smbd_kernel_unbind(void)
8456139Sjb150015 {
846*13082SJoyce.McIntosh@Sun.COM smbd_spool_fini();
8479832Samw@Sun.COM smb_kmod_unbind();
8489832Samw@Sun.COM smbd.s_kbound = B_FALSE;
8498167Samw@Sun.COM }
8508167Samw@Sun.COM
8515331Samw /*
852*13082SJoyce.McIntosh@Sun.COM * Create the Dynamic DNS publisher thread.
85312890SJoyce.McIntosh@Sun.COM */
854*13082SJoyce.McIntosh@Sun.COM static void
smbd_dyndns_init(void)855*13082SJoyce.McIntosh@Sun.COM smbd_dyndns_init(void)
85612890SJoyce.McIntosh@Sun.COM {
857*13082SJoyce.McIntosh@Sun.COM pthread_t tid;
858*13082SJoyce.McIntosh@Sun.COM pthread_attr_t attr;
859*13082SJoyce.McIntosh@Sun.COM int rc;
860*13082SJoyce.McIntosh@Sun.COM
861*13082SJoyce.McIntosh@Sun.COM dyndns_start();
86212890SJoyce.McIntosh@Sun.COM
863*13082SJoyce.McIntosh@Sun.COM (void) pthread_attr_init(&attr);
864*13082SJoyce.McIntosh@Sun.COM (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
865*13082SJoyce.McIntosh@Sun.COM rc = pthread_create(&tid, &attr, dyndns_publisher, NULL);
866*13082SJoyce.McIntosh@Sun.COM (void) pthread_attr_destroy(&attr);
86712890SJoyce.McIntosh@Sun.COM
868*13082SJoyce.McIntosh@Sun.COM if (rc != 0)
869*13082SJoyce.McIntosh@Sun.COM smbd_report("unable to start dyndns publisher: %s",
870*13082SJoyce.McIntosh@Sun.COM strerror(errno));
87112890SJoyce.McIntosh@Sun.COM }
87212890SJoyce.McIntosh@Sun.COM
87312890SJoyce.McIntosh@Sun.COM /*
874*13082SJoyce.McIntosh@Sun.COM * Launches a thread to populate the share cache by share information
875*13082SJoyce.McIntosh@Sun.COM * stored in sharemgr
87612890SJoyce.McIntosh@Sun.COM */
877*13082SJoyce.McIntosh@Sun.COM static void
smbd_load_shares(void)878*13082SJoyce.McIntosh@Sun.COM smbd_load_shares(void)
87912890SJoyce.McIntosh@Sun.COM {
880*13082SJoyce.McIntosh@Sun.COM pthread_t tid;
881*13082SJoyce.McIntosh@Sun.COM pthread_attr_t attr;
882*13082SJoyce.McIntosh@Sun.COM int rc;
88312890SJoyce.McIntosh@Sun.COM
884*13082SJoyce.McIntosh@Sun.COM (void) pthread_attr_init(&attr);
885*13082SJoyce.McIntosh@Sun.COM (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
886*13082SJoyce.McIntosh@Sun.COM rc = pthread_create(&tid, &attr, smb_shr_load, NULL);
887*13082SJoyce.McIntosh@Sun.COM (void) pthread_attr_destroy(&attr);
88812890SJoyce.McIntosh@Sun.COM
889*13082SJoyce.McIntosh@Sun.COM if (rc != 0)
890*13082SJoyce.McIntosh@Sun.COM smbd_report("unable to load disk shares: %s", strerror(errno));
89112890SJoyce.McIntosh@Sun.COM }
89212890SJoyce.McIntosh@Sun.COM
89312890SJoyce.McIntosh@Sun.COM /*
8945331Samw * Initialization of the localtime thread.
8955331Samw * Returns 0 on success, an error number if thread creation fails.
8965331Samw */
8975331Samw
898*13082SJoyce.McIntosh@Sun.COM static void
smbd_localtime_init(void)8995331Samw smbd_localtime_init(void)
9005331Samw {
901*13082SJoyce.McIntosh@Sun.COM pthread_attr_t attr;
902*13082SJoyce.McIntosh@Sun.COM int rc;
9035331Samw
904*13082SJoyce.McIntosh@Sun.COM (void) pthread_attr_init(&attr);
905*13082SJoyce.McIntosh@Sun.COM (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
906*13082SJoyce.McIntosh@Sun.COM rc = pthread_create(&smbd.s_localtime_tid, &attr,
90712914SJoyce.McIntosh@Sun.COM smbd_localtime_monitor, NULL);
908*13082SJoyce.McIntosh@Sun.COM (void) pthread_attr_destroy(&attr);
909*13082SJoyce.McIntosh@Sun.COM
910*13082SJoyce.McIntosh@Sun.COM if (rc != 0)
911*13082SJoyce.McIntosh@Sun.COM smbd_report("unable to monitor localtime: %s", strerror(errno));
9125331Samw }
9135331Samw
9145331Samw /*
915*13082SJoyce.McIntosh@Sun.COM * Send local gmtoff to the kernel module one time at startup and each
916*13082SJoyce.McIntosh@Sun.COM * time it changes (up to twice a year).
917*13082SJoyce.McIntosh@Sun.COM * Local gmtoff is checked once every 15 minutes since some timezones
918*13082SJoyce.McIntosh@Sun.COM * are aligned on half and quarter hour boundaries.
9195331Samw */
9205331Samw /*ARGSUSED*/
9215331Samw static void *
smbd_localtime_monitor(void * arg)9225331Samw smbd_localtime_monitor(void *arg)
9235331Samw {
9245331Samw struct tm local_tm;
9257348SJose.Borrego@Sun.COM time_t secs;
9267348SJose.Borrego@Sun.COM int32_t gmtoff, last_gmtoff = -1;
9275331Samw int timeout;
9289832Samw@Sun.COM int error;
9298167Samw@Sun.COM
93012914SJoyce.McIntosh@Sun.COM smbd_online_wait("smbd_localtime_monitor");
93112914SJoyce.McIntosh@Sun.COM
9325331Samw for (;;) {
9337348SJose.Borrego@Sun.COM gmtoff = smbd_gmtoff();
9345331Samw
9359832Samw@Sun.COM if ((last_gmtoff != gmtoff) && smbd.s_kbound) {
9369832Samw@Sun.COM error = smb_kmod_setgmtoff(gmtoff);
9379832Samw@Sun.COM if (error != 0)
9389832Samw@Sun.COM smbd_report("localtime set failed: %s",
9399832Samw@Sun.COM strerror(error));
9405331Samw }
9415331Samw
9425331Samw /*
9435331Samw * Align the next iteration on a fifteen minute boundary.
9445331Samw */
9455331Samw secs = time(0);
9465331Samw (void) localtime_r(&secs, &local_tm);
9475331Samw timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN);
9485331Samw (void) sleep(timeout);
9495331Samw
9505331Samw last_gmtoff = gmtoff;
9515331Samw }
9525331Samw
9535331Samw /*NOTREACHED*/
9545331Samw return (NULL);
9555331Samw }
9565331Samw
9577348SJose.Borrego@Sun.COM /*
9587348SJose.Borrego@Sun.COM * smbd_gmtoff
9597348SJose.Borrego@Sun.COM *
9607348SJose.Borrego@Sun.COM * Determine offset from GMT. If daylight saving time use altzone,
9617348SJose.Borrego@Sun.COM * otherwise use timezone.
9627348SJose.Borrego@Sun.COM */
9637348SJose.Borrego@Sun.COM static int32_t
smbd_gmtoff(void)9647348SJose.Borrego@Sun.COM smbd_gmtoff(void)
9657348SJose.Borrego@Sun.COM {
9667348SJose.Borrego@Sun.COM time_t clock_val;
9677348SJose.Borrego@Sun.COM struct tm *atm;
9687348SJose.Borrego@Sun.COM int32_t gmtoff;
9697348SJose.Borrego@Sun.COM
9707348SJose.Borrego@Sun.COM (void) time(&clock_val);
9717348SJose.Borrego@Sun.COM atm = localtime(&clock_val);
9727348SJose.Borrego@Sun.COM
9737348SJose.Borrego@Sun.COM gmtoff = (atm->tm_isdst) ? altzone : timezone;
9747348SJose.Borrego@Sun.COM
9757348SJose.Borrego@Sun.COM return (gmtoff);
9767348SJose.Borrego@Sun.COM }
9777348SJose.Borrego@Sun.COM
9785331Samw static void
smbd_sig_handler(int sigval)9795331Samw smbd_sig_handler(int sigval)
9805331Samw {
9815331Samw if (smbd.s_sigval == 0)
9828334SJose.Borrego@Sun.COM (void) atomic_swap_uint(&smbd.s_sigval, sigval);
9838334SJose.Borrego@Sun.COM
9848334SJose.Borrego@Sun.COM if (sigval == SIGHUP) {
9858334SJose.Borrego@Sun.COM atomic_inc_uint(&smbd.s_refreshes);
9868334SJose.Borrego@Sun.COM (void) pthread_cond_signal(&refresh_cond);
9878334SJose.Borrego@Sun.COM }
9888334SJose.Borrego@Sun.COM
9898334SJose.Borrego@Sun.COM if (sigval == SIGINT || sigval == SIGTERM) {
9908334SJose.Borrego@Sun.COM smbd.s_shutting_down = B_TRUE;
9918334SJose.Borrego@Sun.COM (void) pthread_cond_signal(&refresh_cond);
9928334SJose.Borrego@Sun.COM }
9935331Samw }
9945331Samw
9955331Samw /*
9965331Samw * Set up configuration options and parse the command line.
9975331Samw * This function will determine if we will run as a daemon
9985331Samw * or in the foreground.
9995331Samw *
10005331Samw * Failure to find a uid or gid results in using the default (0).
10015331Samw */
10025331Samw static int
smbd_setup_options(int argc,char * argv[])10035331Samw smbd_setup_options(int argc, char *argv[])
10045331Samw {
10055331Samw struct passwd *pwd;
10065331Samw struct group *grp;
10075331Samw int c;
10085331Samw
10095331Samw if ((pwd = getpwnam("root")) != NULL)
10105331Samw smbd.s_uid = pwd->pw_uid;
10115331Samw
10125331Samw if ((grp = getgrnam("sys")) != NULL)
10135331Samw smbd.s_gid = grp->gr_gid;
10145331Samw
10155772Sas200622 smbd.s_fg = smb_config_get_fg_flag();
10165331Samw
10175331Samw while ((c = getopt(argc, argv, ":f")) != -1) {
10185331Samw switch (c) {
10195331Samw case 'f':
10205331Samw smbd.s_fg = 1;
10215331Samw break;
10225331Samw
10235331Samw case ':':
10245331Samw case '?':
10255331Samw default:
10265331Samw smbd_usage(stderr);
10275331Samw return (-1);
10285331Samw }
10295331Samw }
10305331Samw
10315331Samw return (0);
10325331Samw }
10335331Samw
10345331Samw static void
smbd_usage(FILE * fp)10355331Samw smbd_usage(FILE *fp)
10365331Samw {
10375331Samw static char *help[] = {
10385331Samw "-f run program in foreground"
10395331Samw };
10405331Samw
10415331Samw int i;
10425331Samw
10435331Samw (void) fprintf(fp, "Usage: %s [-f]\n", smbd.s_pname);
10445331Samw
10455331Samw for (i = 0; i < sizeof (help)/sizeof (help[0]); ++i)
10465331Samw (void) fprintf(fp, " %s\n", help[i]);
10475331Samw }
10485331Samw
10495331Samw static void
smbd_report(const char * fmt,...)10505331Samw smbd_report(const char *fmt, ...)
10515331Samw {
10525331Samw char buf[128];
10535331Samw va_list ap;
10545331Samw
10555331Samw if (fmt == NULL)
10565331Samw return;
10575331Samw
10585331Samw va_start(ap, fmt);
10595331Samw (void) vsnprintf(buf, 128, fmt, ap);
10605331Samw va_end(ap);
10615331Samw
10625331Samw (void) fprintf(stderr, "smbd: %s\n", buf);
10635331Samw }
10645331Samw
10659832Samw@Sun.COM /*
10665331Samw * Enable libumem debugging by default on DEBUG builds.
10675331Samw */
10685331Samw #ifdef DEBUG
10695331Samw const char *
_umem_debug_init(void)10705331Samw _umem_debug_init(void)
10715331Samw {
10725331Samw return ("default,verbose"); /* $UMEM_DEBUG setting */
10735331Samw }
10745331Samw
10755331Samw const char *
_umem_logging_init(void)10765331Samw _umem_logging_init(void)
10775331Samw {
10785331Samw return ("fail,contents"); /* $UMEM_LOGGING setting */
10795331Samw }
10805331Samw #endif
1081