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
[])
110 smbd
.s_pname
= basename(argv
[0]);
111 openlog(smbd
.s_pname
, LOG_PID
| LOG_NOWAIT
, LOG_DAEMON
);
113 if (smbd_setup_options(argc
, argv
) != 0)
114 return (SMF_EXIT_ERR_FATAL
);
116 if ((uid
= getuid()) != smbd
.s_uid
) {
118 /* Can't manipulate privileges in daemonize. */
119 if (smbd
.s_fg
== 0) {
121 smbd_report("user %d (forced -f)", uid
);
124 smbd_report("user %d: %s", uid
, strerror(EPERM
));
125 return (SMF_EXIT_ERR_FATAL
);
129 if (is_system_labeled()) {
130 smbd_report("Trusted Extensions not supported");
131 return (SMF_EXIT_ERR_FATAL
);
134 if (smbd_already_running())
135 return (SMF_EXIT_OK
);
138 * Raise the file descriptor limit to accommodate simultaneous user
139 * authentications/file access.
141 if ((getrlimit(RLIMIT_NOFILE
, &rl
) == 0) &&
142 (rl
.rlim_cur
< rl
.rlim_max
)) {
143 orig_limit
= rl
.rlim_cur
;
144 rl
.rlim_cur
= rl
.rlim_max
;
145 if (setrlimit(RLIMIT_NOFILE
, &rl
) != 0)
146 smbd_report("Failed to raise file descriptor limit"
147 " from %d to %d", orig_limit
, rl
.rlim_cur
);
151 * Block async signals in all threads.
153 (void) sigemptyset(&set
);
155 (void) sigaddset(&set
, SIGHUP
);
156 (void) sigaddset(&set
, SIGINT
);
157 (void) sigaddset(&set
, SIGQUIT
);
158 (void) sigaddset(&set
, SIGPIPE
);
159 (void) sigaddset(&set
, SIGTERM
);
160 (void) sigaddset(&set
, SIGUSR1
);
161 (void) sigaddset(&set
, SIGUSR2
);
163 (void) sigprocmask(SIG_SETMASK
, &set
, NULL
);
166 if (smbd_service_init() != 0) {
167 smbd_report("service initialization failed");
168 exit(SMF_EXIT_ERR_FATAL
);
172 * "pfd" is a pipe descriptor -- any fatal errors
173 * during subsequent initialization of the child
174 * process should be written to this pipe and the
175 * parent will report this error as the exit status.
177 pfd
= smbd_daemonize_init();
179 if (smbd_service_init() != 0) {
180 smbd_report("daemon initialization failed");
181 exit(SMF_EXIT_ERR_FATAL
);
184 smbd_daemonize_fini(pfd
, SMF_EXIT_OK
);
187 while (!smbd
.s_shutting_down
) {
188 sigval
= sigwait(&set
);
192 syslog(LOG_DEBUG
, "sigwait failed: %s",
199 syslog(LOG_DEBUG
, "refresh requested");
200 smbd_refresh_handler();
204 syslog(LOG_DEBUG
, "SIGUSR1 ignored");
209 * Typically SIGINT or SIGTERM.
211 smbd
.s_shutting_down
= B_TRUE
;
217 * Allow termination signals while shutting down.
219 (void) sigemptyset(&set
);
222 (void) sigaddset(&set
, SIGHUP
);
223 (void) sigaddset(&set
, SIGINT
);
225 (void) sigaddset(&set
, SIGTERM
);
227 (void) sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
230 return ((smbd
.s_fatal_error
) ? SMF_EXIT_ERR_FATAL
: SMF_EXIT_OK
);
234 * This function will fork off a child process,
235 * from which only the child will return.
237 * Use SMF error codes only on exit.
240 smbd_daemonize_init(void)
248 * Reset privileges to the minimum set required. We continue
249 * to run as root to create and access files in /var.
251 rc
= smb_init_daemon_priv(PU_RESETGROUPS
, smbd
.s_uid
, smbd
.s_gid
);
254 smbd_report("insufficient privileges");
255 exit(SMF_EXIT_ERR_FATAL
);
259 * Block all signals prior to the fork and leave them blocked in the
260 * parent so we don't get in a situation where the parent gets SIGINT
261 * and returns non-zero exit status and the child is actually running.
262 * In the child, restore the signal mask once we've done our setsid().
264 (void) sigfillset(&set
);
265 (void) sigdelset(&set
, SIGABRT
);
266 (void) sigprocmask(SIG_BLOCK
, &set
, &oset
);
268 if (pipe(pfds
) == -1) {
269 smbd_report("unable to create pipe");
270 exit(SMF_EXIT_ERR_FATAL
);
275 if ((pid
= fork()) == -1) {
276 openlog(smbd
.s_pname
, LOG_PID
| LOG_NOWAIT
, LOG_DAEMON
);
277 smbd_report("unable to fork");
279 exit(SMF_EXIT_ERR_FATAL
);
283 * If we're the parent process, wait for either the child to send us
284 * the appropriate exit status over the pipe or for the read to fail
285 * (presumably with 0 for EOF if our child terminated abnormally).
286 * If the read fails, exit with either the child's exit status if it
287 * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
290 (void) close(pfds
[1]);
292 if (read(pfds
[0], &status
, sizeof (status
)) == sizeof (status
))
295 if (waitpid(pid
, &status
, 0) == pid
&& WIFEXITED(status
))
296 _exit(WEXITSTATUS(status
));
298 _exit(SMF_EXIT_ERR_FATAL
);
301 openlog(smbd
.s_pname
, LOG_PID
| LOG_NOWAIT
, LOG_DAEMON
);
303 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
306 (void) close(pfds
[0]);
312 * This function is based on __init_daemon_priv() and replaces
313 * __init_daemon_priv() since we want smbd to have all privileges so that it
314 * can execute map/unmap commands with all privileges during share
315 * connection/disconnection. Unused privileges are disabled until command
316 * execution. The permitted and the limit set contains all privileges. The
317 * inheritable set contains no privileges.
320 static const char root_cp
[] = "/core.%f.%t";
321 static const char daemon_cp
[] = "/var/tmp/core.%f.%t";
324 smb_init_daemon_priv(int flags
, uid_t uid
, gid_t gid
)
326 priv_set_t
*perm
= NULL
;
331 * This is not a significant failure: it allows us to start programs
332 * with sufficient privileges and with the proper uid. We don't
333 * care enough about the extra groups in that case.
335 if (flags
& PU_RESETGROUPS
)
336 (void) setgroups(0, NULL
);
338 if (gid
!= (gid_t
)-1 && setgid(gid
) != 0)
341 perm
= priv_allocset();
346 (void) getppriv(PRIV_PERMITTED
, perm
);
347 (void) setppriv(PRIV_SET
, PRIV_EFFECTIVE
, perm
);
349 /* Now reset suid and euid */
350 if (uid
!= (uid_t
)-1 && setreuid(uid
, uid
) != 0)
355 ret
= setppriv(PRIV_SET
, PRIV_INHERITABLE
, perm
);
359 if (core_get_process_path(buf
, sizeof (buf
), getpid()) == 0 &&
360 strcmp(buf
, "core") == 0) {
362 if ((uid
== (uid_t
)-1 ? geteuid() : uid
) == 0) {
363 (void) core_set_process_path(root_cp
, sizeof (root_cp
),
366 (void) core_set_process_path(daemon_cp
,
367 sizeof (daemon_cp
), getpid());
370 (void) setpflags(__PROC_PROTECT
, 0);
376 * Most privileges, except the ones that are required for smbd, are turn off
377 * in the effective set. They will be turn on when needed for command
378 * execution during share connection/disconnection.
381 smbd_daemonize_fini(int fd
, int exit_status
)
386 * Now that we're running, if a pipe fd was specified, write an exit
387 * status to it to indicate that our parent process can safely detach.
388 * Then proceed to loading the remaining non-built-in modules.
391 (void) write(fd
, &exit_status
, sizeof (exit_status
));
395 pset
= priv_allocset();
401 /* list of privileges for smbd */
402 (void) priv_addset(pset
, PRIV_NET_MAC_AWARE
);
403 (void) priv_addset(pset
, PRIV_NET_PRIVADDR
);
404 (void) priv_addset(pset
, PRIV_PROC_AUDIT
);
405 (void) priv_addset(pset
, PRIV_SYS_DEVICES
);
406 (void) priv_addset(pset
, PRIV_SYS_SMB
);
407 (void) priv_addset(pset
, PRIV_SYS_MOUNT
);
411 /* turn off unneeded privileges */
412 (void) setppriv(PRIV_OFF
, PRIV_EFFECTIVE
, pset
);
416 /* reenable core dumps */
417 __fini_daemon_priv(NULL
);
424 smbd_service_init(void)
432 { SMB_SYSROOT
, 0755 },
433 { SMB_SYSTEM32
, 0755 },
435 { SMB_PIPE_DIR
, 0755 },
436 { "/var/smb/lipc", 0755 },
440 smbd
.s_pid
= getpid();
443 * Stop for a debugger attach here, which is after the
444 * fork() etc. in smb_daemonize_init()
446 if (smbd
.s_dbg_stop
) {
447 smbd_report("pid %d stop for debugger attach", smbd
.s_pid
);
448 (void) kill(smbd
.s_pid
, SIGSTOP
);
450 smbd_report("smbd starting, pid %d", smbd
.s_pid
);
452 for (i
= 0; i
< sizeof (dir
)/sizeof (dir
[0]); ++i
) {
453 if ((mkdir(dir
[i
].name
, dir
[i
].perm
) < 0) &&
455 smbd_report("mkdir %s: %s", dir
[i
].name
,
462 * This environment variable tells mech_krb5 to give us
463 * MS-compatible behavior.
465 (void) putenv("MS_INTEROP=1");
467 if ((rc
= smb_ccache_init(SMB_VARRUN_DIR
, SMB_CCACHE_FILE
)) != 0) {
469 smbd_report("mkdir %s: %s", SMB_VARRUN_DIR
,
472 smbd_report("unable to set KRB5CCNAME");
477 /* Upgrade SMF settings, if necessary. */
478 smb_config_upgrade();
483 rc
= smbd_cups_init();
484 if (smb_config_getbool(SMB_CI_PRINT_ENABLE
))
485 smbd_report("print service %savailable", (rc
== 0) ? "" : "un");
487 if (smbd_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI
) != 0)
488 smbd_report("NIC monitor failed to start");
493 if (smb_config_getbool(SMB_CI_NETBIOS_ENABLE
) == 0)
494 smbd_report("NetBIOS services disabled");
495 else if (smb_netbios_start() != 0)
496 smbd_report("NetBIOS services failed to start");
498 smbd_report("NetBIOS services started");
500 smbd
.s_secmode
= smb_config_get_secmode();
501 if ((rc
= smb_domain_init(smbd
.s_secmode
)) != 0) {
502 if (rc
== SMB_DOMAIN_NOMACHINE_SID
) {
504 "no machine SID: check idmap configuration");
509 if (smbd_dc_monitor_init() != 0)
510 smbd_report("DC monitor initialization failed %s",
513 if (smbd_pipesvc_start() != 0) {
514 smbd_report("pipesvc initialization failed");
518 if (smbd_authsvc_start() != 0) {
519 smbd_report("authsvc initialization failed");
523 smbd
.s_door_srv
= smbd_door_start();
524 if (smbd
.s_door_srv
< 0) {
525 smbd_report("door initialization failed %s", strerror(errno
));
529 dyndns_update_zones();
530 smbd_localtime_init();
531 (void) smb_lgrp_start();
532 smb_pwd_init(B_TRUE
);
534 if (smb_shr_start() != 0) {
535 smbd_report("share initialization failed: %s", strerror(errno
));
539 smbd
.s_door_lmshr
= smbd_share_start();
540 if (smbd
.s_door_lmshr
< 0)
541 smbd_report("share initialization failed");
543 /* Open the driver, load the kernel config. */
544 if (smbd_kernel_bind() != 0) {
549 smbd_load_printers();
552 smbd
.s_initialized
= B_TRUE
;
553 smbd_report("service initialized");
559 * Shutdown smbd and smbsrv kernel services.
561 * Called only by the main thread.
564 smbd_service_fini(void)
567 smbd
.s_shutting_down
= B_TRUE
;
568 smbd_report("service shutting down");
577 smbd_kernel_unbind();
582 smb_ccache_remove(SMB_CCACHE_PATH
);
589 smbd
.s_initialized
= B_FALSE
;
590 smbd_report("service terminated");
595 * Called when SMF sends us a SIGHUP. Update the smbd configuration
596 * from SMF and check for changes that require service reconfiguration.
599 smbd_refresh_handler()
603 if (smbd
.s_shutting_down
)
608 new_debug
= smb_config_get_debug();
609 if (smbd
.s_debug
|| new_debug
)
610 smbd_report("debug=%d", new_debug
);
611 smbd
.s_debug
= new_debug
;
614 smbd_dc_monitor_refresh();
615 smb_ccache_remove(SMB_CCACHE_PATH
);
618 * Clear the DNS zones for the existing interfaces
619 * before updating the NIC interface list.
621 dyndns_clear_zones();
623 if (smbd_nicmon_refresh() != 0)
624 smbd_report("NIC monitor refresh failed");
626 smb_netbios_name_reconfig();
627 smb_browser_reconfig();
628 dyndns_update_zones();
630 /* This reloads the in-kernel config. */
631 (void) smbd_kernel_bind();
634 smbd_load_printers();
639 smbd_set_secmode(int secmode
)
642 case SMB_SECMODE_WORKGRP
:
643 case SMB_SECMODE_DOMAIN
:
644 (void) smb_config_set_secmode(secmode
);
645 smbd
.s_secmode
= secmode
;
649 syslog(LOG_ERR
, "invalid security mode: %d", secmode
);
650 syslog(LOG_ERR
, "entering maintenance mode");
651 (void) smb_smf_maintenance_mode();
656 * The service is online if initialization is complete and shutdown
662 return (smbd
.s_initialized
&& !smbd
.s_shutting_down
);
666 * Wait until the service is online. Provided for threads that
667 * should wait until the service has been fully initialized before
668 * they start performing operations.
671 smbd_online_wait(const char *text
)
673 while (!smbd_online())
674 (void) sleep(SMBD_ONLINE_WAIT_INTERVAL
);
677 syslog(LOG_DEBUG
, "%s: online", text
);
678 (void) fprintf(stderr
, "%s: online\n", text
);
683 * If the door has already been opened by another process (non-zero pid
684 * in target), we assume that another smbd is already running. If there
685 * is a race here, it will be caught later when smbsrv is opened because
686 * only one process is allowed to open the device at a time.
689 smbd_already_running(void)
695 door_name
= getenv("SMBD_DOOR_NAME");
696 if (door_name
== NULL
)
697 door_name
= SMBD_DOOR_NAME
;
699 if ((door
= open(door_name
, O_RDONLY
)) < 0)
702 if (door_info(door
, &info
) < 0)
705 if (info
.di_target
> 0) {
706 smbd_report("already running: pid %ld\n", info
.di_target
);
718 * If smbsrv is already bound, reload the configuration and update smbsrv.
719 * Otherwise, open the smbsrv device and start the kernel service.
722 smbd_kernel_bind(void)
728 smb_load_kconfig(&cfg
);
729 smbd_get_authconf(&cfg
);
730 rc
= smb_kmod_setcfg(&cfg
);
732 smbd_report("kernel configuration update failed: %s",
737 if (smb_kmod_isbound())
738 smbd_kernel_unbind();
740 if ((rc
= smb_kmod_bind()) == 0) {
741 rc
= smbd_kernel_start();
745 smbd
.s_kbound
= B_TRUE
;
747 smbd_report("kernel bind error: %s", strerror(rc
));
754 smbd_kernel_start(void)
759 smb_load_kconfig(&cfg
);
760 smbd_get_authconf(&cfg
);
761 rc
= smb_kmod_setcfg(&cfg
);
763 smbd_report("kernel config ioctl error: %s", strerror(rc
));
767 rc
= smb_kmod_setgmtoff(smbd_gmtoff());
769 smbd_report("kernel gmtoff ioctl error: %s", strerror(rc
));
773 rc
= smb_kmod_start(smbd
.s_door_opipe
, smbd
.s_door_lmshr
,
777 smbd_report("kernel start ioctl error: %s", strerror(rc
));
788 smbd_kernel_unbind(void)
791 smbd
.s_kbound
= B_FALSE
;
795 * Create the Dynamic DNS publisher thread.
798 smbd_dyndns_init(void)
806 (void) pthread_attr_init(&attr
);
807 (void) pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
808 rc
= pthread_create(&tid
, &attr
, dyndns_publisher
, NULL
);
809 (void) pthread_attr_destroy(&attr
);
812 smbd_report("unable to start dyndns publisher: %s",
817 * Launches a thread to populate the share cache by share information
821 smbd_load_shares(void)
827 (void) pthread_attr_init(&attr
);
828 (void) pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
829 rc
= pthread_create(&tid
, &attr
, smbd_share_loader
, NULL
);
830 (void) pthread_attr_destroy(&attr
);
833 smbd_report("unable to load disk shares: %s", strerror(errno
));
837 smbd_share_loader(void *args
)
839 (void) smb_shr_load(args
);
844 * Initialization of the localtime thread.
845 * Returns 0 on success, an error number if thread creation fails.
849 smbd_localtime_init(void)
854 (void) pthread_attr_init(&attr
);
855 (void) pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
856 rc
= pthread_create(&smbd
.s_localtime_tid
, &attr
,
857 smbd_localtime_monitor
, NULL
);
858 (void) pthread_attr_destroy(&attr
);
861 smbd_report("unable to monitor localtime: %s", strerror(errno
));
865 * Send local gmtoff to the kernel module one time at startup and each
866 * time it changes (up to twice a year).
867 * Local gmtoff is checked once every 15 minutes since some timezones
868 * are aligned on half and quarter hour boundaries.
872 smbd_localtime_monitor(void *arg
)
876 int32_t gmtoff
, last_gmtoff
= -1;
880 smbd_online_wait("smbd_localtime_monitor");
883 gmtoff
= smbd_gmtoff();
885 if ((last_gmtoff
!= gmtoff
) && smbd
.s_kbound
) {
886 error
= smb_kmod_setgmtoff(gmtoff
);
888 smbd_report("localtime set failed: %s",
893 * Align the next iteration on a fifteen minute boundary.
896 (void) localtime_r(&secs
, &local_tm
);
897 timeout
= ((15 - (local_tm
.tm_min
% 15)) * SECSPERMIN
);
898 (void) sleep(timeout
);
900 last_gmtoff
= gmtoff
;
910 * Determine offset from GMT. If daylight saving time use altzone,
911 * otherwise use timezone.
920 (void) time(&clock_val
);
921 atm
= localtime(&clock_val
);
923 gmtoff
= (atm
->tm_isdst
) ? altzone
: timezone
;
929 * Set up configuration options and parse the command line.
930 * This function will determine if we will run as a daemon
931 * or in the foreground.
933 * Failure to find a uid or gid results in using the default (0).
936 smbd_setup_options(int argc
, char *argv
[])
942 if ((pwd
= getpwnam("root")) != NULL
)
943 smbd
.s_uid
= pwd
->pw_uid
;
945 if ((grp
= getgrnam("sys")) != NULL
)
946 smbd
.s_gid
= grp
->gr_gid
;
948 smbd
.s_debug
= smb_config_get_debug();
949 smbd
.s_fg
= smb_config_get_fg_flag();
951 while ((c
= getopt(argc
, argv
, ":dfs")) != -1) {
976 static char *help
[] = {
977 "-d enable debug messages"
978 "-f run program in foreground"
983 (void) fprintf(fp
, "Usage: %s [-f]\n", smbd
.s_pname
);
985 for (i
= 0; i
< sizeof (help
)/sizeof (help
[0]); ++i
)
986 (void) fprintf(fp
, " %s\n", help
[i
]);
990 smbd_report(const char *fmt
, ...)
999 (void) vsnprintf(buf
, 128, fmt
, ap
);
1002 (void) fprintf(stderr
, "smbd: %s\n", buf
);
1006 * Enable libumem debugging by default on DEBUG builds.
1010 _umem_debug_init(void)
1012 return ("default,verbose"); /* $UMEM_DEBUG setting */
1016 _umem_logging_init(void)
1018 return ("fail,contents"); /* $UMEM_LOGGING setting */