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