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