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 /* 22*5772Sas200622 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 235331Samw * Use is subject to license terms. 245331Samw */ 255331Samw 265331Samw #pragma ident "%Z%%M% %I% %E% SMI" 275331Samw 285331Samw /* 295331Samw * NETR challenge/response client functions. 305331Samw * 315331Samw * NT_STATUS_INVALID_PARAMETER 325331Samw * NT_STATUS_NO_TRUST_SAM_ACCOUNT 335331Samw * NT_STATUS_ACCESS_DENIED 345331Samw */ 355331Samw 365331Samw #include <stdio.h> 375331Samw #include <stdlib.h> 385331Samw #include <strings.h> 395331Samw #include <unistd.h> 405331Samw #include <ctype.h> 415331Samw 425331Samw #include <smbsrv/libsmb.h> 435521Sas200622 #include <smbsrv/libsmbrdr.h> 445331Samw #include <smbsrv/mlsvc_util.h> 455331Samw #include <smbsrv/ndl/netlogon.ndl> 465331Samw #include <smbsrv/ntstatus.h> 475331Samw #include <smbsrv/smbinfo.h> 485331Samw #include <smbsrv/mlsvc.h> 495331Samw #include <smbsrv/netrauth.h> 505331Samw 515331Samw int netr_setup_authenticator(netr_info_t *, struct netr_authenticator *, 525331Samw struct netr_authenticator *); 535331Samw DWORD netr_validate_chain(netr_info_t *, struct netr_authenticator *); 545331Samw 555331Samw static int netr_server_req_challenge(mlsvc_handle_t *, netr_info_t *); 565331Samw static int netr_server_authenticate2(mlsvc_handle_t *, netr_info_t *); 575331Samw static int netr_gen_password(BYTE *, BYTE *, BYTE *); 585331Samw 595331Samw /* 605331Samw * Shared with netr_logon.c 615331Samw */ 625331Samw netr_info_t netr_global_info; 635331Samw 645331Samw /* 655331Samw * netlogon_auth 665331Samw * 675331Samw * This is the core of the NETLOGON authentication protocol. 685331Samw * Do the challenge response authentication. 695331Samw * 705331Samw * Prior to calling this function, an anonymous session to the NETLOGON 715331Samw * pipe on a domain controller(server) should have already been opened. 725331Samw */ 735331Samw DWORD 745331Samw netlogon_auth(char *server, mlsvc_handle_t *netr_handle, DWORD flags) 755331Samw { 765331Samw netr_info_t *netr_info; 775331Samw int rc; 785521Sas200622 DWORD leout_rc[2]; 795331Samw 805331Samw netr_info = &netr_global_info; 815331Samw bzero(netr_info, sizeof (netr_info_t)); 825331Samw 835331Samw netr_info->flags |= flags; 845331Samw 855331Samw rc = smb_getnetbiosname(netr_info->hostname, MLSVC_DOMAIN_NAME_MAX); 865331Samw if (rc != 0) 875331Samw return (NT_STATUS_UNSUCCESSFUL); 885331Samw 895331Samw (void) snprintf(netr_info->server, sizeof (netr_info->server), 905331Samw "\\\\%s", server); 915331Samw 925521Sas200622 LE_OUT32(&leout_rc[0], random()); 935521Sas200622 LE_OUT32(&leout_rc[1], random()); 945521Sas200622 (void) memcpy(&netr_info->client_challenge, leout_rc, 955331Samw sizeof (struct netr_credential)); 965331Samw 975331Samw if ((rc = netr_server_req_challenge(netr_handle, netr_info)) == 0) { 985331Samw rc = netr_server_authenticate2(netr_handle, netr_info); 995331Samw if (rc == 0) 1005331Samw netr_info->flags |= NETR_FLG_VALID; 1015331Samw } 1025331Samw 1035331Samw return ((rc) ? NT_STATUS_UNSUCCESSFUL : NT_STATUS_SUCCESS); 1045331Samw } 1055331Samw 1065331Samw /* 1075331Samw * netr_open 1085331Samw * 1095331Samw * Open an anonymous session to the NETLOGON pipe on a domain 1105331Samw * controller and bind to the NETR RPC interface. We store the 1115331Samw * remote server's native OS type - we may need it due to 1125331Samw * differences between versions of Windows. 1135331Samw */ 1145331Samw int 1155331Samw netr_open(char *server, char *domain, mlsvc_handle_t *netr_handle) 1165331Samw { 1175331Samw int fid; 1185331Samw int remote_os = 0; 1195331Samw int remote_lm = 0; 1205331Samw int server_pdc; 1215521Sas200622 char *user = smbrdr_ipc_get_user(); 1225331Samw 1235521Sas200622 if (mlsvc_logon(server, domain, user) != 0) 1245331Samw return (-1); 1255331Samw 1265521Sas200622 fid = mlsvc_open_pipe(server, domain, user, "\\NETLOGON"); 1275331Samw if (fid < 0) 1285331Samw return (-1); 1295331Samw 1305331Samw if (mlsvc_rpc_bind(netr_handle, fid, "NETR") < 0) { 1315331Samw (void) mlsvc_close_pipe(fid); 1325331Samw return (-1); 1335331Samw } 1345331Samw 1355331Samw (void) mlsvc_session_native_values(fid, &remote_os, &remote_lm, 1365331Samw &server_pdc); 1375331Samw netr_handle->context->server_os = remote_os; 1385331Samw netr_handle->context->server_pdc = server_pdc; 1395331Samw return (0); 1405331Samw } 1415331Samw 1425331Samw /* 1435331Samw * netr_close 1445331Samw * 1455331Samw * Close a NETLOGON pipe and free the RPC context. 1465331Samw */ 1475331Samw int 1485331Samw netr_close(mlsvc_handle_t *netr_handle) 1495331Samw { 1505331Samw (void) mlsvc_close_pipe(netr_handle->context->fid); 1515331Samw free(netr_handle->context); 1525331Samw return (0); 1535331Samw } 1545331Samw 1555331Samw /* 1565331Samw * netr_server_req_challenge 1575331Samw */ 1585331Samw static int 1595331Samw netr_server_req_challenge(mlsvc_handle_t *netr_handle, netr_info_t *netr_info) 1605331Samw { 1615331Samw struct netr_ServerReqChallenge arg; 1625331Samw mlrpc_heapref_t heap; 1635331Samw int opnum; 1645331Samw int rc; 1655331Samw 1665331Samw bzero(&arg, sizeof (struct netr_ServerReqChallenge)); 1675331Samw opnum = NETR_OPNUM_ServerReqChallenge; 1685331Samw 1695331Samw arg.servername = (unsigned char *)netr_info->server; 1705331Samw arg.hostname = (unsigned char *)netr_info->hostname; 1715331Samw 1725331Samw (void) memcpy(&arg.client_challenge, &netr_info->client_challenge, 1735331Samw sizeof (struct netr_credential)); 1745331Samw 1755331Samw (void) mlsvc_rpc_init(&heap); 1765331Samw rc = mlsvc_rpc_call(netr_handle->context, opnum, &arg, &heap); 1775331Samw if (rc == 0) { 1785331Samw if (arg.status != 0) { 1795331Samw mlsvc_rpc_report_status(opnum, arg.status); 1805331Samw rc = -1; 1815331Samw } else { 1825331Samw (void) memcpy(&netr_info->server_challenge, 1835331Samw &arg.server_challenge, 1845331Samw sizeof (struct netr_credential)); 1855331Samw } 1865331Samw } 1875331Samw 1885331Samw mlsvc_rpc_free(netr_handle->context, &heap); 1895331Samw return (rc); 1905331Samw } 1915331Samw 1925331Samw /* 1935331Samw * netr_server_authenticate2 1945331Samw */ 1955331Samw static int 1965331Samw netr_server_authenticate2(mlsvc_handle_t *netr_handle, netr_info_t *netr_info) 1975331Samw { 1985331Samw struct netr_ServerAuthenticate2 arg; 1995331Samw mlrpc_heapref_t heap; 2005331Samw int opnum; 2015331Samw int rc; 2025331Samw char account_name[MLSVC_DOMAIN_NAME_MAX * 2]; 2035331Samw 2045331Samw bzero(&arg, sizeof (struct netr_ServerAuthenticate2)); 2055331Samw opnum = NETR_OPNUM_ServerAuthenticate2; 2065331Samw 2075331Samw (void) snprintf(account_name, sizeof (account_name), "%s$", 2085331Samw netr_info->hostname); 2095331Samw 2105331Samw arg.servername = (unsigned char *)netr_info->server; 2115331Samw arg.account_name = (unsigned char *)account_name; 2125331Samw arg.account_type = NETR_WKSTA_TRUST_ACCOUNT_TYPE; 2135331Samw arg.hostname = (unsigned char *)netr_info->hostname; 2145331Samw arg.negotiate_flags = NETR_NEGOTIATE_FLAGS; 2155331Samw 2165331Samw smb_tracef("server=[%s] account_name=[%s] hostname=[%s]\n", 2175331Samw netr_info->server, account_name, netr_info->hostname); 2185331Samw 2195331Samw if (netr_gen_session_key(netr_info) != SMBAUTH_SUCCESS) 2205331Samw return (-1); 2215331Samw 2225331Samw if (netr_gen_credentials(netr_info->session_key, 2235331Samw &netr_info->client_challenge, 2245331Samw 0, 2255331Samw &netr_info->client_credential) != SMBAUTH_SUCCESS) { 2265331Samw return (-1); 2275331Samw } 2285331Samw 2295331Samw if (netr_gen_credentials(netr_info->session_key, 2305331Samw &netr_info->server_challenge, 2315331Samw 0, 2325331Samw &netr_info->server_credential) != SMBAUTH_SUCCESS) { 2335331Samw return (-1); 2345331Samw } 2355331Samw 2365331Samw (void) memcpy(&arg.client_credential, &netr_info->client_credential, 2375331Samw sizeof (struct netr_credential)); 2385331Samw 2395331Samw (void) mlsvc_rpc_init(&heap); 2405331Samw 2415331Samw rc = mlsvc_rpc_call(netr_handle->context, opnum, &arg, &heap); 2425331Samw if (rc == 0) { 2435331Samw if (arg.status != 0) { 2445331Samw mlsvc_rpc_report_status(opnum, arg.status); 2455331Samw rc = -1; 2465331Samw } else { 2475331Samw rc = memcmp(&netr_info->server_credential, 2485331Samw &arg.server_credential, 2495331Samw sizeof (struct netr_credential)); 2505331Samw } 2515331Samw } 2525331Samw 2535331Samw mlsvc_rpc_free(netr_handle->context, &heap); 2545331Samw return (rc); 2555331Samw } 2565331Samw 2575331Samw /* 2585331Samw * netr_gen_session_key 2595331Samw * 2605331Samw * Generate a session key from the client and server challenges. The 2615331Samw * algorithm is a two stage hash. For the first hash, the input is 2625331Samw * the combination of the client and server challenges, the key is 2635331Samw * the first 8 bytes of the password. The initial password is formed 2645331Samw * using the NT password hash on the local hostname in lower case. 2655331Samw * The result is stored in a temporary buffer. 2665331Samw * 2675331Samw * input: challenge 2685331Samw * key: passwd lower 8 bytes 2695331Samw * output: intermediate result 2705331Samw * 2715331Samw * For the second hash, the input is the result of the first hash and 2725331Samw * the key is the last 8 bytes of the password. 2735331Samw * 2745331Samw * input: result of first hash 2755331Samw * key: passwd upper 8 bytes 2765331Samw * output: session_key 2775331Samw * 2785331Samw * The final output should be the session key. 2795331Samw * 2805331Samw * FYI: smb_auth_DES(output, key, input) 2815331Samw * 2825331Samw * If any difficulties occur using the cryptographic framework, the 2835331Samw * function returns SMBAUTH_FAILURE. Otherwise SMBAUTH_SUCCESS is 2845331Samw * returned. 2855331Samw */ 2865331Samw int 2875331Samw netr_gen_session_key(netr_info_t *netr_info) 2885331Samw { 2895331Samw unsigned char md4hash[32]; 2905331Samw unsigned char buffer[8]; 2915331Samw DWORD data[2]; 2925331Samw DWORD *client_challenge; 2935331Samw DWORD *server_challenge; 2945331Samw int rc; 2955521Sas200622 DWORD le_data[2]; 2965331Samw 2975331Samw client_challenge = (DWORD *)(uintptr_t)&netr_info->client_challenge; 2985331Samw server_challenge = (DWORD *)(uintptr_t)&netr_info->server_challenge; 2995331Samw bzero(md4hash, 32); 3005331Samw 3015331Samw /* 3025331Samw * We should check (netr_info->flags & NETR_FLG_INIT) and use 3035331Samw * the appropriate password but it isn't working yet. So we 3045331Samw * always use the default one for now. 3055331Samw */ 306*5772Sas200622 bzero(netr_info->password, sizeof (netr_info->password)); 307*5772Sas200622 rc = smb_config_getstr(SMB_CI_MACHINE_PASSWD, 308*5772Sas200622 (char *)netr_info->password, sizeof (netr_info->password)); 3095331Samw 310*5772Sas200622 if ((rc != SMBD_SMF_OK) || *netr_info->password == '\0') { 3115331Samw return (-1); 3125331Samw } 3135331Samw 314*5772Sas200622 rc = smb_auth_ntlm_hash((char *)netr_info->password, md4hash); 3155331Samw 3165331Samw if (rc != SMBAUTH_SUCCESS) 3175331Samw return (SMBAUTH_FAILURE); 3185331Samw 3195331Samw data[0] = LE_IN32(&client_challenge[0]) + LE_IN32(&server_challenge[0]); 3205331Samw data[1] = LE_IN32(&client_challenge[1]) + LE_IN32(&server_challenge[1]); 3215521Sas200622 LE_OUT32(&le_data[0], data[0]); 3225521Sas200622 LE_OUT32(&le_data[1], data[1]); 3235331Samw 3245521Sas200622 rc = smb_auth_DES(buffer, 8, md4hash, 8, (unsigned char *)le_data, 8); 3255331Samw if (rc != SMBAUTH_SUCCESS) 3265331Samw return (rc); 3275331Samw 3285331Samw rc = smb_auth_DES(netr_info->session_key, 8, &md4hash[9], 8, buffer, 8); 3295331Samw return (rc); 3305331Samw } 3315331Samw 3325331Samw /* 3335331Samw * netr_gen_credentials 3345331Samw * 3355331Samw * Generate a set of credentials from a challenge and a session key. 3365331Samw * The algorithm is a two stage hash. For the first hash, the 3375331Samw * timestamp is added to the challenge and the result is stored in a 3385331Samw * temporary buffer: 3395331Samw * 3405331Samw * input: challenge (including timestamp) 3415331Samw * key: session_key 3425331Samw * output: intermediate result 3435331Samw * 3445331Samw * For the second hash, the input is the result of the first hash and 3455331Samw * a strange partial key is used: 3465331Samw * 3475331Samw * input: result of first hash 3485331Samw * key: funny partial key 3495331Samw * output: credentiails 3505331Samw * 3515331Samw * The final output should be an encrypted set of credentials. 3525331Samw * 3535331Samw * FYI: smb_auth_DES(output, key, input) 3545331Samw * 3555331Samw * If any difficulties occur using the cryptographic framework, the 3565331Samw * function returns SMBAUTH_FAILURE. Otherwise SMBAUTH_SUCCESS is 3575331Samw * returned. 3585331Samw */ 3595331Samw int 3605331Samw netr_gen_credentials(BYTE *session_key, netr_cred_t *challenge, 3615331Samw DWORD timestamp, netr_cred_t *out_cred) 3625331Samw { 3635331Samw unsigned char buffer[8]; 3645331Samw unsigned char partial_key[8]; 3655331Samw DWORD data[2]; 3665521Sas200622 DWORD le_data[2]; 3675331Samw DWORD *p; 3685331Samw int rc; 3695331Samw 3705331Samw p = (DWORD *)(uintptr_t)challenge; 3715521Sas200622 data[0] = LE_IN32(&p[0]) + timestamp; 3725521Sas200622 data[1] = LE_IN32(&p[1]); 3735521Sas200622 3745521Sas200622 LE_OUT32(&le_data[0], data[0]); 3755521Sas200622 LE_OUT32(&le_data[1], data[1]); 3765331Samw 3775331Samw if (smb_auth_DES(buffer, 8, session_key, 8, 3785521Sas200622 (unsigned char *)le_data, 8) != SMBAUTH_SUCCESS) 3795331Samw return (SMBAUTH_FAILURE); 3805331Samw 3815331Samw bzero(partial_key, 8); 3825331Samw partial_key[0] = session_key[7]; 3835331Samw 3845331Samw rc = smb_auth_DES((unsigned char *)out_cred, 8, partial_key, 8, 3855331Samw buffer, 8); 3865331Samw return (rc); 3875331Samw } 3885331Samw 3895331Samw /* 3905331Samw * netr_server_password_set 3915331Samw * 3925331Samw * Attempt to change the trust account password for this system. 3935331Samw * 3945331Samw * Note that this call may legitimately fail if the registry on the 3955331Samw * domain controller has been setup to deny attempts to change the 3965331Samw * trust account password. In this case we should just continue to 3975331Samw * use the original password. 3985331Samw * 3995331Samw * Possible status values: 4005331Samw * NT_STATUS_ACCESS_DENIED 4015331Samw */ 4025331Samw int 4035331Samw netr_server_password_set(mlsvc_handle_t *netr_handle, netr_info_t *netr_info) 4045331Samw { 4055331Samw struct netr_PasswordSet arg; 4065331Samw mlrpc_heapref_t heap; 4075331Samw int opnum; 4085331Samw int rc; 4095331Samw BYTE new_password[NETR_OWF_PASSWORD_SZ]; 4105331Samw char account_name[MLSVC_DOMAIN_NAME_MAX * 2]; 4115331Samw 4125331Samw bzero(&arg, sizeof (struct netr_PasswordSet)); 4135331Samw opnum = NETR_OPNUM_ServerPasswordSet; 4145331Samw 4155331Samw (void) snprintf(account_name, sizeof (account_name), "%s$", 4165331Samw netr_info->hostname); 4175331Samw 4185331Samw arg.servername = (unsigned char *)netr_info->server; 4195331Samw arg.account_name = (unsigned char *)account_name; 4205331Samw arg.account_type = NETR_WKSTA_TRUST_ACCOUNT_TYPE; 4215331Samw arg.hostname = (unsigned char *)netr_info->hostname; 4225331Samw 4235331Samw /* 4245331Samw * Set up the client side authenticator. 4255331Samw */ 4265331Samw if (netr_setup_authenticator(netr_info, &arg.auth, 0) != 4275331Samw SMBAUTH_SUCCESS) { 4285331Samw return (-1); 4295331Samw } 4305331Samw 4315331Samw /* 4325331Samw * Generate a new password from the old password. 4335331Samw */ 4345331Samw if (netr_gen_password(netr_info->session_key, 4355331Samw netr_info->password, new_password) == SMBAUTH_FAILURE) { 4365331Samw return (-1); 4375331Samw } 4385331Samw 4395331Samw (void) memcpy(&arg.uas_new_password, &new_password, 4405331Samw NETR_OWF_PASSWORD_SZ); 4415331Samw 4425331Samw (void) mlsvc_rpc_init(&heap); 4435331Samw rc = mlsvc_rpc_call(netr_handle->context, opnum, &arg, &heap); 4445331Samw if ((rc != 0) || (arg.status != 0)) { 4455331Samw mlsvc_rpc_report_status(opnum, arg.status); 4465331Samw mlsvc_rpc_free(netr_handle->context, &heap); 4475331Samw return (-1); 4485331Samw } 4495331Samw 4505331Samw /* 4515331Samw * Check the returned credentials. The server returns the new 4525331Samw * client credential rather than the new server credentiali, 4535331Samw * as documented elsewhere. 4545331Samw * 4555331Samw * Generate the new seed for the credential chain. Increment 4565331Samw * the timestamp and add it to the client challenge. Then we 4575331Samw * need to copy the challenge to the credential field in 4585331Samw * preparation for the next cycle. 4595331Samw */ 4605331Samw if (netr_validate_chain(netr_info, &arg.auth) == 0) { 4615331Samw /* 4625331Samw * Save the new password. 4635331Samw */ 4645331Samw (void) memcpy(netr_info->password, new_password, 4655331Samw NETR_OWF_PASSWORD_SZ); 4665331Samw } 4675331Samw 4685331Samw mlsvc_rpc_free(netr_handle->context, &heap); 4695331Samw return (0); 4705331Samw } 4715331Samw 4725331Samw /* 4735331Samw * netr_gen_password 4745331Samw * 4755331Samw * Generate a new pasword from the old password and the session key. 4765331Samw * The algorithm is a two stage hash. The session key is used in the 4775331Samw * first hash but only part of the session key is used in the second 4785331Samw * hash. 4795331Samw * 4805331Samw * If any difficulties occur using the cryptographic framework, the 4815331Samw * function returns SMBAUTH_FAILURE. Otherwise SMBAUTH_SUCCESS is 4825331Samw * returned. 4835331Samw */ 4845331Samw static int 4855331Samw netr_gen_password(BYTE *session_key, BYTE *old_password, BYTE *new_password) 4865331Samw { 4875331Samw unsigned char partial_key[8]; 4885331Samw int rv; 4895331Samw 4905331Samw rv = smb_auth_DES(new_password, 8, session_key, 8, old_password, 8); 4915331Samw if (rv != SMBAUTH_SUCCESS) 4925331Samw return (rv); 4935331Samw 4945331Samw bzero(partial_key, 8); 4955331Samw partial_key[0] = session_key[7]; 4965331Samw 4975331Samw rv = smb_auth_DES(&new_password[8], 8, partial_key, 8, 4985331Samw &old_password[8], 8); 4995331Samw return (rv); 5005331Samw } 501