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/>.
22 #include "libnet/libnet.h"
24 #if defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC)
26 /****************************************************************
27 ****************************************************************/
29 static NTSTATUS keytab_ad_connect(TALLOC_CTX *mem_ctx,
30 const char *domain_name,
33 struct libnet_keytab_context *ctx)
38 struct netr_DsRGetDCNameInfo *info = NULL;
41 status = dsgetdcname(mem_ctx, NULL, domain_name, NULL, NULL, 0, &info);
42 if (!NT_STATUS_IS_OK(status)) {
46 dc = strip_hostname(info->dc_unc);
48 ads = ads_init(NULL, domain_name, dc);
49 NT_STATUS_HAVE_NO_MEMORY(ads);
51 if (getenv(KRB5_ENV_CCNAME) == NULL) {
52 setenv(KRB5_ENV_CCNAME, "MEMORY:libnet_samsync_keytab", 1);
55 ads->auth.user_name = SMB_STRDUP(username);
56 ads->auth.password = SMB_STRDUP(password);
58 ad_status = ads_connect_user_creds(ads);
59 if (!ADS_ERR_OK(ad_status)) {
60 return NT_STATUS_UNSUCCESSFUL;
65 ctx->dns_domain_name = talloc_strdup_upper(mem_ctx, ads->config.realm);
66 NT_STATUS_HAVE_NO_MEMORY(ctx->dns_domain_name);
71 /****************************************************************
72 ****************************************************************/
74 static NTSTATUS fetch_sam_entry_keytab(TALLOC_CTX *mem_ctx,
75 enum netr_SamDatabaseID database_id,
77 struct netr_DELTA_USER *r,
78 struct libnet_keytab_context *ctx)
84 if (memcmp(r->ntpassword.hash, ctx->zero_buf, 16) == 0) {
88 kvno = ads_get_kvno(ctx->ads, r->account_name.string);
89 blob = data_blob_const(r->ntpassword.hash, 16);
91 status = libnet_keytab_add_to_keytab_entries(mem_ctx, ctx,
93 r->account_name.string,
97 if (!NT_STATUS_IS_OK(status)) {
104 /****************************************************************
105 ****************************************************************/
107 static NTSTATUS init_keytab(TALLOC_CTX *mem_ctx,
108 struct samsync_context *ctx,
109 enum netr_SamDatabaseID database_id,
110 uint64_t *sequence_num)
112 krb5_error_code ret = 0;
114 struct libnet_keytab_context *keytab_ctx;
116 ret = libnet_keytab_init(mem_ctx, ctx->output_filename, &keytab_ctx);
118 return krb5_to_nt_status(ret);
121 keytab_ctx->clean_old_entries = ctx->clean_old_entries;
122 ctx->private_data = keytab_ctx;
124 status = keytab_ad_connect(mem_ctx,
129 if (!NT_STATUS_IS_OK(status)) {
130 TALLOC_FREE(keytab_ctx);
137 /****************************************************************
138 ****************************************************************/
140 static NTSTATUS fetch_sam_entries_keytab(TALLOC_CTX *mem_ctx,
141 enum netr_SamDatabaseID database_id,
142 struct netr_DELTA_ENUM_ARRAY *r,
143 struct samsync_context *ctx)
145 struct libnet_keytab_context *keytab_ctx =
146 (struct libnet_keytab_context *)ctx->private_data;
148 NTSTATUS status = NT_STATUS_OK;
151 for (i = 0; i < r->num_deltas; i++) {
153 if (r->delta_enum[i].delta_type != NETR_DELTA_USER) {
157 status = fetch_sam_entry_keytab(mem_ctx, database_id,
158 r->delta_enum[i].delta_id_union.rid,
159 r->delta_enum[i].delta_union.user,
161 if (!NT_STATUS_IS_OK(status)) {
169 /****************************************************************
170 ****************************************************************/
172 static NTSTATUS close_keytab(TALLOC_CTX *mem_ctx,
173 struct samsync_context *ctx,
174 enum netr_SamDatabaseID database_id,
175 uint64_t sequence_num)
177 struct libnet_keytab_context *keytab_ctx =
178 (struct libnet_keytab_context *)ctx->private_data;
182 ret = libnet_keytab_add(keytab_ctx);
184 status = krb5_to_nt_status(ret);
185 ctx->error_message = talloc_asprintf(ctx,
186 "Failed to add entries to keytab %s: %s",
187 keytab_ctx->keytab_name, error_message(ret));
188 TALLOC_FREE(keytab_ctx);
192 ctx->result_message = talloc_asprintf(ctx,
193 "Vampired %d accounts to keytab %s",
195 keytab_ctx->keytab_name);
197 TALLOC_FREE(keytab_ctx);
204 static NTSTATUS init_keytab(TALLOC_CTX *mem_ctx,
205 struct samsync_context *ctx,
206 enum netr_SamDatabaseID database_id,
207 uint64_t *sequence_num)
209 return NT_STATUS_NOT_SUPPORTED;
212 static NTSTATUS fetch_sam_entries_keytab(TALLOC_CTX *mem_ctx,
213 enum netr_SamDatabaseID database_id,
214 struct netr_DELTA_ENUM_ARRAY *r,
215 struct samsync_context *ctx)
217 return NT_STATUS_NOT_SUPPORTED;
220 static NTSTATUS close_keytab(TALLOC_CTX *mem_ctx,
221 struct samsync_context *ctx,
222 enum netr_SamDatabaseID database_id,
223 uint64_t sequence_num)
225 return NT_STATUS_NOT_SUPPORTED;
228 #endif /* defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC) */
230 const struct samsync_ops libnet_samsync_keytab_ops = {
231 .startup = init_keytab,
232 .process_objects = fetch_sam_entries_keytab,
233 .finish = close_keytab