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) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2016 by Delphix. All rights reserved. 24 * Copyright 2018 Nexenta Systems, Inc. All rights reserved. 25 */ 26 27 #include <unistd.h> 28 #include <strings.h> 29 #include <pwd.h> 30 #include <grp.h> 31 #include <time.h> 32 #include <syslog.h> 33 #include <assert.h> 34 #include <synch.h> 35 36 #include <smbsrv/libsmb.h> 37 #include <smbsrv/libmlsvc.h> 38 39 #include <smbsrv/smbinfo.h> 40 #include <smbsrv/smb_token.h> 41 #include <lsalib.h> 42 43 static smb_account_t smb_guest; 44 static smb_account_t smb_domusers; 45 static rwlock_t smb_logoninit_rwl; 46 47 typedef void (*smb_logonop_t)(smb_logon_t *, smb_token_t *); 48 49 static void smb_logon_local(smb_logon_t *, smb_token_t *); 50 static void smb_logon_guest(smb_logon_t *, smb_token_t *); 51 static void smb_logon_anon(smb_logon_t *, smb_token_t *); 52 53 static uint32_t smb_token_auth_local(smb_logon_t *, smb_token_t *, 54 smb_passwd_t *); 55 56 static uint32_t smb_token_setup_local(smb_passwd_t *, smb_token_t *); 57 static uint32_t smb_token_setup_guest(smb_logon_t *, smb_token_t *); 58 static uint32_t smb_token_setup_anon(smb_token_t *token); 59 60 static boolean_t smb_token_is_member(smb_token_t *, smb_sid_t *); 61 static uint32_t smb_token_setup_wingrps(smb_token_t *); 62 static smb_posix_grps_t *smb_token_create_pxgrps(uid_t); 63 64 static void smb_guest_account(char *, size_t); 65 66 /* Consolidation private function from Network Repository */ 67 extern int _getgroupsbymember(const char *, gid_t[], int, int); 68 69 static idmap_stat 70 smb_token_idmap(smb_token_t *token, smb_idmap_batch_t *sib) 71 { 72 idmap_stat stat; 73 smb_idmap_t *sim; 74 smb_id_t *id; 75 int i; 76 77 if (!token || !sib) 78 return (IDMAP_ERR_ARG); 79 80 sim = sib->sib_maps; 81 82 if (token->tkn_flags & SMB_ATF_ANON) { 83 token->tkn_user.i_id = UID_NOBODY; 84 token->tkn_owner.i_id = UID_NOBODY; 85 } else { 86 /* User SID */ 87 id = &token->tkn_user; 88 sim->sim_id = &id->i_id; 89 stat = smb_idmap_batch_getid(sib->sib_idmaph, sim++, 90 id->i_sid, SMB_IDMAP_USER); 91 92 if (stat != IDMAP_SUCCESS) 93 return (stat); 94 95 /* Owner SID */ 96 id = &token->tkn_owner; 97 sim->sim_id = &id->i_id; 98 stat = smb_idmap_batch_getid(sib->sib_idmaph, sim++, 99 id->i_sid, SMB_IDMAP_USER); 100 101 if (stat != IDMAP_SUCCESS) 102 return (stat); 103 } 104 105 /* Primary Group SID */ 106 id = &token->tkn_primary_grp; 107 sim->sim_id = &id->i_id; 108 stat = smb_idmap_batch_getid(sib->sib_idmaph, sim++, id->i_sid, 109 SMB_IDMAP_GROUP); 110 111 if (stat != IDMAP_SUCCESS) 112 return (stat); 113 114 /* Other Windows Group SIDs */ 115 for (i = 0; i < token->tkn_win_grps.i_cnt; i++, sim++) { 116 id = &token->tkn_win_grps.i_ids[i]; 117 sim->sim_id = &id->i_id; 118 stat = smb_idmap_batch_getid(sib->sib_idmaph, sim, 119 id->i_sid, SMB_IDMAP_GROUP); 120 121 if (stat != IDMAP_SUCCESS) 122 break; 123 } 124 125 return (stat); 126 } 127 128 /* 129 * smb_token_sids2ids 130 * 131 * This will map all the SIDs of the access token to UIDs/GIDs. 132 * However, if there are some SIDs we can't map to UIDs/GIDs, 133 * we don't want to fail the logon, and instead just log the 134 * SIDs we could not map and continue as best we can. 135 * The flag SMB_IDMAP_SKIP_ERRS below does that. 136 * 137 * Returns 0 upon success. Otherwise, returns -1. 138 */ 139 static int 140 smb_token_sids2ids(smb_token_t *token) 141 { 142 idmap_stat stat; 143 int nmaps; 144 smb_idmap_batch_t sib; 145 146 /* 147 * Number of idmap lookups: user SID, owner SID, primary group SID, 148 * and all Windows group SIDs. Skip user/owner SID for Anonymous. 149 */ 150 if (token->tkn_flags & SMB_ATF_ANON) 151 nmaps = token->tkn_win_grps.i_cnt + 1; 152 else 153 nmaps = token->tkn_win_grps.i_cnt + 3; 154 155 stat = smb_idmap_batch_create(&sib, nmaps, 156 SMB_IDMAP_SID2ID | SMB_IDMAP_SKIP_ERRS); 157 if (stat != IDMAP_SUCCESS) 158 return (-1); 159 160 stat = smb_token_idmap(token, &sib); 161 if (stat != IDMAP_SUCCESS) { 162 smb_idmap_batch_destroy(&sib); 163 return (-1); 164 } 165 166 stat = smb_idmap_batch_getmappings(&sib); 167 smb_idmap_check("smb_idmap_batch_getmappings", stat); 168 smb_idmap_batch_destroy(&sib); 169 170 return (stat == IDMAP_SUCCESS ? 0 : -1); 171 } 172 173 /* 174 * smb_token_create_pxgrps 175 * 176 * Setup the POSIX group membership of the access token if the given UID is 177 * a POSIX UID (non-ephemeral). Both the user's primary group and 178 * supplementary groups will be added to the POSIX group array of the access 179 * token. 180 */ 181 static smb_posix_grps_t * 182 smb_token_create_pxgrps(uid_t uid) 183 { 184 struct passwd *pwd; 185 smb_posix_grps_t *pgrps; 186 int ngroups_max, num; 187 gid_t *gids; 188 189 if ((ngroups_max = sysconf(_SC_NGROUPS_MAX)) < 0) { 190 syslog(LOG_ERR, "smb_logon: failed to get _SC_NGROUPS_MAX"); 191 return (NULL); 192 } 193 194 pwd = getpwuid(uid); 195 if (pwd == NULL) { 196 pgrps = malloc(sizeof (smb_posix_grps_t)); 197 if (pgrps == NULL) 198 return (NULL); 199 200 pgrps->pg_ngrps = 0; 201 return (pgrps); 202 } 203 204 if (pwd->pw_name == NULL) { 205 pgrps = malloc(sizeof (smb_posix_grps_t)); 206 if (pgrps == NULL) 207 return (NULL); 208 209 pgrps->pg_ngrps = 1; 210 pgrps->pg_grps[0] = pwd->pw_gid; 211 return (pgrps); 212 } 213 214 gids = (gid_t *)malloc(ngroups_max * sizeof (gid_t)); 215 if (gids == NULL) { 216 return (NULL); 217 } 218 bzero(gids, ngroups_max * sizeof (gid_t)); 219 220 gids[0] = pwd->pw_gid; 221 222 /* 223 * Setup the groups starting at index 1 (the last arg) 224 * of gids array. 225 */ 226 num = _getgroupsbymember(pwd->pw_name, gids, ngroups_max, 1); 227 228 if (num == -1) { 229 syslog(LOG_ERR, "smb_logon: unable " 230 "to get user's supplementary groups"); 231 num = 1; 232 } 233 234 pgrps = (smb_posix_grps_t *)malloc(SMB_POSIX_GRPS_SIZE(num)); 235 if (pgrps) { 236 pgrps->pg_ngrps = num; 237 bcopy(gids, pgrps->pg_grps, num * sizeof (gid_t)); 238 } 239 240 free(gids); 241 return (pgrps); 242 } 243 244 /* 245 * smb_token_destroy 246 * 247 * Release all of the memory associated with a token structure. Ensure 248 * that the token has been unlinked before calling. 249 */ 250 void 251 smb_token_destroy(smb_token_t *token) 252 { 253 if (token != NULL) { 254 smb_sid_free(token->tkn_user.i_sid); 255 smb_sid_free(token->tkn_owner.i_sid); 256 smb_sid_free(token->tkn_primary_grp.i_sid); 257 smb_ids_free(&token->tkn_win_grps); 258 smb_privset_free(token->tkn_privileges); 259 free(token->tkn_posix_grps); 260 free(token->tkn_account_name); 261 free(token->tkn_domain_name); 262 free(token->tkn_ssnkey.val); 263 bzero(token, sizeof (smb_token_t)); 264 free(token); 265 } 266 } 267 268 /* 269 * Token owner should be set to local Administrators group 270 * in two cases: 271 * 1. The logged on user is a member of Domain Admins group 272 * 2. They are a member of local Administrators group 273 */ 274 static void 275 smb_token_set_owner(smb_token_t *token) 276 { 277 #ifdef SMB_SUPPORT_GROUP_OWNER 278 smb_sid_t *owner_sid; 279 280 if (token->tkn_flags & SMB_ATF_ADMIN) { 281 owner_sid = smb_wka_get_sid("Administrators"); 282 assert(owner_sid); 283 } else { 284 owner_sid = token->tkn_user->i_sid; 285 } 286 287 token->tkn_owner.i_sid = smb_sid_dup(owner_sid); 288 #endif 289 token->tkn_owner.i_sid = smb_sid_dup(token->tkn_user.i_sid); 290 } 291 292 static smb_privset_t * 293 smb_token_create_privs(smb_token_t *token) 294 { 295 smb_privset_t *privs; 296 smb_giter_t gi; 297 smb_group_t grp; 298 int rc; 299 300 privs = smb_privset_new(); 301 if (privs == NULL) 302 return (NULL); 303 304 if (smb_lgrp_iteropen(&gi) != SMB_LGRP_SUCCESS) { 305 smb_privset_free(privs); 306 return (NULL); 307 } 308 309 while (smb_lgrp_iterate(&gi, &grp) == SMB_LGRP_SUCCESS) { 310 if (smb_lgrp_is_member(&grp, token->tkn_user.i_sid)) 311 smb_privset_merge(privs, grp.sg_privs); 312 smb_lgrp_free(&grp); 313 } 314 smb_lgrp_iterclose(&gi); 315 316 if (token->tkn_flags & SMB_ATF_ADMIN) { 317 char admgrp[] = "Administrators"; 318 319 rc = smb_lgrp_getbyname(admgrp, &grp); 320 if (rc == SMB_LGRP_SUCCESS) { 321 smb_privset_merge(privs, grp.sg_privs); 322 smb_lgrp_free(&grp); 323 } 324 325 /* 326 * This privilege is required to view/edit SACL 327 */ 328 smb_privset_enable(privs, SE_SECURITY_LUID); 329 } 330 331 return (privs); 332 } 333 334 static void 335 smb_token_set_flags(smb_token_t *token) 336 { 337 if (smb_token_is_member(token, smb_wka_get_sid("Administrators"))) 338 token->tkn_flags |= SMB_ATF_ADMIN; 339 340 if (smb_token_is_member(token, smb_wka_get_sid("Power Users"))) 341 token->tkn_flags |= SMB_ATF_POWERUSER; 342 343 if (smb_token_is_member(token, smb_wka_get_sid("Backup Operators"))) 344 token->tkn_flags |= SMB_ATF_BACKUPOP; 345 } 346 347 /* 348 * Common token setup for both local and domain users. 349 * This function must be called after the initial setup 350 * has been done. 351 * 352 * Note that the order of calls in this function are important. 353 * 354 * Returns B_TRUE for success. 355 */ 356 boolean_t 357 smb_token_setup_common(smb_token_t *token) 358 { 359 smb_token_set_flags(token); 360 361 smb_token_set_owner(token); 362 if (token->tkn_owner.i_sid == NULL) 363 return (B_FALSE); 364 365 /* Privileges */ 366 token->tkn_privileges = smb_token_create_privs(token); 367 if (token->tkn_privileges == NULL) 368 return (B_FALSE); 369 370 if (smb_token_sids2ids(token) != 0) { 371 syslog(LOG_ERR, "%s\\%s: idmap failed", 372 token->tkn_domain_name, token->tkn_account_name); 373 return (B_FALSE); 374 } 375 376 /* Solaris Groups */ 377 token->tkn_posix_grps = smb_token_create_pxgrps(token->tkn_user.i_id); 378 379 return (smb_token_valid(token)); 380 } 381 382 uint32_t 383 smb_logon_init(void) 384 { 385 uint32_t status; 386 387 (void) rw_wrlock(&smb_logoninit_rwl); 388 status = smb_sam_lookup_name(NULL, "guest", SidTypeUser, &smb_guest); 389 if (status != NT_STATUS_SUCCESS) { 390 (void) rw_unlock(&smb_logoninit_rwl); 391 return (status); 392 } 393 394 status = smb_sam_lookup_name(NULL, "domain users", SidTypeGroup, 395 &smb_domusers); 396 if (status != NT_STATUS_SUCCESS) { 397 smb_account_free(&smb_guest); 398 bzero(&smb_guest, sizeof (smb_account_t)); 399 (void) rw_unlock(&smb_logoninit_rwl); 400 return (status); 401 } 402 403 (void) rw_unlock(&smb_logoninit_rwl); 404 return (status); 405 } 406 407 void 408 smb_logon_fini(void) 409 { 410 (void) rw_wrlock(&smb_logoninit_rwl); 411 smb_account_free(&smb_guest); 412 smb_account_free(&smb_domusers); 413 bzero(&smb_guest, sizeof (smb_account_t)); 414 bzero(&smb_domusers, sizeof (smb_account_t)); 415 (void) rw_unlock(&smb_logoninit_rwl); 416 } 417 418 /* 419 * Perform user authentication. 420 * 421 * The dispatched functions must only update the user_info status if they 422 * attempt to authenticate the user. 423 * 424 * On success, a pointer to a new access token is returned. 425 * On failure, NULL return and status in user_info->lg_status 426 */ 427 smb_token_t * 428 smb_logon(smb_logon_t *user_info) 429 { 430 static smb_logonop_t ops[] = { 431 smb_logon_anon, 432 smb_logon_local, 433 smb_logon_domain, 434 smb_logon_guest 435 }; 436 smb_token_t *token = NULL; 437 smb_domain_t domain; 438 int n_op = (sizeof (ops) / sizeof (ops[0])); 439 int i; 440 441 user_info->lg_secmode = smb_config_get_secmode(); 442 443 if (smb_domain_lookup_name(user_info->lg_e_domain, &domain)) 444 user_info->lg_domain_type = domain.di_type; 445 else 446 user_info->lg_domain_type = SMB_DOMAIN_NULL; 447 448 if ((token = calloc(1, sizeof (smb_token_t))) == NULL) { 449 syslog(LOG_ERR, "logon[%s\\%s]: %m", 450 user_info->lg_e_domain, user_info->lg_e_username); 451 return (NULL); 452 } 453 454 /* 455 * If any logonop function takes significant action 456 * (logon or authoratative failure) it will change 457 * this status field to something else. 458 */ 459 user_info->lg_status = NT_STATUS_NO_SUCH_USER; 460 for (i = 0; i < n_op; ++i) { 461 (*ops[i])(user_info, token); 462 463 if (user_info->lg_status == NT_STATUS_SUCCESS) 464 break; 465 } 466 467 if (user_info->lg_status == NT_STATUS_SUCCESS) { 468 if (smb_token_setup_common(token)) 469 return (token); /* success */ 470 /* 471 * (else) smb_token_setup_common failed, which usually 472 * means smb_token_sids2ids() failed to map some SIDs to 473 * Unix IDs. This indicates an idmap config problem. 474 */ 475 user_info->lg_status = NT_STATUS_INTERNAL_ERROR; 476 } 477 478 smb_token_destroy(token); 479 480 /* 481 * Any unknown user or bad password should result in 482 * NT_STATUS_LOGON_FAILURE (so we don't give hints). 483 */ 484 if (user_info->lg_status == NT_STATUS_NO_SUCH_USER || 485 user_info->lg_status == NT_STATUS_WRONG_PASSWORD) 486 user_info->lg_status = NT_STATUS_LOGON_FAILURE; 487 488 return (NULL); 489 } 490 491 /* 492 * If the user has an entry in the local database, attempt local authentication. 493 * 494 * In domain mode, we try to exclude domain accounts, which we do by only 495 * accepting local or null (blank) domain names here. Some clients (Mac OS) 496 * don't always send the domain name. 497 * 498 * If we are not going to attempt authentication, this function must return 499 * without updating the status. 500 */ 501 static void 502 smb_logon_local(smb_logon_t *user_info, smb_token_t *token) 503 { 504 char guest[SMB_USERNAME_MAXLEN]; 505 smb_passwd_t smbpw; 506 uint32_t status; 507 508 if (user_info->lg_secmode == SMB_SECMODE_DOMAIN) { 509 if ((user_info->lg_domain_type != SMB_DOMAIN_LOCAL) && 510 (user_info->lg_domain_type != SMB_DOMAIN_NULL)) 511 return; 512 } 513 514 /* 515 * If the requested account name is "guest" (or whatever 516 * our guest account is named) then don't handle it here. 517 * Let this request fall through to smb_logon_guest(). 518 */ 519 smb_guest_account(guest, SMB_USERNAME_MAXLEN); 520 if (smb_strcasecmp(guest, user_info->lg_e_username, 0) == 0) 521 return; 522 523 status = smb_token_auth_local(user_info, token, &smbpw); 524 if (status == NT_STATUS_SUCCESS) 525 status = smb_token_setup_local(&smbpw, token); 526 527 user_info->lg_status = status; 528 } 529 530 /* 531 * Guest authentication. This may be a local guest account or the guest 532 * account may be mapped to a local account. These accounts are regular 533 * accounts with normal password protection. 534 * 535 * Only proceed with a guest logon if previous logon options have resulted 536 * in NO_SUCH_USER. 537 * 538 * If we are not going to attempt authentication, this function must return 539 * without updating the status. 540 */ 541 static void 542 smb_logon_guest(smb_logon_t *user_info, smb_token_t *token) 543 { 544 char guest[SMB_USERNAME_MAXLEN]; 545 smb_passwd_t smbpw; 546 char *temp; 547 548 if (user_info->lg_status != NT_STATUS_NO_SUCH_USER) 549 return; 550 551 /* Get the name of the guest account. */ 552 smb_guest_account(guest, SMB_USERNAME_MAXLEN); 553 554 /* Does the guest account exist? */ 555 if (smb_pwd_getpwnam(guest, &smbpw) == NULL) 556 return; 557 558 /* Is it enabled? (empty p/w is OK) */ 559 if (smbpw.pw_flags & SMB_PWF_DISABLE) 560 return; 561 562 /* 563 * OK, give the client a guest logon. Note that on entry, 564 * lg_e_username is typically something other than "guest" 565 * so we need to set the effective username when createing 566 * the guest token. 567 */ 568 temp = user_info->lg_e_username; 569 user_info->lg_e_username = guest; 570 user_info->lg_status = smb_token_setup_guest(user_info, token); 571 user_info->lg_e_username = temp; 572 } 573 574 /* 575 * If user_info represents an anonymous user then setup the token. 576 * Otherwise return without updating the status. 577 */ 578 static void 579 smb_logon_anon(smb_logon_t *user_info, smb_token_t *token) 580 { 581 if (user_info->lg_flags & SMB_ATF_ANON) 582 user_info->lg_status = smb_token_setup_anon(token); 583 } 584 585 /* 586 * Try both LM hash and NT hashes with user's password(s) to authenticate 587 * the user. 588 */ 589 static uint32_t 590 smb_token_auth_local(smb_logon_t *user_info, smb_token_t *token, 591 smb_passwd_t *smbpw) 592 { 593 boolean_t ok; 594 uint32_t status = NT_STATUS_SUCCESS; 595 596 if (smb_pwd_getpwnam(user_info->lg_e_username, smbpw) == NULL) 597 return (NT_STATUS_NO_SUCH_USER); 598 599 if (smbpw->pw_flags & SMB_PWF_DISABLE) 600 return (NT_STATUS_ACCOUNT_DISABLED); 601 602 if ((smbpw->pw_flags & (SMB_PWF_LM | SMB_PWF_NT)) == 0) { 603 /* 604 * The SMB passwords have not been set. 605 * Return an error that suggests the 606 * password needs to be set. 607 */ 608 return (NT_STATUS_PASSWORD_EXPIRED); 609 } 610 611 token->tkn_ssnkey.val = malloc(SMBAUTH_SESSION_KEY_SZ); 612 if (token->tkn_ssnkey.val == NULL) 613 return (NT_STATUS_NO_MEMORY); 614 token->tkn_ssnkey.len = SMBAUTH_SESSION_KEY_SZ; 615 616 ok = smb_auth_validate( 617 smbpw, 618 user_info->lg_domain, 619 user_info->lg_username, 620 user_info->lg_challenge_key.val, 621 user_info->lg_challenge_key.len, 622 user_info->lg_nt_password.val, 623 user_info->lg_nt_password.len, 624 user_info->lg_lm_password.val, 625 user_info->lg_lm_password.len, 626 token->tkn_ssnkey.val); 627 if (ok) 628 return (NT_STATUS_SUCCESS); 629 630 free(token->tkn_ssnkey.val); 631 token->tkn_ssnkey.val = NULL; 632 token->tkn_ssnkey.len = 0; 633 634 status = NT_STATUS_WRONG_PASSWORD; 635 syslog(LOG_NOTICE, "logon[%s\\%s]: %s", 636 user_info->lg_e_domain, user_info->lg_e_username, 637 xlate_nt_status(status)); 638 639 return (status); 640 } 641 642 /* 643 * Setup an access token for the specified local user. 644 */ 645 static uint32_t 646 smb_token_setup_local(smb_passwd_t *smbpw, smb_token_t *token) 647 { 648 idmap_stat stat; 649 smb_idmap_batch_t sib; 650 smb_idmap_t *umap, *gmap; 651 struct passwd pw; 652 char pwbuf[1024]; 653 char nbname[NETBIOS_NAME_SZ]; 654 655 (void) smb_getnetbiosname(nbname, sizeof (nbname)); 656 token->tkn_account_name = strdup(smbpw->pw_name); 657 token->tkn_domain_name = strdup(nbname); 658 659 if (token->tkn_account_name == NULL || 660 token->tkn_domain_name == NULL) 661 return (NT_STATUS_NO_MEMORY); 662 663 if (getpwuid_r(smbpw->pw_uid, &pw, pwbuf, sizeof (pwbuf)) == NULL) 664 return (NT_STATUS_NO_SUCH_USER); 665 666 /* Get the SID for user's uid & gid */ 667 stat = smb_idmap_batch_create(&sib, 2, SMB_IDMAP_ID2SID); 668 if (stat != IDMAP_SUCCESS) 669 return (NT_STATUS_INTERNAL_ERROR); 670 671 umap = &sib.sib_maps[0]; 672 stat = smb_idmap_batch_getsid(sib.sib_idmaph, umap, pw.pw_uid, 673 SMB_IDMAP_USER); 674 675 if (stat != IDMAP_SUCCESS) { 676 smb_idmap_batch_destroy(&sib); 677 return (NT_STATUS_INTERNAL_ERROR); 678 } 679 680 gmap = &sib.sib_maps[1]; 681 stat = smb_idmap_batch_getsid(sib.sib_idmaph, gmap, pw.pw_gid, 682 SMB_IDMAP_GROUP); 683 684 if (stat != IDMAP_SUCCESS) { 685 smb_idmap_batch_destroy(&sib); 686 return (NT_STATUS_INTERNAL_ERROR); 687 } 688 689 if (smb_idmap_batch_getmappings(&sib) != IDMAP_SUCCESS) 690 return (NT_STATUS_INTERNAL_ERROR); 691 692 token->tkn_user.i_sid = smb_sid_dup(umap->sim_sid); 693 token->tkn_primary_grp.i_sid = smb_sid_dup(gmap->sim_sid); 694 695 smb_idmap_batch_destroy(&sib); 696 697 if (token->tkn_user.i_sid == NULL || 698 token->tkn_primary_grp.i_sid == NULL) 699 return (NT_STATUS_NO_MEMORY); 700 701 return (smb_token_setup_wingrps(token)); 702 } 703 704 /* 705 * Setup access token for guest connections 706 */ 707 static uint32_t 708 smb_token_setup_guest(smb_logon_t *user_info, smb_token_t *token) 709 { 710 token->tkn_account_name = strdup(user_info->lg_e_username); 711 712 (void) rw_rdlock(&smb_logoninit_rwl); 713 token->tkn_domain_name = strdup(smb_guest.a_domain); 714 token->tkn_user.i_sid = smb_sid_dup(smb_guest.a_sid); 715 token->tkn_primary_grp.i_sid = smb_sid_dup(smb_domusers.a_sid); 716 (void) rw_unlock(&smb_logoninit_rwl); 717 token->tkn_flags = SMB_ATF_GUEST; 718 719 if (token->tkn_account_name == NULL || 720 token->tkn_domain_name == NULL || 721 token->tkn_user.i_sid == NULL || 722 token->tkn_primary_grp.i_sid == NULL) 723 return (NT_STATUS_NO_MEMORY); 724 725 return (smb_token_setup_wingrps(token)); 726 } 727 728 /* 729 * Setup access token for anonymous connections 730 */ 731 static uint32_t 732 smb_token_setup_anon(smb_token_t *token) 733 { 734 smb_sid_t *user_sid; 735 736 token->tkn_account_name = strdup("Anonymous"); 737 token->tkn_domain_name = strdup("NT Authority"); 738 user_sid = smb_wka_get_sid("Anonymous"); 739 token->tkn_user.i_sid = smb_sid_dup(user_sid); 740 token->tkn_primary_grp.i_sid = smb_sid_dup(user_sid); 741 token->tkn_flags = SMB_ATF_ANON; 742 743 if (token->tkn_account_name == NULL || 744 token->tkn_domain_name == NULL || 745 token->tkn_user.i_sid == NULL || 746 token->tkn_primary_grp.i_sid == NULL) 747 return (NT_STATUS_NO_MEMORY); 748 749 return (smb_token_setup_wingrps(token)); 750 } 751 752 /* 753 * smb_token_user_sid 754 * 755 * Return a pointer to the user SID in the specified token. A null 756 * pointer indicates an error. 757 */ 758 static smb_sid_t * 759 smb_token_user_sid(smb_token_t *token) 760 { 761 return ((token) ? token->tkn_user.i_sid : NULL); 762 } 763 764 /* 765 * smb_token_group_sid 766 * 767 * Return a pointer to the group SID as indicated by the iterator. 768 * Setting the iterator to 0 before calling this function will return 769 * the first group, which will always be the primary group. The 770 * iterator will be incremented before returning the SID so that this 771 * function can be used to cycle through the groups. The caller can 772 * adjust the iterator as required between calls to obtain any specific 773 * group. 774 * 775 * On success a pointer to the appropriate group SID will be returned. 776 * Otherwise a null pointer will be returned. 777 */ 778 static smb_sid_t * 779 smb_token_group_sid(smb_token_t *token, int *iterator) 780 { 781 int index; 782 783 if (token == NULL || iterator == NULL) 784 return (NULL); 785 786 if (token->tkn_win_grps.i_ids == NULL) 787 return (NULL); 788 789 index = *iterator; 790 791 if (index < 0 || index >= token->tkn_win_grps.i_cnt) 792 return (NULL); 793 794 ++(*iterator); 795 return (token->tkn_win_grps.i_ids[index].i_sid); 796 } 797 798 /* 799 * smb_token_is_member 800 * 801 * This function will determine whether or not the specified SID is a 802 * member of a token. The user SID and all group SIDs are tested. 803 * Returns 1 if the SID is a member of the token. Otherwise returns 0. 804 */ 805 static boolean_t 806 smb_token_is_member(smb_token_t *token, smb_sid_t *sid) 807 { 808 smb_sid_t *tsid; 809 int iterator = 0; 810 811 if (token == NULL || sid == NULL) 812 return (B_FALSE); 813 814 tsid = smb_token_user_sid(token); 815 while (tsid) { 816 if (smb_sid_cmp(tsid, sid)) 817 return (B_TRUE); 818 819 tsid = smb_token_group_sid(token, &iterator); 820 } 821 822 return (B_FALSE); 823 } 824 825 /* 826 * smb_token_log 827 * 828 * Diagnostic routine to write the contents of a token to the log. 829 */ 830 void 831 smb_token_log(smb_token_t *token) 832 { 833 smb_ids_t *w_grps; 834 smb_id_t *grp; 835 smb_posix_grps_t *x_grps; 836 char sidstr[SMB_SID_STRSZ]; 837 int i; 838 839 if (token == NULL) 840 return; 841 842 syslog(LOG_DEBUG, "Token for %s\\%s", 843 (token->tkn_domain_name) ? token->tkn_domain_name : "-NULL-", 844 (token->tkn_account_name) ? token->tkn_account_name : "-NULL-"); 845 846 syslog(LOG_DEBUG, " User->Attr: %d", token->tkn_user.i_attrs); 847 smb_sid_tostr((smb_sid_t *)token->tkn_user.i_sid, sidstr); 848 syslog(LOG_DEBUG, " User->Sid: %s (id=%u)", sidstr, 849 token->tkn_user.i_id); 850 851 smb_sid_tostr((smb_sid_t *)token->tkn_owner.i_sid, sidstr); 852 syslog(LOG_DEBUG, " Ownr->Sid: %s (id=%u)", 853 sidstr, token->tkn_owner.i_id); 854 855 smb_sid_tostr((smb_sid_t *)token->tkn_primary_grp.i_sid, sidstr); 856 syslog(LOG_DEBUG, " PGrp->Sid: %s (id=%u)", 857 sidstr, token->tkn_primary_grp.i_id); 858 859 w_grps = &token->tkn_win_grps; 860 if (w_grps->i_ids) { 861 syslog(LOG_DEBUG, " Windows groups: %d", w_grps->i_cnt); 862 grp = w_grps->i_ids; 863 for (i = 0; i < w_grps->i_cnt; ++i, grp++) { 864 syslog(LOG_DEBUG, 865 " Grp[%d].Attr:%d", i, grp->i_attrs); 866 if (grp->i_sid != NULL) { 867 smb_sid_tostr((smb_sid_t *)grp->i_sid, sidstr); 868 syslog(LOG_DEBUG, 869 " Grp[%d].Sid: %s (id=%u)", i, sidstr, 870 grp->i_id); 871 } 872 } 873 } else { 874 syslog(LOG_DEBUG, " No Windows groups"); 875 } 876 877 x_grps = token->tkn_posix_grps; 878 if (x_grps) { 879 syslog(LOG_DEBUG, " Solaris groups: %d", x_grps->pg_ngrps); 880 for (i = 0; i < x_grps->pg_ngrps; i++) 881 syslog(LOG_DEBUG, " %u", x_grps->pg_grps[i]); 882 } else { 883 syslog(LOG_DEBUG, " No Solaris groups"); 884 } 885 886 if (token->tkn_privileges) 887 smb_privset_log(token->tkn_privileges); 888 else 889 syslog(LOG_DEBUG, " No privileges"); 890 } 891 892 /* 893 * Sets up local and well-known group membership for the given 894 * token. Two assumptions have been made here: 895 * 896 * a) token already contains a valid user SID so that group 897 * memberships can be established 898 * 899 * b) token belongs to a local or anonymous user 900 */ 901 static uint32_t 902 smb_token_setup_wingrps(smb_token_t *token) 903 { 904 smb_ids_t tkn_grps; 905 uint32_t status; 906 907 908 /* 909 * We always want the user's primary group in the list 910 * of groups. 911 */ 912 tkn_grps.i_cnt = 1; 913 if ((tkn_grps.i_ids = malloc(sizeof (smb_id_t))) == NULL) 914 return (NT_STATUS_NO_MEMORY); 915 916 tkn_grps.i_ids->i_sid = smb_sid_dup(token->tkn_primary_grp.i_sid); 917 tkn_grps.i_ids->i_attrs = token->tkn_primary_grp.i_attrs; 918 if (tkn_grps.i_ids->i_sid == NULL) { 919 smb_ids_free(&tkn_grps); 920 return (NT_STATUS_NO_MEMORY); 921 } 922 923 status = smb_sam_usr_groups(token->tkn_user.i_sid, &tkn_grps); 924 if (status != NT_STATUS_SUCCESS) { 925 smb_ids_free(&tkn_grps); 926 return (status); 927 } 928 929 status = smb_wka_token_groups(token->tkn_flags, &tkn_grps); 930 if (status != NT_STATUS_SUCCESS) { 931 smb_ids_free(&tkn_grps); 932 return (status); 933 } 934 935 token->tkn_win_grps = tkn_grps; 936 return (status); 937 } 938 939 /* 940 * Returns the guest account name in the provided buffer. 941 * 942 * By default the name would be "guest" unless there's 943 * a idmap name-based rule which maps the guest to a local 944 * Solaris user in which case the name of that user is 945 * returned. 946 */ 947 static void 948 smb_guest_account(char *guest, size_t buflen) 949 { 950 idmap_stat stat; 951 uid_t guest_uid; 952 struct passwd pw; 953 char pwbuf[1024]; 954 int idtype; 955 956 /* default Guest account name */ 957 (void) rw_rdlock(&smb_logoninit_rwl); 958 (void) strlcpy(guest, smb_guest.a_name, buflen); 959 960 idtype = SMB_IDMAP_USER; 961 stat = smb_idmap_getid(smb_guest.a_sid, &guest_uid, &idtype); 962 (void) rw_unlock(&smb_logoninit_rwl); 963 964 if (stat != IDMAP_SUCCESS) 965 return; 966 967 /* If Ephemeral ID return the default name */ 968 if (IDMAP_ID_IS_EPHEMERAL(guest_uid)) 969 return; 970 971 if (getpwuid_r(guest_uid, &pw, pwbuf, sizeof (pwbuf)) == NULL) 972 return; 973 974 (void) strlcpy(guest, pw.pw_name, buflen); 975 } 976