2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
10 * kadmin/ktutil/ktutil_funcs.c
12 *(C) Copyright 1995, 1996 by the Massachusetts Institute of Technology.
13 * All Rights Reserved.
15 * Export of this software from the United States of America may
16 * require a specific license from the United States Government.
17 * It is the responsibility of any person or organization contemplating
18 * export to obtain such a license before exporting.
20 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
21 * distribute this software and its documentation for any purpose and
22 * without fee is hereby granted, provided that the above copyright
23 * notice appear in all copies and that both that copyright notice and
24 * this permission notice appear in supporting documentation, and that
25 * the name of M.I.T. not be used in advertising or publicity pertaining
26 * to distribution of the software without specific, written prior
27 * permission. Furthermore if you modify this software you must label
28 * your software as modified software and not distribute it in such a
29 * fashion that it might be confused with the original M.I.T. software.
30 * M.I.T. makes no representations about the suitability of
31 * this software for any purpose. It is provided "as is" without express
32 * or implied warranty.
34 * Utility functions for ktutil.
39 #ifdef KRB5_KRB4_COMPAT
40 #include "kerberosIV/krb.h"
50 krb5_error_code
ktutil_free_kt_list(context
, list
)
54 krb5_kt_list lp
, prev
;
55 krb5_error_code retval
= 0;
57 for (lp
= list
; lp
;) {
58 retval
= krb5_kt_free_entry(context
, lp
->entry
);
59 free((char *)lp
->entry
);
70 * Delete a numbered entry in a kt_list. Takes a pointer to a kt_list
71 * in case head gets deleted.
73 krb5_error_code
ktutil_delete(context
, list
, idx
)
78 krb5_kt_list lp
, prev
;
81 for (lp
= *list
, i
= 1; lp
; prev
= lp
, lp
= lp
->next
, i
++) {
86 prev
->next
= lp
->next
;
88 return ktutil_free_kt_list(context
, lp
);
95 * Create a new keytab entry and add it to the keytab list.
96 * Based on the value of use_pass, either prompt the user for a
97 * password or key. If the keytab list is NULL, allocate a new
100 krb5_error_code
ktutil_add(context
, list
, princ_str
, kvno
,
101 enctype_str
, use_pass
)
102 krb5_context context
;
109 krb5_keytab_entry
*entry
;
110 krb5_kt_list lp
= NULL
, prev
= NULL
;
111 krb5_principal princ
;
112 krb5_enctype enctype
;
114 krb5_error_code retval
;
115 krb5_data password
, salt
;
118 char promptstr
[1024];
122 unsigned int pwsize
= BUFSIZ
;
124 retval
= krb5_parse_name(context
, princ_str
, &princ
);
127 /* now unparse in order to get the default realm appended
128 to princ_str, if no realm was specified */
129 retval
= krb5_unparse_name(context
, princ
, &princ_str
);
132 retval
= krb5_string_to_enctype(enctype_str
, &enctype
);
134 return KRB5_BAD_ENCTYPE
;
135 retval
= krb5_timeofday(context
, &now
);
140 /* point lp at the tail of the list */
141 for (lp
= *list
; lp
->next
; lp
= lp
->next
);
143 entry
= (krb5_keytab_entry
*) malloc(sizeof(krb5_keytab_entry
));
147 memset((char *) entry
, 0, sizeof(*entry
));
149 if (!lp
) { /* if list is empty, start one */
150 lp
= (krb5_kt_list
) malloc(sizeof(*lp
));
155 lp
->next
= (krb5_kt_list
) malloc(sizeof(*lp
));
166 password
.length
= pwsize
;
167 password
.data
= (char *) malloc(pwsize
);
168 if (!password
.data
) {
173 (void) snprintf(promptstr
, sizeof(promptstr
),
174 gettext("Password for %.1000s"), princ_str
);
175 retval
= krb5_read_password(context
, promptstr
, NULL
, password
.data
,
179 retval
= krb5_principal2salt(context
, princ
, &salt
);
182 retval
= krb5_c_string_to_key(context
, enctype
, &password
,
186 memset(password
.data
, 0, password
.length
);
188 memcpy(&lp
->entry
->key
, &key
, sizeof(krb5_keyblock
));
190 printf(gettext("Key for %s (hex): "), princ_str
);
191 fgets(buf
, BUFSIZ
, stdin
);
193 * We need to get rid of the trailing '\n' from fgets.
194 * If we have an even number of hex digits (as we should),
195 * write a '\0' over the '\n'. If for some reason we have
196 * an odd number of hex digits, force an even number of hex
197 * digits by writing a '0' into the last position (the string
198 * will still be null-terminated).
200 buf
[strlen(buf
) - 1] = strlen(buf
) % 2 ? '\0' : '0';
201 if (strlen(buf
) == 0) {
202 fprintf(stderr
, "addent: %s", gettext("Error reading key.\n"));
207 lp
->entry
->key
.enctype
= enctype
;
208 lp
->entry
->key
.contents
= (krb5_octet
*) malloc((strlen(buf
) + 1) / 2);
209 if (!lp
->entry
->key
.contents
) {
215 for (cp
= buf
; *cp
; cp
+= 2) {
216 if (!isxdigit((int) cp
[0]) || !isxdigit((int) cp
[1])) {
217 fprintf(stderr
, "addent: %s",
218 gettext("Illegal character in key.\n"));
222 sscanf(cp
, "%02x", &tmp
);
223 lp
->entry
->key
.contents
[i
++] = (krb5_octet
) tmp
;
225 lp
->entry
->key
.length
= i
;
227 lp
->entry
->principal
= princ
;
228 lp
->entry
->vno
= kvno
;
229 lp
->entry
->timestamp
= now
;
239 ktutil_free_kt_list(context
, lp
);
244 * Read in a keytab and append it to list. If list starts as NULL,
245 * allocate a new one if necessary.
247 krb5_error_code
ktutil_read_keytab(context
, name
, list
)
248 krb5_context context
;
252 krb5_kt_list lp
= NULL
, tail
= NULL
, back
= NULL
;
254 krb5_keytab_entry
*entry
;
255 krb5_kt_cursor cursor
;
256 krb5_error_code retval
= 0;
259 /* point lp at the tail of the list */
260 for (lp
= *list
; lp
->next
; lp
= lp
->next
);
263 retval
= krb5_kt_resolve(context
, name
, &kt
);
266 retval
= krb5_kt_start_seq_get(context
, kt
, &cursor
);
270 entry
= (krb5_keytab_entry
*)malloc(sizeof (krb5_keytab_entry
));
275 memset((char *)entry
, 0, sizeof (*entry
));
276 retval
= krb5_kt_next_entry(context
, kt
, entry
, &cursor
);
280 if (!lp
) { /* if list is empty, start one */
281 lp
= (krb5_kt_list
)malloc(sizeof (*lp
));
287 lp
->next
= (krb5_kt_list
)malloc(sizeof (*lp
));
302 if (retval
== KRB5_KT_END
)
305 ktutil_free_kt_list(context
, tail
);
313 krb5_kt_end_seq_get(context
, kt
, &cursor
);
315 krb5_kt_close(context
, kt
);
320 * Takes a kt_list and writes it to the named keytab.
322 krb5_error_code
ktutil_write_keytab(context
, list
, name
)
323 krb5_context context
;
329 char ktname
[MAXPATHLEN
+sizeof("WRFILE:")+1];
330 krb5_error_code retval
= 0;
332 strcpy(ktname
, "WRFILE:");
333 if (strlen (name
) >= MAXPATHLEN
)
335 strncat (ktname
, name
, MAXPATHLEN
);
336 retval
= krb5_kt_resolve(context
, ktname
, &kt
);
339 for (lp
= list
; lp
; lp
= lp
->next
) {
340 retval
= krb5_kt_add_entry(context
, kt
, lp
->entry
);
344 krb5_kt_close(context
, kt
);
348 #ifdef KRB5_KRB4_COMPAT
350 * getstr() takes a file pointer, a string and a count. It reads from
351 * the file until either it has read "count" characters, or until it
352 * reads a null byte. When finished, what has been read exists in the
353 * given string "s". If "count" characters were actually read, the
354 * last is changed to a null, so the returned string is always null-
355 * terminated. getstr() returns the number of characters read,
356 * including the null terminator.
359 static int getstr(fp
, s
, n
)
364 register int count
= n
;
365 while (fread(s
, 1, 1, fp
) > 0 && --count
)
373 * Read in a named krb4 srvtab and append to list. Allocate new list
376 krb5_error_code
ktutil_read_srvtab(context
, name
, list
)
377 krb5_context context
;
381 krb5_kt_list lp
= NULL
, tail
= NULL
, back
= NULL
;
382 krb5_keytab_entry
*entry
;
383 krb5_error_code retval
= 0;
384 char sname
[SNAME_SZ
]; /* name of service */
385 char sinst
[INST_SZ
]; /* instance of service */
386 char srealm
[REALM_SZ
]; /* realm of service */
387 unsigned char kvno
; /* key version number */
392 /* point lp at the tail of the list */
393 for (lp
= *list
; lp
->next
; lp
= lp
->next
);
396 fp
= fopen(name
, "r");
400 entry
= (krb5_keytab_entry
*)malloc(sizeof (krb5_keytab_entry
));
405 memset((char *)entry
, 0, sizeof (*entry
));
406 memset(sname
, 0, sizeof (sname
));
407 memset(sinst
, 0, sizeof (sinst
));
408 memset(srealm
, 0, sizeof (srealm
));
409 if (!(getstr(fp
, sname
, SNAME_SZ
) > 0 &&
410 getstr(fp
, sinst
, INST_SZ
) > 0 &&
411 getstr(fp
, srealm
, REALM_SZ
) > 0 &&
412 fread(&kvno
, 1, 1, fp
) > 0 &&
413 fread((char *)key
, sizeof (key
), 1, fp
) > 0))
415 entry
->magic
= KV5M_KEYTAB_ENTRY
;
416 entry
->timestamp
= 0; /* XXX */
418 retval
= krb5_425_conv_principal(context
,
419 sname
, sinst
, srealm
,
423 entry
->key
.magic
= KV5M_KEYBLOCK
;
424 entry
->key
.enctype
= ENCTYPE_DES_CBC_CRC
;
425 entry
->key
.length
= sizeof (key
);
426 entry
->key
.contents
= (krb5_octet
*)malloc(sizeof (key
));
427 if (!entry
->key
.contents
) {
431 memcpy((char *)entry
->key
.contents
, (char *)key
, sizeof (key
));
432 if (!lp
) { /* if list is empty, start one */
433 lp
= (krb5_kt_list
)malloc(sizeof (*lp
));
439 lp
->next
= (krb5_kt_list
)malloc(sizeof (*lp
));
452 if (entry
->magic
== KV5M_KEYTAB_ENTRY
)
453 krb5_kt_free_entry(context
, entry
);
457 ktutil_free_kt_list(context
, tail
);
469 * Writes a kt_list out to a krb4 srvtab file. Note that it first
470 * prunes the kt_list so that it won't contain any keys that are not
471 * the most recent, and ignores keys that are not ENCTYPE_DES.
473 krb5_error_code
ktutil_write_srvtab(context
, list
, name
)
474 krb5_context context
;
478 krb5_kt_list lp
, lp1
, prev
, pruned
= NULL
;
479 krb5_error_code retval
= 0;
481 char sname
[SNAME_SZ
];
483 char srealm
[REALM_SZ
];
485 /* First do heinous stuff to prune the list. */
486 for (lp
= list
; lp
; lp
= lp
->next
) {
487 if ((lp
->entry
->key
.enctype
!= ENCTYPE_DES_CBC_CRC
) &&
488 (lp
->entry
->key
.enctype
!= ENCTYPE_DES_CBC_MD5
) &&
489 (lp
->entry
->key
.enctype
!= ENCTYPE_DES_CBC_MD4
) &&
490 (lp
->entry
->key
.enctype
!= ENCTYPE_DES_CBC_RAW
))
493 for (lp1
= pruned
; lp1
; prev
= lp1
, lp1
= lp1
->next
) {
494 /* Hunt for the current principal in the pruned list */
495 if (krb5_principal_compare(context
,
496 lp
->entry
->principal
,
497 lp1
->entry
->principal
))
500 if (!lp1
) { /* need to add entry to tail of pruned list */
502 pruned
= (krb5_kt_list
) malloc(sizeof (*pruned
));
505 memset((char *) pruned
, 0, sizeof(*pruned
));
509 = (krb5_kt_list
) malloc(sizeof (*pruned
));
514 memset((char *) prev
->next
, 0, sizeof(*pruned
));
517 lp1
->entry
= lp
->entry
;
519 /* This heuristic should be roughly the same as in the
520 keytab-reading code in libkrb5. */
522 if (lp1
->entry
->vno
> 240 || lp
->entry
->vno
> 240) {
525 #define M(X) (((X) + offset) % 256)
526 if (M(lp1
->entry
->vno
) < M(lp
->entry
->vno
))
527 /* Check if lp->entry is newer kvno; if so, update */
528 lp1
->entry
= lp
->entry
;
531 umask(0077); /*Changing umask for all of ktutil is OK
532 * We don't ever write out anything that should use
534 fp
= fopen(name
, "w");
539 for (lp
= pruned
; lp
; lp
= lp
->next
) {
541 kvno
= (unsigned char) lp
->entry
->vno
;
542 retval
= krb5_524_conv_principal(context
,
543 lp
->entry
->principal
,
544 sname
, sinst
, srealm
);
547 fwrite(sname
, strlen(sname
) + 1, 1, fp
);
548 fwrite(sinst
, strlen(sinst
) + 1, 1, fp
);
549 fwrite(srealm
, strlen(srealm
) + 1, 1, fp
);
550 fwrite((char *)&kvno
, 1, 1, fp
);
551 fwrite((char *)lp
->entry
->key
.contents
,
552 sizeof (des_cblock
), 1, fp
);
557 * Loop over and free the pruned list; don't use free_kt_list
558 * because that kills the entries.
560 for (lp
= pruned
; lp
;) {
567 #endif /* KRB5_KRB4_COMPAT */