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