blob: 4ed50f2f1c7df514bbbcc6ed5155adeb0919c028 [file] [log] [blame]
amwda6c28a2007-10-25 16:34:29 -07001/*
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/*
joyce mcintosh1fdeec62010-07-26 15:02:13 -070022 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
Daniel Hoffman48bbca82017-02-17 11:48:20 -080023 * Copyright (c) 2016 by Delphix. All rights reserved.
Gordon Ross975041d2017-11-25 15:46:51 -050024 * Copyright 2017 Nexenta Systems, Inc. All rights reserved.
amwda6c28a2007-10-25 16:34:29 -070025 */
26
amwda6c28a2007-10-25 16:34:29 -070027#include <unistd.h>
28#include <strings.h>
29#include <pwd.h>
30#include <grp.h>
31#include <time.h>
32#include <syslog.h>
jose borrego2c1b14e2008-09-16 13:04:45 -060033#include <assert.h>
Alan Wright29bd2882009-06-09 14:20:02 -060034#include <synch.h>
amwda6c28a2007-10-25 16:34:29 -070035
36#include <smbsrv/libsmb.h>
37#include <smbsrv/libmlsvc.h>
38
39#include <smbsrv/smbinfo.h>
40#include <smbsrv/smb_token.h>
jose borrego8d7e4162008-12-10 22:16:19 -070041#include <lsalib.h>
amwda6c28a2007-10-25 16:34:29 -070042
Alan Wright29bd2882009-06-09 14:20:02 -060043static smb_account_t smb_guest;
44static smb_account_t smb_domusers;
45static rwlock_t smb_logoninit_rwl;
46
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -070047typedef void (*smb_logonop_t)(smb_logon_t *, smb_token_t *);
amwda6c28a2007-10-25 16:34:29 -070048
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -070049static void smb_logon_local(smb_logon_t *, smb_token_t *);
50static void smb_logon_guest(smb_logon_t *, smb_token_t *);
51static void smb_logon_anon(smb_logon_t *, smb_token_t *);
52
53static uint32_t smb_token_auth_local(smb_logon_t *, smb_token_t *,
Alan Wright29bd2882009-06-09 14:20:02 -060054 smb_passwd_t *);
55
jose borrego7f667e72009-02-01 19:44:54 -070056static uint32_t smb_token_setup_local(smb_passwd_t *, smb_token_t *);
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -070057static uint32_t smb_token_setup_guest(smb_logon_t *, smb_token_t *);
jose borrego7f667e72009-02-01 19:44:54 -070058static uint32_t smb_token_setup_anon(smb_token_t *token);
amwda6c28a2007-10-25 16:34:29 -070059
jose borrego7f667e72009-02-01 19:44:54 -070060static boolean_t smb_token_is_member(smb_token_t *, smb_sid_t *);
61static uint32_t smb_token_setup_wingrps(smb_token_t *);
62static smb_posix_grps_t *smb_token_create_pxgrps(uid_t);
amwda6c28a2007-10-25 16:34:29 -070063
Alan Wright29bd2882009-06-09 14:20:02 -060064static void smb_guest_account(char *, size_t);
65
amwda6c28a2007-10-25 16:34:29 -070066/* Consolidation private function from Network Repository */
67extern int _getgroupsbymember(const char *, gid_t[], int, int);
68
69static idmap_stat
70smb_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) {
jose borrego7f667e72009-02-01 19:44:54 -070083 token->tkn_user.i_id = UID_NOBODY;
84 token->tkn_owner.i_id = UID_NOBODY;
amwda6c28a2007-10-25 16:34:29 -070085 } else {
86 /* User SID */
jose borrego7f667e72009-02-01 19:44:54 -070087 id = &token->tkn_user;
amwda6c28a2007-10-25 16:34:29 -070088 sim->sim_id = &id->i_id;
89 stat = smb_idmap_batch_getid(sib->sib_idmaph, sim++,
jose borrego7f667e72009-02-01 19:44:54 -070090 id->i_sid, SMB_IDMAP_USER);
amwda6c28a2007-10-25 16:34:29 -070091
92 if (stat != IDMAP_SUCCESS)
93 return (stat);
94
95 /* Owner SID */
jose borrego7f667e72009-02-01 19:44:54 -070096 id = &token->tkn_owner;
amwda6c28a2007-10-25 16:34:29 -070097 sim->sim_id = &id->i_id;
98 stat = smb_idmap_batch_getid(sib->sib_idmaph, sim++,
jose borrego7f667e72009-02-01 19:44:54 -070099 id->i_sid, SMB_IDMAP_USER);
amwda6c28a2007-10-25 16:34:29 -0700100
101 if (stat != IDMAP_SUCCESS)
102 return (stat);
103 }
104
105 /* Primary Group SID */
jose borrego7f667e72009-02-01 19:44:54 -0700106 id = &token->tkn_primary_grp;
amwda6c28a2007-10-25 16:34:29 -0700107 sim->sim_id = &id->i_id;
jose borrego7f667e72009-02-01 19:44:54 -0700108 stat = smb_idmap_batch_getid(sib->sib_idmaph, sim++, id->i_sid,
109 SMB_IDMAP_GROUP);
amwda6c28a2007-10-25 16:34:29 -0700110
111 if (stat != IDMAP_SUCCESS)
112 return (stat);
113
114 /* Other Windows Group SIDs */
jose borrego7f667e72009-02-01 19:44:54 -0700115 for (i = 0; i < token->tkn_win_grps.i_cnt; i++, sim++) {
116 id = &token->tkn_win_grps.i_ids[i];
amwda6c28a2007-10-25 16:34:29 -0700117 sim->sim_id = &id->i_id;
118 stat = smb_idmap_batch_getid(sib->sib_idmaph, sim,
jose borrego7f667e72009-02-01 19:44:54 -0700119 id->i_sid, SMB_IDMAP_GROUP);
amwda6c28a2007-10-25 16:34:29 -0700120
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 *
133 * Returns 0 upon success. Otherwise, returns -1.
134 */
135static int
136smb_token_sids2ids(smb_token_t *token)
137{
138 idmap_stat stat;
joyce mcintosh1fdeec62010-07-26 15:02:13 -0700139 int nmaps;
amwda6c28a2007-10-25 16:34:29 -0700140 smb_idmap_batch_t sib;
141
142 /*
143 * Number of idmap lookups: user SID, owner SID, primary group SID,
jose borrego7f667e72009-02-01 19:44:54 -0700144 * and all Windows group SIDs. Skip user/owner SID for Anonymous.
amwda6c28a2007-10-25 16:34:29 -0700145 */
146 if (token->tkn_flags & SMB_ATF_ANON)
jose borrego7f667e72009-02-01 19:44:54 -0700147 nmaps = token->tkn_win_grps.i_cnt + 1;
amwda6c28a2007-10-25 16:34:29 -0700148 else
jose borrego7f667e72009-02-01 19:44:54 -0700149 nmaps = token->tkn_win_grps.i_cnt + 3;
amwda6c28a2007-10-25 16:34:29 -0700150
joyce mcintosh1fdeec62010-07-26 15:02:13 -0700151 stat = smb_idmap_batch_create(&sib, nmaps, SMB_IDMAP_SID2ID);
152 if (stat != IDMAP_SUCCESS)
153 return (-1);
amwda6c28a2007-10-25 16:34:29 -0700154
joyce mcintosh1fdeec62010-07-26 15:02:13 -0700155 stat = smb_token_idmap(token, &sib);
156 if (stat != IDMAP_SUCCESS) {
amwda6c28a2007-10-25 16:34:29 -0700157 smb_idmap_batch_destroy(&sib);
joyce mcintosh1fdeec62010-07-26 15:02:13 -0700158 return (-1);
159 }
160
161 stat = smb_idmap_batch_getmappings(&sib);
joyce mcintosh1fdeec62010-07-26 15:02:13 -0700162 smb_idmap_check("smb_idmap_batch_getmappings", stat);
Gordon Ross12b65582013-06-21 15:59:58 -0400163 smb_idmap_batch_destroy(&sib);
amwda6c28a2007-10-25 16:34:29 -0700164
165 return (stat == IDMAP_SUCCESS ? 0 : -1);
166}
167
168/*
169 * smb_token_create_pxgrps
170 *
171 * Setup the POSIX group membership of the access token if the given UID is
172 * a POSIX UID (non-ephemeral). Both the user's primary group and
173 * supplementary groups will be added to the POSIX group array of the access
174 * token.
175 */
176static smb_posix_grps_t *
177smb_token_create_pxgrps(uid_t uid)
178{
179 struct passwd *pwd;
180 smb_posix_grps_t *pgrps;
181 int ngroups_max, num;
182 gid_t *gids;
183
184 if ((ngroups_max = sysconf(_SC_NGROUPS_MAX)) < 0) {
185 syslog(LOG_ERR, "smb_logon: failed to get _SC_NGROUPS_MAX");
186 return (NULL);
187 }
188
189 pwd = getpwuid(uid);
190 if (pwd == NULL) {
191 pgrps = malloc(sizeof (smb_posix_grps_t));
192 if (pgrps == NULL)
193 return (NULL);
194
195 pgrps->pg_ngrps = 0;
196 return (pgrps);
197 }
198
199 if (pwd->pw_name == NULL) {
200 pgrps = malloc(sizeof (smb_posix_grps_t));
201 if (pgrps == NULL)
202 return (NULL);
203
204 pgrps->pg_ngrps = 1;
205 pgrps->pg_grps[0] = pwd->pw_gid;
206 return (pgrps);
207 }
208
209 gids = (gid_t *)malloc(ngroups_max * sizeof (gid_t));
210 if (gids == NULL) {
211 return (NULL);
212 }
213 bzero(gids, ngroups_max * sizeof (gid_t));
214
215 gids[0] = pwd->pw_gid;
216
217 /*
218 * Setup the groups starting at index 1 (the last arg)
219 * of gids array.
220 */
221 num = _getgroupsbymember(pwd->pw_name, gids, ngroups_max, 1);
222
223 if (num == -1) {
224 syslog(LOG_ERR, "smb_logon: unable "
225 "to get user's supplementary groups");
226 num = 1;
227 }
228
229 pgrps = (smb_posix_grps_t *)malloc(SMB_POSIX_GRPS_SIZE(num));
230 if (pgrps) {
231 pgrps->pg_ngrps = num;
232 bcopy(gids, pgrps->pg_grps, num * sizeof (gid_t));
233 }
234
235 free(gids);
236 return (pgrps);
237}
238
239/*
240 * smb_token_destroy
241 *
242 * Release all of the memory associated with a token structure. Ensure
243 * that the token has been unlinked before calling.
244 */
245void
246smb_token_destroy(smb_token_t *token)
247{
jose borrego7f667e72009-02-01 19:44:54 -0700248 if (token != NULL) {
249 smb_sid_free(token->tkn_user.i_sid);
250 smb_sid_free(token->tkn_owner.i_sid);
251 smb_sid_free(token->tkn_primary_grp.i_sid);
252 smb_ids_free(&token->tkn_win_grps);
253 smb_privset_free(token->tkn_privileges);
254 free(token->tkn_posix_grps);
255 free(token->tkn_account_name);
256 free(token->tkn_domain_name);
Gordon Ross12b65582013-06-21 15:59:58 -0400257 free(token->tkn_ssnkey.val);
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700258 bzero(token, sizeof (smb_token_t));
jose borrego7f667e72009-02-01 19:44:54 -0700259 free(token);
amwda6c28a2007-10-25 16:34:29 -0700260 }
amwda6c28a2007-10-25 16:34:29 -0700261}
262
263/*
264 * Token owner should be set to local Administrators group
265 * in two cases:
266 * 1. The logged on user is a member of Domain Admins group
Daniel Hoffman48bbca82017-02-17 11:48:20 -0800267 * 2. They are a member of local Administrators group
amwda6c28a2007-10-25 16:34:29 -0700268 */
jose borrego7f667e72009-02-01 19:44:54 -0700269static void
270smb_token_set_owner(smb_token_t *token)
amwda6c28a2007-10-25 16:34:29 -0700271{
afshin salek ardakani - Sun Microsystems - Irvine United Statesfc4e8c92008-09-30 20:43:09 -0700272#ifdef SMB_SUPPORT_GROUP_OWNER
jose borrego2c1b14e2008-09-16 13:04:45 -0600273 smb_sid_t *owner_sid;
amwda6c28a2007-10-25 16:34:29 -0700274
jose borrego7f667e72009-02-01 19:44:54 -0700275 if (token->tkn_flags & SMB_ATF_ADMIN) {
276 owner_sid = smb_wka_get_sid("Administrators");
277 assert(owner_sid);
jose borrego2c1b14e2008-09-16 13:04:45 -0600278 } else {
jose borrego7f667e72009-02-01 19:44:54 -0700279 owner_sid = token->tkn_user->i_sid;
jose borrego2c1b14e2008-09-16 13:04:45 -0600280 }
281
jose borrego7f667e72009-02-01 19:44:54 -0700282 token->tkn_owner.i_sid = smb_sid_dup(owner_sid);
afshin salek ardakani - Sun Microsystems - Irvine United Statesfc4e8c92008-09-30 20:43:09 -0700283#endif
jose borrego7f667e72009-02-01 19:44:54 -0700284 token->tkn_owner.i_sid = smb_sid_dup(token->tkn_user.i_sid);
amwda6c28a2007-10-25 16:34:29 -0700285}
286
287static smb_privset_t *
jose borrego7f667e72009-02-01 19:44:54 -0700288smb_token_create_privs(smb_token_t *token)
amwda6c28a2007-10-25 16:34:29 -0700289{
290 smb_privset_t *privs;
as200622dc20a302008-01-05 20:52:22 -0800291 smb_giter_t gi;
292 smb_group_t grp;
293 int rc;
amwda6c28a2007-10-25 16:34:29 -0700294
295 privs = smb_privset_new();
296 if (privs == NULL)
297 return (NULL);
298
as200622dc20a302008-01-05 20:52:22 -0800299 if (smb_lgrp_iteropen(&gi) != SMB_LGRP_SUCCESS) {
300 smb_privset_free(privs);
301 return (NULL);
302 }
303
304 while (smb_lgrp_iterate(&gi, &grp) == SMB_LGRP_SUCCESS) {
jose borrego7f667e72009-02-01 19:44:54 -0700305 if (smb_lgrp_is_member(&grp, token->tkn_user.i_sid))
as200622dc20a302008-01-05 20:52:22 -0800306 smb_privset_merge(privs, grp.sg_privs);
as200622dc20a302008-01-05 20:52:22 -0800307 smb_lgrp_free(&grp);
308 }
309 smb_lgrp_iterclose(&gi);
amwda6c28a2007-10-25 16:34:29 -0700310
jose borrego7f667e72009-02-01 19:44:54 -0700311 if (token->tkn_flags & SMB_ATF_ADMIN) {
Yuri Pankov53d00482012-08-31 15:12:34 -0500312 char admgrp[] = "Administrators";
313
314 rc = smb_lgrp_getbyname(admgrp, &grp);
as200622dc20a302008-01-05 20:52:22 -0800315 if (rc == SMB_LGRP_SUCCESS) {
316 smb_privset_merge(privs, grp.sg_privs);
317 smb_lgrp_free(&grp);
amwda6c28a2007-10-25 16:34:29 -0700318 }
319
320 /*
as200622dc20a302008-01-05 20:52:22 -0800321 * This privilege is required to view/edit SACL
amwda6c28a2007-10-25 16:34:29 -0700322 */
323 smb_privset_enable(privs, SE_SECURITY_LUID);
324 }
325
326 return (privs);
327}
328
329static void
jose borrego7f667e72009-02-01 19:44:54 -0700330smb_token_set_flags(smb_token_t *token)
amwda6c28a2007-10-25 16:34:29 -0700331{
jose borrego7f667e72009-02-01 19:44:54 -0700332 if (smb_token_is_member(token, smb_wka_get_sid("Administrators")))
amwda6c28a2007-10-25 16:34:29 -0700333 token->tkn_flags |= SMB_ATF_ADMIN;
334
jose borrego7f667e72009-02-01 19:44:54 -0700335 if (smb_token_is_member(token, smb_wka_get_sid("Power Users")))
amwda6c28a2007-10-25 16:34:29 -0700336 token->tkn_flags |= SMB_ATF_POWERUSER;
337
jose borrego7f667e72009-02-01 19:44:54 -0700338 if (smb_token_is_member(token, smb_wka_get_sid("Backup Operators")))
amwda6c28a2007-10-25 16:34:29 -0700339 token->tkn_flags |= SMB_ATF_BACKUPOP;
amwda6c28a2007-10-25 16:34:29 -0700340}
341
342/*
jose borrego7f667e72009-02-01 19:44:54 -0700343 * Common token setup for both local and domain users.
344 * This function must be called after the initial setup
345 * has been done.
amwda6c28a2007-10-25 16:34:29 -0700346 *
jose borrego7f667e72009-02-01 19:44:54 -0700347 * Note that the order of calls in this function are important.
Gordon Ross12b65582013-06-21 15:59:58 -0400348 *
349 * Returns B_TRUE for success.
amwda6c28a2007-10-25 16:34:29 -0700350 */
Gordon Ross12b65582013-06-21 15:59:58 -0400351boolean_t
jose borrego7f667e72009-02-01 19:44:54 -0700352smb_token_setup_common(smb_token_t *token)
amwda6c28a2007-10-25 16:34:29 -0700353{
jose borrego7f667e72009-02-01 19:44:54 -0700354 smb_token_set_flags(token);
amwda6c28a2007-10-25 16:34:29 -0700355
jose borrego7f667e72009-02-01 19:44:54 -0700356 smb_token_set_owner(token);
357 if (token->tkn_owner.i_sid == NULL)
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700358 return (B_FALSE);
amwda6c28a2007-10-25 16:34:29 -0700359
360 /* Privileges */
jose borrego7f667e72009-02-01 19:44:54 -0700361 token->tkn_privileges = smb_token_create_privs(token);
362 if (token->tkn_privileges == NULL)
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700363 return (B_FALSE);
amwda6c28a2007-10-25 16:34:29 -0700364
amwda6c28a2007-10-25 16:34:29 -0700365 if (smb_token_sids2ids(token) != 0) {
jose borrego2c1b14e2008-09-16 13:04:45 -0600366 syslog(LOG_ERR, "%s\\%s: idmap failed",
jose borrego7f667e72009-02-01 19:44:54 -0700367 token->tkn_domain_name, token->tkn_account_name);
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700368 return (B_FALSE);
amwda6c28a2007-10-25 16:34:29 -0700369 }
370
371 /* Solaris Groups */
jose borrego7f667e72009-02-01 19:44:54 -0700372 token->tkn_posix_grps = smb_token_create_pxgrps(token->tkn_user.i_id);
amwda6c28a2007-10-25 16:34:29 -0700373
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700374 return (smb_token_valid(token));
amwda6c28a2007-10-25 16:34:29 -0700375}
376
Alan Wright29bd2882009-06-09 14:20:02 -0600377uint32_t
378smb_logon_init(void)
379{
380 uint32_t status;
381
382 (void) rw_wrlock(&smb_logoninit_rwl);
383 status = smb_sam_lookup_name(NULL, "guest", SidTypeUser, &smb_guest);
384 if (status != NT_STATUS_SUCCESS) {
385 (void) rw_unlock(&smb_logoninit_rwl);
386 return (status);
387 }
388
389 status = smb_sam_lookup_name(NULL, "domain users", SidTypeGroup,
390 &smb_domusers);
391 if (status != NT_STATUS_SUCCESS) {
392 smb_account_free(&smb_guest);
393 bzero(&smb_guest, sizeof (smb_account_t));
394 (void) rw_unlock(&smb_logoninit_rwl);
395 return (status);
396 }
397
398 (void) rw_unlock(&smb_logoninit_rwl);
399 return (status);
400}
401
402void
403smb_logon_fini(void)
404{
405 (void) rw_wrlock(&smb_logoninit_rwl);
406 smb_account_free(&smb_guest);
407 smb_account_free(&smb_domusers);
408 bzero(&smb_guest, sizeof (smb_account_t));
409 bzero(&smb_domusers, sizeof (smb_account_t));
410 (void) rw_unlock(&smb_logoninit_rwl);
411}
412
amwda6c28a2007-10-25 16:34:29 -0700413/*
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700414 * Perform user authentication.
amwda6c28a2007-10-25 16:34:29 -0700415 *
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700416 * The dispatched functions must only update the user_info status if they
417 * attempt to authenticate the user.
amwda6c28a2007-10-25 16:34:29 -0700418 *
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700419 * On success, a pointer to a new access token is returned.
Gordon Ross975041d2017-11-25 15:46:51 -0500420 * On failure, NULL return and status in user_info->lg_status
amwda6c28a2007-10-25 16:34:29 -0700421 */
422smb_token_t *
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700423smb_logon(smb_logon_t *user_info)
amwda6c28a2007-10-25 16:34:29 -0700424{
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700425 static smb_logonop_t ops[] = {
426 smb_logon_anon,
427 smb_logon_local,
428 smb_logon_domain,
429 smb_logon_guest
430 };
431 smb_token_t *token = NULL;
432 smb_domain_t domain;
433 int n_op = (sizeof (ops) / sizeof (ops[0]));
434 int i;
amwda6c28a2007-10-25 16:34:29 -0700435
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700436 user_info->lg_secmode = smb_config_get_secmode();
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700437
438 if (smb_domain_lookup_name(user_info->lg_e_domain, &domain))
439 user_info->lg_domain_type = domain.di_type;
440 else
441 user_info->lg_domain_type = SMB_DOMAIN_NULL;
442
443 if ((token = calloc(1, sizeof (smb_token_t))) == NULL) {
444 syslog(LOG_ERR, "logon[%s\\%s]: %m",
445 user_info->lg_e_domain, user_info->lg_e_username);
amwda6c28a2007-10-25 16:34:29 -0700446 return (NULL);
jose borrego7f667e72009-02-01 19:44:54 -0700447 }
amwda6c28a2007-10-25 16:34:29 -0700448
Gordon Ross975041d2017-11-25 15:46:51 -0500449 /*
450 * If any logonop function takes significant action
451 * (logon or authoratative failure) it will change
452 * this status field to something else.
453 */
454 user_info->lg_status = NT_STATUS_NO_SUCH_USER;
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700455 for (i = 0; i < n_op; ++i) {
456 (*ops[i])(user_info, token);
Alan Wright29bd2882009-06-09 14:20:02 -0600457
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700458 if (user_info->lg_status == NT_STATUS_SUCCESS)
459 break;
amwda6c28a2007-10-25 16:34:29 -0700460 }
461
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700462 if (user_info->lg_status == NT_STATUS_SUCCESS) {
463 if (smb_token_setup_common(token))
Gordon Ross975041d2017-11-25 15:46:51 -0500464 return (token); /* success */
465 /*
466 * (else) smb_token_setup_common failed, which usually
467 * means smb_token_sids2ids() failed to map some SIDs to
468 * Unix IDs. This indicates an idmap config problem.
469 */
470 user_info->lg_status = NT_STATUS_INTERNAL_ERROR;
jose borrego7f667e72009-02-01 19:44:54 -0700471 }
amwda6c28a2007-10-25 16:34:29 -0700472
jose borrego7f667e72009-02-01 19:44:54 -0700473 smb_token_destroy(token);
Gordon Ross975041d2017-11-25 15:46:51 -0500474
475 /*
476 * Any unknown user or bad password should result in
477 * NT_STATUS_LOGON_FAILURE (so we don't give hints).
478 */
479 if (user_info->lg_status == NT_STATUS_NO_SUCH_USER ||
480 user_info->lg_status == NT_STATUS_WRONG_PASSWORD)
481 user_info->lg_status = NT_STATUS_LOGON_FAILURE;
482
jose borrego7f667e72009-02-01 19:44:54 -0700483 return (NULL);
amwda6c28a2007-10-25 16:34:29 -0700484}
485
486/*
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700487 * If the user has an entry in the local database, attempt local authentication.
amwda6c28a2007-10-25 16:34:29 -0700488 *
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700489 * In domain mode, we try to exclude domain accounts, which we do by only
490 * accepting local or null (blank) domain names here. Some clients (Mac OS)
491 * don't always send the domain name.
amwda6c28a2007-10-25 16:34:29 -0700492 *
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700493 * If we are not going to attempt authentication, this function must return
494 * without updating the status.
amwda6c28a2007-10-25 16:34:29 -0700495 */
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700496static void
497smb_logon_local(smb_logon_t *user_info, smb_token_t *token)
amwda6c28a2007-10-25 16:34:29 -0700498{
Alan Wright29bd2882009-06-09 14:20:02 -0600499 char guest[SMB_USERNAME_MAXLEN];
amwda6c28a2007-10-25 16:34:29 -0700500 smb_passwd_t smbpw;
amwda6c28a2007-10-25 16:34:29 -0700501 uint32_t status;
502
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700503 if (user_info->lg_secmode == SMB_SECMODE_DOMAIN) {
504 if ((user_info->lg_domain_type != SMB_DOMAIN_LOCAL) &&
505 (user_info->lg_domain_type != SMB_DOMAIN_NULL))
506 return;
jose borrego7f667e72009-02-01 19:44:54 -0700507 }
508
Gordon Ross12b65582013-06-21 15:59:58 -0400509 /*
510 * If the requested account name is "guest" (or whatever
511 * our guest account is named) then don't handle it here.
512 * Let this request fall through to smb_logon_guest().
513 */
Alan Wright29bd2882009-06-09 14:20:02 -0600514 smb_guest_account(guest, SMB_USERNAME_MAXLEN);
Gordon Ross12b65582013-06-21 15:59:58 -0400515 if (smb_strcasecmp(guest, user_info->lg_e_username, 0) == 0)
516 return;
amwda6c28a2007-10-25 16:34:29 -0700517
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700518 status = smb_token_auth_local(user_info, token, &smbpw);
Gordon Ross12b65582013-06-21 15:59:58 -0400519 if (status == NT_STATUS_SUCCESS)
520 status = smb_token_setup_local(&smbpw, token);
amwda6c28a2007-10-25 16:34:29 -0700521
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700522 user_info->lg_status = status;
Alan Wright29bd2882009-06-09 14:20:02 -0600523}
524
525/*
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700526 * Guest authentication. This may be a local guest account or the guest
527 * account may be mapped to a local account. These accounts are regular
528 * accounts with normal password protection.
529 *
530 * Only proceed with a guest logon if previous logon options have resulted
531 * in NO_SUCH_USER.
532 *
533 * If we are not going to attempt authentication, this function must return
534 * without updating the status.
Alan Wright29bd2882009-06-09 14:20:02 -0600535 */
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700536static void
537smb_logon_guest(smb_logon_t *user_info, smb_token_t *token)
Alan Wright29bd2882009-06-09 14:20:02 -0600538{
539 char guest[SMB_USERNAME_MAXLEN];
540 smb_passwd_t smbpw;
541 char *temp;
Alan Wright29bd2882009-06-09 14:20:02 -0600542
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700543 if (user_info->lg_status != NT_STATUS_NO_SUCH_USER)
544 return;
Alan Wright29bd2882009-06-09 14:20:02 -0600545
Gordon Ross12b65582013-06-21 15:59:58 -0400546 /* Get the name of the guest account. */
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700547 smb_guest_account(guest, SMB_USERNAME_MAXLEN);
Gordon Ross12b65582013-06-21 15:59:58 -0400548
549 /* Does the guest account exist? */
550 if (smb_pwd_getpwnam(guest, &smbpw) == NULL)
551 return;
552
553 /* Is it enabled? (empty p/w is OK) */
554 if (smbpw.pw_flags & SMB_PWF_DISABLE)
555 return;
556
557 /*
558 * OK, give the client a guest logon. Note that on entry,
559 * lg_e_username is typically something other than "guest"
560 * so we need to set the effective username when createing
561 * the guest token.
562 */
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700563 temp = user_info->lg_e_username;
564 user_info->lg_e_username = guest;
Gordon Ross12b65582013-06-21 15:59:58 -0400565 user_info->lg_status = smb_token_setup_guest(user_info, token);
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700566 user_info->lg_e_username = temp;
amwda6c28a2007-10-25 16:34:29 -0700567}
568
569/*
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700570 * If user_info represents an anonymous user then setup the token.
571 * Otherwise return without updating the status.
amwda6c28a2007-10-25 16:34:29 -0700572 */
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700573static void
574smb_logon_anon(smb_logon_t *user_info, smb_token_t *token)
amwda6c28a2007-10-25 16:34:29 -0700575{
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700576 if (user_info->lg_flags & SMB_ATF_ANON)
577 user_info->lg_status = smb_token_setup_anon(token);
amwda6c28a2007-10-25 16:34:29 -0700578}
579
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700580/*
581 * Try both LM hash and NT hashes with user's password(s) to authenticate
582 * the user.
583 */
Alan Wright29bd2882009-06-09 14:20:02 -0600584static uint32_t
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700585smb_token_auth_local(smb_logon_t *user_info, smb_token_t *token,
Alan Wright29bd2882009-06-09 14:20:02 -0600586 smb_passwd_t *smbpw)
587{
Gordon Ross12b65582013-06-21 15:59:58 -0400588 boolean_t ok;
Alan Wright29bd2882009-06-09 14:20:02 -0600589 uint32_t status = NT_STATUS_SUCCESS;
590
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700591 if (smb_pwd_getpwnam(user_info->lg_e_username, smbpw) == NULL)
Alan Wright29bd2882009-06-09 14:20:02 -0600592 return (NT_STATUS_NO_SUCH_USER);
593
594 if (smbpw->pw_flags & SMB_PWF_DISABLE)
595 return (NT_STATUS_ACCOUNT_DISABLED);
596
Gordon Ross12b65582013-06-21 15:59:58 -0400597 if ((smbpw->pw_flags & (SMB_PWF_LM | SMB_PWF_NT)) == 0) {
598 /*
599 * The SMB passwords have not been set.
600 * Return an error that suggests the
601 * password needs to be set.
602 */
603 return (NT_STATUS_PASSWORD_EXPIRED);
Alan Wright29bd2882009-06-09 14:20:02 -0600604 }
605
Gordon Ross12b65582013-06-21 15:59:58 -0400606 token->tkn_ssnkey.val = malloc(SMBAUTH_SESSION_KEY_SZ);
607 if (token->tkn_ssnkey.val == NULL)
608 return (NT_STATUS_NO_MEMORY);
609 token->tkn_ssnkey.len = SMBAUTH_SESSION_KEY_SZ;
Alan Wright29bd2882009-06-09 14:20:02 -0600610
Gordon Ross12b65582013-06-21 15:59:58 -0400611 ok = smb_auth_validate(
612 smbpw,
613 user_info->lg_domain,
614 user_info->lg_username,
615 user_info->lg_challenge_key.val,
616 user_info->lg_challenge_key.len,
617 user_info->lg_nt_password.val,
618 user_info->lg_nt_password.len,
619 user_info->lg_lm_password.val,
620 user_info->lg_lm_password.len,
621 token->tkn_ssnkey.val);
622 if (ok)
623 return (NT_STATUS_SUCCESS);
624
625 free(token->tkn_ssnkey.val);
626 token->tkn_ssnkey.val = NULL;
627 token->tkn_ssnkey.len = 0;
628
629 status = NT_STATUS_WRONG_PASSWORD;
630 syslog(LOG_NOTICE, "logon[%s\\%s]: %s",
631 user_info->lg_e_domain, user_info->lg_e_username,
632 xlate_nt_status(status));
Alan Wright29bd2882009-06-09 14:20:02 -0600633
634 return (status);
635}
636
amwda6c28a2007-10-25 16:34:29 -0700637/*
jose borrego7f667e72009-02-01 19:44:54 -0700638 * Setup an access token for the specified local user.
amwda6c28a2007-10-25 16:34:29 -0700639 */
640static uint32_t
jose borrego7f667e72009-02-01 19:44:54 -0700641smb_token_setup_local(smb_passwd_t *smbpw, smb_token_t *token)
amwda6c28a2007-10-25 16:34:29 -0700642{
643 idmap_stat stat;
644 smb_idmap_batch_t sib;
645 smb_idmap_t *umap, *gmap;
amwda6c28a2007-10-25 16:34:29 -0700646 struct passwd pw;
647 char pwbuf[1024];
natalie li - Sun Microsystems - Irvine United Statesb89a8332008-10-28 03:34:04 -0700648 char nbname[NETBIOS_NAME_SZ];
amwda6c28a2007-10-25 16:34:29 -0700649
natalie li - Sun Microsystems - Irvine United Statesb89a8332008-10-28 03:34:04 -0700650 (void) smb_getnetbiosname(nbname, sizeof (nbname));
jose borrego7f667e72009-02-01 19:44:54 -0700651 token->tkn_account_name = strdup(smbpw->pw_name);
652 token->tkn_domain_name = strdup(nbname);
amwda6c28a2007-10-25 16:34:29 -0700653
jose borrego7f667e72009-02-01 19:44:54 -0700654 if (token->tkn_account_name == NULL ||
655 token->tkn_domain_name == NULL)
656 return (NT_STATUS_NO_MEMORY);
amwda6c28a2007-10-25 16:34:29 -0700657
jose borrego7f667e72009-02-01 19:44:54 -0700658 if (getpwuid_r(smbpw->pw_uid, &pw, pwbuf, sizeof (pwbuf)) == NULL)
amwda6c28a2007-10-25 16:34:29 -0700659 return (NT_STATUS_NO_SUCH_USER);
660
661 /* Get the SID for user's uid & gid */
662 stat = smb_idmap_batch_create(&sib, 2, SMB_IDMAP_ID2SID);
jose borrego7f667e72009-02-01 19:44:54 -0700663 if (stat != IDMAP_SUCCESS)
amwda6c28a2007-10-25 16:34:29 -0700664 return (NT_STATUS_INTERNAL_ERROR);
amwda6c28a2007-10-25 16:34:29 -0700665
666 umap = &sib.sib_maps[0];
667 stat = smb_idmap_batch_getsid(sib.sib_idmaph, umap, pw.pw_uid,
668 SMB_IDMAP_USER);
669
670 if (stat != IDMAP_SUCCESS) {
671 smb_idmap_batch_destroy(&sib);
672 return (NT_STATUS_INTERNAL_ERROR);
673 }
674
675 gmap = &sib.sib_maps[1];
676 stat = smb_idmap_batch_getsid(sib.sib_idmaph, gmap, pw.pw_gid,
677 SMB_IDMAP_GROUP);
678
679 if (stat != IDMAP_SUCCESS) {
680 smb_idmap_batch_destroy(&sib);
681 return (NT_STATUS_INTERNAL_ERROR);
682 }
683
jose borrego7f667e72009-02-01 19:44:54 -0700684 if (smb_idmap_batch_getmappings(&sib) != IDMAP_SUCCESS)
amwda6c28a2007-10-25 16:34:29 -0700685 return (NT_STATUS_INTERNAL_ERROR);
amwda6c28a2007-10-25 16:34:29 -0700686
jose borrego7f667e72009-02-01 19:44:54 -0700687 token->tkn_user.i_sid = smb_sid_dup(umap->sim_sid);
688 token->tkn_primary_grp.i_sid = smb_sid_dup(gmap->sim_sid);
amwda6c28a2007-10-25 16:34:29 -0700689
690 smb_idmap_batch_destroy(&sib);
691
jose borrego7f667e72009-02-01 19:44:54 -0700692 if (token->tkn_user.i_sid == NULL ||
693 token->tkn_primary_grp.i_sid == NULL)
amwda6c28a2007-10-25 16:34:29 -0700694 return (NT_STATUS_NO_MEMORY);
695
jose borrego7f667e72009-02-01 19:44:54 -0700696 return (smb_token_setup_wingrps(token));
amwda6c28a2007-10-25 16:34:29 -0700697}
698
699/*
Alan Wright29bd2882009-06-09 14:20:02 -0600700 * Setup access token for guest connections
701 */
702static uint32_t
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700703smb_token_setup_guest(smb_logon_t *user_info, smb_token_t *token)
Alan Wright29bd2882009-06-09 14:20:02 -0600704{
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700705 token->tkn_account_name = strdup(user_info->lg_e_username);
Alan Wright29bd2882009-06-09 14:20:02 -0600706
707 (void) rw_rdlock(&smb_logoninit_rwl);
708 token->tkn_domain_name = strdup(smb_guest.a_domain);
709 token->tkn_user.i_sid = smb_sid_dup(smb_guest.a_sid);
710 token->tkn_primary_grp.i_sid = smb_sid_dup(smb_domusers.a_sid);
711 (void) rw_unlock(&smb_logoninit_rwl);
712 token->tkn_flags = SMB_ATF_GUEST;
713
714 if (token->tkn_account_name == NULL ||
715 token->tkn_domain_name == NULL ||
716 token->tkn_user.i_sid == NULL ||
717 token->tkn_primary_grp.i_sid == NULL)
718 return (NT_STATUS_NO_MEMORY);
719
720 return (smb_token_setup_wingrps(token));
721}
722
723/*
724 * Setup access token for anonymous connections
amwda6c28a2007-10-25 16:34:29 -0700725 */
jose borrego7f667e72009-02-01 19:44:54 -0700726static uint32_t
727smb_token_setup_anon(smb_token_t *token)
amwda6c28a2007-10-25 16:34:29 -0700728{
jose borrego7f667e72009-02-01 19:44:54 -0700729 smb_sid_t *user_sid;
amwda6c28a2007-10-25 16:34:29 -0700730
jose borrego7f667e72009-02-01 19:44:54 -0700731 token->tkn_account_name = strdup("Anonymous");
732 token->tkn_domain_name = strdup("NT Authority");
733 user_sid = smb_wka_get_sid("Anonymous");
734 token->tkn_user.i_sid = smb_sid_dup(user_sid);
735 token->tkn_primary_grp.i_sid = smb_sid_dup(user_sid);
736 token->tkn_flags = SMB_ATF_ANON;
amwda6c28a2007-10-25 16:34:29 -0700737
jose borrego7f667e72009-02-01 19:44:54 -0700738 if (token->tkn_account_name == NULL ||
739 token->tkn_domain_name == NULL ||
740 token->tkn_user.i_sid == NULL ||
741 token->tkn_primary_grp.i_sid == NULL)
742 return (NT_STATUS_NO_MEMORY);
743
744 return (smb_token_setup_wingrps(token));
amwda6c28a2007-10-25 16:34:29 -0700745}
746
747/*
748 * smb_token_user_sid
749 *
750 * Return a pointer to the user SID in the specified token. A null
751 * pointer indicates an error.
752 */
as2006226537f382008-04-14 10:40:32 -0700753static smb_sid_t *
amwda6c28a2007-10-25 16:34:29 -0700754smb_token_user_sid(smb_token_t *token)
755{
jose borrego7f667e72009-02-01 19:44:54 -0700756 return ((token) ? token->tkn_user.i_sid : NULL);
amwda6c28a2007-10-25 16:34:29 -0700757}
758
759/*
760 * smb_token_group_sid
761 *
762 * Return a pointer to the group SID as indicated by the iterator.
763 * Setting the iterator to 0 before calling this function will return
764 * the first group, which will always be the primary group. The
765 * iterator will be incremented before returning the SID so that this
766 * function can be used to cycle through the groups. The caller can
767 * adjust the iterator as required between calls to obtain any specific
768 * group.
769 *
770 * On success a pointer to the appropriate group SID will be returned.
771 * Otherwise a null pointer will be returned.
772 */
as2006226537f382008-04-14 10:40:32 -0700773static smb_sid_t *
amwda6c28a2007-10-25 16:34:29 -0700774smb_token_group_sid(smb_token_t *token, int *iterator)
775{
amwda6c28a2007-10-25 16:34:29 -0700776 int index;
777
jose borrego7f667e72009-02-01 19:44:54 -0700778 if (token == NULL || iterator == NULL)
amwda6c28a2007-10-25 16:34:29 -0700779 return (NULL);
amwda6c28a2007-10-25 16:34:29 -0700780
jose borrego7f667e72009-02-01 19:44:54 -0700781 if (token->tkn_win_grps.i_ids == NULL)
amwda6c28a2007-10-25 16:34:29 -0700782 return (NULL);
amwda6c28a2007-10-25 16:34:29 -0700783
784 index = *iterator;
785
jose borrego7f667e72009-02-01 19:44:54 -0700786 if (index < 0 || index >= token->tkn_win_grps.i_cnt)
amwda6c28a2007-10-25 16:34:29 -0700787 return (NULL);
amwda6c28a2007-10-25 16:34:29 -0700788
789 ++(*iterator);
jose borrego7f667e72009-02-01 19:44:54 -0700790 return (token->tkn_win_grps.i_ids[index].i_sid);
amwda6c28a2007-10-25 16:34:29 -0700791}
792
793/*
794 * smb_token_is_member
795 *
796 * This function will determine whether or not the specified SID is a
797 * member of a token. The user SID and all group SIDs are tested.
798 * Returns 1 if the SID is a member of the token. Otherwise returns 0.
799 */
jose borrego7f667e72009-02-01 19:44:54 -0700800static boolean_t
as2006226537f382008-04-14 10:40:32 -0700801smb_token_is_member(smb_token_t *token, smb_sid_t *sid)
amwda6c28a2007-10-25 16:34:29 -0700802{
as2006226537f382008-04-14 10:40:32 -0700803 smb_sid_t *tsid;
amwda6c28a2007-10-25 16:34:29 -0700804 int iterator = 0;
805
jose borrego7f667e72009-02-01 19:44:54 -0700806 if (token == NULL || sid == NULL)
807 return (B_FALSE);
808
amwda6c28a2007-10-25 16:34:29 -0700809 tsid = smb_token_user_sid(token);
810 while (tsid) {
as2006226537f382008-04-14 10:40:32 -0700811 if (smb_sid_cmp(tsid, sid))
jose borrego7f667e72009-02-01 19:44:54 -0700812 return (B_TRUE);
amwda6c28a2007-10-25 16:34:29 -0700813
814 tsid = smb_token_group_sid(token, &iterator);
815 }
816
jose borrego7f667e72009-02-01 19:44:54 -0700817 return (B_FALSE);
amwda6c28a2007-10-25 16:34:29 -0700818}
as2006226537f382008-04-14 10:40:32 -0700819
820/*
821 * smb_token_log
822 *
823 * Diagnostic routine to write the contents of a token to the log.
824 */
825void
826smb_token_log(smb_token_t *token)
827{
jose borrego7f667e72009-02-01 19:44:54 -0700828 smb_ids_t *w_grps;
829 smb_id_t *grp;
as2006226537f382008-04-14 10:40:32 -0700830 smb_posix_grps_t *x_grps;
as2006226537f382008-04-14 10:40:32 -0700831 char sidstr[SMB_SID_STRSZ];
832 int i;
833
834 if (token == NULL)
835 return;
836
837 syslog(LOG_DEBUG, "Token for %s\\%s",
838 (token->tkn_domain_name) ? token->tkn_domain_name : "-NULL-",
839 (token->tkn_account_name) ? token->tkn_account_name : "-NULL-");
840
jose borrego7f667e72009-02-01 19:44:54 -0700841 syslog(LOG_DEBUG, " User->Attr: %d", token->tkn_user.i_attrs);
842 smb_sid_tostr((smb_sid_t *)token->tkn_user.i_sid, sidstr);
843 syslog(LOG_DEBUG, " User->Sid: %s (id=%u)", sidstr,
844 token->tkn_user.i_id);
as2006226537f382008-04-14 10:40:32 -0700845
jose borrego7f667e72009-02-01 19:44:54 -0700846 smb_sid_tostr((smb_sid_t *)token->tkn_owner.i_sid, sidstr);
as2006226537f382008-04-14 10:40:32 -0700847 syslog(LOG_DEBUG, " Ownr->Sid: %s (id=%u)",
jose borrego7f667e72009-02-01 19:44:54 -0700848 sidstr, token->tkn_owner.i_id);
as2006226537f382008-04-14 10:40:32 -0700849
jose borrego7f667e72009-02-01 19:44:54 -0700850 smb_sid_tostr((smb_sid_t *)token->tkn_primary_grp.i_sid, sidstr);
as2006226537f382008-04-14 10:40:32 -0700851 syslog(LOG_DEBUG, " PGrp->Sid: %s (id=%u)",
jose borrego7f667e72009-02-01 19:44:54 -0700852 sidstr, token->tkn_primary_grp.i_id);
as2006226537f382008-04-14 10:40:32 -0700853
jose borrego7f667e72009-02-01 19:44:54 -0700854 w_grps = &token->tkn_win_grps;
855 if (w_grps->i_ids) {
856 syslog(LOG_DEBUG, " Windows groups: %d", w_grps->i_cnt);
857 grp = w_grps->i_ids;
858 for (i = 0; i < w_grps->i_cnt; ++i, grp++) {
as2006226537f382008-04-14 10:40:32 -0700859 syslog(LOG_DEBUG,
jose borrego7f667e72009-02-01 19:44:54 -0700860 " Grp[%d].Attr:%d", i, grp->i_attrs);
861 if (grp->i_sid != NULL) {
862 smb_sid_tostr((smb_sid_t *)grp->i_sid, sidstr);
as2006226537f382008-04-14 10:40:32 -0700863 syslog(LOG_DEBUG,
864 " Grp[%d].Sid: %s (id=%u)", i, sidstr,
jose borrego7f667e72009-02-01 19:44:54 -0700865 grp->i_id);
as2006226537f382008-04-14 10:40:32 -0700866 }
867 }
jose borrego7f667e72009-02-01 19:44:54 -0700868 } else {
as2006226537f382008-04-14 10:40:32 -0700869 syslog(LOG_DEBUG, " No Windows groups");
jose borrego7f667e72009-02-01 19:44:54 -0700870 }
as2006226537f382008-04-14 10:40:32 -0700871
872 x_grps = token->tkn_posix_grps;
873 if (x_grps) {
jose borrego7f667e72009-02-01 19:44:54 -0700874 syslog(LOG_DEBUG, " Solaris groups: %d", x_grps->pg_ngrps);
as2006226537f382008-04-14 10:40:32 -0700875 for (i = 0; i < x_grps->pg_ngrps; i++)
jose borrego7f667e72009-02-01 19:44:54 -0700876 syslog(LOG_DEBUG, " %u", x_grps->pg_grps[i]);
877 } else {
as2006226537f382008-04-14 10:40:32 -0700878 syslog(LOG_DEBUG, " No Solaris groups");
jose borrego7f667e72009-02-01 19:44:54 -0700879 }
as2006226537f382008-04-14 10:40:32 -0700880
881 if (token->tkn_privileges)
882 smb_privset_log(token->tkn_privileges);
883 else
884 syslog(LOG_DEBUG, " No privileges");
885}
jose borrego7f667e72009-02-01 19:44:54 -0700886
887/*
888 * Sets up local and well-known group membership for the given
889 * token. Two assumptions have been made here:
890 *
891 * a) token already contains a valid user SID so that group
892 * memberships can be established
893 *
894 * b) token belongs to a local or anonymous user
895 */
896static uint32_t
897smb_token_setup_wingrps(smb_token_t *token)
898{
899 smb_ids_t tkn_grps;
900 uint32_t status;
901
902
903 /*
904 * We always want the user's primary group in the list
905 * of groups.
906 */
907 tkn_grps.i_cnt = 1;
908 if ((tkn_grps.i_ids = malloc(sizeof (smb_id_t))) == NULL)
909 return (NT_STATUS_NO_MEMORY);
910
911 tkn_grps.i_ids->i_sid = smb_sid_dup(token->tkn_primary_grp.i_sid);
912 tkn_grps.i_ids->i_attrs = token->tkn_primary_grp.i_attrs;
913 if (tkn_grps.i_ids->i_sid == NULL) {
914 smb_ids_free(&tkn_grps);
915 return (NT_STATUS_NO_MEMORY);
916 }
917
918 status = smb_sam_usr_groups(token->tkn_user.i_sid, &tkn_grps);
919 if (status != NT_STATUS_SUCCESS) {
920 smb_ids_free(&tkn_grps);
921 return (status);
922 }
923
Alan Wright29bd2882009-06-09 14:20:02 -0600924 status = smb_wka_token_groups(token->tkn_flags, &tkn_grps);
925 if (status != NT_STATUS_SUCCESS) {
926 smb_ids_free(&tkn_grps);
927 return (status);
jose borrego7f667e72009-02-01 19:44:54 -0700928 }
929
930 token->tkn_win_grps = tkn_grps;
931 return (status);
932}
Alan Wright29bd2882009-06-09 14:20:02 -0600933
934/*
935 * Returns the guest account name in the provided buffer.
936 *
937 * By default the name would be "guest" unless there's
938 * a idmap name-based rule which maps the guest to a local
939 * Solaris user in which case the name of that user is
940 * returned.
941 */
942static void
943smb_guest_account(char *guest, size_t buflen)
944{
945 idmap_stat stat;
946 uid_t guest_uid;
947 struct passwd pw;
948 char pwbuf[1024];
949 int idtype;
950
951 /* default Guest account name */
952 (void) rw_rdlock(&smb_logoninit_rwl);
953 (void) strlcpy(guest, smb_guest.a_name, buflen);
954
955 idtype = SMB_IDMAP_USER;
956 stat = smb_idmap_getid(smb_guest.a_sid, &guest_uid, &idtype);
957 (void) rw_unlock(&smb_logoninit_rwl);
958
959 if (stat != IDMAP_SUCCESS)
960 return;
961
afshin salek ardakani - Sun Microsystems - Irvine United States9fb67ea2010-03-20 13:03:54 -0700962 /* If Ephemeral ID return the default name */
963 if (IDMAP_ID_IS_EPHEMERAL(guest_uid))
Alan Wright29bd2882009-06-09 14:20:02 -0600964 return;
965
966 if (getpwuid_r(guest_uid, &pw, pwbuf, sizeof (pwbuf)) == NULL)
967 return;
968
969 (void) strlcpy(guest, pw.pw_name, buflen);
970}