1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 24 * Copyright (c) 2016 by Delphix. All rights reserved. 25 */ 26 27 /* 28 * General Structures Layout 29 * ------------------------- 30 * 31 * This is a simplified diagram showing the relationship between most of the 32 * main structures. 33 * 34 * +-------------------+ 35 * | SMB_INFO | 36 * +-------------------+ 37 * | 38 * | 39 * v 40 * +-------------------+ +-------------------+ +-------------------+ 41 * | SESSION |<----->| SESSION |......| SESSION | 42 * +-------------------+ +-------------------+ +-------------------+ 43 * | | 44 * | | 45 * | v 46 * | +-------------------+ +-------------------+ +-------------------+ 47 * | | USER |<--->| USER |...| USER | 48 * | +-------------------+ +-------------------+ +-------------------+ 49 * | 50 * | 51 * v 52 * +-------------------+ +-------------------+ +-------------------+ 53 * | TREE |<----->| TREE |......| TREE | 54 * +-------------------+ +-------------------+ +-------------------+ 55 * | | 56 * | | 57 * | v 58 * | +-------+ +-------+ +-------+ 59 * | | OFILE |<----->| OFILE |......| OFILE | 60 * | +-------+ +-------+ +-------+ 61 * | 62 * | 63 * v 64 * +-------+ +------+ +------+ 65 * | ODIR |<----->| ODIR |......| ODIR | 66 * +-------+ +------+ +------+ 67 * 68 * 69 * User State Machine 70 * ------------------ 71 * 72 * 73 * | T0: Creation/Allocation 74 * | (1st session setup) 75 * v 76 * +-----------------------------+ 77 * | SMB_USER_STATE_LOGGING_ON |<----------+ 78 * +-----------------------------+ addl. session setup 79 * | | (more proc. required) 80 * | T2 | ^ 81 * | | | T1: (cont.) 82 * | +------->-------? 83 * v | T3: (fail) 84 * +-----------------------------+ v 85 * | SMB_USER_STATE_LOGGED_ON | (logged off) 86 * +-----------------------------+ 87 * | 88 * | T4 89 * | 90 * v 91 * +-----------------------------+ 92 * | SMB_USER_STATE_LOGGING_OFF | 93 * +-----------------------------+ 94 * | 95 * | T5 96 * | 97 * v 98 * +-----------------------------+ T6 99 * | SMB_USER_STATE_LOGGED_OFF |----------> Deletion/Free 100 * +-----------------------------+ 101 * 102 * SMB_USER_STATE_LOGGING_ON 103 * 104 * While in this state: 105 * - The user is in the list of users for their session. 106 * - References will be given out ONLY for session setup. 107 * - This user can not access anything yet. 108 * 109 * SMB_USER_STATE_LOGGED_ON 110 * 111 * While in this state: 112 * - The user is in the list of users for their session. 113 * - References will be given out if the user is looked up. 114 * - The user can access files and pipes. 115 * 116 * SMB_USER_STATE_LOGGING_OFF 117 * 118 * While in this state: 119 * - The user is in the list of users for their session. 120 * - References will not be given out if the user is looked up. 121 * - The trees the user connected are being disconnected. 122 * - The resources associated with the user remain. 123 * 124 * SMB_USER_STATE_LOGGED_OFF 125 * 126 * While in this state: 127 * - The user is queued in the list of users of their session. 128 * - References will not be given out if the user is looked up. 129 * - The user has no more trees connected. 130 * - The resources associated with the user remain. 131 * 132 * Transition T0 133 * 134 * First request in an SMB Session Setup sequence creates a 135 * new user object and adds it to the list of users for 136 * this session. User UID is assigned and returned. 137 * 138 * Transition T1 139 * 140 * Subsequent SMB Session Setup requests (on the same UID 141 * assigned in T0) update the state of this user object, 142 * communicating with smbd for the crypto work. 143 * 144 * Transition T2 145 * 146 * If the SMB Session Setup sequence is successful, T2 147 * makes the new user object available for requests. 148 * 149 * Transition T3 150 * 151 * If an Session Setup request gets an error other than 152 * the expected "more processing required", then T3 153 * leads to state "LOGGED_OFF" and then tear-down of the 154 * partially constructed user. 155 * 156 * Transition T4 157 * 158 * Normal SMB User Logoff request, or session tear-down. 159 * 160 * Transition T5 161 * 162 * This transition occurs in smb_user_release(). The resources associated 163 * with the user are deleted as well as the user. For the transition to 164 * occur, the user must be in the SMB_USER_STATE_LOGGED_OFF state and the 165 * reference count be zero. 166 * 167 * Comments 168 * -------- 169 * 170 * The state machine of the user structures is controlled by 3 elements: 171 * - The list of users of the session they belong to. 172 * - The mutex embedded in the structure itself. 173 * - The reference count. 174 * 175 * There's a mutex embedded in the user structure used to protect its fields 176 * and there's a lock embedded in the list of users of a session. To 177 * increment or to decrement the reference count the mutex must be entered. 178 * To insert the user into the list of users of the session and to remove 179 * the user from it, the lock must be entered in RW_WRITER mode. 180 * 181 * Rules of access to a user structure: 182 * 183 * 1) In order to avoid deadlocks, when both (mutex and lock of the session 184 * list) have to be entered, the lock must be entered first. 185 * 186 * 2) All actions applied to a user require a reference count. 187 * 188 * 3) There are 2 ways of getting a reference count. One is when the user 189 * logs in. The other when the user is looked up. 190 * 191 * It should be noted that the reference count of a user registers the 192 * number of references to the user in other structures (such as an smb 193 * request). The reference count is not incremented in these 2 instances: 194 * 195 * 1) The user is logged in. An user is anchored by their state. If there's 196 * no activity involving a user currently logged in, the reference 197 * count of that user is zero. 198 * 199 * 2) The user is queued in the list of users of the session. The fact of 200 * being queued in that list is NOT registered by incrementing the 201 * reference count. 202 */ 203 #include <sys/types.h> 204 #include <sys/sid.h> 205 #include <sys/priv_names.h> 206 #include <smbsrv/smb_kproto.h> 207 #include <smbsrv/smb_door.h> 208 209 #define ADMINISTRATORS_SID "S-1-5-32-544" 210 211 static int smb_user_enum_private(smb_user_t *, smb_svcenum_t *); 212 static void smb_user_auth_logoff(smb_user_t *); 213 214 215 /* 216 * Create a new user. 217 */ 218 smb_user_t * 219 smb_user_new(smb_session_t *session) 220 { 221 smb_user_t *user; 222 223 ASSERT(session); 224 ASSERT(session->s_magic == SMB_SESSION_MAGIC); 225 226 user = kmem_cache_alloc(smb_cache_user, KM_SLEEP); 227 bzero(user, sizeof (smb_user_t)); 228 229 user->u_refcnt = 1; 230 user->u_session = session; 231 user->u_server = session->s_server; 232 user->u_logon_time = gethrestime_sec(); 233 234 if (smb_idpool_alloc(&session->s_uid_pool, &user->u_uid)) 235 goto errout; 236 237 mutex_init(&user->u_mutex, NULL, MUTEX_DEFAULT, NULL); 238 user->u_state = SMB_USER_STATE_LOGGING_ON; 239 user->u_magic = SMB_USER_MAGIC; 240 241 smb_llist_enter(&session->s_user_list, RW_WRITER); 242 smb_llist_insert_tail(&session->s_user_list, user); 243 smb_llist_exit(&session->s_user_list); 244 smb_server_inc_users(session->s_server); 245 246 return (user); 247 248 errout: 249 if (user->u_uid != 0) 250 smb_idpool_free(&session->s_uid_pool, user->u_uid); 251 kmem_cache_free(smb_cache_user, user); 252 return (NULL); 253 } 254 255 /* 256 * Fill in the details of a user, meaning a transition 257 * from state LOGGING_ON to state LOGGED_ON. 258 */ 259 int 260 smb_user_logon( 261 smb_user_t *user, 262 cred_t *cr, 263 char *domain_name, 264 char *account_name, 265 uint32_t flags, 266 uint32_t privileges, 267 uint32_t audit_sid) 268 { 269 ksocket_t authsock = NULL; 270 271 ASSERT(user->u_magic == SMB_USER_MAGIC); 272 ASSERT(cr); 273 ASSERT(account_name); 274 ASSERT(domain_name); 275 276 mutex_enter(&user->u_mutex); 277 278 if (user->u_state != SMB_USER_STATE_LOGGING_ON) { 279 mutex_exit(&user->u_mutex); 280 return (-1); 281 } 282 283 /* 284 * In the transition from LOGGING_ON to LOGGED_ON, 285 * we always have an auth. socket to close. 286 */ 287 authsock = user->u_authsock; 288 ASSERT(authsock != NULL); 289 user->u_authsock = NULL; 290 291 user->u_state = SMB_USER_STATE_LOGGED_ON; 292 user->u_flags = flags; 293 user->u_name_len = strlen(account_name) + 1; 294 user->u_domain_len = strlen(domain_name) + 1; 295 user->u_name = smb_mem_strdup(account_name); 296 user->u_domain = smb_mem_strdup(domain_name); 297 user->u_audit_sid = audit_sid; 298 299 smb_user_setcred(user, cr, privileges); 300 301 mutex_exit(&user->u_mutex); 302 303 /* This close can block, so not under the mutex. */ 304 smb_authsock_close(user, authsock); 305 306 return (0); 307 } 308 309 /* 310 * smb_user_logoff 311 * 312 * Change the user state and disconnect trees. 313 * The user list must not be entered or modified here. 314 */ 315 void 316 smb_user_logoff( 317 smb_user_t *user) 318 { 319 ksocket_t authsock = NULL; 320 321 ASSERT(user->u_magic == SMB_USER_MAGIC); 322 323 mutex_enter(&user->u_mutex); 324 ASSERT(user->u_refcnt); 325 switch (user->u_state) { 326 case SMB_USER_STATE_LOGGING_ON: 327 authsock = user->u_authsock; 328 user->u_authsock = NULL; 329 user->u_state = SMB_USER_STATE_LOGGED_OFF; 330 smb_server_dec_users(user->u_server); 331 break; 332 333 case SMB_USER_STATE_LOGGED_ON: 334 /* 335 * The user is moved into a state indicating that the log off 336 * process has started. 337 */ 338 user->u_state = SMB_USER_STATE_LOGGING_OFF; 339 mutex_exit(&user->u_mutex); 340 smb_session_disconnect_owned_trees(user->u_session, user); 341 smb_user_auth_logoff(user); 342 mutex_enter(&user->u_mutex); 343 user->u_state = SMB_USER_STATE_LOGGED_OFF; 344 smb_server_dec_users(user->u_server); 345 break; 346 347 case SMB_USER_STATE_LOGGED_OFF: 348 case SMB_USER_STATE_LOGGING_OFF: 349 break; 350 351 default: 352 ASSERT(0); 353 break; 354 } 355 mutex_exit(&user->u_mutex); 356 357 /* This close can block, so not under the mutex. */ 358 if (authsock != NULL) { 359 smb_authsock_close(user, authsock); 360 } 361 } 362 363 /* 364 * Take a reference on a user. Do not return a reference unless the user is in 365 * the logged-in state. 366 */ 367 boolean_t 368 smb_user_hold(smb_user_t *user) 369 { 370 SMB_USER_VALID(user); 371 372 mutex_enter(&user->u_mutex); 373 374 if (user->u_state == SMB_USER_STATE_LOGGED_ON) { 375 user->u_refcnt++; 376 mutex_exit(&user->u_mutex); 377 return (B_TRUE); 378 } 379 380 mutex_exit(&user->u_mutex); 381 return (B_FALSE); 382 } 383 384 /* 385 * Unconditionally take a reference on a user. 386 */ 387 void 388 smb_user_hold_internal(smb_user_t *user) 389 { 390 SMB_USER_VALID(user); 391 392 mutex_enter(&user->u_mutex); 393 user->u_refcnt++; 394 mutex_exit(&user->u_mutex); 395 } 396 397 /* 398 * Release a reference on a user. If the reference count falls to 399 * zero and the user has logged off, post the object for deletion. 400 * Object deletion is deferred to avoid modifying a list while an 401 * iteration may be in progress. 402 */ 403 void 404 smb_user_release( 405 smb_user_t *user) 406 { 407 ASSERT(user->u_magic == SMB_USER_MAGIC); 408 409 mutex_enter(&user->u_mutex); 410 ASSERT(user->u_refcnt); 411 user->u_refcnt--; 412 413 switch (user->u_state) { 414 case SMB_USER_STATE_LOGGED_OFF: 415 if (user->u_refcnt == 0) 416 smb_session_post_user(user->u_session, user); 417 break; 418 419 case SMB_USER_STATE_LOGGING_ON: 420 case SMB_USER_STATE_LOGGED_ON: 421 case SMB_USER_STATE_LOGGING_OFF: 422 break; 423 424 default: 425 ASSERT(0); 426 break; 427 } 428 mutex_exit(&user->u_mutex); 429 } 430 431 /* 432 * Determine whether or not the user is an administrator. 433 * Members of the administrators group have administrative rights. 434 */ 435 boolean_t 436 smb_user_is_admin(smb_user_t *user) 437 { 438 #ifdef _KERNEL 439 char sidstr[SMB_SID_STRSZ]; 440 ksidlist_t *ksidlist; 441 ksid_t ksid1; 442 ksid_t *ksid2; 443 int i; 444 #endif /* _KERNEL */ 445 boolean_t rc = B_FALSE; 446 447 ASSERT(user); 448 ASSERT(user->u_cred); 449 450 if (SMB_USER_IS_ADMIN(user)) 451 return (B_TRUE); 452 453 #ifdef _KERNEL 454 bzero(&ksid1, sizeof (ksid_t)); 455 (void) strlcpy(sidstr, ADMINISTRATORS_SID, SMB_SID_STRSZ); 456 ASSERT(smb_sid_splitstr(sidstr, &ksid1.ks_rid) == 0); 457 ksid1.ks_domain = ksid_lookupdomain(sidstr); 458 459 ksidlist = crgetsidlist(user->u_cred); 460 ASSERT(ksidlist); 461 ASSERT(ksid1.ks_domain); 462 ASSERT(ksid1.ks_domain->kd_name); 463 464 i = 0; 465 ksid2 = crgetsid(user->u_cred, KSID_USER); 466 do { 467 ASSERT(ksid2->ks_domain); 468 ASSERT(ksid2->ks_domain->kd_name); 469 470 if (strcmp(ksid1.ks_domain->kd_name, 471 ksid2->ks_domain->kd_name) == 0 && 472 ksid1.ks_rid == ksid2->ks_rid) { 473 user->u_flags |= SMB_USER_FLAG_ADMIN; 474 rc = B_TRUE; 475 break; 476 } 477 478 ksid2 = &ksidlist->ksl_sids[i]; 479 } while (i++ < ksidlist->ksl_nsid); 480 481 ksid_rele(&ksid1); 482 #endif /* _KERNEL */ 483 return (rc); 484 } 485 486 /* 487 * This function should be called with a hold on the user. 488 */ 489 boolean_t 490 smb_user_namecmp(smb_user_t *user, const char *name) 491 { 492 char *fq_name; 493 boolean_t match; 494 495 if (smb_strcasecmp(name, user->u_name, 0) == 0) 496 return (B_TRUE); 497 498 fq_name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 499 500 (void) snprintf(fq_name, MAXNAMELEN, "%s\\%s", 501 user->u_domain, user->u_name); 502 503 match = (smb_strcasecmp(name, fq_name, 0) == 0); 504 if (!match) { 505 (void) snprintf(fq_name, MAXNAMELEN, "%s@%s", 506 user->u_name, user->u_domain); 507 508 match = (smb_strcasecmp(name, fq_name, 0) == 0); 509 } 510 511 kmem_free(fq_name, MAXNAMELEN); 512 return (match); 513 } 514 515 /* 516 * If the enumeration request is for user data, handle the request 517 * here. Otherwise, pass it on to the trees. 518 * 519 * This function should be called with a hold on the user. 520 */ 521 int 522 smb_user_enum(smb_user_t *user, smb_svcenum_t *svcenum) 523 { 524 int rc = 0; 525 526 ASSERT(user); 527 ASSERT(user->u_magic == SMB_USER_MAGIC); 528 529 if (svcenum->se_type == SMB_SVCENUM_TYPE_USER) 530 return (smb_user_enum_private(user, svcenum)); 531 532 return (rc); 533 } 534 535 /* *************************** Static Functions ***************************** */ 536 537 /* 538 * Delete a user. The tree list should be empty. 539 * 540 * Remove the user from the session's user list before freeing resources 541 * associated with the user. 542 */ 543 void 544 smb_user_delete(void *arg) 545 { 546 smb_session_t *session; 547 smb_user_t *user = (smb_user_t *)arg; 548 549 SMB_USER_VALID(user); 550 ASSERT(user->u_refcnt == 0); 551 ASSERT(user->u_state == SMB_USER_STATE_LOGGED_OFF); 552 ASSERT(user->u_authsock == NULL); 553 554 session = user->u_session; 555 smb_llist_enter(&session->s_user_list, RW_WRITER); 556 smb_llist_remove(&session->s_user_list, user); 557 smb_idpool_free(&session->s_uid_pool, user->u_uid); 558 smb_llist_exit(&session->s_user_list); 559 560 mutex_enter(&user->u_mutex); 561 mutex_exit(&user->u_mutex); 562 563 user->u_magic = (uint32_t)~SMB_USER_MAGIC; 564 mutex_destroy(&user->u_mutex); 565 if (user->u_cred) 566 crfree(user->u_cred); 567 if (user->u_privcred) 568 crfree(user->u_privcred); 569 smb_mem_free(user->u_name); 570 smb_mem_free(user->u_domain); 571 kmem_cache_free(smb_cache_user, user); 572 } 573 574 cred_t * 575 smb_user_getcred(smb_user_t *user) 576 { 577 return (user->u_cred); 578 } 579 580 cred_t * 581 smb_user_getprivcred(smb_user_t *user) 582 { 583 return ((user->u_privcred)? user->u_privcred : user->u_cred); 584 } 585 586 #ifdef _KERNEL 587 /* 588 * Assign the user cred and privileges. 589 * 590 * If the user has backup and/or restore privleges, dup the cred 591 * and add those privileges to this new privileged cred. 592 */ 593 void 594 smb_user_setcred(smb_user_t *user, cred_t *cr, uint32_t privileges) 595 { 596 cred_t *privcred = NULL; 597 598 ASSERT(cr); 599 crhold(cr); 600 601 if (privileges & (SMB_USER_PRIV_BACKUP | SMB_USER_PRIV_RESTORE)) 602 privcred = crdup(cr); 603 604 if (privcred != NULL) { 605 if (privileges & SMB_USER_PRIV_BACKUP) { 606 (void) crsetpriv(privcred, PRIV_FILE_DAC_READ, 607 PRIV_FILE_DAC_SEARCH, PRIV_SYS_MOUNT, NULL); 608 } 609 610 if (privileges & SMB_USER_PRIV_RESTORE) { 611 (void) crsetpriv(privcred, PRIV_FILE_DAC_WRITE, 612 PRIV_FILE_CHOWN, PRIV_FILE_CHOWN_SELF, 613 PRIV_FILE_DAC_SEARCH, PRIV_FILE_LINK_ANY, 614 PRIV_FILE_OWNER, PRIV_FILE_SETID, 615 PRIV_SYS_LINKDIR, PRIV_SYS_MOUNT, NULL); 616 } 617 } 618 619 user->u_cred = cr; 620 user->u_privcred = privcred; 621 user->u_privileges = privileges; 622 } 623 #endif /* _KERNEL */ 624 625 /* 626 * Private function to support smb_user_enum. 627 */ 628 static int 629 smb_user_enum_private(smb_user_t *user, smb_svcenum_t *svcenum) 630 { 631 uint8_t *pb; 632 uint_t nbytes; 633 int rc; 634 635 if (svcenum->se_nskip > 0) { 636 svcenum->se_nskip--; 637 return (0); 638 } 639 640 if (svcenum->se_nitems >= svcenum->se_nlimit) { 641 svcenum->se_nitems = svcenum->se_nlimit; 642 return (0); 643 } 644 645 pb = &svcenum->se_buf[svcenum->se_bused]; 646 rc = smb_user_netinfo_encode(user, pb, svcenum->se_bavail, &nbytes); 647 if (rc == 0) { 648 svcenum->se_bavail -= nbytes; 649 svcenum->se_bused += nbytes; 650 svcenum->se_nitems++; 651 } 652 653 return (rc); 654 } 655 656 /* 657 * Encode the NetInfo for a user into a buffer. NetInfo contains 658 * information that is often needed in user space to support RPC 659 * requests. 660 */ 661 int 662 smb_user_netinfo_encode(smb_user_t *user, uint8_t *buf, size_t buflen, 663 uint32_t *nbytes) 664 { 665 smb_netuserinfo_t info; 666 int rc; 667 668 smb_user_netinfo_init(user, &info); 669 rc = smb_netuserinfo_encode(&info, buf, buflen, nbytes); 670 smb_user_netinfo_fini(&info); 671 672 return (rc); 673 } 674 675 void 676 smb_user_netinfo_init(smb_user_t *user, smb_netuserinfo_t *info) 677 { 678 smb_session_t *session; 679 char *buf; 680 681 ASSERT(user); 682 ASSERT(user->u_domain); 683 ASSERT(user->u_name); 684 685 session = user->u_session; 686 ASSERT(session); 687 ASSERT(session->workstation); 688 689 info->ui_session_id = session->s_kid; 690 info->ui_native_os = session->native_os; 691 info->ui_ipaddr = session->ipaddr; 692 info->ui_numopens = session->s_file_cnt; 693 info->ui_smb_uid = user->u_uid; 694 info->ui_logon_time = user->u_logon_time; 695 info->ui_flags = user->u_flags; 696 info->ui_posix_uid = crgetuid(user->u_cred); 697 698 info->ui_domain_len = user->u_domain_len; 699 info->ui_domain = smb_mem_strdup(user->u_domain); 700 701 info->ui_account_len = user->u_name_len; 702 info->ui_account = smb_mem_strdup(user->u_name); 703 704 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 705 smb_session_getclient(session, buf, MAXNAMELEN); 706 info->ui_workstation_len = strlen(buf) + 1; 707 info->ui_workstation = smb_mem_strdup(buf); 708 kmem_free(buf, MAXNAMELEN); 709 } 710 711 void 712 smb_user_netinfo_fini(smb_netuserinfo_t *info) 713 { 714 if (info == NULL) 715 return; 716 717 if (info->ui_domain) 718 smb_mem_free(info->ui_domain); 719 if (info->ui_account) 720 smb_mem_free(info->ui_account); 721 if (info->ui_workstation) 722 smb_mem_free(info->ui_workstation); 723 724 bzero(info, sizeof (smb_netuserinfo_t)); 725 } 726 727 static void 728 smb_user_auth_logoff(smb_user_t *user) 729 { 730 uint32_t audit_sid = user->u_audit_sid; 731 732 (void) smb_kdoor_upcall(user->u_server, SMB_DR_USER_AUTH_LOGOFF, 733 &audit_sid, xdr_uint32_t, NULL, NULL); 734 } 735