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