xref: /onnv-gate/usr/src/cmd/krb5/kadmin/ktutil/ktutil_funcs.c (revision 2881:ea6360e7e1c5)
10Sstevel@tonic-gate /*
21692Ssemery  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate 
8*2881Smp153739 
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate  * kadmin/ktutil/ktutil_funcs.c
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  *(C) Copyright 1995, 1996 by the Massachusetts Institute of Technology.
130Sstevel@tonic-gate  * All Rights Reserved.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * Export of this software from the United States of America may
160Sstevel@tonic-gate  *   require a specific license from the United States Government.
170Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
180Sstevel@tonic-gate  *   export to obtain such a license before exporting.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
210Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
220Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
230Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
240Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
250Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
260Sstevel@tonic-gate  * to distribution of the software without specific, written prior
270Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
280Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
290Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
300Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
310Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
320Sstevel@tonic-gate  * or implied warranty.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * Utility functions for ktutil.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include "k5-int.h"
380Sstevel@tonic-gate #include "ktutil.h"
390Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
400Sstevel@tonic-gate #include "kerberosIV/krb.h"
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #endif
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <ctype.h>
450Sstevel@tonic-gate #include <libintl.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Free a kt_list
490Sstevel@tonic-gate  */
ktutil_free_kt_list(context,list)500Sstevel@tonic-gate krb5_error_code ktutil_free_kt_list(context, list)
510Sstevel@tonic-gate     krb5_context context;
520Sstevel@tonic-gate     krb5_kt_list list;
530Sstevel@tonic-gate {
540Sstevel@tonic-gate     krb5_kt_list lp, prev;
550Sstevel@tonic-gate     krb5_error_code retval = 0;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate     for (lp = list; lp;) {
580Sstevel@tonic-gate 	retval = krb5_kt_free_entry(context, lp->entry);
590Sstevel@tonic-gate 	free((char *)lp->entry);
600Sstevel@tonic-gate 	if (retval)
610Sstevel@tonic-gate 	    break;
620Sstevel@tonic-gate 	prev = lp;
630Sstevel@tonic-gate 	lp = lp->next;
640Sstevel@tonic-gate 	free((char *)prev);
650Sstevel@tonic-gate     }
660Sstevel@tonic-gate     return retval;
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * Delete a numbered entry in a kt_list.  Takes a pointer to a kt_list
710Sstevel@tonic-gate  * in case head gets deleted.
720Sstevel@tonic-gate  */
ktutil_delete(context,list,idx)73*2881Smp153739 krb5_error_code ktutil_delete(context, list, idx)
740Sstevel@tonic-gate     krb5_context context;
750Sstevel@tonic-gate     krb5_kt_list *list;
76*2881Smp153739     int idx;
770Sstevel@tonic-gate {
780Sstevel@tonic-gate     krb5_kt_list lp, prev;
790Sstevel@tonic-gate     int i;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate     for (lp = *list, i = 1; lp; prev = lp, lp = lp->next, i++) {
82*2881Smp153739 	if (i == idx) {
830Sstevel@tonic-gate 	    if (i == 1)
840Sstevel@tonic-gate 		*list = lp->next;
850Sstevel@tonic-gate 	    else
860Sstevel@tonic-gate 		prev->next = lp->next;
870Sstevel@tonic-gate 	    lp->next = NULL;
880Sstevel@tonic-gate 	    return ktutil_free_kt_list(context, lp);
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate     }
910Sstevel@tonic-gate     return EINVAL;
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * Create a new keytab entry and add it to the keytab list.
960Sstevel@tonic-gate  * Based on the value of use_pass, either prompt the user for a
970Sstevel@tonic-gate  * password or key.  If the keytab list is NULL, allocate a new
980Sstevel@tonic-gate  * one first.
990Sstevel@tonic-gate  */
ktutil_add(context,list,princ_str,kvno,enctype_str,use_pass)1000Sstevel@tonic-gate krb5_error_code ktutil_add(context, list, princ_str, kvno,
1010Sstevel@tonic-gate 			   enctype_str, use_pass)
1020Sstevel@tonic-gate     krb5_context context;
1030Sstevel@tonic-gate     krb5_kt_list *list;
1040Sstevel@tonic-gate     char *princ_str;
1050Sstevel@tonic-gate     krb5_kvno kvno;
1060Sstevel@tonic-gate     char *enctype_str;
1070Sstevel@tonic-gate     int use_pass;
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate     krb5_keytab_entry *entry;
1100Sstevel@tonic-gate     krb5_kt_list lp = NULL, prev = NULL;
1110Sstevel@tonic-gate     krb5_principal princ;
1120Sstevel@tonic-gate     krb5_enctype enctype;
1130Sstevel@tonic-gate     krb5_timestamp now;
1140Sstevel@tonic-gate     krb5_error_code retval;
1150Sstevel@tonic-gate     krb5_data password, salt;
1160Sstevel@tonic-gate     krb5_keyblock key;
1170Sstevel@tonic-gate     char buf[BUFSIZ];
1180Sstevel@tonic-gate     char promptstr[1024];
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate     char *cp;
121*2881Smp153739     int i, tmp;
122*2881Smp153739     unsigned int pwsize = BUFSIZ;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate     retval = krb5_parse_name(context, princ_str, &princ);
1250Sstevel@tonic-gate     if (retval)
1260Sstevel@tonic-gate         return retval;
1270Sstevel@tonic-gate     /* now unparse in order to get the default realm appended
1280Sstevel@tonic-gate        to princ_str, if no realm was specified */
1290Sstevel@tonic-gate     retval = krb5_unparse_name(context, princ, &princ_str);
1300Sstevel@tonic-gate     if (retval)
1310Sstevel@tonic-gate         return retval;
1320Sstevel@tonic-gate     retval = krb5_string_to_enctype(enctype_str, &enctype);
1330Sstevel@tonic-gate     if (retval)
1340Sstevel@tonic-gate         return KRB5_BAD_ENCTYPE;
1350Sstevel@tonic-gate     retval = krb5_timeofday(context, &now);
1360Sstevel@tonic-gate     if (retval)
1370Sstevel@tonic-gate         return retval;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate     if (*list) {
1400Sstevel@tonic-gate         /* point lp at the tail of the list */
1410Sstevel@tonic-gate         for (lp = *list; lp->next; lp = lp->next);
1420Sstevel@tonic-gate     }
1430Sstevel@tonic-gate     entry = (krb5_keytab_entry *) malloc(sizeof(krb5_keytab_entry));
1440Sstevel@tonic-gate     if (!entry) {
1450Sstevel@tonic-gate         return ENOMEM;
1460Sstevel@tonic-gate     }
1470Sstevel@tonic-gate     memset((char *) entry, 0, sizeof(*entry));
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate     if (!lp) {		/* if list is empty, start one */
1501795Smp153739         lp = (krb5_kt_list) malloc(sizeof(*lp));
1510Sstevel@tonic-gate 	if (!lp) {
1520Sstevel@tonic-gate 	    return ENOMEM;
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate     } else {
1551795Smp153739         lp->next = (krb5_kt_list) malloc(sizeof(*lp));
1560Sstevel@tonic-gate 	if (!lp->next) {
1570Sstevel@tonic-gate 	    return ENOMEM;
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 	prev = lp;
1600Sstevel@tonic-gate 	lp = lp->next;
1610Sstevel@tonic-gate     }
1620Sstevel@tonic-gate     lp->next = NULL;
1630Sstevel@tonic-gate     lp->entry = entry;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate     if (use_pass) {
1660Sstevel@tonic-gate         password.length = pwsize;
1670Sstevel@tonic-gate 	password.data = (char *) malloc(pwsize);
1680Sstevel@tonic-gate 	if (!password.data) {
1690Sstevel@tonic-gate 	    retval = ENOMEM;
1700Sstevel@tonic-gate 	    goto cleanup;
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	(void) snprintf(promptstr, sizeof(promptstr),
1741692Ssemery 		gettext("Password for %.1000s"), princ_str);
1750Sstevel@tonic-gate         retval = krb5_read_password(context, promptstr, NULL, password.data,
1760Sstevel@tonic-gate 				    &password.length);
1770Sstevel@tonic-gate 	if (retval)
1780Sstevel@tonic-gate 	    goto cleanup;
1790Sstevel@tonic-gate 	retval = krb5_principal2salt(context, princ, &salt);
1800Sstevel@tonic-gate 	if (retval)
1810Sstevel@tonic-gate 	    goto cleanup;
1820Sstevel@tonic-gate 	retval = krb5_c_string_to_key(context, enctype, &password,
1830Sstevel@tonic-gate 				      &salt, &key);
1840Sstevel@tonic-gate 	if (retval)
1850Sstevel@tonic-gate 	    goto cleanup;
1860Sstevel@tonic-gate 	memset(password.data, 0, password.length);
1870Sstevel@tonic-gate 	password.length = 0;
1880Sstevel@tonic-gate 	memcpy(&lp->entry->key, &key, sizeof(krb5_keyblock));
1890Sstevel@tonic-gate     } else {
1900Sstevel@tonic-gate         printf(gettext("Key for %s (hex): "), princ_str);
1910Sstevel@tonic-gate 	fgets(buf, BUFSIZ, stdin);
1920Sstevel@tonic-gate 	/*
1930Sstevel@tonic-gate 	 * We need to get rid of the trailing '\n' from fgets.
1940Sstevel@tonic-gate 	 * If we have an even number of hex digits (as we should),
1950Sstevel@tonic-gate 	 * write a '\0' over the '\n'.  If for some reason we have
1960Sstevel@tonic-gate 	 * an odd number of hex digits, force an even number of hex
1970Sstevel@tonic-gate 	 * digits by writing a '0' into the last position (the string
1980Sstevel@tonic-gate 	 * will still be null-terminated).
1990Sstevel@tonic-gate 	 */
2000Sstevel@tonic-gate 	buf[strlen(buf) - 1] = strlen(buf) % 2 ? '\0' : '0';
2010Sstevel@tonic-gate 	if (strlen(buf) == 0) {
2020Sstevel@tonic-gate 	    fprintf(stderr, "addent: %s", gettext("Error reading key.\n"));
2030Sstevel@tonic-gate 	    retval = 0;
2040Sstevel@tonic-gate 	    goto cleanup;
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate         lp->entry->key.enctype = enctype;
2080Sstevel@tonic-gate 	lp->entry->key.contents = (krb5_octet *) malloc((strlen(buf) + 1) / 2);
2090Sstevel@tonic-gate 	if (!lp->entry->key.contents) {
2100Sstevel@tonic-gate 	    retval = ENOMEM;
2110Sstevel@tonic-gate 	    goto cleanup;
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	i = 0;
2150Sstevel@tonic-gate 	for (cp = buf; *cp; cp += 2) {
216*2881Smp153739 	    if (!isxdigit((int) cp[0]) || !isxdigit((int) cp[1])) {
2170Sstevel@tonic-gate 	        fprintf(stderr, "addent: %s",
2180Sstevel@tonic-gate 			gettext("Illegal character in key.\n"));
2190Sstevel@tonic-gate 		retval = 0;
2200Sstevel@tonic-gate 		goto cleanup;
2210Sstevel@tonic-gate 	    }
2220Sstevel@tonic-gate 	    sscanf(cp, "%02x", &tmp);
2230Sstevel@tonic-gate 	    lp->entry->key.contents[i++] = (krb5_octet) tmp;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	lp->entry->key.length = i;
2260Sstevel@tonic-gate     }
2270Sstevel@tonic-gate     lp->entry->principal = princ;
2280Sstevel@tonic-gate     lp->entry->vno = kvno;
2290Sstevel@tonic-gate     lp->entry->timestamp = now;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate     if (!*list)
2320Sstevel@tonic-gate 	*list = lp;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate     return 0;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate  cleanup:
2370Sstevel@tonic-gate     if (prev)
2380Sstevel@tonic-gate         prev->next = NULL;
2390Sstevel@tonic-gate     ktutil_free_kt_list(context, lp);
2400Sstevel@tonic-gate     return retval;
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * Read in a keytab and append it to list.  If list starts as NULL,
2450Sstevel@tonic-gate  * allocate a new one if necessary.
2460Sstevel@tonic-gate  */
ktutil_read_keytab(context,name,list)2470Sstevel@tonic-gate krb5_error_code ktutil_read_keytab(context, name, list)
2480Sstevel@tonic-gate     krb5_context context;
2490Sstevel@tonic-gate     char *name;
2500Sstevel@tonic-gate     krb5_kt_list *list;
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate     krb5_kt_list lp = NULL, tail = NULL, back = NULL;
2530Sstevel@tonic-gate     krb5_keytab kt;
2540Sstevel@tonic-gate     krb5_keytab_entry *entry;
2550Sstevel@tonic-gate     krb5_kt_cursor cursor;
2560Sstevel@tonic-gate     krb5_error_code retval = 0;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate     if (*list) {
2590Sstevel@tonic-gate 	/* point lp at the tail of the list */
2600Sstevel@tonic-gate 	for (lp = *list; lp->next; lp = lp->next);
2610Sstevel@tonic-gate 	back = lp;
2620Sstevel@tonic-gate     }
2630Sstevel@tonic-gate     retval = krb5_kt_resolve(context, name, &kt);
2640Sstevel@tonic-gate     if (retval)
2650Sstevel@tonic-gate 	return retval;
2660Sstevel@tonic-gate     retval = krb5_kt_start_seq_get(context, kt, &cursor);
2670Sstevel@tonic-gate     if (retval)
2680Sstevel@tonic-gate 	goto close_kt;
2690Sstevel@tonic-gate     for (;;) {
2700Sstevel@tonic-gate 	entry = (krb5_keytab_entry *)malloc(sizeof (krb5_keytab_entry));
2710Sstevel@tonic-gate 	if (!entry) {
2720Sstevel@tonic-gate 	    retval = ENOMEM;
2730Sstevel@tonic-gate 	    break;
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 	memset((char *)entry, 0, sizeof (*entry));
2760Sstevel@tonic-gate 	retval = krb5_kt_next_entry(context, kt, entry, &cursor);
2770Sstevel@tonic-gate 	if (retval)
2780Sstevel@tonic-gate 	    break;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if (!lp) {		/* if list is empty, start one */
2810Sstevel@tonic-gate 	    lp = (krb5_kt_list)malloc(sizeof (*lp));
2820Sstevel@tonic-gate 	    if (!lp) {
2830Sstevel@tonic-gate 		retval = ENOMEM;
2840Sstevel@tonic-gate 		break;
2850Sstevel@tonic-gate 	    }
2860Sstevel@tonic-gate 	} else {
2870Sstevel@tonic-gate 	    lp->next = (krb5_kt_list)malloc(sizeof (*lp));
2880Sstevel@tonic-gate 	    if (!lp->next) {
2890Sstevel@tonic-gate 		retval = ENOMEM;
2900Sstevel@tonic-gate 		break;
2910Sstevel@tonic-gate 	    }
2920Sstevel@tonic-gate 	    lp = lp->next;
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 	if (!tail)
2950Sstevel@tonic-gate 	    tail = lp;
2960Sstevel@tonic-gate 	lp->next = NULL;
2970Sstevel@tonic-gate 	lp->entry = entry;
2980Sstevel@tonic-gate     }
2990Sstevel@tonic-gate     if (entry)
3000Sstevel@tonic-gate 	free((char *)entry);
301*2881Smp153739     if (retval) {
3020Sstevel@tonic-gate 	if (retval == KRB5_KT_END)
3030Sstevel@tonic-gate 	    retval = 0;
3040Sstevel@tonic-gate 	else {
3050Sstevel@tonic-gate 	    ktutil_free_kt_list(context, tail);
3060Sstevel@tonic-gate 	    tail = NULL;
3070Sstevel@tonic-gate 	    if (back)
3080Sstevel@tonic-gate 		back->next = NULL;
3090Sstevel@tonic-gate 	}
310*2881Smp153739     }
3110Sstevel@tonic-gate     if (!*list)
3120Sstevel@tonic-gate 	*list = tail;
3130Sstevel@tonic-gate     krb5_kt_end_seq_get(context, kt, &cursor);
3140Sstevel@tonic-gate  close_kt:
3150Sstevel@tonic-gate     krb5_kt_close(context, kt);
3160Sstevel@tonic-gate     return retval;
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate  * Takes a kt_list and writes it to the named keytab.
3210Sstevel@tonic-gate  */
ktutil_write_keytab(context,list,name)3220Sstevel@tonic-gate krb5_error_code ktutil_write_keytab(context, list, name)
3230Sstevel@tonic-gate     krb5_context context;
3240Sstevel@tonic-gate     krb5_kt_list list;
3250Sstevel@tonic-gate     char *name;
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate     krb5_kt_list lp;
3280Sstevel@tonic-gate     krb5_keytab kt;
3290Sstevel@tonic-gate     char ktname[MAXPATHLEN+sizeof("WRFILE:")+1];
3300Sstevel@tonic-gate     krb5_error_code retval = 0;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate     strcpy(ktname, "WRFILE:");
3330Sstevel@tonic-gate     if (strlen (name) >= MAXPATHLEN)
3340Sstevel@tonic-gate 	return ENAMETOOLONG;
3350Sstevel@tonic-gate     strncat (ktname, name, MAXPATHLEN);
3360Sstevel@tonic-gate     retval = krb5_kt_resolve(context, ktname, &kt);
3370Sstevel@tonic-gate     if (retval)
3380Sstevel@tonic-gate 	return retval;
3390Sstevel@tonic-gate     for (lp = list; lp; lp = lp->next) {
3400Sstevel@tonic-gate 	retval = krb5_kt_add_entry(context, kt, lp->entry);
3410Sstevel@tonic-gate 	if (retval)
3420Sstevel@tonic-gate 	    break;
3430Sstevel@tonic-gate     }
3440Sstevel@tonic-gate     krb5_kt_close(context, kt);
3450Sstevel@tonic-gate     return retval;
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * getstr() takes a file pointer, a string and a count.  It reads from
3510Sstevel@tonic-gate  * the file until either it has read "count" characters, or until it
3520Sstevel@tonic-gate  * reads a null byte.  When finished, what has been read exists in the
3530Sstevel@tonic-gate  * given string "s".  If "count" characters were actually read, the
3540Sstevel@tonic-gate  * last is changed to a null, so the returned string is always null-
3550Sstevel@tonic-gate  * terminated.  getstr() returns the number of characters read,
3560Sstevel@tonic-gate  * including the null terminator.
3570Sstevel@tonic-gate  */
3580Sstevel@tonic-gate 
getstr(fp,s,n)359*2881Smp153739 static int getstr(fp, s, n)
3600Sstevel@tonic-gate     FILE *fp;
3610Sstevel@tonic-gate     register char *s;
3620Sstevel@tonic-gate     int n;
3630Sstevel@tonic-gate {
364*2881Smp153739     register int count = n;
3650Sstevel@tonic-gate     while (fread(s, 1, 1, fp) > 0 && --count)
3660Sstevel@tonic-gate         if (*s++ == '\0')
3670Sstevel@tonic-gate             return (n - count);
3680Sstevel@tonic-gate     *s = '\0';
3690Sstevel@tonic-gate     return (n - count);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate /*
3730Sstevel@tonic-gate  * Read in a named krb4 srvtab and append to list.  Allocate new list
3740Sstevel@tonic-gate  * if needed.
3750Sstevel@tonic-gate  */
ktutil_read_srvtab(context,name,list)3760Sstevel@tonic-gate krb5_error_code ktutil_read_srvtab(context, name, list)
3770Sstevel@tonic-gate     krb5_context context;
3780Sstevel@tonic-gate     char *name;
3790Sstevel@tonic-gate     krb5_kt_list *list;
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate     krb5_kt_list lp = NULL, tail = NULL, back = NULL;
3820Sstevel@tonic-gate     krb5_keytab_entry *entry;
3830Sstevel@tonic-gate     krb5_error_code retval = 0;
3840Sstevel@tonic-gate     char sname[SNAME_SZ];	/* name of service */
3850Sstevel@tonic-gate     char sinst[INST_SZ];	/* instance of service */
3860Sstevel@tonic-gate     char srealm[REALM_SZ];	/* realm of service */
3870Sstevel@tonic-gate     unsigned char kvno;		/* key version number */
3880Sstevel@tonic-gate     des_cblock key;
3890Sstevel@tonic-gate     FILE *fp;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate     if (*list) {
3920Sstevel@tonic-gate 	/* point lp at the tail of the list */
3930Sstevel@tonic-gate 	for (lp = *list; lp->next; lp = lp->next);
3940Sstevel@tonic-gate 	back = lp;
3950Sstevel@tonic-gate     }
3960Sstevel@tonic-gate     fp = fopen(name, "r");
3970Sstevel@tonic-gate     if (!fp)
3980Sstevel@tonic-gate 	return EIO;
3990Sstevel@tonic-gate     for (;;) {
4000Sstevel@tonic-gate 	entry = (krb5_keytab_entry *)malloc(sizeof (krb5_keytab_entry));
4010Sstevel@tonic-gate 	if (!entry) {
4020Sstevel@tonic-gate 	    retval = ENOMEM;
4030Sstevel@tonic-gate 	    break;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	memset((char *)entry, 0, sizeof (*entry));
4060Sstevel@tonic-gate 	memset(sname, 0, sizeof (sname));
4070Sstevel@tonic-gate 	memset(sinst, 0, sizeof (sinst));
4080Sstevel@tonic-gate 	memset(srealm, 0, sizeof (srealm));
4090Sstevel@tonic-gate 	if (!(getstr(fp, sname, SNAME_SZ) > 0 &&
4100Sstevel@tonic-gate 	      getstr(fp, sinst, INST_SZ) > 0 &&
4110Sstevel@tonic-gate 	      getstr(fp, srealm, REALM_SZ) > 0 &&
4120Sstevel@tonic-gate 	      fread(&kvno, 1, 1, fp) > 0 &&
4130Sstevel@tonic-gate 	      fread((char *)key, sizeof (key), 1, fp) > 0))
4140Sstevel@tonic-gate 	    break;
4150Sstevel@tonic-gate 	entry->magic = KV5M_KEYTAB_ENTRY;
4160Sstevel@tonic-gate 	entry->timestamp = 0;	/* XXX */
4170Sstevel@tonic-gate 	entry->vno = kvno;
4180Sstevel@tonic-gate 	retval = krb5_425_conv_principal(context,
4190Sstevel@tonic-gate 					 sname, sinst, srealm,
4200Sstevel@tonic-gate 					 &entry->principal);
4210Sstevel@tonic-gate 	if (retval)
4220Sstevel@tonic-gate 	    break;
4230Sstevel@tonic-gate 	entry->key.magic = KV5M_KEYBLOCK;
4240Sstevel@tonic-gate 	entry->key.enctype = ENCTYPE_DES_CBC_CRC;
4250Sstevel@tonic-gate 	entry->key.length = sizeof (key);
4260Sstevel@tonic-gate 	entry->key.contents = (krb5_octet *)malloc(sizeof (key));
4270Sstevel@tonic-gate 	if (!entry->key.contents) {
4280Sstevel@tonic-gate 	    retval = ENOMEM;
4290Sstevel@tonic-gate 	    break;
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 	memcpy((char *)entry->key.contents, (char *)key, sizeof (key));
4320Sstevel@tonic-gate 	if (!lp) {		/* if list is empty, start one */
4330Sstevel@tonic-gate 	    lp = (krb5_kt_list)malloc(sizeof (*lp));
4340Sstevel@tonic-gate 	    if (!lp) {
4350Sstevel@tonic-gate 		retval = ENOMEM;
4360Sstevel@tonic-gate 		break;
4370Sstevel@tonic-gate 	    }
4380Sstevel@tonic-gate 	} else {
4390Sstevel@tonic-gate 	    lp->next = (krb5_kt_list)malloc(sizeof (*lp));
4400Sstevel@tonic-gate 	    if (!lp->next) {
4410Sstevel@tonic-gate 		retval = ENOMEM;
4420Sstevel@tonic-gate 		break;
4430Sstevel@tonic-gate 	    }
4440Sstevel@tonic-gate 	    lp = lp->next;
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 	lp->next = NULL;
4470Sstevel@tonic-gate 	lp->entry = entry;
4480Sstevel@tonic-gate 	if (!tail)
4490Sstevel@tonic-gate 	    tail = lp;
4500Sstevel@tonic-gate     }
4510Sstevel@tonic-gate     if (entry) {
4520Sstevel@tonic-gate 	if (entry->magic == KV5M_KEYTAB_ENTRY)
4530Sstevel@tonic-gate 	    krb5_kt_free_entry(context, entry);
4540Sstevel@tonic-gate 	free((char *)entry);
4550Sstevel@tonic-gate     }
4560Sstevel@tonic-gate     if (retval) {
4570Sstevel@tonic-gate 	ktutil_free_kt_list(context, tail);
4580Sstevel@tonic-gate 	tail = NULL;
4590Sstevel@tonic-gate 	if (back)
4600Sstevel@tonic-gate 	    back->next = NULL;
4610Sstevel@tonic-gate     }
4620Sstevel@tonic-gate     if (!*list)
4630Sstevel@tonic-gate 	*list = tail;
4640Sstevel@tonic-gate     fclose(fp);
4650Sstevel@tonic-gate     return retval;
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate /*
4690Sstevel@tonic-gate  * Writes a kt_list out to a krb4 srvtab file.  Note that it first
4700Sstevel@tonic-gate  * prunes the kt_list so that it won't contain any keys that are not
4710Sstevel@tonic-gate  * the most recent, and ignores keys that are not ENCTYPE_DES.
4720Sstevel@tonic-gate  */
ktutil_write_srvtab(context,list,name)4730Sstevel@tonic-gate krb5_error_code ktutil_write_srvtab(context, list, name)
4740Sstevel@tonic-gate     krb5_context context;
4750Sstevel@tonic-gate     krb5_kt_list list;
4760Sstevel@tonic-gate     char *name;
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate     krb5_kt_list lp, lp1, prev, pruned = NULL;
4790Sstevel@tonic-gate     krb5_error_code retval = 0;
4800Sstevel@tonic-gate     FILE *fp;
4810Sstevel@tonic-gate     char sname[SNAME_SZ];
4820Sstevel@tonic-gate     char sinst[INST_SZ];
4830Sstevel@tonic-gate     char srealm[REALM_SZ];
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate     /* First do heinous stuff to prune the list. */
4860Sstevel@tonic-gate     for (lp = list; lp; lp = lp->next) {
4870Sstevel@tonic-gate 	if ((lp->entry->key.enctype != ENCTYPE_DES_CBC_CRC) &&
4880Sstevel@tonic-gate 	    (lp->entry->key.enctype != ENCTYPE_DES_CBC_MD5) &&
4890Sstevel@tonic-gate 	    (lp->entry->key.enctype != ENCTYPE_DES_CBC_MD4) &&
4900Sstevel@tonic-gate 	    (lp->entry->key.enctype != ENCTYPE_DES_CBC_RAW))
4910Sstevel@tonic-gate 	    continue;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	for (lp1 = pruned; lp1; prev = lp1, lp1 = lp1->next) {
4940Sstevel@tonic-gate 	    /* Hunt for the current principal in the pruned list */
4950Sstevel@tonic-gate 	    if (krb5_principal_compare(context,
4960Sstevel@tonic-gate 				       lp->entry->principal,
4970Sstevel@tonic-gate 				       lp1->entry->principal))
4980Sstevel@tonic-gate 		    break;
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 	if (!lp1) {		/* need to add entry to tail of pruned list */
5010Sstevel@tonic-gate 	    if (!pruned) {
5020Sstevel@tonic-gate 		pruned = (krb5_kt_list) malloc(sizeof (*pruned));
5030Sstevel@tonic-gate 		if (!pruned)
5040Sstevel@tonic-gate 		    return ENOMEM;
5050Sstevel@tonic-gate 		memset((char *) pruned, 0, sizeof(*pruned));
5060Sstevel@tonic-gate 		lp1 = pruned;
5070Sstevel@tonic-gate 	    } else {
5080Sstevel@tonic-gate 		prev->next
5090Sstevel@tonic-gate 		    = (krb5_kt_list) malloc(sizeof (*pruned));
5100Sstevel@tonic-gate 		if (!prev->next) {
5110Sstevel@tonic-gate 		    retval = ENOMEM;
5120Sstevel@tonic-gate 		    goto free_pruned;
5130Sstevel@tonic-gate 		}
5140Sstevel@tonic-gate 		memset((char *) prev->next, 0, sizeof(*pruned));
5150Sstevel@tonic-gate 		lp1 = prev->next;
5160Sstevel@tonic-gate 	    }
5170Sstevel@tonic-gate 	    lp1->entry = lp->entry;
518*2881Smp153739 	} else {
519*2881Smp153739 	    /* This heuristic should be roughly the same as in the
520*2881Smp153739 	       keytab-reading code in libkrb5.  */
521*2881Smp153739 	    int offset = 0;
522*2881Smp153739 	    if (lp1->entry->vno > 240 || lp->entry->vno > 240) {
523*2881Smp153739 		offset = 128;
524*2881Smp153739 	    }
525*2881Smp153739 #define M(X) (((X) + offset) % 256)
526*2881Smp153739 	    if (M(lp1->entry->vno) < M(lp->entry->vno))
527*2881Smp153739 		/* Check if lp->entry is newer kvno; if so, update */
528*2881Smp153739 		lp1->entry = lp->entry;
529*2881Smp153739 	}
5300Sstevel@tonic-gate     }
531*2881Smp153739     umask(0077); /*Changing umask for all of ktutil is OK
532*2881Smp153739 		  * We don't ever write out anything that should use
533*2881Smp153739 		  * default umask.*/
5340Sstevel@tonic-gate     fp = fopen(name, "w");
5350Sstevel@tonic-gate     if (!fp) {
5360Sstevel@tonic-gate 	retval = EIO;
5370Sstevel@tonic-gate 	goto free_pruned;
5380Sstevel@tonic-gate     }
5390Sstevel@tonic-gate     for (lp = pruned; lp; lp = lp->next) {
5400Sstevel@tonic-gate 	unsigned char  kvno;
5410Sstevel@tonic-gate 	kvno = (unsigned char) lp->entry->vno;
5420Sstevel@tonic-gate 	retval = krb5_524_conv_principal(context,
5430Sstevel@tonic-gate 					 lp->entry->principal,
5440Sstevel@tonic-gate 					 sname, sinst, srealm);
5450Sstevel@tonic-gate 	if (retval)
5460Sstevel@tonic-gate 	    break;
5470Sstevel@tonic-gate 	fwrite(sname, strlen(sname) + 1, 1, fp);
5480Sstevel@tonic-gate 	fwrite(sinst, strlen(sinst) + 1, 1, fp);
5490Sstevel@tonic-gate 	fwrite(srealm, strlen(srealm) + 1, 1, fp);
5500Sstevel@tonic-gate 	fwrite((char *)&kvno, 1, 1, fp);
5510Sstevel@tonic-gate 	fwrite((char *)lp->entry->key.contents,
5520Sstevel@tonic-gate 	       sizeof (des_cblock), 1, fp);
5530Sstevel@tonic-gate     }
5540Sstevel@tonic-gate     fclose(fp);
5550Sstevel@tonic-gate  free_pruned:
5560Sstevel@tonic-gate     /*
5570Sstevel@tonic-gate      * Loop over and free the pruned list; don't use free_kt_list
5580Sstevel@tonic-gate      * because that kills the entries.
5590Sstevel@tonic-gate      */
5600Sstevel@tonic-gate     for (lp = pruned; lp;) {
5610Sstevel@tonic-gate 	prev = lp;
5620Sstevel@tonic-gate 	lp = lp->next;
5630Sstevel@tonic-gate 	free((char *)prev);
5640Sstevel@tonic-gate     }
5650Sstevel@tonic-gate     return retval;
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate #endif /* KRB5_KRB4_COMPAT */
568