xref: /titanic_44/usr/src/cmd/smbsrv/smbd/smbd_main.c (revision 193974072f41a843678abf5f61979c748687e66b)
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_lmshr = smb_share_dsrv_start();
405 	if (smbd.s_door_lmshr < 0) {
406 		smbd_report("share initialization failed");
407 	}
408 
409 	smbd.s_door_srv = smb_door_srv_start();
410 	if (smbd.s_door_srv < 0)
411 		return (rc);
412 
413 	if ((rc = smbd_refresh_init()) != 0)
414 		return (rc);
415 
416 	if (smb_getfqdomainname(fqdn, MAXHOSTNAMELEN) == 0)
417 		(void) dyndns_update_core(fqdn);
418 
419 	(void) smbd_localtime_init();
420 
421 	smbd.s_door_opipe = smbd_opipe_dsrv_start();
422 	if (smbd.s_door_opipe < 0) {
423 		smbd_report("opipe initialization failed %s",
424 		    strerror(errno));
425 		return (rc);
426 	}
427 
428 	(void) smb_lgrp_start();
429 
430 	smb_pwd_init(B_TRUE);
431 
432 	rc = smbd_kernel_bind();
433 	if (rc != 0) {
434 		smbd_report("kernel bind error: %s", strerror(errno));
435 		return (rc);
436 	}
437 
438 	if ((rc = smb_shr_start()) != 0) {
439 		smbd_report("share initialization failed: %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_resource_domain;
567 		old_dom = smbd.s_kcfg.skc_resource_domain;
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 	smb_io_t	smb_io;
632 	int		rc;
633 
634 	bzero(&smb_io, sizeof (smb_io));
635 	smb_io.sio_version = SMB_IOC_VERSION;
636 
637 	if (smbd.s_drv_fd != -1)
638 		(void) close(smbd.s_drv_fd);
639 
640 	if ((smbd.s_drv_fd = open(DRV_DEVICE_PATH, 0)) < 0) {
641 		smbd.s_drv_fd = -1;
642 		return (errno);
643 	}
644 	smb_load_kconfig(&smbd.s_kcfg);
645 	bcopy(&smbd.s_kcfg, &smb_io.sio_data.cfg, sizeof (smb_io.sio_data.cfg));
646 	if (ioctl(smbd.s_drv_fd, SMB_IOC_CONFIG, &smb_io) < 0) {
647 		(void) close(smbd.s_drv_fd);
648 		smbd.s_drv_fd = -1;
649 		return (errno);
650 	}
651 	smb_io.sio_data.gmtoff = smbd_gmtoff();
652 	if (ioctl(smbd.s_drv_fd, SMB_IOC_GMTOFF, &smb_io) < 0) {
653 		(void) close(smbd.s_drv_fd);
654 		smbd.s_drv_fd = -1;
655 		return (errno);
656 	}
657 	smb_io.sio_data.start.opipe = smbd.s_door_opipe;
658 	smb_io.sio_data.start.lmshrd = smbd.s_door_lmshr;
659 	smb_io.sio_data.start.udoor = smbd.s_door_srv;
660 	if (ioctl(smbd.s_drv_fd, SMB_IOC_START, &smb_io) < 0) {
661 		(void) close(smbd.s_drv_fd);
662 		smbd.s_drv_fd = -1;
663 		return (errno);
664 	}
665 
666 	rc = pthread_create(&nbt_listener, NULL, smbd_nbt_listener, NULL);
667 	if (rc == 0) {
668 		rc = pthread_create(&tcp_listener, NULL, smbd_tcp_listener,
669 		    NULL);
670 		if (rc == 0) {
671 			smbd.s_kbound = B_TRUE;
672 			return (0);
673 		}
674 	}
675 
676 	rc = pthread_create(&nbt_listener, NULL, smbd_nbt_listener, NULL);
677 	if (rc == 0) {
678 		rc = pthread_create(&tcp_listener, NULL, smbd_tcp_listener,
679 		    NULL);
680 		if (rc == 0) {
681 			smbd.s_kbound = B_TRUE;
682 			return (0);
683 		}
684 	}
685 	(void) close(smbd.s_drv_fd);
686 	smbd.s_drv_fd = -1;
687 	return (rc);
688 }
689 
690 /*
691  * smbd_kernel_unbind
692  */
693 static void
694 smbd_kernel_unbind(void)
695 {
696 	if (smbd.s_drv_fd != -1) {
697 		(void) close(smbd.s_drv_fd);
698 		smbd.s_drv_fd = -1;
699 		smbd.s_kbound = B_FALSE;
700 	}
701 }
702 
703 /*
704  * Initialization of the localtime thread.
705  * Returns 0 on success, an error number if thread creation fails.
706  */
707 
708 int
709 smbd_localtime_init(void)
710 {
711 	pthread_attr_t tattr;
712 	int rc;
713 
714 	(void) pthread_attr_init(&tattr);
715 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
716 	rc = pthread_create(&localtime_thr, &tattr, smbd_localtime_monitor, 0);
717 	(void) pthread_attr_destroy(&tattr);
718 	return (rc);
719 }
720 
721 /*
722  * Local time thread to kernel land.
723  * Send local gmtoff to kernel module one time at startup
724  * and each time it changes (up to twice a year).
725  * Local gmtoff is checked once every 15 minutes and
726  * since some timezones are aligned on half and qtr hour boundaries,
727  * once an hour would likely suffice.
728  */
729 
730 /*ARGSUSED*/
731 static void *
732 smbd_localtime_monitor(void *arg)
733 {
734 	struct tm local_tm;
735 	time_t secs;
736 	int32_t gmtoff, last_gmtoff = -1;
737 	int timeout;
738 
739 	for (;;) {
740 		gmtoff = smbd_gmtoff();
741 
742 		if ((last_gmtoff != gmtoff) && (smbd.s_drv_fd != -1)) {
743 			if (ioctl(smbd.s_drv_fd, SMB_IOC_GMTOFF, &gmtoff) < 0) {
744 				smbd_report("localtime ioctl: %s",
745 				    strerror(errno));
746 			}
747 		}
748 
749 		/*
750 		 * Align the next iteration on a fifteen minute boundary.
751 		 */
752 		secs = time(0);
753 		(void) localtime_r(&secs, &local_tm);
754 		timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN);
755 		(void) sleep(timeout);
756 
757 		last_gmtoff = gmtoff;
758 	}
759 
760 	/*NOTREACHED*/
761 	return (NULL);
762 }
763 
764 /*
765  * smbd_gmtoff
766  *
767  * Determine offset from GMT. If daylight saving time use altzone,
768  * otherwise use timezone.
769  */
770 static int32_t
771 smbd_gmtoff(void)
772 {
773 	time_t clock_val;
774 	struct tm *atm;
775 	int32_t gmtoff;
776 
777 	(void) time(&clock_val);
778 	atm = localtime(&clock_val);
779 
780 	gmtoff = (atm->tm_isdst) ? altzone : timezone;
781 
782 	return (gmtoff);
783 }
784 
785 static void
786 smbd_sig_handler(int sigval)
787 {
788 	if (smbd.s_sigval == 0)
789 		smbd.s_sigval = sigval;
790 }
791 
792 /*
793  * Set up configuration options and parse the command line.
794  * This function will determine if we will run as a daemon
795  * or in the foreground.
796  *
797  * Failure to find a uid or gid results in using the default (0).
798  */
799 static int
800 smbd_setup_options(int argc, char *argv[])
801 {
802 	struct passwd *pwd;
803 	struct group *grp;
804 	int c;
805 
806 	if ((pwd = getpwnam("root")) != NULL)
807 		smbd.s_uid = pwd->pw_uid;
808 
809 	if ((grp = getgrnam("sys")) != NULL)
810 		smbd.s_gid = grp->gr_gid;
811 
812 	smbd.s_fg = smb_config_get_fg_flag();
813 
814 	while ((c = getopt(argc, argv, ":f")) != -1) {
815 		switch (c) {
816 		case 'f':
817 			smbd.s_fg = 1;
818 			break;
819 
820 		case ':':
821 		case '?':
822 		default:
823 			smbd_usage(stderr);
824 			return (-1);
825 		}
826 	}
827 
828 	return (0);
829 }
830 
831 static void
832 smbd_usage(FILE *fp)
833 {
834 	static char *help[] = {
835 		"-f  run program in foreground"
836 	};
837 
838 	int i;
839 
840 	(void) fprintf(fp, "Usage: %s [-f]\n", smbd.s_pname);
841 
842 	for (i = 0; i < sizeof (help)/sizeof (help[0]); ++i)
843 		(void) fprintf(fp, "    %s\n", help[i]);
844 }
845 
846 static void
847 smbd_report(const char *fmt, ...)
848 {
849 	char buf[128];
850 	va_list ap;
851 
852 	if (fmt == NULL)
853 		return;
854 
855 	va_start(ap, fmt);
856 	(void) vsnprintf(buf, 128, fmt, ap);
857 	va_end(ap);
858 
859 	(void) fprintf(stderr, "smbd: %s\n", buf);
860 }
861 
862 /*
863  * Enable libumem debugging by default on DEBUG builds.
864  */
865 #ifdef DEBUG
866 const char *
867 _umem_debug_init(void)
868 {
869 	return ("default,verbose"); /* $UMEM_DEBUG setting */
870 }
871 
872 const char *
873 _umem_logging_init(void)
874 {
875 	return ("fail,contents"); /* $UMEM_LOGGING setting */
876 }
877 #endif
878