1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/types.h> 29 #include <sys/stat.h> 30 #include <sys/ioccom.h> 31 #include <stdio.h> 32 #include <string.h> 33 #include <strings.h> 34 #include <stdlib.h> 35 #include <unistd.h> 36 #include <stdarg.h> 37 #include <fcntl.h> 38 #include <wait.h> 39 #include <signal.h> 40 #include <libscf.h> 41 #include <limits.h> 42 #include <priv_utils.h> 43 #include <door.h> 44 #include <errno.h> 45 #include <syslog.h> 46 #include <pthread.h> 47 #include <time.h> 48 #include <libscf.h> 49 #include <zone.h> 50 #include <tzfile.h> 51 #include <libgen.h> 52 #include <pwd.h> 53 #include <grp.h> 54 55 #include <smbsrv/smb_door_svc.h> 56 #include <smbsrv/smb_ioctl.h> 57 #include <smbsrv/libsmb.h> 58 #include <smbsrv/libsmbns.h> 59 #include <smbsrv/libsmbrdr.h> 60 #include <smbsrv/libmlsvc.h> 61 62 #include "smbd.h" 63 64 #define DRV_DEVICE_PATH "/devices/pseudo/smbsrv@0:smbsrv" 65 #define SMB_VARRUN_DIR "/var/run/smb" 66 #define SMB_CCACHE SMB_VARRUN_DIR "/ccache" 67 #define SMB_DBDIR "/var/smb" 68 69 extern void smb_netbios_name_reconfig(); 70 extern void smb_browser_config(); 71 72 static int smbd_daemonize_init(void); 73 static void smbd_daemonize_fini(int, int); 74 75 static int smbd_kernel_bind(void); 76 static void smbd_kernel_unbind(void); 77 static int smbd_already_running(void); 78 79 static void smbd_remove_ccache(void); 80 static int smbd_service_init(void); 81 static void smbd_service_fini(void); 82 83 static int smbd_setup_options(int argc, char *argv[]); 84 static void smbd_usage(FILE *fp); 85 static void smbd_report(const char *fmt, ...); 86 87 static void smbd_sig_handler(int sig); 88 89 static int smbd_localtime_init(void); 90 static void *smbd_localtime_monitor(void *arg); 91 92 extern time_t altzone; 93 94 static pthread_t localtime_thr; 95 96 static int smbd_refresh_init(void); 97 static void smbd_refresh_fini(void); 98 static void *smbd_refresh_monitor(void *); 99 static pthread_t refresh_thr; 100 static pthread_cond_t refresh_cond; 101 static pthread_mutex_t refresh_mutex; 102 103 static smbd_t smbd; 104 105 /* 106 * smbd user land daemon 107 * 108 * Use SMF error codes only on return or exit. 109 */ 110 int 111 main(int argc, char *argv[]) 112 { 113 struct sigaction act; 114 sigset_t set; 115 uid_t uid; 116 int pfd = -1; 117 118 smbd.s_pname = basename(argv[0]); 119 openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON); 120 121 if (smbd_setup_options(argc, argv) != 0) 122 return (SMF_EXIT_ERR_FATAL); 123 124 if ((uid = getuid()) != smbd.s_uid) { 125 smbd_report("user %d: %s", uid, strerror(EPERM)); 126 return (SMF_EXIT_ERR_FATAL); 127 } 128 129 if (getzoneid() != GLOBAL_ZONEID) { 130 smbd_report("non-global zones are not supported"); 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 (void) sigfillset(&set); 143 (void) sigdelset(&set, SIGABRT); 144 145 (void) sigfillset(&act.sa_mask); 146 act.sa_handler = smbd_sig_handler; 147 act.sa_flags = 0; 148 149 (void) sigaction(SIGTERM, &act, NULL); 150 (void) sigaction(SIGHUP, &act, NULL); 151 (void) sigaction(SIGINT, &act, NULL); 152 (void) sigaction(SIGPIPE, &act, NULL); 153 154 (void) sigdelset(&set, SIGTERM); 155 (void) sigdelset(&set, SIGHUP); 156 (void) sigdelset(&set, SIGINT); 157 (void) sigdelset(&set, SIGPIPE); 158 159 if (smbd.s_fg) { 160 (void) sigdelset(&set, SIGTSTP); 161 (void) sigdelset(&set, SIGTTIN); 162 (void) sigdelset(&set, SIGTTOU); 163 164 if (smbd_service_init() != 0) { 165 smbd_report("service initialization failed"); 166 exit(SMF_EXIT_ERR_FATAL); 167 } 168 } else { 169 /* 170 * "pfd" is a pipe descriptor -- any fatal errors 171 * during subsequent initialization of the child 172 * process should be written to this pipe and the 173 * parent will report this error as the exit status. 174 */ 175 pfd = smbd_daemonize_init(); 176 177 if (smbd_service_init() != 0) { 178 smbd_report("daemon initialization failed"); 179 exit(SMF_EXIT_ERR_FATAL); 180 } 181 182 smbd_daemonize_fini(pfd, SMF_EXIT_OK); 183 } 184 185 (void) atexit(smbd_service_fini); 186 187 while (!smbd.s_shutdown_flag) { 188 (void) sigsuspend(&set); 189 190 switch (smbd.s_sigval) { 191 case 0: 192 break; 193 194 case SIGPIPE: 195 break; 196 197 case SIGHUP: 198 /* Refresh config was triggered */ 199 if (smbd.s_fg) 200 smbd_report("reconfiguration requested"); 201 (void) pthread_cond_signal(&refresh_cond); 202 break; 203 204 default: 205 /* 206 * Typically SIGINT or SIGTERM. 207 */ 208 smbd.s_shutdown_flag = 1; 209 break; 210 } 211 212 smbd.s_sigval = 0; 213 } 214 215 smbd_service_fini(); 216 closelog(); 217 return (SMF_EXIT_OK); 218 } 219 220 /* 221 * This function will fork off a child process, 222 * from which only the child will return. 223 * 224 * Use SMF error codes only on exit. 225 */ 226 static int 227 smbd_daemonize_init(void) 228 { 229 int status, pfds[2]; 230 sigset_t set, oset; 231 pid_t pid; 232 int rc; 233 234 /* 235 * Reset privileges to the minimum set required. We continue 236 * to run as root to create and access files in /var. 237 */ 238 rc = __init_daemon_priv(PU_RESETGROUPS | PU_LIMITPRIVS, 239 smbd.s_uid, smbd.s_gid, 240 PRIV_NET_MAC_AWARE, PRIV_NET_PRIVADDR, PRIV_PROC_AUDIT, 241 PRIV_SYS_DEVICES, PRIV_SYS_SMB, NULL); 242 243 if (rc != 0) { 244 smbd_report("insufficient privileges"); 245 exit(SMF_EXIT_ERR_FATAL); 246 } 247 248 /* 249 * Block all signals prior to the fork and leave them blocked in the 250 * parent so we don't get in a situation where the parent gets SIGINT 251 * and returns non-zero exit status and the child is actually running. 252 * In the child, restore the signal mask once we've done our setsid(). 253 */ 254 (void) sigfillset(&set); 255 (void) sigdelset(&set, SIGABRT); 256 (void) sigprocmask(SIG_BLOCK, &set, &oset); 257 258 if (pipe(pfds) == -1) { 259 smbd_report("unable to create pipe"); 260 exit(SMF_EXIT_ERR_FATAL); 261 } 262 263 closelog(); 264 265 if ((pid = fork()) == -1) { 266 openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON); 267 smbd_report("unable to fork"); 268 closelog(); 269 exit(SMF_EXIT_ERR_FATAL); 270 } 271 272 /* 273 * If we're the parent process, wait for either the child to send us 274 * the appropriate exit status over the pipe or for the read to fail 275 * (presumably with 0 for EOF if our child terminated abnormally). 276 * If the read fails, exit with either the child's exit status if it 277 * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal. 278 */ 279 if (pid != 0) { 280 (void) close(pfds[1]); 281 282 if (read(pfds[0], &status, sizeof (status)) == sizeof (status)) 283 _exit(status); 284 285 if (waitpid(pid, &status, 0) == pid && WIFEXITED(status)) 286 _exit(WEXITSTATUS(status)); 287 288 _exit(SMF_EXIT_ERR_FATAL); 289 } 290 291 openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON); 292 smbd.s_pid = getpid(); 293 (void) setsid(); 294 (void) sigprocmask(SIG_SETMASK, &oset, NULL); 295 (void) chdir("/"); 296 (void) umask(022); 297 (void) close(pfds[0]); 298 299 return (pfds[1]); 300 } 301 302 static void 303 smbd_daemonize_fini(int fd, int exit_status) 304 { 305 /* 306 * Now that we're running, if a pipe fd was specified, write an exit 307 * status to it to indicate that our parent process can safely detach. 308 * Then proceed to loading the remaining non-built-in modules. 309 */ 310 if (fd >= 0) 311 (void) write(fd, &exit_status, sizeof (exit_status)); 312 313 (void) close(fd); 314 315 if ((fd = open("/dev/null", O_RDWR)) >= 0) { 316 (void) fcntl(fd, F_DUP2FD, STDIN_FILENO); 317 (void) fcntl(fd, F_DUP2FD, STDOUT_FILENO); 318 (void) fcntl(fd, F_DUP2FD, STDERR_FILENO); 319 (void) close(fd); 320 } 321 322 __fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION, 323 PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, NULL); 324 } 325 326 static int 327 smbd_service_init(void) 328 { 329 static char *dir[] = { 330 SMB_DBDIR, /* smbpasswd */ 331 SMB_VARRUN_DIR /* KRB credential cache */ 332 }; 333 334 int door_id; 335 int rc; 336 uint32_t mode; 337 char resource_domain[SMB_PI_MAX_DOMAIN]; 338 int i; 339 340 smbd.s_drv_fd = -1; 341 342 for (i = 0; i < (sizeof (dir)/sizeof (dir[0])); ++i) { 343 errno = 0; 344 345 if ((mkdir(dir[i], 0700) < 0) && (errno != EEXIST)) { 346 smbd_report("mkdir %s: %s", dir[i], strerror(errno)); 347 return (1); 348 } 349 } 350 351 /* 352 * Set KRB5CCNAME (for the SMB credential cache) in the environment. 353 */ 354 if (putenv("KRB5CCNAME=" SMB_CCACHE) != 0) { 355 smbd_report("unable to set KRB5CCNAME"); 356 return (1); 357 } 358 359 (void) oem_language_set("english"); 360 361 if (!nt_builtin_init()) { 362 smbd_report("out of memory"); 363 return (1); 364 } 365 366 (void) smb_nicmon_start(); 367 if (dns_msgid_init() != 0) { 368 smbd_report("DNS message id initialization failed"); 369 return (1); 370 } 371 372 smbrdr_init(); 373 374 if (smb_netbios_start() != 0) 375 smbd_report("NetBIOS services failed to start"); 376 else 377 smbd_report("NetBIOS services started"); 378 379 if (smb_netlogon_init() != 0) { 380 smbd_report("netlogon initialization failed"); 381 return (1); 382 } 383 384 (void) smb_getdomainname(resource_domain, SMB_PI_MAX_DOMAIN); 385 (void) utf8_strupr(resource_domain); 386 387 mode = smb_config_get_secmode(); 388 if (mode == SMB_SECMODE_DOMAIN) 389 (void) locate_resource_pdc(resource_domain); 390 391 /* Get the ID map client handle */ 392 if ((rc = smb_idmap_start()) != 0) { 393 smbd_report("no idmap handle"); 394 return (rc); 395 } 396 397 if ((rc = nt_domain_init(resource_domain, mode)) != 0) { 398 if (rc == SMB_DOMAIN_NOMACHINE_SID) { 399 smbd_report( 400 "no machine SID: check idmap configuration"); 401 return (rc); 402 } 403 } 404 405 ads_init(); 406 if ((rc = mlsvc_init()) != 0) { 407 smbd_report("msrpc initialization failed"); 408 return (rc); 409 } 410 411 if (smb_lmshrd_srv_start() != 0) { 412 smbd_report("share initialization failed"); 413 } 414 415 /* XXX following will get removed */ 416 (void) smb_doorsrv_start(); 417 if ((rc = smb_door_srv_start()) != 0) 418 return (rc); 419 420 if ((rc = smbd_refresh_init()) != 0) 421 return (rc); 422 423 /* Call dyndns update - Just in case its configured to refresh DNS */ 424 if (smb_config_getbool(SMB_CI_DYNDNS_ENABLE)) 425 (void) dyndns_update(); 426 427 if ((rc = smbd_kernel_bind()) != 0) 428 smbd_report("kernel bind error: %s", strerror(errno)); 429 430 (void) smbd_localtime_init(); 431 432 if ((door_id = smb_winpipe_doorsvc_start()) == -1) { 433 smbd_report("winpipe initialization failed %s", 434 strerror(errno)); 435 return (rc); 436 } else { 437 if (ioctl(smbd.s_drv_fd, SMB_IOC_WINPIPE, &door_id) < 0) 438 smbd_report("winpipe ioctl: %s", strerror(errno)); 439 } 440 441 (void) smb_lgrp_start(); 442 return (lmshare_start()); 443 } 444 445 static void 446 smbd_remove_ccache(void) 447 { 448 if ((remove(SMB_CCACHE) < 0) && (errno != ENOENT)) 449 smbd_report("failed to remove SMB ccache"); 450 } 451 452 /* 453 * Close the kernel service and shutdown smbd services. 454 * This function is registered with atexit(): ensure that anything 455 * called from here is safe to be called multiple times. 456 */ 457 static void 458 smbd_service_fini(void) 459 { 460 smb_winpipe_doorsvc_stop(); 461 nt_builtin_fini(); 462 smbd_refresh_fini(); 463 smbd_kernel_unbind(); 464 smb_door_srv_stop(); 465 smb_doorsrv_stop(); 466 smb_lmshrd_srv_stop(); 467 lmshare_stop(); 468 smb_nicmon_stop(); 469 smb_idmap_stop(); 470 smb_lgrp_stop(); 471 smbd_remove_ccache(); 472 473 } 474 475 /* 476 * smbd_refresh_init() 477 * 478 * SMB service refresh thread initialization. This thread waits for a 479 * refresh event and updates the daemon's view of the configuration 480 * before going back to sleep. 481 */ 482 static int 483 smbd_refresh_init() 484 { 485 pthread_attr_t tattr; 486 pthread_condattr_t cattr; 487 int rc; 488 489 (void) pthread_condattr_init(&cattr); 490 (void) pthread_cond_init(&refresh_cond, &cattr); 491 (void) pthread_condattr_destroy(&cattr); 492 493 (void) pthread_mutex_init(&refresh_mutex, NULL); 494 495 (void) pthread_attr_init(&tattr); 496 (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); 497 rc = pthread_create(&refresh_thr, &tattr, smbd_refresh_monitor, 0); 498 (void) pthread_attr_destroy(&tattr); 499 return (rc); 500 501 } 502 503 /* 504 * smbd_refresh_fini() 505 * 506 * Stop the refresh thread. 507 */ 508 static void 509 smbd_refresh_fini() 510 { 511 (void) pthread_cancel(refresh_thr); 512 513 (void) pthread_cond_destroy(&refresh_cond); 514 (void) pthread_mutex_destroy(&refresh_mutex); 515 } 516 517 /* 518 * smbd_refresh_monitor() 519 * 520 * Wait for a refresh event. When this thread wakes up, update the 521 * smbd configuration from the SMF config information then go back to 522 * wait for the next refresh. 523 */ 524 /*ARGSUSED*/ 525 static void * 526 smbd_refresh_monitor(void *arg) 527 { 528 int dummy = 0; 529 530 (void) pthread_mutex_lock(&refresh_mutex); 531 while (pthread_cond_wait(&refresh_cond, &refresh_mutex) == 0) { 532 /* 533 * We've been woken up by a refresh event so go do 534 * what is necessary. 535 */ 536 ads_refresh(); 537 smb_nic_build_info(); 538 (void) smb_netbios_name_reconfig(); 539 (void) smb_browser_config(); 540 smbd_remove_ccache(); 541 if (ioctl(smbd.s_drv_fd, SMB_IOC_CONFIG, &dummy) < 0) { 542 smbd_report("configuration update ioctl: %s", 543 strerror(errno)); 544 } 545 } 546 return (NULL); 547 } 548 549 550 /* 551 * If the door has already been opened by another process (non-zero pid 552 * in target), we assume that another smbd is already running. If there 553 * is a race here, it will be caught later when smbsrv is opened because 554 * only one process is allowed to open the device at a time. 555 */ 556 static int 557 smbd_already_running(void) 558 { 559 door_info_t info; 560 int door; 561 562 if ((door = open(SMBD_DOOR_NAME, O_RDONLY)) < 0) 563 return (0); 564 565 if (door_info(door, &info) < 0) 566 return (0); 567 568 if (info.di_target > 0) { 569 smbd_report("already running: pid %ld\n", info.di_target); 570 (void) close(door); 571 return (1); 572 } 573 574 (void) close(door); 575 return (0); 576 } 577 578 static int 579 smbd_kernel_bind(void) 580 { 581 if (smbd.s_drv_fd != -1) 582 (void) close(smbd.s_drv_fd); 583 584 if ((smbd.s_drv_fd = open(DRV_DEVICE_PATH, 0)) < 0) { 585 smbd.s_drv_fd = -1; 586 return (1); 587 } 588 return (0); 589 } 590 591 592 /* 593 * Initialization of the localtime thread. 594 * Returns 0 on success, an error number if thread creation fails. 595 */ 596 597 int 598 smbd_localtime_init(void) 599 { 600 pthread_attr_t tattr; 601 int rc; 602 603 (void) pthread_attr_init(&tattr); 604 (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); 605 rc = pthread_create(&localtime_thr, &tattr, smbd_localtime_monitor, 0); 606 (void) pthread_attr_destroy(&tattr); 607 return (rc); 608 } 609 610 /* 611 * Local time thread to kernel land. 612 * Send local gmtoff to kernel module one time at startup 613 * and each time it changes (up to twice a year). 614 * Local gmtoff is checked once every 15 minutes and 615 * since some timezones are aligned on half and qtr hour boundaries, 616 * once an hour would likely suffice. 617 */ 618 619 /*ARGSUSED*/ 620 static void * 621 smbd_localtime_monitor(void *arg) 622 { 623 struct tm local_tm; 624 time_t secs, gmtoff; 625 time_t last_gmtoff = -1; 626 int timeout; 627 628 for (;;) { 629 gmtoff = -altzone; 630 631 if ((last_gmtoff != gmtoff) && (smbd.s_drv_fd != -1)) { 632 if (ioctl(smbd.s_drv_fd, SMB_IOC_GMTOFF, &gmtoff) < 0) { 633 smbd_report("localtime ioctl: %s", 634 strerror(errno)); 635 } 636 } 637 638 /* 639 * Align the next iteration on a fifteen minute boundary. 640 */ 641 secs = time(0); 642 (void) localtime_r(&secs, &local_tm); 643 timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN); 644 (void) sleep(timeout); 645 646 last_gmtoff = gmtoff; 647 } 648 649 /*NOTREACHED*/ 650 return (NULL); 651 } 652 653 654 static void 655 smbd_kernel_unbind(void) 656 { 657 if (smbd.s_drv_fd != -1) { 658 (void) close(smbd.s_drv_fd); 659 smbd.s_drv_fd = -1; 660 } 661 } 662 663 static void 664 smbd_sig_handler(int sigval) 665 { 666 if (smbd.s_sigval == 0) 667 smbd.s_sigval = sigval; 668 } 669 670 /* 671 * Set up configuration options and parse the command line. 672 * This function will determine if we will run as a daemon 673 * or in the foreground. 674 * 675 * Failure to find a uid or gid results in using the default (0). 676 */ 677 static int 678 smbd_setup_options(int argc, char *argv[]) 679 { 680 struct passwd *pwd; 681 struct group *grp; 682 int c; 683 684 if ((pwd = getpwnam("root")) != NULL) 685 smbd.s_uid = pwd->pw_uid; 686 687 if ((grp = getgrnam("sys")) != NULL) 688 smbd.s_gid = grp->gr_gid; 689 690 smbd.s_fg = smb_config_get_fg_flag(); 691 692 while ((c = getopt(argc, argv, ":f")) != -1) { 693 switch (c) { 694 case 'f': 695 smbd.s_fg = 1; 696 break; 697 698 case ':': 699 case '?': 700 default: 701 smbd_usage(stderr); 702 return (-1); 703 } 704 } 705 706 return (0); 707 } 708 709 static void 710 smbd_usage(FILE *fp) 711 { 712 static char *help[] = { 713 "-f run program in foreground" 714 }; 715 716 int i; 717 718 (void) fprintf(fp, "Usage: %s [-f]\n", smbd.s_pname); 719 720 for (i = 0; i < sizeof (help)/sizeof (help[0]); ++i) 721 (void) fprintf(fp, " %s\n", help[i]); 722 } 723 724 static void 725 smbd_report(const char *fmt, ...) 726 { 727 char buf[128]; 728 va_list ap; 729 730 if (fmt == NULL) 731 return; 732 733 va_start(ap, fmt); 734 (void) vsnprintf(buf, 128, fmt, ap); 735 va_end(ap); 736 737 (void) fprintf(stderr, "smbd: %s\n", buf); 738 } 739 740 /* 741 * Enable libumem debugging by default on DEBUG builds. 742 */ 743 #ifdef DEBUG 744 const char * 745 _umem_debug_init(void) 746 { 747 return ("default,verbose"); /* $UMEM_DEBUG setting */ 748 } 749 750 const char * 751 _umem_logging_init(void) 752 { 753 return ("fail,contents"); /* $UMEM_LOGGING setting */ 754 } 755 #endif 756