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 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 22 * Copyright 2016 Nexenta Systems, Inc. All rights reserved. 23 */ 24 25 /* 26 * SMB/CIFS share cache implementation. 27 */ 28 29 #include <errno.h> 30 #include <synch.h> 31 #include <stdlib.h> 32 #include <strings.h> 33 #include <syslog.h> 34 #include <thread.h> 35 #include <pthread.h> 36 #include <assert.h> 37 #include <libshare.h> 38 #include <libzfs.h> 39 #include <priv_utils.h> 40 #include <sys/types.h> 41 #include <sys/wait.h> 42 #include <unistd.h> 43 #include <pwd.h> 44 #include <signal.h> 45 #include <dirent.h> 46 #include <dlfcn.h> 47 48 #include <smbsrv/libsmb.h> 49 #include <smbsrv/libsmbns.h> 50 #include <smbsrv/libmlsvc.h> 51 #include <smbsrv/smb_share.h> 52 #include <smbsrv/smb.h> 53 #include <mlsvc.h> 54 #include <dfs.h> 55 56 #define SMB_SHR_ERROR_THRESHOLD 3 57 #define SMB_SHR_CSC_BUFSZ 64 58 59 typedef struct smb_transient { 60 char *name; 61 char *cmnt; 62 char *path; 63 char drive; 64 boolean_t check; 65 } smb_transient_t; 66 67 static smb_transient_t tshare[] = { 68 { "IPC$", "Remote IPC", NULL, '\0', B_FALSE }, 69 { "c$", "Default Share", SMB_CVOL, 'C', B_FALSE }, 70 { "vss$", "VSS", SMB_VSS, 'V', B_TRUE } 71 }; 72 73 static struct { 74 char *value; 75 uint32_t flag; 76 } cscopt[] = { 77 { "disabled", SMB_SHRF_CSC_DISABLED }, 78 { "manual", SMB_SHRF_CSC_MANUAL }, 79 { "auto", SMB_SHRF_CSC_AUTO }, 80 { "vdo", SMB_SHRF_CSC_VDO } 81 }; 82 83 /* 84 * Cache functions and vars 85 */ 86 #define SMB_SHR_HTAB_SZ 1024 87 88 /* 89 * Cache handle 90 * 91 * Shares cache is a hash table. 92 * 93 * sc_cache pointer to hash table handle 94 * sc_cache_lck synchronize cache read/write accesses 95 * sc_state cache state machine values 96 * sc_nops number of inflight/pending cache operations 97 * sc_mtx protects handle fields 98 */ 99 typedef struct smb_shr_cache { 100 HT_HANDLE *sc_cache; 101 rwlock_t sc_cache_lck; 102 mutex_t sc_mtx; 103 cond_t sc_cv; 104 uint32_t sc_state; 105 uint32_t sc_nops; 106 } smb_shr_cache_t; 107 108 /* 109 * Cache states 110 */ 111 #define SMB_SHR_CACHE_STATE_NONE 0 112 #define SMB_SHR_CACHE_STATE_CREATED 1 113 #define SMB_SHR_CACHE_STATE_DESTROYING 2 114 115 /* 116 * Cache lock modes 117 */ 118 #define SMB_SHR_CACHE_RDLOCK 0 119 #define SMB_SHR_CACHE_WRLOCK 1 120 121 static smb_shr_cache_t smb_shr_cache; 122 123 static uint32_t smb_shr_cache_create(void); 124 static void smb_shr_cache_destroy(void); 125 static uint32_t smb_shr_cache_lock(int); 126 static void smb_shr_cache_unlock(void); 127 static int smb_shr_cache_count(void); 128 static smb_share_t *smb_shr_cache_iterate(smb_shriter_t *); 129 130 static smb_share_t *smb_shr_cache_findent(char *); 131 static uint32_t smb_shr_cache_addent(smb_share_t *); 132 static void smb_shr_cache_delent(char *); 133 static void smb_shr_cache_freent(HT_ITEM *); 134 135 static boolean_t smb_shr_is_empty(const char *); 136 static boolean_t smb_shr_is_dot_or_dotdot(const char *); 137 138 /* 139 * sharemgr functions 140 */ 141 static void smb_shr_sa_loadgrp(sa_group_t); 142 static uint32_t smb_shr_sa_load(sa_share_t, sa_resource_t); 143 static uint32_t smb_shr_sa_loadbyname(char *); 144 static uint32_t smb_shr_sa_get(sa_share_t, sa_resource_t, smb_share_t *); 145 146 /* 147 * .ZFS management functions 148 */ 149 static void smb_shr_zfs_add(smb_share_t *); 150 static void smb_shr_zfs_remove(smb_share_t *); 151 static void smb_shr_zfs_rename(smb_share_t *, smb_share_t *); 152 153 /* 154 * share publishing 155 */ 156 #define SMB_SHR_PUBLISH 0 157 #define SMB_SHR_UNPUBLISH 1 158 159 typedef struct smb_shr_pitem { 160 list_node_t spi_lnd; 161 char spi_name[MAXNAMELEN]; 162 char spi_container[MAXPATHLEN]; 163 char spi_op; 164 } smb_shr_pitem_t; 165 166 /* 167 * publish queue states 168 */ 169 #define SMB_SHR_PQS_NOQUEUE 0 170 #define SMB_SHR_PQS_READY 1 /* the queue is ready */ 171 #define SMB_SHR_PQS_PUBLISHING 2 /* publisher thread is running */ 172 #define SMB_SHR_PQS_STOPPING 3 173 174 /* 175 * share publishing queue 176 */ 177 typedef struct smb_shr_pqueue { 178 list_t spq_list; 179 mutex_t spq_mtx; 180 cond_t spq_cv; 181 uint32_t spq_state; 182 } smb_shr_pqueue_t; 183 184 static smb_shr_pqueue_t ad_queue; 185 186 static int smb_shr_publisher_start(void); 187 static void smb_shr_publisher_stop(void); 188 static void smb_shr_publisher_send(smb_ads_handle_t *, list_t *, const char *); 189 static void smb_shr_publisher_queue(const char *, const char *, char); 190 static void *smb_shr_publisher(void *); 191 static void smb_shr_publisher_flush(list_t *); 192 static void smb_shr_publish(const char *, const char *); 193 static void smb_shr_unpublish(const char *, const char *); 194 195 /* 196 * Utility/helper functions 197 */ 198 static uint32_t smb_shr_lookup(char *, smb_share_t *); 199 static uint32_t smb_shr_add_transient(char *, char *, char *); 200 static int smb_shr_enable_all_privs(void); 201 static int smb_shr_expand_subs(char **, smb_share_t *, smb_shr_execinfo_t *); 202 static char **smb_shr_tokenize_cmd(char *); 203 static void smb_shr_sig_abnormal_term(int); 204 static void smb_shr_sig_child(int); 205 static int smb_shr_encode(smb_share_t *, nvlist_t **); 206 207 /* 208 * libshare handle and synchronization 209 */ 210 typedef struct smb_sa_handle { 211 sa_handle_t sa_handle; 212 mutex_t sa_mtx; 213 boolean_t sa_in_service; 214 } smb_sa_handle_t; 215 216 static smb_sa_handle_t smb_sa_handle; 217 218 static char smb_shr_exec_map[MAXPATHLEN]; 219 static char smb_shr_exec_unmap[MAXPATHLEN]; 220 static mutex_t smb_shr_exec_mtx; 221 222 /* 223 * Semaphore held during temporary, process-wide changes 224 * such as process privileges. It is a seamaphore and 225 * not a mutex so a child of fork can reset it. 226 */ 227 static sema_t smb_proc_sem = DEFAULTSEMA; 228 229 /* 230 * Creates and initializes the cache and starts the publisher 231 * thread. 232 */ 233 int 234 smb_shr_start(void) 235 { 236 smb_transient_t *ts; 237 uint32_t nerr; 238 int i; 239 240 (void) mutex_lock(&smb_sa_handle.sa_mtx); 241 smb_sa_handle.sa_in_service = B_TRUE; 242 (void) mutex_unlock(&smb_sa_handle.sa_mtx); 243 244 if (smb_shr_cache_create() != NERR_Success) 245 return (ENOMEM); 246 247 for (i = 0; i < sizeof (tshare)/sizeof (tshare[0]); ++i) { 248 ts = &tshare[i]; 249 250 if (ts->check && smb_shr_is_empty(ts->path)) 251 continue; 252 253 nerr = smb_shr_add_transient(ts->name, ts->cmnt, ts->path); 254 if (nerr != NERR_Success) 255 return (ENOMEM); 256 } 257 258 return (smb_shr_publisher_start()); 259 } 260 261 void 262 smb_shr_stop(void) 263 { 264 smb_shr_cache_destroy(); 265 smb_shr_publisher_stop(); 266 267 (void) mutex_lock(&smb_sa_handle.sa_mtx); 268 smb_sa_handle.sa_in_service = B_FALSE; 269 270 if (smb_sa_handle.sa_handle != NULL) { 271 sa_fini(smb_sa_handle.sa_handle); 272 smb_sa_handle.sa_handle = NULL; 273 } 274 275 (void) mutex_unlock(&smb_sa_handle.sa_mtx); 276 } 277 278 /* 279 * Get a handle and exclusive access to the libshare API. 280 */ 281 sa_handle_t 282 smb_shr_sa_enter(void) 283 { 284 (void) mutex_lock(&smb_sa_handle.sa_mtx); 285 if (!smb_sa_handle.sa_in_service) { 286 (void) mutex_unlock(&smb_sa_handle.sa_mtx); 287 return (NULL); 288 } 289 290 if (smb_sa_handle.sa_handle != NULL && 291 sa_needs_refresh(smb_sa_handle.sa_handle)) { 292 sa_fini(smb_sa_handle.sa_handle); 293 smb_sa_handle.sa_handle = NULL; 294 } 295 296 if (smb_sa_handle.sa_handle == NULL) { 297 smb_sa_handle.sa_handle = sa_init(SA_INIT_SHARE_API); 298 if (smb_sa_handle.sa_handle == NULL) { 299 syslog(LOG_ERR, "share: failed to get libshare handle"); 300 (void) mutex_unlock(&smb_sa_handle.sa_mtx); 301 return (NULL); 302 } 303 } 304 305 return (smb_sa_handle.sa_handle); 306 } 307 308 /* 309 * Release exclusive access to the libshare API. 310 */ 311 void 312 smb_shr_sa_exit(void) 313 { 314 (void) mutex_unlock(&smb_sa_handle.sa_mtx); 315 } 316 317 /* 318 * Return the total number of shares 319 */ 320 int 321 smb_shr_count(void) 322 { 323 int n_shares = 0; 324 325 if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) { 326 n_shares = smb_shr_cache_count(); 327 smb_shr_cache_unlock(); 328 } 329 330 return (n_shares); 331 } 332 333 /* 334 * smb_shr_iterinit 335 * 336 * Initialize given iterator for traversing hash table. 337 */ 338 void 339 smb_shr_iterinit(smb_shriter_t *shi) 340 { 341 bzero(shi, sizeof (smb_shriter_t)); 342 shi->si_first = B_TRUE; 343 } 344 345 /* 346 * smb_shr_iterate 347 * 348 * Iterate on the shares in the hash table. The iterator must be initialized 349 * before the first iteration. On subsequent calls, the iterator must be 350 * passed unchanged. 351 * 352 * Returns NULL on failure or when all shares are visited, otherwise 353 * returns information of visited share. 354 */ 355 smb_share_t * 356 smb_shr_iterate(smb_shriter_t *shi) 357 { 358 smb_share_t *share = NULL; 359 smb_share_t *cached_si; 360 361 if (shi == NULL) 362 return (NULL); 363 364 if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) { 365 if ((cached_si = smb_shr_cache_iterate(shi)) != NULL) { 366 share = &shi->si_share; 367 bcopy(cached_si, share, sizeof (smb_share_t)); 368 } 369 smb_shr_cache_unlock(); 370 } 371 372 return (share); 373 } 374 375 /* 376 * Adds the given share to cache, publishes the share in ADS 377 * if it has an AD container, calls kernel to take a hold on 378 * the shared file system. If it can't take a hold on the 379 * shared file system, it's either because shared directory 380 * does not exist or some other error has occurred, in any 381 * case the share is removed from the cache. 382 * 383 * If the specified share is an autohome share which already 384 * exists in the cache, just increments the reference count. 385 */ 386 uint32_t 387 smb_shr_add(smb_share_t *si) 388 { 389 struct stat st; 390 smb_share_t *cached_si; 391 nvlist_t *shrlist; 392 boolean_t created_zfs = B_FALSE; 393 uint32_t status; 394 int rc; 395 396 assert(si != NULL); 397 398 if (smb_name_validate_share(si->shr_name) != ERROR_SUCCESS) 399 return (ERROR_INVALID_NAME); 400 401 if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success) 402 return (NERR_InternalError); 403 404 cached_si = smb_shr_cache_findent(si->shr_name); 405 if (cached_si) { 406 if (si->shr_flags & SMB_SHRF_AUTOHOME) { 407 cached_si->shr_refcnt++; 408 status = NERR_Success; 409 } else { 410 status = NERR_DuplicateShare; 411 } 412 smb_shr_cache_unlock(); 413 return (status); 414 } 415 416 if (STYPE_ISDSK(si->shr_type)) { 417 /* 418 * If share type is STYPE_DISKTREE then the path to the 419 * share should exist so that we can add the share to cache. 420 * If path is ZFS, add the .zfs/shares/<share> entry. 421 * 422 * Both actions may require privileges that main dropped, 423 * so we need to temporarily make those effective. 424 */ 425 if (smb_proc_takesem() == 0) { 426 427 (void) priv_set(PRIV_ON, PRIV_EFFECTIVE, 428 PRIV_FILE_DAC_READ, 429 PRIV_FILE_DAC_SEARCH, 430 PRIV_FILE_DAC_WRITE, 431 NULL); 432 433 rc = stat(si->shr_path, &st); 434 if (rc == 0) { 435 smb_shr_zfs_add(si); 436 created_zfs = B_TRUE; 437 } 438 439 (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, 440 PRIV_FILE_DAC_READ, 441 PRIV_FILE_DAC_SEARCH, 442 PRIV_FILE_DAC_WRITE, 443 NULL); 444 smb_proc_givesem(); 445 } else { 446 rc = NERR_InternalError; 447 } 448 if (rc != 0) { 449 smb_shr_cache_unlock(); 450 return (NERR_ItemNotFound); 451 } 452 } 453 454 if ((status = smb_shr_cache_addent(si)) != NERR_Success) { 455 /* This error should be impossible after findent above. */ 456 smb_shr_cache_unlock(); 457 return (status); 458 } 459 460 /* don't hold the lock across door call */ 461 smb_shr_cache_unlock(); 462 463 if ((rc = smb_shr_encode(si, &shrlist)) == 0) { 464 /* send the share to kernel */ 465 rc = smb_kmod_share(shrlist); 466 nvlist_free(shrlist); 467 468 if (rc == 0) { 469 smb_shr_publish(si->shr_name, si->shr_container); 470 471 if ((si->shr_flags & SMB_SHRF_DFSROOT) != 0) 472 dfs_namespace_load(si->shr_name); 473 474 return (NERR_Success); 475 } 476 } 477 478 /* 479 * Error code path, i.e. when the kernel could not accept 480 * the new share for some reason. 481 */ 482 if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) == NERR_Success) { 483 smb_shr_cache_delent(si->shr_name); 484 smb_shr_cache_unlock(); 485 } 486 487 if (created_zfs && smb_proc_takesem() == 0) { 488 489 (void) priv_set(PRIV_ON, PRIV_EFFECTIVE, 490 PRIV_FILE_DAC_READ, 491 PRIV_FILE_DAC_SEARCH, 492 PRIV_FILE_DAC_WRITE, 493 NULL); 494 495 smb_shr_zfs_remove(si); 496 497 (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, 498 PRIV_FILE_DAC_READ, 499 PRIV_FILE_DAC_SEARCH, 500 PRIV_FILE_DAC_WRITE, 501 NULL); 502 503 smb_proc_givesem(); 504 } 505 506 /* 507 * rc == ENOENT means the shared directory doesn't exist 508 */ 509 return ((rc == ENOENT) ? NERR_UnknownDevDir : NERR_InternalError); 510 } 511 512 /* 513 * Removes the specified share from cache, removes it from AD 514 * if it has an AD container, and calls the kernel to release 515 * the hold on the shared file system. 516 * 517 * If this is an autohome share then decrement the reference 518 * count. If it reaches 0 then it proceeds with removing steps. 519 */ 520 uint32_t 521 smb_shr_remove(char *sharename) 522 { 523 smb_share_t *si; 524 char container[MAXPATHLEN]; 525 boolean_t dfsroot; 526 nvlist_t *shrlist; 527 528 assert(sharename != NULL); 529 530 if (smb_name_validate_share(sharename) != ERROR_SUCCESS) 531 return (ERROR_INVALID_NAME); 532 533 if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success) 534 return (NERR_InternalError); 535 536 if ((si = smb_shr_cache_findent(sharename)) == NULL) { 537 smb_shr_cache_unlock(); 538 return (NERR_NetNameNotFound); 539 } 540 541 if (STYPE_ISIPC(si->shr_type)) { 542 /* IPC$ share cannot be removed */ 543 smb_shr_cache_unlock(); 544 return (ERROR_ACCESS_DENIED); 545 } 546 547 if (si->shr_flags & SMB_SHRF_AUTOHOME) { 548 if ((--si->shr_refcnt) > 0) { 549 smb_shr_cache_unlock(); 550 return (NERR_Success); 551 } 552 } 553 554 /* 555 * If path is ZFS, remove the .zfs/shares/<share> entry. Need 556 * to remove before cleanup of cache occurs. These actions 557 * require temporary elevation of privileges. 558 */ 559 if (smb_proc_takesem() == 0) { 560 561 (void) priv_set(PRIV_ON, PRIV_EFFECTIVE, 562 PRIV_FILE_DAC_READ, 563 PRIV_FILE_DAC_SEARCH, 564 PRIV_FILE_DAC_WRITE, 565 NULL); 566 567 smb_shr_zfs_remove(si); 568 569 (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, 570 PRIV_FILE_DAC_READ, 571 PRIV_FILE_DAC_SEARCH, 572 PRIV_FILE_DAC_WRITE, 573 NULL); 574 575 smb_proc_givesem(); 576 } 577 578 (void) smb_shr_encode(si, &shrlist); 579 580 (void) strlcpy(container, si->shr_container, sizeof (container)); 581 dfsroot = ((si->shr_flags & SMB_SHRF_DFSROOT) != 0); 582 smb_shr_cache_delent(sharename); 583 smb_shr_cache_unlock(); 584 585 smb_shr_unpublish(sharename, container); 586 587 /* call kernel to release the hold on the shared file system */ 588 if (shrlist != NULL) { 589 (void) smb_kmod_unshare(shrlist); 590 nvlist_free(shrlist); 591 } 592 593 if (dfsroot) 594 dfs_namespace_unload(sharename); 595 596 return (NERR_Success); 597 } 598 599 /* 600 * Rename a share. Check that the current name exists and the new name 601 * doesn't exist. The rename is performed by deleting the current share 602 * definition and creating a new share with the new name. 603 */ 604 uint32_t 605 smb_shr_rename(char *from_name, char *to_name) 606 { 607 smb_share_t *from_si; 608 smb_share_t to_si; 609 uint32_t status; 610 nvlist_t *shrlist; 611 612 assert((from_name != NULL) && (to_name != NULL)); 613 614 if (smb_name_validate_share(from_name) != ERROR_SUCCESS || 615 smb_name_validate_share(to_name) != ERROR_SUCCESS) 616 return (ERROR_INVALID_NAME); 617 618 if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success) 619 return (NERR_InternalError); 620 621 if ((from_si = smb_shr_cache_findent(from_name)) == NULL) { 622 smb_shr_cache_unlock(); 623 return (NERR_NetNameNotFound); 624 } 625 626 if (STYPE_ISIPC(from_si->shr_type)) { 627 /* IPC$ share cannot be renamed */ 628 smb_shr_cache_unlock(); 629 return (ERROR_ACCESS_DENIED); 630 } 631 632 if (smb_shr_cache_findent(to_name) != NULL) { 633 smb_shr_cache_unlock(); 634 return (NERR_DuplicateShare); 635 } 636 637 bcopy(from_si, &to_si, sizeof (smb_share_t)); 638 (void) strlcpy(to_si.shr_name, to_name, sizeof (to_si.shr_name)); 639 640 /* If path is ZFS, rename the .zfs/shares/<share> entry. */ 641 if (smb_proc_takesem() == 0) { 642 643 (void) priv_set(PRIV_ON, PRIV_EFFECTIVE, 644 PRIV_FILE_DAC_READ, 645 PRIV_FILE_DAC_SEARCH, 646 PRIV_FILE_DAC_WRITE, 647 NULL); 648 649 smb_shr_zfs_rename(from_si, &to_si); 650 651 (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, 652 PRIV_FILE_DAC_READ, 653 PRIV_FILE_DAC_SEARCH, 654 PRIV_FILE_DAC_WRITE, 655 NULL); 656 657 smb_proc_givesem(); 658 } 659 660 if ((status = smb_shr_cache_addent(&to_si)) != NERR_Success) { 661 smb_shr_cache_unlock(); 662 return (status); 663 } 664 665 smb_shr_cache_delent(from_name); 666 smb_shr_cache_unlock(); 667 668 if (smb_shr_encode(from_si, &shrlist) == 0) { 669 (void) smb_kmod_unshare(shrlist); 670 nvlist_free(shrlist); 671 672 if (smb_shr_encode(&to_si, &shrlist) == 0) { 673 (void) smb_kmod_share(shrlist); 674 nvlist_free(shrlist); 675 } 676 } 677 678 smb_shr_unpublish(from_name, to_si.shr_container); 679 smb_shr_publish(to_name, to_si.shr_container); 680 681 return (NERR_Success); 682 } 683 684 /* 685 * Load the information for the specified share into the supplied share 686 * info structure. 687 * 688 * First looks up the cache to see if the specified share exists, if there 689 * is a miss then it looks up sharemgr. 690 */ 691 uint32_t 692 smb_shr_get(char *sharename, smb_share_t *si) 693 { 694 uint32_t status; 695 696 if (sharename == NULL || *sharename == '\0') 697 return (NERR_NetNameNotFound); 698 699 if ((status = smb_shr_lookup(sharename, si)) == NERR_Success) 700 return (status); 701 702 if ((status = smb_shr_sa_loadbyname(sharename)) == NERR_Success) 703 status = smb_shr_lookup(sharename, si); 704 705 return (status); 706 } 707 708 /* 709 * Modifies an existing share. Properties that can be modified are: 710 * 711 * o comment 712 * o AD container 713 * o host access 714 * o flags 715 */ 716 uint32_t 717 smb_shr_modify(smb_share_t *new_si) 718 { 719 smb_share_t old_si; 720 smb_share_t *si; 721 boolean_t adc_changed = B_FALSE; 722 boolean_t quota_flag_changed = B_FALSE; 723 uint32_t access, flag; 724 nvlist_t *shrlist; 725 726 assert(new_si != NULL); 727 728 if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success) 729 return (NERR_InternalError); 730 731 if ((si = smb_shr_cache_findent(new_si->shr_name)) == NULL) { 732 smb_shr_cache_unlock(); 733 return (NERR_NetNameNotFound); 734 } 735 736 if (STYPE_ISIPC(si->shr_type)) { 737 /* IPC$ share cannot be modified */ 738 smb_shr_cache_unlock(); 739 return (ERROR_ACCESS_DENIED); 740 } 741 742 /* 743 * Keep a copy of what the share entry looks like before we 744 * modify it. We need this for things like unpublishing 745 * from the old share container, removing the quota dir. 746 */ 747 bcopy(si, &old_si, sizeof (old_si)); 748 749 /* Share comment */ 750 (void) strlcpy(si->shr_cmnt, new_si->shr_cmnt, sizeof (si->shr_cmnt)); 751 752 /* Container */ 753 (void) strlcpy(si->shr_container, new_si->shr_container, 754 sizeof (si->shr_container)); 755 adc_changed = (strcmp(old_si.shr_container, si->shr_container) != 0); 756 757 flag = (new_si->shr_flags & SMB_SHRF_ABE); 758 si->shr_flags &= ~SMB_SHRF_ABE; 759 si->shr_flags |= flag; 760 761 flag = (new_si->shr_flags & SMB_SHRF_CATIA); 762 si->shr_flags &= ~SMB_SHRF_CATIA; 763 si->shr_flags |= flag; 764 765 flag = (new_si->shr_flags & SMB_SHRF_GUEST_OK); 766 si->shr_flags &= ~SMB_SHRF_GUEST_OK; 767 si->shr_flags |= flag; 768 769 flag = (new_si->shr_flags & SMB_SHRF_DFSROOT); 770 si->shr_flags &= ~SMB_SHRF_DFSROOT; 771 si->shr_flags |= flag; 772 773 flag = (new_si->shr_flags & SMB_SHRF_QUOTAS); 774 si->shr_flags &= ~SMB_SHRF_QUOTAS; 775 si->shr_flags |= flag; 776 if ((old_si.shr_flags ^ si->shr_flags) & SMB_SHRF_QUOTAS) 777 quota_flag_changed = B_TRUE; 778 779 flag = (new_si->shr_flags & SMB_SHRF_CSC_MASK); 780 si->shr_flags &= ~SMB_SHRF_CSC_MASK; 781 si->shr_flags |= flag; 782 783 access = (new_si->shr_flags & SMB_SHRF_ACC_ALL); 784 si->shr_flags &= ~SMB_SHRF_ACC_ALL; 785 si->shr_flags |= access; 786 787 if (access & SMB_SHRF_ACC_NONE) 788 (void) strlcpy(si->shr_access_none, new_si->shr_access_none, 789 sizeof (si->shr_access_none)); 790 791 if (access & SMB_SHRF_ACC_RO) 792 (void) strlcpy(si->shr_access_ro, new_si->shr_access_ro, 793 sizeof (si->shr_access_ro)); 794 795 if (access & SMB_SHRF_ACC_RW) 796 (void) strlcpy(si->shr_access_rw, new_si->shr_access_rw, 797 sizeof (si->shr_access_rw)); 798 799 smb_shr_cache_unlock(); 800 801 if (smb_shr_encode(si, &shrlist) == 0) { 802 (void) smb_kmod_unshare(shrlist); 803 nvlist_free(shrlist); 804 805 if (smb_shr_encode(new_si, &shrlist) == 0) { 806 (void) smb_kmod_share(shrlist); 807 nvlist_free(shrlist); 808 } 809 } 810 811 if (adc_changed) { 812 smb_shr_unpublish(old_si.shr_name, old_si.shr_container); 813 smb_shr_publish(new_si->shr_name, new_si->shr_container); 814 } 815 816 /* The following required privileges we dropped. */ 817 if (quota_flag_changed && smb_proc_takesem() == 0) { 818 819 (void) priv_set(PRIV_ON, PRIV_EFFECTIVE, 820 PRIV_FILE_DAC_READ, 821 PRIV_FILE_DAC_SEARCH, 822 PRIV_FILE_DAC_WRITE, 823 NULL); 824 825 smb_shr_zfs_remove(&old_si); 826 smb_shr_zfs_add(si); 827 828 (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, 829 PRIV_FILE_DAC_READ, 830 PRIV_FILE_DAC_SEARCH, 831 PRIV_FILE_DAC_WRITE, 832 NULL); 833 834 smb_proc_givesem(); 835 } 836 837 return (NERR_Success); 838 } 839 840 /* 841 * smb_shr_exists 842 * 843 * Returns B_TRUE if the share exists. Otherwise returns B_FALSE 844 */ 845 boolean_t 846 smb_shr_exists(char *sharename) 847 { 848 boolean_t exists = B_FALSE; 849 850 if (sharename == NULL || *sharename == '\0') 851 return (B_FALSE); 852 853 if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) { 854 exists = (smb_shr_cache_findent(sharename) != NULL); 855 smb_shr_cache_unlock(); 856 } 857 858 return (exists); 859 } 860 861 /* 862 * If the shared directory does not begin with a /, one will be 863 * inserted as a prefix. If ipaddr is not zero, then also return 864 * information about access based on the host level access lists, if 865 * present. Also return access check if there is an IP address and 866 * shr_accflags. 867 * 868 * The value of smb_chk_hostaccess is checked for an access match. 869 * -1 is wildcard match 870 * 0 is no match 871 * 1 is match 872 * 873 * Precedence is none is checked first followed by ro then rw if 874 * needed. If x is wildcard (< 0) then check to see if the other 875 * values are a match. If a match, that wins. 876 */ 877 uint32_t 878 smb_shr_hostaccess(smb_inaddr_t *ipaddr, char *none_list, char *ro_list, 879 char *rw_list, uint32_t flag) 880 { 881 uint32_t acc = SMB_SHRF_ACC_NONE; 882 int none = 0; 883 int ro = 0; 884 int rw = 0; 885 886 if (!smb_inet_iszero(ipaddr)) { 887 if ((flag & SMB_SHRF_ACC_NONE) != 0) 888 none = smb_chk_hostaccess(ipaddr, none_list); 889 if ((flag & SMB_SHRF_ACC_RO) != 0) 890 ro = smb_chk_hostaccess(ipaddr, ro_list); 891 if ((flag & SMB_SHRF_ACC_RW) != 0) 892 rw = smb_chk_hostaccess(ipaddr, rw_list); 893 894 /* make first pass to get basic value */ 895 if (none != 0) 896 acc = SMB_SHRF_ACC_NONE; 897 else if (ro != 0) 898 acc = SMB_SHRF_ACC_RO; 899 else if (rw != 0) 900 acc = SMB_SHRF_ACC_RW; 901 902 /* make second pass to handle '*' case */ 903 if (none < 0) { 904 acc = SMB_SHRF_ACC_NONE; 905 if (ro > 0) 906 acc = SMB_SHRF_ACC_RO; 907 else if (rw > 0) 908 acc = SMB_SHRF_ACC_RW; 909 } else if (ro < 0) { 910 acc = SMB_SHRF_ACC_RO; 911 if (none > 0) 912 acc = SMB_SHRF_ACC_NONE; 913 else if (rw > 0) 914 acc = SMB_SHRF_ACC_RW; 915 } else if (rw < 0) { 916 acc = SMB_SHRF_ACC_RW; 917 if (none > 0) 918 acc = SMB_SHRF_ACC_NONE; 919 else if (ro > 0) 920 acc = SMB_SHRF_ACC_RO; 921 } 922 } 923 924 return (acc); 925 } 926 927 /* 928 * smb_shr_is_special 929 * 930 * Special share reserved for interprocess communication (IPC$) or 931 * remote administration of the server (ADMIN$). Can also refer to 932 * administrative shares such as C$, D$, E$, and so forth. 933 */ 934 int 935 smb_shr_is_special(char *sharename) 936 { 937 int len; 938 939 if (sharename == NULL) 940 return (0); 941 942 if ((len = strlen(sharename)) == 0) 943 return (0); 944 945 if (sharename[len - 1] == '$') 946 return (STYPE_SPECIAL); 947 948 return (0); 949 } 950 951 /* 952 * smb_shr_is_restricted 953 * 954 * Check whether or not there is a restriction on a share. Restricted 955 * shares are generally STYPE_SPECIAL, for example, IPC$. All the 956 * administration share names are restricted: C$, D$ etc. Returns B_TRUE 957 * if the share is restricted. Otherwise B_FALSE is returned to indicate 958 * that there are no restrictions. 959 */ 960 boolean_t 961 smb_shr_is_restricted(char *sharename) 962 { 963 static char *restricted[] = { 964 "IPC$" 965 }; 966 967 int i; 968 969 if (sharename == NULL) 970 return (B_FALSE); 971 972 for (i = 0; i < sizeof (restricted)/sizeof (restricted[0]); i++) { 973 if (smb_strcasecmp(restricted[i], sharename, 0) == 0) 974 return (B_TRUE); 975 } 976 977 return (smb_shr_is_admin(sharename)); 978 } 979 980 /* 981 * smb_shr_is_admin 982 * 983 * Check whether or not access to the share should be restricted to 984 * administrators. This is a bit of a hack because what we're doing 985 * is checking for the default admin shares: C$, D$ etc.. There are 986 * other shares that have restrictions: see smb_shr_is_restricted(). 987 * 988 * Returns B_TRUE if the shares is an admin share. Otherwise B_FALSE 989 * is returned to indicate that there are no restrictions. 990 */ 991 boolean_t 992 smb_shr_is_admin(char *sharename) 993 { 994 if (sharename == NULL) 995 return (B_FALSE); 996 997 if (strlen(sharename) == 2 && 998 smb_isalpha(sharename[0]) && sharename[1] == '$') { 999 return (B_TRUE); 1000 } 1001 1002 return (B_FALSE); 1003 } 1004 1005 char 1006 smb_shr_drive_letter(const char *path) 1007 { 1008 smb_transient_t *ts; 1009 int i; 1010 1011 if (path == NULL) 1012 return ('\0'); 1013 1014 for (i = 0; i < sizeof (tshare)/sizeof (tshare[0]); ++i) { 1015 ts = &tshare[i]; 1016 1017 if (ts->path == NULL) 1018 continue; 1019 1020 if (strcasecmp(ts->path, path) == 0) 1021 return (ts->drive); 1022 } 1023 1024 return ('\0'); 1025 } 1026 1027 /* 1028 * Returns true if the specified directory is empty, 1029 * otherwise returns false. 1030 */ 1031 static boolean_t 1032 smb_shr_is_empty(const char *path) 1033 { 1034 DIR *dirp; 1035 struct dirent *dp; 1036 1037 if (path == NULL) 1038 return (B_TRUE); 1039 1040 if ((dirp = opendir(path)) == NULL) 1041 return (B_TRUE); 1042 1043 while ((dp = readdir(dirp)) != NULL) { 1044 if (!smb_shr_is_dot_or_dotdot(dp->d_name)) 1045 return (B_FALSE); 1046 } 1047 1048 (void) closedir(dirp); 1049 return (B_TRUE); 1050 } 1051 1052 /* 1053 * Returns true if name is "." or "..", otherwise returns false. 1054 */ 1055 static boolean_t 1056 smb_shr_is_dot_or_dotdot(const char *name) 1057 { 1058 if (*name != '.') 1059 return (B_FALSE); 1060 1061 if ((name[1] == '\0') || (name[1] == '.' && name[2] == '\0')) 1062 return (B_TRUE); 1063 1064 return (B_FALSE); 1065 } 1066 1067 /* 1068 * smb_shr_get_realpath 1069 * 1070 * Derive the real path for a share from the path provided by a client. 1071 * For instance, the real path of C:\ may be /cvol or the real path of 1072 * F:\home may be /vol1/home. 1073 * 1074 * clntpath - path provided by the Windows client is in the 1075 * format of <drive letter>:\<dir> 1076 * realpath - path that will be stored as the directory field of 1077 * the smb_share_t structure of the share. 1078 * maxlen - maximum length of the realpath buffer 1079 * 1080 * Return LAN Manager network error code. 1081 */ 1082 uint32_t 1083 smb_shr_get_realpath(const char *clntpath, char *realpath, int maxlen) 1084 { 1085 const char *p; 1086 int len; 1087 1088 if ((p = strchr(clntpath, ':')) != NULL) 1089 ++p; 1090 else 1091 p = clntpath; 1092 1093 (void) strlcpy(realpath, p, maxlen); 1094 (void) strcanon(realpath, "/\\"); 1095 (void) strsubst(realpath, '\\', '/'); 1096 1097 len = strlen(realpath); 1098 if ((len > 1) && (realpath[len - 1] == '/')) 1099 realpath[len - 1] = '\0'; 1100 1101 return (NERR_Success); 1102 } 1103 1104 void 1105 smb_shr_list(int offset, smb_shrlist_t *list) 1106 { 1107 smb_shriter_t iterator; 1108 smb_share_t *si; 1109 int n = 0; 1110 1111 bzero(list, sizeof (smb_shrlist_t)); 1112 smb_shr_iterinit(&iterator); 1113 1114 while ((si = smb_shr_iterate(&iterator)) != NULL) { 1115 if (--offset > 0) 1116 continue; 1117 1118 if ((si->shr_flags & SMB_SHRF_TRANS) && 1119 (!STYPE_ISIPC(si->shr_type))) { 1120 bcopy(si, &list->sl_shares[n], sizeof (smb_share_t)); 1121 if (++n == LMSHARES_PER_REQUEST) 1122 break; 1123 } 1124 } 1125 1126 list->sl_cnt = n; 1127 } 1128 1129 /* 1130 * Executes the map/unmap command associated with a share. 1131 * 1132 * Returns 0 on success. Otherwise non-zero for errors. 1133 */ 1134 int 1135 smb_shr_exec(smb_shr_execinfo_t *subs) 1136 { 1137 char cmd[MAXPATHLEN], **cmd_tokens, *path, *ptr; 1138 pid_t child_pid; 1139 int child_status; 1140 struct sigaction pact, cact; 1141 smb_share_t si; 1142 1143 if (smb_shr_get(subs->e_sharename, &si) != 0) 1144 return (-1); 1145 1146 *cmd = '\0'; 1147 1148 (void) mutex_lock(&smb_shr_exec_mtx); 1149 1150 switch (subs->e_type) { 1151 case SMB_EXEC_MAP: 1152 (void) strlcpy(cmd, smb_shr_exec_map, sizeof (cmd)); 1153 break; 1154 case SMB_EXEC_UNMAP: 1155 (void) strlcpy(cmd, smb_shr_exec_unmap, sizeof (cmd)); 1156 break; 1157 default: 1158 (void) mutex_unlock(&smb_shr_exec_mtx); 1159 return (-1); 1160 } 1161 1162 (void) mutex_unlock(&smb_shr_exec_mtx); 1163 1164 if (*cmd == '\0') 1165 return (0); 1166 1167 if (smb_proc_takesem() != 0) 1168 return (-1); 1169 1170 pact.sa_handler = smb_shr_sig_child; 1171 pact.sa_flags = 0; 1172 (void) sigemptyset(&pact.sa_mask); 1173 sigaction(SIGCHLD, &pact, NULL); 1174 1175 (void) priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_FORK, NULL); 1176 1177 if ((child_pid = fork()) == -1) { 1178 (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_PROC_FORK, NULL); 1179 smb_proc_givesem(); 1180 return (-1); 1181 } 1182 1183 if (child_pid == 0) { 1184 1185 /* child process */ 1186 1187 cact.sa_handler = smb_shr_sig_abnormal_term; 1188 cact.sa_flags = 0; 1189 (void) sigemptyset(&cact.sa_mask); 1190 sigaction(SIGTERM, &cact, NULL); 1191 sigaction(SIGABRT, &cact, NULL); 1192 sigaction(SIGSEGV, &cact, NULL); 1193 1194 if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_EXEC, 1195 PRIV_FILE_DAC_EXECUTE, NULL)) 1196 _exit(-1); 1197 1198 if (smb_shr_enable_all_privs()) 1199 _exit(-1); 1200 1201 smb_proc_initsem(); 1202 1203 (void) trim_whitespace(cmd); 1204 (void) strcanon(cmd, " "); 1205 1206 if ((cmd_tokens = smb_shr_tokenize_cmd(cmd)) != NULL) { 1207 1208 if (smb_shr_expand_subs(cmd_tokens, &si, subs) != 0) { 1209 free(cmd_tokens[0]); 1210 free(cmd_tokens); 1211 _exit(-1); 1212 } 1213 1214 ptr = cmd; 1215 path = strsep(&ptr, " "); 1216 1217 (void) execv(path, cmd_tokens); 1218 } 1219 1220 _exit(-1); 1221 } 1222 1223 (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_PROC_FORK, NULL); 1224 smb_proc_givesem(); 1225 1226 /* parent process */ 1227 1228 while (waitpid(child_pid, &child_status, 0) < 0) { 1229 if (errno != EINTR) 1230 break; 1231 1232 /* continue if waitpid got interrupted by a signal */ 1233 errno = 0; 1234 continue; 1235 } 1236 1237 if (WIFEXITED(child_status)) 1238 return (WEXITSTATUS(child_status)); 1239 1240 return (child_status); 1241 } 1242 1243 /* 1244 * Locking for process-wide settings (i.e. privileges) 1245 */ 1246 void 1247 smb_proc_initsem(void) 1248 { 1249 (void) sema_init(&smb_proc_sem, 1, USYNC_THREAD, NULL); 1250 } 1251 1252 int 1253 smb_proc_takesem(void) 1254 { 1255 return (sema_wait(&smb_proc_sem)); 1256 } 1257 1258 void 1259 smb_proc_givesem(void) 1260 { 1261 (void) sema_post(&smb_proc_sem); 1262 } 1263 1264 /* 1265 * ============================================ 1266 * Private helper/utility functions 1267 * ============================================ 1268 */ 1269 1270 /* 1271 * Looks up the given share in the cache and return 1272 * the info in 'si' 1273 */ 1274 static uint32_t 1275 smb_shr_lookup(char *sharename, smb_share_t *si) 1276 { 1277 smb_share_t *cached_si; 1278 uint32_t status = NERR_NetNameNotFound; 1279 1280 if (sharename == NULL || *sharename == '\0') 1281 return (NERR_NetNameNotFound); 1282 if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) { 1283 cached_si = smb_shr_cache_findent(sharename); 1284 if (cached_si != NULL) { 1285 bcopy(cached_si, si, sizeof (smb_share_t)); 1286 status = NERR_Success; 1287 } 1288 1289 smb_shr_cache_unlock(); 1290 } 1291 return (status); 1292 } 1293 1294 /* 1295 * Add IPC$ or Admin shares to the cache upon startup. 1296 */ 1297 static uint32_t 1298 smb_shr_add_transient(char *name, char *cmnt, char *path) 1299 { 1300 smb_share_t trans; 1301 uint32_t status = NERR_InternalError; 1302 1303 if (name == NULL) 1304 return (status); 1305 1306 bzero(&trans, sizeof (smb_share_t)); 1307 (void) strlcpy(trans.shr_name, name, MAXNAMELEN); 1308 if (cmnt) 1309 (void) strlcpy(trans.shr_cmnt, cmnt, SMB_SHARE_CMNT_MAX); 1310 1311 if (path) 1312 (void) strlcpy(trans.shr_path, path, MAXPATHLEN); 1313 1314 if (strcasecmp(name, "IPC$") == 0) 1315 trans.shr_type = STYPE_IPC; 1316 1317 trans.shr_flags = SMB_SHRF_TRANS; 1318 1319 if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) == NERR_Success) { 1320 status = smb_shr_cache_addent(&trans); 1321 smb_shr_cache_unlock(); 1322 } 1323 1324 return (status); 1325 } 1326 1327 /* 1328 * ============================================ 1329 * Cache management functions 1330 * 1331 * All cache functions are private 1332 * ============================================ 1333 */ 1334 1335 /* 1336 * Create the share cache (hash table). 1337 */ 1338 static uint32_t 1339 smb_shr_cache_create(void) 1340 { 1341 uint32_t status = NERR_Success; 1342 1343 (void) mutex_lock(&smb_shr_cache.sc_mtx); 1344 switch (smb_shr_cache.sc_state) { 1345 case SMB_SHR_CACHE_STATE_NONE: 1346 smb_shr_cache.sc_cache = ht_create_table(SMB_SHR_HTAB_SZ, 1347 MAXNAMELEN, 0); 1348 if (smb_shr_cache.sc_cache == NULL) { 1349 status = NERR_InternalError; 1350 break; 1351 } 1352 1353 (void) ht_register_callback(smb_shr_cache.sc_cache, 1354 smb_shr_cache_freent); 1355 smb_shr_cache.sc_nops = 0; 1356 smb_shr_cache.sc_state = SMB_SHR_CACHE_STATE_CREATED; 1357 break; 1358 1359 default: 1360 assert(0); 1361 status = NERR_InternalError; 1362 break; 1363 } 1364 (void) mutex_unlock(&smb_shr_cache.sc_mtx); 1365 1366 return (status); 1367 } 1368 1369 /* 1370 * Destroy the share cache (hash table). 1371 * Wait for inflight/pending operations to finish or abort before 1372 * destroying the cache. 1373 */ 1374 static void 1375 smb_shr_cache_destroy(void) 1376 { 1377 (void) mutex_lock(&smb_shr_cache.sc_mtx); 1378 if (smb_shr_cache.sc_state == SMB_SHR_CACHE_STATE_CREATED) { 1379 smb_shr_cache.sc_state = SMB_SHR_CACHE_STATE_DESTROYING; 1380 while (smb_shr_cache.sc_nops > 0) 1381 (void) cond_wait(&smb_shr_cache.sc_cv, 1382 &smb_shr_cache.sc_mtx); 1383 1384 smb_shr_cache.sc_cache = NULL; 1385 smb_shr_cache.sc_state = SMB_SHR_CACHE_STATE_NONE; 1386 } 1387 (void) mutex_unlock(&smb_shr_cache.sc_mtx); 1388 } 1389 1390 /* 1391 * If the cache is in "created" state, lock the cache for read 1392 * or read/write based on the specified mode. 1393 * 1394 * Whenever a lock is granted, the number of inflight cache 1395 * operations is incremented. 1396 */ 1397 static uint32_t 1398 smb_shr_cache_lock(int mode) 1399 { 1400 (void) mutex_lock(&smb_shr_cache.sc_mtx); 1401 if (smb_shr_cache.sc_state != SMB_SHR_CACHE_STATE_CREATED) { 1402 (void) mutex_unlock(&smb_shr_cache.sc_mtx); 1403 return (NERR_InternalError); 1404 } 1405 smb_shr_cache.sc_nops++; 1406 (void) mutex_unlock(&smb_shr_cache.sc_mtx); 1407 1408 /* 1409 * Lock has to be taken outside the mutex otherwise 1410 * there could be a deadlock 1411 */ 1412 if (mode == SMB_SHR_CACHE_RDLOCK) 1413 (void) rw_rdlock(&smb_shr_cache.sc_cache_lck); 1414 else 1415 (void) rw_wrlock(&smb_shr_cache.sc_cache_lck); 1416 1417 return (NERR_Success); 1418 } 1419 1420 /* 1421 * Decrement the number of inflight operations and then unlock. 1422 */ 1423 static void 1424 smb_shr_cache_unlock(void) 1425 { 1426 (void) mutex_lock(&smb_shr_cache.sc_mtx); 1427 assert(smb_shr_cache.sc_nops > 0); 1428 smb_shr_cache.sc_nops--; 1429 (void) cond_broadcast(&smb_shr_cache.sc_cv); 1430 (void) mutex_unlock(&smb_shr_cache.sc_mtx); 1431 1432 (void) rw_unlock(&smb_shr_cache.sc_cache_lck); 1433 } 1434 1435 /* 1436 * Return the total number of shares 1437 */ 1438 static int 1439 smb_shr_cache_count(void) 1440 { 1441 return (ht_get_total_items(smb_shr_cache.sc_cache)); 1442 } 1443 1444 /* 1445 * looks up the given share name in the cache and if it 1446 * finds a match returns a pointer to the cached entry. 1447 * Note that since a pointer is returned this function 1448 * MUST be protected by smb_shr_cache_lock/unlock pair 1449 */ 1450 static smb_share_t * 1451 smb_shr_cache_findent(char *sharename) 1452 { 1453 HT_ITEM *item; 1454 1455 (void) smb_strlwr(sharename); 1456 item = ht_find_item(smb_shr_cache.sc_cache, sharename); 1457 if (item && item->hi_data) 1458 return ((smb_share_t *)item->hi_data); 1459 1460 return (NULL); 1461 } 1462 1463 /* 1464 * Return a pointer to the first/next entry in 1465 * the cache based on the given iterator. 1466 * 1467 * Calls to this function MUST be protected by 1468 * smb_shr_cache_lock/unlock. 1469 */ 1470 static smb_share_t * 1471 smb_shr_cache_iterate(smb_shriter_t *shi) 1472 { 1473 HT_ITEM *item; 1474 1475 if (shi->si_first) { 1476 item = ht_findfirst(smb_shr_cache.sc_cache, &shi->si_hashiter); 1477 shi->si_first = B_FALSE; 1478 } else { 1479 item = ht_findnext(&shi->si_hashiter); 1480 } 1481 1482 if (item && item->hi_data) 1483 return ((smb_share_t *)item->hi_data); 1484 1485 return (NULL); 1486 } 1487 1488 /* 1489 * Add the specified share to the cache. Memory needs to be allocated 1490 * for the cache entry and the passed information is copied to the 1491 * allocated space. 1492 */ 1493 static uint32_t 1494 smb_shr_cache_addent(smb_share_t *si) 1495 { 1496 smb_share_t *cache_ent; 1497 uint32_t status = NERR_Success; 1498 1499 if ((cache_ent = malloc(sizeof (smb_share_t))) == NULL) 1500 return (ERROR_NOT_ENOUGH_MEMORY); 1501 1502 (void) smb_strlwr(si->shr_name); 1503 1504 si->shr_type |= smb_shr_is_special(cache_ent->shr_name); 1505 1506 if (smb_shr_is_admin(cache_ent->shr_name)) 1507 si->shr_flags |= SMB_SHRF_ADMIN; 1508 1509 bcopy(si, cache_ent, sizeof (smb_share_t)); 1510 1511 if (si->shr_flags & SMB_SHRF_AUTOHOME) 1512 cache_ent->shr_refcnt = 1; 1513 1514 if (ht_add_item(smb_shr_cache.sc_cache, cache_ent->shr_name, cache_ent) 1515 == NULL) { 1516 syslog(LOG_DEBUG, "share: %s: cache update failed", 1517 cache_ent->shr_name); 1518 free(cache_ent); 1519 status = NERR_InternalError; 1520 } 1521 1522 return (status); 1523 } 1524 1525 /* 1526 * Delete the specified share from the cache. 1527 */ 1528 static void 1529 smb_shr_cache_delent(char *sharename) 1530 { 1531 (void) smb_strlwr(sharename); 1532 (void) ht_remove_item(smb_shr_cache.sc_cache, sharename); 1533 } 1534 1535 /* 1536 * Call back to free the given cache entry. 1537 */ 1538 static void 1539 smb_shr_cache_freent(HT_ITEM *item) 1540 { 1541 if (item && item->hi_data) 1542 free(item->hi_data); 1543 } 1544 1545 /* 1546 * ============================================ 1547 * Interfaces to sharemgr 1548 * 1549 * All functions in this section are private 1550 * ============================================ 1551 */ 1552 1553 /* 1554 * Load shares from sharemgr 1555 */ 1556 /*ARGSUSED*/ 1557 void * 1558 smb_shr_load(void *args) 1559 { 1560 sa_handle_t handle; 1561 sa_group_t group, subgroup; 1562 char *gstate; 1563 boolean_t gdisabled; 1564 1565 smb_shr_load_execinfo(); 1566 1567 if ((handle = smb_shr_sa_enter()) == NULL) { 1568 syslog(LOG_ERR, "smb_shr_load: load failed"); 1569 return (NULL); 1570 } 1571 1572 for (group = sa_get_group(handle, NULL); 1573 group != NULL; group = sa_get_next_group(group)) { 1574 gstate = sa_get_group_attr(group, "state"); 1575 if (gstate == NULL) 1576 continue; 1577 1578 gdisabled = (strcasecmp(gstate, "disabled") == 0); 1579 sa_free_attr_string(gstate); 1580 if (gdisabled) 1581 continue; 1582 1583 smb_shr_sa_loadgrp(group); 1584 1585 for (subgroup = sa_get_sub_group(group); 1586 subgroup != NULL; 1587 subgroup = sa_get_next_group(subgroup)) { 1588 smb_shr_sa_loadgrp(subgroup); 1589 } 1590 1591 } 1592 smb_shr_sa_exit(); 1593 return (NULL); 1594 } 1595 1596 void 1597 smb_shr_load_execinfo() 1598 { 1599 (void) mutex_lock(&smb_shr_exec_mtx); 1600 (void) smb_config_get_execinfo(smb_shr_exec_map, smb_shr_exec_unmap, 1601 MAXPATHLEN); 1602 (void) mutex_unlock(&smb_shr_exec_mtx); 1603 } 1604 1605 /* 1606 * Load the shares contained in the specified group. 1607 * 1608 * Don't process groups on which the smb protocol is disabled. 1609 * The top level ZFS group won't have the smb protocol enabled 1610 * but sub-groups will. 1611 * 1612 * We will tolerate a limited number of errors and then give 1613 * up on the current group. A typical error might be that the 1614 * shared directory no longer exists. 1615 */ 1616 static void 1617 smb_shr_sa_loadgrp(sa_group_t group) 1618 { 1619 sa_share_t share; 1620 sa_resource_t resource; 1621 int error_count = 0; 1622 1623 if (sa_get_optionset(group, SMB_PROTOCOL_NAME) == NULL) 1624 return; 1625 1626 for (share = sa_get_share(group, NULL); 1627 share != NULL; 1628 share = sa_get_next_share(share)) { 1629 for (resource = sa_get_share_resource(share, NULL); 1630 resource != NULL; 1631 resource = sa_get_next_resource(resource)) { 1632 if (smb_shr_sa_load(share, resource)) 1633 ++error_count; 1634 1635 if (error_count > SMB_SHR_ERROR_THRESHOLD) 1636 break; 1637 } 1638 1639 if (error_count > SMB_SHR_ERROR_THRESHOLD) 1640 break; 1641 } 1642 } 1643 1644 /* 1645 * Load a share definition from sharemgr and add it to the cache. 1646 * If the share is already in the cache then it doesn't do anything. 1647 * 1648 * This function does not report duplicate shares as error since 1649 * a share might have been added by smb_shr_get() while load is 1650 * in progress. 1651 */ 1652 static uint32_t 1653 smb_shr_sa_load(sa_share_t share, sa_resource_t resource) 1654 { 1655 smb_share_t si; 1656 char *sharename; 1657 uint32_t status; 1658 boolean_t loaded; 1659 1660 if ((sharename = sa_get_resource_attr(resource, "name")) == NULL) 1661 return (NERR_InternalError); 1662 1663 loaded = smb_shr_exists(sharename); 1664 sa_free_attr_string(sharename); 1665 1666 if (loaded) 1667 return (NERR_Success); 1668 1669 if ((status = smb_shr_sa_get(share, resource, &si)) != NERR_Success) { 1670 syslog(LOG_DEBUG, "share: failed to load %s (%d)", 1671 si.shr_name, status); 1672 return (status); 1673 } 1674 1675 status = smb_shr_add(&si); 1676 if ((status != NERR_Success) && (status != NERR_DuplicateShare)) { 1677 syslog(LOG_DEBUG, "share: failed to cache %s (%d)", 1678 si.shr_name, status); 1679 return (status); 1680 } 1681 1682 return (NERR_Success); 1683 } 1684 1685 static char * 1686 smb_shr_sa_getprop(sa_optionset_t opts, char *propname) 1687 { 1688 sa_property_t prop; 1689 char *val = NULL; 1690 1691 prop = sa_get_property(opts, propname); 1692 if (prop != NULL) 1693 val = sa_get_property_attr(prop, "value"); 1694 1695 return (val); 1696 } 1697 1698 /* 1699 * Read the specified share information from sharemgr and return 1700 * it in the given smb_share_t structure. 1701 * 1702 * Shares read from sharemgr are marked as permanent/persistent. 1703 */ 1704 static uint32_t 1705 smb_shr_sa_get(sa_share_t share, sa_resource_t resource, smb_share_t *si) 1706 { 1707 sa_optionset_t opts; 1708 char *val = NULL; 1709 char *path; 1710 char *rname; 1711 1712 if ((path = sa_get_share_attr(share, "path")) == NULL) 1713 return (NERR_InternalError); 1714 1715 if ((rname = sa_get_resource_attr(resource, "name")) == NULL) { 1716 sa_free_attr_string(path); 1717 return (NERR_InternalError); 1718 } 1719 1720 bzero(si, sizeof (smb_share_t)); 1721 si->shr_flags = SMB_SHRF_PERM; 1722 1723 (void) strlcpy(si->shr_path, path, sizeof (si->shr_path)); 1724 (void) strlcpy(si->shr_name, rname, sizeof (si->shr_name)); 1725 sa_free_attr_string(path); 1726 sa_free_attr_string(rname); 1727 1728 val = sa_get_resource_description(resource); 1729 if (val == NULL) 1730 val = sa_get_share_description(share); 1731 1732 if (val != NULL) { 1733 (void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt)); 1734 sa_free_share_description(val); 1735 } 1736 1737 opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1); 1738 if (opts == NULL) 1739 return (NERR_Success); 1740 1741 val = smb_shr_sa_getprop(opts, SHOPT_AD_CONTAINER); 1742 if (val != NULL) { 1743 (void) strlcpy(si->shr_container, val, 1744 sizeof (si->shr_container)); 1745 free(val); 1746 } 1747 1748 val = smb_shr_sa_getprop(opts, SHOPT_CATIA); 1749 if (val != NULL) { 1750 smb_shr_sa_setflag(val, si, SMB_SHRF_CATIA); 1751 free(val); 1752 } 1753 1754 val = smb_shr_sa_getprop(opts, SHOPT_ABE); 1755 if (val != NULL) { 1756 smb_shr_sa_setflag(val, si, SMB_SHRF_ABE); 1757 free(val); 1758 } 1759 1760 val = smb_shr_sa_getprop(opts, SHOPT_GUEST); 1761 if (val != NULL) { 1762 smb_shr_sa_setflag(val, si, SMB_SHRF_GUEST_OK); 1763 free(val); 1764 } 1765 1766 val = smb_shr_sa_getprop(opts, SHOPT_DFSROOT); 1767 if (val != NULL) { 1768 smb_shr_sa_setflag(val, si, SMB_SHRF_DFSROOT); 1769 free(val); 1770 } 1771 1772 val = smb_shr_sa_getprop(opts, SHOPT_QUOTAS); 1773 if (val != NULL) { 1774 /* Turn the flag on or off */ 1775 smb_shr_sa_setflag(val, si, SMB_SHRF_QUOTAS); 1776 free(val); 1777 } else { 1778 /* Default for this is enabled. */ 1779 si->shr_flags |= SMB_SHRF_QUOTAS; 1780 } 1781 1782 val = smb_shr_sa_getprop(opts, SHOPT_CSC); 1783 if (val != NULL) { 1784 smb_shr_sa_csc_option(val, si); 1785 free(val); 1786 } 1787 1788 val = smb_shr_sa_getprop(opts, SHOPT_NONE); 1789 if (val != NULL) { 1790 (void) strlcpy(si->shr_access_none, val, 1791 sizeof (si->shr_access_none)); 1792 free(val); 1793 si->shr_flags |= SMB_SHRF_ACC_NONE; 1794 } 1795 1796 val = smb_shr_sa_getprop(opts, SHOPT_RO); 1797 if (val != NULL) { 1798 (void) strlcpy(si->shr_access_ro, val, 1799 sizeof (si->shr_access_ro)); 1800 free(val); 1801 si->shr_flags |= SMB_SHRF_ACC_RO; 1802 } 1803 1804 val = smb_shr_sa_getprop(opts, SHOPT_RW); 1805 if (val != NULL) { 1806 (void) strlcpy(si->shr_access_rw, val, 1807 sizeof (si->shr_access_rw)); 1808 free(val); 1809 si->shr_flags |= SMB_SHRF_ACC_RW; 1810 } 1811 1812 sa_free_derived_optionset(opts); 1813 return (NERR_Success); 1814 } 1815 1816 /* 1817 * Map a client-side caching (CSC) option to the appropriate share 1818 * flag. Only one option is allowed; an error will be logged if 1819 * multiple options have been specified. We don't need to do anything 1820 * about multiple values here because the SRVSVC will not recognize 1821 * a value containing multiple flags and will return the default value. 1822 * 1823 * If the option value is not recognized, it will be ignored: invalid 1824 * values will typically be caught and rejected by sharemgr. 1825 */ 1826 void 1827 smb_shr_sa_csc_option(const char *value, smb_share_t *si) 1828 { 1829 int i; 1830 1831 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) { 1832 if (strcasecmp(value, cscopt[i].value) == 0) { 1833 si->shr_flags |= cscopt[i].flag; 1834 break; 1835 } 1836 } 1837 1838 switch (si->shr_flags & SMB_SHRF_CSC_MASK) { 1839 case 0: 1840 case SMB_SHRF_CSC_DISABLED: 1841 case SMB_SHRF_CSC_MANUAL: 1842 case SMB_SHRF_CSC_AUTO: 1843 case SMB_SHRF_CSC_VDO: 1844 break; 1845 1846 default: 1847 syslog(LOG_INFO, "csc option conflict: 0x%08x", 1848 si->shr_flags & SMB_SHRF_CSC_MASK); 1849 break; 1850 } 1851 } 1852 1853 /* 1854 * Return the option name for the first CSC flag (there should be only 1855 * one) encountered in the share flags. 1856 */ 1857 char * 1858 smb_shr_sa_csc_name(const smb_share_t *si) 1859 { 1860 int i; 1861 1862 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) { 1863 if (si->shr_flags & cscopt[i].flag) 1864 return (cscopt[i].value); 1865 } 1866 1867 return (NULL); 1868 } 1869 1870 /* 1871 * Takes the value of a boolean share property and set/clear the 1872 * specified flag based on the property's value. 1873 */ 1874 void 1875 smb_shr_sa_setflag(const char *value, smb_share_t *si, uint32_t flag) 1876 { 1877 if ((strcasecmp(value, "true") == 0) || (strcmp(value, "1") == 0)) 1878 si->shr_flags |= flag; 1879 else 1880 si->shr_flags &= ~flag; 1881 } 1882 1883 /* 1884 * looks up sharemgr for the given share (resource) and loads 1885 * the definition into cache if lookup is successful 1886 */ 1887 static uint32_t 1888 smb_shr_sa_loadbyname(char *sharename) 1889 { 1890 sa_handle_t handle; 1891 sa_share_t share; 1892 sa_resource_t resource; 1893 uint32_t status; 1894 1895 if ((handle = smb_shr_sa_enter()) == NULL) 1896 return (NERR_InternalError); 1897 1898 resource = sa_find_resource(handle, sharename); 1899 if (resource == NULL) { 1900 smb_shr_sa_exit(); 1901 return (NERR_NetNameNotFound); 1902 } 1903 1904 share = sa_get_resource_parent(resource); 1905 if (share == NULL) { 1906 smb_shr_sa_exit(); 1907 return (NERR_InternalError); 1908 } 1909 1910 status = smb_shr_sa_load(share, resource); 1911 1912 smb_shr_sa_exit(); 1913 return (status); 1914 } 1915 1916 /* 1917 * ============================================ 1918 * Share publishing functions 1919 * 1920 * All the functions are private 1921 * ============================================ 1922 */ 1923 1924 static void 1925 smb_shr_publish(const char *sharename, const char *container) 1926 { 1927 smb_shr_publisher_queue(sharename, container, SMB_SHR_PUBLISH); 1928 } 1929 1930 static void 1931 smb_shr_unpublish(const char *sharename, const char *container) 1932 { 1933 smb_shr_publisher_queue(sharename, container, SMB_SHR_UNPUBLISH); 1934 } 1935 1936 /* 1937 * In domain mode, put a share on the publisher queue. 1938 * This is a no-op if the smb service is in Workgroup mode. 1939 */ 1940 static void 1941 smb_shr_publisher_queue(const char *sharename, const char *container, char op) 1942 { 1943 smb_shr_pitem_t *item = NULL; 1944 1945 if (container == NULL || *container == '\0') 1946 return; 1947 1948 if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN) 1949 return; 1950 1951 (void) mutex_lock(&ad_queue.spq_mtx); 1952 switch (ad_queue.spq_state) { 1953 case SMB_SHR_PQS_READY: 1954 case SMB_SHR_PQS_PUBLISHING: 1955 break; 1956 default: 1957 (void) mutex_unlock(&ad_queue.spq_mtx); 1958 return; 1959 } 1960 (void) mutex_unlock(&ad_queue.spq_mtx); 1961 1962 if ((item = malloc(sizeof (smb_shr_pitem_t))) == NULL) 1963 return; 1964 1965 item->spi_op = op; 1966 (void) strlcpy(item->spi_name, sharename, sizeof (item->spi_name)); 1967 (void) strlcpy(item->spi_container, container, 1968 sizeof (item->spi_container)); 1969 1970 (void) mutex_lock(&ad_queue.spq_mtx); 1971 list_insert_tail(&ad_queue.spq_list, item); 1972 (void) cond_signal(&ad_queue.spq_cv); 1973 (void) mutex_unlock(&ad_queue.spq_mtx); 1974 } 1975 1976 /* 1977 * Publishing won't be activated if the smb service is running in 1978 * Workgroup mode. 1979 */ 1980 static int 1981 smb_shr_publisher_start(void) 1982 { 1983 pthread_t publish_thr; 1984 pthread_attr_t tattr; 1985 int rc; 1986 1987 if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN) 1988 return (0); 1989 1990 (void) mutex_lock(&ad_queue.spq_mtx); 1991 if (ad_queue.spq_state != SMB_SHR_PQS_NOQUEUE) { 1992 (void) mutex_unlock(&ad_queue.spq_mtx); 1993 errno = EINVAL; 1994 return (-1); 1995 } 1996 1997 list_create(&ad_queue.spq_list, sizeof (smb_shr_pitem_t), 1998 offsetof(smb_shr_pitem_t, spi_lnd)); 1999 ad_queue.spq_state = SMB_SHR_PQS_READY; 2000 (void) mutex_unlock(&ad_queue.spq_mtx); 2001 2002 (void) pthread_attr_init(&tattr); 2003 (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); 2004 rc = pthread_create(&publish_thr, &tattr, smb_shr_publisher, 0); 2005 (void) pthread_attr_destroy(&tattr); 2006 2007 return (rc); 2008 } 2009 2010 static void 2011 smb_shr_publisher_stop(void) 2012 { 2013 if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN) 2014 return; 2015 2016 (void) mutex_lock(&ad_queue.spq_mtx); 2017 switch (ad_queue.spq_state) { 2018 case SMB_SHR_PQS_READY: 2019 case SMB_SHR_PQS_PUBLISHING: 2020 ad_queue.spq_state = SMB_SHR_PQS_STOPPING; 2021 (void) cond_signal(&ad_queue.spq_cv); 2022 break; 2023 default: 2024 break; 2025 } 2026 (void) mutex_unlock(&ad_queue.spq_mtx); 2027 } 2028 2029 /* 2030 * This is the publisher daemon thread. While running, the thread waits 2031 * on a conditional variable until notified that a share needs to be 2032 * [un]published or that the thread should be terminated. 2033 * 2034 * Entries may remain in the outgoing queue if the Active Directory 2035 * service is inaccessible, in which case the thread wakes up every 60 2036 * seconds to retry. 2037 */ 2038 /*ARGSUSED*/ 2039 static void * 2040 smb_shr_publisher(void *arg) 2041 { 2042 smb_ads_handle_t *ah; 2043 smb_shr_pitem_t *shr; 2044 list_t publist; 2045 timestruc_t pubretry; 2046 char hostname[MAXHOSTNAMELEN]; 2047 2048 (void) mutex_lock(&ad_queue.spq_mtx); 2049 if (ad_queue.spq_state != SMB_SHR_PQS_READY) { 2050 (void) mutex_unlock(&ad_queue.spq_mtx); 2051 return (NULL); 2052 } 2053 ad_queue.spq_state = SMB_SHR_PQS_PUBLISHING; 2054 (void) mutex_unlock(&ad_queue.spq_mtx); 2055 2056 (void) smb_gethostname(hostname, MAXHOSTNAMELEN, 2057 SMB_CASE_PRESERVE); 2058 2059 list_create(&publist, sizeof (smb_shr_pitem_t), 2060 offsetof(smb_shr_pitem_t, spi_lnd)); 2061 2062 for (;;) { 2063 (void) mutex_lock(&ad_queue.spq_mtx); 2064 2065 while (list_is_empty(&ad_queue.spq_list) && 2066 (ad_queue.spq_state == SMB_SHR_PQS_PUBLISHING)) { 2067 if (list_is_empty(&publist)) { 2068 (void) cond_wait(&ad_queue.spq_cv, 2069 &ad_queue.spq_mtx); 2070 } else { 2071 pubretry.tv_sec = 60; 2072 pubretry.tv_nsec = 0; 2073 (void) cond_reltimedwait(&ad_queue.spq_cv, 2074 &ad_queue.spq_mtx, &pubretry); 2075 break; 2076 } 2077 } 2078 2079 if (ad_queue.spq_state != SMB_SHR_PQS_PUBLISHING) { 2080 (void) mutex_unlock(&ad_queue.spq_mtx); 2081 break; 2082 } 2083 2084 /* 2085 * Transfer queued items to the local list so that 2086 * the mutex can be released. 2087 */ 2088 while ((shr = list_head(&ad_queue.spq_list)) != NULL) { 2089 list_remove(&ad_queue.spq_list, shr); 2090 list_insert_tail(&publist, shr); 2091 } 2092 2093 (void) mutex_unlock(&ad_queue.spq_mtx); 2094 2095 if ((ah = smb_ads_open()) != NULL) { 2096 smb_shr_publisher_send(ah, &publist, hostname); 2097 smb_ads_close(ah); 2098 } 2099 } 2100 2101 (void) mutex_lock(&ad_queue.spq_mtx); 2102 smb_shr_publisher_flush(&ad_queue.spq_list); 2103 list_destroy(&ad_queue.spq_list); 2104 ad_queue.spq_state = SMB_SHR_PQS_NOQUEUE; 2105 (void) mutex_unlock(&ad_queue.spq_mtx); 2106 2107 smb_shr_publisher_flush(&publist); 2108 list_destroy(&publist); 2109 return (NULL); 2110 } 2111 2112 /* 2113 * Remove items from the specified queue and [un]publish them. 2114 */ 2115 static void 2116 smb_shr_publisher_send(smb_ads_handle_t *ah, list_t *publist, const char *host) 2117 { 2118 smb_shr_pitem_t *shr; 2119 2120 while ((shr = list_head(publist)) != NULL) { 2121 (void) mutex_lock(&ad_queue.spq_mtx); 2122 if (ad_queue.spq_state != SMB_SHR_PQS_PUBLISHING) { 2123 (void) mutex_unlock(&ad_queue.spq_mtx); 2124 return; 2125 } 2126 (void) mutex_unlock(&ad_queue.spq_mtx); 2127 2128 list_remove(publist, shr); 2129 2130 if (shr->spi_op == SMB_SHR_PUBLISH) 2131 (void) smb_ads_publish_share(ah, shr->spi_name, 2132 NULL, shr->spi_container, host); 2133 else 2134 (void) smb_ads_remove_share(ah, shr->spi_name, 2135 NULL, shr->spi_container, host); 2136 2137 free(shr); 2138 } 2139 } 2140 2141 /* 2142 * Flush all remaining items from the specified list/queue. 2143 */ 2144 static void 2145 smb_shr_publisher_flush(list_t *lst) 2146 { 2147 smb_shr_pitem_t *shr; 2148 2149 while ((shr = list_head(lst)) != NULL) { 2150 list_remove(lst, shr); 2151 free(shr); 2152 } 2153 } 2154 2155 /* 2156 * If the share path refers to a ZFS file system, add the 2157 * .zfs/shares/<share> object and add or remove the special 2158 * directory and file telling clients about quota support. 2159 */ 2160 static void 2161 smb_shr_zfs_add(smb_share_t *si) 2162 { 2163 libzfs_handle_t *libhd; 2164 zfs_handle_t *zfshd; 2165 int ret; 2166 char buf[MAXPATHLEN]; /* dataset or mountpoint */ 2167 2168 if (smb_getdataset(si->shr_path, buf, MAXPATHLEN) != 0) 2169 return; 2170 2171 if ((libhd = libzfs_init()) == NULL) 2172 return; 2173 2174 if ((zfshd = zfs_open(libhd, buf, ZFS_TYPE_FILESYSTEM)) == NULL) { 2175 libzfs_fini(libhd); 2176 return; 2177 } 2178 2179 errno = 0; 2180 ret = zfs_smb_acl_add(libhd, buf, si->shr_path, si->shr_name); 2181 if (ret != 0 && errno != EAGAIN && errno != EEXIST) 2182 syslog(LOG_INFO, "share: failed to add ACL object: %s: %s\n", 2183 si->shr_name, strerror(errno)); 2184 2185 ret = zfs_prop_get(zfshd, ZFS_PROP_MOUNTPOINT, 2186 buf, MAXPATHLEN, NULL, NULL, 0, B_FALSE); 2187 if (ret != 0) { 2188 syslog(LOG_INFO, "share: failed to get mountpoint: " 2189 "%s\n", si->shr_name); 2190 } else { 2191 if ((si->shr_flags & SMB_SHRF_QUOTAS) != 0) { 2192 smb_quota_add_fs(buf); 2193 } else { 2194 smb_quota_remove_fs(buf); 2195 } 2196 } 2197 2198 zfs_close(zfshd); 2199 libzfs_fini(libhd); 2200 } 2201 2202 /* 2203 * If the share path refers to a ZFS file system, remove the 2204 * .zfs/shares/<share> object. 2205 */ 2206 static void 2207 smb_shr_zfs_remove(smb_share_t *si) 2208 { 2209 libzfs_handle_t *libhd; 2210 int ret; 2211 char buf[MAXPATHLEN]; /* dataset or mountpoint */ 2212 2213 if (smb_getdataset(si->shr_path, buf, MAXPATHLEN) != 0) 2214 return; 2215 2216 if ((libhd = libzfs_init()) == NULL) 2217 return; 2218 2219 errno = 0; 2220 ret = zfs_smb_acl_remove(libhd, buf, si->shr_path, si->shr_name); 2221 if (ret != 0 && errno != EAGAIN) 2222 syslog(LOG_INFO, "share: failed to remove ACL object: %s: %s\n", 2223 si->shr_name, strerror(errno)); 2224 2225 /* 2226 * We could remove the quotas directory here, but that adds 2227 * significantly to the time required for a zpool export, 2228 * so just leave it here and fixup when we share next. 2229 */ 2230 2231 libzfs_fini(libhd); 2232 } 2233 2234 /* 2235 * If the share path refers to a ZFS file system, rename the 2236 * .zfs/shares/<share> object. 2237 */ 2238 static void 2239 smb_shr_zfs_rename(smb_share_t *from, smb_share_t *to) 2240 { 2241 libzfs_handle_t *libhd; 2242 zfs_handle_t *zfshd; 2243 int ret; 2244 char dataset[MAXPATHLEN]; 2245 2246 if (smb_getdataset(from->shr_path, dataset, MAXPATHLEN) != 0) 2247 return; 2248 2249 if ((libhd = libzfs_init()) == NULL) 2250 return; 2251 2252 if ((zfshd = zfs_open(libhd, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) { 2253 libzfs_fini(libhd); 2254 return; 2255 } 2256 2257 errno = 0; 2258 ret = zfs_smb_acl_rename(libhd, dataset, from->shr_path, 2259 from->shr_name, to->shr_name); 2260 if (ret != 0 && errno != EAGAIN) 2261 syslog(LOG_INFO, "share: failed to rename ACL object: %s: %s\n", 2262 from->shr_name, strerror(errno)); 2263 2264 zfs_close(zfshd); 2265 libzfs_fini(libhd); 2266 } 2267 2268 /* 2269 * Enable all privileges in the inheritable set to execute command. 2270 */ 2271 static int 2272 smb_shr_enable_all_privs(void) 2273 { 2274 priv_set_t *pset; 2275 2276 pset = priv_allocset(); 2277 if (pset == NULL) 2278 return (-1); 2279 2280 if (getppriv(PRIV_LIMIT, pset)) { 2281 priv_freeset(pset); 2282 return (-1); 2283 } 2284 2285 if (setppriv(PRIV_ON, PRIV_INHERITABLE, pset)) { 2286 priv_freeset(pset); 2287 return (-1); 2288 } 2289 2290 priv_freeset(pset); 2291 return (0); 2292 } 2293 2294 /* 2295 * Tokenizes the command string and returns the list of tokens in an array. 2296 * 2297 * Returns NULL if there are no tokens. 2298 */ 2299 static char ** 2300 smb_shr_tokenize_cmd(char *cmdstr) 2301 { 2302 char *cmd, *buf, *bp, *value; 2303 char **argv, **ap; 2304 int argc, i; 2305 2306 if (cmdstr == NULL || *cmdstr == '\0') 2307 return (NULL); 2308 2309 if ((buf = malloc(MAXPATHLEN)) == NULL) 2310 return (NULL); 2311 2312 (void) strlcpy(buf, cmdstr, MAXPATHLEN); 2313 2314 for (argc = 2, bp = cmdstr; *bp != '\0'; ++bp) 2315 if (*bp == ' ') 2316 ++argc; 2317 2318 if ((argv = calloc(argc, sizeof (char *))) == NULL) { 2319 free(buf); 2320 return (NULL); 2321 } 2322 2323 ap = argv; 2324 for (bp = buf, i = 0; i < argc; ++i) { 2325 do { 2326 if ((value = strsep(&bp, " ")) == NULL) 2327 break; 2328 } while (*value == '\0'); 2329 2330 if (value == NULL) 2331 break; 2332 2333 *ap++ = value; 2334 } 2335 2336 /* get the filename of the command from the path */ 2337 if ((cmd = strrchr(argv[0], '/')) != NULL) 2338 (void) strlcpy(argv[0], ++cmd, strlen(argv[0])); 2339 2340 return (argv); 2341 } 2342 2343 /* 2344 * Expands the command string for the following substitution tokens: 2345 * 2346 * %U - Windows username 2347 * %D - Name of the domain or workgroup of %U 2348 * %h - The server hostname 2349 * %M - The client hostname 2350 * %L - The server NetBIOS name 2351 * %m - The client NetBIOS name. This option is only valid for NetBIOS 2352 * connections (port 139). 2353 * %I - The IP address of the client machine 2354 * %i - The local IP address to which the client is connected 2355 * %S - The name of the share 2356 * %P - The root directory of the share 2357 * %u - The UID of the Unix user 2358 * 2359 * Returns 0 on success. Otherwise -1. 2360 */ 2361 static int 2362 smb_shr_expand_subs(char **cmd_toks, smb_share_t *si, smb_shr_execinfo_t *subs) 2363 { 2364 char *fmt, *sub_chr, *ptr; 2365 boolean_t unknown; 2366 char hostname[MAXHOSTNAMELEN]; 2367 char ip_str[INET6_ADDRSTRLEN]; 2368 char name[SMB_PI_MAX_HOST]; 2369 smb_wchar_t wbuf[SMB_PI_MAX_HOST]; 2370 int i; 2371 2372 if (cmd_toks == NULL || *cmd_toks == NULL) 2373 return (-1); 2374 2375 for (i = 1; cmd_toks[i]; i++) { 2376 fmt = cmd_toks[i]; 2377 if (*fmt == '%') { 2378 sub_chr = fmt + 1; 2379 unknown = B_FALSE; 2380 2381 switch (*sub_chr) { 2382 case 'U': 2383 ptr = strdup(subs->e_winname); 2384 break; 2385 case 'D': 2386 ptr = strdup(subs->e_userdom); 2387 break; 2388 case 'h': 2389 if (gethostname(hostname, MAXHOSTNAMELEN) != 0) 2390 unknown = B_TRUE; 2391 else 2392 ptr = strdup(hostname); 2393 break; 2394 case 'M': 2395 if (smb_getnameinfo(&subs->e_cli_ipaddr, 2396 hostname, sizeof (hostname), 0) != 0) 2397 unknown = B_TRUE; 2398 else 2399 ptr = strdup(hostname); 2400 break; 2401 case 'L': 2402 if (smb_getnetbiosname(hostname, 2403 NETBIOS_NAME_SZ) != 0) 2404 unknown = B_TRUE; 2405 else 2406 ptr = strdup(hostname); 2407 break; 2408 case 'm': 2409 if (*subs->e_cli_netbiosname == '\0') 2410 unknown = B_TRUE; 2411 else { 2412 (void) smb_mbstowcs(wbuf, 2413 subs->e_cli_netbiosname, 2414 SMB_PI_MAX_HOST - 1); 2415 2416 if (ucstooem(name, wbuf, 2417 SMB_PI_MAX_HOST, OEM_CPG_850) == 0) 2418 (void) strlcpy(name, 2419 subs->e_cli_netbiosname, 2420 SMB_PI_MAX_HOST); 2421 2422 ptr = strdup(name); 2423 } 2424 break; 2425 case 'I': 2426 if (smb_inet_ntop(&subs->e_cli_ipaddr, ip_str, 2427 SMB_IPSTRLEN(subs->e_cli_ipaddr.a_family)) 2428 != NULL) 2429 ptr = strdup(ip_str); 2430 else 2431 unknown = B_TRUE; 2432 break; 2433 case 'i': 2434 if (smb_inet_ntop(&subs->e_srv_ipaddr, ip_str, 2435 SMB_IPSTRLEN(subs->e_srv_ipaddr.a_family)) 2436 != NULL) 2437 ptr = strdup(ip_str); 2438 else 2439 unknown = B_TRUE; 2440 break; 2441 case 'S': 2442 ptr = strdup(si->shr_name); 2443 break; 2444 case 'P': 2445 ptr = strdup(si->shr_path); 2446 break; 2447 case 'u': 2448 (void) snprintf(name, sizeof (name), "%u", 2449 subs->e_uid); 2450 ptr = strdup(name); 2451 break; 2452 default: 2453 /* unknown sub char */ 2454 unknown = B_TRUE; 2455 break; 2456 } 2457 2458 if (unknown) 2459 ptr = strdup(""); 2460 2461 } else /* first char of cmd's arg is not '%' char */ 2462 ptr = strdup(""); 2463 2464 cmd_toks[i] = ptr; 2465 2466 if (ptr == NULL) { 2467 for (i = 1; cmd_toks[i]; i++) 2468 free(cmd_toks[i]); 2469 2470 return (-1); 2471 } 2472 } 2473 2474 return (0); 2475 } 2476 2477 /*ARGSUSED*/ 2478 static void 2479 smb_shr_sig_abnormal_term(int sig_val) 2480 { 2481 /* 2482 * Calling _exit() prevents parent process from getting SIGTERM/SIGINT 2483 * signal. 2484 */ 2485 _exit(-1); 2486 } 2487 2488 /*ARGSUSED*/ 2489 static void 2490 smb_shr_sig_child(int sig_val) 2491 { 2492 /* 2493 * Catch the signal and allow the exit status of the child process 2494 * to be available for reaping. 2495 */ 2496 } 2497 2498 /* 2499 * This is a temporary function which converts the given smb_share_t 2500 * structure to the nvlist format that will be provided by libsharev2 2501 */ 2502 static int 2503 smb_shr_encode(smb_share_t *si, nvlist_t **nvlist) 2504 { 2505 nvlist_t *list; 2506 nvlist_t *share; 2507 nvlist_t *smb; 2508 char *csc; 2509 int rc = 0; 2510 2511 *nvlist = NULL; 2512 2513 if ((rc = nvlist_alloc(&list, NV_UNIQUE_NAME, 0)) != 0) 2514 return (rc); 2515 2516 if ((rc = nvlist_alloc(&share, NV_UNIQUE_NAME, 0)) != 0) { 2517 nvlist_free(list); 2518 return (rc); 2519 } 2520 2521 if ((rc = nvlist_alloc(&smb, NV_UNIQUE_NAME, 0)) != 0) { 2522 nvlist_free(share); 2523 nvlist_free(list); 2524 return (rc); 2525 } 2526 2527 /* global share properties */ 2528 rc |= nvlist_add_string(share, "name", si->shr_name); 2529 rc |= nvlist_add_string(share, "path", si->shr_path); 2530 rc |= nvlist_add_string(share, "desc", si->shr_cmnt); 2531 2532 /* smb protocol properties */ 2533 rc = nvlist_add_string(smb, SHOPT_AD_CONTAINER, si->shr_container); 2534 if ((si->shr_flags & SMB_SHRF_ACC_NONE) != 0) 2535 rc |= nvlist_add_string(smb, SHOPT_NONE, si->shr_access_none); 2536 if ((si->shr_flags & SMB_SHRF_ACC_RO) != 0) 2537 rc |= nvlist_add_string(smb, SHOPT_RO, si->shr_access_ro); 2538 if ((si->shr_flags & SMB_SHRF_ACC_RW) != 0) 2539 rc |= nvlist_add_string(smb, SHOPT_RW, si->shr_access_rw); 2540 2541 if ((si->shr_flags & SMB_SHRF_ABE) != 0) 2542 rc |= nvlist_add_string(smb, SHOPT_ABE, "true"); 2543 if ((si->shr_flags & SMB_SHRF_CATIA) != 0) 2544 rc |= nvlist_add_string(smb, SHOPT_CATIA, "true"); 2545 if ((si->shr_flags & SMB_SHRF_GUEST_OK) != 0) 2546 rc |= nvlist_add_string(smb, SHOPT_GUEST, "true"); 2547 if ((si->shr_flags & SMB_SHRF_DFSROOT) != 0) 2548 rc |= nvlist_add_string(smb, SHOPT_DFSROOT, "true"); 2549 if ((si->shr_flags & SMB_SHRF_QUOTAS) != 0) 2550 rc |= nvlist_add_string(smb, SHOPT_QUOTAS, "true"); 2551 2552 if ((si->shr_flags & SMB_SHRF_AUTOHOME) != 0) { 2553 rc |= nvlist_add_string(smb, SHOPT_AUTOHOME, "true"); 2554 rc |= nvlist_add_uint32(smb, "uid", si->shr_uid); 2555 rc |= nvlist_add_uint32(smb, "gid", si->shr_gid); 2556 } 2557 2558 if ((csc = smb_shr_sa_csc_name(si)) != NULL) 2559 rc |= nvlist_add_string(smb, SHOPT_CSC, csc); 2560 2561 rc |= nvlist_add_uint32(smb, "type", si->shr_type); 2562 2563 rc |= nvlist_add_nvlist(share, "smb", smb); 2564 rc |= nvlist_add_nvlist(list, si->shr_name, share); 2565 2566 nvlist_free(share); 2567 nvlist_free(smb); 2568 2569 if (rc != 0) 2570 nvlist_free(list); 2571 else 2572 *nvlist = list; 2573 2574 return (rc); 2575 } 2576