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