selftest: Add test for samba-tool dsacl get, cross-checked with samba-tool dsacl set
[Samba.git] / libcli / samsync / decrypt.c
blob66cc9158b677d7ce61a98c59fdd380b105ba9075
1 /*
2 Unix SMB/CIFS implementation.
4 Extract the user/system database from a remote SamSync server
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
7 Copyright (C) Guenther Deschner <gd@samba.org> 2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "../lib/crypto/crypto.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../libcli/samsync/samsync.h"
28 #include "librpc/gen_ndr/ndr_netlogon.h"
30 /**
31 * Decrypt and extract the user's passwords.
33 * The writes decrypted (no longer 'RID encrypted' or arcfour encrypted)
34 * passwords back into the structure
37 static NTSTATUS fix_user(TALLOC_CTX *mem_ctx,
38 struct netlogon_creds_CredentialState *creds,
39 enum netr_SamDatabaseID database_id,
40 struct netr_DELTA_ENUM *delta)
43 uint32_t rid = delta->delta_id_union.rid;
44 struct netr_DELTA_USER *user = delta->delta_union.user;
45 struct samr_Password lm_hash;
46 struct samr_Password nt_hash;
48 /* Note that win2000 may send us all zeros
49 * for the hashes if it doesn't
50 * think this channel is secure enough. */
51 if (user->lm_password_present) {
52 if (!all_zero(user->lmpassword.hash, 16)) {
53 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
54 } else {
55 memset(lm_hash.hash, '\0', sizeof(lm_hash.hash));
57 user->lmpassword = lm_hash;
60 if (user->nt_password_present) {
61 if (!all_zero(user->ntpassword.hash, 16)) {
62 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
63 } else {
64 memset(nt_hash.hash, '\0', sizeof(nt_hash.hash));
66 user->ntpassword = nt_hash;
69 if (user->user_private_info.SensitiveData) {
70 DATA_BLOB data;
71 struct netr_USER_KEYS keys;
72 enum ndr_err_code ndr_err;
73 data.data = user->user_private_info.SensitiveData;
74 data.length = user->user_private_info.DataLength;
75 netlogon_creds_arcfour_crypt(creds, data.data, data.length);
76 user->user_private_info.SensitiveData = data.data;
77 user->user_private_info.DataLength = data.length;
79 ndr_err = ndr_pull_struct_blob(&data, mem_ctx, &keys,
80 (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
81 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
82 dump_data(10, data.data, data.length);
83 return ndr_map_error2ntstatus(ndr_err);
86 /* Note that win2000 may send us all zeros
87 * for the hashes if it doesn't
88 * think this channel is secure enough. */
89 if (keys.keys.keys2.lmpassword.length == 16) {
90 if (!all_zero(keys.keys.keys2.lmpassword.pwd.hash,
91 16)) {
92 sam_rid_crypt(rid,
93 keys.keys.keys2.lmpassword.pwd.hash,
94 lm_hash.hash, 0);
95 } else {
96 memset(lm_hash.hash, '\0', sizeof(lm_hash.hash));
98 user->lmpassword = lm_hash;
99 user->lm_password_present = true;
101 if (keys.keys.keys2.ntpassword.length == 16) {
102 if (!all_zero(keys.keys.keys2.ntpassword.pwd.hash,
103 16)) {
104 sam_rid_crypt(rid,
105 keys.keys.keys2.ntpassword.pwd.hash,
106 nt_hash.hash, 0);
107 } else {
108 memset(nt_hash.hash, '\0', sizeof(nt_hash.hash));
110 user->ntpassword = nt_hash;
111 user->nt_password_present = true;
113 /* TODO: rid decrypt history fields */
115 return NT_STATUS_OK;
119 * Decrypt and extract the secrets
121 * The writes decrypted secrets back into the structure
123 static NTSTATUS fix_secret(TALLOC_CTX *mem_ctx,
124 struct netlogon_creds_CredentialState *creds,
125 enum netr_SamDatabaseID database,
126 struct netr_DELTA_ENUM *delta)
128 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
129 netlogon_creds_arcfour_crypt(creds, secret->current_cipher.cipher_data,
130 secret->current_cipher.maxlen);
132 netlogon_creds_arcfour_crypt(creds, secret->old_cipher.cipher_data,
133 secret->old_cipher.maxlen);
135 return NT_STATUS_OK;
139 * Fix up the delta, dealing with encryption issues so that the final
140 * callback need only do the printing or application logic
143 NTSTATUS samsync_fix_delta(TALLOC_CTX *mem_ctx,
144 struct netlogon_creds_CredentialState *creds,
145 enum netr_SamDatabaseID database_id,
146 struct netr_DELTA_ENUM *delta)
148 NTSTATUS status = NT_STATUS_OK;
150 switch (delta->delta_type) {
151 case NETR_DELTA_USER:
153 status = fix_user(mem_ctx,
154 creds,
155 database_id,
156 delta);
157 break;
158 case NETR_DELTA_SECRET:
160 status = fix_secret(mem_ctx,
161 creds,
162 database_id,
163 delta);
164 break;
165 default:
166 break;
169 return status;