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/>.
27 * Decrypt and extract the user's passwords.
29 * The writes decrypted (no longer 'RID encrypted' or arcfour encrypted)
30 * passwords back into the structure
33 static NTSTATUS
fix_user(TALLOC_CTX
*mem_ctx
,
34 DATA_BLOB
*session_key
,
36 enum netr_SamDatabaseID database_id
,
37 struct netr_DELTA_ENUM
*delta
)
40 uint32_t rid
= delta
->delta_id_union
.rid
;
41 struct netr_DELTA_USER
*user
= delta
->delta_union
.user
;
42 struct samr_Password lm_hash
;
43 struct samr_Password nt_hash
;
44 const char *username
= user
->account_name
.string
;
47 if (user
->lm_password_present
) {
48 sam_pwd_hash(rid
, user
->lmpassword
.hash
, lm_hash
.hash
, 0);
49 user
->lmpassword
= lm_hash
;
52 if (user
->nt_password_present
) {
53 sam_pwd_hash(rid
, user
->ntpassword
.hash
, nt_hash
.hash
, 0);
54 user
->ntpassword
= nt_hash
;
58 if (user
->user_private_info
.SensitiveData
) {
60 struct netr_USER_KEYS keys
;
61 enum ndr_err_code ndr_err
;
62 data
.data
= user
->user_private_info
.SensitiveData
;
63 data
.length
= user
->user_private_info
.DataLength
;
64 SamOEMhashBlob(data
.data
, data
.length
, session_key
);
65 user
->user_private_info
.SensitiveData
= data
.data
;
66 user
->user_private_info
.DataLength
= data
.length
;
68 ndr_err
= ndr_pull_struct_blob(&data
, mem_ctx
, &keys
,
69 (ndr_pull_flags_fn_t
)ndr_pull_netr_USER_KEYS
);
70 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
71 dump_data(10, data
.data
, data
.length
);
72 return ndr_map_error2ntstatus(ndr_err
);
75 if (keys
.keys
.keys2
.lmpassword
.length
== 16) {
78 keys
.keys
.keys2
.lmpassword
.pwd
.hash
,
80 user
->lmpassword
= lm_hash
;
82 user
->lmpassword
= keys
.keys
.keys2
.lmpassword
.pwd
;
84 user
->lm_password_present
= true;
86 if (keys
.keys
.keys2
.ntpassword
.length
== 16) {
89 keys
.keys
.keys2
.ntpassword
.pwd
.hash
,
91 user
->ntpassword
= nt_hash
;
93 user
->ntpassword
= keys
.keys
.keys2
.ntpassword
.pwd
;
95 user
->nt_password_present
= true;
97 /* TODO: rid decrypt history fields */
103 * Decrypt and extract the secrets
105 * The writes decrypted secrets back into the structure
107 static NTSTATUS
fix_secret(TALLOC_CTX
*mem_ctx
,
108 DATA_BLOB
*session_key
,
109 enum netr_SamDatabaseID database_id
,
110 struct netr_DELTA_ENUM
*delta
)
112 struct netr_DELTA_SECRET
*secret
= delta
->delta_union
.secret
;
114 SamOEMhashBlob(secret
->current_cipher
.cipher_data
,
115 secret
->current_cipher
.maxlen
,
118 SamOEMhashBlob(secret
->old_cipher
.cipher_data
,
119 secret
->old_cipher
.maxlen
,
126 * Fix up the delta, dealing with encryption issues so that the final
127 * callback need only do the printing or application logic
130 static NTSTATUS
samsync_fix_delta(TALLOC_CTX
*mem_ctx
,
131 DATA_BLOB
*session_key
,
133 enum netr_SamDatabaseID database_id
,
134 struct netr_DELTA_ENUM
*delta
)
136 NTSTATUS status
= NT_STATUS_OK
;
138 switch (delta
->delta_type
) {
139 case NETR_DELTA_USER
:
141 status
= fix_user(mem_ctx
,
147 case NETR_DELTA_SECRET
:
149 status
= fix_secret(mem_ctx
,
162 * Fix up the delta, dealing with encryption issues so that the final
163 * callback need only do the printing or application logic
166 NTSTATUS
samsync_fix_delta_array(TALLOC_CTX
*mem_ctx
,
167 DATA_BLOB
*session_key
,
169 enum netr_SamDatabaseID database_id
,
170 struct netr_DELTA_ENUM_ARRAY
*r
)
175 for (i
= 0; i
< r
->num_deltas
; i
++) {
177 status
= samsync_fix_delta(mem_ctx
,
182 if (!NT_STATUS_IS_OK(status
)) {