xref: /onnv-gate/usr/src/lib/smbsrv/libmlsvc/common/netr_auth.c (revision 8334:5f1c6a3b0fad)
15331Samw /*
25331Samw  * CDDL HEADER START
35331Samw  *
45331Samw  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
75331Samw  *
85331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw  * or http://www.opensolaris.org/os/licensing.
105331Samw  * See the License for the specific language governing permissions
115331Samw  * and limitations under the License.
125331Samw  *
135331Samw  * When distributing Covered Code, include this CDDL HEADER in each
145331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw  * If applicable, add the following below this CDDL HEADER, with the
165331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw  *
195331Samw  * CDDL HEADER END
205331Samw  */
215331Samw /*
225772Sas200622  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235331Samw  * Use is subject to license terms.
245331Samw  */
255331Samw 
265331Samw /*
275331Samw  * NETR challenge/response client functions.
285331Samw  *
295331Samw  * NT_STATUS_INVALID_PARAMETER
305331Samw  * NT_STATUS_NO_TRUST_SAM_ACCOUNT
315331Samw  * NT_STATUS_ACCESS_DENIED
325331Samw  */
335331Samw 
345331Samw #include <stdio.h>
355331Samw #include <stdlib.h>
365331Samw #include <strings.h>
375331Samw #include <unistd.h>
385331Samw #include <ctype.h>
397052Samw #include <security/cryptoki.h>
407052Samw #include <security/pkcs11.h>
415331Samw 
425331Samw #include <smbsrv/libsmb.h>
435521Sas200622 #include <smbsrv/libsmbrdr.h>
446432Sas200622 #include <smbsrv/libsmbns.h>
45*8334SJose.Borrego@Sun.COM #include <smbsrv/libmlsvc.h>
465331Samw #include <smbsrv/ndl/netlogon.ndl>
475331Samw #include <smbsrv/ntstatus.h>
485331Samw #include <smbsrv/smbinfo.h>
495331Samw #include <smbsrv/netrauth.h>
505331Samw 
517052Samw #define	NETR_SESSKEY_ZEROBUF_SZ		4
527619SJose.Borrego@Sun.COM /* The DES algorithm uses a 56-bit encryption key. */
537052Samw #define	NETR_DESKEY_LEN			7
547052Samw 
555331Samw int netr_setup_authenticator(netr_info_t *, struct netr_authenticator *,
565331Samw     struct netr_authenticator *);
575331Samw DWORD netr_validate_chain(netr_info_t *, struct netr_authenticator *);
585331Samw 
595331Samw static int netr_server_req_challenge(mlsvc_handle_t *, netr_info_t *);
605331Samw static int netr_server_authenticate2(mlsvc_handle_t *, netr_info_t *);
615331Samw static int netr_gen_password(BYTE *, BYTE *, BYTE *);
625331Samw 
635331Samw /*
645331Samw  * Shared with netr_logon.c
655331Samw  */
665331Samw netr_info_t netr_global_info;
675331Samw 
685331Samw /*
695331Samw  * netlogon_auth
705331Samw  *
715331Samw  * This is the core of the NETLOGON authentication protocol.
725331Samw  * Do the challenge response authentication.
735331Samw  *
745331Samw  * Prior to calling this function, an anonymous session to the NETLOGON
755331Samw  * pipe on a domain controller(server) should have already been opened.
766139Sjb150015  *
776139Sjb150015  * Upon a successful NETLOGON credential chain establishment, the
786139Sjb150015  * netlogon sequence number will be set to match the kpasswd sequence
796139Sjb150015  * number.
806139Sjb150015  *
815331Samw  */
825331Samw DWORD
835331Samw netlogon_auth(char *server, mlsvc_handle_t *netr_handle, DWORD flags)
845331Samw {
855331Samw 	netr_info_t *netr_info;
865331Samw 	int rc;
875521Sas200622 	DWORD leout_rc[2];
885331Samw 
895331Samw 	netr_info = &netr_global_info;
905331Samw 	bzero(netr_info, sizeof (netr_info_t));
915331Samw 
925331Samw 	netr_info->flags |= flags;
935331Samw 
947961SNatalie.Li@Sun.COM 	rc = smb_getnetbiosname(netr_info->hostname, NETBIOS_NAME_SZ);
955331Samw 	if (rc != 0)
965331Samw 		return (NT_STATUS_UNSUCCESSFUL);
975331Samw 
985331Samw 	(void) snprintf(netr_info->server, sizeof (netr_info->server),
995331Samw 	    "\\\\%s", server);
1005331Samw 
1015521Sas200622 	LE_OUT32(&leout_rc[0], random());
1025521Sas200622 	LE_OUT32(&leout_rc[1], random());
1035521Sas200622 	(void) memcpy(&netr_info->client_challenge, leout_rc,
1045331Samw 	    sizeof (struct netr_credential));
1055331Samw 
1065331Samw 	if ((rc = netr_server_req_challenge(netr_handle, netr_info)) == 0) {
1075331Samw 		rc = netr_server_authenticate2(netr_handle, netr_info);
1086139Sjb150015 		if (rc == 0) {
1096139Sjb150015 			smb_update_netlogon_seqnum();
1105331Samw 			netr_info->flags |= NETR_FLG_VALID;
1116139Sjb150015 
1126139Sjb150015 		}
1135331Samw 	}
1145331Samw 
1155331Samw 	return ((rc) ? NT_STATUS_UNSUCCESSFUL : NT_STATUS_SUCCESS);
1165331Samw }
1175331Samw 
1185331Samw /*
1195331Samw  * netr_open
1205331Samw  *
1215331Samw  * Open an anonymous session to the NETLOGON pipe on a domain
1225331Samw  * controller and bind to the NETR RPC interface. We store the
1235331Samw  * remote server's native OS type - we may need it due to
1245331Samw  * differences between versions of Windows.
1255331Samw  */
1265331Samw int
1275331Samw netr_open(char *server, char *domain, mlsvc_handle_t *netr_handle)
1285331Samw {
1295521Sas200622 	char *user = smbrdr_ipc_get_user();
1305331Samw 
131*8334SJose.Borrego@Sun.COM 	if (ndr_rpc_bind(netr_handle, server, domain, user, "NETR") < 0)
1325331Samw 		return (-1);
1335331Samw 
1345331Samw 	return (0);
1355331Samw }
1365331Samw 
1375331Samw /*
1385331Samw  * netr_close
1395331Samw  *
1405331Samw  * Close a NETLOGON pipe and free the RPC context.
1415331Samw  */
1425331Samw int
1435331Samw netr_close(mlsvc_handle_t *netr_handle)
1445331Samw {
145*8334SJose.Borrego@Sun.COM 	ndr_rpc_unbind(netr_handle);
1465331Samw 	return (0);
1475331Samw }
1485331Samw 
1495331Samw /*
1505331Samw  * netr_server_req_challenge
1515331Samw  */
1525331Samw static int
1535331Samw netr_server_req_challenge(mlsvc_handle_t *netr_handle, netr_info_t *netr_info)
1545331Samw {
1555331Samw 	struct netr_ServerReqChallenge arg;
1565331Samw 	int opnum;
1575331Samw 
1585331Samw 	bzero(&arg, sizeof (struct netr_ServerReqChallenge));
1595331Samw 	opnum = NETR_OPNUM_ServerReqChallenge;
1605331Samw 
1615331Samw 	arg.servername = (unsigned char *)netr_info->server;
1625331Samw 	arg.hostname = (unsigned char *)netr_info->hostname;
1635331Samw 
1645331Samw 	(void) memcpy(&arg.client_challenge, &netr_info->client_challenge,
1655331Samw 	    sizeof (struct netr_credential));
1665331Samw 
167*8334SJose.Borrego@Sun.COM 	if (ndr_rpc_call(netr_handle, opnum, &arg) != 0)
168*8334SJose.Borrego@Sun.COM 		return (-1);
169*8334SJose.Borrego@Sun.COM 
170*8334SJose.Borrego@Sun.COM 	if (arg.status != 0) {
171*8334SJose.Borrego@Sun.COM 		ndr_rpc_status(netr_handle, opnum, arg.status);
172*8334SJose.Borrego@Sun.COM 		ndr_rpc_release(netr_handle);
173*8334SJose.Borrego@Sun.COM 		return (-1);
1745331Samw 	}
1755331Samw 
176*8334SJose.Borrego@Sun.COM 	(void) memcpy(&netr_info->server_challenge, &arg.server_challenge,
177*8334SJose.Borrego@Sun.COM 	    sizeof (struct netr_credential));
178*8334SJose.Borrego@Sun.COM 
179*8334SJose.Borrego@Sun.COM 	ndr_rpc_release(netr_handle);
180*8334SJose.Borrego@Sun.COM 	return (0);
1815331Samw }
1825331Samw 
1835331Samw /*
1845331Samw  * netr_server_authenticate2
1855331Samw  */
1865331Samw static int
1875331Samw netr_server_authenticate2(mlsvc_handle_t *netr_handle, netr_info_t *netr_info)
1885331Samw {
1895331Samw 	struct netr_ServerAuthenticate2 arg;
1905331Samw 	int opnum;
1915331Samw 	int rc;
1927961SNatalie.Li@Sun.COM 	char account_name[NETBIOS_NAME_SZ * 2];
1935331Samw 
1945331Samw 	bzero(&arg, sizeof (struct netr_ServerAuthenticate2));
1955331Samw 	opnum = NETR_OPNUM_ServerAuthenticate2;
1965331Samw 
1975331Samw 	(void) snprintf(account_name, sizeof (account_name), "%s$",
1985331Samw 	    netr_info->hostname);
1995331Samw 
2007619SJose.Borrego@Sun.COM 	smb_tracef("server=[%s] account_name=[%s] hostname=[%s]\n",
2017619SJose.Borrego@Sun.COM 	    netr_info->server, account_name, netr_info->hostname);
2027619SJose.Borrego@Sun.COM 
2035331Samw 	arg.servername = (unsigned char *)netr_info->server;
2045331Samw 	arg.account_name = (unsigned char *)account_name;
2055331Samw 	arg.account_type = NETR_WKSTA_TRUST_ACCOUNT_TYPE;
2065331Samw 	arg.hostname = (unsigned char *)netr_info->hostname;
2077619SJose.Borrego@Sun.COM 	arg.negotiate_flags = NETR_NEGOTIATE_BASE_FLAGS;
2085331Samw 
209*8334SJose.Borrego@Sun.COM 	if (ndr_rpc_server_os(netr_handle) != NATIVE_OS_WINNT) {
2107619SJose.Borrego@Sun.COM 		arg.negotiate_flags |= NETR_NEGOTIATE_STRONGKEY_FLAG;
2117619SJose.Borrego@Sun.COM 		if (netr_gen_skey128(netr_info) != SMBAUTH_SUCCESS)
2127619SJose.Borrego@Sun.COM 			return (-1);
2137619SJose.Borrego@Sun.COM 	} else {
2147619SJose.Borrego@Sun.COM 		if (netr_gen_skey64(netr_info) != SMBAUTH_SUCCESS)
2157619SJose.Borrego@Sun.COM 			return (-1);
2167619SJose.Borrego@Sun.COM 	}
2175331Samw 
2187619SJose.Borrego@Sun.COM 	if (netr_gen_credentials(netr_info->session_key.key,
2197619SJose.Borrego@Sun.COM 	    &netr_info->client_challenge, 0,
2205331Samw 	    &netr_info->client_credential) != SMBAUTH_SUCCESS) {
2215331Samw 		return (-1);
2225331Samw 	}
2235331Samw 
2247619SJose.Borrego@Sun.COM 	if (netr_gen_credentials(netr_info->session_key.key,
2257619SJose.Borrego@Sun.COM 	    &netr_info->server_challenge, 0,
2265331Samw 	    &netr_info->server_credential) != SMBAUTH_SUCCESS) {
2275331Samw 		return (-1);
2285331Samw 	}
2295331Samw 
2305331Samw 	(void) memcpy(&arg.client_credential, &netr_info->client_credential,
2315331Samw 	    sizeof (struct netr_credential));
2325331Samw 
233*8334SJose.Borrego@Sun.COM 	if (ndr_rpc_call(netr_handle, opnum, &arg) != 0)
234*8334SJose.Borrego@Sun.COM 		return (-1);
2355331Samw 
236*8334SJose.Borrego@Sun.COM 	if (arg.status != 0) {
237*8334SJose.Borrego@Sun.COM 		ndr_rpc_status(netr_handle, opnum, arg.status);
238*8334SJose.Borrego@Sun.COM 		ndr_rpc_release(netr_handle);
239*8334SJose.Borrego@Sun.COM 		return (-1);
2405331Samw 	}
2415331Samw 
242*8334SJose.Borrego@Sun.COM 	rc = memcmp(&netr_info->server_credential, &arg.server_credential,
243*8334SJose.Borrego@Sun.COM 	    sizeof (struct netr_credential));
244*8334SJose.Borrego@Sun.COM 
245*8334SJose.Borrego@Sun.COM 	ndr_rpc_release(netr_handle);
2465331Samw 	return (rc);
2475331Samw }
2485331Samw 
2495331Samw /*
2507619SJose.Borrego@Sun.COM  * netr_gen_skey128
2515331Samw  *
2527619SJose.Borrego@Sun.COM  * Generate a 128-bit session key from the client and server challenges.
2537619SJose.Borrego@Sun.COM  * See "Session-Key Computation" section of MS-NRPC document.
2545331Samw  */
2557052Samw int
2567619SJose.Borrego@Sun.COM netr_gen_skey128(netr_info_t *netr_info)
2577052Samw {
2587052Samw 	unsigned char ntlmhash[SMBAUTH_HASH_SZ];
2597052Samw 	int rc = SMBAUTH_FAILURE;
2607052Samw 	CK_RV rv;
2617052Samw 	CK_MECHANISM mechanism;
2627052Samw 	CK_SESSION_HANDLE hSession;
2637052Samw 	CK_ULONG diglen = MD_DIGEST_LEN;
2647052Samw 	unsigned char md5digest[MD_DIGEST_LEN];
2657052Samw 	unsigned char zerobuf[NETR_SESSKEY_ZEROBUF_SZ];
2667052Samw 
2677052Samw 	bzero(ntlmhash, SMBAUTH_HASH_SZ);
2687052Samw 	/*
2697052Samw 	 * We should check (netr_info->flags & NETR_FLG_INIT) and use
2707052Samw 	 * the appropriate password but it isn't working yet.  So we
2717052Samw 	 * always use the default one for now.
2727052Samw 	 */
2737052Samw 	bzero(netr_info->password, sizeof (netr_info->password));
2747052Samw 	rc = smb_config_getstr(SMB_CI_MACHINE_PASSWD,
2757052Samw 	    (char *)netr_info->password, sizeof (netr_info->password));
2767052Samw 
2777052Samw 	if ((rc != SMBD_SMF_OK) || *netr_info->password == '\0') {
2787052Samw 		return (SMBAUTH_FAILURE);
2797052Samw 	}
2807052Samw 
2817052Samw 	rc = smb_auth_ntlm_hash((char *)netr_info->password, ntlmhash);
2827052Samw 	if (rc != SMBAUTH_SUCCESS)
2837052Samw 		return (SMBAUTH_FAILURE);
2847052Samw 
2857052Samw 	bzero(zerobuf, NETR_SESSKEY_ZEROBUF_SZ);
2867052Samw 
2877052Samw 	mechanism.mechanism = CKM_MD5;
2887052Samw 	mechanism.pParameter = 0;
2897052Samw 	mechanism.ulParameterLen = 0;
2907052Samw 
2917052Samw 	rv = SUNW_C_GetMechSession(mechanism.mechanism, &hSession);
2927052Samw 	if (rv != CKR_OK)
2937052Samw 		return (SMBAUTH_FAILURE);
2947052Samw 
2957052Samw 	rv = C_DigestInit(hSession, &mechanism);
2967052Samw 	if (rv != CKR_OK)
2977052Samw 		goto cleanup;
2987052Samw 
2997052Samw 	rv = C_DigestUpdate(hSession, (CK_BYTE_PTR)zerobuf,
3007052Samw 	    NETR_SESSKEY_ZEROBUF_SZ);
3017052Samw 	if (rv != CKR_OK)
3027052Samw 		goto cleanup;
3037052Samw 
3047052Samw 	rv = C_DigestUpdate(hSession,
3057052Samw 	    (CK_BYTE_PTR)netr_info->client_challenge.data, NETR_CRED_DATA_SZ);
3067052Samw 	if (rv != CKR_OK)
3077052Samw 		goto cleanup;
3087052Samw 
3097052Samw 	rv = C_DigestUpdate(hSession,
3107052Samw 	    (CK_BYTE_PTR)netr_info->server_challenge.data, NETR_CRED_DATA_SZ);
3117052Samw 	if (rv != CKR_OK)
3127052Samw 		goto cleanup;
3137052Samw 
3147052Samw 	rv = C_DigestFinal(hSession, (CK_BYTE_PTR)md5digest, &diglen);
3157052Samw 	if (rv != CKR_OK)
3167052Samw 		goto cleanup;
3177052Samw 
3187052Samw 	rc = smb_auth_hmac_md5(md5digest, diglen, ntlmhash, SMBAUTH_HASH_SZ,
3197619SJose.Borrego@Sun.COM 	    netr_info->session_key.key);
3207052Samw 
3217619SJose.Borrego@Sun.COM 	netr_info->session_key.len = NETR_SESSKEY128_SZ;
3227052Samw cleanup:
3237052Samw 	(void) C_CloseSession(hSession);
3247052Samw 	return (rc);
3257052Samw 
3267052Samw }
3277619SJose.Borrego@Sun.COM /*
3287619SJose.Borrego@Sun.COM  * netr_gen_skey64
3297619SJose.Borrego@Sun.COM  *
3307619SJose.Borrego@Sun.COM  * Generate a 64-bit session key from the client and server challenges.
3317619SJose.Borrego@Sun.COM  * See "Session-Key Computation" section of MS-NRPC document.
3327619SJose.Borrego@Sun.COM  *
3337619SJose.Borrego@Sun.COM  * The algorithm is a two stage hash. For the first hash, the input is
3347619SJose.Borrego@Sun.COM  * the combination of the client and server challenges, the key is
3357619SJose.Borrego@Sun.COM  * the first 7 bytes of the password. The initial password is formed
3367619SJose.Borrego@Sun.COM  * using the NT password hash on the local hostname in lower case.
3377619SJose.Borrego@Sun.COM  * The result is stored in a temporary buffer.
3387619SJose.Borrego@Sun.COM  *
3397619SJose.Borrego@Sun.COM  *		input:	challenge
3407619SJose.Borrego@Sun.COM  *		key:	passwd lower 7 bytes
3417619SJose.Borrego@Sun.COM  *		output:	intermediate result
3427619SJose.Borrego@Sun.COM  *
3437619SJose.Borrego@Sun.COM  * For the second hash, the input is the result of the first hash and
3447619SJose.Borrego@Sun.COM  * the key is the last 7 bytes of the password.
3457619SJose.Borrego@Sun.COM  *
3467619SJose.Borrego@Sun.COM  *		input:	result of first hash
3477619SJose.Borrego@Sun.COM  *		key:	passwd upper 7 bytes
3487619SJose.Borrego@Sun.COM  *		output:	session_key
3497619SJose.Borrego@Sun.COM  *
3507619SJose.Borrego@Sun.COM  * The final output should be the session key.
3517619SJose.Borrego@Sun.COM  *
3527619SJose.Borrego@Sun.COM  *		FYI: smb_auth_DES(output, key, input)
3537619SJose.Borrego@Sun.COM  *
3547619SJose.Borrego@Sun.COM  * If any difficulties occur using the cryptographic framework, the
3557619SJose.Borrego@Sun.COM  * function returns SMBAUTH_FAILURE.  Otherwise SMBAUTH_SUCCESS is
3567619SJose.Borrego@Sun.COM  * returned.
3577619SJose.Borrego@Sun.COM  */
3585331Samw int
3597619SJose.Borrego@Sun.COM netr_gen_skey64(netr_info_t *netr_info)
3605331Samw {
3615331Samw 	unsigned char md4hash[32];
3625331Samw 	unsigned char buffer[8];
3635331Samw 	DWORD data[2];
3645331Samw 	DWORD *client_challenge;
3655331Samw 	DWORD *server_challenge;
3665331Samw 	int rc;
3675521Sas200622 	DWORD le_data[2];
3685331Samw 
3695331Samw 	client_challenge = (DWORD *)(uintptr_t)&netr_info->client_challenge;
3705331Samw 	server_challenge = (DWORD *)(uintptr_t)&netr_info->server_challenge;
3715331Samw 	bzero(md4hash, 32);
3725331Samw 
3735331Samw 	/*
3745331Samw 	 * We should check (netr_info->flags & NETR_FLG_INIT) and use
3755331Samw 	 * the appropriate password but it isn't working yet.  So we
3765331Samw 	 * always use the default one for now.
3775331Samw 	 */
3785772Sas200622 	bzero(netr_info->password, sizeof (netr_info->password));
3795772Sas200622 	rc = smb_config_getstr(SMB_CI_MACHINE_PASSWD,
3805772Sas200622 	    (char *)netr_info->password, sizeof (netr_info->password));
3815331Samw 
3825772Sas200622 	if ((rc != SMBD_SMF_OK) || *netr_info->password == '\0') {
3837052Samw 		return (SMBAUTH_FAILURE);
3845331Samw 	}
3855331Samw 
3865772Sas200622 	rc = smb_auth_ntlm_hash((char *)netr_info->password, md4hash);
3875331Samw 
3885331Samw 	if (rc != SMBAUTH_SUCCESS)
3895331Samw 		return (SMBAUTH_FAILURE);
3905331Samw 
3915331Samw 	data[0] = LE_IN32(&client_challenge[0]) + LE_IN32(&server_challenge[0]);
3925331Samw 	data[1] = LE_IN32(&client_challenge[1]) + LE_IN32(&server_challenge[1]);
3935521Sas200622 	LE_OUT32(&le_data[0], data[0]);
3945521Sas200622 	LE_OUT32(&le_data[1], data[1]);
3957619SJose.Borrego@Sun.COM 	rc = smb_auth_DES(buffer, 8, md4hash, NETR_DESKEY_LEN,
3967619SJose.Borrego@Sun.COM 	    (unsigned char *)le_data, 8);
3975331Samw 
3985331Samw 	if (rc != SMBAUTH_SUCCESS)
3995331Samw 		return (rc);
4005331Samw 
4017619SJose.Borrego@Sun.COM 	netr_info->session_key.len = NETR_SESSKEY64_SZ;
4027619SJose.Borrego@Sun.COM 	rc = smb_auth_DES(netr_info->session_key.key,
4037619SJose.Borrego@Sun.COM 	    netr_info->session_key.len, &md4hash[9], NETR_DESKEY_LEN, buffer,
4047619SJose.Borrego@Sun.COM 	    8);
4057619SJose.Borrego@Sun.COM 
4065331Samw 	return (rc);
4075331Samw }
4085331Samw 
4095331Samw /*
4105331Samw  * netr_gen_credentials
4115331Samw  *
4125331Samw  * Generate a set of credentials from a challenge and a session key.
4135331Samw  * The algorithm is a two stage hash. For the first hash, the
4145331Samw  * timestamp is added to the challenge and the result is stored in a
4155331Samw  * temporary buffer:
4165331Samw  *
4175331Samw  *		input:	challenge (including timestamp)
4185331Samw  *		key:	session_key
4195331Samw  *		output:	intermediate result
4205331Samw  *
4215331Samw  * For the second hash, the input is the result of the first hash and
4225331Samw  * a strange partial key is used:
4235331Samw  *
4245331Samw  *		input:	result of first hash
4255331Samw  *		key:	funny partial key
4265331Samw  *		output:	credentiails
4275331Samw  *
4285331Samw  * The final output should be an encrypted set of credentials.
4295331Samw  *
4305331Samw  *		FYI: smb_auth_DES(output, key, input)
4315331Samw  *
4325331Samw  * If any difficulties occur using the cryptographic framework, the
4335331Samw  * function returns SMBAUTH_FAILURE.  Otherwise SMBAUTH_SUCCESS is
4345331Samw  * returned.
4355331Samw  */
4365331Samw int
4375331Samw netr_gen_credentials(BYTE *session_key, netr_cred_t *challenge,
4385331Samw     DWORD timestamp, netr_cred_t *out_cred)
4395331Samw {
4405331Samw 	unsigned char buffer[8];
4415331Samw 	DWORD data[2];
4425521Sas200622 	DWORD le_data[2];
4435331Samw 	DWORD *p;
4445331Samw 	int rc;
4455331Samw 
4465331Samw 	p = (DWORD *)(uintptr_t)challenge;
4475521Sas200622 	data[0] = LE_IN32(&p[0]) + timestamp;
4485521Sas200622 	data[1] = LE_IN32(&p[1]);
4495521Sas200622 
4505521Sas200622 	LE_OUT32(&le_data[0], data[0]);
4515521Sas200622 	LE_OUT32(&le_data[1], data[1]);
4525331Samw 
4537052Samw 	if (smb_auth_DES(buffer, 8, session_key, NETR_DESKEY_LEN,
4545521Sas200622 	    (unsigned char *)le_data, 8) != SMBAUTH_SUCCESS)
4555331Samw 		return (SMBAUTH_FAILURE);
4565331Samw 
4577052Samw 	rc = smb_auth_DES(out_cred->data, 8, &session_key[NETR_DESKEY_LEN],
4587052Samw 	    NETR_DESKEY_LEN, buffer, 8);
4595331Samw 
4605331Samw 	return (rc);
4615331Samw }
4625331Samw 
4635331Samw /*
4645331Samw  * netr_server_password_set
4655331Samw  *
4665331Samw  * Attempt to change the trust account password for this system.
4675331Samw  *
4685331Samw  * Note that this call may legitimately fail if the registry on the
4695331Samw  * domain controller has been setup to deny attempts to change the
4705331Samw  * trust account password. In this case we should just continue to
4715331Samw  * use the original password.
4725331Samw  *
4735331Samw  * Possible status values:
4745331Samw  *	NT_STATUS_ACCESS_DENIED
4755331Samw  */
4765331Samw int
4775331Samw netr_server_password_set(mlsvc_handle_t *netr_handle, netr_info_t *netr_info)
4785331Samw {
4795331Samw 	struct netr_PasswordSet  arg;
4805331Samw 	int opnum;
4815331Samw 	BYTE new_password[NETR_OWF_PASSWORD_SZ];
4827961SNatalie.Li@Sun.COM 	char account_name[NETBIOS_NAME_SZ * 2];
4835331Samw 
4845331Samw 	bzero(&arg, sizeof (struct netr_PasswordSet));
4855331Samw 	opnum = NETR_OPNUM_ServerPasswordSet;
4865331Samw 
4875331Samw 	(void) snprintf(account_name, sizeof (account_name), "%s$",
4885331Samw 	    netr_info->hostname);
4895331Samw 
4905331Samw 	arg.servername = (unsigned char *)netr_info->server;
4915331Samw 	arg.account_name = (unsigned char *)account_name;
4925331Samw 	arg.account_type = NETR_WKSTA_TRUST_ACCOUNT_TYPE;
4935331Samw 	arg.hostname = (unsigned char *)netr_info->hostname;
4945331Samw 
4955331Samw 	/*
4965331Samw 	 * Set up the client side authenticator.
4975331Samw 	 */
4985331Samw 	if (netr_setup_authenticator(netr_info, &arg.auth, 0) !=
4995331Samw 	    SMBAUTH_SUCCESS) {
5005331Samw 		return (-1);
5015331Samw 	}
5025331Samw 
5035331Samw 	/*
5045331Samw 	 * Generate a new password from the old password.
5055331Samw 	 */
5067619SJose.Borrego@Sun.COM 	if (netr_gen_password(netr_info->session_key.key,
5075331Samw 	    netr_info->password, new_password) == SMBAUTH_FAILURE) {
5085331Samw 		return (-1);
5095331Samw 	}
5105331Samw 
5115331Samw 	(void) memcpy(&arg.uas_new_password, &new_password,
5125331Samw 	    NETR_OWF_PASSWORD_SZ);
5135331Samw 
514*8334SJose.Borrego@Sun.COM 	if (ndr_rpc_call(netr_handle, opnum, &arg) != 0)
515*8334SJose.Borrego@Sun.COM 		return (-1);
516*8334SJose.Borrego@Sun.COM 
517*8334SJose.Borrego@Sun.COM 	if (arg.status != 0) {
518*8334SJose.Borrego@Sun.COM 		ndr_rpc_status(netr_handle, opnum, arg.status);
519*8334SJose.Borrego@Sun.COM 		ndr_rpc_release(netr_handle);
5205331Samw 		return (-1);
5215331Samw 	}
5225331Samw 
5235331Samw 	/*
5245331Samw 	 * Check the returned credentials.  The server returns the new
5255331Samw 	 * client credential rather than the new server credentiali,
5265331Samw 	 * as documented elsewhere.
5275331Samw 	 *
5285331Samw 	 * Generate the new seed for the credential chain.  Increment
5295331Samw 	 * the timestamp and add it to the client challenge.  Then we
5305331Samw 	 * need to copy the challenge to the credential field in
5315331Samw 	 * preparation for the next cycle.
5325331Samw 	 */
5335331Samw 	if (netr_validate_chain(netr_info, &arg.auth) == 0) {
5345331Samw 		/*
5355331Samw 		 * Save the new password.
5365331Samw 		 */
5375331Samw 		(void) memcpy(netr_info->password, new_password,
5385331Samw 		    NETR_OWF_PASSWORD_SZ);
5395331Samw 	}
5405331Samw 
541*8334SJose.Borrego@Sun.COM 	ndr_rpc_release(netr_handle);
5425331Samw 	return (0);
5435331Samw }
5445331Samw 
5455331Samw /*
5465331Samw  * netr_gen_password
5475331Samw  *
5485331Samw  * Generate a new pasword from the old password  and the session key.
5495331Samw  * The algorithm is a two stage hash. The session key is used in the
5505331Samw  * first hash but only part of the session key is used in the second
5515331Samw  * hash.
5525331Samw  *
5535331Samw  * If any difficulties occur using the cryptographic framework, the
5545331Samw  * function returns SMBAUTH_FAILURE.  Otherwise SMBAUTH_SUCCESS is
5555331Samw  * returned.
5565331Samw  */
5575331Samw static int
5585331Samw netr_gen_password(BYTE *session_key, BYTE *old_password, BYTE *new_password)
5595331Samw {
5605331Samw 	int rv;
5615331Samw 
5627052Samw 	rv = smb_auth_DES(new_password, 8, session_key, NETR_DESKEY_LEN,
5637052Samw 	    old_password, 8);
5645331Samw 	if (rv != SMBAUTH_SUCCESS)
5655331Samw 		return (rv);
5665331Samw 
5677052Samw 	rv = smb_auth_DES(&new_password[8], 8, &session_key[NETR_DESKEY_LEN],
5687052Samw 	    NETR_DESKEY_LEN, &old_password[8], 8);
5695331Samw 	return (rv);
5705331Samw }
571