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