xref: /titanic_41/usr/src/lib/smbsrv/libmlsvc/common/netr_logon.c (revision 80ac04ddcdb18d996dfcdf61bc8c55c2329bfd59)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25  */
26 
27 /*
28  * NETR SamLogon and SamLogoff RPC client functions.
29  */
30 
31 #include <stdio.h>
32 #include <strings.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <alloca.h>
36 #include <unistd.h>
37 #include <netdb.h>
38 #include <thread.h>
39 
40 #include <smbsrv/libsmb.h>
41 #include <smbsrv/libmlrpc.h>
42 #include <smbsrv/libmlsvc.h>
43 #include <smbsrv/ndl/netlogon.ndl>
44 #include <smbsrv/netrauth.h>
45 #include <smbsrv/smbinfo.h>
46 #include <smbsrv/smb_token.h>
47 #include <mlsvc.h>
48 
49 #define	NETLOGON_ATTEMPTS	2
50 
51 static uint32_t netlogon_logon(smb_logon_t *, smb_token_t *);
52 static uint32_t netr_server_samlogon(mlsvc_handle_t *, netr_info_t *, char *,
53     smb_logon_t *, smb_token_t *);
54 static void netr_invalidate_chain(void);
55 static void netr_interactive_samlogon(netr_info_t *, smb_logon_t *,
56     struct netr_logon_info1 *);
57 static void netr_network_samlogon(ndr_heap_t *, netr_info_t *,
58     smb_logon_t *, struct netr_logon_info2 *);
59 static void netr_setup_identity(ndr_heap_t *, smb_logon_t *,
60     netr_logon_id_t *);
61 static boolean_t netr_isadmin(struct netr_validation_info3 *);
62 static uint32_t netr_setup_domain_groups(struct netr_validation_info3 *,
63     smb_ids_t *);
64 static uint32_t netr_setup_token_wingrps(struct netr_validation_info3 *,
65     smb_token_t *);
66 
67 /*
68  * Shared with netr_auth.c
69  */
70 extern netr_info_t netr_global_info;
71 
72 static mutex_t netlogon_mutex;
73 static cond_t netlogon_cv;
74 static boolean_t netlogon_busy = B_FALSE;
75 static boolean_t netlogon_abort = B_FALSE;
76 
77 /*
78  * Abort impending domain logon requests.
79  */
80 void
81 smb_logon_abort(void)
82 {
83 	(void) mutex_lock(&netlogon_mutex);
84 	if (netlogon_busy && !netlogon_abort)
85 		syslog(LOG_DEBUG, "logon abort");
86 	netlogon_abort = B_TRUE;
87 	(void) cond_broadcast(&netlogon_cv);
88 	(void) mutex_unlock(&netlogon_mutex);
89 }
90 
91 /*
92  * This is the entry point for authenticating domain users.
93  *
94  * If we are not going to attempt to authenticate the user,
95  * this function must return without updating the status.
96  *
97  * If the user is successfully authenticated, we build an
98  * access token and the status will be NT_STATUS_SUCCESS.
99  * Otherwise, the token contents are invalid.
100  */
101 void
102 smb_logon_domain(smb_logon_t *user_info, smb_token_t *token)
103 {
104 	uint32_t	status;
105 	int		i;
106 
107 	if (user_info->lg_secmode != SMB_SECMODE_DOMAIN)
108 		return;
109 
110 	if (user_info->lg_domain_type == SMB_DOMAIN_LOCAL)
111 		return;
112 
113 	for (i = 0; i < NETLOGON_ATTEMPTS; ++i) {
114 		(void) mutex_lock(&netlogon_mutex);
115 		while (netlogon_busy && !netlogon_abort)
116 			(void) cond_wait(&netlogon_cv, &netlogon_mutex);
117 
118 		if (netlogon_abort) {
119 			(void) mutex_unlock(&netlogon_mutex);
120 			user_info->lg_status = NT_STATUS_REQUEST_ABORTED;
121 			return;
122 		}
123 
124 		netlogon_busy = B_TRUE;
125 		(void) mutex_unlock(&netlogon_mutex);
126 
127 		status = netlogon_logon(user_info, token);
128 
129 		(void) mutex_lock(&netlogon_mutex);
130 		netlogon_busy = B_FALSE;
131 		if (netlogon_abort)
132 			status = NT_STATUS_REQUEST_ABORTED;
133 		(void) cond_signal(&netlogon_cv);
134 		(void) mutex_unlock(&netlogon_mutex);
135 
136 		if (status != NT_STATUS_CANT_ACCESS_DOMAIN_INFO)
137 			break;
138 	}
139 
140 	if (status != NT_STATUS_SUCCESS)
141 		syslog(LOG_INFO, "logon[%s\\%s]: %s", user_info->lg_e_domain,
142 		    user_info->lg_e_username, xlate_nt_status(status));
143 
144 	user_info->lg_status = status;
145 }
146 
147 static uint32_t
148 netlogon_logon(smb_logon_t *user_info, smb_token_t *token)
149 {
150 	char resource_domain[SMB_PI_MAX_DOMAIN];
151 	char server[NETBIOS_NAME_SZ * 2];
152 	mlsvc_handle_t netr_handle;
153 	smb_domainex_t di;
154 	uint32_t status;
155 	int retries = 0;
156 
157 	(void) smb_getdomainname(resource_domain, SMB_PI_MAX_DOMAIN);
158 
159 	/* Avoid interfering with DC discovery. */
160 	if (smb_ddiscover_wait() != 0 ||
161 	    !smb_domain_getinfo(&di)) {
162 		netr_invalidate_chain();
163 		return (NT_STATUS_CANT_ACCESS_DOMAIN_INFO);
164 	}
165 
166 	do {
167 		if (netr_open(di.d_dc, di.d_primary.di_nbname, &netr_handle)
168 		    != 0)
169 			return (NT_STATUS_OPEN_FAILED);
170 
171 		if (di.d_dc && (*netr_global_info.server != '\0')) {
172 			(void) snprintf(server, sizeof (server),
173 			    "\\\\%s", di.d_dc);
174 			if (strncasecmp(netr_global_info.server,
175 			    server, strlen(server)) != 0)
176 				netr_invalidate_chain();
177 		}
178 
179 		if ((netr_global_info.flags & NETR_FLG_VALID) == 0 ||
180 		    !smb_match_netlogon_seqnum()) {
181 			status = netlogon_auth(di.d_dc, &netr_handle,
182 			    NETR_FLG_NULL);
183 
184 			if (status != 0) {
185 				(void) netr_close(&netr_handle);
186 				return (NT_STATUS_LOGON_FAILURE);
187 			}
188 
189 			netr_global_info.flags |= NETR_FLG_VALID;
190 		}
191 
192 		status = netr_server_samlogon(&netr_handle,
193 		    &netr_global_info, di.d_dc, user_info, token);
194 
195 		(void) netr_close(&netr_handle);
196 	} while (status == NT_STATUS_INSUFFICIENT_LOGON_INFO && retries++ < 3);
197 
198 	if (retries >= 3)
199 		status = NT_STATUS_LOGON_FAILURE;
200 
201 	return (status);
202 }
203 
204 static uint32_t
205 netr_setup_token(struct netr_validation_info3 *info3, smb_logon_t *user_info,
206     netr_info_t *netr_info, smb_token_t *token)
207 {
208 	char *username, *domain;
209 	unsigned char rc4key[SMBAUTH_SESSION_KEY_SZ];
210 	smb_sid_t *domsid;
211 	uint32_t status;
212 	char nbdomain[NETBIOS_NAME_SZ];
213 
214 	domsid = (smb_sid_t *)info3->LogonDomainId;
215 
216 	token->tkn_user.i_sid = smb_sid_splice(domsid, info3->UserId);
217 	if (token->tkn_user.i_sid == NULL)
218 		return (NT_STATUS_NO_MEMORY);
219 
220 	token->tkn_primary_grp.i_sid = smb_sid_splice(domsid,
221 	    info3->PrimaryGroupId);
222 	if (token->tkn_primary_grp.i_sid == NULL)
223 		return (NT_STATUS_NO_MEMORY);
224 
225 	username = (info3->EffectiveName.str)
226 	    ? (char *)info3->EffectiveName.str : user_info->lg_e_username;
227 
228 	if (info3->LogonDomainName.str) {
229 		domain = (char *)info3->LogonDomainName.str;
230 	} else if (*user_info->lg_e_domain != '\0') {
231 		domain = user_info->lg_e_domain;
232 	} else {
233 		(void) smb_getdomainname(nbdomain, sizeof (nbdomain));
234 		domain = nbdomain;
235 	}
236 
237 	if (username)
238 		token->tkn_account_name = strdup(username);
239 	if (domain)
240 		token->tkn_domain_name = strdup(domain);
241 
242 	if (token->tkn_account_name == NULL || token->tkn_domain_name == NULL)
243 		return (NT_STATUS_NO_MEMORY);
244 
245 	status = netr_setup_token_wingrps(info3, token);
246 	if (status != NT_STATUS_SUCCESS)
247 		return (status);
248 
249 	/*
250 	 * The UserSessionKey in NetrSamLogon RPC is obfuscated using the
251 	 * session key obtained in the NETLOGON credential chain.
252 	 * An 8 byte session key is zero extended to 16 bytes. This 16 byte
253 	 * key is the key to the RC4 algorithm. The RC4 byte stream is
254 	 * exclusively ored with the 16 byte UserSessionKey to recover
255 	 * the the clear form.
256 	 */
257 	if ((token->tkn_session_key = malloc(SMBAUTH_SESSION_KEY_SZ)) == NULL)
258 		return (NT_STATUS_NO_MEMORY);
259 	bzero(rc4key, SMBAUTH_SESSION_KEY_SZ);
260 	bcopy(netr_info->session_key.key, rc4key, netr_info->session_key.len);
261 	bcopy(info3->UserSessionKey.data, token->tkn_session_key,
262 	    SMBAUTH_SESSION_KEY_SZ);
263 	rand_hash((unsigned char *)token->tkn_session_key,
264 	    SMBAUTH_SESSION_KEY_SZ, rc4key, SMBAUTH_SESSION_KEY_SZ);
265 
266 	return (NT_STATUS_SUCCESS);
267 }
268 
269 /*
270  * netr_server_samlogon
271  *
272  * NetrServerSamLogon RPC: interactive or network. It is assumed that
273  * we have already authenticated with the PDC. If everything works,
274  * we build a user info structure and return it, where the caller will
275  * probably build an access token.
276  *
277  * Returns an NT status. There are numerous possibilities here.
278  * For example:
279  *	NT_STATUS_INVALID_INFO_CLASS
280  *	NT_STATUS_INVALID_PARAMETER
281  *	NT_STATUS_ACCESS_DENIED
282  *	NT_STATUS_PASSWORD_MUST_CHANGE
283  *	NT_STATUS_NO_SUCH_USER
284  *	NT_STATUS_WRONG_PASSWORD
285  *	NT_STATUS_LOGON_FAILURE
286  *	NT_STATUS_ACCOUNT_RESTRICTION
287  *	NT_STATUS_INVALID_LOGON_HOURS
288  *	NT_STATUS_INVALID_WORKSTATION
289  *	NT_STATUS_INTERNAL_ERROR
290  *	NT_STATUS_PASSWORD_EXPIRED
291  *	NT_STATUS_ACCOUNT_DISABLED
292  */
293 uint32_t
294 netr_server_samlogon(mlsvc_handle_t *netr_handle, netr_info_t *netr_info,
295     char *server, smb_logon_t *user_info, smb_token_t *token)
296 {
297 	struct netr_SamLogon arg;
298 	struct netr_authenticator auth;
299 	struct netr_authenticator ret_auth;
300 	struct netr_logon_info1 info1;
301 	struct netr_logon_info2 info2;
302 	struct netr_validation_info3 *info3;
303 	ndr_heap_t *heap;
304 	int opnum;
305 	int rc, len;
306 	uint32_t status;
307 
308 	bzero(&arg, sizeof (struct netr_SamLogon));
309 	opnum = NETR_OPNUM_SamLogon;
310 
311 	/*
312 	 * Should we get the server and hostname from netr_info?
313 	 */
314 
315 	len = strlen(server) + 4;
316 	arg.servername = ndr_rpc_malloc(netr_handle, len);
317 	arg.hostname = ndr_rpc_malloc(netr_handle, NETBIOS_NAME_SZ);
318 	if (arg.servername == NULL || arg.hostname == NULL) {
319 		ndr_rpc_release(netr_handle);
320 		return (NT_STATUS_INTERNAL_ERROR);
321 	}
322 
323 	(void) snprintf((char *)arg.servername, len, "\\\\%s", server);
324 	if (smb_getnetbiosname((char *)arg.hostname, NETBIOS_NAME_SZ) != 0) {
325 		ndr_rpc_release(netr_handle);
326 		return (NT_STATUS_INTERNAL_ERROR);
327 	}
328 
329 	rc = netr_setup_authenticator(netr_info, &auth, &ret_auth);
330 	if (rc != SMBAUTH_SUCCESS) {
331 		ndr_rpc_release(netr_handle);
332 		return (NT_STATUS_INTERNAL_ERROR);
333 	}
334 
335 	arg.auth = &auth;
336 	arg.ret_auth = &ret_auth;
337 	arg.validation_level = NETR_VALIDATION_LEVEL3;
338 	arg.logon_info.logon_level = user_info->lg_level;
339 	arg.logon_info.switch_value = user_info->lg_level;
340 
341 	heap = ndr_rpc_get_heap(netr_handle);
342 
343 	switch (user_info->lg_level) {
344 	case NETR_INTERACTIVE_LOGON:
345 		netr_setup_identity(heap, user_info, &info1.identity);
346 		netr_interactive_samlogon(netr_info, user_info, &info1);
347 		arg.logon_info.ru.info1 = &info1;
348 		break;
349 
350 	case NETR_NETWORK_LOGON:
351 		netr_setup_identity(heap, user_info, &info2.identity);
352 		netr_network_samlogon(heap, netr_info, user_info, &info2);
353 		arg.logon_info.ru.info2 = &info2;
354 		break;
355 
356 	default:
357 		ndr_rpc_release(netr_handle);
358 		return (NT_STATUS_INVALID_PARAMETER);
359 	}
360 
361 	rc = ndr_rpc_call(netr_handle, opnum, &arg);
362 	if (rc != 0) {
363 		bzero(netr_info, sizeof (netr_info_t));
364 		status = NT_STATUS_INVALID_PARAMETER;
365 	} else if (arg.status != 0) {
366 		status = NT_SC_VALUE(arg.status);
367 
368 		/*
369 		 * We need to validate the chain even though we have
370 		 * a non-zero status. If the status is ACCESS_DENIED
371 		 * this will trigger a new credential chain. However,
372 		 * a valid credential is returned with some status
373 		 * codes; for example, WRONG_PASSWORD.
374 		 */
375 		(void) netr_validate_chain(netr_info, arg.ret_auth);
376 	} else {
377 		status = netr_validate_chain(netr_info, arg.ret_auth);
378 		if (status == NT_STATUS_INSUFFICIENT_LOGON_INFO) {
379 			ndr_rpc_release(netr_handle);
380 			return (status);
381 		}
382 
383 		info3 = arg.ru.info3;
384 		status = netr_setup_token(info3, user_info, netr_info, token);
385 	}
386 
387 	ndr_rpc_release(netr_handle);
388 	return (status);
389 }
390 
391 /*
392  * netr_interactive_samlogon
393  *
394  * Set things up for an interactive SamLogon. Copy the NT and LM
395  * passwords to the logon structure and hash them with the session
396  * key.
397  */
398 static void
399 netr_interactive_samlogon(netr_info_t *netr_info, smb_logon_t *user_info,
400     struct netr_logon_info1 *info1)
401 {
402 	BYTE key[NETR_OWF_PASSWORD_SZ];
403 
404 	(void) memcpy(&info1->lm_owf_password,
405 	    user_info->lg_lm_password.val, sizeof (netr_owf_password_t));
406 
407 	(void) memcpy(&info1->nt_owf_password,
408 	    user_info->lg_nt_password.val, sizeof (netr_owf_password_t));
409 
410 	(void) memset(key, 0, NETR_OWF_PASSWORD_SZ);
411 	(void) memcpy(key, netr_info->session_key.key,
412 	    netr_info->session_key.len);
413 
414 	rand_hash((unsigned char *)&info1->lm_owf_password,
415 	    NETR_OWF_PASSWORD_SZ, key, NETR_OWF_PASSWORD_SZ);
416 
417 	rand_hash((unsigned char *)&info1->nt_owf_password,
418 	    NETR_OWF_PASSWORD_SZ, key, NETR_OWF_PASSWORD_SZ);
419 }
420 
421 /*
422  * netr_network_samlogon
423  *
424  * Set things up for a network SamLogon.  We provide a copy of the random
425  * challenge, that we sent to the client, to the domain controller.  This
426  * is the key that the client will have used to encrypt the NT and LM
427  * passwords.  Note that Windows 9x clients may not provide both passwords.
428  */
429 /*ARGSUSED*/
430 static void
431 netr_network_samlogon(ndr_heap_t *heap, netr_info_t *netr_info,
432     smb_logon_t *user_info, struct netr_logon_info2 *info2)
433 {
434 	uint32_t len;
435 
436 	bcopy(user_info->lg_challenge_key.val, info2->lm_challenge.data, 8);
437 
438 	if ((len = user_info->lg_nt_password.len) != 0) {
439 		ndr_heap_mkvcb(heap, user_info->lg_nt_password.val, len,
440 		    (ndr_vcbuf_t *)&info2->nt_response);
441 	} else {
442 		bzero(&info2->nt_response, sizeof (netr_vcbuf_t));
443 	}
444 
445 	if ((len = user_info->lg_lm_password.len) != 0) {
446 		ndr_heap_mkvcb(heap, user_info->lg_lm_password.val, len,
447 		    (ndr_vcbuf_t *)&info2->lm_response);
448 	} else {
449 		bzero(&info2->lm_response, sizeof (netr_vcbuf_t));
450 	}
451 }
452 
453 /*
454  * netr_setup_authenticator
455  *
456  * Set up the request and return authenticators. A new credential is
457  * generated from the session key, the current client credential and
458  * the current time, i.e.
459  *
460  *		NewCredential = Cred(SessionKey, OldCredential, time);
461  *
462  * The timestamp, which is used as a random seed, is stored in both
463  * the request and return authenticators.
464  *
465  * If any difficulties occur using the cryptographic framework, the
466  * function returns SMBAUTH_FAILURE.  Otherwise SMBAUTH_SUCCESS is
467  * returned.
468  */
469 int
470 netr_setup_authenticator(netr_info_t *netr_info,
471     struct netr_authenticator *auth, struct netr_authenticator *ret_auth)
472 {
473 	bzero(auth, sizeof (struct netr_authenticator));
474 
475 	netr_info->timestamp = time(0);
476 	auth->timestamp = netr_info->timestamp;
477 
478 	if (netr_gen_credentials(netr_info->session_key.key,
479 	    &netr_info->client_credential,
480 	    netr_info->timestamp,
481 	    (netr_cred_t *)&auth->credential) != SMBAUTH_SUCCESS)
482 		return (SMBAUTH_FAILURE);
483 
484 	if (ret_auth) {
485 		bzero(ret_auth, sizeof (struct netr_authenticator));
486 		ret_auth->timestamp = netr_info->timestamp;
487 	}
488 
489 	return (SMBAUTH_SUCCESS);
490 }
491 
492 /*
493  * Validate the returned credentials and update the credential chain.
494  * The server returns an updated client credential rather than a new
495  * server credential.  The server uses (timestamp + 1) when generating
496  * the credential.
497  *
498  * Generate the new seed for the credential chain. The new seed is
499  * formed by adding (timestamp + 1) to the current client credential.
500  * The only quirk is the uint32_t style addition.
501  *
502  * Returns NT_STATUS_INSUFFICIENT_LOGON_INFO if auth->credential is a
503  * NULL pointer. The Authenticator field of the SamLogon response packet
504  * sent by the Samba 3 PDC always return NULL pointer if the received
505  * SamLogon request is not immediately followed by the ServerReqChallenge
506  * and ServerAuthenticate2 requests.
507  *
508  * Returns NT_STATUS_SUCCESS if the server returned a valid credential.
509  * Otherwise we retirm NT_STATUS_UNSUCCESSFUL.
510  */
511 uint32_t
512 netr_validate_chain(netr_info_t *netr_info, struct netr_authenticator *auth)
513 {
514 	netr_cred_t cred;
515 	uint32_t result = NT_STATUS_SUCCESS;
516 	uint32_t *dwp;
517 
518 	++netr_info->timestamp;
519 
520 	if (netr_gen_credentials(netr_info->session_key.key,
521 	    &netr_info->client_credential,
522 	    netr_info->timestamp, &cred) != SMBAUTH_SUCCESS)
523 		return (NT_STATUS_INTERNAL_ERROR);
524 
525 	if (&auth->credential == 0) {
526 		/*
527 		 * If the validation fails, destroy the credential chain.
528 		 * This should trigger a new authentication chain.
529 		 */
530 		bzero(netr_info, sizeof (netr_info_t));
531 		return (NT_STATUS_INSUFFICIENT_LOGON_INFO);
532 	}
533 
534 	result = memcmp(&cred, &auth->credential, sizeof (netr_cred_t));
535 	if (result != 0) {
536 		/*
537 		 * If the validation fails, destroy the credential chain.
538 		 * This should trigger a new authentication chain.
539 		 */
540 		bzero(netr_info, sizeof (netr_info_t));
541 		result = NT_STATUS_UNSUCCESSFUL;
542 	} else {
543 		/*
544 		 * Otherwise generate the next step in the chain.
545 		 */
546 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
547 		dwp = (uint32_t *)&netr_info->client_credential;
548 		dwp[0] += netr_info->timestamp;
549 
550 		netr_info->flags |= NETR_FLG_VALID;
551 	}
552 
553 	return (result);
554 }
555 
556 /*
557  * netr_invalidate_chain
558  *
559  * Mark the credential chain as invalid so that it will be recreated
560  * on the next attempt.
561  */
562 static void
563 netr_invalidate_chain(void)
564 {
565 	netr_global_info.flags &= ~NETR_FLG_VALID;
566 }
567 
568 /*
569  * netr_setup_identity
570  *
571  * Set up the client identity information. All of this information is
572  * specifically related to the client user and workstation attempting
573  * to access this system. It may not be in our primary domain.
574  *
575  * I don't know what logon_id is, it seems to be a unique identifier.
576  * Increment it before each use.
577  */
578 static void
579 netr_setup_identity(ndr_heap_t *heap, smb_logon_t *user_info,
580     netr_logon_id_t *identity)
581 {
582 	static mutex_t logon_id_mutex;
583 	static uint32_t logon_id;
584 
585 	(void) mutex_lock(&logon_id_mutex);
586 
587 	if (logon_id == 0)
588 		logon_id = 0xDCD0;
589 
590 	++logon_id;
591 	user_info->lg_logon_id = logon_id;
592 
593 	(void) mutex_unlock(&logon_id_mutex);
594 
595 	identity->parameter_control = 0;
596 	identity->logon_id.LowPart = logon_id;
597 	identity->logon_id.HighPart = 0;
598 
599 	ndr_heap_mkvcs(heap, user_info->lg_domain,
600 	    (ndr_vcstr_t *)&identity->domain_name);
601 
602 	ndr_heap_mkvcs(heap, user_info->lg_username,
603 	    (ndr_vcstr_t *)&identity->username);
604 
605 	/*
606 	 * Some systems prefix the client workstation name with \\.
607 	 * It doesn't seem to make any difference whether it's there
608 	 * or not.
609 	 */
610 	ndr_heap_mkvcs(heap, user_info->lg_workstation,
611 	    (ndr_vcstr_t *)&identity->workstation);
612 }
613 
614 /*
615  * Sets up domain, local and well-known group membership for the given
616  * token. Two assumptions have been made here:
617  *
618  *   a) token already contains a valid user SID so that group
619  *      memberships can be established
620  *
621  *   b) token belongs to a domain user
622  */
623 static uint32_t
624 netr_setup_token_wingrps(struct netr_validation_info3 *info3,
625     smb_token_t *token)
626 {
627 	smb_ids_t tkn_grps;
628 	uint32_t status;
629 
630 	tkn_grps.i_cnt = 0;
631 	tkn_grps.i_ids = NULL;
632 
633 	status = netr_setup_domain_groups(info3, &tkn_grps);
634 	if (status != NT_STATUS_SUCCESS) {
635 		smb_ids_free(&tkn_grps);
636 		return (status);
637 	}
638 
639 	status = smb_sam_usr_groups(token->tkn_user.i_sid, &tkn_grps);
640 	if (status != NT_STATUS_SUCCESS) {
641 		smb_ids_free(&tkn_grps);
642 		return (status);
643 	}
644 
645 	if (netr_isadmin(info3))
646 		token->tkn_flags |= SMB_ATF_ADMIN;
647 
648 	status = smb_wka_token_groups(token->tkn_flags, &tkn_grps);
649 	if (status == NT_STATUS_SUCCESS)
650 		token->tkn_win_grps = tkn_grps;
651 	else
652 		smb_ids_free(&tkn_grps);
653 
654 	return (status);
655 }
656 
657 /*
658  * Converts groups information in the returned structure by domain controller
659  * (info3) to an internal representation (gids)
660  */
661 static uint32_t
662 netr_setup_domain_groups(struct netr_validation_info3 *info3, smb_ids_t *gids)
663 {
664 	smb_sid_t *domain_sid;
665 	smb_id_t *ids;
666 	int i, total_cnt;
667 
668 	if ((i = info3->GroupCount) == 0)
669 		i++;
670 	i += info3->SidCount;
671 
672 	total_cnt = gids->i_cnt + i;
673 
674 	gids->i_ids = realloc(gids->i_ids, total_cnt * sizeof (smb_id_t));
675 	if (gids->i_ids == NULL)
676 		return (NT_STATUS_NO_MEMORY);
677 
678 	domain_sid = (smb_sid_t *)info3->LogonDomainId;
679 
680 	ids = gids->i_ids + gids->i_cnt;
681 	for (i = 0; i < info3->GroupCount; i++, gids->i_cnt++, ids++) {
682 		ids->i_sid = smb_sid_splice(domain_sid, info3->GroupIds[i].rid);
683 		if (ids->i_sid == NULL)
684 			return (NT_STATUS_NO_MEMORY);
685 
686 		ids->i_attrs = info3->GroupIds[i].attributes;
687 	}
688 
689 	if (info3->GroupCount == 0) {
690 		/*
691 		 * if there's no global group should add the primary group.
692 		 */
693 		ids->i_sid = smb_sid_splice(domain_sid, info3->PrimaryGroupId);
694 		if (ids->i_sid == NULL)
695 			return (NT_STATUS_NO_MEMORY);
696 
697 		ids->i_attrs = 0x7;
698 		gids->i_cnt++;
699 		ids++;
700 	}
701 
702 	/* Add the extra SIDs */
703 	for (i = 0; i < info3->SidCount; i++, gids->i_cnt++, ids++) {
704 		ids->i_sid = smb_sid_dup((smb_sid_t *)info3->ExtraSids[i].sid);
705 		if (ids->i_sid == NULL)
706 			return (NT_STATUS_NO_MEMORY);
707 
708 		ids->i_attrs = info3->ExtraSids[i].attributes;
709 	}
710 
711 	return (NT_STATUS_SUCCESS);
712 }
713 
714 /*
715  * Determines if the given user is the domain Administrator or a
716  * member of Domain Admins
717  */
718 static boolean_t
719 netr_isadmin(struct netr_validation_info3 *info3)
720 {
721 	smb_domain_t di;
722 	int i;
723 
724 	if (!smb_domain_lookup_sid((smb_sid_t *)info3->LogonDomainId, &di))
725 		return (B_FALSE);
726 
727 	if (di.di_type != SMB_DOMAIN_PRIMARY)
728 		return (B_FALSE);
729 
730 	if ((info3->UserId == DOMAIN_USER_RID_ADMIN) ||
731 	    (info3->PrimaryGroupId == DOMAIN_GROUP_RID_ADMINS))
732 		return (B_TRUE);
733 
734 	for (i = 0; i < info3->GroupCount; i++)
735 		if (info3->GroupIds[i].rid == DOMAIN_GROUP_RID_ADMINS)
736 			return (B_TRUE);
737 
738 	return (B_FALSE);
739 }
740