Fix net rpc vampire, based on an *amazing* piece of debugging work by "Cooper S....
[ira/wip.git] / source3 / libnet / libnet_samsync_keytab.c
1 /*
2    Unix SMB/CIFS implementation.
3    dump the remote SAM using rpc samsync operations
4
5    Copyright (C) Guenther Deschner 2008.
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "libnet/libnet.h"
23
24 #if defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC)
25
26 /****************************************************************
27 ****************************************************************/
28
29 static NTSTATUS keytab_ad_connect(TALLOC_CTX *mem_ctx,
30                                   const char *domain_name,
31                                   const char *username,
32                                   const char *password,
33                                   struct libnet_keytab_context *ctx)
34 {
35         NTSTATUS status;
36         ADS_STATUS ad_status;
37         ADS_STRUCT *ads;
38         struct netr_DsRGetDCNameInfo *info = NULL;
39         const char *dc;
40
41         status = dsgetdcname(mem_ctx, NULL, domain_name, NULL, NULL, 0, &info);
42         if (!NT_STATUS_IS_OK(status)) {
43                 return status;
44         }
45
46         dc = strip_hostname(info->dc_unc);
47
48         ads = ads_init(NULL, domain_name, dc);
49         NT_STATUS_HAVE_NO_MEMORY(ads);
50
51         if (getenv(KRB5_ENV_CCNAME) == NULL) {
52                 setenv(KRB5_ENV_CCNAME, "MEMORY:libnet_samsync_keytab", 1);
53         }
54
55         ads->auth.user_name = SMB_STRDUP(username);
56         ads->auth.password = SMB_STRDUP(password);
57
58         ad_status = ads_connect_user_creds(ads);
59         if (!ADS_ERR_OK(ad_status)) {
60                 return NT_STATUS_UNSUCCESSFUL;
61         }
62
63         ctx->ads = ads;
64
65         ctx->dns_domain_name = talloc_strdup_upper(mem_ctx, ads->config.realm);
66         NT_STATUS_HAVE_NO_MEMORY(ctx->dns_domain_name);
67
68         return NT_STATUS_OK;
69 }
70
71 /****************************************************************
72 ****************************************************************/
73
74 static NTSTATUS fetch_sam_entry_keytab(TALLOC_CTX *mem_ctx,
75                                        enum netr_SamDatabaseID database_id,
76                                        uint32_t rid,
77                                        struct netr_DELTA_USER *r,
78                                        bool last_query,
79                                        struct libnet_keytab_context *ctx)
80 {
81         struct libnet_keytab_entry entry;
82
83         if (memcmp(r->ntpassword.hash, ctx->zero_buf, 16) == 0) {
84                 return NT_STATUS_OK;
85         }
86
87         entry.name = talloc_strdup(mem_ctx, r->account_name.string);
88         entry.principal = talloc_asprintf(mem_ctx, "%s@%s",
89                                           r->account_name.string,
90                                           ctx->dns_domain_name);
91         entry.password = data_blob_talloc(mem_ctx, r->ntpassword.hash, 16);
92         entry.kvno = ads_get_kvno(ctx->ads, entry.name);
93         entry.enctype = ENCTYPE_NULL;
94
95         NT_STATUS_HAVE_NO_MEMORY(entry.name);
96         NT_STATUS_HAVE_NO_MEMORY(entry.principal);
97         NT_STATUS_HAVE_NO_MEMORY(entry.password.data);
98
99
100         ADD_TO_ARRAY(mem_ctx, struct libnet_keytab_entry, entry,
101                      &ctx->entries, &ctx->count);
102
103         return NT_STATUS_OK;
104 }
105
106 /****************************************************************
107 ****************************************************************/
108
109 NTSTATUS fetch_sam_entries_keytab(TALLOC_CTX *mem_ctx,
110                                   enum netr_SamDatabaseID database_id,
111                                   struct netr_DELTA_ENUM_ARRAY *r,
112                                   bool last_query,
113                                   struct samsync_context *ctx)
114 {
115         NTSTATUS status = NT_STATUS_OK;
116         krb5_error_code ret = 0;
117         static struct libnet_keytab_context *keytab_ctx = NULL;
118         int i;
119
120         if (!keytab_ctx) {
121                 ret = libnet_keytab_init(mem_ctx, ctx->output_filename,
122                                          &keytab_ctx);
123                 if (ret) {
124                         status = krb5_to_nt_status(ret);
125                         goto out;
126                 }
127         }
128
129         status = keytab_ad_connect(mem_ctx,
130                                    ctx->domain_name,
131                                    ctx->username,
132                                    ctx->password,
133                                    keytab_ctx);
134         if (!NT_STATUS_IS_OK(status)) {
135                 goto out;
136         }
137
138         for (i = 0; i < r->num_deltas; i++) {
139
140                 if (r->delta_enum[i].delta_type != NETR_DELTA_USER) {
141                         continue;
142                 }
143
144                 status = fetch_sam_entry_keytab(mem_ctx, database_id,
145                                                 r->delta_enum[i].delta_id_union.rid,
146                                                 r->delta_enum[i].delta_union.user,
147                                                 last_query,
148                                                 keytab_ctx);
149                 if (!NT_STATUS_IS_OK(status)) {
150                         goto out;
151                 }
152         }
153
154         if (last_query) {
155
156                 ret = libnet_keytab_add(keytab_ctx);
157                 if (ret) {
158                         status = krb5_to_nt_status(ret);
159                         ctx->error_message = talloc_asprintf(mem_ctx,
160                                 "Failed to add entries to keytab %s: %s",
161                                 keytab_ctx->keytab_name, error_message(ret));
162                         goto out;
163                 }
164
165                 ctx->result_message = talloc_asprintf(mem_ctx,
166                         "Vampired %d accounts to keytab %s",
167                         keytab_ctx->count,
168                         keytab_ctx->keytab_name);
169
170                 TALLOC_FREE(keytab_ctx);
171         }
172
173         return NT_STATUS_OK;
174  out:
175         TALLOC_FREE(keytab_ctx);
176
177         return status;
178 }
179
180 #else
181
182 NTSTATUS fetch_sam_entries_keytab(TALLOC_CTX *mem_ctx,
183                                   enum netr_SamDatabaseID database_id,
184                                   struct netr_DELTA_ENUM_ARRAY *r,
185                                   bool last_query,
186                                   struct samsync_context *ctx)
187 {
188         return NT_STATUS_NOT_SUPPORTED;
189 }
190
191 #endif /* defined(HAVE_ADS) && defined(ENCTYPE_ARCFOUR_HMAC) */