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