libnet keytab: test for matching enctype in libnet_keytab_search().
[Samba/bb.git] / source / libnet / libnet_keytab.c
blobcc51c29326aea8ad8207c679bfc97428798158bb
1 /*
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/>.
21 #include "includes.h"
22 #include "libnet/libnet.h"
24 #ifdef HAVE_KRB5
26 /****************************************************************
27 ****************************************************************/
29 static int keytab_close(struct libnet_keytab_context *ctx)
31 if (!ctx) {
32 return 0;
35 if (ctx->keytab && ctx->context) {
36 krb5_kt_close(ctx->context, ctx->keytab);
39 if (ctx->context) {
40 krb5_free_context(ctx->context);
43 if (ctx->ads) {
44 ads_destroy(&ctx->ads);
47 TALLOC_FREE(ctx);
49 return 0;
52 /****************************************************************
53 ****************************************************************/
55 krb5_error_code libnet_keytab_init(TALLOC_CTX *mem_ctx,
56 const char *keytab_name,
57 struct libnet_keytab_context **ctx)
59 krb5_error_code ret = 0;
60 krb5_context context = NULL;
61 krb5_keytab keytab = NULL;
62 const char *keytab_string = NULL;
64 struct libnet_keytab_context *r;
66 r = TALLOC_ZERO_P(mem_ctx, struct libnet_keytab_context);
67 if (!r) {
68 return ENOMEM;
71 talloc_set_destructor(r, keytab_close);
73 initialize_krb5_error_table();
74 ret = krb5_init_context(&context);
75 if (ret) {
76 DEBUG(1,("keytab_init: could not krb5_init_context: %s\n",
77 error_message(ret)));
78 return ret;
81 ret = smb_krb5_open_keytab(context, keytab_name, true, &keytab);
82 if (ret) {
83 DEBUG(1,("keytab_init: smb_krb5_open_keytab failed (%s)\n",
84 error_message(ret)));
85 krb5_free_context(context);
86 return ret;
89 ret = smb_krb5_keytab_name(mem_ctx, context, keytab, &keytab_string);
90 if (ret) {
91 krb5_kt_close(context, keytab);
92 krb5_free_context(context);
93 return ret;
96 r->context = context;
97 r->keytab = keytab;
98 r->keytab_name = keytab_string;
100 *ctx = r;
102 return 0;
105 /****************************************************************
106 ****************************************************************/
108 krb5_error_code libnet_keytab_add(struct libnet_keytab_context *ctx)
110 #if defined(ENCTYPE_ARCFOUR_HMAC)
111 krb5_error_code ret = 0;
112 krb5_enctype enctypes[2] = { ENCTYPE_ARCFOUR_HMAC, 0 };
113 int i;
115 for (i=0; i<ctx->count; i++) {
117 struct libnet_keytab_entry *entry = &ctx->entries[i];
118 krb5_data password;
120 password.data = (char *)entry->password.data;
121 password.length = entry->password.length;
123 ret = smb_krb5_kt_add_entry_ext(ctx->context,
124 ctx->keytab,
125 entry->kvno,
126 entry->principal,
127 enctypes,
128 password,
129 true,
130 true);
131 if (ret) {
132 DEBUG(1,("libnet_keytab_add: "
133 "Failed to add entry to keytab file\n"));
134 return ret;
138 return ret;
139 #else
140 return -1;
141 #endif /* defined(ENCTYPE_ARCFOUR_HMAC) */
144 struct libnet_keytab_entry *libnet_keytab_search(struct libnet_keytab_context *ctx,
145 const char *principal, int kvno,
146 const krb5_enctype enctype,
147 TALLOC_CTX *mem_ctx)
149 krb5_error_code ret = 0;
150 krb5_kt_cursor cursor;
151 krb5_keytab_entry kt_entry;
152 struct libnet_keytab_entry *entry = NULL;
154 ZERO_STRUCT(kt_entry);
155 ZERO_STRUCT(cursor);
157 ret = krb5_kt_start_seq_get(ctx->context, ctx->keytab, &cursor);
158 if (ret) {
159 return NULL;
162 while (krb5_kt_next_entry(ctx->context, ctx->keytab, &kt_entry, &cursor) == 0) {
163 char *princ_s = NULL;
165 if (kt_entry.vno != kvno) {
166 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
167 continue;
170 if (kt_entry.key.enctype != enctype) {
171 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
172 continue;
175 ret = smb_krb5_unparse_name(ctx->context, kt_entry.principal, &princ_s);
176 if (ret) {
177 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
178 continue;
181 if (strcmp(principal, princ_s) != 0) {
182 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
183 SAFE_FREE(princ_s);
184 continue;
187 entry = talloc_zero(mem_ctx, struct libnet_keytab_entry);
188 if (!entry) {
189 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
190 SAFE_FREE(princ_s);
191 break;
194 entry->name = talloc_strdup(entry, princ_s);
195 if (!entry->name) {
196 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
197 SAFE_FREE(princ_s);
198 TALLOC_FREE(entry);
199 break;
202 entry->principal = talloc_strdup(entry, princ_s);
203 if (!entry->principal) {
204 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
205 SAFE_FREE(princ_s);
206 TALLOC_FREE(entry);
207 break;
210 entry->password = data_blob_talloc(entry, kt_entry.key.contents, kt_entry.key.length);
211 if (!entry->password.data) {
212 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
213 SAFE_FREE(princ_s);
214 TALLOC_FREE(entry);
215 break;
218 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
219 SAFE_FREE(princ_s);
220 break;
223 krb5_kt_end_seq_get(ctx->context, ctx->keytab, &cursor);
224 return entry;
227 #endif /* HAVE_KRB5 */