2 Unix SMB/CIFS implementation.
3 dump the remote SAM using rpc samsync operations
5 Copyright (C) Guenther Deschner 2008.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libnet/libnet_keytab.h"
25 #include "libnet/libnet_samsync.h"
30 /****************************************************************
31 ****************************************************************/
33 static NTSTATUS
keytab_ad_connect(TALLOC_CTX
*mem_ctx
,
34 const char *domain_name
,
38 struct libnet_keytab_context
*ctx
)
43 ads
= ads_init(NULL
, domain_name
, dc
);
44 NT_STATUS_HAVE_NO_MEMORY(ads
);
46 if (getenv(KRB5_ENV_CCNAME
) == NULL
) {
47 setenv(KRB5_ENV_CCNAME
, "MEMORY:libnet_samsync_keytab", 1);
50 ads
->auth
.user_name
= SMB_STRDUP(username
);
51 ads
->auth
.password
= SMB_STRDUP(password
);
53 ad_status
= ads_connect_user_creds(ads
);
54 if (!ADS_ERR_OK(ad_status
)) {
55 return NT_STATUS_UNSUCCESSFUL
;
60 ctx
->dns_domain_name
= talloc_strdup_upper(mem_ctx
, ads
->config
.realm
);
61 NT_STATUS_HAVE_NO_MEMORY(ctx
->dns_domain_name
);
66 /****************************************************************
67 ****************************************************************/
69 static NTSTATUS
fetch_sam_entry_keytab(TALLOC_CTX
*mem_ctx
,
70 enum netr_SamDatabaseID database_id
,
72 struct netr_DELTA_USER
*r
,
73 struct libnet_keytab_context
*ctx
)
79 if (memcmp(r
->ntpassword
.hash
, ctx
->zero_buf
, 16) == 0) {
83 kvno
= ads_get_kvno(ctx
->ads
, r
->account_name
.string
);
84 blob
= data_blob_const(r
->ntpassword
.hash
, 16);
86 status
= libnet_keytab_add_to_keytab_entries(mem_ctx
, ctx
,
88 r
->account_name
.string
,
92 if (!NT_STATUS_IS_OK(status
)) {
99 /****************************************************************
100 ****************************************************************/
102 static NTSTATUS
init_keytab(TALLOC_CTX
*mem_ctx
,
103 struct samsync_context
*ctx
,
104 enum netr_SamDatabaseID database_id
,
105 uint64_t *sequence_num
)
107 krb5_error_code ret
= 0;
109 struct libnet_keytab_context
*keytab_ctx
= NULL
;
110 struct libnet_keytab_entry
*entry
;
111 uint64_t old_sequence_num
= 0;
112 const char *principal
= NULL
;
113 struct netr_DsRGetDCNameInfo
*info
= NULL
;
116 ret
= libnet_keytab_init(mem_ctx
, ctx
->output_filename
, &keytab_ctx
);
118 return krb5_to_nt_status(ret
);
121 status
= dsgetdcname(mem_ctx
, ctx
->msg_ctx
,
122 ctx
->domain_name
, NULL
, NULL
, 0, &info
);
123 if (!NT_STATUS_IS_OK(status
)) {
127 dc
= strip_hostname(info
->dc_unc
);
129 keytab_ctx
->clean_old_entries
= ctx
->clean_old_entries
;
130 ctx
->private_data
= keytab_ctx
;
132 status
= keytab_ad_connect(mem_ctx
,
138 if (!NT_STATUS_IS_OK(status
)) {
139 TALLOC_FREE(keytab_ctx
);
143 principal
= talloc_asprintf(mem_ctx
, "SEQUENCE_NUM@%s",
144 keytab_ctx
->dns_domain_name
);
145 NT_STATUS_HAVE_NO_MEMORY(principal
);
147 entry
= libnet_keytab_search(keytab_ctx
, principal
, 0, ENCTYPE_NULL
,
149 if (entry
&& (entry
->password
.length
== 8)) {
150 old_sequence_num
= BVAL(entry
->password
.data
, 0);
154 *sequence_num
= old_sequence_num
;
160 /****************************************************************
161 ****************************************************************/
163 static NTSTATUS
fetch_sam_entries_keytab(TALLOC_CTX
*mem_ctx
,
164 enum netr_SamDatabaseID database_id
,
165 struct netr_DELTA_ENUM_ARRAY
*r
,
166 uint64_t *sequence_num
,
167 struct samsync_context
*ctx
)
169 struct libnet_keytab_context
*keytab_ctx
=
170 (struct libnet_keytab_context
*)ctx
->private_data
;
172 NTSTATUS status
= NT_STATUS_OK
;
175 for (i
= 0; i
< r
->num_deltas
; i
++) {
177 switch (r
->delta_enum
[i
].delta_type
) {
178 case NETR_DELTA_USER
:
180 case NETR_DELTA_DOMAIN
:
183 r
->delta_enum
[i
].delta_union
.domain
->sequence_num
;
186 case NETR_DELTA_MODIFY_COUNT
:
189 *r
->delta_enum
[i
].delta_union
.modified_count
;
196 status
= fetch_sam_entry_keytab(mem_ctx
, database_id
,
197 r
->delta_enum
[i
].delta_id_union
.rid
,
198 r
->delta_enum
[i
].delta_union
.user
,
200 if (!NT_STATUS_IS_OK(status
)) {
208 /****************************************************************
209 ****************************************************************/
211 static NTSTATUS
close_keytab(TALLOC_CTX
*mem_ctx
,
212 struct samsync_context
*ctx
,
213 enum netr_SamDatabaseID database_id
,
214 uint64_t sequence_num
)
216 struct libnet_keytab_context
*keytab_ctx
=
217 (struct libnet_keytab_context
*)ctx
->private_data
;
220 struct libnet_keytab_entry
*entry
;
221 uint64_t old_sequence_num
= 0;
222 const char *principal
= NULL
;
224 principal
= talloc_asprintf(mem_ctx
, "SEQUENCE_NUM@%s",
225 keytab_ctx
->dns_domain_name
);
226 NT_STATUS_HAVE_NO_MEMORY(principal
);
229 entry
= libnet_keytab_search(keytab_ctx
, principal
, 0, ENCTYPE_NULL
,
231 if (entry
&& (entry
->password
.length
== 8)) {
232 old_sequence_num
= BVAL(entry
->password
.data
, 0);
236 if (sequence_num
> old_sequence_num
) {
238 blob
= data_blob_talloc_zero(mem_ctx
, 8);
239 SBVAL(blob
.data
, 0, sequence_num
);
241 status
= libnet_keytab_add_to_keytab_entries(mem_ctx
, keytab_ctx
,
247 if (!NT_STATUS_IS_OK(status
)) {
252 ret
= libnet_keytab_add(keytab_ctx
);
254 status
= krb5_to_nt_status(ret
);
255 ctx
->error_message
= talloc_asprintf(ctx
,
256 "Failed to add entries to keytab %s: %s",
257 keytab_ctx
->keytab_name
, error_message(ret
));
258 TALLOC_FREE(keytab_ctx
);
262 ctx
->result_message
= talloc_asprintf(ctx
,
263 "Vampired %d accounts to keytab %s",
265 keytab_ctx
->keytab_name
);
267 status
= NT_STATUS_OK
;
270 TALLOC_FREE(keytab_ctx
);
277 static NTSTATUS
init_keytab(TALLOC_CTX
*mem_ctx
,
278 struct samsync_context
*ctx
,
279 enum netr_SamDatabaseID database_id
,
280 uint64_t *sequence_num
)
282 return NT_STATUS_NOT_SUPPORTED
;
285 static NTSTATUS
fetch_sam_entries_keytab(TALLOC_CTX
*mem_ctx
,
286 enum netr_SamDatabaseID database_id
,
287 struct netr_DELTA_ENUM_ARRAY
*r
,
288 uint64_t *sequence_num
,
289 struct samsync_context
*ctx
)
291 return NT_STATUS_NOT_SUPPORTED
;
294 static NTSTATUS
close_keytab(TALLOC_CTX
*mem_ctx
,
295 struct samsync_context
*ctx
,
296 enum netr_SamDatabaseID database_id
,
297 uint64_t sequence_num
)
299 return NT_STATUS_NOT_SUPPORTED
;
302 #endif /* defined(HAVE_ADS) */
304 const struct samsync_ops libnet_samsync_keytab_ops
= {
305 .startup
= init_keytab
,
306 .process_objects
= fetch_sam_entries_keytab
,
307 .finish
= close_keytab