17c478bd9Sstevel@tonic-gate /*
2e49962a0Ssemery * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * kadmin/ktutil/ktutil_funcs.c
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate *(C) Copyright 1995, 1996 by the Massachusetts Institute of Technology.
107c478bd9Sstevel@tonic-gate * All Rights Reserved.
117c478bd9Sstevel@tonic-gate *
127c478bd9Sstevel@tonic-gate * Export of this software from the United States of America may
137c478bd9Sstevel@tonic-gate * require a specific license from the United States Government.
147c478bd9Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating
157c478bd9Sstevel@tonic-gate * export to obtain such a license before exporting.
167c478bd9Sstevel@tonic-gate *
177c478bd9Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
187c478bd9Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
197c478bd9Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
207c478bd9Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
217c478bd9Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
227c478bd9Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
237c478bd9Sstevel@tonic-gate * to distribution of the software without specific, written prior
247c478bd9Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label
257c478bd9Sstevel@tonic-gate * your software as modified software and not distribute it in such a
267c478bd9Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
277c478bd9Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of
287c478bd9Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
297c478bd9Sstevel@tonic-gate * or implied warranty.
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * Utility functions for ktutil.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include "k5-int.h"
357c478bd9Sstevel@tonic-gate #include "ktutil.h"
367c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
377c478bd9Sstevel@tonic-gate #include "kerberosIV/krb.h"
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <ctype.h>
427c478bd9Sstevel@tonic-gate #include <libintl.h>
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate * Free a kt_list
467c478bd9Sstevel@tonic-gate */
ktutil_free_kt_list(context,list)477c478bd9Sstevel@tonic-gate krb5_error_code ktutil_free_kt_list(context, list)
487c478bd9Sstevel@tonic-gate krb5_context context;
497c478bd9Sstevel@tonic-gate krb5_kt_list list;
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate krb5_kt_list lp, prev;
527c478bd9Sstevel@tonic-gate krb5_error_code retval = 0;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate for (lp = list; lp;) {
557c478bd9Sstevel@tonic-gate retval = krb5_kt_free_entry(context, lp->entry);
567c478bd9Sstevel@tonic-gate free((char *)lp->entry);
577c478bd9Sstevel@tonic-gate if (retval)
587c478bd9Sstevel@tonic-gate break;
597c478bd9Sstevel@tonic-gate prev = lp;
607c478bd9Sstevel@tonic-gate lp = lp->next;
617c478bd9Sstevel@tonic-gate free((char *)prev);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate return retval;
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * Delete a numbered entry in a kt_list. Takes a pointer to a kt_list
687c478bd9Sstevel@tonic-gate * in case head gets deleted.
697c478bd9Sstevel@tonic-gate */
ktutil_delete(context,list,idx)70*56a424ccSmp153739 krb5_error_code ktutil_delete(context, list, idx)
717c478bd9Sstevel@tonic-gate krb5_context context;
727c478bd9Sstevel@tonic-gate krb5_kt_list *list;
73*56a424ccSmp153739 int idx;
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate krb5_kt_list lp, prev;
767c478bd9Sstevel@tonic-gate int i;
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate for (lp = *list, i = 1; lp; prev = lp, lp = lp->next, i++) {
79*56a424ccSmp153739 if (i == idx) {
807c478bd9Sstevel@tonic-gate if (i == 1)
817c478bd9Sstevel@tonic-gate *list = lp->next;
827c478bd9Sstevel@tonic-gate else
837c478bd9Sstevel@tonic-gate prev->next = lp->next;
847c478bd9Sstevel@tonic-gate lp->next = NULL;
857c478bd9Sstevel@tonic-gate return ktutil_free_kt_list(context, lp);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate return EINVAL;
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate * Create a new keytab entry and add it to the keytab list.
937c478bd9Sstevel@tonic-gate * Based on the value of use_pass, either prompt the user for a
947c478bd9Sstevel@tonic-gate * password or key. If the keytab list is NULL, allocate a new
957c478bd9Sstevel@tonic-gate * one first.
967c478bd9Sstevel@tonic-gate */
ktutil_add(context,list,princ_str,kvno,enctype_str,use_pass)977c478bd9Sstevel@tonic-gate krb5_error_code ktutil_add(context, list, princ_str, kvno,
987c478bd9Sstevel@tonic-gate enctype_str, use_pass)
997c478bd9Sstevel@tonic-gate krb5_context context;
1007c478bd9Sstevel@tonic-gate krb5_kt_list *list;
1017c478bd9Sstevel@tonic-gate char *princ_str;
1027c478bd9Sstevel@tonic-gate krb5_kvno kvno;
1037c478bd9Sstevel@tonic-gate char *enctype_str;
1047c478bd9Sstevel@tonic-gate int use_pass;
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate krb5_keytab_entry *entry;
1077c478bd9Sstevel@tonic-gate krb5_kt_list lp = NULL, prev = NULL;
1087c478bd9Sstevel@tonic-gate krb5_principal princ;
1097c478bd9Sstevel@tonic-gate krb5_enctype enctype;
1107c478bd9Sstevel@tonic-gate krb5_timestamp now;
1117c478bd9Sstevel@tonic-gate krb5_error_code retval;
1127c478bd9Sstevel@tonic-gate krb5_data password, salt;
1137c478bd9Sstevel@tonic-gate krb5_keyblock key;
1147c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
1157c478bd9Sstevel@tonic-gate char promptstr[1024];
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate char *cp;
118*56a424ccSmp153739 int i, tmp;
119*56a424ccSmp153739 unsigned int pwsize = BUFSIZ;
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate retval = krb5_parse_name(context, princ_str, &princ);
1227c478bd9Sstevel@tonic-gate if (retval)
1237c478bd9Sstevel@tonic-gate return retval;
1247c478bd9Sstevel@tonic-gate /* now unparse in order to get the default realm appended
1257c478bd9Sstevel@tonic-gate to princ_str, if no realm was specified */
1267c478bd9Sstevel@tonic-gate retval = krb5_unparse_name(context, princ, &princ_str);
1277c478bd9Sstevel@tonic-gate if (retval)
1287c478bd9Sstevel@tonic-gate return retval;
1297c478bd9Sstevel@tonic-gate retval = krb5_string_to_enctype(enctype_str, &enctype);
1307c478bd9Sstevel@tonic-gate if (retval)
1317c478bd9Sstevel@tonic-gate return KRB5_BAD_ENCTYPE;
1327c478bd9Sstevel@tonic-gate retval = krb5_timeofday(context, &now);
1337c478bd9Sstevel@tonic-gate if (retval)
1347c478bd9Sstevel@tonic-gate return retval;
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate if (*list) {
1377c478bd9Sstevel@tonic-gate /* point lp at the tail of the list */
1387c478bd9Sstevel@tonic-gate for (lp = *list; lp->next; lp = lp->next);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate entry = (krb5_keytab_entry *) malloc(sizeof(krb5_keytab_entry));
1417c478bd9Sstevel@tonic-gate if (!entry) {
1427c478bd9Sstevel@tonic-gate return ENOMEM;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate memset((char *) entry, 0, sizeof(*entry));
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate if (!lp) { /* if list is empty, start one */
147d2ec6b54Smp153739 lp = (krb5_kt_list) malloc(sizeof(*lp));
1487c478bd9Sstevel@tonic-gate if (!lp) {
1497c478bd9Sstevel@tonic-gate return ENOMEM;
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate } else {
152d2ec6b54Smp153739 lp->next = (krb5_kt_list) malloc(sizeof(*lp));
1537c478bd9Sstevel@tonic-gate if (!lp->next) {
1547c478bd9Sstevel@tonic-gate return ENOMEM;
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate prev = lp;
1577c478bd9Sstevel@tonic-gate lp = lp->next;
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate lp->next = NULL;
1607c478bd9Sstevel@tonic-gate lp->entry = entry;
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate if (use_pass) {
1637c478bd9Sstevel@tonic-gate password.length = pwsize;
1647c478bd9Sstevel@tonic-gate password.data = (char *) malloc(pwsize);
1657c478bd9Sstevel@tonic-gate if (!password.data) {
1667c478bd9Sstevel@tonic-gate retval = ENOMEM;
1677c478bd9Sstevel@tonic-gate goto cleanup;
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate (void) snprintf(promptstr, sizeof(promptstr),
171e49962a0Ssemery gettext("Password for %.1000s"), princ_str);
1727c478bd9Sstevel@tonic-gate retval = krb5_read_password(context, promptstr, NULL, password.data,
1737c478bd9Sstevel@tonic-gate &password.length);
1747c478bd9Sstevel@tonic-gate if (retval)
1757c478bd9Sstevel@tonic-gate goto cleanup;
1767c478bd9Sstevel@tonic-gate retval = krb5_principal2salt(context, princ, &salt);
1777c478bd9Sstevel@tonic-gate if (retval)
1787c478bd9Sstevel@tonic-gate goto cleanup;
1797c478bd9Sstevel@tonic-gate retval = krb5_c_string_to_key(context, enctype, &password,
1807c478bd9Sstevel@tonic-gate &salt, &key);
1817c478bd9Sstevel@tonic-gate if (retval)
1827c478bd9Sstevel@tonic-gate goto cleanup;
1837c478bd9Sstevel@tonic-gate memset(password.data, 0, password.length);
1847c478bd9Sstevel@tonic-gate password.length = 0;
1857c478bd9Sstevel@tonic-gate memcpy(&lp->entry->key, &key, sizeof(krb5_keyblock));
1867c478bd9Sstevel@tonic-gate } else {
1877c478bd9Sstevel@tonic-gate printf(gettext("Key for %s (hex): "), princ_str);
1887c478bd9Sstevel@tonic-gate fgets(buf, BUFSIZ, stdin);
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate * We need to get rid of the trailing '\n' from fgets.
1917c478bd9Sstevel@tonic-gate * If we have an even number of hex digits (as we should),
1927c478bd9Sstevel@tonic-gate * write a '\0' over the '\n'. If for some reason we have
1937c478bd9Sstevel@tonic-gate * an odd number of hex digits, force an even number of hex
1947c478bd9Sstevel@tonic-gate * digits by writing a '0' into the last position (the string
1957c478bd9Sstevel@tonic-gate * will still be null-terminated).
1967c478bd9Sstevel@tonic-gate */
1977c478bd9Sstevel@tonic-gate buf[strlen(buf) - 1] = strlen(buf) % 2 ? '\0' : '0';
1987c478bd9Sstevel@tonic-gate if (strlen(buf) == 0) {
1997c478bd9Sstevel@tonic-gate fprintf(stderr, "addent: %s", gettext("Error reading key.\n"));
2007c478bd9Sstevel@tonic-gate retval = 0;
2017c478bd9Sstevel@tonic-gate goto cleanup;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate lp->entry->key.enctype = enctype;
2057c478bd9Sstevel@tonic-gate lp->entry->key.contents = (krb5_octet *) malloc((strlen(buf) + 1) / 2);
2067c478bd9Sstevel@tonic-gate if (!lp->entry->key.contents) {
2077c478bd9Sstevel@tonic-gate retval = ENOMEM;
2087c478bd9Sstevel@tonic-gate goto cleanup;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate i = 0;
2127c478bd9Sstevel@tonic-gate for (cp = buf; *cp; cp += 2) {
213*56a424ccSmp153739 if (!isxdigit((int) cp[0]) || !isxdigit((int) cp[1])) {
2147c478bd9Sstevel@tonic-gate fprintf(stderr, "addent: %s",
2157c478bd9Sstevel@tonic-gate gettext("Illegal character in key.\n"));
2167c478bd9Sstevel@tonic-gate retval = 0;
2177c478bd9Sstevel@tonic-gate goto cleanup;
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate sscanf(cp, "%02x", &tmp);
2207c478bd9Sstevel@tonic-gate lp->entry->key.contents[i++] = (krb5_octet) tmp;
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate lp->entry->key.length = i;
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate lp->entry->principal = princ;
2257c478bd9Sstevel@tonic-gate lp->entry->vno = kvno;
2267c478bd9Sstevel@tonic-gate lp->entry->timestamp = now;
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate if (!*list)
2297c478bd9Sstevel@tonic-gate *list = lp;
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate return 0;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate cleanup:
2347c478bd9Sstevel@tonic-gate if (prev)
2357c478bd9Sstevel@tonic-gate prev->next = NULL;
2367c478bd9Sstevel@tonic-gate ktutil_free_kt_list(context, lp);
2377c478bd9Sstevel@tonic-gate return retval;
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate * Read in a keytab and append it to list. If list starts as NULL,
2427c478bd9Sstevel@tonic-gate * allocate a new one if necessary.
2437c478bd9Sstevel@tonic-gate */
ktutil_read_keytab(context,name,list)2447c478bd9Sstevel@tonic-gate krb5_error_code ktutil_read_keytab(context, name, list)
2457c478bd9Sstevel@tonic-gate krb5_context context;
2467c478bd9Sstevel@tonic-gate char *name;
2477c478bd9Sstevel@tonic-gate krb5_kt_list *list;
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate krb5_kt_list lp = NULL, tail = NULL, back = NULL;
2507c478bd9Sstevel@tonic-gate krb5_keytab kt;
2517c478bd9Sstevel@tonic-gate krb5_keytab_entry *entry;
2527c478bd9Sstevel@tonic-gate krb5_kt_cursor cursor;
2537c478bd9Sstevel@tonic-gate krb5_error_code retval = 0;
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate if (*list) {
2567c478bd9Sstevel@tonic-gate /* point lp at the tail of the list */
2577c478bd9Sstevel@tonic-gate for (lp = *list; lp->next; lp = lp->next);
2587c478bd9Sstevel@tonic-gate back = lp;
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate retval = krb5_kt_resolve(context, name, &kt);
2617c478bd9Sstevel@tonic-gate if (retval)
2627c478bd9Sstevel@tonic-gate return retval;
2637c478bd9Sstevel@tonic-gate retval = krb5_kt_start_seq_get(context, kt, &cursor);
2647c478bd9Sstevel@tonic-gate if (retval)
2657c478bd9Sstevel@tonic-gate goto close_kt;
2667c478bd9Sstevel@tonic-gate for (;;) {
2677c478bd9Sstevel@tonic-gate entry = (krb5_keytab_entry *)malloc(sizeof (krb5_keytab_entry));
2687c478bd9Sstevel@tonic-gate if (!entry) {
2697c478bd9Sstevel@tonic-gate retval = ENOMEM;
2707c478bd9Sstevel@tonic-gate break;
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate memset((char *)entry, 0, sizeof (*entry));
2737c478bd9Sstevel@tonic-gate retval = krb5_kt_next_entry(context, kt, entry, &cursor);
2747c478bd9Sstevel@tonic-gate if (retval)
2757c478bd9Sstevel@tonic-gate break;
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate if (!lp) { /* if list is empty, start one */
2787c478bd9Sstevel@tonic-gate lp = (krb5_kt_list)malloc(sizeof (*lp));
2797c478bd9Sstevel@tonic-gate if (!lp) {
2807c478bd9Sstevel@tonic-gate retval = ENOMEM;
2817c478bd9Sstevel@tonic-gate break;
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate } else {
2847c478bd9Sstevel@tonic-gate lp->next = (krb5_kt_list)malloc(sizeof (*lp));
2857c478bd9Sstevel@tonic-gate if (!lp->next) {
2867c478bd9Sstevel@tonic-gate retval = ENOMEM;
2877c478bd9Sstevel@tonic-gate break;
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate lp = lp->next;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate if (!tail)
2927c478bd9Sstevel@tonic-gate tail = lp;
2937c478bd9Sstevel@tonic-gate lp->next = NULL;
2947c478bd9Sstevel@tonic-gate lp->entry = entry;
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate if (entry)
2977c478bd9Sstevel@tonic-gate free((char *)entry);
298*56a424ccSmp153739 if (retval) {
2997c478bd9Sstevel@tonic-gate if (retval == KRB5_KT_END)
3007c478bd9Sstevel@tonic-gate retval = 0;
3017c478bd9Sstevel@tonic-gate else {
3027c478bd9Sstevel@tonic-gate ktutil_free_kt_list(context, tail);
3037c478bd9Sstevel@tonic-gate tail = NULL;
3047c478bd9Sstevel@tonic-gate if (back)
3057c478bd9Sstevel@tonic-gate back->next = NULL;
3067c478bd9Sstevel@tonic-gate }
307*56a424ccSmp153739 }
3087c478bd9Sstevel@tonic-gate if (!*list)
3097c478bd9Sstevel@tonic-gate *list = tail;
3107c478bd9Sstevel@tonic-gate krb5_kt_end_seq_get(context, kt, &cursor);
3117c478bd9Sstevel@tonic-gate close_kt:
3127c478bd9Sstevel@tonic-gate krb5_kt_close(context, kt);
3137c478bd9Sstevel@tonic-gate return retval;
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate * Takes a kt_list and writes it to the named keytab.
3187c478bd9Sstevel@tonic-gate */
ktutil_write_keytab(context,list,name)3197c478bd9Sstevel@tonic-gate krb5_error_code ktutil_write_keytab(context, list, name)
3207c478bd9Sstevel@tonic-gate krb5_context context;
3217c478bd9Sstevel@tonic-gate krb5_kt_list list;
3227c478bd9Sstevel@tonic-gate char *name;
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate krb5_kt_list lp;
3257c478bd9Sstevel@tonic-gate krb5_keytab kt;
3267c478bd9Sstevel@tonic-gate char ktname[MAXPATHLEN+sizeof("WRFILE:")+1];
3277c478bd9Sstevel@tonic-gate krb5_error_code retval = 0;
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate strcpy(ktname, "WRFILE:");
3307c478bd9Sstevel@tonic-gate if (strlen (name) >= MAXPATHLEN)
3317c478bd9Sstevel@tonic-gate return ENAMETOOLONG;
3327c478bd9Sstevel@tonic-gate strncat (ktname, name, MAXPATHLEN);
3337c478bd9Sstevel@tonic-gate retval = krb5_kt_resolve(context, ktname, &kt);
3347c478bd9Sstevel@tonic-gate if (retval)
3357c478bd9Sstevel@tonic-gate return retval;
3367c478bd9Sstevel@tonic-gate for (lp = list; lp; lp = lp->next) {
3377c478bd9Sstevel@tonic-gate retval = krb5_kt_add_entry(context, kt, lp->entry);
3387c478bd9Sstevel@tonic-gate if (retval)
3397c478bd9Sstevel@tonic-gate break;
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate krb5_kt_close(context, kt);
3427c478bd9Sstevel@tonic-gate return retval;
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate * getstr() takes a file pointer, a string and a count. It reads from
3487c478bd9Sstevel@tonic-gate * the file until either it has read "count" characters, or until it
3497c478bd9Sstevel@tonic-gate * reads a null byte. When finished, what has been read exists in the
3507c478bd9Sstevel@tonic-gate * given string "s". If "count" characters were actually read, the
3517c478bd9Sstevel@tonic-gate * last is changed to a null, so the returned string is always null-
3527c478bd9Sstevel@tonic-gate * terminated. getstr() returns the number of characters read,
3537c478bd9Sstevel@tonic-gate * including the null terminator.
3547c478bd9Sstevel@tonic-gate */
3557c478bd9Sstevel@tonic-gate
getstr(fp,s,n)356*56a424ccSmp153739 static int getstr(fp, s, n)
3577c478bd9Sstevel@tonic-gate FILE *fp;
3587c478bd9Sstevel@tonic-gate register char *s;
3597c478bd9Sstevel@tonic-gate int n;
3607c478bd9Sstevel@tonic-gate {
361*56a424ccSmp153739 register int count = n;
3627c478bd9Sstevel@tonic-gate while (fread(s, 1, 1, fp) > 0 && --count)
3637c478bd9Sstevel@tonic-gate if (*s++ == '\0')
3647c478bd9Sstevel@tonic-gate return (n - count);
3657c478bd9Sstevel@tonic-gate *s = '\0';
3667c478bd9Sstevel@tonic-gate return (n - count);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate * Read in a named krb4 srvtab and append to list. Allocate new list
3717c478bd9Sstevel@tonic-gate * if needed.
3727c478bd9Sstevel@tonic-gate */
ktutil_read_srvtab(context,name,list)3737c478bd9Sstevel@tonic-gate krb5_error_code ktutil_read_srvtab(context, name, list)
3747c478bd9Sstevel@tonic-gate krb5_context context;
3757c478bd9Sstevel@tonic-gate char *name;
3767c478bd9Sstevel@tonic-gate krb5_kt_list *list;
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate krb5_kt_list lp = NULL, tail = NULL, back = NULL;
3797c478bd9Sstevel@tonic-gate krb5_keytab_entry *entry;
3807c478bd9Sstevel@tonic-gate krb5_error_code retval = 0;
3817c478bd9Sstevel@tonic-gate char sname[SNAME_SZ]; /* name of service */
3827c478bd9Sstevel@tonic-gate char sinst[INST_SZ]; /* instance of service */
3837c478bd9Sstevel@tonic-gate char srealm[REALM_SZ]; /* realm of service */
3847c478bd9Sstevel@tonic-gate unsigned char kvno; /* key version number */
3857c478bd9Sstevel@tonic-gate des_cblock key;
3867c478bd9Sstevel@tonic-gate FILE *fp;
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate if (*list) {
3897c478bd9Sstevel@tonic-gate /* point lp at the tail of the list */
3907c478bd9Sstevel@tonic-gate for (lp = *list; lp->next; lp = lp->next);
3917c478bd9Sstevel@tonic-gate back = lp;
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate fp = fopen(name, "r");
3947c478bd9Sstevel@tonic-gate if (!fp)
3957c478bd9Sstevel@tonic-gate return EIO;
3967c478bd9Sstevel@tonic-gate for (;;) {
3977c478bd9Sstevel@tonic-gate entry = (krb5_keytab_entry *)malloc(sizeof (krb5_keytab_entry));
3987c478bd9Sstevel@tonic-gate if (!entry) {
3997c478bd9Sstevel@tonic-gate retval = ENOMEM;
4007c478bd9Sstevel@tonic-gate break;
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate memset((char *)entry, 0, sizeof (*entry));
4037c478bd9Sstevel@tonic-gate memset(sname, 0, sizeof (sname));
4047c478bd9Sstevel@tonic-gate memset(sinst, 0, sizeof (sinst));
4057c478bd9Sstevel@tonic-gate memset(srealm, 0, sizeof (srealm));
4067c478bd9Sstevel@tonic-gate if (!(getstr(fp, sname, SNAME_SZ) > 0 &&
4077c478bd9Sstevel@tonic-gate getstr(fp, sinst, INST_SZ) > 0 &&
4087c478bd9Sstevel@tonic-gate getstr(fp, srealm, REALM_SZ) > 0 &&
4097c478bd9Sstevel@tonic-gate fread(&kvno, 1, 1, fp) > 0 &&
4107c478bd9Sstevel@tonic-gate fread((char *)key, sizeof (key), 1, fp) > 0))
4117c478bd9Sstevel@tonic-gate break;
4127c478bd9Sstevel@tonic-gate entry->magic = KV5M_KEYTAB_ENTRY;
4137c478bd9Sstevel@tonic-gate entry->timestamp = 0; /* XXX */
4147c478bd9Sstevel@tonic-gate entry->vno = kvno;
4157c478bd9Sstevel@tonic-gate retval = krb5_425_conv_principal(context,
4167c478bd9Sstevel@tonic-gate sname, sinst, srealm,
4177c478bd9Sstevel@tonic-gate &entry->principal);
4187c478bd9Sstevel@tonic-gate if (retval)
4197c478bd9Sstevel@tonic-gate break;
4207c478bd9Sstevel@tonic-gate entry->key.magic = KV5M_KEYBLOCK;
4217c478bd9Sstevel@tonic-gate entry->key.enctype = ENCTYPE_DES_CBC_CRC;
4227c478bd9Sstevel@tonic-gate entry->key.length = sizeof (key);
4237c478bd9Sstevel@tonic-gate entry->key.contents = (krb5_octet *)malloc(sizeof (key));
4247c478bd9Sstevel@tonic-gate if (!entry->key.contents) {
4257c478bd9Sstevel@tonic-gate retval = ENOMEM;
4267c478bd9Sstevel@tonic-gate break;
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate memcpy((char *)entry->key.contents, (char *)key, sizeof (key));
4297c478bd9Sstevel@tonic-gate if (!lp) { /* if list is empty, start one */
4307c478bd9Sstevel@tonic-gate lp = (krb5_kt_list)malloc(sizeof (*lp));
4317c478bd9Sstevel@tonic-gate if (!lp) {
4327c478bd9Sstevel@tonic-gate retval = ENOMEM;
4337c478bd9Sstevel@tonic-gate break;
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate } else {
4367c478bd9Sstevel@tonic-gate lp->next = (krb5_kt_list)malloc(sizeof (*lp));
4377c478bd9Sstevel@tonic-gate if (!lp->next) {
4387c478bd9Sstevel@tonic-gate retval = ENOMEM;
4397c478bd9Sstevel@tonic-gate break;
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate lp = lp->next;
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate lp->next = NULL;
4447c478bd9Sstevel@tonic-gate lp->entry = entry;
4457c478bd9Sstevel@tonic-gate if (!tail)
4467c478bd9Sstevel@tonic-gate tail = lp;
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate if (entry) {
4497c478bd9Sstevel@tonic-gate if (entry->magic == KV5M_KEYTAB_ENTRY)
4507c478bd9Sstevel@tonic-gate krb5_kt_free_entry(context, entry);
4517c478bd9Sstevel@tonic-gate free((char *)entry);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate if (retval) {
4547c478bd9Sstevel@tonic-gate ktutil_free_kt_list(context, tail);
4557c478bd9Sstevel@tonic-gate tail = NULL;
4567c478bd9Sstevel@tonic-gate if (back)
4577c478bd9Sstevel@tonic-gate back->next = NULL;
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate if (!*list)
4607c478bd9Sstevel@tonic-gate *list = tail;
4617c478bd9Sstevel@tonic-gate fclose(fp);
4627c478bd9Sstevel@tonic-gate return retval;
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate * Writes a kt_list out to a krb4 srvtab file. Note that it first
4677c478bd9Sstevel@tonic-gate * prunes the kt_list so that it won't contain any keys that are not
4687c478bd9Sstevel@tonic-gate * the most recent, and ignores keys that are not ENCTYPE_DES.
4697c478bd9Sstevel@tonic-gate */
ktutil_write_srvtab(context,list,name)4707c478bd9Sstevel@tonic-gate krb5_error_code ktutil_write_srvtab(context, list, name)
4717c478bd9Sstevel@tonic-gate krb5_context context;
4727c478bd9Sstevel@tonic-gate krb5_kt_list list;
4737c478bd9Sstevel@tonic-gate char *name;
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate krb5_kt_list lp, lp1, prev, pruned = NULL;
4767c478bd9Sstevel@tonic-gate krb5_error_code retval = 0;
4777c478bd9Sstevel@tonic-gate FILE *fp;
4787c478bd9Sstevel@tonic-gate char sname[SNAME_SZ];
4797c478bd9Sstevel@tonic-gate char sinst[INST_SZ];
4807c478bd9Sstevel@tonic-gate char srealm[REALM_SZ];
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate /* First do heinous stuff to prune the list. */
4837c478bd9Sstevel@tonic-gate for (lp = list; lp; lp = lp->next) {
4847c478bd9Sstevel@tonic-gate if ((lp->entry->key.enctype != ENCTYPE_DES_CBC_CRC) &&
4857c478bd9Sstevel@tonic-gate (lp->entry->key.enctype != ENCTYPE_DES_CBC_MD5) &&
4867c478bd9Sstevel@tonic-gate (lp->entry->key.enctype != ENCTYPE_DES_CBC_MD4) &&
4877c478bd9Sstevel@tonic-gate (lp->entry->key.enctype != ENCTYPE_DES_CBC_RAW))
4887c478bd9Sstevel@tonic-gate continue;
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate for (lp1 = pruned; lp1; prev = lp1, lp1 = lp1->next) {
4917c478bd9Sstevel@tonic-gate /* Hunt for the current principal in the pruned list */
4927c478bd9Sstevel@tonic-gate if (krb5_principal_compare(context,
4937c478bd9Sstevel@tonic-gate lp->entry->principal,
4947c478bd9Sstevel@tonic-gate lp1->entry->principal))
4957c478bd9Sstevel@tonic-gate break;
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate if (!lp1) { /* need to add entry to tail of pruned list */
4987c478bd9Sstevel@tonic-gate if (!pruned) {
4997c478bd9Sstevel@tonic-gate pruned = (krb5_kt_list) malloc(sizeof (*pruned));
5007c478bd9Sstevel@tonic-gate if (!pruned)
5017c478bd9Sstevel@tonic-gate return ENOMEM;
5027c478bd9Sstevel@tonic-gate memset((char *) pruned, 0, sizeof(*pruned));
5037c478bd9Sstevel@tonic-gate lp1 = pruned;
5047c478bd9Sstevel@tonic-gate } else {
5057c478bd9Sstevel@tonic-gate prev->next
5067c478bd9Sstevel@tonic-gate = (krb5_kt_list) malloc(sizeof (*pruned));
5077c478bd9Sstevel@tonic-gate if (!prev->next) {
5087c478bd9Sstevel@tonic-gate retval = ENOMEM;
5097c478bd9Sstevel@tonic-gate goto free_pruned;
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate memset((char *) prev->next, 0, sizeof(*pruned));
5127c478bd9Sstevel@tonic-gate lp1 = prev->next;
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate lp1->entry = lp->entry;
515*56a424ccSmp153739 } else {
516*56a424ccSmp153739 /* This heuristic should be roughly the same as in the
517*56a424ccSmp153739 keytab-reading code in libkrb5. */
518*56a424ccSmp153739 int offset = 0;
519*56a424ccSmp153739 if (lp1->entry->vno > 240 || lp->entry->vno > 240) {
520*56a424ccSmp153739 offset = 128;
521*56a424ccSmp153739 }
522*56a424ccSmp153739 #define M(X) (((X) + offset) % 256)
523*56a424ccSmp153739 if (M(lp1->entry->vno) < M(lp->entry->vno))
5247c478bd9Sstevel@tonic-gate /* Check if lp->entry is newer kvno; if so, update */
5257c478bd9Sstevel@tonic-gate lp1->entry = lp->entry;
5267c478bd9Sstevel@tonic-gate }
527*56a424ccSmp153739 }
528*56a424ccSmp153739 umask(0077); /*Changing umask for all of ktutil is OK
529*56a424ccSmp153739 * We don't ever write out anything that should use
530*56a424ccSmp153739 * default umask.*/
5317c478bd9Sstevel@tonic-gate fp = fopen(name, "w");
5327c478bd9Sstevel@tonic-gate if (!fp) {
5337c478bd9Sstevel@tonic-gate retval = EIO;
5347c478bd9Sstevel@tonic-gate goto free_pruned;
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate for (lp = pruned; lp; lp = lp->next) {
5377c478bd9Sstevel@tonic-gate unsigned char kvno;
5387c478bd9Sstevel@tonic-gate kvno = (unsigned char) lp->entry->vno;
5397c478bd9Sstevel@tonic-gate retval = krb5_524_conv_principal(context,
5407c478bd9Sstevel@tonic-gate lp->entry->principal,
5417c478bd9Sstevel@tonic-gate sname, sinst, srealm);
5427c478bd9Sstevel@tonic-gate if (retval)
5437c478bd9Sstevel@tonic-gate break;
5447c478bd9Sstevel@tonic-gate fwrite(sname, strlen(sname) + 1, 1, fp);
5457c478bd9Sstevel@tonic-gate fwrite(sinst, strlen(sinst) + 1, 1, fp);
5467c478bd9Sstevel@tonic-gate fwrite(srealm, strlen(srealm) + 1, 1, fp);
5477c478bd9Sstevel@tonic-gate fwrite((char *)&kvno, 1, 1, fp);
5487c478bd9Sstevel@tonic-gate fwrite((char *)lp->entry->key.contents,
5497c478bd9Sstevel@tonic-gate sizeof (des_cblock), 1, fp);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate fclose(fp);
5527c478bd9Sstevel@tonic-gate free_pruned:
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate * Loop over and free the pruned list; don't use free_kt_list
5557c478bd9Sstevel@tonic-gate * because that kills the entries.
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate for (lp = pruned; lp;) {
5587c478bd9Sstevel@tonic-gate prev = lp;
5597c478bd9Sstevel@tonic-gate lp = lp->next;
5607c478bd9Sstevel@tonic-gate free((char *)prev);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate return retval;
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate #endif /* KRB5_KRB4_COMPAT */
565