xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/keytab.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: keytab.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "krb5_locl.h"
37 
38 /**
39  * @page krb5_keytab_intro The keytab handing functions
40  * @section section_krb5_keytab Kerberos Keytabs
41  *
42  * See the library functions here: @ref krb5_keytab
43  *
44  * Keytabs are long term key storage for servers, their equvalment of
45  * password files.
46  *
47  * Normally the only function that useful for server are to specify
48  * what keytab to use to other core functions like krb5_rd_req()
49  * krb5_kt_resolve(), and krb5_kt_close().
50  *
51  * @subsection krb5_keytab_names Keytab names
52  *
53  * A keytab name is on the form type:residual. The residual part is
54  * specific to each keytab-type.
55  *
56  * When a keytab-name is resolved, the type is matched with an internal
57  * list of keytab types. If there is no matching keytab type,
58  * the default keytab is used. The current default type is FILE.
59  *
60  * The default value can be changed in the configuration file
61  * /etc/krb5.conf by setting the variable
62  * [defaults]default_keytab_name.
63  *
64  * The keytab types that are implemented in Heimdal are:
65  * - file
66  *   store the keytab in a file, the type's name is FILE .  The
67  *   residual part is a filename. For compatibility with other
68  *   Kerberos implemtation WRFILE and JAVA14 is also accepted.  WRFILE
69  *   has the same format as FILE. JAVA14 have a format that is
70  *   compatible with older versions of MIT kerberos and SUN's Java
71  *   based installation.  They store a truncted kvno, so when the knvo
72  *   excess 255, they are truncted in this format.
73  *
74  * - keytab
75  *   store the keytab in a AFS keyfile (usually /usr/afs/etc/KeyFile ),
76  *   the type's name is AFSKEYFILE. The residual part is a filename.
77  *
78  * - memory
79  *   The keytab is stored in a memory segment. This allows sensitive
80  *   and/or temporary data not to be stored on disk. The type's name
81  *   is MEMORY. Each MEMORY keytab is referenced counted by and
82  *   opened by the residual name, so two handles can point to the
83  *   same memory area.  When the last user closes using krb5_kt_close()
84  *   the keytab, the keys in they keytab is memset() to zero and freed
85  *   and can no longer be looked up by name.
86  *
87  *
88  * @subsection krb5_keytab_example Keytab example
89  *
90  *  This is a minimalistic version of ktutil.
91  *
92  * @code
93 int
94 main (int argc, char **argv)
95 {
96     krb5_context context;
97     krb5_keytab keytab;
98     krb5_kt_cursor cursor;
99     krb5_keytab_entry entry;
100     krb5_error_code ret;
101     char *principal;
102 
103     if (krb5_init_context (&context) != 0)
104 	errx(1, "krb5_context");
105 
106     ret = krb5_kt_default (context, &keytab);
107     if (ret)
108 	krb5_err(context, 1, ret, "krb5_kt_default");
109 
110     ret = krb5_kt_start_seq_get(context, keytab, &cursor);
111     if (ret)
112 	krb5_err(context, 1, ret, "krb5_kt_start_seq_get");
113     while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0){
114 	krb5_unparse_name(context, entry.principal, &principal);
115 	printf("principal: %s\n", principal);
116 	free(principal);
117 	krb5_kt_free_entry(context, &entry);
118     }
119     ret = krb5_kt_end_seq_get(context, keytab, &cursor);
120     if (ret)
121 	krb5_err(context, 1, ret, "krb5_kt_end_seq_get");
122     ret = krb5_kt_close(context, keytab);
123     if (ret)
124 	krb5_err(context, 1, ret, "krb5_kt_close");
125     krb5_free_context(context);
126     return 0;
127 }
128  * @endcode
129  *
130  */
131 
132 
133 /**
134  * Register a new keytab backend.
135  *
136  * @param context a Keberos context.
137  * @param ops a backend to register.
138  *
139  * @return Return an error code or 0, see krb5_get_error_message().
140  *
141  * @ingroup krb5_keytab
142  */
143 
144 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
145 krb5_kt_register(krb5_context context,
146 		 const krb5_kt_ops *ops)
147 {
148     struct krb5_keytab_data *tmp;
149 
150     if (strlen(ops->prefix) > KRB5_KT_PREFIX_MAX_LEN - 1) {
151 	krb5_set_error_message(context, KRB5_KT_BADNAME,
152 			       N_("can't register cache type, prefix too long", ""));
153 	return KRB5_KT_BADNAME;
154     }
155 
156     tmp = realloc(context->kt_types,
157 		  (context->num_kt_types + 1) * sizeof(*context->kt_types));
158     if(tmp == NULL)
159 	return krb5_enomem(context);
160     memcpy(&tmp[context->num_kt_types], ops,
161 	   sizeof(tmp[context->num_kt_types]));
162     context->kt_types = tmp;
163     context->num_kt_types++;
164     return 0;
165 }
166 
167 static const char *
168 keytab_name(const char *name, const char **type, size_t *type_len)
169 {
170     const char *residual;
171 
172     residual = strchr(name, ':');
173 
174     if (residual == NULL ||
175 	ISPATHSEP(name[0])
176 #ifdef _WIN32
177         /* Avoid treating <drive>:<path> as a keytab type
178          * specification */
179         || name + 1 == residual
180 #endif
181         ) {
182 
183         *type = "FILE";
184         *type_len = strlen(*type);
185         residual = name;
186     } else {
187         *type = name;
188         *type_len = residual - name;
189         residual++;
190     }
191 
192     return residual;
193 }
194 
195 /**
196  * Resolve the keytab name (of the form `type:residual') in `name'
197  * into a keytab in `id'.
198  *
199  * @param context a Keberos context.
200  * @param name name to resolve
201  * @param id resulting keytab, free with krb5_kt_close().
202  *
203  * @return Return an error code or 0, see krb5_get_error_message().
204  *
205  * @ingroup krb5_keytab
206  */
207 
208 
209 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
210 krb5_kt_resolve(krb5_context context,
211 		const char *name,
212 		krb5_keytab *id)
213 {
214     krb5_keytab k;
215     int i;
216     const char *type, *residual;
217     size_t type_len;
218     krb5_error_code ret;
219 
220     residual = keytab_name(name, &type, &type_len);
221 
222     for(i = 0; i < context->num_kt_types; i++) {
223 	if(strncasecmp(type, context->kt_types[i].prefix, type_len) == 0)
224 	    break;
225     }
226     if(i == context->num_kt_types) {
227 	krb5_set_error_message(context, KRB5_KT_UNKNOWN_TYPE,
228 			       N_("unknown keytab type %.*s", "type"),
229 			       (int)type_len, type);
230 	return KRB5_KT_UNKNOWN_TYPE;
231     }
232 
233     k = malloc (sizeof(*k));
234     if (k == NULL)
235 	return krb5_enomem(context);
236     memcpy(k, &context->kt_types[i], sizeof(*k));
237     k->data = NULL;
238     ret = (*k->resolve)(context, residual, k);
239     if(ret) {
240 	free(k);
241 	k = NULL;
242     }
243     *id = k;
244     return ret;
245 }
246 
247 /*
248  * Default ktname from context with possible environment
249  * override
250  */
251 static const char *default_ktname(krb5_context context)
252 {
253     const char *tmp = NULL;
254 
255     if(!issuid())
256 	tmp = getenv("KRB5_KTNAME");
257     if(tmp != NULL)
258 	return tmp;
259     return context->default_keytab;
260 }
261 
262 /**
263  * copy the name of the default keytab into `name'.
264  *
265  * @param context a Keberos context.
266  * @param name buffer where the name will be written
267  * @param namesize length of name
268  *
269  * @return Return an error code or 0, see krb5_get_error_message().
270  *
271  * @ingroup krb5_keytab
272  */
273 
274 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
275 krb5_kt_default_name(krb5_context context, char *name, size_t namesize)
276 {
277     if (strlcpy (name, default_ktname(context), namesize) >= namesize) {
278 	krb5_clear_error_message (context);
279 	return KRB5_CONFIG_NOTENUFSPACE;
280     }
281     return 0;
282 }
283 
284 /**
285  * Copy the name of the default modify keytab into `name'.
286  *
287  * @param context a Keberos context.
288  * @param name buffer where the name will be written
289  * @param namesize length of name
290  *
291  * @return Return an error code or 0, see krb5_get_error_message().
292  *
293  * @ingroup krb5_keytab
294  */
295 
296 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
297 krb5_kt_default_modify_name(krb5_context context, char *name, size_t namesize)
298 {
299     const char *kt;
300 
301     if(context->default_keytab_modify == NULL) {
302 	kt = default_ktname(context);
303 
304 	if (strncasecmp(kt, "ANY:", 4) == 0) {
305 	    size_t len = strcspn(kt + 4, ",");
306 	    if (len >= namesize) {
307 		krb5_clear_error_message(context);
308 		return KRB5_CONFIG_NOTENUFSPACE;
309 	    }
310 	    strlcpy(name, kt + 4, namesize);
311 	    name[len] = '\0';
312 	    return 0;
313 	}
314     } else
315 	kt = context->default_keytab_modify;
316     if (strlcpy (name, kt, namesize) >= namesize) {
317 	krb5_clear_error_message (context);
318 	return KRB5_CONFIG_NOTENUFSPACE;
319     }
320     return 0;
321 }
322 
323 /**
324  * Set `id' to the default keytab.
325  *
326  * @param context a Keberos context.
327  * @param id the new default keytab.
328  *
329  * @return Return an error code or 0, see krb5_get_error_message().
330  *
331  * @ingroup krb5_keytab
332  */
333 
334 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
335 krb5_kt_default(krb5_context context, krb5_keytab *id)
336 {
337     return krb5_kt_resolve (context, default_ktname(context), id);
338 }
339 
340 /**
341  * Read the key identified by `(principal, vno, enctype)' from the
342  * keytab in `keyprocarg' (the default if == NULL) into `*key'.
343  *
344  * @param context a Keberos context.
345  * @param keyprocarg
346  * @param principal
347  * @param vno
348  * @param enctype
349  * @param key
350  *
351  * @return Return an error code or 0, see krb5_get_error_message().
352  *
353  * @ingroup krb5_keytab
354  */
355 
356 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
357 krb5_kt_read_service_key(krb5_context context,
358 			 krb5_pointer keyprocarg,
359 			 krb5_principal principal,
360 			 krb5_kvno vno,
361 			 krb5_enctype enctype,
362 			 krb5_keyblock **key)
363 {
364     krb5_keytab keytab;
365     krb5_keytab_entry entry;
366     krb5_error_code ret;
367 
368     if (keyprocarg)
369 	ret = krb5_kt_resolve (context, keyprocarg, &keytab);
370     else
371 	ret = krb5_kt_default (context, &keytab);
372 
373     if (ret)
374 	return ret;
375 
376     ret = krb5_kt_get_entry (context, keytab, principal, vno, enctype, &entry);
377     krb5_kt_close (context, keytab);
378     if (ret)
379 	return ret;
380     ret = krb5_copy_keyblock (context, &entry.keyblock, key);
381     krb5_kt_free_entry(context, &entry);
382     return ret;
383 }
384 
385 /**
386  * Return the type of the `keytab' in the string `prefix of length
387  * `prefixsize'.
388  *
389  * @param context a Keberos context.
390  * @param keytab the keytab to get the prefix for
391  * @param prefix prefix buffer
392  * @param prefixsize length of prefix buffer
393  *
394  * @return Return an error code or 0, see krb5_get_error_message().
395  *
396  * @ingroup krb5_keytab
397  */
398 
399 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
400 krb5_kt_get_type(krb5_context context,
401 		 krb5_keytab keytab,
402 		 char *prefix,
403 		 size_t prefixsize)
404 {
405     strlcpy(prefix, keytab->prefix, prefixsize);
406     return 0;
407 }
408 
409 /**
410  * Retrieve the name of the keytab `keytab' into `name', `namesize'
411  *
412  * @param context a Keberos context.
413  * @param keytab the keytab to get the name for.
414  * @param name name buffer.
415  * @param namesize size of name buffer.
416  *
417  * @return Return an error code or 0, see krb5_get_error_message().
418  *
419  * @ingroup krb5_keytab
420  */
421 
422 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
423 krb5_kt_get_name(krb5_context context,
424 		 krb5_keytab keytab,
425 		 char *name,
426 		 size_t namesize)
427 {
428     return (*keytab->get_name)(context, keytab, name, namesize);
429 }
430 
431 /**
432  * Retrieve the full name of the keytab `keytab' and store the name in
433  * `str'.
434  *
435  * @param context a Keberos context.
436  * @param keytab keytab to get name for.
437  * @param str the name of the keytab name, usee krb5_xfree() to free
438  *        the string.  On error, *str is set to NULL.
439  *
440  * @return Return an error code or 0, see krb5_get_error_message().
441  *
442  * @ingroup krb5_keytab
443  */
444 
445 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
446 krb5_kt_get_full_name(krb5_context context,
447 		      krb5_keytab keytab,
448 		      char **str)
449 {
450     char type[KRB5_KT_PREFIX_MAX_LEN];
451     char name[MAXPATHLEN];
452     krb5_error_code ret;
453 
454     *str = NULL;
455 
456     ret = krb5_kt_get_type(context, keytab, type, sizeof(type));
457     if (ret)
458 	return ret;
459 
460     ret = krb5_kt_get_name(context, keytab, name, sizeof(name));
461     if (ret)
462 	return ret;
463 
464     if (asprintf(str, "%s:%s", type, name) == -1) {
465 	*str = NULL;
466 	return krb5_enomem(context);
467     }
468 
469     return 0;
470 }
471 
472 /**
473  * Finish using the keytab in `id'.  All resources will be released,
474  * even on errors.
475  *
476  * @param context a Keberos context.
477  * @param id keytab to close.
478  *
479  * @return Return an error code or 0, see krb5_get_error_message().
480  *
481  * @ingroup krb5_keytab
482  */
483 
484 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
485 krb5_kt_close(krb5_context context,
486 	      krb5_keytab id)
487 {
488     krb5_error_code ret;
489 
490     ret = (*id->close)(context, id);
491     memset(id, 0, sizeof(*id));
492     free(id);
493     return ret;
494 }
495 
496 /**
497  * Destroy (remove) the keytab in `id'.  All resources will be released,
498  * even on errors, does the equvalment of krb5_kt_close() on the resources.
499  *
500  * @param context a Keberos context.
501  * @param id keytab to destroy.
502  *
503  * @return Return an error code or 0, see krb5_get_error_message().
504  *
505  * @ingroup krb5_keytab
506  */
507 
508 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
509 krb5_kt_destroy(krb5_context context,
510 		krb5_keytab id)
511 {
512     krb5_error_code ret;
513 
514     ret = (*id->destroy)(context, id);
515     krb5_kt_close(context, id);
516     return ret;
517 }
518 
519 /*
520  * Match any aliases in keytab `entry' with `principal'.
521  */
522 
523 static krb5_boolean
524 compare_aliases(krb5_context context,
525 		 krb5_keytab_entry *entry,
526 		 krb5_const_principal principal)
527 {
528     unsigned int i;
529     if (entry->aliases == NULL)
530 	return FALSE;
531     for (i = 0; i < entry->aliases->len; i++)
532 	if (krb5_principal_compare(context, &entry->aliases->val[i], principal))
533 	    return TRUE;
534     return FALSE;
535 }
536 
537 /**
538  * Compare `entry' against `principal, vno, enctype'.
539  * Any of `principal, vno, enctype' might be 0 which acts as a wildcard.
540  * Return TRUE if they compare the same, FALSE otherwise.
541  *
542  * @param context a Keberos context.
543  * @param entry an entry to match with.
544  * @param principal principal to match, NULL matches all principals.
545  * @param vno key version to match, 0 matches all key version numbers.
546  * @param enctype encryption type to match, 0 matches all encryption types.
547  *
548  * @return Return TRUE or match, FALSE if not matched.
549  *
550  * @ingroup krb5_keytab
551  */
552 
553 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
554 krb5_kt_compare(krb5_context context,
555 		krb5_keytab_entry *entry,
556 		krb5_const_principal principal,
557 		krb5_kvno vno,
558 		krb5_enctype enctype)
559 {
560     /* krb5_principal_compare() does not special-case the referral realm */
561     if (principal != NULL && strcmp(principal->realm, "") == 0 &&
562         !(krb5_principal_compare_any_realm(context, entry->principal, principal) ||
563           compare_aliases(context, entry, principal))) {
564         return FALSE;
565     } else if (principal != NULL && strcmp(principal->realm, "") != 0 &&
566         !(krb5_principal_compare(context, entry->principal, principal) ||
567           compare_aliases(context, entry, principal))) {
568 	return FALSE;
569     }
570     if (vno && vno != entry->vno)
571 	return FALSE;
572     if (enctype && enctype != entry->keyblock.keytype)
573 	return FALSE;
574     return TRUE;
575 }
576 
577 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
578 _krb5_kt_principal_not_found(krb5_context context,
579 			     krb5_error_code ret,
580 			     krb5_keytab id,
581 			     krb5_const_principal principal,
582 			     krb5_enctype enctype,
583 			     int kvno)
584 {
585     char princ[256], kvno_str[25], *kt_name;
586     char *enctype_str = NULL;
587 
588     krb5_unparse_name_fixed (context, principal, princ, sizeof(princ));
589     krb5_kt_get_full_name (context, id, &kt_name);
590     if (enctype)
591 	krb5_enctype_to_string(context, enctype, &enctype_str);
592 
593     if (kvno)
594 	snprintf(kvno_str, sizeof(kvno_str), "(kvno %d)", kvno);
595     else
596 	kvno_str[0] = '\0';
597 
598     krb5_set_error_message (context, ret,
599 			    N_("Failed to find %s%s in keytab %s (%s)",
600 			       "principal, kvno, keytab file, enctype"),
601 			    princ,
602 			    kvno_str,
603 			    kt_name ? kt_name : "unknown keytab",
604 			    enctype_str ? enctype_str : "unknown enctype");
605     free(kt_name);
606     if (enctype_str)
607 	free(enctype_str);
608     return ret;
609 }
610 
611 static krb5_error_code
612 krb5_kt_get_entry_wrapped(krb5_context context,
613 			  krb5_keytab id,
614 			  krb5_const_principal principal,
615 			  krb5_kvno kvno,
616 			  krb5_enctype enctype,
617 			  krb5_keytab_entry *entry)
618 {
619     krb5_keytab_entry tmp;
620     krb5_error_code ret;
621     krb5_kt_cursor cursor;
622 
623     if(id->get)
624 	return (*id->get)(context, id, principal, kvno, enctype, entry);
625 
626     ret = krb5_kt_start_seq_get (context, id, &cursor);
627     if (ret) {
628 	/* This is needed for krb5_verify_init_creds, but keep error
629 	 * string from previous error for the human. */
630 	context->error_code = KRB5_KT_NOTFOUND;
631 	return KRB5_KT_NOTFOUND;
632     }
633 
634     entry->vno = 0;
635     while (krb5_kt_next_entry(context, id, &tmp, &cursor) == 0) {
636 	if (krb5_kt_compare(context, &tmp, principal, 0, enctype)) {
637 	    /* the file keytab might only store the lower 8 bits of
638 	       the kvno, so only compare those bits */
639 	    if (kvno == tmp.vno
640 		|| (tmp.vno < 256 && kvno % 256 == tmp.vno)) {
641 		krb5_kt_copy_entry_contents (context, &tmp, entry);
642 		krb5_kt_free_entry (context, &tmp);
643 		krb5_kt_end_seq_get(context, id, &cursor);
644 		return 0;
645 	    } else if (kvno == 0 && tmp.vno > entry->vno) {
646 		if (entry->vno)
647 		    krb5_kt_free_entry (context, entry);
648 		krb5_kt_copy_entry_contents (context, &tmp, entry);
649 	    }
650 	}
651 	krb5_kt_free_entry(context, &tmp);
652     }
653     krb5_kt_end_seq_get (context, id, &cursor);
654     if (entry->vno == 0)
655 	return _krb5_kt_principal_not_found(context, KRB5_KT_NOTFOUND,
656 					    id, principal, enctype, kvno);
657     return 0;
658 }
659 
660 /**
661  * Retrieve the keytab entry for `principal, kvno, enctype' into `entry'
662  * from the keytab `id'. Matching is done like krb5_kt_compare().
663  *
664  * @param context a Keberos context.
665  * @param id a keytab.
666  * @param principal principal to match, NULL matches all principals.
667  * @param kvno key version to match, 0 matches all key version numbers.
668  * @param enctype encryption type to match, 0 matches all encryption types.
669  * @param entry the returned entry, free with krb5_kt_free_entry().
670  *
671  * @return Return an error code or 0, see krb5_get_error_message().
672  *
673  * @ingroup krb5_keytab
674  */
675 
676 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
677 krb5_kt_get_entry(krb5_context context,
678 		  krb5_keytab id,
679 		  krb5_const_principal principal,
680 		  krb5_kvno kvno,
681 		  krb5_enctype enctype,
682 		  krb5_keytab_entry *entry)
683 {
684     krb5_error_code ret;
685     krb5_const_principal try_princ;
686     krb5_name_canon_iterator name_canon_iter;
687 
688     if (!principal)
689 	return krb5_kt_get_entry_wrapped(context, id, principal, kvno, enctype,
690 					 entry);
691 
692     ret = krb5_name_canon_iterator_start(context, principal, &name_canon_iter);
693     if (ret)
694 	return ret;
695 
696     do {
697 	ret = krb5_name_canon_iterate(context, &name_canon_iter, &try_princ,
698                                       NULL);
699 	if (ret)
700 	    break;
701         if (try_princ == NULL) {
702             ret = KRB5_KT_NOTFOUND;
703             continue;
704         }
705 	ret = krb5_kt_get_entry_wrapped(context, id, try_princ, kvno,
706 					enctype, entry);
707     } while (ret == KRB5_KT_NOTFOUND && name_canon_iter);
708 
709     if (ret != KRB5_KT_NOTFOUND)
710 	krb5_set_error_message(context, ret,
711 			       N_("Name canon failed while searching keytab",
712 				  ""));
713     krb5_free_name_canon_iterator(context, name_canon_iter);
714     return ret;
715 }
716 
717 /**
718  * Copy the contents of `in' into `out'.
719  *
720  * @param context a Keberos context.
721  * @param in the keytab entry to copy.
722  * @param out the copy of the keytab entry, free with krb5_kt_free_entry().
723  *
724  * @return Return an error code or 0, see krb5_get_error_message().
725  *
726  * @ingroup krb5_keytab
727  */
728 
729 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
730 krb5_kt_copy_entry_contents(krb5_context context,
731 			    const krb5_keytab_entry *in,
732 			    krb5_keytab_entry *out)
733 {
734     krb5_error_code ret;
735 
736     memset(out, 0, sizeof(*out));
737     out->vno = in->vno;
738 
739     ret = krb5_copy_principal (context, in->principal, &out->principal);
740     if (ret)
741 	goto fail;
742     ret = krb5_copy_keyblock_contents (context,
743 				       &in->keyblock,
744 				       &out->keyblock);
745     if (ret)
746 	goto fail;
747     out->timestamp = in->timestamp;
748     return 0;
749 fail:
750     krb5_kt_free_entry (context, out);
751     return ret;
752 }
753 
754 /**
755  * Free the contents of `entry'.
756  *
757  * @param context a Keberos context.
758  * @param entry the entry to free
759  *
760  * @return Return an error code or 0, see krb5_get_error_message().
761  *
762  * @ingroup krb5_keytab
763  */
764 
765 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
766 krb5_kt_free_entry(krb5_context context,
767 		   krb5_keytab_entry *entry)
768 {
769     krb5_free_principal (context, entry->principal);
770     krb5_free_keyblock_contents (context, &entry->keyblock);
771     memset(entry, 0, sizeof(*entry));
772     return 0;
773 }
774 
775 /**
776  * Set `cursor' to point at the beginning of `id'.
777  *
778  * @param context a Keberos context.
779  * @param id a keytab.
780  * @param cursor a newly allocated cursor, free with krb5_kt_end_seq_get().
781  *
782  * @return Return an error code or 0, see krb5_get_error_message().
783  *
784  * @ingroup krb5_keytab
785  */
786 
787 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
788 krb5_kt_start_seq_get(krb5_context context,
789 		      krb5_keytab id,
790 		      krb5_kt_cursor *cursor)
791 {
792     if(id->start_seq_get == NULL) {
793 	krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
794 			       N_("start_seq_get is not supported "
795 				  "in the %s keytab type", ""),
796 			       id->prefix);
797 	return HEIM_ERR_OPNOTSUPP;
798     }
799     return (*id->start_seq_get)(context, id, cursor);
800 }
801 
802 /**
803  * Get the next entry from keytab, advance the cursor.  On last entry
804  * the function will return KRB5_KT_END.
805  *
806  * @param context a Keberos context.
807  * @param id a keytab.
808  * @param entry the returned entry, free with krb5_kt_free_entry().
809  * @param cursor the cursor of the iteration.
810  *
811  * @return Return an error code or 0, see krb5_get_error_message().
812  *
813  * @ingroup krb5_keytab
814  */
815 
816 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
817 krb5_kt_next_entry(krb5_context context,
818 		   krb5_keytab id,
819 		   krb5_keytab_entry *entry,
820 		   krb5_kt_cursor *cursor)
821 {
822     if(id->next_entry == NULL) {
823 	krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
824 			       N_("next_entry is not supported in the %s "
825 				  " keytab", ""),
826 			       id->prefix);
827 	return HEIM_ERR_OPNOTSUPP;
828     }
829     return (*id->next_entry)(context, id, entry, cursor);
830 }
831 
832 /**
833  * Release all resources associated with `cursor'.
834  *
835  * @param context a Keberos context.
836  * @param id a keytab.
837  * @param cursor the cursor to free.
838  *
839  * @return Return an error code or 0, see krb5_get_error_message().
840  *
841  * @ingroup krb5_keytab
842  */
843 
844 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
845 krb5_kt_end_seq_get(krb5_context context,
846 		    krb5_keytab id,
847 		    krb5_kt_cursor *cursor)
848 {
849     if(id->end_seq_get == NULL) {
850 	krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
851 			       "end_seq_get is not supported in the %s "
852 			       " keytab", id->prefix);
853 	return HEIM_ERR_OPNOTSUPP;
854     }
855     return (*id->end_seq_get)(context, id, cursor);
856 }
857 
858 /**
859  * Add the entry in `entry' to the keytab `id'.
860  *
861  * @param context a Keberos context.
862  * @param id a keytab.
863  * @param entry the entry to add
864  *
865  * @return Return an error code or 0, see krb5_get_error_message().
866  *
867  * @ingroup krb5_keytab
868  */
869 
870 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
871 krb5_kt_add_entry(krb5_context context,
872 		  krb5_keytab id,
873 		  krb5_keytab_entry *entry)
874 {
875     if(id->add == NULL) {
876 	krb5_set_error_message(context, KRB5_KT_NOWRITE,
877 			       N_("Add is not supported in the %s keytab", ""),
878 			       id->prefix);
879 	return KRB5_KT_NOWRITE;
880     }
881     entry->timestamp = time(NULL);
882     return (*id->add)(context, id,entry);
883 }
884 
885 /**
886  * Remove an entry from the keytab, matching is done using
887  * krb5_kt_compare().
888 
889  * @param context a Keberos context.
890  * @param id a keytab.
891  * @param entry the entry to remove
892  *
893  * @return Return an error code or 0, see krb5_get_error_message().
894  *
895  * @ingroup krb5_keytab
896  */
897 
898 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
899 krb5_kt_remove_entry(krb5_context context,
900 		     krb5_keytab id,
901 		     krb5_keytab_entry *entry)
902 {
903     if(id->remove == NULL) {
904 	krb5_set_error_message(context, KRB5_KT_NOWRITE,
905 			       N_("Remove is not supported in the %s keytab", ""),
906 			       id->prefix);
907 	return KRB5_KT_NOWRITE;
908     }
909     return (*id->remove)(context, id, entry);
910 }
911 
912 /**
913  * Return true if the keytab exists and have entries
914  *
915  * @param context a Keberos context.
916  * @param id a keytab.
917  *
918  * @return Return an error code or 0, see krb5_get_error_message().
919  *
920  * @ingroup krb5_keytab
921  */
922 
923 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
924 krb5_kt_have_content(krb5_context context,
925 		     krb5_keytab id)
926 {
927     krb5_keytab_entry entry;
928     krb5_kt_cursor cursor;
929     krb5_error_code ret;
930     char *name;
931 
932     ret = krb5_kt_start_seq_get(context, id, &cursor);
933     if (ret)
934 	goto notfound;
935 
936     ret = krb5_kt_next_entry(context, id, &entry, &cursor);
937     krb5_kt_end_seq_get(context, id, &cursor);
938     if (ret)
939 	goto notfound;
940 
941     krb5_kt_free_entry(context, &entry);
942 
943     return 0;
944 
945  notfound:
946     ret = krb5_kt_get_full_name(context, id, &name);
947     if (ret == 0) {
948 	krb5_set_error_message(context, KRB5_KT_NOTFOUND,
949 			       N_("No entry in keytab: %s", ""), name);
950 	free(name);
951     }
952     return KRB5_KT_NOTFOUND;
953 }
954