xref: /titanic_51/usr/src/lib/smbsrv/libmlsvc/common/smb_logon.c (revision 12b65585e720714b31036daaa2b30eb76014048e)
1b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
2b89a8333Snatalie li - Sun Microsystems - Irvine United States  * CDDL HEADER START
3b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
4b89a8333Snatalie li - Sun Microsystems - Irvine United States  * The contents of this file are subject to the terms of the
5b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Common Development and Distribution License (the "License").
6b89a8333Snatalie li - Sun Microsystems - Irvine United States  * You may not use this file except in compliance with the License.
7b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
8b89a8333Snatalie li - Sun Microsystems - Irvine United States  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9b89a8333Snatalie li - Sun Microsystems - Irvine United States  * or http://www.opensolaris.org/os/licensing.
10b89a8333Snatalie li - Sun Microsystems - Irvine United States  * See the License for the specific language governing permissions
11b89a8333Snatalie li - Sun Microsystems - Irvine United States  * and limitations under the License.
12b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
13b89a8333Snatalie li - Sun Microsystems - Irvine United States  * When distributing Covered Code, include this CDDL HEADER in each
14b89a8333Snatalie li - Sun Microsystems - Irvine United States  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15b89a8333Snatalie li - Sun Microsystems - Irvine United States  * If applicable, add the following below this CDDL HEADER, with the
16b89a8333Snatalie li - Sun Microsystems - Irvine United States  * fields enclosed by brackets "[]" replaced with your own identifying
17b89a8333Snatalie li - Sun Microsystems - Irvine United States  * information: Portions Copyright [yyyy] [name of copyright owner]
18b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
19b89a8333Snatalie li - Sun Microsystems - Irvine United States  * CDDL HEADER END
20b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
21b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
221fdeec65Sjoyce mcintosh  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23*12b65585SGordon Ross  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
24b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
25b89a8333Snatalie li - Sun Microsystems - Irvine United States 
26b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <unistd.h>
27b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <strings.h>
28b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <pwd.h>
29b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <grp.h>
30b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <time.h>
31b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <syslog.h>
32b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <assert.h>
3329bd2886SAlan Wright #include <synch.h>
34b89a8333Snatalie li - Sun Microsystems - Irvine United States 
35b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <smbsrv/libsmb.h>
36b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <smbsrv/libmlsvc.h>
37b89a8333Snatalie li - Sun Microsystems - Irvine United States 
38b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <smbsrv/smbinfo.h>
39b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <smbsrv/smb_token.h>
408d7e4166Sjose borrego #include <lsalib.h>
41b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4229bd2886SAlan Wright static smb_account_t smb_guest;
4329bd2886SAlan Wright static smb_account_t smb_domusers;
4429bd2886SAlan Wright static rwlock_t smb_logoninit_rwl;
4529bd2886SAlan Wright 
469fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States typedef void (*smb_logonop_t)(smb_logon_t *, smb_token_t *);
47b89a8333Snatalie li - Sun Microsystems - Irvine United States 
489fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static void smb_logon_local(smb_logon_t *, smb_token_t *);
499fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static void smb_logon_guest(smb_logon_t *, smb_token_t *);
509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static void smb_logon_anon(smb_logon_t *, smb_token_t *);
519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
529fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static uint32_t smb_token_auth_local(smb_logon_t *, smb_token_t *,
5329bd2886SAlan Wright     smb_passwd_t *);
5429bd2886SAlan Wright 
557f667e74Sjose borrego static uint32_t smb_token_setup_local(smb_passwd_t *, smb_token_t *);
569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static uint32_t smb_token_setup_guest(smb_logon_t *, smb_token_t *);
577f667e74Sjose borrego static uint32_t smb_token_setup_anon(smb_token_t *token);
58b89a8333Snatalie li - Sun Microsystems - Irvine United States 
597f667e74Sjose borrego static boolean_t smb_token_is_member(smb_token_t *, smb_sid_t *);
607f667e74Sjose borrego static uint32_t smb_token_setup_wingrps(smb_token_t *);
617f667e74Sjose borrego static smb_posix_grps_t *smb_token_create_pxgrps(uid_t);
62b89a8333Snatalie li - Sun Microsystems - Irvine United States 
6329bd2886SAlan Wright static void smb_guest_account(char *, size_t);
6429bd2886SAlan Wright 
65b89a8333Snatalie li - Sun Microsystems - Irvine United States /* Consolidation private function from Network Repository */
66b89a8333Snatalie li - Sun Microsystems - Irvine United States extern int _getgroupsbymember(const char *, gid_t[], int, int);
67b89a8333Snatalie li - Sun Microsystems - Irvine United States 
68b89a8333Snatalie li - Sun Microsystems - Irvine United States static idmap_stat
69b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_token_idmap(smb_token_t *token, smb_idmap_batch_t *sib)
70b89a8333Snatalie li - Sun Microsystems - Irvine United States {
71b89a8333Snatalie li - Sun Microsystems - Irvine United States 	idmap_stat stat;
72b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_idmap_t *sim;
73b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_id_t *id;
74b89a8333Snatalie li - Sun Microsystems - Irvine United States 	int i;
75b89a8333Snatalie li - Sun Microsystems - Irvine United States 
76b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (!token || !sib)
77b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (IDMAP_ERR_ARG);
78b89a8333Snatalie li - Sun Microsystems - Irvine United States 
79b89a8333Snatalie li - Sun Microsystems - Irvine United States 	sim = sib->sib_maps;
80b89a8333Snatalie li - Sun Microsystems - Irvine United States 
81b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (token->tkn_flags & SMB_ATF_ANON) {
827f667e74Sjose borrego 		token->tkn_user.i_id = UID_NOBODY;
837f667e74Sjose borrego 		token->tkn_owner.i_id = UID_NOBODY;
84b89a8333Snatalie li - Sun Microsystems - Irvine United States 	} else {
85b89a8333Snatalie li - Sun Microsystems - Irvine United States 		/* User SID */
867f667e74Sjose borrego 		id = &token->tkn_user;
87b89a8333Snatalie li - Sun Microsystems - Irvine United States 		sim->sim_id = &id->i_id;
88b89a8333Snatalie li - Sun Microsystems - Irvine United States 		stat = smb_idmap_batch_getid(sib->sib_idmaph, sim++,
897f667e74Sjose borrego 		    id->i_sid, SMB_IDMAP_USER);
90b89a8333Snatalie li - Sun Microsystems - Irvine United States 
91b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (stat != IDMAP_SUCCESS)
92b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (stat);
93b89a8333Snatalie li - Sun Microsystems - Irvine United States 
94b89a8333Snatalie li - Sun Microsystems - Irvine United States 		/* Owner SID */
957f667e74Sjose borrego 		id = &token->tkn_owner;
96b89a8333Snatalie li - Sun Microsystems - Irvine United States 		sim->sim_id = &id->i_id;
97b89a8333Snatalie li - Sun Microsystems - Irvine United States 		stat = smb_idmap_batch_getid(sib->sib_idmaph, sim++,
987f667e74Sjose borrego 		    id->i_sid, SMB_IDMAP_USER);
99b89a8333Snatalie li - Sun Microsystems - Irvine United States 
100b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (stat != IDMAP_SUCCESS)
101b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (stat);
102b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
103b89a8333Snatalie li - Sun Microsystems - Irvine United States 
104b89a8333Snatalie li - Sun Microsystems - Irvine United States 	/* Primary Group SID */
1057f667e74Sjose borrego 	id = &token->tkn_primary_grp;
106b89a8333Snatalie li - Sun Microsystems - Irvine United States 	sim->sim_id = &id->i_id;
1077f667e74Sjose borrego 	stat = smb_idmap_batch_getid(sib->sib_idmaph, sim++, id->i_sid,
1087f667e74Sjose borrego 	    SMB_IDMAP_GROUP);
109b89a8333Snatalie li - Sun Microsystems - Irvine United States 
110b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (stat != IDMAP_SUCCESS)
111b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (stat);
112b89a8333Snatalie li - Sun Microsystems - Irvine United States 
113b89a8333Snatalie li - Sun Microsystems - Irvine United States 	/* Other Windows Group SIDs */
1147f667e74Sjose borrego 	for (i = 0; i < token->tkn_win_grps.i_cnt; i++, sim++) {
1157f667e74Sjose borrego 		id = &token->tkn_win_grps.i_ids[i];
116b89a8333Snatalie li - Sun Microsystems - Irvine United States 		sim->sim_id = &id->i_id;
117b89a8333Snatalie li - Sun Microsystems - Irvine United States 		stat = smb_idmap_batch_getid(sib->sib_idmaph, sim,
1187f667e74Sjose borrego 		    id->i_sid, SMB_IDMAP_GROUP);
119b89a8333Snatalie li - Sun Microsystems - Irvine United States 
120b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (stat != IDMAP_SUCCESS)
121b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
122b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
123b89a8333Snatalie li - Sun Microsystems - Irvine United States 
124b89a8333Snatalie li - Sun Microsystems - Irvine United States 	return (stat);
125b89a8333Snatalie li - Sun Microsystems - Irvine United States }
126b89a8333Snatalie li - Sun Microsystems - Irvine United States 
127b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
128b89a8333Snatalie li - Sun Microsystems - Irvine United States  * smb_token_sids2ids
129b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
130b89a8333Snatalie li - Sun Microsystems - Irvine United States  * This will map all the SIDs of the access token to UIDs/GIDs.
131b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
132b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Returns 0 upon success.  Otherwise, returns -1.
133b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
134b89a8333Snatalie li - Sun Microsystems - Irvine United States static int
135b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_token_sids2ids(smb_token_t *token)
136b89a8333Snatalie li - Sun Microsystems - Irvine United States {
137b89a8333Snatalie li - Sun Microsystems - Irvine United States 	idmap_stat stat;
1381fdeec65Sjoyce mcintosh 	int nmaps;
139b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_idmap_batch_t sib;
140b89a8333Snatalie li - Sun Microsystems - Irvine United States 
141b89a8333Snatalie li - Sun Microsystems - Irvine United States 	/*
142b89a8333Snatalie li - Sun Microsystems - Irvine United States 	 * Number of idmap lookups: user SID, owner SID, primary group SID,
1437f667e74Sjose borrego 	 * and all Windows group SIDs. Skip user/owner SID for Anonymous.
144b89a8333Snatalie li - Sun Microsystems - Irvine United States 	 */
145b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (token->tkn_flags & SMB_ATF_ANON)
1467f667e74Sjose borrego 		nmaps = token->tkn_win_grps.i_cnt + 1;
147b89a8333Snatalie li - Sun Microsystems - Irvine United States 	else
1487f667e74Sjose borrego 		nmaps = token->tkn_win_grps.i_cnt + 3;
149b89a8333Snatalie li - Sun Microsystems - Irvine United States 
150b89a8333Snatalie li - Sun Microsystems - Irvine United States 	stat = smb_idmap_batch_create(&sib, nmaps, SMB_IDMAP_SID2ID);
151b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (stat != IDMAP_SUCCESS)
152b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (-1);
153b89a8333Snatalie li - Sun Microsystems - Irvine United States 
154b89a8333Snatalie li - Sun Microsystems - Irvine United States 	stat = smb_token_idmap(token, &sib);
155b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (stat != IDMAP_SUCCESS) {
156b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smb_idmap_batch_destroy(&sib);
157b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (-1);
158b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
159b89a8333Snatalie li - Sun Microsystems - Irvine United States 
160b89a8333Snatalie li - Sun Microsystems - Irvine United States 	stat = smb_idmap_batch_getmappings(&sib);
1619fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_idmap_check("smb_idmap_batch_getmappings", stat);
162*12b65585SGordon Ross 	smb_idmap_batch_destroy(&sib);
163b89a8333Snatalie li - Sun Microsystems - Irvine United States 
164b89a8333Snatalie li - Sun Microsystems - Irvine United States 	return (stat == IDMAP_SUCCESS ? 0 : -1);
165b89a8333Snatalie li - Sun Microsystems - Irvine United States }
166b89a8333Snatalie li - Sun Microsystems - Irvine United States 
167b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
168b89a8333Snatalie li - Sun Microsystems - Irvine United States  * smb_token_create_pxgrps
169b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
170b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Setup the POSIX group membership of the access token if the given UID is
171b89a8333Snatalie li - Sun Microsystems - Irvine United States  * a POSIX UID (non-ephemeral). Both the user's primary group and
172b89a8333Snatalie li - Sun Microsystems - Irvine United States  * supplementary groups will be added to the POSIX group array of the access
173b89a8333Snatalie li - Sun Microsystems - Irvine United States  * token.
174b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
175b89a8333Snatalie li - Sun Microsystems - Irvine United States static smb_posix_grps_t *
176b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_token_create_pxgrps(uid_t uid)
177b89a8333Snatalie li - Sun Microsystems - Irvine United States {
178b89a8333Snatalie li - Sun Microsystems - Irvine United States 	struct passwd *pwd;
179b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_posix_grps_t *pgrps;
180b89a8333Snatalie li - Sun Microsystems - Irvine United States 	int ngroups_max, num;
181b89a8333Snatalie li - Sun Microsystems - Irvine United States 	gid_t *gids;
182b89a8333Snatalie li - Sun Microsystems - Irvine United States 
183b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if ((ngroups_max = sysconf(_SC_NGROUPS_MAX)) < 0) {
184b89a8333Snatalie li - Sun Microsystems - Irvine United States 		syslog(LOG_ERR, "smb_logon: failed to get _SC_NGROUPS_MAX");
185b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NULL);
186b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
187b89a8333Snatalie li - Sun Microsystems - Irvine United States 
188b89a8333Snatalie li - Sun Microsystems - Irvine United States 	pwd = getpwuid(uid);
189b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (pwd == NULL) {
190b89a8333Snatalie li - Sun Microsystems - Irvine United States 		pgrps = malloc(sizeof (smb_posix_grps_t));
191b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (pgrps == NULL)
192b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (NULL);
193b89a8333Snatalie li - Sun Microsystems - Irvine United States 
194b89a8333Snatalie li - Sun Microsystems - Irvine United States 		pgrps->pg_ngrps = 0;
195b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (pgrps);
196b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
197b89a8333Snatalie li - Sun Microsystems - Irvine United States 
198b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (pwd->pw_name == NULL) {
199b89a8333Snatalie li - Sun Microsystems - Irvine United States 		pgrps = malloc(sizeof (smb_posix_grps_t));
200b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (pgrps == NULL)
201b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (NULL);
202b89a8333Snatalie li - Sun Microsystems - Irvine United States 
203b89a8333Snatalie li - Sun Microsystems - Irvine United States 		pgrps->pg_ngrps = 1;
204b89a8333Snatalie li - Sun Microsystems - Irvine United States 		pgrps->pg_grps[0] = pwd->pw_gid;
205b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (pgrps);
206b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
207b89a8333Snatalie li - Sun Microsystems - Irvine United States 
208b89a8333Snatalie li - Sun Microsystems - Irvine United States 	gids = (gid_t *)malloc(ngroups_max * sizeof (gid_t));
209b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (gids == NULL) {
210b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NULL);
211b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
212b89a8333Snatalie li - Sun Microsystems - Irvine United States 	bzero(gids, ngroups_max * sizeof (gid_t));
213b89a8333Snatalie li - Sun Microsystems - Irvine United States 
214b89a8333Snatalie li - Sun Microsystems - Irvine United States 	gids[0] = pwd->pw_gid;
215b89a8333Snatalie li - Sun Microsystems - Irvine United States 
216b89a8333Snatalie li - Sun Microsystems - Irvine United States 	/*
217b89a8333Snatalie li - Sun Microsystems - Irvine United States 	 * Setup the groups starting at index 1 (the last arg)
218b89a8333Snatalie li - Sun Microsystems - Irvine United States 	 * of gids array.
219b89a8333Snatalie li - Sun Microsystems - Irvine United States 	 */
220b89a8333Snatalie li - Sun Microsystems - Irvine United States 	num = _getgroupsbymember(pwd->pw_name, gids, ngroups_max, 1);
221b89a8333Snatalie li - Sun Microsystems - Irvine United States 
222b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (num == -1) {
223b89a8333Snatalie li - Sun Microsystems - Irvine United States 		syslog(LOG_ERR, "smb_logon: unable "
224b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    "to get user's supplementary groups");
225b89a8333Snatalie li - Sun Microsystems - Irvine United States 		num = 1;
226b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
227b89a8333Snatalie li - Sun Microsystems - Irvine United States 
228b89a8333Snatalie li - Sun Microsystems - Irvine United States 	pgrps = (smb_posix_grps_t *)malloc(SMB_POSIX_GRPS_SIZE(num));
229b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (pgrps) {
230b89a8333Snatalie li - Sun Microsystems - Irvine United States 		pgrps->pg_ngrps = num;
231b89a8333Snatalie li - Sun Microsystems - Irvine United States 		bcopy(gids, pgrps->pg_grps, num * sizeof (gid_t));
232b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
233b89a8333Snatalie li - Sun Microsystems - Irvine United States 
234b89a8333Snatalie li - Sun Microsystems - Irvine United States 	free(gids);
235b89a8333Snatalie li - Sun Microsystems - Irvine United States 	return (pgrps);
236b89a8333Snatalie li - Sun Microsystems - Irvine United States }
237b89a8333Snatalie li - Sun Microsystems - Irvine United States 
238b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
239b89a8333Snatalie li - Sun Microsystems - Irvine United States  * smb_token_destroy
240b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
241b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Release all of the memory associated with a token structure. Ensure
242b89a8333Snatalie li - Sun Microsystems - Irvine United States  * that the token has been unlinked before calling.
243b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
244b89a8333Snatalie li - Sun Microsystems - Irvine United States void
245b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_token_destroy(smb_token_t *token)
246b89a8333Snatalie li - Sun Microsystems - Irvine United States {
2477f667e74Sjose borrego 	if (token != NULL) {
2487f667e74Sjose borrego 		smb_sid_free(token->tkn_user.i_sid);
2497f667e74Sjose borrego 		smb_sid_free(token->tkn_owner.i_sid);
2507f667e74Sjose borrego 		smb_sid_free(token->tkn_primary_grp.i_sid);
2517f667e74Sjose borrego 		smb_ids_free(&token->tkn_win_grps);
252b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smb_privset_free(token->tkn_privileges);
253b89a8333Snatalie li - Sun Microsystems - Irvine United States 		free(token->tkn_posix_grps);
254b89a8333Snatalie li - Sun Microsystems - Irvine United States 		free(token->tkn_account_name);
255b89a8333Snatalie li - Sun Microsystems - Irvine United States 		free(token->tkn_domain_name);
256*12b65585SGordon Ross 		free(token->tkn_ssnkey.val);
2579fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		bzero(token, sizeof (smb_token_t));
258b89a8333Snatalie li - Sun Microsystems - Irvine United States 		free(token);
259b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
260b89a8333Snatalie li - Sun Microsystems - Irvine United States }
261b89a8333Snatalie li - Sun Microsystems - Irvine United States 
262b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
263b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Token owner should be set to local Administrators group
264b89a8333Snatalie li - Sun Microsystems - Irvine United States  * in two cases:
265b89a8333Snatalie li - Sun Microsystems - Irvine United States  *   1. The logged on user is a member of Domain Admins group
266b89a8333Snatalie li - Sun Microsystems - Irvine United States  *   2. he/she is a member of local Administrators group
267b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
2687f667e74Sjose borrego static void
2697f667e74Sjose borrego smb_token_set_owner(smb_token_t *token)
270b89a8333Snatalie li - Sun Microsystems - Irvine United States {
271b89a8333Snatalie li - Sun Microsystems - Irvine United States #ifdef SMB_SUPPORT_GROUP_OWNER
272b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_sid_t *owner_sid;
273b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2747f667e74Sjose borrego 	if (token->tkn_flags & SMB_ATF_ADMIN) {
2757f667e74Sjose borrego 		owner_sid = smb_wka_get_sid("Administrators");
2767f667e74Sjose borrego 		assert(owner_sid);
277b89a8333Snatalie li - Sun Microsystems - Irvine United States 	} else {
2787f667e74Sjose borrego 		owner_sid = token->tkn_user->i_sid;
279b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
280b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2817f667e74Sjose borrego 	token->tkn_owner.i_sid = smb_sid_dup(owner_sid);
282b89a8333Snatalie li - Sun Microsystems - Irvine United States #endif
2837f667e74Sjose borrego 	token->tkn_owner.i_sid = smb_sid_dup(token->tkn_user.i_sid);
284b89a8333Snatalie li - Sun Microsystems - Irvine United States }
285b89a8333Snatalie li - Sun Microsystems - Irvine United States 
286b89a8333Snatalie li - Sun Microsystems - Irvine United States static smb_privset_t *
2877f667e74Sjose borrego smb_token_create_privs(smb_token_t *token)
288b89a8333Snatalie li - Sun Microsystems - Irvine United States {
289b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_privset_t *privs;
290b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_giter_t gi;
291b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_group_t grp;
292b89a8333Snatalie li - Sun Microsystems - Irvine United States 	int rc;
293b89a8333Snatalie li - Sun Microsystems - Irvine United States 
294b89a8333Snatalie li - Sun Microsystems - Irvine United States 	privs = smb_privset_new();
295b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (privs == NULL)
296b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NULL);
297b89a8333Snatalie li - Sun Microsystems - Irvine United States 
298b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (smb_lgrp_iteropen(&gi) != SMB_LGRP_SUCCESS) {
299b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smb_privset_free(privs);
300b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NULL);
301b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
302b89a8333Snatalie li - Sun Microsystems - Irvine United States 
303b89a8333Snatalie li - Sun Microsystems - Irvine United States 	while (smb_lgrp_iterate(&gi, &grp) == SMB_LGRP_SUCCESS) {
3047f667e74Sjose borrego 		if (smb_lgrp_is_member(&grp, token->tkn_user.i_sid))
305b89a8333Snatalie li - Sun Microsystems - Irvine United States 			smb_privset_merge(privs, grp.sg_privs);
306b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smb_lgrp_free(&grp);
307b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
308b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_lgrp_iterclose(&gi);
309b89a8333Snatalie li - Sun Microsystems - Irvine United States 
3107f667e74Sjose borrego 	if (token->tkn_flags & SMB_ATF_ADMIN) {
31153d00481SYuri Pankov 		char admgrp[] = "Administrators";
31253d00481SYuri Pankov 
31353d00481SYuri Pankov 		rc = smb_lgrp_getbyname(admgrp, &grp);
314b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (rc == SMB_LGRP_SUCCESS) {
315b89a8333Snatalie li - Sun Microsystems - Irvine United States 			smb_privset_merge(privs, grp.sg_privs);
316b89a8333Snatalie li - Sun Microsystems - Irvine United States 			smb_lgrp_free(&grp);
317b89a8333Snatalie li - Sun Microsystems - Irvine United States 		}
318b89a8333Snatalie li - Sun Microsystems - Irvine United States 
319b89a8333Snatalie li - Sun Microsystems - Irvine United States 		/*
320b89a8333Snatalie li - Sun Microsystems - Irvine United States 		 * This privilege is required to view/edit SACL
321b89a8333Snatalie li - Sun Microsystems - Irvine United States 		 */
322b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smb_privset_enable(privs, SE_SECURITY_LUID);
323b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
324b89a8333Snatalie li - Sun Microsystems - Irvine United States 
325b89a8333Snatalie li - Sun Microsystems - Irvine United States 	return (privs);
326b89a8333Snatalie li - Sun Microsystems - Irvine United States }
327b89a8333Snatalie li - Sun Microsystems - Irvine United States 
328b89a8333Snatalie li - Sun Microsystems - Irvine United States static void
3297f667e74Sjose borrego smb_token_set_flags(smb_token_t *token)
330b89a8333Snatalie li - Sun Microsystems - Irvine United States {
3317f667e74Sjose borrego 	if (smb_token_is_member(token, smb_wka_get_sid("Administrators")))
332b89a8333Snatalie li - Sun Microsystems - Irvine United States 		token->tkn_flags |= SMB_ATF_ADMIN;
333b89a8333Snatalie li - Sun Microsystems - Irvine United States 
3347f667e74Sjose borrego 	if (smb_token_is_member(token, smb_wka_get_sid("Power Users")))
335b89a8333Snatalie li - Sun Microsystems - Irvine United States 		token->tkn_flags |= SMB_ATF_POWERUSER;
336b89a8333Snatalie li - Sun Microsystems - Irvine United States 
3377f667e74Sjose borrego 	if (smb_token_is_member(token, smb_wka_get_sid("Backup Operators")))
338b89a8333Snatalie li - Sun Microsystems - Irvine United States 		token->tkn_flags |= SMB_ATF_BACKUPOP;
339b89a8333Snatalie li - Sun Microsystems - Irvine United States }
340b89a8333Snatalie li - Sun Microsystems - Irvine United States 
341b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
3427f667e74Sjose borrego  * Common token setup for both local and domain users.
3437f667e74Sjose borrego  * This function must be called after the initial setup
3447f667e74Sjose borrego  * has been done.
345b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
3467f667e74Sjose borrego  * Note that the order of calls in this function are important.
347*12b65585SGordon Ross  *
348*12b65585SGordon Ross  * Returns B_TRUE for success.
349b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
350*12b65585SGordon Ross boolean_t
3517f667e74Sjose borrego smb_token_setup_common(smb_token_t *token)
352b89a8333Snatalie li - Sun Microsystems - Irvine United States {
3537f667e74Sjose borrego 	smb_token_set_flags(token);
354b89a8333Snatalie li - Sun Microsystems - Irvine United States 
3557f667e74Sjose borrego 	smb_token_set_owner(token);
3567f667e74Sjose borrego 	if (token->tkn_owner.i_sid == NULL)
3579fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (B_FALSE);
358b89a8333Snatalie li - Sun Microsystems - Irvine United States 
359b89a8333Snatalie li - Sun Microsystems - Irvine United States 	/* Privileges */
3607f667e74Sjose borrego 	token->tkn_privileges = smb_token_create_privs(token);
3617f667e74Sjose borrego 	if (token->tkn_privileges == NULL)
3629fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (B_FALSE);
363b89a8333Snatalie li - Sun Microsystems - Irvine United States 
364b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (smb_token_sids2ids(token) != 0) {
365b89a8333Snatalie li - Sun Microsystems - Irvine United States 		syslog(LOG_ERR, "%s\\%s: idmap failed",
3667f667e74Sjose borrego 		    token->tkn_domain_name, token->tkn_account_name);
3679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (B_FALSE);
368b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
369b89a8333Snatalie li - Sun Microsystems - Irvine United States 
370b89a8333Snatalie li - Sun Microsystems - Irvine United States 	/* Solaris Groups */
3717f667e74Sjose borrego 	token->tkn_posix_grps = smb_token_create_pxgrps(token->tkn_user.i_id);
372b89a8333Snatalie li - Sun Microsystems - Irvine United States 
3739fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	return (smb_token_valid(token));
374b89a8333Snatalie li - Sun Microsystems - Irvine United States }
375b89a8333Snatalie li - Sun Microsystems - Irvine United States 
37629bd2886SAlan Wright uint32_t
37729bd2886SAlan Wright smb_logon_init(void)
37829bd2886SAlan Wright {
37929bd2886SAlan Wright 	uint32_t status;
38029bd2886SAlan Wright 
38129bd2886SAlan Wright 	(void) rw_wrlock(&smb_logoninit_rwl);
38229bd2886SAlan Wright 	status = smb_sam_lookup_name(NULL, "guest", SidTypeUser, &smb_guest);
38329bd2886SAlan Wright 	if (status != NT_STATUS_SUCCESS) {
38429bd2886SAlan Wright 		(void) rw_unlock(&smb_logoninit_rwl);
38529bd2886SAlan Wright 		return (status);
38629bd2886SAlan Wright 	}
38729bd2886SAlan Wright 
38829bd2886SAlan Wright 	status = smb_sam_lookup_name(NULL, "domain users", SidTypeGroup,
38929bd2886SAlan Wright 	    &smb_domusers);
39029bd2886SAlan Wright 	if (status != NT_STATUS_SUCCESS) {
39129bd2886SAlan Wright 		smb_account_free(&smb_guest);
39229bd2886SAlan Wright 		bzero(&smb_guest, sizeof (smb_account_t));
39329bd2886SAlan Wright 		(void) rw_unlock(&smb_logoninit_rwl);
39429bd2886SAlan Wright 		return (status);
39529bd2886SAlan Wright 	}
39629bd2886SAlan Wright 
39729bd2886SAlan Wright 	(void) rw_unlock(&smb_logoninit_rwl);
39829bd2886SAlan Wright 	return (status);
39929bd2886SAlan Wright }
40029bd2886SAlan Wright 
40129bd2886SAlan Wright void
40229bd2886SAlan Wright smb_logon_fini(void)
40329bd2886SAlan Wright {
40429bd2886SAlan Wright 	(void) rw_wrlock(&smb_logoninit_rwl);
40529bd2886SAlan Wright 	smb_account_free(&smb_guest);
40629bd2886SAlan Wright 	smb_account_free(&smb_domusers);
40729bd2886SAlan Wright 	bzero(&smb_guest, sizeof (smb_account_t));
40829bd2886SAlan Wright 	bzero(&smb_domusers, sizeof (smb_account_t));
40929bd2886SAlan Wright 	(void) rw_unlock(&smb_logoninit_rwl);
41029bd2886SAlan Wright }
41129bd2886SAlan Wright 
412b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
4139fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Perform user authentication.
414b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
4159fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * The dispatched functions must only update the user_info status if they
4169fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * attempt to authenticate the user.
417b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
4189fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * On success, a pointer to a new access token is returned.
419b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
420b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_token_t *
4219fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_logon(smb_logon_t *user_info)
422b89a8333Snatalie li - Sun Microsystems - Irvine United States {
4239fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	static smb_logonop_t	ops[] = {
4249fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		smb_logon_anon,
4259fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		smb_logon_local,
4269fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		smb_logon_domain,
4279fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		smb_logon_guest
4289fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	};
429b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_token_t		*token = NULL;
4309fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_domain_t		domain;
4319fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	int			n_op = (sizeof (ops) / sizeof (ops[0]));
4329fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	int			i;
433b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	user_info->lg_secmode = smb_config_get_secmode();
4359fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	user_info->lg_status = NT_STATUS_NO_SUCH_USER;
4369fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
4379fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_domain_lookup_name(user_info->lg_e_domain, &domain))
4389fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		user_info->lg_domain_type = domain.di_type;
4399fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	else
4409fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		user_info->lg_domain_type = SMB_DOMAIN_NULL;
4419fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
4429fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((token = calloc(1, sizeof (smb_token_t))) == NULL) {
4439fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		syslog(LOG_ERR, "logon[%s\\%s]: %m",
4449fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    user_info->lg_e_domain, user_info->lg_e_username);
445b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NULL);
4467f667e74Sjose borrego 	}
447b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4489fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	for (i = 0; i < n_op; ++i) {
4499fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		(*ops[i])(user_info, token);
4509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
4519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		if (user_info->lg_status == NT_STATUS_SUCCESS)
4529fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			break;
4537f667e74Sjose borrego 	}
45429bd2886SAlan Wright 
4559fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (user_info->lg_status == NT_STATUS_SUCCESS) {
4569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		if (smb_token_setup_common(token))
457b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (token);
458b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
459b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4607f667e74Sjose borrego 	smb_token_destroy(token);
4617f667e74Sjose borrego 	return (NULL);
4627f667e74Sjose borrego }
4637f667e74Sjose borrego 
464b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
4659fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * If the user has an entry in the local database, attempt local authentication.
466b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
4679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * In domain mode, we try to exclude domain accounts, which we do by only
4689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * accepting local or null (blank) domain names here.  Some clients (Mac OS)
4699fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * don't always send the domain name.
470b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
4719fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * If we are not going to attempt authentication, this function must return
4729fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * without updating the status.
473b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
4749fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static void
4759fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_logon_local(smb_logon_t *user_info, smb_token_t *token)
476b89a8333Snatalie li - Sun Microsystems - Irvine United States {
47729bd2886SAlan Wright 	char guest[SMB_USERNAME_MAXLEN];
478b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_passwd_t smbpw;
479b89a8333Snatalie li - Sun Microsystems - Irvine United States 	uint32_t status;
480b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4819fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (user_info->lg_secmode == SMB_SECMODE_DOMAIN) {
4829fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		if ((user_info->lg_domain_type != SMB_DOMAIN_LOCAL) &&
4839fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    (user_info->lg_domain_type != SMB_DOMAIN_NULL))
4849fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			return;
485b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
486b89a8333Snatalie li - Sun Microsystems - Irvine United States 
487*12b65585SGordon Ross 	/*
488*12b65585SGordon Ross 	 * If the requested account name is "guest" (or whatever
489*12b65585SGordon Ross 	 * our guest account is named) then don't handle it here.
490*12b65585SGordon Ross 	 * Let this request fall through to smb_logon_guest().
491*12b65585SGordon Ross 	 */
49229bd2886SAlan Wright 	smb_guest_account(guest, SMB_USERNAME_MAXLEN);
493*12b65585SGordon Ross 	if (smb_strcasecmp(guest, user_info->lg_e_username, 0) == 0)
494*12b65585SGordon Ross 		return;
495b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4969fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	status = smb_token_auth_local(user_info, token, &smbpw);
497*12b65585SGordon Ross 	if (status == NT_STATUS_SUCCESS)
4987f667e74Sjose borrego 		status = smb_token_setup_local(&smbpw, token);
49929bd2886SAlan Wright 
5009fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	user_info->lg_status = status;
50129bd2886SAlan Wright }
50229bd2886SAlan Wright 
50329bd2886SAlan Wright /*
5049fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Guest authentication.  This may be a local guest account or the guest
5059fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * account may be mapped to a local account.  These accounts are regular
5069fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * accounts with normal password protection.
5079fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  *
5089fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Only proceed with a guest logon if previous logon options have resulted
5099fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * in NO_SUCH_USER.
5109fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  *
5119fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * If we are not going to attempt authentication, this function must return
5129fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * without updating the status.
51329bd2886SAlan Wright  */
5149fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static void
5159fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_logon_guest(smb_logon_t *user_info, smb_token_t *token)
51629bd2886SAlan Wright {
51729bd2886SAlan Wright 	char guest[SMB_USERNAME_MAXLEN];
51829bd2886SAlan Wright 	smb_passwd_t smbpw;
51929bd2886SAlan Wright 	char *temp;
52029bd2886SAlan Wright 
5219fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (user_info->lg_status != NT_STATUS_NO_SUCH_USER)
5229fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return;
52329bd2886SAlan Wright 
524*12b65585SGordon Ross 	/* Get the name of the guest account. */
5259fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_guest_account(guest, SMB_USERNAME_MAXLEN);
526*12b65585SGordon Ross 
527*12b65585SGordon Ross 	/* Does the guest account exist? */
528*12b65585SGordon Ross 	if (smb_pwd_getpwnam(guest, &smbpw) == NULL)
529*12b65585SGordon Ross 		return;
530*12b65585SGordon Ross 
531*12b65585SGordon Ross 	/* Is it enabled? (empty p/w is OK) */
532*12b65585SGordon Ross 	if (smbpw.pw_flags & SMB_PWF_DISABLE)
533*12b65585SGordon Ross 		return;
534*12b65585SGordon Ross 
535*12b65585SGordon Ross 	/*
536*12b65585SGordon Ross 	 * OK, give the client a guest logon.  Note that on entry,
537*12b65585SGordon Ross 	 * lg_e_username is typically something other than "guest"
538*12b65585SGordon Ross 	 * so we need to set the effective username when createing
539*12b65585SGordon Ross 	 * the guest token.
540*12b65585SGordon Ross 	 */
5419fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	temp = user_info->lg_e_username;
5429fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	user_info->lg_e_username = guest;
543*12b65585SGordon Ross 	user_info->lg_status = smb_token_setup_guest(user_info, token);
5449fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	user_info->lg_e_username = temp;
545b89a8333Snatalie li - Sun Microsystems - Irvine United States }
546b89a8333Snatalie li - Sun Microsystems - Irvine United States 
547b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
5489fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * If user_info represents an anonymous user then setup the token.
5499fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Otherwise return without updating the status.
5509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  */
5519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static void
5529fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_logon_anon(smb_logon_t *user_info, smb_token_t *token)
5539fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
5549fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (user_info->lg_flags & SMB_ATF_ANON)
5559fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		user_info->lg_status = smb_token_setup_anon(token);
5569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
5579fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
5589fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States /*
5599fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Try both LM hash and NT hashes with user's password(s) to authenticate
5609fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * the user.
561b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
562b89a8333Snatalie li - Sun Microsystems - Irvine United States static uint32_t
5639fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_token_auth_local(smb_logon_t *user_info, smb_token_t *token,
56429bd2886SAlan Wright     smb_passwd_t *smbpw)
56529bd2886SAlan Wright {
566*12b65585SGordon Ross 	boolean_t ok;
56729bd2886SAlan Wright 	uint32_t status = NT_STATUS_SUCCESS;
56829bd2886SAlan Wright 
5699fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_pwd_getpwnam(user_info->lg_e_username, smbpw) == NULL)
57029bd2886SAlan Wright 		return (NT_STATUS_NO_SUCH_USER);
57129bd2886SAlan Wright 
57229bd2886SAlan Wright 	if (smbpw->pw_flags & SMB_PWF_DISABLE)
57329bd2886SAlan Wright 		return (NT_STATUS_ACCOUNT_DISABLED);
57429bd2886SAlan Wright 
575*12b65585SGordon Ross 	if ((smbpw->pw_flags & (SMB_PWF_LM | SMB_PWF_NT)) == 0) {
576*12b65585SGordon Ross 		/*
577*12b65585SGordon Ross 		 * The SMB passwords have not been set.
578*12b65585SGordon Ross 		 * Return an error that suggests the
579*12b65585SGordon Ross 		 * password needs to be set.
580*12b65585SGordon Ross 		 */
581*12b65585SGordon Ross 		return (NT_STATUS_PASSWORD_EXPIRED);
58229bd2886SAlan Wright 	}
58329bd2886SAlan Wright 
584*12b65585SGordon Ross 	token->tkn_ssnkey.val = malloc(SMBAUTH_SESSION_KEY_SZ);
585*12b65585SGordon Ross 	if (token->tkn_ssnkey.val == NULL)
58629bd2886SAlan Wright 		return (NT_STATUS_NO_MEMORY);
587*12b65585SGordon Ross 	token->tkn_ssnkey.len = SMBAUTH_SESSION_KEY_SZ;
588*12b65585SGordon Ross 
589*12b65585SGordon Ross 	ok = smb_auth_validate(
59029bd2886SAlan Wright 	    smbpw,
5919fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    user_info->lg_domain,
5929fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    user_info->lg_username,
593*12b65585SGordon Ross 	    user_info->lg_challenge_key.val,
594*12b65585SGordon Ross 	    user_info->lg_challenge_key.len,
595*12b65585SGordon Ross 	    user_info->lg_nt_password.val,
596*12b65585SGordon Ross 	    user_info->lg_nt_password.len,
597*12b65585SGordon Ross 	    user_info->lg_lm_password.val,
598*12b65585SGordon Ross 	    user_info->lg_lm_password.len,
599*12b65585SGordon Ross 	    token->tkn_ssnkey.val);
600*12b65585SGordon Ross 	if (ok)
601*12b65585SGordon Ross 		return (NT_STATUS_SUCCESS);
60229bd2886SAlan Wright 
603*12b65585SGordon Ross 	free(token->tkn_ssnkey.val);
604*12b65585SGordon Ross 	token->tkn_ssnkey.val = NULL;
605*12b65585SGordon Ross 	token->tkn_ssnkey.len = 0;
606*12b65585SGordon Ross 
60729bd2886SAlan Wright 	status = NT_STATUS_WRONG_PASSWORD;
6089fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	syslog(LOG_NOTICE, "logon[%s\\%s]: %s",
6099fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    user_info->lg_e_domain, user_info->lg_e_username,
61029bd2886SAlan Wright 	    xlate_nt_status(status));
61129bd2886SAlan Wright 
61229bd2886SAlan Wright 	return (status);
61329bd2886SAlan Wright }
61429bd2886SAlan Wright 
615b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
6167f667e74Sjose borrego  * Setup an access token for the specified local user.
617b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
618b89a8333Snatalie li - Sun Microsystems - Irvine United States static uint32_t
6197f667e74Sjose borrego smb_token_setup_local(smb_passwd_t *smbpw, smb_token_t *token)
620b89a8333Snatalie li - Sun Microsystems - Irvine United States {
621b89a8333Snatalie li - Sun Microsystems - Irvine United States 	idmap_stat stat;
622b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_idmap_batch_t sib;
623b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_idmap_t *umap, *gmap;
624b89a8333Snatalie li - Sun Microsystems - Irvine United States 	struct passwd pw;
625b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char pwbuf[1024];
626b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char nbname[NETBIOS_NAME_SZ];
627b89a8333Snatalie li - Sun Microsystems - Irvine United States 
628b89a8333Snatalie li - Sun Microsystems - Irvine United States 	(void) smb_getnetbiosname(nbname, sizeof (nbname));
6297f667e74Sjose borrego 	token->tkn_account_name = strdup(smbpw->pw_name);
6307f667e74Sjose borrego 	token->tkn_domain_name = strdup(nbname);
631b89a8333Snatalie li - Sun Microsystems - Irvine United States 
6327f667e74Sjose borrego 	if (token->tkn_account_name == NULL ||
6337f667e74Sjose borrego 	    token->tkn_domain_name == NULL)
634b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NT_STATUS_NO_MEMORY);
635b89a8333Snatalie li - Sun Microsystems - Irvine United States 
6367f667e74Sjose borrego 	if (getpwuid_r(smbpw->pw_uid, &pw, pwbuf, sizeof (pwbuf)) == NULL)
637b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NT_STATUS_NO_SUCH_USER);
638b89a8333Snatalie li - Sun Microsystems - Irvine United States 
639b89a8333Snatalie li - Sun Microsystems - Irvine United States 	/* Get the SID for user's uid & gid */
640b89a8333Snatalie li - Sun Microsystems - Irvine United States 	stat = smb_idmap_batch_create(&sib, 2, SMB_IDMAP_ID2SID);
6417f667e74Sjose borrego 	if (stat != IDMAP_SUCCESS)
642b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NT_STATUS_INTERNAL_ERROR);
643b89a8333Snatalie li - Sun Microsystems - Irvine United States 
644b89a8333Snatalie li - Sun Microsystems - Irvine United States 	umap = &sib.sib_maps[0];
645b89a8333Snatalie li - Sun Microsystems - Irvine United States 	stat = smb_idmap_batch_getsid(sib.sib_idmaph, umap, pw.pw_uid,
646b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    SMB_IDMAP_USER);
647b89a8333Snatalie li - Sun Microsystems - Irvine United States 
648b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (stat != IDMAP_SUCCESS) {
649b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smb_idmap_batch_destroy(&sib);
650b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NT_STATUS_INTERNAL_ERROR);
651b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
652b89a8333Snatalie li - Sun Microsystems - Irvine United States 
653b89a8333Snatalie li - Sun Microsystems - Irvine United States 	gmap = &sib.sib_maps[1];
654b89a8333Snatalie li - Sun Microsystems - Irvine United States 	stat = smb_idmap_batch_getsid(sib.sib_idmaph, gmap, pw.pw_gid,
655b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    SMB_IDMAP_GROUP);
656b89a8333Snatalie li - Sun Microsystems - Irvine United States 
657b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (stat != IDMAP_SUCCESS) {
658b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smb_idmap_batch_destroy(&sib);
659b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NT_STATUS_INTERNAL_ERROR);
660b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
661b89a8333Snatalie li - Sun Microsystems - Irvine United States 
6627f667e74Sjose borrego 	if (smb_idmap_batch_getmappings(&sib) != IDMAP_SUCCESS)
663b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NT_STATUS_INTERNAL_ERROR);
664b89a8333Snatalie li - Sun Microsystems - Irvine United States 
6657f667e74Sjose borrego 	token->tkn_user.i_sid = smb_sid_dup(umap->sim_sid);
6667f667e74Sjose borrego 	token->tkn_primary_grp.i_sid = smb_sid_dup(gmap->sim_sid);
667b89a8333Snatalie li - Sun Microsystems - Irvine United States 
668b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_idmap_batch_destroy(&sib);
669b89a8333Snatalie li - Sun Microsystems - Irvine United States 
6707f667e74Sjose borrego 	if (token->tkn_user.i_sid == NULL ||
6717f667e74Sjose borrego 	    token->tkn_primary_grp.i_sid == NULL)
672b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NT_STATUS_NO_MEMORY);
673b89a8333Snatalie li - Sun Microsystems - Irvine United States 
6747f667e74Sjose borrego 	return (smb_token_setup_wingrps(token));
675b89a8333Snatalie li - Sun Microsystems - Irvine United States }
676b89a8333Snatalie li - Sun Microsystems - Irvine United States 
677b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
67829bd2886SAlan Wright  * Setup access token for guest connections
67929bd2886SAlan Wright  */
68029bd2886SAlan Wright static uint32_t
6819fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_token_setup_guest(smb_logon_t *user_info, smb_token_t *token)
68229bd2886SAlan Wright {
6839fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	token->tkn_account_name = strdup(user_info->lg_e_username);
68429bd2886SAlan Wright 
68529bd2886SAlan Wright 	(void) rw_rdlock(&smb_logoninit_rwl);
68629bd2886SAlan Wright 	token->tkn_domain_name = strdup(smb_guest.a_domain);
68729bd2886SAlan Wright 	token->tkn_user.i_sid = smb_sid_dup(smb_guest.a_sid);
68829bd2886SAlan Wright 	token->tkn_primary_grp.i_sid = smb_sid_dup(smb_domusers.a_sid);
68929bd2886SAlan Wright 	(void) rw_unlock(&smb_logoninit_rwl);
69029bd2886SAlan Wright 	token->tkn_flags = SMB_ATF_GUEST;
69129bd2886SAlan Wright 
69229bd2886SAlan Wright 	if (token->tkn_account_name == NULL ||
69329bd2886SAlan Wright 	    token->tkn_domain_name == NULL ||
69429bd2886SAlan Wright 	    token->tkn_user.i_sid == NULL ||
69529bd2886SAlan Wright 	    token->tkn_primary_grp.i_sid == NULL)
69629bd2886SAlan Wright 		return (NT_STATUS_NO_MEMORY);
69729bd2886SAlan Wright 
69829bd2886SAlan Wright 	return (smb_token_setup_wingrps(token));
69929bd2886SAlan Wright }
70029bd2886SAlan Wright 
70129bd2886SAlan Wright /*
70229bd2886SAlan Wright  * Setup access token for anonymous connections
703b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
7047f667e74Sjose borrego static uint32_t
7057f667e74Sjose borrego smb_token_setup_anon(smb_token_t *token)
706b89a8333Snatalie li - Sun Microsystems - Irvine United States {
7077f667e74Sjose borrego 	smb_sid_t *user_sid;
708b89a8333Snatalie li - Sun Microsystems - Irvine United States 
7097f667e74Sjose borrego 	token->tkn_account_name = strdup("Anonymous");
7107f667e74Sjose borrego 	token->tkn_domain_name = strdup("NT Authority");
7117f667e74Sjose borrego 	user_sid = smb_wka_get_sid("Anonymous");
7127f667e74Sjose borrego 	token->tkn_user.i_sid = smb_sid_dup(user_sid);
7137f667e74Sjose borrego 	token->tkn_primary_grp.i_sid = smb_sid_dup(user_sid);
7147f667e74Sjose borrego 	token->tkn_flags = SMB_ATF_ANON;
715b89a8333Snatalie li - Sun Microsystems - Irvine United States 
7167f667e74Sjose borrego 	if (token->tkn_account_name == NULL ||
7177f667e74Sjose borrego 	    token->tkn_domain_name == NULL ||
7187f667e74Sjose borrego 	    token->tkn_user.i_sid == NULL ||
7197f667e74Sjose borrego 	    token->tkn_primary_grp.i_sid == NULL)
7207f667e74Sjose borrego 		return (NT_STATUS_NO_MEMORY);
7217f667e74Sjose borrego 
7227f667e74Sjose borrego 	return (smb_token_setup_wingrps(token));
723b89a8333Snatalie li - Sun Microsystems - Irvine United States }
724b89a8333Snatalie li - Sun Microsystems - Irvine United States 
725b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
726b89a8333Snatalie li - Sun Microsystems - Irvine United States  * smb_token_user_sid
727b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
728b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Return a pointer to the user SID in the specified token. A null
729b89a8333Snatalie li - Sun Microsystems - Irvine United States  * pointer indicates an error.
730b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
731b89a8333Snatalie li - Sun Microsystems - Irvine United States static smb_sid_t *
732b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_token_user_sid(smb_token_t *token)
733b89a8333Snatalie li - Sun Microsystems - Irvine United States {
7347f667e74Sjose borrego 	return ((token) ? token->tkn_user.i_sid : NULL);
735b89a8333Snatalie li - Sun Microsystems - Irvine United States }
736b89a8333Snatalie li - Sun Microsystems - Irvine United States 
737b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
738b89a8333Snatalie li - Sun Microsystems - Irvine United States  * smb_token_group_sid
739b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
740b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Return a pointer to the group SID as indicated by the iterator.
741b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Setting the iterator to 0 before calling this function will return
742b89a8333Snatalie li - Sun Microsystems - Irvine United States  * the first group, which will always be the primary group. The
743b89a8333Snatalie li - Sun Microsystems - Irvine United States  * iterator will be incremented before returning the SID so that this
744b89a8333Snatalie li - Sun Microsystems - Irvine United States  * function can be used to cycle through the groups. The caller can
745b89a8333Snatalie li - Sun Microsystems - Irvine United States  * adjust the iterator as required between calls to obtain any specific
746b89a8333Snatalie li - Sun Microsystems - Irvine United States  * group.
747b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
748b89a8333Snatalie li - Sun Microsystems - Irvine United States  * On success a pointer to the appropriate group SID will be returned.
749b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Otherwise a null pointer will be returned.
750b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
751b89a8333Snatalie li - Sun Microsystems - Irvine United States static smb_sid_t *
752b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_token_group_sid(smb_token_t *token, int *iterator)
753b89a8333Snatalie li - Sun Microsystems - Irvine United States {
754b89a8333Snatalie li - Sun Microsystems - Irvine United States 	int index;
755b89a8333Snatalie li - Sun Microsystems - Irvine United States 
7567f667e74Sjose borrego 	if (token == NULL || iterator == NULL)
757b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NULL);
758b89a8333Snatalie li - Sun Microsystems - Irvine United States 
7597f667e74Sjose borrego 	if (token->tkn_win_grps.i_ids == NULL)
760b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NULL);
761b89a8333Snatalie li - Sun Microsystems - Irvine United States 
762b89a8333Snatalie li - Sun Microsystems - Irvine United States 	index = *iterator;
763b89a8333Snatalie li - Sun Microsystems - Irvine United States 
7647f667e74Sjose borrego 	if (index < 0 || index >= token->tkn_win_grps.i_cnt)
765b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NULL);
766b89a8333Snatalie li - Sun Microsystems - Irvine United States 
767b89a8333Snatalie li - Sun Microsystems - Irvine United States 	++(*iterator);
7687f667e74Sjose borrego 	return (token->tkn_win_grps.i_ids[index].i_sid);
769b89a8333Snatalie li - Sun Microsystems - Irvine United States }
770b89a8333Snatalie li - Sun Microsystems - Irvine United States 
771b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
772b89a8333Snatalie li - Sun Microsystems - Irvine United States  * smb_token_is_member
773b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
774b89a8333Snatalie li - Sun Microsystems - Irvine United States  * This function will determine whether or not the specified SID is a
775b89a8333Snatalie li - Sun Microsystems - Irvine United States  * member of a token. The user SID and all group SIDs are tested.
776b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Returns 1 if the SID is a member of the token. Otherwise returns 0.
777b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
7787f667e74Sjose borrego static boolean_t
779b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_token_is_member(smb_token_t *token, smb_sid_t *sid)
780b89a8333Snatalie li - Sun Microsystems - Irvine United States {
781b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_sid_t *tsid;
782b89a8333Snatalie li - Sun Microsystems - Irvine United States 	int iterator = 0;
783b89a8333Snatalie li - Sun Microsystems - Irvine United States 
7847f667e74Sjose borrego 	if (token == NULL || sid == NULL)
7857f667e74Sjose borrego 		return (B_FALSE);
7867f667e74Sjose borrego 
787b89a8333Snatalie li - Sun Microsystems - Irvine United States 	tsid = smb_token_user_sid(token);
788b89a8333Snatalie li - Sun Microsystems - Irvine United States 	while (tsid) {
789b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (smb_sid_cmp(tsid, sid))
7907f667e74Sjose borrego 			return (B_TRUE);
791b89a8333Snatalie li - Sun Microsystems - Irvine United States 
792b89a8333Snatalie li - Sun Microsystems - Irvine United States 		tsid = smb_token_group_sid(token, &iterator);
793b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
794b89a8333Snatalie li - Sun Microsystems - Irvine United States 
7957f667e74Sjose borrego 	return (B_FALSE);
796b89a8333Snatalie li - Sun Microsystems - Irvine United States }
797b89a8333Snatalie li - Sun Microsystems - Irvine United States 
798b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
799b89a8333Snatalie li - Sun Microsystems - Irvine United States  * smb_token_log
800b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
801b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Diagnostic routine to write the contents of a token to the log.
802b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
803b89a8333Snatalie li - Sun Microsystems - Irvine United States void
804b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_token_log(smb_token_t *token)
805b89a8333Snatalie li - Sun Microsystems - Irvine United States {
8067f667e74Sjose borrego 	smb_ids_t *w_grps;
8077f667e74Sjose borrego 	smb_id_t *grp;
808b89a8333Snatalie li - Sun Microsystems - Irvine United States 	smb_posix_grps_t *x_grps;
809b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char sidstr[SMB_SID_STRSZ];
810b89a8333Snatalie li - Sun Microsystems - Irvine United States 	int i;
811b89a8333Snatalie li - Sun Microsystems - Irvine United States 
812b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (token == NULL)
813b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return;
814b89a8333Snatalie li - Sun Microsystems - Irvine United States 
815b89a8333Snatalie li - Sun Microsystems - Irvine United States 	syslog(LOG_DEBUG, "Token for %s\\%s",
816b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    (token->tkn_domain_name) ? token->tkn_domain_name : "-NULL-",
817b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    (token->tkn_account_name) ? token->tkn_account_name : "-NULL-");
818b89a8333Snatalie li - Sun Microsystems - Irvine United States 
8197f667e74Sjose borrego 	syslog(LOG_DEBUG, "   User->Attr: %d", token->tkn_user.i_attrs);
8207f667e74Sjose borrego 	smb_sid_tostr((smb_sid_t *)token->tkn_user.i_sid, sidstr);
8217f667e74Sjose borrego 	syslog(LOG_DEBUG, "   User->Sid: %s (id=%u)", sidstr,
8227f667e74Sjose borrego 	    token->tkn_user.i_id);
823b89a8333Snatalie li - Sun Microsystems - Irvine United States 
8247f667e74Sjose borrego 	smb_sid_tostr((smb_sid_t *)token->tkn_owner.i_sid, sidstr);
825b89a8333Snatalie li - Sun Microsystems - Irvine United States 	syslog(LOG_DEBUG, "   Ownr->Sid: %s (id=%u)",
8267f667e74Sjose borrego 	    sidstr, token->tkn_owner.i_id);
827b89a8333Snatalie li - Sun Microsystems - Irvine United States 
8287f667e74Sjose borrego 	smb_sid_tostr((smb_sid_t *)token->tkn_primary_grp.i_sid, sidstr);
829b89a8333Snatalie li - Sun Microsystems - Irvine United States 	syslog(LOG_DEBUG, "   PGrp->Sid: %s (id=%u)",
8307f667e74Sjose borrego 	    sidstr, token->tkn_primary_grp.i_id);
831b89a8333Snatalie li - Sun Microsystems - Irvine United States 
8327f667e74Sjose borrego 	w_grps = &token->tkn_win_grps;
8337f667e74Sjose borrego 	if (w_grps->i_ids) {
8347f667e74Sjose borrego 		syslog(LOG_DEBUG, "   Windows groups: %d", w_grps->i_cnt);
8357f667e74Sjose borrego 		grp = w_grps->i_ids;
8367f667e74Sjose borrego 		for (i = 0; i < w_grps->i_cnt; ++i, grp++) {
837b89a8333Snatalie li - Sun Microsystems - Irvine United States 			syslog(LOG_DEBUG,
8387f667e74Sjose borrego 			    "    Grp[%d].Attr:%d", i, grp->i_attrs);
8397f667e74Sjose borrego 			if (grp->i_sid != NULL) {
8407f667e74Sjose borrego 				smb_sid_tostr((smb_sid_t *)grp->i_sid, sidstr);
841b89a8333Snatalie li - Sun Microsystems - Irvine United States 				syslog(LOG_DEBUG,
842b89a8333Snatalie li - Sun Microsystems - Irvine United States 				    "    Grp[%d].Sid: %s (id=%u)", i, sidstr,
8437f667e74Sjose borrego 				    grp->i_id);
844b89a8333Snatalie li - Sun Microsystems - Irvine United States 			}
845b89a8333Snatalie li - Sun Microsystems - Irvine United States 		}
8467f667e74Sjose borrego 	} else {
847b89a8333Snatalie li - Sun Microsystems - Irvine United States 		syslog(LOG_DEBUG, "   No Windows groups");
8487f667e74Sjose borrego 	}
849b89a8333Snatalie li - Sun Microsystems - Irvine United States 
850b89a8333Snatalie li - Sun Microsystems - Irvine United States 	x_grps = token->tkn_posix_grps;
851b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (x_grps) {
8527f667e74Sjose borrego 		syslog(LOG_DEBUG, "   Solaris groups: %d", x_grps->pg_ngrps);
853b89a8333Snatalie li - Sun Microsystems - Irvine United States 		for (i = 0; i < x_grps->pg_ngrps; i++)
8547f667e74Sjose borrego 			syslog(LOG_DEBUG, "    %u", x_grps->pg_grps[i]);
8557f667e74Sjose borrego 	} else {
856b89a8333Snatalie li - Sun Microsystems - Irvine United States 		syslog(LOG_DEBUG, "   No Solaris groups");
8577f667e74Sjose borrego 	}
858b89a8333Snatalie li - Sun Microsystems - Irvine United States 
859b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (token->tkn_privileges)
860b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smb_privset_log(token->tkn_privileges);
861b89a8333Snatalie li - Sun Microsystems - Irvine United States 	else
862b89a8333Snatalie li - Sun Microsystems - Irvine United States 		syslog(LOG_DEBUG, "   No privileges");
863b89a8333Snatalie li - Sun Microsystems - Irvine United States }
8647f667e74Sjose borrego 
8657f667e74Sjose borrego /*
8667f667e74Sjose borrego  * Sets up local and well-known group membership for the given
8677f667e74Sjose borrego  * token. Two assumptions have been made here:
8687f667e74Sjose borrego  *
8697f667e74Sjose borrego  *   a) token already contains a valid user SID so that group
8707f667e74Sjose borrego  *      memberships can be established
8717f667e74Sjose borrego  *
8727f667e74Sjose borrego  *   b) token belongs to a local or anonymous user
8737f667e74Sjose borrego  */
8747f667e74Sjose borrego static uint32_t
8757f667e74Sjose borrego smb_token_setup_wingrps(smb_token_t *token)
8767f667e74Sjose borrego {
8777f667e74Sjose borrego 	smb_ids_t tkn_grps;
8787f667e74Sjose borrego 	uint32_t status;
8797f667e74Sjose borrego 
8807f667e74Sjose borrego 
8817f667e74Sjose borrego 	/*
8827f667e74Sjose borrego 	 * We always want the user's primary group in the list
8837f667e74Sjose borrego 	 * of groups.
8847f667e74Sjose borrego 	 */
8857f667e74Sjose borrego 	tkn_grps.i_cnt = 1;
8867f667e74Sjose borrego 	if ((tkn_grps.i_ids = malloc(sizeof (smb_id_t))) == NULL)
8877f667e74Sjose borrego 		return (NT_STATUS_NO_MEMORY);
8887f667e74Sjose borrego 
8897f667e74Sjose borrego 	tkn_grps.i_ids->i_sid = smb_sid_dup(token->tkn_primary_grp.i_sid);
8907f667e74Sjose borrego 	tkn_grps.i_ids->i_attrs = token->tkn_primary_grp.i_attrs;
8917f667e74Sjose borrego 	if (tkn_grps.i_ids->i_sid == NULL) {
8927f667e74Sjose borrego 		smb_ids_free(&tkn_grps);
8937f667e74Sjose borrego 		return (NT_STATUS_NO_MEMORY);
8947f667e74Sjose borrego 	}
8957f667e74Sjose borrego 
8967f667e74Sjose borrego 	status = smb_sam_usr_groups(token->tkn_user.i_sid, &tkn_grps);
8977f667e74Sjose borrego 	if (status != NT_STATUS_SUCCESS) {
8987f667e74Sjose borrego 		smb_ids_free(&tkn_grps);
8997f667e74Sjose borrego 		return (status);
9007f667e74Sjose borrego 	}
9017f667e74Sjose borrego 
90229bd2886SAlan Wright 	status = smb_wka_token_groups(token->tkn_flags, &tkn_grps);
9037f667e74Sjose borrego 	if (status != NT_STATUS_SUCCESS) {
9047f667e74Sjose borrego 		smb_ids_free(&tkn_grps);
9057f667e74Sjose borrego 		return (status);
9067f667e74Sjose borrego 	}
9077f667e74Sjose borrego 
9087f667e74Sjose borrego 	token->tkn_win_grps = tkn_grps;
9097f667e74Sjose borrego 	return (status);
9107f667e74Sjose borrego }
91129bd2886SAlan Wright 
91229bd2886SAlan Wright /*
91329bd2886SAlan Wright  * Returns the guest account name in the provided buffer.
91429bd2886SAlan Wright  *
91529bd2886SAlan Wright  * By default the name would be "guest" unless there's
91629bd2886SAlan Wright  * a idmap name-based rule which maps the guest to a local
91729bd2886SAlan Wright  * Solaris user in which case the name of that user is
91829bd2886SAlan Wright  * returned.
91929bd2886SAlan Wright  */
92029bd2886SAlan Wright static void
92129bd2886SAlan Wright smb_guest_account(char *guest, size_t buflen)
92229bd2886SAlan Wright {
92329bd2886SAlan Wright 	idmap_stat stat;
92429bd2886SAlan Wright 	uid_t guest_uid;
92529bd2886SAlan Wright 	struct passwd pw;
92629bd2886SAlan Wright 	char pwbuf[1024];
92729bd2886SAlan Wright 	int idtype;
92829bd2886SAlan Wright 
92929bd2886SAlan Wright 	/* default Guest account name */
93029bd2886SAlan Wright 	(void) rw_rdlock(&smb_logoninit_rwl);
93129bd2886SAlan Wright 	(void) strlcpy(guest, smb_guest.a_name, buflen);
93229bd2886SAlan Wright 
93329bd2886SAlan Wright 	idtype = SMB_IDMAP_USER;
93429bd2886SAlan Wright 	stat = smb_idmap_getid(smb_guest.a_sid, &guest_uid, &idtype);
93529bd2886SAlan Wright 	(void) rw_unlock(&smb_logoninit_rwl);
93629bd2886SAlan Wright 
93729bd2886SAlan Wright 	if (stat != IDMAP_SUCCESS)
93829bd2886SAlan Wright 		return;
93929bd2886SAlan Wright 
9409fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	/* If Ephemeral ID return the default name */
9419fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (IDMAP_ID_IS_EPHEMERAL(guest_uid))
94229bd2886SAlan Wright 		return;
94329bd2886SAlan Wright 
94429bd2886SAlan Wright 	if (getpwuid_r(guest_uid, &pw, pwbuf, sizeof (pwbuf)) == NULL)
94529bd2886SAlan Wright 		return;
94629bd2886SAlan Wright 
94729bd2886SAlan Wright 	(void) strlcpy(guest, pw.pw_name, buflen);
94829bd2886SAlan Wright }
949