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 */
21*12508Samw@Sun.COM
225331Samw /*
23*12508Samw@Sun.COM * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
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>
436432Sas200622 #include <smbsrv/libsmbns.h>
448334SJose.Borrego@Sun.COM #include <smbsrv/libmlsvc.h>
455331Samw #include <smbsrv/ndl/netlogon.ndl>
465331Samw #include <smbsrv/smbinfo.h>
475331Samw #include <smbsrv/netrauth.h>
485331Samw
497052Samw #define NETR_SESSKEY_ZEROBUF_SZ 4
507619SJose.Borrego@Sun.COM /* The DES algorithm uses a 56-bit encryption key. */
517052Samw #define NETR_DESKEY_LEN 7
527052Samw
535331Samw int netr_setup_authenticator(netr_info_t *, struct netr_authenticator *,
545331Samw struct netr_authenticator *);
555331Samw DWORD netr_validate_chain(netr_info_t *, struct netr_authenticator *);
565331Samw
575331Samw static int netr_server_req_challenge(mlsvc_handle_t *, netr_info_t *);
585331Samw static int netr_server_authenticate2(mlsvc_handle_t *, netr_info_t *);
595331Samw static int netr_gen_password(BYTE *, BYTE *, BYTE *);
605331Samw
615331Samw /*
625331Samw * Shared with netr_logon.c
635331Samw */
645331Samw netr_info_t netr_global_info;
655331Samw
665331Samw /*
675331Samw * netlogon_auth
685331Samw *
695331Samw * This is the core of the NETLOGON authentication protocol.
705331Samw * Do the challenge response authentication.
715331Samw *
725331Samw * Prior to calling this function, an anonymous session to the NETLOGON
735331Samw * pipe on a domain controller(server) should have already been opened.
746139Sjb150015 *
756139Sjb150015 * Upon a successful NETLOGON credential chain establishment, the
766139Sjb150015 * netlogon sequence number will be set to match the kpasswd sequence
776139Sjb150015 * number.
786139Sjb150015 *
795331Samw */
805331Samw DWORD
netlogon_auth(char * server,mlsvc_handle_t * netr_handle,DWORD flags)815331Samw netlogon_auth(char *server, mlsvc_handle_t *netr_handle, DWORD flags)
825331Samw {
835331Samw netr_info_t *netr_info;
845331Samw int rc;
855521Sas200622 DWORD leout_rc[2];
865331Samw
875331Samw netr_info = &netr_global_info;
885331Samw bzero(netr_info, sizeof (netr_info_t));
895331Samw
905331Samw netr_info->flags |= flags;
915331Samw
927961SNatalie.Li@Sun.COM rc = smb_getnetbiosname(netr_info->hostname, NETBIOS_NAME_SZ);
935331Samw if (rc != 0)
945331Samw return (NT_STATUS_UNSUCCESSFUL);
955331Samw
965331Samw (void) snprintf(netr_info->server, sizeof (netr_info->server),
975331Samw "\\\\%s", server);
985331Samw
995521Sas200622 LE_OUT32(&leout_rc[0], random());
1005521Sas200622 LE_OUT32(&leout_rc[1], random());
1015521Sas200622 (void) memcpy(&netr_info->client_challenge, leout_rc,
1025331Samw sizeof (struct netr_credential));
1035331Samw
1045331Samw if ((rc = netr_server_req_challenge(netr_handle, netr_info)) == 0) {
1055331Samw rc = netr_server_authenticate2(netr_handle, netr_info);
1066139Sjb150015 if (rc == 0) {
1076139Sjb150015 smb_update_netlogon_seqnum();
1085331Samw netr_info->flags |= NETR_FLG_VALID;
1096139Sjb150015
1106139Sjb150015 }
1115331Samw }
1125331Samw
1135331Samw return ((rc) ? NT_STATUS_UNSUCCESSFUL : NT_STATUS_SUCCESS);
1145331Samw }
1155331Samw
1165331Samw /*
1175331Samw * netr_open
1185331Samw *
11910122SJordan.Brown@Sun.COM * Open an anonymous session to the NETLOGON pipe on a domain controller
12010122SJordan.Brown@Sun.COM * and bind to the NETR RPC interface.
12110122SJordan.Brown@Sun.COM *
12210122SJordan.Brown@Sun.COM * We store the remote server information, which is used to drive Windows
12310122SJordan.Brown@Sun.COM * version specific behavior.
1245331Samw */
1255331Samw int
netr_open(char * server,char * domain,mlsvc_handle_t * netr_handle)1265331Samw netr_open(char *server, char *domain, mlsvc_handle_t *netr_handle)
1275331Samw {
12810717Samw@Sun.COM char user[SMB_USERNAME_MAXLEN];
1295331Samw
13010717Samw@Sun.COM smb_ipc_get_user(user, SMB_USERNAME_MAXLEN);
13110122SJordan.Brown@Sun.COM
1328334SJose.Borrego@Sun.COM if (ndr_rpc_bind(netr_handle, server, domain, user, "NETR") < 0)
1335331Samw return (-1);
1345331Samw
1355331Samw return (0);
1365331Samw }
1375331Samw
1385331Samw /*
1395331Samw * netr_close
1405331Samw *
1415331Samw * Close a NETLOGON pipe and free the RPC context.
1425331Samw */
1435331Samw int
netr_close(mlsvc_handle_t * netr_handle)1445331Samw netr_close(mlsvc_handle_t *netr_handle)
1455331Samw {
1468334SJose.Borrego@Sun.COM ndr_rpc_unbind(netr_handle);
1475331Samw return (0);
1485331Samw }
1495331Samw
1505331Samw /*
1515331Samw * netr_server_req_challenge
1525331Samw */
1535331Samw static int
netr_server_req_challenge(mlsvc_handle_t * netr_handle,netr_info_t * netr_info)1545331Samw netr_server_req_challenge(mlsvc_handle_t *netr_handle, netr_info_t *netr_info)
1555331Samw {
1565331Samw struct netr_ServerReqChallenge arg;
1575331Samw int opnum;
1585331Samw
1595331Samw bzero(&arg, sizeof (struct netr_ServerReqChallenge));
1605331Samw opnum = NETR_OPNUM_ServerReqChallenge;
1615331Samw
1625331Samw arg.servername = (unsigned char *)netr_info->server;
1635331Samw arg.hostname = (unsigned char *)netr_info->hostname;
1645331Samw
1655331Samw (void) memcpy(&arg.client_challenge, &netr_info->client_challenge,
1665331Samw sizeof (struct netr_credential));
1675331Samw
1688334SJose.Borrego@Sun.COM if (ndr_rpc_call(netr_handle, opnum, &arg) != 0)
1698334SJose.Borrego@Sun.COM return (-1);
1708334SJose.Borrego@Sun.COM
1718334SJose.Borrego@Sun.COM if (arg.status != 0) {
1728334SJose.Borrego@Sun.COM ndr_rpc_status(netr_handle, opnum, arg.status);
1738334SJose.Borrego@Sun.COM ndr_rpc_release(netr_handle);
1748334SJose.Borrego@Sun.COM return (-1);
1755331Samw }
1765331Samw
1778334SJose.Borrego@Sun.COM (void) memcpy(&netr_info->server_challenge, &arg.server_challenge,
1788334SJose.Borrego@Sun.COM sizeof (struct netr_credential));
1798334SJose.Borrego@Sun.COM
1808334SJose.Borrego@Sun.COM ndr_rpc_release(netr_handle);
1818334SJose.Borrego@Sun.COM return (0);
1825331Samw }
1835331Samw
1845331Samw /*
1855331Samw * netr_server_authenticate2
1865331Samw */
1875331Samw static int
netr_server_authenticate2(mlsvc_handle_t * netr_handle,netr_info_t * netr_info)1885331Samw netr_server_authenticate2(mlsvc_handle_t *netr_handle, netr_info_t *netr_info)
1895331Samw {
1905331Samw struct netr_ServerAuthenticate2 arg;
1915331Samw int opnum;
1925331Samw int rc;
1937961SNatalie.Li@Sun.COM char account_name[NETBIOS_NAME_SZ * 2];
1945331Samw
1955331Samw bzero(&arg, sizeof (struct netr_ServerAuthenticate2));
1965331Samw opnum = NETR_OPNUM_ServerAuthenticate2;
1975331Samw
1985331Samw (void) snprintf(account_name, sizeof (account_name), "%s$",
1995331Samw netr_info->hostname);
2005331Samw
2017619SJose.Borrego@Sun.COM smb_tracef("server=[%s] account_name=[%s] hostname=[%s]\n",
2027619SJose.Borrego@Sun.COM netr_info->server, account_name, netr_info->hostname);
2037619SJose.Borrego@Sun.COM
2045331Samw arg.servername = (unsigned char *)netr_info->server;
2055331Samw arg.account_name = (unsigned char *)account_name;
2065331Samw arg.account_type = NETR_WKSTA_TRUST_ACCOUNT_TYPE;
2075331Samw arg.hostname = (unsigned char *)netr_info->hostname;
2087619SJose.Borrego@Sun.COM arg.negotiate_flags = NETR_NEGOTIATE_BASE_FLAGS;
2095331Samw
21010717Samw@Sun.COM if (ndr_rpc_server_os(netr_handle) == NATIVE_OS_WIN2000) {
2117619SJose.Borrego@Sun.COM arg.negotiate_flags |= NETR_NEGOTIATE_STRONGKEY_FLAG;
2127619SJose.Borrego@Sun.COM if (netr_gen_skey128(netr_info) != SMBAUTH_SUCCESS)
2137619SJose.Borrego@Sun.COM return (-1);
2147619SJose.Borrego@Sun.COM } else {
2157619SJose.Borrego@Sun.COM if (netr_gen_skey64(netr_info) != SMBAUTH_SUCCESS)
2167619SJose.Borrego@Sun.COM return (-1);
2177619SJose.Borrego@Sun.COM }
2185331Samw
2197619SJose.Borrego@Sun.COM if (netr_gen_credentials(netr_info->session_key.key,
2207619SJose.Borrego@Sun.COM &netr_info->client_challenge, 0,
2215331Samw &netr_info->client_credential) != SMBAUTH_SUCCESS) {
2225331Samw return (-1);
2235331Samw }
2245331Samw
2257619SJose.Borrego@Sun.COM if (netr_gen_credentials(netr_info->session_key.key,
2267619SJose.Borrego@Sun.COM &netr_info->server_challenge, 0,
2275331Samw &netr_info->server_credential) != SMBAUTH_SUCCESS) {
2285331Samw return (-1);
2295331Samw }
2305331Samw
2315331Samw (void) memcpy(&arg.client_credential, &netr_info->client_credential,
2325331Samw sizeof (struct netr_credential));
2335331Samw
2348334SJose.Borrego@Sun.COM if (ndr_rpc_call(netr_handle, opnum, &arg) != 0)
2358334SJose.Borrego@Sun.COM return (-1);
2365331Samw
2378334SJose.Borrego@Sun.COM if (arg.status != 0) {
2388334SJose.Borrego@Sun.COM ndr_rpc_status(netr_handle, opnum, arg.status);
2398334SJose.Borrego@Sun.COM ndr_rpc_release(netr_handle);
2408334SJose.Borrego@Sun.COM return (-1);
2415331Samw }
2425331Samw
2438334SJose.Borrego@Sun.COM rc = memcmp(&netr_info->server_credential, &arg.server_credential,
2448334SJose.Borrego@Sun.COM sizeof (struct netr_credential));
2458334SJose.Borrego@Sun.COM
2468334SJose.Borrego@Sun.COM ndr_rpc_release(netr_handle);
2475331Samw return (rc);
2485331Samw }
2495331Samw
2505331Samw /*
2517619SJose.Borrego@Sun.COM * netr_gen_skey128
2525331Samw *
2537619SJose.Borrego@Sun.COM * Generate a 128-bit session key from the client and server challenges.
2547619SJose.Borrego@Sun.COM * See "Session-Key Computation" section of MS-NRPC document.
2555331Samw */
2567052Samw int
netr_gen_skey128(netr_info_t * netr_info)2577619SJose.Borrego@Sun.COM netr_gen_skey128(netr_info_t *netr_info)
2587052Samw {
2597052Samw unsigned char ntlmhash[SMBAUTH_HASH_SZ];
2607052Samw int rc = SMBAUTH_FAILURE;
2617052Samw CK_RV rv;
2627052Samw CK_MECHANISM mechanism;
2637052Samw CK_SESSION_HANDLE hSession;
2647052Samw CK_ULONG diglen = MD_DIGEST_LEN;
2657052Samw unsigned char md5digest[MD_DIGEST_LEN];
2667052Samw unsigned char zerobuf[NETR_SESSKEY_ZEROBUF_SZ];
2677052Samw
2687052Samw bzero(ntlmhash, SMBAUTH_HASH_SZ);
2697052Samw /*
2707052Samw * We should check (netr_info->flags & NETR_FLG_INIT) and use
2717052Samw * the appropriate password but it isn't working yet. So we
2727052Samw * always use the default one for now.
2737052Samw */
2747052Samw bzero(netr_info->password, sizeof (netr_info->password));
2757052Samw rc = smb_config_getstr(SMB_CI_MACHINE_PASSWD,
2767052Samw (char *)netr_info->password, sizeof (netr_info->password));
2777052Samw
2787052Samw if ((rc != SMBD_SMF_OK) || *netr_info->password == '\0') {
2797052Samw return (SMBAUTH_FAILURE);
2807052Samw }
2817052Samw
2827052Samw rc = smb_auth_ntlm_hash((char *)netr_info->password, ntlmhash);
2837052Samw if (rc != SMBAUTH_SUCCESS)
2847052Samw return (SMBAUTH_FAILURE);
2857052Samw
2867052Samw bzero(zerobuf, NETR_SESSKEY_ZEROBUF_SZ);
2877052Samw
2887052Samw mechanism.mechanism = CKM_MD5;
2897052Samw mechanism.pParameter = 0;
2907052Samw mechanism.ulParameterLen = 0;
2917052Samw
2927052Samw rv = SUNW_C_GetMechSession(mechanism.mechanism, &hSession);
2937052Samw if (rv != CKR_OK)
2947052Samw return (SMBAUTH_FAILURE);
2957052Samw
2967052Samw rv = C_DigestInit(hSession, &mechanism);
2977052Samw if (rv != CKR_OK)
2987052Samw goto cleanup;
2997052Samw
3007052Samw rv = C_DigestUpdate(hSession, (CK_BYTE_PTR)zerobuf,
3017052Samw NETR_SESSKEY_ZEROBUF_SZ);
3027052Samw if (rv != CKR_OK)
3037052Samw goto cleanup;
3047052Samw
3057052Samw rv = C_DigestUpdate(hSession,
3067052Samw (CK_BYTE_PTR)netr_info->client_challenge.data, NETR_CRED_DATA_SZ);
3077052Samw if (rv != CKR_OK)
3087052Samw goto cleanup;
3097052Samw
3107052Samw rv = C_DigestUpdate(hSession,
3117052Samw (CK_BYTE_PTR)netr_info->server_challenge.data, NETR_CRED_DATA_SZ);
3127052Samw if (rv != CKR_OK)
3137052Samw goto cleanup;
3147052Samw
3157052Samw rv = C_DigestFinal(hSession, (CK_BYTE_PTR)md5digest, &diglen);
3167052Samw if (rv != CKR_OK)
3177052Samw goto cleanup;
3187052Samw
3197052Samw rc = smb_auth_hmac_md5(md5digest, diglen, ntlmhash, SMBAUTH_HASH_SZ,
3207619SJose.Borrego@Sun.COM netr_info->session_key.key);
3217052Samw
3227619SJose.Borrego@Sun.COM netr_info->session_key.len = NETR_SESSKEY128_SZ;
3237052Samw cleanup:
3247052Samw (void) C_CloseSession(hSession);
3257052Samw return (rc);
3267052Samw
3277052Samw }
3287619SJose.Borrego@Sun.COM /*
3297619SJose.Borrego@Sun.COM * netr_gen_skey64
3307619SJose.Borrego@Sun.COM *
3317619SJose.Borrego@Sun.COM * Generate a 64-bit session key from the client and server challenges.
3327619SJose.Borrego@Sun.COM * See "Session-Key Computation" section of MS-NRPC document.
3337619SJose.Borrego@Sun.COM *
3347619SJose.Borrego@Sun.COM * The algorithm is a two stage hash. For the first hash, the input is
3357619SJose.Borrego@Sun.COM * the combination of the client and server challenges, the key is
3367619SJose.Borrego@Sun.COM * the first 7 bytes of the password. The initial password is formed
3377619SJose.Borrego@Sun.COM * using the NT password hash on the local hostname in lower case.
3387619SJose.Borrego@Sun.COM * The result is stored in a temporary buffer.
3397619SJose.Borrego@Sun.COM *
3407619SJose.Borrego@Sun.COM * input: challenge
3417619SJose.Borrego@Sun.COM * key: passwd lower 7 bytes
3427619SJose.Borrego@Sun.COM * output: intermediate result
3437619SJose.Borrego@Sun.COM *
3447619SJose.Borrego@Sun.COM * For the second hash, the input is the result of the first hash and
3457619SJose.Borrego@Sun.COM * the key is the last 7 bytes of the password.
3467619SJose.Borrego@Sun.COM *
3477619SJose.Borrego@Sun.COM * input: result of first hash
3487619SJose.Borrego@Sun.COM * key: passwd upper 7 bytes
3497619SJose.Borrego@Sun.COM * output: session_key
3507619SJose.Borrego@Sun.COM *
3517619SJose.Borrego@Sun.COM * The final output should be the session key.
3527619SJose.Borrego@Sun.COM *
3537619SJose.Borrego@Sun.COM * FYI: smb_auth_DES(output, key, input)
3547619SJose.Borrego@Sun.COM *
3557619SJose.Borrego@Sun.COM * If any difficulties occur using the cryptographic framework, the
3567619SJose.Borrego@Sun.COM * function returns SMBAUTH_FAILURE. Otherwise SMBAUTH_SUCCESS is
3577619SJose.Borrego@Sun.COM * returned.
3587619SJose.Borrego@Sun.COM */
3595331Samw int
netr_gen_skey64(netr_info_t * netr_info)3607619SJose.Borrego@Sun.COM netr_gen_skey64(netr_info_t *netr_info)
3615331Samw {
3625331Samw unsigned char md4hash[32];
3635331Samw unsigned char buffer[8];
3645331Samw DWORD data[2];
3655331Samw DWORD *client_challenge;
3665331Samw DWORD *server_challenge;
3675331Samw int rc;
3685521Sas200622 DWORD le_data[2];
3695331Samw
3705331Samw client_challenge = (DWORD *)(uintptr_t)&netr_info->client_challenge;
3715331Samw server_challenge = (DWORD *)(uintptr_t)&netr_info->server_challenge;
3725331Samw bzero(md4hash, 32);
3735331Samw
3745331Samw /*
3755331Samw * We should check (netr_info->flags & NETR_FLG_INIT) and use
3765331Samw * the appropriate password but it isn't working yet. So we
3775331Samw * always use the default one for now.
3785331Samw */
3795772Sas200622 bzero(netr_info->password, sizeof (netr_info->password));
3805772Sas200622 rc = smb_config_getstr(SMB_CI_MACHINE_PASSWD,
3815772Sas200622 (char *)netr_info->password, sizeof (netr_info->password));
3825331Samw
3835772Sas200622 if ((rc != SMBD_SMF_OK) || *netr_info->password == '\0') {
3847052Samw return (SMBAUTH_FAILURE);
3855331Samw }
3865331Samw
3875772Sas200622 rc = smb_auth_ntlm_hash((char *)netr_info->password, md4hash);
3885331Samw
3895331Samw if (rc != SMBAUTH_SUCCESS)
3905331Samw return (SMBAUTH_FAILURE);
3915331Samw
3925331Samw data[0] = LE_IN32(&client_challenge[0]) + LE_IN32(&server_challenge[0]);
3935331Samw data[1] = LE_IN32(&client_challenge[1]) + LE_IN32(&server_challenge[1]);
3945521Sas200622 LE_OUT32(&le_data[0], data[0]);
3955521Sas200622 LE_OUT32(&le_data[1], data[1]);
3967619SJose.Borrego@Sun.COM rc = smb_auth_DES(buffer, 8, md4hash, NETR_DESKEY_LEN,
3977619SJose.Borrego@Sun.COM (unsigned char *)le_data, 8);
3985331Samw
3995331Samw if (rc != SMBAUTH_SUCCESS)
4005331Samw return (rc);
4015331Samw
4027619SJose.Borrego@Sun.COM netr_info->session_key.len = NETR_SESSKEY64_SZ;
4037619SJose.Borrego@Sun.COM rc = smb_auth_DES(netr_info->session_key.key,
4047619SJose.Borrego@Sun.COM netr_info->session_key.len, &md4hash[9], NETR_DESKEY_LEN, buffer,
4057619SJose.Borrego@Sun.COM 8);
4067619SJose.Borrego@Sun.COM
4075331Samw return (rc);
4085331Samw }
4095331Samw
4105331Samw /*
4115331Samw * netr_gen_credentials
4125331Samw *
4135331Samw * Generate a set of credentials from a challenge and a session key.
4145331Samw * The algorithm is a two stage hash. For the first hash, the
4155331Samw * timestamp is added to the challenge and the result is stored in a
4165331Samw * temporary buffer:
4175331Samw *
4185331Samw * input: challenge (including timestamp)
4195331Samw * key: session_key
4205331Samw * output: intermediate result
4215331Samw *
4225331Samw * For the second hash, the input is the result of the first hash and
4235331Samw * a strange partial key is used:
4245331Samw *
4255331Samw * input: result of first hash
4265331Samw * key: funny partial key
4275331Samw * output: credentiails
4285331Samw *
4295331Samw * The final output should be an encrypted set of credentials.
4305331Samw *
4315331Samw * FYI: smb_auth_DES(output, key, input)
4325331Samw *
4335331Samw * If any difficulties occur using the cryptographic framework, the
4345331Samw * function returns SMBAUTH_FAILURE. Otherwise SMBAUTH_SUCCESS is
4355331Samw * returned.
4365331Samw */
4375331Samw int
netr_gen_credentials(BYTE * session_key,netr_cred_t * challenge,DWORD timestamp,netr_cred_t * out_cred)4385331Samw netr_gen_credentials(BYTE *session_key, netr_cred_t *challenge,
4395331Samw DWORD timestamp, netr_cred_t *out_cred)
4405331Samw {
4415331Samw unsigned char buffer[8];
4425331Samw DWORD data[2];
4435521Sas200622 DWORD le_data[2];
4445331Samw DWORD *p;
4455331Samw int rc;
4465331Samw
4475331Samw p = (DWORD *)(uintptr_t)challenge;
4485521Sas200622 data[0] = LE_IN32(&p[0]) + timestamp;
4495521Sas200622 data[1] = LE_IN32(&p[1]);
4505521Sas200622
4515521Sas200622 LE_OUT32(&le_data[0], data[0]);
4525521Sas200622 LE_OUT32(&le_data[1], data[1]);
4535331Samw
4547052Samw if (smb_auth_DES(buffer, 8, session_key, NETR_DESKEY_LEN,
4555521Sas200622 (unsigned char *)le_data, 8) != SMBAUTH_SUCCESS)
4565331Samw return (SMBAUTH_FAILURE);
4575331Samw
4587052Samw rc = smb_auth_DES(out_cred->data, 8, &session_key[NETR_DESKEY_LEN],
4597052Samw NETR_DESKEY_LEN, buffer, 8);
4605331Samw
4615331Samw return (rc);
4625331Samw }
4635331Samw
4645331Samw /*
4655331Samw * netr_server_password_set
4665331Samw *
4675331Samw * Attempt to change the trust account password for this system.
4685331Samw *
4695331Samw * Note that this call may legitimately fail if the registry on the
4705331Samw * domain controller has been setup to deny attempts to change the
4715331Samw * trust account password. In this case we should just continue to
4725331Samw * use the original password.
4735331Samw *
4745331Samw * Possible status values:
4755331Samw * NT_STATUS_ACCESS_DENIED
4765331Samw */
4775331Samw int
netr_server_password_set(mlsvc_handle_t * netr_handle,netr_info_t * netr_info)4785331Samw netr_server_password_set(mlsvc_handle_t *netr_handle, netr_info_t *netr_info)
4795331Samw {
4805331Samw struct netr_PasswordSet arg;
4815331Samw int opnum;
4825331Samw BYTE new_password[NETR_OWF_PASSWORD_SZ];
4837961SNatalie.Li@Sun.COM char account_name[NETBIOS_NAME_SZ * 2];
4845331Samw
4855331Samw bzero(&arg, sizeof (struct netr_PasswordSet));
4865331Samw opnum = NETR_OPNUM_ServerPasswordSet;
4875331Samw
4885331Samw (void) snprintf(account_name, sizeof (account_name), "%s$",
4895331Samw netr_info->hostname);
4905331Samw
4915331Samw arg.servername = (unsigned char *)netr_info->server;
4925331Samw arg.account_name = (unsigned char *)account_name;
4935331Samw arg.account_type = NETR_WKSTA_TRUST_ACCOUNT_TYPE;
4945331Samw arg.hostname = (unsigned char *)netr_info->hostname;
4955331Samw
4965331Samw /*
4975331Samw * Set up the client side authenticator.
4985331Samw */
4995331Samw if (netr_setup_authenticator(netr_info, &arg.auth, 0) !=
5005331Samw SMBAUTH_SUCCESS) {
5015331Samw return (-1);
5025331Samw }
5035331Samw
5045331Samw /*
5055331Samw * Generate a new password from the old password.
5065331Samw */
5077619SJose.Borrego@Sun.COM if (netr_gen_password(netr_info->session_key.key,
5085331Samw netr_info->password, new_password) == SMBAUTH_FAILURE) {
5095331Samw return (-1);
5105331Samw }
5115331Samw
5125331Samw (void) memcpy(&arg.uas_new_password, &new_password,
5135331Samw NETR_OWF_PASSWORD_SZ);
5145331Samw
5158334SJose.Borrego@Sun.COM if (ndr_rpc_call(netr_handle, opnum, &arg) != 0)
5168334SJose.Borrego@Sun.COM return (-1);
5178334SJose.Borrego@Sun.COM
5188334SJose.Borrego@Sun.COM if (arg.status != 0) {
5198334SJose.Borrego@Sun.COM ndr_rpc_status(netr_handle, opnum, arg.status);
5208334SJose.Borrego@Sun.COM ndr_rpc_release(netr_handle);
5215331Samw return (-1);
5225331Samw }
5235331Samw
5245331Samw /*
5255331Samw * Check the returned credentials. The server returns the new
5265331Samw * client credential rather than the new server credentiali,
5275331Samw * as documented elsewhere.
5285331Samw *
5295331Samw * Generate the new seed for the credential chain. Increment
5305331Samw * the timestamp and add it to the client challenge. Then we
5315331Samw * need to copy the challenge to the credential field in
5325331Samw * preparation for the next cycle.
5335331Samw */
5345331Samw if (netr_validate_chain(netr_info, &arg.auth) == 0) {
5355331Samw /*
5365331Samw * Save the new password.
5375331Samw */
5385331Samw (void) memcpy(netr_info->password, new_password,
5395331Samw NETR_OWF_PASSWORD_SZ);
5405331Samw }
5415331Samw
5428334SJose.Borrego@Sun.COM ndr_rpc_release(netr_handle);
5435331Samw return (0);
5445331Samw }
5455331Samw
5465331Samw /*
5475331Samw * netr_gen_password
5485331Samw *
5495331Samw * Generate a new pasword from the old password and the session key.
5505331Samw * The algorithm is a two stage hash. The session key is used in the
5515331Samw * first hash but only part of the session key is used in the second
5525331Samw * hash.
5535331Samw *
5545331Samw * If any difficulties occur using the cryptographic framework, the
5555331Samw * function returns SMBAUTH_FAILURE. Otherwise SMBAUTH_SUCCESS is
5565331Samw * returned.
5575331Samw */
5585331Samw static int
netr_gen_password(BYTE * session_key,BYTE * old_password,BYTE * new_password)5595331Samw netr_gen_password(BYTE *session_key, BYTE *old_password, BYTE *new_password)
5605331Samw {
5615331Samw int rv;
5625331Samw
5637052Samw rv = smb_auth_DES(new_password, 8, session_key, NETR_DESKEY_LEN,
5647052Samw old_password, 8);
5655331Samw if (rv != SMBAUTH_SUCCESS)
5665331Samw return (rv);
5675331Samw
5687052Samw rv = smb_auth_DES(&new_password[8], 8, &session_key[NETR_DESKEY_LEN],
5697052Samw NETR_DESKEY_LEN, &old_password[8], 8);
5705331Samw return (rv);
5715331Samw }
572