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