libnet keytab: implement cleaning of old entries in libnet_keytab_add().
[samba.git] / source3 / libnet / libnet_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 #ifdef HAVE_KRB5
25
26 /****************************************************************
27 ****************************************************************/
28
29 static int keytab_close(struct libnet_keytab_context *ctx)
30 {
31         if (!ctx) {
32                 return 0;
33         }
34
35         if (ctx->keytab && ctx->context) {
36                 krb5_kt_close(ctx->context, ctx->keytab);
37         }
38
39         if (ctx->context) {
40                 krb5_free_context(ctx->context);
41         }
42
43         if (ctx->ads) {
44                 ads_destroy(&ctx->ads);
45         }
46
47         TALLOC_FREE(ctx);
48
49         return 0;
50 }
51
52 /****************************************************************
53 ****************************************************************/
54
55 krb5_error_code libnet_keytab_init(TALLOC_CTX *mem_ctx,
56                                    const char *keytab_name,
57                                    struct libnet_keytab_context **ctx)
58 {
59         krb5_error_code ret = 0;
60         krb5_context context = NULL;
61         krb5_keytab keytab = NULL;
62         const char *keytab_string = NULL;
63
64         struct libnet_keytab_context *r;
65
66         r = TALLOC_ZERO_P(mem_ctx, struct libnet_keytab_context);
67         if (!r) {
68                 return ENOMEM;
69         }
70
71         talloc_set_destructor(r, keytab_close);
72
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;
79         }
80
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;
87         }
88
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;
94         }
95
96         r->context = context;
97         r->keytab = keytab;
98         r->keytab_name = keytab_string;
99         r->clean_old_entries = false;
100
101         *ctx = r;
102
103         return 0;
104 }
105
106 /****************************************************************
107 ****************************************************************/
108
109 /**
110  * Remove all entries that have the given principal, kvno and enctype.
111  */
112 static krb5_error_code libnet_keytab_remove_entries(krb5_context context,
113                                                     krb5_keytab keytab,
114                                                     const char *principal,
115                                                     int kvno,
116                                                     const krb5_enctype enctype,
117                                                     bool ignore_kvno)
118 {
119         krb5_error_code ret;
120         krb5_kt_cursor cursor;
121         krb5_keytab_entry kt_entry;
122
123         ZERO_STRUCT(kt_entry);
124         ZERO_STRUCT(cursor);
125
126         ret = krb5_kt_start_seq_get(context, keytab, &cursor);
127         if (ret) {
128                 return 0;
129         }
130
131         while (krb5_kt_next_entry(context, keytab, &kt_entry, &cursor) == 0)
132         {
133                 char *princ_s = NULL;
134
135                 if (kt_entry.vno != kvno && !ignore_kvno) {
136                         goto cont;
137                 }
138
139                 if (kt_entry.key.enctype != enctype) {
140                         goto cont;
141                 }
142
143                 ret = smb_krb5_unparse_name(context, kt_entry.principal,
144                                             &princ_s);
145                 if (ret) {
146                         DEBUG(5, ("smb_krb5_unparse_name failed (%s)\n",
147                                   error_message(ret)));
148                         goto cont;
149                 }
150
151                 if (strcmp(principal, princ_s) != 0) {
152                         goto cont;
153                 }
154
155                 /* match found - remove */
156
157                 DEBUG(10, ("found entry for principal %s, kvno %d, "
158                            "enctype %d - trying to remove it\n",
159                            princ_s, kt_entry.vno, kt_entry.key.enctype));
160
161                 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
162                 ZERO_STRUCT(cursor);
163                 if (ret) {
164                         DEBUG(5, ("krb5_kt_end_seq_get failed (%s)\n",
165                                   error_message(ret)));
166                         goto cont;
167                 }
168
169                 ret = krb5_kt_remove_entry(context, keytab,
170                                            &kt_entry);
171                 if (ret) {
172                         DEBUG(5, ("krb5_kt_remove_entry failed (%s)\n",
173                                   error_message(ret)));
174                         goto cont;
175                 }
176                 DEBUG(10, ("removed entry for principal %s, kvno %d, "
177                            "enctype %d\n", princ_s, kt_entry.vno,
178                            kt_entry.key.enctype));
179
180                 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
181                 if (ret) {
182                         DEBUG(5, ("krb5_kt_start_seq_get failed (%s)\n",
183                                   error_message(ret)));
184                         goto cont;
185                 }
186
187 cont:
188                 smb_krb5_kt_free_entry(context, &kt_entry);
189                 SAFE_FREE(princ_s);
190         }
191
192         ret = krb5_kt_end_seq_get(context, keytab, &cursor);
193         if (ret) {
194                 DEBUG(5, ("krb5_kt_end_seq_get failed (%s)\n",
195                           error_message(ret)));
196         }
197
198         return ret;
199 }
200
201 static krb5_error_code libnet_keytab_add_entry(krb5_context context,
202                                                krb5_keytab keytab,
203                                                krb5_kvno kvno,
204                                                const char *princ_s,
205                                                krb5_enctype enctype,
206                                                krb5_data password)
207 {
208         krb5_keyblock *keyp;
209         krb5_keytab_entry kt_entry;
210         krb5_error_code ret;
211
212         /* remove duplicates first ... */
213         ret = libnet_keytab_remove_entries(context, keytab, princ_s, kvno,
214                                            enctype, false);
215         if (ret) {
216                 DEBUG(1, ("libnet_keytab_remove_entries failed: %s\n",
217                           error_message(ret)));
218         }
219
220         ZERO_STRUCT(kt_entry);
221
222         kt_entry.vno = kvno;
223
224         ret = smb_krb5_parse_name(context, princ_s, &kt_entry.principal);
225         if (ret) {
226                 DEBUG(1, ("smb_krb5_parse_name(%s) failed (%s)\n",
227                           princ_s, error_message(ret)));
228                 return ret;
229         }
230
231 #if !defined(HAVE_KRB5_KEYTAB_ENTRY_KEY) && !defined(HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK)
232 #error krb5_keytab_entry has no key or keyblock member
233 #endif
234 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEY               /* MIT */
235         keyp = &kt_entry.key;
236 #endif
237 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK          /* Heimdal */
238         keyp = &kt_entry.keyblock;
239 #endif
240
241         if (create_kerberos_key_from_string(context, kt_entry.principal,
242                                             &password, keyp, enctype, true))
243         {
244                 ret = KRB5KRB_ERR_GENERIC;
245                 goto done;
246         }
247
248         ret = krb5_kt_add_entry(context, keytab, &kt_entry);
249         if (ret) {
250                 DEBUG(1, ("adding entry to keytab failed (%s)\n",
251                           error_message(ret)));
252         }
253
254 done:
255         krb5_free_keyblock_contents(context, keyp);
256         krb5_free_principal(context, kt_entry.principal);
257         ZERO_STRUCT(kt_entry);
258         smb_krb5_kt_free_entry(context, &kt_entry);
259
260         return ret;
261 }
262
263 krb5_error_code libnet_keytab_add(struct libnet_keytab_context *ctx)
264 {
265         krb5_error_code ret = 0;
266         uint32_t i;
267
268
269         if (ctx->clean_old_entries) {
270                 DEBUG(0, ("cleaning old entries...\n"));
271                 for (i=0; i < ctx->count; i++) {
272                         struct libnet_keytab_entry *entry = &ctx->entries[i];
273
274                         ret = libnet_keytab_remove_entries(ctx->context,
275                                                            ctx->keytab,
276                                                            entry->principal,
277                                                            0,
278                                                            entry->enctype,
279                                                            true);
280                         if (ret) {
281                                 DEBUG(1,("libnet_keytab_add: Failed to remove "
282                                          "old entries for %s (enctype %u): %s\n",
283                                          entry->principal, entry->enctype,
284                                          error_message(ret)));
285                                 return ret;
286                         }
287                 }
288         }
289
290         for (i=0; i<ctx->count; i++) {
291
292                 struct libnet_keytab_entry *entry = &ctx->entries[i];
293                 krb5_data password;
294
295                 password.data = (char *)entry->password.data;
296                 password.length = entry->password.length;
297
298                 ret = libnet_keytab_add_entry(ctx->context,
299                                               ctx->keytab,
300                                               entry->kvno,
301                                               entry->principal,
302                                               entry->enctype,
303                                               password);
304                 if (ret) {
305                         DEBUG(1,("libnet_keytab_add: "
306                                 "Failed to add entry to keytab file\n"));
307                         return ret;
308                 }
309         }
310
311         return ret;
312 }
313
314 struct libnet_keytab_entry *libnet_keytab_search(struct libnet_keytab_context *ctx,
315                                                  const char *principal,
316                                                  int kvno,
317                                                  const krb5_enctype enctype,
318                                                  TALLOC_CTX *mem_ctx)
319 {
320         krb5_error_code ret = 0;
321         krb5_kt_cursor cursor;
322         krb5_keytab_entry kt_entry;
323         struct libnet_keytab_entry *entry = NULL;
324
325         ZERO_STRUCT(kt_entry);
326         ZERO_STRUCT(cursor);
327
328         ret = krb5_kt_start_seq_get(ctx->context, ctx->keytab, &cursor);
329         if (ret) {
330                 DEBUG(10, ("krb5_kt_start_seq_get failed: %s",
331                           error_message(ret)));
332                 return NULL;
333         }
334
335         while (krb5_kt_next_entry(ctx->context, ctx->keytab, &kt_entry, &cursor) == 0)
336         {
337                 char *princ_s = NULL;
338
339                 if (kt_entry.vno != kvno) {
340                         goto cont;
341                 }
342
343                 if (kt_entry.key.enctype != enctype) {
344                         goto cont;
345                 }
346
347                 ret = smb_krb5_unparse_name(ctx->context, kt_entry.principal,
348                                             &princ_s);
349                 if (ret) {
350                         goto cont;
351                 }
352
353                 if (strcmp(principal, princ_s) != 0) {
354                         goto cont;
355                 }
356
357                 entry = talloc_zero(mem_ctx, struct libnet_keytab_entry);
358                 if (!entry) {
359                         DEBUG(3, ("talloc failed\n"));
360                         goto fail;
361                 }
362
363                 entry->name = talloc_strdup(entry, princ_s);
364                 if (!entry->name) {
365                         DEBUG(3, ("talloc_strdup_failed\n"));
366                         goto fail;
367                 }
368
369                 entry->principal = talloc_strdup(entry, princ_s);
370                 if (!entry->principal) {
371                         DEBUG(3, ("talloc_strdup_failed\n"));
372                         goto fail;
373                 }
374
375                 entry->password = data_blob_talloc(entry, kt_entry.key.contents,
376                                                    kt_entry.key.length);
377                 if (!entry->password.data) {
378                         DEBUG(3, ("data_blob_talloc failed\n"));
379                         goto fail;
380                 }
381
382                 DEBUG(10, ("found entry\n"));
383
384                 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
385                 SAFE_FREE(princ_s);
386                 break;
387
388 fail:
389                 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
390                 SAFE_FREE(princ_s);
391                 TALLOC_FREE(entry);
392                 break;
393
394 cont:
395                 smb_krb5_kt_free_entry(ctx->context, &kt_entry);
396                 SAFE_FREE(princ_s);
397                 continue;
398         }
399
400         krb5_kt_end_seq_get(ctx->context, ctx->keytab, &cursor);
401         return entry;
402 }
403
404 #endif /* HAVE_KRB5 */