xref: /illumos-gate/usr/src/cmd/smbsrv/smbd/smbd_main.c (revision 46b592853d0f4f11781b6b0a7533f267c6aee132)
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 <sys/corectl.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <stdarg.h>
36 #include <fcntl.h>
37 #include <wait.h>
38 #include <signal.h>
39 #include <atomic.h>
40 #include <libscf.h>
41 #include <limits.h>
42 #include <priv_utils.h>
43 #include <door.h>
44 #include <errno.h>
45 #include <pthread.h>
46 #include <time.h>
47 #include <libscf.h>
48 #include <zone.h>
49 #include <libgen.h>
50 #include <pwd.h>
51 #include <grp.h>
52 
53 #include <smbsrv/smb_door_svc.h>
54 #include <smbsrv/smb_ioctl.h>
55 #include <smbsrv/libsmb.h>
56 #include <smbsrv/libsmbns.h>
57 #include <smbsrv/libmlsvc.h>
58 #include "smbd.h"
59 
60 #define	DRV_DEVICE_PATH	"/devices/pseudo/smbsrv@0:smbsrv"
61 #define	SMB_DBDIR "/var/smb"
62 
63 static void *smbd_nbt_listener(void *);
64 static void *smbd_tcp_listener(void *);
65 static void *smbd_nbt_receiver(void *);
66 static void *smbd_tcp_receiver(void *);
67 
68 static int smbd_daemonize_init(void);
69 static void smbd_daemonize_fini(int, int);
70 static int smb_init_daemon_priv(int, uid_t, gid_t);
71 
72 static int smbd_kernel_bind(void);
73 static void smbd_kernel_unbind(void);
74 static int smbd_already_running(void);
75 
76 static int smbd_service_init(void);
77 static void smbd_service_fini(void);
78 
79 static int smbd_setup_options(int argc, char *argv[]);
80 static void smbd_usage(FILE *fp);
81 static void smbd_report(const char *fmt, ...);
82 
83 static void smbd_sig_handler(int sig);
84 
85 static int32_t smbd_gmtoff(void);
86 static int smbd_localtime_init(void);
87 static void *smbd_localtime_monitor(void *arg);
88 
89 static pthread_t localtime_thr;
90 
91 static int smbd_refresh_init(void);
92 static void smbd_refresh_fini(void);
93 static void *smbd_refresh_monitor(void *);
94 static void smbd_refresh_dc(void);
95 
96 static void *smbd_nbt_receiver(void *);
97 static void *smbd_nbt_listener(void *);
98 
99 static void *smbd_tcp_receiver(void *);
100 static void *smbd_tcp_listener(void *);
101 
102 static int smbd_start_listeners(void);
103 static void smbd_stop_listeners(void);
104 static int smbd_kernel_start(void);
105 
106 static void smbd_fatal_error(const char *);
107 
108 static pthread_t refresh_thr;
109 static pthread_cond_t refresh_cond;
110 static pthread_mutex_t refresh_mutex;
111 
112 smbd_t smbd;
113 
114 /*
115  * smbd user land daemon
116  *
117  * Use SMF error codes only on return or exit.
118  */
119 int
120 main(int argc, char *argv[])
121 {
122 	struct sigaction	act;
123 	sigset_t		set;
124 	uid_t			uid;
125 	int			pfd = -1;
126 	uint_t			sigval;
127 
128 	smbd.s_pname = basename(argv[0]);
129 	openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
130 
131 	if (smbd_setup_options(argc, argv) != 0)
132 		return (SMF_EXIT_ERR_FATAL);
133 
134 	if ((uid = getuid()) != smbd.s_uid) {
135 		smbd_report("user %d: %s", uid, strerror(EPERM));
136 		return (SMF_EXIT_ERR_FATAL);
137 	}
138 
139 	if (getzoneid() != GLOBAL_ZONEID) {
140 		smbd_report("non-global zones are not supported");
141 		return (SMF_EXIT_ERR_FATAL);
142 	}
143 
144 	if (is_system_labeled()) {
145 		smbd_report("Trusted Extensions not supported");
146 		return (SMF_EXIT_ERR_FATAL);
147 	}
148 
149 	if (smbd_already_running())
150 		return (SMF_EXIT_OK);
151 
152 	(void) sigfillset(&set);
153 	(void) sigdelset(&set, SIGABRT);
154 
155 	(void) sigfillset(&act.sa_mask);
156 	act.sa_handler = smbd_sig_handler;
157 	act.sa_flags = 0;
158 
159 	(void) sigaction(SIGTERM, &act, NULL);
160 	(void) sigaction(SIGHUP, &act, NULL);
161 	(void) sigaction(SIGINT, &act, NULL);
162 	(void) sigaction(SIGPIPE, &act, NULL);
163 
164 	(void) sigdelset(&set, SIGTERM);
165 	(void) sigdelset(&set, SIGHUP);
166 	(void) sigdelset(&set, SIGINT);
167 	(void) sigdelset(&set, SIGPIPE);
168 
169 	if (smbd.s_fg) {
170 		(void) sigdelset(&set, SIGTSTP);
171 		(void) sigdelset(&set, SIGTTIN);
172 		(void) sigdelset(&set, SIGTTOU);
173 
174 		if (smbd_service_init() != 0) {
175 			smbd_report("service initialization failed");
176 			exit(SMF_EXIT_ERR_FATAL);
177 		}
178 	} else {
179 		/*
180 		 * "pfd" is a pipe descriptor -- any fatal errors
181 		 * during subsequent initialization of the child
182 		 * process should be written to this pipe and the
183 		 * parent will report this error as the exit status.
184 		 */
185 		pfd = smbd_daemonize_init();
186 
187 		if (smbd_service_init() != 0) {
188 			smbd_report("daemon initialization failed");
189 			exit(SMF_EXIT_ERR_FATAL);
190 		}
191 
192 		smbd_daemonize_fini(pfd, SMF_EXIT_OK);
193 	}
194 
195 	(void) atexit(smbd_service_fini);
196 
197 	while (!smbd.s_shutting_down) {
198 		if (smbd.s_sigval == 0 && smbd.s_refreshes == 0)
199 			(void) sigsuspend(&set);
200 
201 		sigval = atomic_swap_uint(&smbd.s_sigval, 0);
202 
203 		switch (sigval) {
204 		case 0:
205 		case SIGPIPE:
206 			break;
207 
208 		case SIGHUP:
209 			syslog(LOG_DEBUG, "refresh requested");
210 			(void) pthread_cond_signal(&refresh_cond);
211 			break;
212 
213 		default:
214 			/*
215 			 * Typically SIGINT or SIGTERM.
216 			 */
217 			smbd.s_shutting_down = B_TRUE;
218 			break;
219 		}
220 	}
221 
222 	smbd_service_fini();
223 	closelog();
224 	return ((smbd.s_fatal_error) ? SMF_EXIT_ERR_FATAL : SMF_EXIT_OK);
225 }
226 
227 /*
228  * This function will fork off a child process,
229  * from which only the child will return.
230  *
231  * Use SMF error codes only on exit.
232  */
233 static int
234 smbd_daemonize_init(void)
235 {
236 	int status, pfds[2];
237 	sigset_t set, oset;
238 	pid_t pid;
239 	int rc;
240 
241 	/*
242 	 * Reset privileges to the minimum set required. We continue
243 	 * to run as root to create and access files in /var.
244 	 */
245 	rc = smb_init_daemon_priv(PU_RESETGROUPS, smbd.s_uid, smbd.s_gid);
246 
247 	if (rc != 0) {
248 		smbd_report("insufficient privileges");
249 		exit(SMF_EXIT_ERR_FATAL);
250 	}
251 
252 	/*
253 	 * Block all signals prior to the fork and leave them blocked in the
254 	 * parent so we don't get in a situation where the parent gets SIGINT
255 	 * and returns non-zero exit status and the child is actually running.
256 	 * In the child, restore the signal mask once we've done our setsid().
257 	 */
258 	(void) sigfillset(&set);
259 	(void) sigdelset(&set, SIGABRT);
260 	(void) sigprocmask(SIG_BLOCK, &set, &oset);
261 
262 	if (pipe(pfds) == -1) {
263 		smbd_report("unable to create pipe");
264 		exit(SMF_EXIT_ERR_FATAL);
265 	}
266 
267 	closelog();
268 
269 	if ((pid = fork()) == -1) {
270 		openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
271 		smbd_report("unable to fork");
272 		closelog();
273 		exit(SMF_EXIT_ERR_FATAL);
274 	}
275 
276 	/*
277 	 * If we're the parent process, wait for either the child to send us
278 	 * the appropriate exit status over the pipe or for the read to fail
279 	 * (presumably with 0 for EOF if our child terminated abnormally).
280 	 * If the read fails, exit with either the child's exit status if it
281 	 * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
282 	 */
283 	if (pid != 0) {
284 		(void) close(pfds[1]);
285 
286 		if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
287 			_exit(status);
288 
289 		if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
290 			_exit(WEXITSTATUS(status));
291 
292 		_exit(SMF_EXIT_ERR_FATAL);
293 	}
294 
295 	openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
296 	(void) setsid();
297 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
298 	(void) chdir("/");
299 	(void) umask(022);
300 	(void) close(pfds[0]);
301 
302 	return (pfds[1]);
303 }
304 
305 /*
306  * This function is based on __init_daemon_priv() and replaces
307  * __init_daemon_priv() since we want smbd to have all privileges so that it
308  * can execute map/unmap commands with all privileges during share
309  * connection/disconnection.  Unused privileges are disabled until command
310  * execution.  The permitted and the limit set contains all privileges.  The
311  * inheritable set contains no privileges.
312  */
313 
314 static const char root_cp[] = "/core.%f.%t";
315 static const char daemon_cp[] = "/var/tmp/core.%f.%t";
316 
317 static int
318 smb_init_daemon_priv(int flags, uid_t uid, gid_t gid)
319 {
320 	priv_set_t *perm = NULL;
321 	int ret = -1;
322 	char buf[1024];
323 
324 	/*
325 	 * This is not a significant failure: it allows us to start programs
326 	 * with sufficient privileges and with the proper uid.   We don't
327 	 * care enough about the extra groups in that case.
328 	 */
329 	if (flags & PU_RESETGROUPS)
330 		(void) setgroups(0, NULL);
331 
332 	if (gid != (gid_t)-1 && setgid(gid) != 0)
333 		goto end;
334 
335 	perm = priv_allocset();
336 	if (perm == NULL)
337 		goto end;
338 
339 	/* E = P */
340 	(void) getppriv(PRIV_PERMITTED, perm);
341 	(void) setppriv(PRIV_SET, PRIV_EFFECTIVE, perm);
342 
343 	/* Now reset suid and euid */
344 	if (uid != (uid_t)-1 && setreuid(uid, uid) != 0)
345 		goto end;
346 
347 	/* I = 0 */
348 	priv_emptyset(perm);
349 	ret = setppriv(PRIV_SET, PRIV_INHERITABLE, perm);
350 end:
351 	priv_freeset(perm);
352 
353 	if (core_get_process_path(buf, sizeof (buf), getpid()) == 0 &&
354 	    strcmp(buf, "core") == 0) {
355 
356 		if ((uid == (uid_t)-1 ? geteuid() : uid) == 0) {
357 			(void) core_set_process_path(root_cp, sizeof (root_cp),
358 			    getpid());
359 		} else {
360 			(void) core_set_process_path(daemon_cp,
361 			    sizeof (daemon_cp), getpid());
362 		}
363 	}
364 	(void) setpflags(__PROC_PROTECT, 0);
365 
366 	return (ret);
367 }
368 
369 /*
370  * Most privileges, except the ones that are required for smbd, are turn off
371  * in the effective set.  They will be turn on when needed for command
372  * execution during share connection/disconnection.
373  */
374 static void
375 smbd_daemonize_fini(int fd, int exit_status)
376 {
377 	priv_set_t *pset;
378 
379 	/*
380 	 * Now that we're running, if a pipe fd was specified, write an exit
381 	 * status to it to indicate that our parent process can safely detach.
382 	 * Then proceed to loading the remaining non-built-in modules.
383 	 */
384 	if (fd >= 0)
385 		(void) write(fd, &exit_status, sizeof (exit_status));
386 
387 	(void) close(fd);
388 
389 	pset = priv_allocset();
390 	if (pset == NULL)
391 		return;
392 
393 	priv_emptyset(pset);
394 
395 	/* list of privileges for smbd */
396 	(void) priv_addset(pset, PRIV_NET_MAC_AWARE);
397 	(void) priv_addset(pset, PRIV_NET_PRIVADDR);
398 	(void) priv_addset(pset, PRIV_PROC_AUDIT);
399 	(void) priv_addset(pset, PRIV_SYS_DEVICES);
400 	(void) priv_addset(pset, PRIV_SYS_SMB);
401 
402 	priv_inverse(pset);
403 
404 	/* turn off unneeded privileges */
405 	(void) setppriv(PRIV_OFF, PRIV_EFFECTIVE, pset);
406 
407 	priv_freeset(pset);
408 
409 	/* reenable core dumps */
410 	__fini_daemon_priv(NULL);
411 }
412 
413 /*
414  * smbd_service_init
415  */
416 static int
417 smbd_service_init(void)
418 {
419 	int	rc;
420 
421 	smbd.s_pid = getpid();
422 	if ((mkdir(SMB_DBDIR, 0700) < 0) && (errno != EEXIST)) {
423 		smbd_report("mkdir %s: %s", SMB_DBDIR, strerror(errno));
424 		return (1);
425 	}
426 
427 	if ((rc = smb_ccache_init(SMB_VARRUN_DIR, SMB_CCACHE_FILE)) != 0) {
428 		if (rc == -1)
429 			smbd_report("mkdir %s: %s", SMB_VARRUN_DIR,
430 			    strerror(errno));
431 		else
432 			smbd_report("unable to set KRB5CCNAME");
433 		return (1);
434 	}
435 
436 
437 	(void) oem_language_set("english");
438 
439 	if (!smb_wka_init()) {
440 		smbd_report("out of memory");
441 		return (1);
442 	}
443 
444 	if (smb_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI) != 0)
445 		smbd_report("NIC monitoring failed to start");
446 
447 	(void) dyndns_start();
448 	smb_ipc_init();
449 
450 	if (smb_netbios_start() != 0)
451 		smbd_report("NetBIOS services failed to start");
452 	else
453 		smbd_report("NetBIOS services started");
454 
455 	/* Get the ID map client handle */
456 	if ((rc = smb_idmap_start()) != 0) {
457 		smbd_report("no idmap handle");
458 		return (rc);
459 	}
460 
461 	smbd.s_secmode = smb_config_get_secmode();
462 	if ((rc = smb_domain_init(smbd.s_secmode)) != 0) {
463 		if (rc == SMB_DOMAIN_NOMACHINE_SID) {
464 			smbd_report(
465 			    "no machine SID: check idmap configuration");
466 			return (rc);
467 		}
468 	}
469 
470 	smb_ads_init();
471 	if ((rc = mlsvc_init()) != 0) {
472 		smbd_report("msrpc initialization failed");
473 		return (rc);
474 	}
475 
476 	if (smbd.s_secmode == SMB_SECMODE_DOMAIN)
477 		if (smbd_locate_dc_start() != 0)
478 			smbd_report("dc discovery failed %s", strerror(errno));
479 
480 	smbd.s_door_srv = smb_door_srv_start();
481 	if (smbd.s_door_srv < 0)
482 		return (rc);
483 
484 	if ((rc = smbd_refresh_init()) != 0)
485 		return (rc);
486 
487 	dyndns_update_zones();
488 
489 	(void) smbd_localtime_init();
490 
491 	smbd.s_door_opipe = smbd_opipe_dsrv_start();
492 	if (smbd.s_door_opipe < 0) {
493 		smbd_report("opipe initialization failed %s",
494 		    strerror(errno));
495 		return (rc);
496 	}
497 
498 	(void) smb_lgrp_start();
499 
500 	smb_pwd_init(B_TRUE);
501 
502 	if ((rc = smb_shr_start()) != 0) {
503 		smbd_report("share initialization failed: %s", strerror(errno));
504 		return (rc);
505 	}
506 
507 	smbd.s_door_lmshr = smb_share_dsrv_start();
508 	if (smbd.s_door_lmshr < 0) {
509 		smbd_report("share initialization failed");
510 	}
511 
512 	if ((rc = smbd_kernel_bind()) != 0) {
513 		smbd_report("kernel bind error: %s", strerror(errno));
514 		return (rc);
515 	}
516 
517 	if ((rc = smb_shr_load()) != 0) {
518 		smbd_report("failed to start loading shares: %s",
519 		    strerror(errno));
520 		return (rc);
521 	}
522 
523 	return (0);
524 }
525 
526 /*
527  * Close the kernel service and shutdown smbd services.
528  * This function is registered with atexit(): ensure that anything
529  * called from here is safe to be called multiple times.
530  */
531 static void
532 smbd_service_fini(void)
533 {
534 	smbd_opipe_dsrv_stop();
535 	smb_wka_fini();
536 	smbd_refresh_fini();
537 	smbd_kernel_unbind();
538 	smb_door_srv_stop();
539 	smb_share_dsrv_stop();
540 	smb_shr_stop();
541 	dyndns_stop();
542 	smb_nicmon_stop();
543 	smb_idmap_stop();
544 	smb_lgrp_stop();
545 	smb_ccache_remove(SMB_CCACHE_PATH);
546 	smb_pwd_fini();
547 	smb_domain_fini();
548 	mlsvc_fini();
549 	smb_ads_fini();
550 	smb_netbios_stop();
551 }
552 
553 
554 /*
555  * smbd_refresh_init()
556  *
557  * SMB service refresh thread initialization.  This thread waits for a
558  * refresh event and updates the daemon's view of the configuration
559  * before going back to sleep.
560  */
561 static int
562 smbd_refresh_init()
563 {
564 	pthread_attr_t		tattr;
565 	pthread_condattr_t	cattr;
566 	int			rc;
567 
568 	(void) pthread_condattr_init(&cattr);
569 	(void) pthread_cond_init(&refresh_cond, &cattr);
570 	(void) pthread_condattr_destroy(&cattr);
571 
572 	(void) pthread_mutex_init(&refresh_mutex, NULL);
573 
574 	(void) pthread_attr_init(&tattr);
575 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
576 	rc = pthread_create(&refresh_thr, &tattr, smbd_refresh_monitor, 0);
577 	(void) pthread_attr_destroy(&tattr);
578 
579 	return (rc);
580 }
581 
582 /*
583  * smbd_refresh_fini()
584  *
585  * Stop the refresh thread.
586  */
587 static void
588 smbd_refresh_fini()
589 {
590 	(void) pthread_cancel(refresh_thr);
591 
592 	(void) pthread_cond_destroy(&refresh_cond);
593 	(void) pthread_mutex_destroy(&refresh_mutex);
594 }
595 
596 /*
597  * smbd_refresh_monitor()
598  *
599  * Wait for a refresh event. When this thread wakes up, update the
600  * smbd configuration from the SMF config information then go back to
601  * wait for the next refresh.
602  */
603 /*ARGSUSED*/
604 static void *
605 smbd_refresh_monitor(void *arg)
606 {
607 	smb_kmod_cfg_t	cfg;
608 	int		error;
609 
610 	while (!smbd.s_shutting_down) {
611 		(void) pthread_mutex_lock(&refresh_mutex);
612 		while ((atomic_swap_uint(&smbd.s_refreshes, 0) == 0) &&
613 		    (!smbd.s_shutting_down))
614 			(void) pthread_cond_wait(&refresh_cond, &refresh_mutex);
615 		(void) pthread_mutex_unlock(&refresh_mutex);
616 
617 		if (smbd.s_shutting_down) {
618 			syslog(LOG_DEBUG, "shutting down");
619 			exit((smbd.s_fatal_error) ? SMF_EXIT_ERR_FATAL :
620 			    SMF_EXIT_OK);
621 		}
622 
623 		/*
624 		 * We've been woken up by a refresh event so go do
625 		 * what is necessary.
626 		 */
627 		smb_ads_refresh();
628 		smb_ccache_remove(SMB_CCACHE_PATH);
629 
630 		/*
631 		 * Start the dyndns thread, if required.
632 		 * Clear the DNS zones for the existing interfaces
633 		 * before updating the NIC interface list.
634 		 */
635 		(void) dyndns_start();
636 		dyndns_clear_zones();
637 
638 		/* re-initialize NIC table */
639 		if (smb_nic_init() != 0)
640 			smbd_report("failed to get NIC information");
641 		smb_netbios_name_reconfig();
642 		smb_browser_reconfig();
643 		smbd_refresh_dc();
644 		dyndns_update_zones();
645 
646 		if (smbd_set_netlogon_cred()) {
647 			/*
648 			 * Restart required because the domain changed
649 			 * or the credential chain setup failed.
650 			 */
651 			if (smb_smf_restart_service() != 0) {
652 				syslog(LOG_ERR,
653 				    "unable to restart smb service. "
654 				    "Run 'svcs -xv smb/server' for more "
655 				    "information.");
656 				smbd.s_shutting_down = B_TRUE;
657 				exit(SMF_EXIT_OK);
658 			}
659 
660 			break;
661 		}
662 
663 		if (!smbd.s_kbound) {
664 			error = smbd_kernel_bind();
665 			if (error != 0)
666 				smbd_report("kernel bind error: %s",
667 				    strerror(error));
668 			else
669 				(void) smb_shr_load();
670 
671 			continue;
672 		}
673 
674 		(void) smb_shr_load();
675 
676 		smb_load_kconfig(&cfg);
677 		error = smb_kmod_setcfg(&cfg);
678 		if (error < 0)
679 			smbd_report("configuration update failed: %s",
680 			    strerror(error));
681 	}
682 
683 	return (NULL);
684 }
685 
686 /*
687  * Update DC information on a refresh.
688  */
689 static void
690 smbd_refresh_dc(void)
691 {
692 	char fqdomain[MAXHOSTNAMELEN];
693 	if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN)
694 		return;
695 
696 	if (smb_getfqdomainname(fqdomain, MAXHOSTNAMELEN))
697 		return;
698 
699 	if (smb_locate_dc(fqdomain, "", NULL))
700 		smbd_report("DC discovery failed");
701 }
702 
703 void
704 smbd_set_secmode(int secmode)
705 {
706 	switch (secmode) {
707 	case SMB_SECMODE_WORKGRP:
708 	case SMB_SECMODE_DOMAIN:
709 		(void) smb_config_set_secmode(secmode);
710 		smbd.s_secmode = secmode;
711 		break;
712 
713 	default:
714 		syslog(LOG_ERR, "invalid security mode: %d", secmode);
715 		syslog(LOG_ERR, "entering maintenance mode");
716 		(void) smb_smf_maintenance_mode();
717 	}
718 }
719 
720 /*
721  * If the door has already been opened by another process (non-zero pid
722  * in target), we assume that another smbd is already running.  If there
723  * is a race here, it will be caught later when smbsrv is opened because
724  * only one process is allowed to open the device at a time.
725  */
726 static int
727 smbd_already_running(void)
728 {
729 	door_info_t info;
730 	int door;
731 
732 	if ((door = open(SMB_DR_SVC_NAME, O_RDONLY)) < 0)
733 		return (0);
734 
735 	if (door_info(door, &info) < 0)
736 		return (0);
737 
738 	if (info.di_target > 0) {
739 		smbd_report("already running: pid %ld\n", info.di_target);
740 		(void) close(door);
741 		return (1);
742 	}
743 
744 	(void) close(door);
745 	return (0);
746 }
747 
748 /*
749  * smbd_kernel_bind
750  *
751  * This function open the smbsrv device and start the kernel service.
752  */
753 static int
754 smbd_kernel_bind(void)
755 {
756 	int rc;
757 
758 	smbd_kernel_unbind();
759 
760 	rc = smb_kmod_bind();
761 	if (rc == 0) {
762 		rc = smbd_kernel_start();
763 		if (rc != 0)
764 			smb_kmod_unbind();
765 		else
766 			smbd.s_kbound = B_TRUE;
767 	}
768 	return (rc);
769 }
770 
771 static int
772 smbd_kernel_start(void)
773 {
774 	smb_kmod_cfg_t	cfg;
775 	int		rc;
776 
777 	smb_load_kconfig(&cfg);
778 	rc = smb_kmod_setcfg(&cfg);
779 	if (rc != 0)
780 		return (rc);
781 
782 	rc = smb_kmod_setgmtoff(smbd_gmtoff());
783 	if (rc != 0)
784 		return (rc);
785 
786 	rc = smb_kmod_start(smbd.s_door_opipe, smbd.s_door_lmshr,
787 	    smbd.s_door_srv);
788 	if (rc != 0)
789 		return (rc);
790 
791 	rc = smbd_start_listeners();
792 	if (rc != 0)
793 		return (rc);
794 
795 	return (0);
796 }
797 
798 /*
799  * smbd_kernel_unbind
800  */
801 static void
802 smbd_kernel_unbind(void)
803 {
804 	smbd_stop_listeners();
805 	smb_kmod_unbind();
806 	smbd.s_kbound = B_FALSE;
807 }
808 
809 /*
810  * Initialization of the localtime thread.
811  * Returns 0 on success, an error number if thread creation fails.
812  */
813 
814 int
815 smbd_localtime_init(void)
816 {
817 	pthread_attr_t tattr;
818 	int rc;
819 
820 	(void) pthread_attr_init(&tattr);
821 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
822 	rc = pthread_create(&localtime_thr, &tattr, smbd_localtime_monitor, 0);
823 	(void) pthread_attr_destroy(&tattr);
824 	return (rc);
825 }
826 
827 /*
828  * Local time thread to kernel land.
829  * Send local gmtoff to kernel module one time at startup
830  * and each time it changes (up to twice a year).
831  * Local gmtoff is checked once every 15 minutes and
832  * since some timezones are aligned on half and qtr hour boundaries,
833  * once an hour would likely suffice.
834  */
835 
836 /*ARGSUSED*/
837 static void *
838 smbd_localtime_monitor(void *arg)
839 {
840 	struct tm local_tm;
841 	time_t secs;
842 	int32_t gmtoff, last_gmtoff = -1;
843 	int timeout;
844 	int error;
845 
846 	for (;;) {
847 		gmtoff = smbd_gmtoff();
848 
849 		if ((last_gmtoff != gmtoff) && smbd.s_kbound) {
850 			error = smb_kmod_setgmtoff(gmtoff);
851 			if (error != 0)
852 				smbd_report("localtime set failed: %s",
853 				    strerror(error));
854 		}
855 
856 		/*
857 		 * Align the next iteration on a fifteen minute boundary.
858 		 */
859 		secs = time(0);
860 		(void) localtime_r(&secs, &local_tm);
861 		timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN);
862 		(void) sleep(timeout);
863 
864 		last_gmtoff = gmtoff;
865 	}
866 
867 	/*NOTREACHED*/
868 	return (NULL);
869 }
870 
871 /*
872  * smbd_gmtoff
873  *
874  * Determine offset from GMT. If daylight saving time use altzone,
875  * otherwise use timezone.
876  */
877 static int32_t
878 smbd_gmtoff(void)
879 {
880 	time_t clock_val;
881 	struct tm *atm;
882 	int32_t gmtoff;
883 
884 	(void) time(&clock_val);
885 	atm = localtime(&clock_val);
886 
887 	gmtoff = (atm->tm_isdst) ? altzone : timezone;
888 
889 	return (gmtoff);
890 }
891 
892 static void
893 smbd_sig_handler(int sigval)
894 {
895 	if (smbd.s_sigval == 0)
896 		(void) atomic_swap_uint(&smbd.s_sigval, sigval);
897 
898 	if (sigval == SIGHUP) {
899 		atomic_inc_uint(&smbd.s_refreshes);
900 		(void) pthread_cond_signal(&refresh_cond);
901 	}
902 
903 	if (sigval == SIGINT || sigval == SIGTERM) {
904 		smbd.s_shutting_down = B_TRUE;
905 		(void) pthread_cond_signal(&refresh_cond);
906 	}
907 }
908 
909 /*
910  * Set up configuration options and parse the command line.
911  * This function will determine if we will run as a daemon
912  * or in the foreground.
913  *
914  * Failure to find a uid or gid results in using the default (0).
915  */
916 static int
917 smbd_setup_options(int argc, char *argv[])
918 {
919 	struct passwd *pwd;
920 	struct group *grp;
921 	int c;
922 
923 	if ((pwd = getpwnam("root")) != NULL)
924 		smbd.s_uid = pwd->pw_uid;
925 
926 	if ((grp = getgrnam("sys")) != NULL)
927 		smbd.s_gid = grp->gr_gid;
928 
929 	smbd.s_fg = smb_config_get_fg_flag();
930 
931 	while ((c = getopt(argc, argv, ":f")) != -1) {
932 		switch (c) {
933 		case 'f':
934 			smbd.s_fg = 1;
935 			break;
936 
937 		case ':':
938 		case '?':
939 		default:
940 			smbd_usage(stderr);
941 			return (-1);
942 		}
943 	}
944 
945 	return (0);
946 }
947 
948 static void
949 smbd_usage(FILE *fp)
950 {
951 	static char *help[] = {
952 		"-f  run program in foreground"
953 	};
954 
955 	int i;
956 
957 	(void) fprintf(fp, "Usage: %s [-f]\n", smbd.s_pname);
958 
959 	for (i = 0; i < sizeof (help)/sizeof (help[0]); ++i)
960 		(void) fprintf(fp, "    %s\n", help[i]);
961 }
962 
963 static void
964 smbd_report(const char *fmt, ...)
965 {
966 	char buf[128];
967 	va_list ap;
968 
969 	if (fmt == NULL)
970 		return;
971 
972 	va_start(ap, fmt);
973 	(void) vsnprintf(buf, 128, fmt, ap);
974 	va_end(ap);
975 
976 	(void) fprintf(stderr, "smbd: %s\n", buf);
977 }
978 
979 static int
980 smbd_start_listeners(void)
981 {
982 	int		rc1;
983 	int		rc2;
984 	pthread_attr_t	tattr;
985 
986 	(void) pthread_attr_init(&tattr);
987 
988 	if (!smbd.s_nbt_listener_running) {
989 		rc1 = pthread_create(&smbd.s_nbt_listener_id, &tattr,
990 		    smbd_nbt_listener, NULL);
991 		if (rc1 != 0)
992 			smbd_report("unable to start NBT service");
993 		else
994 			smbd.s_nbt_listener_running = B_TRUE;
995 	}
996 
997 	if (!smbd.s_tcp_listener_running) {
998 		rc2 = pthread_create(&smbd.s_tcp_listener_id, &tattr,
999 		    smbd_tcp_listener, NULL);
1000 		if (rc2 != 0)
1001 			smbd_report("unable to start TCP service");
1002 		else
1003 			smbd.s_tcp_listener_running = B_TRUE;
1004 	}
1005 
1006 	(void) pthread_attr_destroy(&tattr);
1007 
1008 	if (rc1 != 0)
1009 		return (rc1);
1010 	return (rc2);
1011 }
1012 
1013 static void
1014 smbd_stop_listeners(void)
1015 {
1016 	void	*status;
1017 
1018 	if (smbd.s_nbt_listener_running) {
1019 		(void) pthread_kill(smbd.s_nbt_listener_id, SIGTERM);
1020 		(void) pthread_join(smbd.s_nbt_listener_id, &status);
1021 		smbd.s_nbt_listener_running = B_FALSE;
1022 	}
1023 
1024 	if (smbd.s_tcp_listener_running) {
1025 		(void) pthread_kill(smbd.s_tcp_listener_id, SIGTERM);
1026 		(void) pthread_join(smbd.s_tcp_listener_id, &status);
1027 		smbd.s_tcp_listener_running = B_FALSE;
1028 	}
1029 }
1030 
1031 /*
1032  * Perform fatal error exit.
1033  */
1034 static void
1035 smbd_fatal_error(const char *msg)
1036 {
1037 	if (msg == NULL)
1038 		msg = "Fatal error";
1039 
1040 	smbd_report("%s", msg);
1041 	smbd.s_fatal_error = B_TRUE;
1042 	(void) kill(smbd.s_pid, SIGTERM);
1043 }
1044 
1045 /*ARGSUSED*/
1046 static void *
1047 smbd_nbt_receiver(void *arg)
1048 {
1049 	(void) smb_kmod_nbtreceive();
1050 	return (NULL);
1051 }
1052 
1053 /*ARGSUSED*/
1054 static void *
1055 smbd_nbt_listener(void *arg)
1056 {
1057 	pthread_attr_t	tattr;
1058 	sigset_t	set;
1059 	sigset_t	oset;
1060 	pthread_t	tid;
1061 	int		error = 0;
1062 
1063 	(void) sigfillset(&set);
1064 	(void) sigdelset(&set, SIGTERM);
1065 	(void) sigdelset(&set, SIGINT);
1066 	(void) pthread_sigmask(SIG_SETMASK, &set, &oset);
1067 	(void) pthread_attr_init(&tattr);
1068 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
1069 
1070 	while (smb_kmod_nbtlisten(error) == 0)
1071 		error = pthread_create(&tid, &tattr, smbd_nbt_receiver, NULL);
1072 
1073 	(void) pthread_attr_destroy(&tattr);
1074 
1075 	if (!smbd.s_shutting_down)
1076 		smbd_fatal_error("NBT listener thread terminated unexpectedly");
1077 
1078 	return (NULL);
1079 }
1080 
1081 /*ARGSUSED*/
1082 static void *
1083 smbd_tcp_receiver(void *arg)
1084 {
1085 	(void) smb_kmod_tcpreceive();
1086 	return (NULL);
1087 }
1088 
1089 /*ARGSUSED*/
1090 static void *
1091 smbd_tcp_listener(void *arg)
1092 {
1093 	pthread_attr_t	tattr;
1094 	sigset_t	set;
1095 	sigset_t	oset;
1096 	pthread_t	tid;
1097 	int		error = 0;
1098 
1099 	(void) sigfillset(&set);
1100 	(void) sigdelset(&set, SIGTERM);
1101 	(void) sigdelset(&set, SIGINT);
1102 	(void) pthread_sigmask(SIG_SETMASK, &set, &oset);
1103 	(void) pthread_attr_init(&tattr);
1104 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
1105 
1106 	while (smb_kmod_tcplisten(error) == 0)
1107 		error = pthread_create(&tid, &tattr, smbd_tcp_receiver, NULL);
1108 
1109 	(void) pthread_attr_destroy(&tattr);
1110 
1111 	if (!smbd.s_shutting_down)
1112 		smbd_fatal_error("TCP listener thread terminated unexpectedly");
1113 
1114 	return (NULL);
1115 }
1116 
1117 /*
1118  * Enable libumem debugging by default on DEBUG builds.
1119  */
1120 #ifdef DEBUG
1121 const char *
1122 _umem_debug_init(void)
1123 {
1124 	return ("default,verbose"); /* $UMEM_DEBUG setting */
1125 }
1126 
1127 const char *
1128 _umem_logging_init(void)
1129 {
1130 	return ("fail,contents"); /* $UMEM_LOGGING setting */
1131 }
1132 #endif
1133