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> 455331Samw #include <smbsrv/mlsvc_util.h> 465331Samw #include <smbsrv/ndl/netlogon.ndl> 475331Samw #include <smbsrv/ntstatus.h> 485331Samw #include <smbsrv/smbinfo.h> 495331Samw #include <smbsrv/mlsvc.h> 505331Samw #include <smbsrv/netrauth.h> 515331Samw 527052Samw #define NETR_SESSKEY_ZEROBUF_SZ 4 537619SJose.Borrego@Sun.COM /* The DES algorithm uses a 56-bit encryption key. */ 547052Samw #define NETR_DESKEY_LEN 7 557052Samw 565331Samw int netr_setup_authenticator(netr_info_t *, struct netr_authenticator *, 575331Samw struct netr_authenticator *); 585331Samw DWORD netr_validate_chain(netr_info_t *, struct netr_authenticator *); 595331Samw 605331Samw static int netr_server_req_challenge(mlsvc_handle_t *, netr_info_t *); 615331Samw static int netr_server_authenticate2(mlsvc_handle_t *, netr_info_t *); 625331Samw static int netr_gen_password(BYTE *, BYTE *, BYTE *); 635331Samw 645331Samw /* 655331Samw * Shared with netr_logon.c 665331Samw */ 675331Samw netr_info_t netr_global_info; 685331Samw 695331Samw /* 705331Samw * netlogon_auth 715331Samw * 725331Samw * This is the core of the NETLOGON authentication protocol. 735331Samw * Do the challenge response authentication. 745331Samw * 755331Samw * Prior to calling this function, an anonymous session to the NETLOGON 765331Samw * pipe on a domain controller(server) should have already been opened. 776139Sjb150015 * 786139Sjb150015 * Upon a successful NETLOGON credential chain establishment, the 796139Sjb150015 * netlogon sequence number will be set to match the kpasswd sequence 806139Sjb150015 * number. 816139Sjb150015 * 825331Samw */ 835331Samw DWORD 845331Samw netlogon_auth(char *server, mlsvc_handle_t *netr_handle, DWORD flags) 855331Samw { 865331Samw netr_info_t *netr_info; 875331Samw int rc; 885521Sas200622 DWORD leout_rc[2]; 895331Samw 905331Samw netr_info = &netr_global_info; 915331Samw bzero(netr_info, sizeof (netr_info_t)); 925331Samw 935331Samw netr_info->flags |= flags; 945331Samw 95*7961SNatalie.Li@Sun.COM rc = smb_getnetbiosname(netr_info->hostname, NETBIOS_NAME_SZ); 965331Samw if (rc != 0) 975331Samw return (NT_STATUS_UNSUCCESSFUL); 985331Samw 995331Samw (void) snprintf(netr_info->server, sizeof (netr_info->server), 1005331Samw "\\\\%s", server); 1015331Samw 1025521Sas200622 LE_OUT32(&leout_rc[0], random()); 1035521Sas200622 LE_OUT32(&leout_rc[1], random()); 1045521Sas200622 (void) memcpy(&netr_info->client_challenge, leout_rc, 1055331Samw sizeof (struct netr_credential)); 1065331Samw 1075331Samw if ((rc = netr_server_req_challenge(netr_handle, netr_info)) == 0) { 1085331Samw rc = netr_server_authenticate2(netr_handle, netr_info); 1096139Sjb150015 if (rc == 0) { 1106139Sjb150015 smb_update_netlogon_seqnum(); 1115331Samw netr_info->flags |= NETR_FLG_VALID; 1126139Sjb150015 1136139Sjb150015 } 1145331Samw } 1155331Samw 1165331Samw return ((rc) ? NT_STATUS_UNSUCCESSFUL : NT_STATUS_SUCCESS); 1175331Samw } 1185331Samw 1195331Samw /* 1205331Samw * netr_open 1215331Samw * 1225331Samw * Open an anonymous session to the NETLOGON pipe on a domain 1235331Samw * controller and bind to the NETR RPC interface. We store the 1245331Samw * remote server's native OS type - we may need it due to 1255331Samw * differences between versions of Windows. 1265331Samw */ 1275331Samw int 1285331Samw netr_open(char *server, char *domain, mlsvc_handle_t *netr_handle) 1295331Samw { 1305331Samw int fid; 1315331Samw int remote_os = 0; 1325331Samw int remote_lm = 0; 1335331Samw int server_pdc; 1345521Sas200622 char *user = smbrdr_ipc_get_user(); 1355331Samw 1365521Sas200622 if (mlsvc_logon(server, domain, user) != 0) 1375331Samw return (-1); 1385331Samw 1395521Sas200622 fid = mlsvc_open_pipe(server, domain, user, "\\NETLOGON"); 1405331Samw if (fid < 0) 1415331Samw return (-1); 1425331Samw 1435331Samw if (mlsvc_rpc_bind(netr_handle, fid, "NETR") < 0) { 1445331Samw (void) mlsvc_close_pipe(fid); 1455331Samw return (-1); 1465331Samw } 1475331Samw 1485331Samw (void) mlsvc_session_native_values(fid, &remote_os, &remote_lm, 1495331Samw &server_pdc); 1505331Samw netr_handle->context->server_os = remote_os; 1515331Samw netr_handle->context->server_pdc = server_pdc; 1525331Samw return (0); 1535331Samw } 1545331Samw 1555331Samw /* 1565331Samw * netr_close 1575331Samw * 1585331Samw * Close a NETLOGON pipe and free the RPC context. 1595331Samw */ 1605331Samw int 1615331Samw netr_close(mlsvc_handle_t *netr_handle) 1625331Samw { 1635331Samw (void) mlsvc_close_pipe(netr_handle->context->fid); 1645331Samw free(netr_handle->context); 1655331Samw return (0); 1665331Samw } 1675331Samw 1685331Samw /* 1695331Samw * netr_server_req_challenge 1705331Samw */ 1715331Samw static int 1725331Samw netr_server_req_challenge(mlsvc_handle_t *netr_handle, netr_info_t *netr_info) 1735331Samw { 1745331Samw struct netr_ServerReqChallenge arg; 1755331Samw mlrpc_heapref_t heap; 1765331Samw int opnum; 1775331Samw int rc; 1785331Samw 1795331Samw bzero(&arg, sizeof (struct netr_ServerReqChallenge)); 1805331Samw opnum = NETR_OPNUM_ServerReqChallenge; 1815331Samw 1825331Samw arg.servername = (unsigned char *)netr_info->server; 1835331Samw arg.hostname = (unsigned char *)netr_info->hostname; 1845331Samw 1855331Samw (void) memcpy(&arg.client_challenge, &netr_info->client_challenge, 1865331Samw sizeof (struct netr_credential)); 1875331Samw 1885331Samw (void) mlsvc_rpc_init(&heap); 1895331Samw rc = mlsvc_rpc_call(netr_handle->context, opnum, &arg, &heap); 1905331Samw if (rc == 0) { 1915331Samw if (arg.status != 0) { 1925331Samw mlsvc_rpc_report_status(opnum, arg.status); 1935331Samw rc = -1; 1945331Samw } else { 1955331Samw (void) memcpy(&netr_info->server_challenge, 1965331Samw &arg.server_challenge, 1975331Samw sizeof (struct netr_credential)); 1985331Samw } 1995331Samw } 2005331Samw 2015331Samw mlsvc_rpc_free(netr_handle->context, &heap); 2025331Samw return (rc); 2035331Samw } 2045331Samw 2055331Samw /* 2065331Samw * netr_server_authenticate2 2075331Samw */ 2085331Samw static int 2095331Samw netr_server_authenticate2(mlsvc_handle_t *netr_handle, netr_info_t *netr_info) 2105331Samw { 2115331Samw struct netr_ServerAuthenticate2 arg; 2125331Samw mlrpc_heapref_t heap; 2135331Samw int opnum; 2145331Samw int rc; 215*7961SNatalie.Li@Sun.COM char account_name[NETBIOS_NAME_SZ * 2]; 2165331Samw 2175331Samw bzero(&arg, sizeof (struct netr_ServerAuthenticate2)); 2185331Samw opnum = NETR_OPNUM_ServerAuthenticate2; 2195331Samw 2205331Samw (void) snprintf(account_name, sizeof (account_name), "%s$", 2215331Samw netr_info->hostname); 2225331Samw 2237619SJose.Borrego@Sun.COM smb_tracef("server=[%s] account_name=[%s] hostname=[%s]\n", 2247619SJose.Borrego@Sun.COM netr_info->server, account_name, netr_info->hostname); 2257619SJose.Borrego@Sun.COM 2265331Samw arg.servername = (unsigned char *)netr_info->server; 2275331Samw arg.account_name = (unsigned char *)account_name; 2285331Samw arg.account_type = NETR_WKSTA_TRUST_ACCOUNT_TYPE; 2295331Samw arg.hostname = (unsigned char *)netr_info->hostname; 2307619SJose.Borrego@Sun.COM arg.negotiate_flags = NETR_NEGOTIATE_BASE_FLAGS; 2315331Samw 2327619SJose.Borrego@Sun.COM if (netr_handle->context->server_os != NATIVE_OS_WINNT) { 2337619SJose.Borrego@Sun.COM arg.negotiate_flags |= NETR_NEGOTIATE_STRONGKEY_FLAG; 2347619SJose.Borrego@Sun.COM if (netr_gen_skey128(netr_info) != SMBAUTH_SUCCESS) 2357619SJose.Borrego@Sun.COM return (-1); 2367619SJose.Borrego@Sun.COM } else { 2377619SJose.Borrego@Sun.COM if (netr_gen_skey64(netr_info) != SMBAUTH_SUCCESS) 2387619SJose.Borrego@Sun.COM return (-1); 2397619SJose.Borrego@Sun.COM } 2405331Samw 2417619SJose.Borrego@Sun.COM if (netr_gen_credentials(netr_info->session_key.key, 2427619SJose.Borrego@Sun.COM &netr_info->client_challenge, 0, 2435331Samw &netr_info->client_credential) != SMBAUTH_SUCCESS) { 2445331Samw return (-1); 2455331Samw } 2465331Samw 2477619SJose.Borrego@Sun.COM if (netr_gen_credentials(netr_info->session_key.key, 2487619SJose.Borrego@Sun.COM &netr_info->server_challenge, 0, 2495331Samw &netr_info->server_credential) != SMBAUTH_SUCCESS) { 2505331Samw return (-1); 2515331Samw } 2525331Samw 2535331Samw (void) memcpy(&arg.client_credential, &netr_info->client_credential, 2545331Samw sizeof (struct netr_credential)); 2555331Samw 2565331Samw (void) mlsvc_rpc_init(&heap); 2575331Samw 2585331Samw rc = mlsvc_rpc_call(netr_handle->context, opnum, &arg, &heap); 2595331Samw if (rc == 0) { 2605331Samw if (arg.status != 0) { 2615331Samw mlsvc_rpc_report_status(opnum, arg.status); 2625331Samw rc = -1; 2635331Samw } else { 2645331Samw rc = memcmp(&netr_info->server_credential, 2655331Samw &arg.server_credential, 2665331Samw sizeof (struct netr_credential)); 2675331Samw } 2685331Samw } 2695331Samw 2705331Samw mlsvc_rpc_free(netr_handle->context, &heap); 2715331Samw return (rc); 2725331Samw } 2735331Samw 2745331Samw /* 2757619SJose.Borrego@Sun.COM * netr_gen_skey128 2765331Samw * 2777619SJose.Borrego@Sun.COM * Generate a 128-bit session key from the client and server challenges. 2787619SJose.Borrego@Sun.COM * See "Session-Key Computation" section of MS-NRPC document. 2795331Samw */ 2807052Samw int 2817619SJose.Borrego@Sun.COM netr_gen_skey128(netr_info_t *netr_info) 2827052Samw { 2837052Samw unsigned char ntlmhash[SMBAUTH_HASH_SZ]; 2847052Samw int rc = SMBAUTH_FAILURE; 2857052Samw CK_RV rv; 2867052Samw CK_MECHANISM mechanism; 2877052Samw CK_SESSION_HANDLE hSession; 2887052Samw CK_ULONG diglen = MD_DIGEST_LEN; 2897052Samw unsigned char md5digest[MD_DIGEST_LEN]; 2907052Samw unsigned char zerobuf[NETR_SESSKEY_ZEROBUF_SZ]; 2917052Samw 2927052Samw bzero(ntlmhash, SMBAUTH_HASH_SZ); 2937052Samw /* 2947052Samw * We should check (netr_info->flags & NETR_FLG_INIT) and use 2957052Samw * the appropriate password but it isn't working yet. So we 2967052Samw * always use the default one for now. 2977052Samw */ 2987052Samw bzero(netr_info->password, sizeof (netr_info->password)); 2997052Samw rc = smb_config_getstr(SMB_CI_MACHINE_PASSWD, 3007052Samw (char *)netr_info->password, sizeof (netr_info->password)); 3017052Samw 3027052Samw if ((rc != SMBD_SMF_OK) || *netr_info->password == '\0') { 3037052Samw return (SMBAUTH_FAILURE); 3047052Samw } 3057052Samw 3067052Samw rc = smb_auth_ntlm_hash((char *)netr_info->password, ntlmhash); 3077052Samw if (rc != SMBAUTH_SUCCESS) 3087052Samw return (SMBAUTH_FAILURE); 3097052Samw 3107052Samw bzero(zerobuf, NETR_SESSKEY_ZEROBUF_SZ); 3117052Samw 3127052Samw mechanism.mechanism = CKM_MD5; 3137052Samw mechanism.pParameter = 0; 3147052Samw mechanism.ulParameterLen = 0; 3157052Samw 3167052Samw rv = SUNW_C_GetMechSession(mechanism.mechanism, &hSession); 3177052Samw if (rv != CKR_OK) 3187052Samw return (SMBAUTH_FAILURE); 3197052Samw 3207052Samw rv = C_DigestInit(hSession, &mechanism); 3217052Samw if (rv != CKR_OK) 3227052Samw goto cleanup; 3237052Samw 3247052Samw rv = C_DigestUpdate(hSession, (CK_BYTE_PTR)zerobuf, 3257052Samw NETR_SESSKEY_ZEROBUF_SZ); 3267052Samw if (rv != CKR_OK) 3277052Samw goto cleanup; 3287052Samw 3297052Samw rv = C_DigestUpdate(hSession, 3307052Samw (CK_BYTE_PTR)netr_info->client_challenge.data, NETR_CRED_DATA_SZ); 3317052Samw if (rv != CKR_OK) 3327052Samw goto cleanup; 3337052Samw 3347052Samw rv = C_DigestUpdate(hSession, 3357052Samw (CK_BYTE_PTR)netr_info->server_challenge.data, NETR_CRED_DATA_SZ); 3367052Samw if (rv != CKR_OK) 3377052Samw goto cleanup; 3387052Samw 3397052Samw rv = C_DigestFinal(hSession, (CK_BYTE_PTR)md5digest, &diglen); 3407052Samw if (rv != CKR_OK) 3417052Samw goto cleanup; 3427052Samw 3437052Samw rc = smb_auth_hmac_md5(md5digest, diglen, ntlmhash, SMBAUTH_HASH_SZ, 3447619SJose.Borrego@Sun.COM netr_info->session_key.key); 3457052Samw 3467619SJose.Borrego@Sun.COM netr_info->session_key.len = NETR_SESSKEY128_SZ; 3477052Samw cleanup: 3487052Samw (void) C_CloseSession(hSession); 3497052Samw return (rc); 3507052Samw 3517052Samw } 3527619SJose.Borrego@Sun.COM /* 3537619SJose.Borrego@Sun.COM * netr_gen_skey64 3547619SJose.Borrego@Sun.COM * 3557619SJose.Borrego@Sun.COM * Generate a 64-bit session key from the client and server challenges. 3567619SJose.Borrego@Sun.COM * See "Session-Key Computation" section of MS-NRPC document. 3577619SJose.Borrego@Sun.COM * 3587619SJose.Borrego@Sun.COM * The algorithm is a two stage hash. For the first hash, the input is 3597619SJose.Borrego@Sun.COM * the combination of the client and server challenges, the key is 3607619SJose.Borrego@Sun.COM * the first 7 bytes of the password. The initial password is formed 3617619SJose.Borrego@Sun.COM * using the NT password hash on the local hostname in lower case. 3627619SJose.Borrego@Sun.COM * The result is stored in a temporary buffer. 3637619SJose.Borrego@Sun.COM * 3647619SJose.Borrego@Sun.COM * input: challenge 3657619SJose.Borrego@Sun.COM * key: passwd lower 7 bytes 3667619SJose.Borrego@Sun.COM * output: intermediate result 3677619SJose.Borrego@Sun.COM * 3687619SJose.Borrego@Sun.COM * For the second hash, the input is the result of the first hash and 3697619SJose.Borrego@Sun.COM * the key is the last 7 bytes of the password. 3707619SJose.Borrego@Sun.COM * 3717619SJose.Borrego@Sun.COM * input: result of first hash 3727619SJose.Borrego@Sun.COM * key: passwd upper 7 bytes 3737619SJose.Borrego@Sun.COM * output: session_key 3747619SJose.Borrego@Sun.COM * 3757619SJose.Borrego@Sun.COM * The final output should be the session key. 3767619SJose.Borrego@Sun.COM * 3777619SJose.Borrego@Sun.COM * FYI: smb_auth_DES(output, key, input) 3787619SJose.Borrego@Sun.COM * 3797619SJose.Borrego@Sun.COM * If any difficulties occur using the cryptographic framework, the 3807619SJose.Borrego@Sun.COM * function returns SMBAUTH_FAILURE. Otherwise SMBAUTH_SUCCESS is 3817619SJose.Borrego@Sun.COM * returned. 3827619SJose.Borrego@Sun.COM */ 3835331Samw int 3847619SJose.Borrego@Sun.COM netr_gen_skey64(netr_info_t *netr_info) 3855331Samw { 3865331Samw unsigned char md4hash[32]; 3875331Samw unsigned char buffer[8]; 3885331Samw DWORD data[2]; 3895331Samw DWORD *client_challenge; 3905331Samw DWORD *server_challenge; 3915331Samw int rc; 3925521Sas200622 DWORD le_data[2]; 3935331Samw 3945331Samw client_challenge = (DWORD *)(uintptr_t)&netr_info->client_challenge; 3955331Samw server_challenge = (DWORD *)(uintptr_t)&netr_info->server_challenge; 3965331Samw bzero(md4hash, 32); 3975331Samw 3985331Samw /* 3995331Samw * We should check (netr_info->flags & NETR_FLG_INIT) and use 4005331Samw * the appropriate password but it isn't working yet. So we 4015331Samw * always use the default one for now. 4025331Samw */ 4035772Sas200622 bzero(netr_info->password, sizeof (netr_info->password)); 4045772Sas200622 rc = smb_config_getstr(SMB_CI_MACHINE_PASSWD, 4055772Sas200622 (char *)netr_info->password, sizeof (netr_info->password)); 4065331Samw 4075772Sas200622 if ((rc != SMBD_SMF_OK) || *netr_info->password == '\0') { 4087052Samw return (SMBAUTH_FAILURE); 4095331Samw } 4105331Samw 4115772Sas200622 rc = smb_auth_ntlm_hash((char *)netr_info->password, md4hash); 4125331Samw 4135331Samw if (rc != SMBAUTH_SUCCESS) 4145331Samw return (SMBAUTH_FAILURE); 4155331Samw 4165331Samw data[0] = LE_IN32(&client_challenge[0]) + LE_IN32(&server_challenge[0]); 4175331Samw data[1] = LE_IN32(&client_challenge[1]) + LE_IN32(&server_challenge[1]); 4185521Sas200622 LE_OUT32(&le_data[0], data[0]); 4195521Sas200622 LE_OUT32(&le_data[1], data[1]); 4207619SJose.Borrego@Sun.COM rc = smb_auth_DES(buffer, 8, md4hash, NETR_DESKEY_LEN, 4217619SJose.Borrego@Sun.COM (unsigned char *)le_data, 8); 4225331Samw 4235331Samw if (rc != SMBAUTH_SUCCESS) 4245331Samw return (rc); 4255331Samw 4267619SJose.Borrego@Sun.COM netr_info->session_key.len = NETR_SESSKEY64_SZ; 4277619SJose.Borrego@Sun.COM rc = smb_auth_DES(netr_info->session_key.key, 4287619SJose.Borrego@Sun.COM netr_info->session_key.len, &md4hash[9], NETR_DESKEY_LEN, buffer, 4297619SJose.Borrego@Sun.COM 8); 4307619SJose.Borrego@Sun.COM 4315331Samw return (rc); 4325331Samw } 4335331Samw 4345331Samw /* 4355331Samw * netr_gen_credentials 4365331Samw * 4375331Samw * Generate a set of credentials from a challenge and a session key. 4385331Samw * The algorithm is a two stage hash. For the first hash, the 4395331Samw * timestamp is added to the challenge and the result is stored in a 4405331Samw * temporary buffer: 4415331Samw * 4425331Samw * input: challenge (including timestamp) 4435331Samw * key: session_key 4445331Samw * output: intermediate result 4455331Samw * 4465331Samw * For the second hash, the input is the result of the first hash and 4475331Samw * a strange partial key is used: 4485331Samw * 4495331Samw * input: result of first hash 4505331Samw * key: funny partial key 4515331Samw * output: credentiails 4525331Samw * 4535331Samw * The final output should be an encrypted set of credentials. 4545331Samw * 4555331Samw * FYI: smb_auth_DES(output, key, input) 4565331Samw * 4575331Samw * If any difficulties occur using the cryptographic framework, the 4585331Samw * function returns SMBAUTH_FAILURE. Otherwise SMBAUTH_SUCCESS is 4595331Samw * returned. 4605331Samw */ 4615331Samw int 4625331Samw netr_gen_credentials(BYTE *session_key, netr_cred_t *challenge, 4635331Samw DWORD timestamp, netr_cred_t *out_cred) 4645331Samw { 4655331Samw unsigned char buffer[8]; 4665331Samw DWORD data[2]; 4675521Sas200622 DWORD le_data[2]; 4685331Samw DWORD *p; 4695331Samw int rc; 4705331Samw 4715331Samw p = (DWORD *)(uintptr_t)challenge; 4725521Sas200622 data[0] = LE_IN32(&p[0]) + timestamp; 4735521Sas200622 data[1] = LE_IN32(&p[1]); 4745521Sas200622 4755521Sas200622 LE_OUT32(&le_data[0], data[0]); 4765521Sas200622 LE_OUT32(&le_data[1], data[1]); 4775331Samw 4787052Samw if (smb_auth_DES(buffer, 8, session_key, NETR_DESKEY_LEN, 4795521Sas200622 (unsigned char *)le_data, 8) != SMBAUTH_SUCCESS) 4805331Samw return (SMBAUTH_FAILURE); 4815331Samw 4827052Samw rc = smb_auth_DES(out_cred->data, 8, &session_key[NETR_DESKEY_LEN], 4837052Samw NETR_DESKEY_LEN, buffer, 8); 4845331Samw 4855331Samw return (rc); 4865331Samw } 4875331Samw 4885331Samw /* 4895331Samw * netr_server_password_set 4905331Samw * 4915331Samw * Attempt to change the trust account password for this system. 4925331Samw * 4935331Samw * Note that this call may legitimately fail if the registry on the 4945331Samw * domain controller has been setup to deny attempts to change the 4955331Samw * trust account password. In this case we should just continue to 4965331Samw * use the original password. 4975331Samw * 4985331Samw * Possible status values: 4995331Samw * NT_STATUS_ACCESS_DENIED 5005331Samw */ 5015331Samw int 5025331Samw netr_server_password_set(mlsvc_handle_t *netr_handle, netr_info_t *netr_info) 5035331Samw { 5045331Samw struct netr_PasswordSet arg; 5055331Samw mlrpc_heapref_t heap; 5065331Samw int opnum; 5075331Samw int rc; 5085331Samw BYTE new_password[NETR_OWF_PASSWORD_SZ]; 509*7961SNatalie.Li@Sun.COM char account_name[NETBIOS_NAME_SZ * 2]; 5105331Samw 5115331Samw bzero(&arg, sizeof (struct netr_PasswordSet)); 5125331Samw opnum = NETR_OPNUM_ServerPasswordSet; 5135331Samw 5145331Samw (void) snprintf(account_name, sizeof (account_name), "%s$", 5155331Samw netr_info->hostname); 5165331Samw 5175331Samw arg.servername = (unsigned char *)netr_info->server; 5185331Samw arg.account_name = (unsigned char *)account_name; 5195331Samw arg.account_type = NETR_WKSTA_TRUST_ACCOUNT_TYPE; 5205331Samw arg.hostname = (unsigned char *)netr_info->hostname; 5215331Samw 5225331Samw /* 5235331Samw * Set up the client side authenticator. 5245331Samw */ 5255331Samw if (netr_setup_authenticator(netr_info, &arg.auth, 0) != 5265331Samw SMBAUTH_SUCCESS) { 5275331Samw return (-1); 5285331Samw } 5295331Samw 5305331Samw /* 5315331Samw * Generate a new password from the old password. 5325331Samw */ 5337619SJose.Borrego@Sun.COM if (netr_gen_password(netr_info->session_key.key, 5345331Samw netr_info->password, new_password) == SMBAUTH_FAILURE) { 5355331Samw return (-1); 5365331Samw } 5375331Samw 5385331Samw (void) memcpy(&arg.uas_new_password, &new_password, 5395331Samw NETR_OWF_PASSWORD_SZ); 5405331Samw 5415331Samw (void) mlsvc_rpc_init(&heap); 5425331Samw rc = mlsvc_rpc_call(netr_handle->context, opnum, &arg, &heap); 5435331Samw if ((rc != 0) || (arg.status != 0)) { 5445331Samw mlsvc_rpc_report_status(opnum, arg.status); 5455331Samw mlsvc_rpc_free(netr_handle->context, &heap); 5465331Samw return (-1); 5475331Samw } 5485331Samw 5495331Samw /* 5505331Samw * Check the returned credentials. The server returns the new 5515331Samw * client credential rather than the new server credentiali, 5525331Samw * as documented elsewhere. 5535331Samw * 5545331Samw * Generate the new seed for the credential chain. Increment 5555331Samw * the timestamp and add it to the client challenge. Then we 5565331Samw * need to copy the challenge to the credential field in 5575331Samw * preparation for the next cycle. 5585331Samw */ 5595331Samw if (netr_validate_chain(netr_info, &arg.auth) == 0) { 5605331Samw /* 5615331Samw * Save the new password. 5625331Samw */ 5635331Samw (void) memcpy(netr_info->password, new_password, 5645331Samw NETR_OWF_PASSWORD_SZ); 5655331Samw } 5665331Samw 5675331Samw mlsvc_rpc_free(netr_handle->context, &heap); 5685331Samw return (0); 5695331Samw } 5705331Samw 5715331Samw /* 5725331Samw * netr_gen_password 5735331Samw * 5745331Samw * Generate a new pasword from the old password and the session key. 5755331Samw * The algorithm is a two stage hash. The session key is used in the 5765331Samw * first hash but only part of the session key is used in the second 5775331Samw * hash. 5785331Samw * 5795331Samw * If any difficulties occur using the cryptographic framework, the 5805331Samw * function returns SMBAUTH_FAILURE. Otherwise SMBAUTH_SUCCESS is 5815331Samw * returned. 5825331Samw */ 5835331Samw static int 5845331Samw netr_gen_password(BYTE *session_key, BYTE *old_password, BYTE *new_password) 5855331Samw { 5865331Samw int rv; 5875331Samw 5887052Samw rv = smb_auth_DES(new_password, 8, session_key, NETR_DESKEY_LEN, 5897052Samw old_password, 8); 5905331Samw if (rv != SMBAUTH_SUCCESS) 5915331Samw return (rv); 5925331Samw 5937052Samw rv = smb_auth_DES(&new_password[8], 8, &session_key[NETR_DESKEY_LEN], 5947052Samw NETR_DESKEY_LEN, &old_password[8], 8); 5955331Samw return (rv); 5965331Samw } 597