xref: /illumos-gate/usr/src/cmd/smbsrv/smbd/smbd_main.c (revision 1b8adde7ba7d5e04395c141c5400dc2cffd7d809)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"@(#)smbd_main.c	1.13	08/08/05 SMI"
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/ioccom.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <stdarg.h>
37 #include <fcntl.h>
38 #include <wait.h>
39 #include <signal.h>
40 #include <libscf.h>
41 #include <limits.h>
42 #include <priv_utils.h>
43 #include <door.h>
44 #include <errno.h>
45 #include <syslog.h>
46 #include <pthread.h>
47 #include <time.h>
48 #include <libscf.h>
49 #include <zone.h>
50 #include <time.h>
51 #include <tzfile.h>
52 #include <libgen.h>
53 #include <pwd.h>
54 #include <grp.h>
55 
56 #include <smbsrv/smb_door_svc.h>
57 #include <smbsrv/smb_ioctl.h>
58 #include <smbsrv/libsmb.h>
59 #include <smbsrv/libsmbns.h>
60 #include <smbsrv/libsmbrdr.h>
61 #include <smbsrv/libmlsvc.h>
62 
63 #include "smbd.h"
64 
65 #define	DRV_DEVICE_PATH	"/devices/pseudo/smbsrv@0:smbsrv"
66 #define	SMB_DBDIR "/var/smb"
67 
68 extern void *smbd_nbt_listener(void *);
69 extern void *smbd_tcp_listener(void *);
70 
71 static int smbd_daemonize_init(void);
72 static void smbd_daemonize_fini(int, int);
73 
74 static int smbd_kernel_bind(void);
75 static void smbd_kernel_unbind(void);
76 static int smbd_already_running(void);
77 
78 static int smbd_service_init(void);
79 static void smbd_service_fini(void);
80 
81 static int smbd_setup_options(int argc, char *argv[]);
82 static void smbd_usage(FILE *fp);
83 static void smbd_report(const char *fmt, ...);
84 
85 static void smbd_sig_handler(int sig);
86 
87 static int32_t smbd_gmtoff(void);
88 static int smbd_localtime_init(void);
89 static void *smbd_localtime_monitor(void *arg);
90 
91 static pthread_t localtime_thr;
92 
93 static int smbd_refresh_init(void);
94 static void smbd_refresh_fini(void);
95 static void *smbd_refresh_monitor(void *);
96 static pthread_t nbt_listener;
97 static pthread_t tcp_listener;
98 static pthread_t refresh_thr;
99 static pthread_cond_t refresh_cond;
100 static pthread_mutex_t refresh_mutex;
101 
102 smbd_t smbd;
103 
104 /*
105  * smbd user land daemon
106  *
107  * Use SMF error codes only on return or exit.
108  */
109 int
110 main(int argc, char *argv[])
111 {
112 	struct sigaction	act;
113 	sigset_t		set;
114 	uid_t			uid;
115 	int			pfd = -1;
116 	int			sigval;
117 
118 	smbd.s_pname = basename(argv[0]);
119 	openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
120 
121 	if (smbd_setup_options(argc, argv) != 0)
122 		return (SMF_EXIT_ERR_FATAL);
123 
124 	if ((uid = getuid()) != smbd.s_uid) {
125 		smbd_report("user %d: %s", uid, strerror(EPERM));
126 		return (SMF_EXIT_ERR_FATAL);
127 	}
128 
129 	if (getzoneid() != GLOBAL_ZONEID) {
130 		smbd_report("non-global zones are not supported");
131 		return (SMF_EXIT_ERR_FATAL);
132 	}
133 
134 	if (is_system_labeled()) {
135 		smbd_report("Trusted Extensions not supported");
136 		return (SMF_EXIT_ERR_FATAL);
137 	}
138 
139 	if (smbd_already_running())
140 		return (SMF_EXIT_OK);
141 
142 	(void) sigfillset(&set);
143 	(void) sigdelset(&set, SIGABRT);
144 
145 	(void) sigfillset(&act.sa_mask);
146 	act.sa_handler = smbd_sig_handler;
147 	act.sa_flags = 0;
148 
149 	(void) sigaction(SIGTERM, &act, NULL);
150 	(void) sigaction(SIGHUP, &act, NULL);
151 	(void) sigaction(SIGINT, &act, NULL);
152 	(void) sigaction(SIGPIPE, &act, NULL);
153 
154 	(void) sigdelset(&set, SIGTERM);
155 	(void) sigdelset(&set, SIGHUP);
156 	(void) sigdelset(&set, SIGINT);
157 	(void) sigdelset(&set, SIGPIPE);
158 
159 	if (smbd.s_fg) {
160 		(void) sigdelset(&set, SIGTSTP);
161 		(void) sigdelset(&set, SIGTTIN);
162 		(void) sigdelset(&set, SIGTTOU);
163 
164 		if (smbd_service_init() != 0) {
165 			smbd_report("service initialization failed");
166 			exit(SMF_EXIT_ERR_FATAL);
167 		}
168 	} else {
169 		/*
170 		 * "pfd" is a pipe descriptor -- any fatal errors
171 		 * during subsequent initialization of the child
172 		 * process should be written to this pipe and the
173 		 * parent will report this error as the exit status.
174 		 */
175 		pfd = smbd_daemonize_init();
176 
177 		if (smbd_service_init() != 0) {
178 			smbd_report("daemon initialization failed");
179 			exit(SMF_EXIT_ERR_FATAL);
180 		}
181 
182 		smbd_daemonize_fini(pfd, SMF_EXIT_OK);
183 	}
184 
185 	(void) atexit(smbd_service_fini);
186 
187 	while (!smbd.s_shutdown_flag) {
188 		if (smbd.s_sigval == 0)
189 			(void) sigsuspend(&set);
190 
191 		sigval = smbd.s_sigval;
192 		smbd.s_sigval = 0;
193 
194 		switch (sigval) {
195 		case 0:
196 		case SIGPIPE:
197 			break;
198 
199 		case SIGHUP:
200 			/* Refresh config was triggered */
201 			if (smbd.s_fg)
202 				smbd_report("reconfiguration requested");
203 			(void) pthread_cond_signal(&refresh_cond);
204 			break;
205 
206 		default:
207 			/*
208 			 * Typically SIGINT or SIGTERM.
209 			 */
210 			smbd.s_shutdown_flag = 1;
211 			break;
212 		}
213 	}
214 
215 	smbd_service_fini();
216 	closelog();
217 	return (SMF_EXIT_OK);
218 }
219 
220 /*
221  * This function will fork off a child process,
222  * from which only the child will return.
223  *
224  * Use SMF error codes only on exit.
225  */
226 static int
227 smbd_daemonize_init(void)
228 {
229 	int status, pfds[2];
230 	sigset_t set, oset;
231 	pid_t pid;
232 	int rc;
233 
234 	/*
235 	 * Reset privileges to the minimum set required. We continue
236 	 * to run as root to create and access files in /var.
237 	 */
238 	rc = __init_daemon_priv(PU_RESETGROUPS | PU_LIMITPRIVS,
239 	    smbd.s_uid, smbd.s_gid,
240 	    PRIV_NET_MAC_AWARE, PRIV_NET_PRIVADDR, PRIV_PROC_AUDIT,
241 	    PRIV_SYS_DEVICES, PRIV_SYS_SMB, NULL);
242 
243 	if (rc != 0) {
244 		smbd_report("insufficient privileges");
245 		exit(SMF_EXIT_ERR_FATAL);
246 	}
247 
248 	/*
249 	 * Block all signals prior to the fork and leave them blocked in the
250 	 * parent so we don't get in a situation where the parent gets SIGINT
251 	 * and returns non-zero exit status and the child is actually running.
252 	 * In the child, restore the signal mask once we've done our setsid().
253 	 */
254 	(void) sigfillset(&set);
255 	(void) sigdelset(&set, SIGABRT);
256 	(void) sigprocmask(SIG_BLOCK, &set, &oset);
257 
258 	if (pipe(pfds) == -1) {
259 		smbd_report("unable to create pipe");
260 		exit(SMF_EXIT_ERR_FATAL);
261 	}
262 
263 	closelog();
264 
265 	if ((pid = fork()) == -1) {
266 		openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
267 		smbd_report("unable to fork");
268 		closelog();
269 		exit(SMF_EXIT_ERR_FATAL);
270 	}
271 
272 	/*
273 	 * If we're the parent process, wait for either the child to send us
274 	 * the appropriate exit status over the pipe or for the read to fail
275 	 * (presumably with 0 for EOF if our child terminated abnormally).
276 	 * If the read fails, exit with either the child's exit status if it
277 	 * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
278 	 */
279 	if (pid != 0) {
280 		(void) close(pfds[1]);
281 
282 		if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
283 			_exit(status);
284 
285 		if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
286 			_exit(WEXITSTATUS(status));
287 
288 		_exit(SMF_EXIT_ERR_FATAL);
289 	}
290 
291 	openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
292 	smbd.s_pid = getpid();
293 	(void) setsid();
294 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
295 	(void) chdir("/");
296 	(void) umask(022);
297 	(void) close(pfds[0]);
298 
299 	return (pfds[1]);
300 }
301 
302 static void
303 smbd_daemonize_fini(int fd, int exit_status)
304 {
305 	/*
306 	 * Now that we're running, if a pipe fd was specified, write an exit
307 	 * status to it to indicate that our parent process can safely detach.
308 	 * Then proceed to loading the remaining non-built-in modules.
309 	 */
310 	if (fd >= 0)
311 		(void) write(fd, &exit_status, sizeof (exit_status));
312 
313 	(void) close(fd);
314 
315 	if ((fd = open("/dev/null", O_RDWR)) >= 0) {
316 		(void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
317 		(void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
318 		(void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
319 		(void) close(fd);
320 	}
321 
322 	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
323 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, NULL);
324 }
325 
326 /*
327  * smbd_service_init
328  */
329 static int
330 smbd_service_init(void)
331 {
332 	int	rc;
333 	char	resource_domain[SMB_PI_MAX_DOMAIN];
334 	char	fqdn[MAXHOSTNAMELEN];
335 
336 	smbd.s_drv_fd = -1;
337 
338 	if ((mkdir(SMB_DBDIR, 0700) < 0) && (errno != EEXIST)) {
339 		smbd_report("mkdir %s: %s", SMB_DBDIR, strerror(errno));
340 		return (1);
341 	}
342 
343 	if ((rc = smb_ccache_init(SMB_VARRUN_DIR, SMB_CCACHE_FILE)) != 0) {
344 		if (rc == -1)
345 			smbd_report("mkdir %s: %s", SMB_VARRUN_DIR,
346 			    strerror(errno));
347 		else
348 			smbd_report("unable to set KRB5CCNAME");
349 		return (1);
350 	}
351 
352 
353 	(void) oem_language_set("english");
354 
355 	if (!smb_wka_init()) {
356 		smbd_report("out of memory");
357 		return (1);
358 	}
359 
360 	if (smb_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI) != 0)
361 		smbd_report("NIC monitoring failed to start");
362 
363 	dns_msgid_init();
364 	smbrdr_init();
365 
366 	if (smb_netbios_start() != 0)
367 		smbd_report("NetBIOS services failed to start");
368 	else
369 		smbd_report("NetBIOS services started");
370 
371 	if (smb_netlogon_init() != 0) {
372 		smbd_report("netlogon initialization failed");
373 		return (1);
374 	}
375 
376 	(void) smb_getdomainname(resource_domain, SMB_PI_MAX_DOMAIN);
377 	(void) utf8_strupr(resource_domain);
378 
379 	/* Get the ID map client handle */
380 	if ((rc = smb_idmap_start()) != 0) {
381 		smbd_report("no idmap handle");
382 		return (rc);
383 	}
384 
385 	smbd.s_secmode = smb_config_get_secmode();
386 	if ((rc = nt_domain_init(resource_domain, smbd.s_secmode)) != 0) {
387 		if (rc == SMB_DOMAIN_NOMACHINE_SID) {
388 			smbd_report(
389 			    "no machine SID: check idmap configuration");
390 			return (rc);
391 		}
392 	}
393 
394 	smb_ads_init();
395 	if ((rc = mlsvc_init()) != 0) {
396 		smbd_report("msrpc initialization failed");
397 		return (rc);
398 	}
399 
400 	if (smbd.s_secmode == SMB_SECMODE_DOMAIN)
401 		if (smbd_locate_dc_start(resource_domain) != 0)
402 			smbd_report("dc discovery failed %s", strerror(errno));
403 
404 	smbd.s_door_srv = smb_door_srv_start();
405 	if (smbd.s_door_srv < 0)
406 		return (rc);
407 
408 	if ((rc = smbd_refresh_init()) != 0)
409 		return (rc);
410 
411 	if (smb_getfqdomainname(fqdn, MAXHOSTNAMELEN) == 0)
412 		(void) dyndns_update_core(fqdn);
413 
414 	(void) smbd_localtime_init();
415 
416 	smbd.s_door_opipe = smbd_opipe_dsrv_start();
417 	if (smbd.s_door_opipe < 0) {
418 		smbd_report("opipe initialization failed %s",
419 		    strerror(errno));
420 		return (rc);
421 	}
422 
423 	(void) smb_lgrp_start();
424 
425 	smb_pwd_init(B_TRUE);
426 
427 	if ((rc = smb_shr_start()) != 0) {
428 		smbd_report("share initialization failed: %s", strerror(errno));
429 		return (rc);
430 	}
431 
432 	smbd.s_door_lmshr = smb_share_dsrv_start();
433 	if (smbd.s_door_lmshr < 0) {
434 		smbd_report("share initialization failed");
435 	}
436 
437 	rc = smbd_kernel_bind();
438 	if (rc != 0) {
439 		smbd_report("kernel bind error: %s", strerror(errno));
440 		return (rc);
441 	}
442 
443 	return (0);
444 }
445 
446 /*
447  * Close the kernel service and shutdown smbd services.
448  * This function is registered with atexit(): ensure that anything
449  * called from here is safe to be called multiple times.
450  */
451 static void
452 smbd_service_fini(void)
453 {
454 	smbd_opipe_dsrv_stop();
455 	smb_wka_fini();
456 	smbd_refresh_fini();
457 	smbd_kernel_unbind();
458 	smb_door_srv_stop();
459 	smb_share_dsrv_stop();
460 	smb_shr_stop();
461 	smb_nicmon_stop();
462 	smb_idmap_stop();
463 	smb_lgrp_stop();
464 	smb_ccache_remove(SMB_CCACHE_PATH);
465 	smb_pwd_fini();
466 
467 }
468 
469 
470 /*
471  * smbd_refresh_init()
472  *
473  * SMB service refresh thread initialization.  This thread waits for a
474  * refresh event and updates the daemon's view of the configuration
475  * before going back to sleep.
476  */
477 static int
478 smbd_refresh_init()
479 {
480 	pthread_attr_t		tattr;
481 	pthread_condattr_t	cattr;
482 	int			rc;
483 
484 	(void) pthread_condattr_init(&cattr);
485 	(void) pthread_cond_init(&refresh_cond, &cattr);
486 	(void) pthread_condattr_destroy(&cattr);
487 
488 	(void) pthread_mutex_init(&refresh_mutex, NULL);
489 
490 	(void) pthread_attr_init(&tattr);
491 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
492 	rc = pthread_create(&refresh_thr, &tattr, smbd_refresh_monitor, 0);
493 	(void) pthread_attr_destroy(&tattr);
494 
495 	return (rc);
496 }
497 
498 /*
499  * smbd_refresh_fini()
500  *
501  * Stop the refresh thread.
502  */
503 static void
504 smbd_refresh_fini()
505 {
506 	(void) pthread_cancel(refresh_thr);
507 
508 	(void) pthread_cond_destroy(&refresh_cond);
509 	(void) pthread_mutex_destroy(&refresh_mutex);
510 }
511 
512 /*
513  * smbd_refresh_monitor()
514  *
515  * Wait for a refresh event. When this thread wakes up, update the
516  * smbd configuration from the SMF config information then go back to
517  * wait for the next refresh.
518  */
519 /*ARGSUSED*/
520 static void *
521 smbd_refresh_monitor(void *arg)
522 {
523 	smb_io_t	smb_io;
524 	size_t		len;
525 	char		*new_dom;
526 	int		new_secmod;
527 	char		*old_dom;
528 	char		fqdn[MAXHOSTNAMELEN];
529 	int		rc = 0;
530 
531 	bzero(&smb_io, sizeof (smb_io));
532 	smb_io.sio_version = SMB_IOC_VERSION;
533 
534 	(void) pthread_mutex_lock(&refresh_mutex);
535 	while (pthread_cond_wait(&refresh_cond, &refresh_mutex) == 0) {
536 		/*
537 		 * We've been woken up by a refresh event so go do
538 		 * what is necessary.
539 		 */
540 		smb_ads_refresh();
541 		smb_ccache_remove(SMB_CCACHE_PATH);
542 
543 		if ((rc = smb_getfqdomainname(fqdn, MAXHOSTNAMELEN)) != 0)
544 			smbd_report("failed to get fully qualified domainname");
545 
546 		if (rc == 0)
547 			/* Clear rev zone before creating if list */
548 			if (dyndns_clear_rev_zone(fqdn) != 0)
549 				smbd_report("failed to clear DNS reverse "
550 				    "lookup zone");
551 
552 		/* re-initialize NIC table */
553 		if (smb_nic_init() != 0)
554 			smbd_report("failed to get NIC information");
555 
556 		smb_netbios_name_reconfig();
557 		smb_browser_reconfig();
558 
559 		if (rc == 0)
560 			if (dyndns_update_core(fqdn) != 0)
561 				smbd_report("failed to update dynamic DNS");
562 
563 		smb_set_netlogon_cred();
564 
565 		smb_load_kconfig(&smb_io.sio_data.cfg);
566 		new_dom = smb_io.sio_data.cfg.skc_nbdomain;
567 		old_dom = smbd.s_kcfg.skc_nbdomain;
568 		len = strlen(old_dom);
569 		new_secmod = smb_config_get_secmode();
570 		if ((len != strlen(new_dom)) ||
571 		    (strncasecmp(new_dom, old_dom, len)) ||
572 		    (new_secmod != smbd.s_secmode) ||
573 		    (smbd.s_drv_fd == -1)) {
574 			/*
575 			 * The active sessions have to be disconnected.
576 			 */
577 			smbd_kernel_unbind();
578 			if (smbd_kernel_bind()) {
579 				smbd_report("kernel bind error: %s",
580 				    strerror(errno));
581 			}
582 			continue;
583 		}
584 
585 		bcopy(&smb_io.sio_data.cfg, &smbd.s_kcfg, sizeof (smbd.s_kcfg));
586 		if (ioctl(smbd.s_drv_fd, SMB_IOC_CONFIG, &smb_io) < 0) {
587 			smbd_report("configuration update ioctl: %s",
588 			    strerror(errno));
589 		}
590 	}
591 	return (NULL);
592 }
593 
594 
595 /*
596  * If the door has already been opened by another process (non-zero pid
597  * in target), we assume that another smbd is already running.  If there
598  * is a race here, it will be caught later when smbsrv is opened because
599  * only one process is allowed to open the device at a time.
600  */
601 static int
602 smbd_already_running(void)
603 {
604 	door_info_t info;
605 	int door;
606 
607 	if ((door = open(SMB_DR_SVC_NAME, O_RDONLY)) < 0)
608 		return (0);
609 
610 	if (door_info(door, &info) < 0)
611 		return (0);
612 
613 	if (info.di_target > 0) {
614 		smbd_report("already running: pid %ld\n", info.di_target);
615 		(void) close(door);
616 		return (1);
617 	}
618 
619 	(void) close(door);
620 	return (0);
621 }
622 
623 /*
624  * smbd_kernel_bind
625  *
626  * This function open the smbsrv device and start the kernel service.
627  */
628 static int
629 smbd_kernel_bind(void)
630 {
631 	pthread_attr_t	tattr;
632 	smb_io_t	smb_io;
633 	int		rc1;
634 	int		rc2;
635 	int		rc;
636 
637 	bzero(&smb_io, sizeof (smb_io));
638 	smb_io.sio_version = SMB_IOC_VERSION;
639 
640 	if (smbd.s_drv_fd != -1)
641 		(void) close(smbd.s_drv_fd);
642 
643 	if ((smbd.s_drv_fd = open(DRV_DEVICE_PATH, 0)) < 0) {
644 		smbd.s_drv_fd = -1;
645 		return (errno);
646 	}
647 	smb_load_kconfig(&smbd.s_kcfg);
648 	bcopy(&smbd.s_kcfg, &smb_io.sio_data.cfg, sizeof (smb_io.sio_data.cfg));
649 	if (ioctl(smbd.s_drv_fd, SMB_IOC_CONFIG, &smb_io) < 0) {
650 		(void) close(smbd.s_drv_fd);
651 		smbd.s_drv_fd = -1;
652 		return (errno);
653 	}
654 	smb_io.sio_data.gmtoff = smbd_gmtoff();
655 	if (ioctl(smbd.s_drv_fd, SMB_IOC_GMTOFF, &smb_io) < 0) {
656 		(void) close(smbd.s_drv_fd);
657 		smbd.s_drv_fd = -1;
658 		return (errno);
659 	}
660 	smb_io.sio_data.start.opipe = smbd.s_door_opipe;
661 	smb_io.sio_data.start.lmshrd = smbd.s_door_lmshr;
662 	smb_io.sio_data.start.udoor = smbd.s_door_srv;
663 	if (ioctl(smbd.s_drv_fd, SMB_IOC_START, &smb_io) < 0) {
664 		(void) close(smbd.s_drv_fd);
665 		smbd.s_drv_fd = -1;
666 		return (errno);
667 	}
668 
669 	(void) pthread_attr_init(&tattr);
670 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
671 
672 	rc1 = pthread_create(&nbt_listener, &tattr, smbd_nbt_listener, NULL);
673 	if (rc1 != 0)
674 		smbd_report("unable to start NBT service");
675 
676 	rc2 = pthread_create(&tcp_listener, &tattr, smbd_tcp_listener, NULL);
677 	if (rc2 != 0)
678 		smbd_report("unable to start TCP service");
679 
680 	(void) pthread_attr_destroy(&tattr);
681 
682 	rc = rc1;
683 	if (rc == 0)
684 		rc = rc2;
685 
686 	if (rc == 0) {
687 		smbd.s_kbound = B_TRUE;
688 		return (0);
689 	}
690 
691 	(void) close(smbd.s_drv_fd);
692 	smbd.s_drv_fd = -1;
693 	return (rc);
694 }
695 
696 /*
697  * smbd_kernel_unbind
698  */
699 static void
700 smbd_kernel_unbind(void)
701 {
702 	if (smbd.s_drv_fd != -1) {
703 		(void) close(smbd.s_drv_fd);
704 		smbd.s_drv_fd = -1;
705 		smbd.s_kbound = B_FALSE;
706 	}
707 }
708 
709 /*
710  * Initialization of the localtime thread.
711  * Returns 0 on success, an error number if thread creation fails.
712  */
713 
714 int
715 smbd_localtime_init(void)
716 {
717 	pthread_attr_t tattr;
718 	int rc;
719 
720 	(void) pthread_attr_init(&tattr);
721 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
722 	rc = pthread_create(&localtime_thr, &tattr, smbd_localtime_monitor, 0);
723 	(void) pthread_attr_destroy(&tattr);
724 	return (rc);
725 }
726 
727 /*
728  * Local time thread to kernel land.
729  * Send local gmtoff to kernel module one time at startup
730  * and each time it changes (up to twice a year).
731  * Local gmtoff is checked once every 15 minutes and
732  * since some timezones are aligned on half and qtr hour boundaries,
733  * once an hour would likely suffice.
734  */
735 
736 /*ARGSUSED*/
737 static void *
738 smbd_localtime_monitor(void *arg)
739 {
740 	struct tm local_tm;
741 	time_t secs;
742 	int32_t gmtoff, last_gmtoff = -1;
743 	int timeout;
744 
745 	for (;;) {
746 		gmtoff = smbd_gmtoff();
747 
748 		if ((last_gmtoff != gmtoff) && (smbd.s_drv_fd != -1)) {
749 			if (ioctl(smbd.s_drv_fd, SMB_IOC_GMTOFF, &gmtoff) < 0) {
750 				smbd_report("localtime ioctl: %s",
751 				    strerror(errno));
752 			}
753 		}
754 
755 		/*
756 		 * Align the next iteration on a fifteen minute boundary.
757 		 */
758 		secs = time(0);
759 		(void) localtime_r(&secs, &local_tm);
760 		timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN);
761 		(void) sleep(timeout);
762 
763 		last_gmtoff = gmtoff;
764 	}
765 
766 	/*NOTREACHED*/
767 	return (NULL);
768 }
769 
770 /*
771  * smbd_gmtoff
772  *
773  * Determine offset from GMT. If daylight saving time use altzone,
774  * otherwise use timezone.
775  */
776 static int32_t
777 smbd_gmtoff(void)
778 {
779 	time_t clock_val;
780 	struct tm *atm;
781 	int32_t gmtoff;
782 
783 	(void) time(&clock_val);
784 	atm = localtime(&clock_val);
785 
786 	gmtoff = (atm->tm_isdst) ? altzone : timezone;
787 
788 	return (gmtoff);
789 }
790 
791 static void
792 smbd_sig_handler(int sigval)
793 {
794 	if (smbd.s_sigval == 0)
795 		smbd.s_sigval = sigval;
796 }
797 
798 /*
799  * Set up configuration options and parse the command line.
800  * This function will determine if we will run as a daemon
801  * or in the foreground.
802  *
803  * Failure to find a uid or gid results in using the default (0).
804  */
805 static int
806 smbd_setup_options(int argc, char *argv[])
807 {
808 	struct passwd *pwd;
809 	struct group *grp;
810 	int c;
811 
812 	if ((pwd = getpwnam("root")) != NULL)
813 		smbd.s_uid = pwd->pw_uid;
814 
815 	if ((grp = getgrnam("sys")) != NULL)
816 		smbd.s_gid = grp->gr_gid;
817 
818 	smbd.s_fg = smb_config_get_fg_flag();
819 
820 	while ((c = getopt(argc, argv, ":f")) != -1) {
821 		switch (c) {
822 		case 'f':
823 			smbd.s_fg = 1;
824 			break;
825 
826 		case ':':
827 		case '?':
828 		default:
829 			smbd_usage(stderr);
830 			return (-1);
831 		}
832 	}
833 
834 	return (0);
835 }
836 
837 static void
838 smbd_usage(FILE *fp)
839 {
840 	static char *help[] = {
841 		"-f  run program in foreground"
842 	};
843 
844 	int i;
845 
846 	(void) fprintf(fp, "Usage: %s [-f]\n", smbd.s_pname);
847 
848 	for (i = 0; i < sizeof (help)/sizeof (help[0]); ++i)
849 		(void) fprintf(fp, "    %s\n", help[i]);
850 }
851 
852 static void
853 smbd_report(const char *fmt, ...)
854 {
855 	char buf[128];
856 	va_list ap;
857 
858 	if (fmt == NULL)
859 		return;
860 
861 	va_start(ap, fmt);
862 	(void) vsnprintf(buf, 128, fmt, ap);
863 	va_end(ap);
864 
865 	(void) fprintf(stderr, "smbd: %s\n", buf);
866 }
867 
868 /*
869  * Enable libumem debugging by default on DEBUG builds.
870  */
871 #ifdef DEBUG
872 const char *
873 _umem_debug_init(void)
874 {
875 	return ("default,verbose"); /* $UMEM_DEBUG setting */
876 }
877 
878 const char *
879 _umem_logging_init(void)
880 {
881 	return ("fail,contents"); /* $UMEM_LOGGING setting */
882 }
883 #endif
884