2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
39 * Register a new keytab in `ops'
40 * Return 0 or an error.
43 krb5_error_code KRB5_LIB_FUNCTION
44 krb5_kt_register(krb5_context context,
45 const krb5_kt_ops *ops)
47 struct krb5_keytab_data *tmp;
49 if (strlen(ops->prefix) > KRB5_KT_PREFIX_MAX_LEN - 1) {
50 krb5_set_error_message(context, KRB5_KT_BADNAME,
51 N_("can't register cache type, prefix too long", ""));
52 return KRB5_KT_BADNAME;
55 tmp = realloc(context->kt_types,
56 (context->num_kt_types + 1) * sizeof(*context->kt_types));
58 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
61 memcpy(&tmp[context->num_kt_types], ops,
62 sizeof(tmp[context->num_kt_types]));
63 context->kt_types = tmp;
64 context->num_kt_types++;
69 * Resolve the keytab name (of the form `type:residual') in `name'
70 * into a keytab in `id'.
71 * Return 0 or an error
74 krb5_error_code KRB5_LIB_FUNCTION
75 krb5_kt_resolve(krb5_context context,
81 const char *type, *residual;
85 residual = strchr(name, ':');
86 if(residual == NULL) {
88 type_len = strlen(type);
92 type_len = residual - name;
96 for(i = 0; i < context->num_kt_types; i++) {
97 if(strncasecmp(type, context->kt_types[i].prefix, type_len) == 0)
100 if(i == context->num_kt_types) {
101 krb5_set_error_message(context, KRB5_KT_UNKNOWN_TYPE,
102 N_("unknown keytab type %.*s", "type"),
103 (int)type_len, type);
104 return KRB5_KT_UNKNOWN_TYPE;
107 k = malloc (sizeof(*k));
109 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
112 memcpy(k, &context->kt_types[i], sizeof(*k));
114 ret = (*k->resolve)(context, residual, k);
124 * copy the name of the default keytab into `name'.
125 * Return 0 or KRB5_CONFIG_NOTENUFSPACE if `namesize' is too short.
128 krb5_error_code KRB5_LIB_FUNCTION
129 krb5_kt_default_name(krb5_context context, char *name, size_t namesize)
131 if (strlcpy (name, context->default_keytab, namesize) >= namesize) {
132 krb5_clear_error_message (context);
133 return KRB5_CONFIG_NOTENUFSPACE;
139 * copy the name of the default modify keytab into `name'.
140 * Return 0 or KRB5_CONFIG_NOTENUFSPACE if `namesize' is too short.
143 krb5_error_code KRB5_LIB_FUNCTION
144 krb5_kt_default_modify_name(krb5_context context, char *name, size_t namesize)
146 const char *kt = NULL;
147 if(context->default_keytab_modify == NULL) {
148 if(strncasecmp(context->default_keytab, "ANY:", 4) != 0)
149 kt = context->default_keytab;
151 size_t len = strcspn(context->default_keytab + 4, ",");
152 if(len >= namesize) {
153 krb5_clear_error_message(context);
154 return KRB5_CONFIG_NOTENUFSPACE;
156 strlcpy(name, context->default_keytab + 4, namesize);
161 kt = context->default_keytab_modify;
162 if (strlcpy (name, kt, namesize) >= namesize) {
163 krb5_clear_error_message (context);
164 return KRB5_CONFIG_NOTENUFSPACE;
170 * Set `id' to the default keytab.
171 * Return 0 or an error.
174 krb5_error_code KRB5_LIB_FUNCTION
175 krb5_kt_default(krb5_context context, krb5_keytab *id)
177 return krb5_kt_resolve (context, context->default_keytab, id);
181 * Read the key identified by `(principal, vno, enctype)' from the
182 * keytab in `keyprocarg' (the default if == NULL) into `*key'.
183 * Return 0 or an error.
186 krb5_error_code KRB5_LIB_FUNCTION
187 krb5_kt_read_service_key(krb5_context context,
188 krb5_pointer keyprocarg,
189 krb5_principal principal,
191 krb5_enctype enctype,
195 krb5_keytab_entry entry;
199 ret = krb5_kt_resolve (context, keyprocarg, &keytab);
201 ret = krb5_kt_default (context, &keytab);
206 ret = krb5_kt_get_entry (context, keytab, principal, vno, enctype, &entry);
207 krb5_kt_close (context, keytab);
210 ret = krb5_copy_keyblock (context, &entry.keyblock, key);
211 krb5_kt_free_entry(context, &entry);
216 * Return the type of the `keytab' in the string `prefix of length
220 krb5_error_code KRB5_LIB_FUNCTION
221 krb5_kt_get_type(krb5_context context,
226 strlcpy(prefix, keytab->prefix, prefixsize);
231 * Retrieve the name of the keytab `keytab' into `name', `namesize'
232 * Return 0 or an error.
235 krb5_error_code KRB5_LIB_FUNCTION
236 krb5_kt_get_name(krb5_context context,
241 return (*keytab->get_name)(context, keytab, name, namesize);
245 * Retrieve the full name of the keytab `keytab' and store the name in
246 * `str'. `str' needs to be freed by the caller using free(3).
247 * Returns 0 or an error. On error, *str is set to NULL.
250 krb5_error_code KRB5_LIB_FUNCTION
251 krb5_kt_get_full_name(krb5_context context,
255 char type[KRB5_KT_PREFIX_MAX_LEN];
256 char name[MAXPATHLEN];
261 ret = krb5_kt_get_type(context, keytab, type, sizeof(type));
265 ret = krb5_kt_get_name(context, keytab, name, sizeof(name));
269 if (asprintf(str, "%s:%s", type, name) == -1) {
270 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
279 * Finish using the keytab in `id'. All resources will be released,
280 * even on errors. Return 0 or an error.
283 krb5_error_code KRB5_LIB_FUNCTION
284 krb5_kt_close(krb5_context context,
289 ret = (*id->close)(context, id);
290 memset(id, 0, sizeof(*id));
296 * Compare `entry' against `principal, vno, enctype'.
297 * Any of `principal, vno, enctype' might be 0 which acts as a wildcard.
298 * Return TRUE if they compare the same, FALSE otherwise.
301 krb5_boolean KRB5_LIB_FUNCTION
302 krb5_kt_compare(krb5_context context,
303 krb5_keytab_entry *entry,
304 krb5_const_principal principal,
306 krb5_enctype enctype)
308 if(principal != NULL &&
309 !krb5_principal_compare(context, entry->principal, principal))
311 if(vno && vno != entry->vno)
313 if(enctype && enctype != entry->keyblock.keytype)
319 * Retrieve the keytab entry for `principal, kvno, enctype' into `entry'
320 * from the keytab `id'.
321 * kvno == 0 is a wildcard and gives the keytab with the highest vno.
322 * Return 0 or an error.
325 krb5_error_code KRB5_LIB_FUNCTION
326 krb5_kt_get_entry(krb5_context context,
328 krb5_const_principal principal,
330 krb5_enctype enctype,
331 krb5_keytab_entry *entry)
333 krb5_keytab_entry tmp;
335 krb5_kt_cursor cursor;
338 return (*id->get)(context, id, principal, kvno, enctype, entry);
340 ret = krb5_kt_start_seq_get (context, id, &cursor);
342 /* This is needed for krb5_verify_init_creds, but keep error
343 * string from previous error for the human. */
344 context->error_code = KRB5_KT_NOTFOUND;
345 return KRB5_KT_NOTFOUND;
349 while (krb5_kt_next_entry(context, id, &tmp, &cursor) == 0) {
350 if (krb5_kt_compare(context, &tmp, principal, 0, enctype)) {
351 /* the file keytab might only store the lower 8 bits of
352 the kvno, so only compare those bits */
354 || (tmp.vno < 256 && kvno % 256 == tmp.vno)) {
355 krb5_kt_copy_entry_contents (context, &tmp, entry);
356 krb5_kt_free_entry (context, &tmp);
357 krb5_kt_end_seq_get(context, id, &cursor);
359 } else if (kvno == 0 && tmp.vno > entry->vno) {
361 krb5_kt_free_entry (context, entry);
362 krb5_kt_copy_entry_contents (context, &tmp, entry);
365 krb5_kt_free_entry(context, &tmp);
367 krb5_kt_end_seq_get (context, id, &cursor);
371 char princ[256], kvno_str[25], *kt_name;
372 char *enctype_str = NULL;
374 krb5_unparse_name_fixed (context, principal, princ, sizeof(princ));
375 krb5_kt_get_full_name (context, id, &kt_name);
376 krb5_enctype_to_string(context, enctype, &enctype_str);
379 snprintf(kvno_str, sizeof(kvno_str), "(kvno %d)", kvno);
383 krb5_set_error_message (context, KRB5_KT_NOTFOUND,
384 N_("Failed to find %s%s in keytab %s (%s)",
385 "principal, kvno, keytab file, enctype"),
388 kt_name ? kt_name : "unknown keytab",
389 enctype_str ? enctype_str : "unknown enctype");
392 return KRB5_KT_NOTFOUND;
397 * Copy the contents of `in' into `out'.
398 * Return 0 or an error. */
400 krb5_error_code KRB5_LIB_FUNCTION
401 krb5_kt_copy_entry_contents(krb5_context context,
402 const krb5_keytab_entry *in,
403 krb5_keytab_entry *out)
407 memset(out, 0, sizeof(*out));
410 ret = krb5_copy_principal (context, in->principal, &out->principal);
413 ret = krb5_copy_keyblock_contents (context,
418 out->timestamp = in->timestamp;
421 krb5_kt_free_entry (context, out);
426 * Free the contents of `entry'.
429 krb5_error_code KRB5_LIB_FUNCTION
430 krb5_kt_free_entry(krb5_context context,
431 krb5_keytab_entry *entry)
433 krb5_free_principal (context, entry->principal);
434 krb5_free_keyblock_contents (context, &entry->keyblock);
435 memset(entry, 0, sizeof(*entry));
440 * Set `cursor' to point at the beginning of `id'.
441 * Return 0 or an error.
444 krb5_error_code KRB5_LIB_FUNCTION
445 krb5_kt_start_seq_get(krb5_context context,
447 krb5_kt_cursor *cursor)
449 if(id->start_seq_get == NULL) {
450 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
451 N_("start_seq_get is not supported "
452 "in the %s keytab type", ""),
454 return HEIM_ERR_OPNOTSUPP;
456 return (*id->start_seq_get)(context, id, cursor);
460 * Get the next entry from `id' pointed to by `cursor' and advance the
462 * Return 0 or an error.
465 krb5_error_code KRB5_LIB_FUNCTION
466 krb5_kt_next_entry(krb5_context context,
468 krb5_keytab_entry *entry,
469 krb5_kt_cursor *cursor)
471 if(id->next_entry == NULL) {
472 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
473 N_("next_entry is not supported in the %s "
476 return HEIM_ERR_OPNOTSUPP;
478 return (*id->next_entry)(context, id, entry, cursor);
482 * Release all resources associated with `cursor'.
485 krb5_error_code KRB5_LIB_FUNCTION
486 krb5_kt_end_seq_get(krb5_context context,
488 krb5_kt_cursor *cursor)
490 if(id->end_seq_get == NULL) {
491 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
492 "end_seq_get is not supported in the %s "
493 " keytab", id->prefix);
494 return HEIM_ERR_OPNOTSUPP;
496 return (*id->end_seq_get)(context, id, cursor);
500 * Add the entry in `entry' to the keytab `id'.
501 * Return 0 or an error.
504 krb5_error_code KRB5_LIB_FUNCTION
505 krb5_kt_add_entry(krb5_context context,
507 krb5_keytab_entry *entry)
509 if(id->add == NULL) {
510 krb5_set_error_message(context, KRB5_KT_NOWRITE,
511 N_("Add is not supported in the %s keytab", ""),
513 return KRB5_KT_NOWRITE;
515 entry->timestamp = time(NULL);
516 return (*id->add)(context, id,entry);
520 * Remove the entry `entry' from the keytab `id'.
521 * Return 0 or an error.
524 krb5_error_code KRB5_LIB_FUNCTION
525 krb5_kt_remove_entry(krb5_context context,
527 krb5_keytab_entry *entry)
529 if(id->remove == NULL) {
530 krb5_set_error_message(context, KRB5_KT_NOWRITE,
531 N_("Remove is not supported in the %s keytab", ""),
533 return KRB5_KT_NOWRITE;
535 return (*id->remove)(context, id, entry);