xref: /titanic_50/usr/src/cmd/smbsrv/smbd/smbd_main.c (revision 7bc22e45a20f905cdd06bb98c98a5c8be7fd25c0)
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 
533 	(void) pthread_mutex_lock(&refresh_mutex);
534 	while (pthread_cond_wait(&refresh_cond, &refresh_mutex) == 0) {
535 		/*
536 		 * We've been woken up by a refresh event so go do
537 		 * what is necessary.
538 		 */
539 		smb_ads_refresh();
540 		smb_ccache_remove(SMB_CCACHE_PATH);
541 
542 		if ((rc = smb_getfqdomainname(fqdn, MAXHOSTNAMELEN)) != 0)
543 			smbd_report("failed to get fully qualified domainname");
544 
545 		if (rc == 0)
546 			/* Clear rev zone before creating if list */
547 			if (dyndns_clear_rev_zone(fqdn) != 0)
548 				smbd_report("failed to clear DNS reverse "
549 				    "lookup zone");
550 
551 		/* re-initialize NIC table */
552 		if (smb_nic_init() != 0)
553 			smbd_report("failed to get NIC information");
554 
555 		smb_netbios_name_reconfig();
556 		smb_browser_reconfig();
557 
558 		if (rc == 0)
559 			if (dyndns_update_core(fqdn) != 0)
560 				smbd_report("failed to update dynamic DNS");
561 
562 		smb_set_netlogon_cred();
563 
564 		smb_load_kconfig(&smb_io.sio_data.cfg);
565 		new_dom = smb_io.sio_data.cfg.skc_nbdomain;
566 		old_dom = smbd.s_kcfg.skc_nbdomain;
567 		len = strlen(old_dom);
568 		new_secmod = smb_config_get_secmode();
569 		if ((len != strlen(new_dom)) ||
570 		    (strncasecmp(new_dom, old_dom, len)) ||
571 		    (new_secmod != smbd.s_secmode) ||
572 		    (smbd.s_drv_fd == -1)) {
573 			/*
574 			 * The active sessions have to be disconnected.
575 			 */
576 			smbd_kernel_unbind();
577 			if (smbd_kernel_bind()) {
578 				smbd_report("kernel bind error: %s",
579 				    strerror(errno));
580 			}
581 			continue;
582 		}
583 
584 		bcopy(&smb_io.sio_data.cfg, &smbd.s_kcfg, sizeof (smbd.s_kcfg));
585 		if (smbd_ioctl(SMB_IOC_CONFIG, &smb_io) < 0) {
586 			smbd_report("configuration update ioctl: %s",
587 			    strerror(errno));
588 		}
589 	}
590 	return (NULL);
591 }
592 
593 
594 /*
595  * If the door has already been opened by another process (non-zero pid
596  * in target), we assume that another smbd is already running.  If there
597  * is a race here, it will be caught later when smbsrv is opened because
598  * only one process is allowed to open the device at a time.
599  */
600 static int
601 smbd_already_running(void)
602 {
603 	door_info_t info;
604 	int door;
605 
606 	if ((door = open(SMB_DR_SVC_NAME, O_RDONLY)) < 0)
607 		return (0);
608 
609 	if (door_info(door, &info) < 0)
610 		return (0);
611 
612 	if (info.di_target > 0) {
613 		smbd_report("already running: pid %ld\n", info.di_target);
614 		(void) close(door);
615 		return (1);
616 	}
617 
618 	(void) close(door);
619 	return (0);
620 }
621 
622 /*
623  * smbd_kernel_bind
624  *
625  * This function open the smbsrv device and start the kernel service.
626  */
627 static int
628 smbd_kernel_bind(void)
629 {
630 	pthread_attr_t	tattr;
631 	smb_io_t	smb_io;
632 	int		rc1;
633 	int		rc2;
634 	int		rc;
635 
636 	bzero(&smb_io, sizeof (smb_io));
637 
638 	if (smbd.s_drv_fd != -1)
639 		(void) close(smbd.s_drv_fd);
640 
641 	if ((smbd.s_drv_fd = open(DRV_DEVICE_PATH, 0)) < 0) {
642 		smbd.s_drv_fd = -1;
643 		return (errno);
644 	}
645 
646 	smb_load_kconfig(&smbd.s_kcfg);
647 	bcopy(&smbd.s_kcfg, &smb_io.sio_data.cfg, sizeof (smb_io.sio_data.cfg));
648 	if (smbd_ioctl(SMB_IOC_CONFIG, &smb_io) < 0) {
649 		(void) close(smbd.s_drv_fd);
650 		smbd.s_drv_fd = -1;
651 		return (errno);
652 	}
653 	smb_io.sio_data.gmtoff = smbd_gmtoff();
654 	if (smbd_ioctl(SMB_IOC_GMTOFF, &smb_io) < 0) {
655 		(void) close(smbd.s_drv_fd);
656 		smbd.s_drv_fd = -1;
657 		return (errno);
658 	}
659 	smb_io.sio_data.start.opipe = smbd.s_door_opipe;
660 	smb_io.sio_data.start.lmshrd = smbd.s_door_lmshr;
661 	smb_io.sio_data.start.udoor = smbd.s_door_srv;
662 	if (smbd_ioctl(SMB_IOC_START, &smb_io) < 0) {
663 		(void) close(smbd.s_drv_fd);
664 		smbd.s_drv_fd = -1;
665 		return (errno);
666 	}
667 
668 	(void) pthread_attr_init(&tattr);
669 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
670 
671 	rc1 = pthread_create(&nbt_listener, &tattr, smbd_nbt_listener, NULL);
672 	if (rc1 != 0)
673 		smbd_report("unable to start NBT service");
674 
675 	rc2 = pthread_create(&tcp_listener, &tattr, smbd_tcp_listener, NULL);
676 	if (rc2 != 0)
677 		smbd_report("unable to start TCP service");
678 
679 	(void) pthread_attr_destroy(&tattr);
680 
681 	rc = rc1;
682 	if (rc == 0)
683 		rc = rc2;
684 
685 	if (rc == 0) {
686 		smbd.s_kbound = B_TRUE;
687 		return (0);
688 	}
689 
690 	(void) close(smbd.s_drv_fd);
691 	smbd.s_drv_fd = -1;
692 	return (rc);
693 }
694 
695 /*
696  * smbd_kernel_unbind
697  */
698 static void
699 smbd_kernel_unbind(void)
700 {
701 	if (smbd.s_drv_fd != -1) {
702 		(void) close(smbd.s_drv_fd);
703 		smbd.s_drv_fd = -1;
704 		smbd.s_kbound = B_FALSE;
705 	}
706 }
707 
708 int
709 smbd_ioctl(int cmd, smb_io_t *smb_io)
710 {
711 	smb_io->sio_version = SMB_IOC_VERSION;
712 	smb_io->sio_crc = 0;
713 	smb_io->sio_crc = smb_crc_gen((uint8_t *)smb_io, sizeof (smb_io_t));
714 
715 	return (ioctl(smbd.s_drv_fd, cmd, smb_io));
716 }
717 
718 /*
719  * Initialization of the localtime thread.
720  * Returns 0 on success, an error number if thread creation fails.
721  */
722 
723 int
724 smbd_localtime_init(void)
725 {
726 	pthread_attr_t tattr;
727 	int rc;
728 
729 	(void) pthread_attr_init(&tattr);
730 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
731 	rc = pthread_create(&localtime_thr, &tattr, smbd_localtime_monitor, 0);
732 	(void) pthread_attr_destroy(&tattr);
733 	return (rc);
734 }
735 
736 /*
737  * Local time thread to kernel land.
738  * Send local gmtoff to kernel module one time at startup
739  * and each time it changes (up to twice a year).
740  * Local gmtoff is checked once every 15 minutes and
741  * since some timezones are aligned on half and qtr hour boundaries,
742  * once an hour would likely suffice.
743  */
744 
745 /*ARGSUSED*/
746 static void *
747 smbd_localtime_monitor(void *arg)
748 {
749 	smb_io_t smb_io;
750 	struct tm local_tm;
751 	time_t secs;
752 	int32_t gmtoff, last_gmtoff = -1;
753 	int timeout;
754 
755 	bzero(&smb_io, sizeof (smb_io));
756 
757 	for (;;) {
758 		gmtoff = smbd_gmtoff();
759 
760 		if ((last_gmtoff != gmtoff) && (smbd.s_drv_fd != -1)) {
761 			smb_io.sio_data.gmtoff = gmtoff;
762 			if (smbd_ioctl(SMB_IOC_GMTOFF, &smb_io) < 0) {
763 				smbd_report("localtime ioctl: %s",
764 				    strerror(errno));
765 			}
766 		}
767 
768 		/*
769 		 * Align the next iteration on a fifteen minute boundary.
770 		 */
771 		secs = time(0);
772 		(void) localtime_r(&secs, &local_tm);
773 		timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN);
774 		(void) sleep(timeout);
775 
776 		last_gmtoff = gmtoff;
777 	}
778 
779 	/*NOTREACHED*/
780 	return (NULL);
781 }
782 
783 /*
784  * smbd_gmtoff
785  *
786  * Determine offset from GMT. If daylight saving time use altzone,
787  * otherwise use timezone.
788  */
789 static int32_t
790 smbd_gmtoff(void)
791 {
792 	time_t clock_val;
793 	struct tm *atm;
794 	int32_t gmtoff;
795 
796 	(void) time(&clock_val);
797 	atm = localtime(&clock_val);
798 
799 	gmtoff = (atm->tm_isdst) ? altzone : timezone;
800 
801 	return (gmtoff);
802 }
803 
804 static void
805 smbd_sig_handler(int sigval)
806 {
807 	if (smbd.s_sigval == 0)
808 		smbd.s_sigval = sigval;
809 }
810 
811 /*
812  * Set up configuration options and parse the command line.
813  * This function will determine if we will run as a daemon
814  * or in the foreground.
815  *
816  * Failure to find a uid or gid results in using the default (0).
817  */
818 static int
819 smbd_setup_options(int argc, char *argv[])
820 {
821 	struct passwd *pwd;
822 	struct group *grp;
823 	int c;
824 
825 	if ((pwd = getpwnam("root")) != NULL)
826 		smbd.s_uid = pwd->pw_uid;
827 
828 	if ((grp = getgrnam("sys")) != NULL)
829 		smbd.s_gid = grp->gr_gid;
830 
831 	smbd.s_fg = smb_config_get_fg_flag();
832 
833 	while ((c = getopt(argc, argv, ":f")) != -1) {
834 		switch (c) {
835 		case 'f':
836 			smbd.s_fg = 1;
837 			break;
838 
839 		case ':':
840 		case '?':
841 		default:
842 			smbd_usage(stderr);
843 			return (-1);
844 		}
845 	}
846 
847 	return (0);
848 }
849 
850 static void
851 smbd_usage(FILE *fp)
852 {
853 	static char *help[] = {
854 		"-f  run program in foreground"
855 	};
856 
857 	int i;
858 
859 	(void) fprintf(fp, "Usage: %s [-f]\n", smbd.s_pname);
860 
861 	for (i = 0; i < sizeof (help)/sizeof (help[0]); ++i)
862 		(void) fprintf(fp, "    %s\n", help[i]);
863 }
864 
865 static void
866 smbd_report(const char *fmt, ...)
867 {
868 	char buf[128];
869 	va_list ap;
870 
871 	if (fmt == NULL)
872 		return;
873 
874 	va_start(ap, fmt);
875 	(void) vsnprintf(buf, 128, fmt, ap);
876 	va_end(ap);
877 
878 	(void) fprintf(stderr, "smbd: %s\n", buf);
879 }
880 
881 /*
882  * Enable libumem debugging by default on DEBUG builds.
883  */
884 #ifdef DEBUG
885 const char *
886 _umem_debug_init(void)
887 {
888 	return ("default,verbose"); /* $UMEM_DEBUG setting */
889 }
890 
891 const char *
892 _umem_logging_init(void)
893 {
894 	return ("fail,contents"); /* $UMEM_LOGGING setting */
895 }
896 #endif
897