4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
26 #include <sys/types.h>
28 #include <sys/ioccom.h>
29 #include <sys/corectl.h>
42 #include <priv_utils.h>
53 #include <smbsrv/smb_door.h>
54 #include <smbsrv/smb_ioctl.h>
55 #include <smbsrv/string.h>
56 #include <smbsrv/libsmb.h>
57 #include <smbsrv/libsmbns.h>
58 #include <smbsrv/libmlsvc.h>
62 #define SMBD_ONLINE_WAIT_INTERVAL 10
63 #define SMBD_REFRESH_INTERVAL 10
64 #define SMB_DBDIR "/var/smb"
66 static int smbd_daemonize_init(void);
67 static void smbd_daemonize_fini(int, int);
68 static int smb_init_daemon_priv(int, uid_t
, gid_t
);
70 static int smbd_kernel_bind(void);
71 static void smbd_kernel_unbind(void);
72 static int smbd_already_running(void);
74 static int smbd_service_init(void);
75 static void smbd_service_fini(void);
77 static int smbd_setup_options(int argc
, char *argv
[]);
78 static void smbd_usage(FILE *fp
);
80 static int32_t smbd_gmtoff(void);
81 static void smbd_localtime_init(void);
82 static void *smbd_localtime_monitor(void *arg
);
84 static void smbd_dyndns_init(void);
85 static void smbd_load_shares(void);
86 static void *smbd_share_loader(void *);
88 static void smbd_refresh_handler(void);
90 static int smbd_kernel_start(void);
95 * Use SMF error codes only on return or exit.
98 main(int argc
, char *argv
[])
107 smbd
.s_pname
= basename(argv
[0]);
108 openlog(smbd
.s_pname
, LOG_PID
| LOG_NOWAIT
, LOG_DAEMON
);
110 if (smbd_setup_options(argc
, argv
) != 0)
111 return (SMF_EXIT_ERR_FATAL
);
113 if ((uid
= getuid()) != smbd
.s_uid
) {
114 smbd_report("user %d: %s", uid
, strerror(EPERM
));
115 return (SMF_EXIT_ERR_FATAL
);
118 if (smbd_already_running())
119 return (SMF_EXIT_OK
);
122 * Raise the file descriptor limit to accommodate simultaneous user
123 * authentications/file access.
125 if ((getrlimit(RLIMIT_NOFILE
, &rl
) == 0) &&
126 (rl
.rlim_cur
< rl
.rlim_max
)) {
127 orig_limit
= rl
.rlim_cur
;
128 rl
.rlim_cur
= rl
.rlim_max
;
129 if (setrlimit(RLIMIT_NOFILE
, &rl
) != 0)
130 smbd_report("Failed to raise file descriptor limit"
131 " from %d to %d", orig_limit
, rl
.rlim_cur
);
135 * Block async signals in all threads.
137 (void) sigemptyset(&set
);
139 (void) sigaddset(&set
, SIGHUP
);
140 (void) sigaddset(&set
, SIGINT
);
141 (void) sigaddset(&set
, SIGQUIT
);
142 (void) sigaddset(&set
, SIGPIPE
);
143 (void) sigaddset(&set
, SIGTERM
);
144 (void) sigaddset(&set
, SIGUSR1
);
145 (void) sigaddset(&set
, SIGUSR2
);
147 (void) sigprocmask(SIG_SETMASK
, &set
, NULL
);
150 if (smbd_service_init() != 0) {
151 smbd_report("service initialization failed");
152 exit(SMF_EXIT_ERR_FATAL
);
156 * "pfd" is a pipe descriptor -- any fatal errors
157 * during subsequent initialization of the child
158 * process should be written to this pipe and the
159 * parent will report this error as the exit status.
161 pfd
= smbd_daemonize_init();
163 if (smbd_service_init() != 0) {
164 smbd_report("daemon initialization failed");
165 exit(SMF_EXIT_ERR_FATAL
);
168 smbd_daemonize_fini(pfd
, SMF_EXIT_OK
);
171 while (!smbd
.s_shutting_down
) {
172 sigval
= sigwait(&set
);
176 syslog(LOG_DEBUG
, "sigwait failed: %s",
183 syslog(LOG_DEBUG
, "refresh requested");
184 smbd_refresh_handler();
188 syslog(LOG_DEBUG
, "SIGUSR1 ignored");
193 * Typically SIGINT or SIGTERM.
195 smbd
.s_shutting_down
= B_TRUE
;
201 * Allow termination signals while shutting down.
203 (void) sigemptyset(&set
);
206 (void) sigaddset(&set
, SIGHUP
);
207 (void) sigaddset(&set
, SIGINT
);
209 (void) sigaddset(&set
, SIGTERM
);
211 (void) sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
214 return ((smbd
.s_fatal_error
) ? SMF_EXIT_ERR_FATAL
: SMF_EXIT_OK
);
218 * This function will fork off a child process,
219 * from which only the child will return.
221 * Use SMF error codes only on exit.
224 smbd_daemonize_init(void)
232 * Reset privileges to the minimum set required. We continue
233 * to run as root to create and access files in /var.
235 rc
= smb_init_daemon_priv(PU_RESETGROUPS
, smbd
.s_uid
, smbd
.s_gid
);
238 smbd_report("insufficient privileges");
239 exit(SMF_EXIT_ERR_FATAL
);
243 * Block all signals prior to the fork and leave them blocked in the
244 * parent so we don't get in a situation where the parent gets SIGINT
245 * and returns non-zero exit status and the child is actually running.
246 * In the child, restore the signal mask once we've done our setsid().
248 (void) sigfillset(&set
);
249 (void) sigdelset(&set
, SIGABRT
);
250 (void) sigprocmask(SIG_BLOCK
, &set
, &oset
);
252 if (pipe(pfds
) == -1) {
253 smbd_report("unable to create pipe");
254 exit(SMF_EXIT_ERR_FATAL
);
259 if ((pid
= fork()) == -1) {
260 openlog(smbd
.s_pname
, LOG_PID
| LOG_NOWAIT
, LOG_DAEMON
);
261 smbd_report("unable to fork");
263 exit(SMF_EXIT_ERR_FATAL
);
267 * If we're the parent process, wait for either the child to send us
268 * the appropriate exit status over the pipe or for the read to fail
269 * (presumably with 0 for EOF if our child terminated abnormally).
270 * If the read fails, exit with either the child's exit status if it
271 * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
274 (void) close(pfds
[1]);
276 if (read(pfds
[0], &status
, sizeof (status
)) == sizeof (status
))
279 if (waitpid(pid
, &status
, 0) == pid
&& WIFEXITED(status
))
280 _exit(WEXITSTATUS(status
));
282 _exit(SMF_EXIT_ERR_FATAL
);
285 openlog(smbd
.s_pname
, LOG_PID
| LOG_NOWAIT
, LOG_DAEMON
);
287 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
290 (void) close(pfds
[0]);
296 * This function is based on __init_daemon_priv() and replaces
297 * __init_daemon_priv() since we want smbd to have all privileges so that it
298 * can execute map/unmap commands with all privileges during share
299 * connection/disconnection. Unused privileges are disabled until command
300 * execution. The permitted and the limit set contains all privileges. The
301 * inheritable set contains no privileges.
304 static const char root_cp
[] = "/core.%f.%t";
305 static const char daemon_cp
[] = "/var/tmp/core.%f.%t";
308 smb_init_daemon_priv(int flags
, uid_t uid
, gid_t gid
)
310 priv_set_t
*perm
= NULL
;
315 * This is not a significant failure: it allows us to start programs
316 * with sufficient privileges and with the proper uid. We don't
317 * care enough about the extra groups in that case.
319 if (flags
& PU_RESETGROUPS
)
320 (void) setgroups(0, NULL
);
322 if (gid
!= (gid_t
)-1 && setgid(gid
) != 0)
325 perm
= priv_allocset();
330 (void) getppriv(PRIV_PERMITTED
, perm
);
331 (void) setppriv(PRIV_SET
, PRIV_EFFECTIVE
, perm
);
333 /* Now reset suid and euid */
334 if (uid
!= (uid_t
)-1 && setreuid(uid
, uid
) != 0)
339 ret
= setppriv(PRIV_SET
, PRIV_INHERITABLE
, perm
);
343 if (core_get_process_path(buf
, sizeof (buf
), getpid()) == 0 &&
344 strcmp(buf
, "core") == 0) {
346 if ((uid
== (uid_t
)-1 ? geteuid() : uid
) == 0) {
347 (void) core_set_process_path(root_cp
, sizeof (root_cp
),
350 (void) core_set_process_path(daemon_cp
,
351 sizeof (daemon_cp
), getpid());
354 (void) setpflags(__PROC_PROTECT
, 0);
360 * Most privileges, except the ones that are required for smbd, are turn off
361 * in the effective set. They will be turn on when needed for command
362 * execution during share connection/disconnection.
365 smbd_daemonize_fini(int fd
, int exit_status
)
370 * Now that we're running, if a pipe fd was specified, write an exit
371 * status to it to indicate that our parent process can safely detach.
372 * Then proceed to loading the remaining non-built-in modules.
375 (void) write(fd
, &exit_status
, sizeof (exit_status
));
379 pset
= priv_allocset();
385 /* list of privileges for smbd */
386 (void) priv_addset(pset
, PRIV_NET_PRIVADDR
);
387 (void) priv_addset(pset
, PRIV_PROC_AUDIT
);
388 (void) priv_addset(pset
, PRIV_SYS_DEVICES
);
389 (void) priv_addset(pset
, PRIV_SYS_SMB
);
390 (void) priv_addset(pset
, PRIV_SYS_MOUNT
);
394 /* turn off unneeded privileges */
395 (void) setppriv(PRIV_OFF
, PRIV_EFFECTIVE
, pset
);
399 /* reenable core dumps */
400 __fini_daemon_priv(NULL
);
407 smbd_service_init(void)
415 { SMB_SYSROOT
, 0755 },
416 { SMB_SYSTEM32
, 0755 },
418 { SMB_PIPE_DIR
, 0755 },
419 { "/var/smb/lipc", 0755 },
423 smbd
.s_pid
= getpid();
426 * Stop for a debugger attach here, which is after the
427 * fork() etc. in smb_daemonize_init()
429 if (smbd
.s_dbg_stop
) {
430 smbd_report("pid %d stop for debugger attach", smbd
.s_pid
);
431 (void) kill(smbd
.s_pid
, SIGSTOP
);
433 smbd_report("smbd starting, pid %d", smbd
.s_pid
);
435 for (i
= 0; i
< sizeof (dir
)/sizeof (dir
[0]); ++i
) {
436 if ((mkdir(dir
[i
].name
, dir
[i
].perm
) < 0) &&
438 smbd_report("mkdir %s: %s", dir
[i
].name
,
445 * This environment variable tells mech_krb5 to give us
446 * MS-compatible behavior.
448 (void) putenv("MS_INTEROP=1");
450 if ((rc
= smb_ccache_init(SMB_VARRUN_DIR
, SMB_CCACHE_FILE
)) != 0) {
452 smbd_report("mkdir %s: %s", SMB_VARRUN_DIR
,
455 smbd_report("unable to set KRB5CCNAME");
459 /* Upgrade SMF settings, if necessary. */
460 smb_config_upgrade();
464 rc
= smbd_cups_init();
465 if (smb_config_getbool(SMB_CI_PRINT_ENABLE
))
466 smbd_report("print service %savailable", (rc
== 0) ? "" : "un");
468 if (smbd_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI
) != 0)
469 smbd_report("NIC monitor failed to start");
474 if (smb_config_getbool(SMB_CI_NETBIOS_ENABLE
) == 0)
475 smbd_report("NetBIOS services disabled");
476 else if (smb_netbios_start() != 0)
477 smbd_report("NetBIOS services failed to start");
479 smbd_report("NetBIOS services started");
481 smbd
.s_secmode
= smb_config_get_secmode();
482 if ((rc
= smb_domain_init(smbd
.s_secmode
)) != 0) {
483 if (rc
== SMB_DOMAIN_NOMACHINE_SID
) {
485 "no machine SID: check idmap configuration");
490 if (smbd_dc_monitor_init() != 0)
491 smbd_report("DC monitor initialization failed %s",
494 if (smbd_pipesvc_start() != 0) {
495 smbd_report("pipesvc initialization failed");
499 if (smbd_authsvc_start() != 0) {
500 smbd_report("authsvc initialization failed");
504 smbd
.s_door_srv
= smbd_door_start();
505 if (smbd
.s_door_srv
< 0) {
506 smbd_report("door initialization failed %s", strerror(errno
));
510 dyndns_update_zones();
511 smbd_localtime_init();
512 (void) smb_lgrp_start();
513 smb_pwd_init(B_TRUE
);
515 if (smb_shr_start() != 0) {
516 smbd_report("share initialization failed: %s", strerror(errno
));
520 smbd
.s_door_lmshr
= smbd_share_start();
521 if (smbd
.s_door_lmshr
< 0)
522 smbd_report("share initialization failed");
524 /* Open the driver, load the kernel config. */
525 if (smbd_kernel_bind() != 0) {
530 smbd_load_printers();
533 smbd
.s_initialized
= B_TRUE
;
534 smbd_report("service initialized");
540 * Shutdown smbd and smbsrv kernel services.
542 * Called only by the main thread.
545 smbd_service_fini(void)
548 smbd
.s_shutting_down
= B_TRUE
;
549 smbd_report("service shutting down");
558 smbd_kernel_unbind();
563 smb_ccache_remove(SMB_CCACHE_PATH
);
570 smbd
.s_initialized
= B_FALSE
;
571 smbd_report("service terminated");
576 * Called when SMF sends us a SIGHUP. Update the smbd configuration
577 * from SMF and check for changes that require service reconfiguration.
580 smbd_refresh_handler()
584 if (smbd
.s_shutting_down
)
589 new_debug
= smb_config_get_debug();
590 if (smbd
.s_debug
|| new_debug
)
591 smbd_report("debug=%d", new_debug
);
592 smbd
.s_debug
= new_debug
;
595 smbd_dc_monitor_refresh();
596 smb_ccache_remove(SMB_CCACHE_PATH
);
599 * Clear the DNS zones for the existing interfaces
600 * before updating the NIC interface list.
602 dyndns_clear_zones();
604 if (smbd_nicmon_refresh() != 0)
605 smbd_report("NIC monitor refresh failed");
607 smb_netbios_name_reconfig();
608 smb_browser_reconfig();
609 dyndns_update_zones();
611 /* This reloads the in-kernel config. */
612 (void) smbd_kernel_bind();
615 smbd_load_printers();
620 smbd_set_secmode(int secmode
)
623 case SMB_SECMODE_WORKGRP
:
624 case SMB_SECMODE_DOMAIN
:
625 (void) smb_config_set_secmode(secmode
);
626 smbd
.s_secmode
= secmode
;
630 syslog(LOG_ERR
, "invalid security mode: %d", secmode
);
631 syslog(LOG_ERR
, "entering maintenance mode");
632 (void) smb_smf_maintenance_mode();
637 * The service is online if initialization is complete and shutdown
643 return (smbd
.s_initialized
&& !smbd
.s_shutting_down
);
647 * Wait until the service is online. Provided for threads that
648 * should wait until the service has been fully initialized before
649 * they start performing operations.
652 smbd_online_wait(const char *text
)
654 while (!smbd_online())
655 (void) sleep(SMBD_ONLINE_WAIT_INTERVAL
);
658 syslog(LOG_DEBUG
, "%s: online", text
);
659 (void) fprintf(stderr
, "%s: online\n", text
);
664 * If the door has already been opened by another process (non-zero pid
665 * in target), we assume that another smbd is already running. If there
666 * is a race here, it will be caught later when smbsrv is opened because
667 * only one process is allowed to open the device at a time.
670 smbd_already_running(void)
676 door_name
= getenv("SMBD_DOOR_NAME");
677 if (door_name
== NULL
)
678 door_name
= SMBD_DOOR_NAME
;
680 if ((door
= open(door_name
, O_RDONLY
)) < 0)
683 if (door_info(door
, &info
) < 0)
686 if (info
.di_target
> 0) {
687 smbd_report("already running: pid %ld\n", info
.di_target
);
699 * If smbsrv is already bound, reload the configuration and update smbsrv.
700 * Otherwise, open the smbsrv device and start the kernel service.
703 smbd_kernel_bind(void)
709 smb_load_kconfig(&cfg
);
710 smbd_get_authconf(&cfg
);
711 rc
= smb_kmod_setcfg(&cfg
);
713 smbd_report("kernel configuration update failed: %s",
718 if (smb_kmod_isbound())
719 smbd_kernel_unbind();
721 if ((rc
= smb_kmod_bind()) == 0) {
722 rc
= smbd_kernel_start();
726 smbd
.s_kbound
= B_TRUE
;
728 smbd_report("kernel bind error: %s", strerror(rc
));
735 smbd_kernel_start(void)
740 smb_load_kconfig(&cfg
);
741 smbd_get_authconf(&cfg
);
742 rc
= smb_kmod_setcfg(&cfg
);
744 smbd_report("kernel config ioctl error: %s", strerror(rc
));
748 rc
= smb_kmod_setgmtoff(smbd_gmtoff());
750 smbd_report("kernel gmtoff ioctl error: %s", strerror(rc
));
754 rc
= smb_kmod_start(smbd
.s_door_opipe
, smbd
.s_door_lmshr
,
758 smbd_report("kernel start ioctl error: %s", strerror(rc
));
769 smbd_kernel_unbind(void)
772 smbd
.s_kbound
= B_FALSE
;
776 * Create the Dynamic DNS publisher thread.
779 smbd_dyndns_init(void)
787 (void) pthread_attr_init(&attr
);
788 (void) pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
789 rc
= pthread_create(&tid
, &attr
, dyndns_publisher
, NULL
);
790 (void) pthread_attr_destroy(&attr
);
793 smbd_report("unable to start dyndns publisher: %s",
798 * Launches a thread to populate the share cache by share information
802 smbd_load_shares(void)
808 (void) pthread_attr_init(&attr
);
809 (void) pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
810 rc
= pthread_create(&tid
, &attr
, smbd_share_loader
, NULL
);
811 (void) pthread_attr_destroy(&attr
);
814 smbd_report("unable to load disk shares: %s", strerror(errno
));
818 smbd_share_loader(void *args
)
820 (void) smb_shr_load(args
);
825 * Initialization of the localtime thread.
826 * Returns 0 on success, an error number if thread creation fails.
830 smbd_localtime_init(void)
835 (void) pthread_attr_init(&attr
);
836 (void) pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
837 rc
= pthread_create(&smbd
.s_localtime_tid
, &attr
,
838 smbd_localtime_monitor
, NULL
);
839 (void) pthread_attr_destroy(&attr
);
842 smbd_report("unable to monitor localtime: %s", strerror(errno
));
846 * Send local gmtoff to the kernel module one time at startup and each
847 * time it changes (up to twice a year).
848 * Local gmtoff is checked once every 15 minutes since some timezones
849 * are aligned on half and quarter hour boundaries.
853 smbd_localtime_monitor(void *arg
)
857 int32_t gmtoff
, last_gmtoff
= -1;
861 smbd_online_wait("smbd_localtime_monitor");
864 gmtoff
= smbd_gmtoff();
866 if ((last_gmtoff
!= gmtoff
) && smbd
.s_kbound
) {
867 error
= smb_kmod_setgmtoff(gmtoff
);
869 smbd_report("localtime set failed: %s",
874 * Align the next iteration on a fifteen minute boundary.
877 (void) localtime_r(&secs
, &local_tm
);
878 timeout
= ((15 - (local_tm
.tm_min
% 15)) * SECSPERMIN
);
879 (void) sleep(timeout
);
881 last_gmtoff
= gmtoff
;
891 * Determine offset from GMT. If daylight saving time use altzone,
892 * otherwise use timezone.
901 (void) time(&clock_val
);
902 atm
= localtime(&clock_val
);
904 gmtoff
= (atm
->tm_isdst
) ? altzone
: timezone
;
910 * Set up configuration options and parse the command line.
911 * This function will determine if we will run as a daemon
912 * or in the foreground.
914 * Failure to find a uid or gid results in using the default (0).
917 smbd_setup_options(int argc
, char *argv
[])
923 if ((pwd
= getpwnam("root")) != NULL
)
924 smbd
.s_uid
= pwd
->pw_uid
;
926 if ((grp
= getgrnam("sys")) != NULL
)
927 smbd
.s_gid
= grp
->gr_gid
;
929 smbd
.s_debug
= smb_config_get_debug();
930 smbd
.s_fg
= smb_config_get_fg_flag();
932 while ((c
= getopt(argc
, argv
, ":dfs")) != -1) {
957 static char *help
[] = {
958 "-d enable debug messages"
959 "-f run program in foreground"
964 (void) fprintf(fp
, "Usage: %s [-f]\n", smbd
.s_pname
);
966 for (i
= 0; i
< sizeof (help
)/sizeof (help
[0]); ++i
)
967 (void) fprintf(fp
, " %s\n", help
[i
]);
971 smbd_report(const char *fmt
, ...)
980 (void) vsnprintf(buf
, 128, fmt
, ap
);
983 (void) fprintf(stderr
, "smbd: %s\n", buf
);
987 * Enable libumem debugging by default on DEBUG builds.
991 _umem_debug_init(void)
993 return ("default,verbose"); /* $UMEM_DEBUG setting */
997 _umem_logging_init(void)
999 return ("fail,contents"); /* $UMEM_LOGGING setting */