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