17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * lib/kdb/kdb_xdr.c
37c478bd9Sstevel@tonic-gate *
4*1da57d55SToomas Soome * Copyright 1995 by the Massachusetts Institute of Technology.
57c478bd9Sstevel@tonic-gate * All Rights Reserved.
67c478bd9Sstevel@tonic-gate *
77c478bd9Sstevel@tonic-gate * Export of this software from the United States of America may
87c478bd9Sstevel@tonic-gate * require a specific license from the United States Government.
97c478bd9Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating
107c478bd9Sstevel@tonic-gate * export to obtain such a license before exporting.
11*1da57d55SToomas Soome *
127c478bd9Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
137c478bd9Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
147c478bd9Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
157c478bd9Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
167c478bd9Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
177c478bd9Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
187c478bd9Sstevel@tonic-gate * to distribution of the software without specific, written prior
197c478bd9Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label
207c478bd9Sstevel@tonic-gate * your software as modified software and not distribute it in such a
217c478bd9Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
227c478bd9Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of
237c478bd9Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
247c478bd9Sstevel@tonic-gate * or implied warranty.
25*1da57d55SToomas Soome *
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include "k5-int.h"
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
3254925bf6Swillf #include "kdb_xdr.h"
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate krb5_error_code
krb5_encode_princ_dbkey(context,key,principal)357c478bd9Sstevel@tonic-gate krb5_encode_princ_dbkey(context, key, principal)
367c478bd9Sstevel@tonic-gate krb5_context context;
377c478bd9Sstevel@tonic-gate krb5_data *key;
387c478bd9Sstevel@tonic-gate krb5_const_principal principal;
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate char *princ_name;
417c478bd9Sstevel@tonic-gate krb5_error_code retval;
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate if (!(retval = krb5_unparse_name(context, principal, &princ_name))) {
447c478bd9Sstevel@tonic-gate /* need to store the NULL for decoding */
45*1da57d55SToomas Soome key->length = strlen(princ_name)+1;
467c478bd9Sstevel@tonic-gate key->data = princ_name;
477c478bd9Sstevel@tonic-gate }
487c478bd9Sstevel@tonic-gate return(retval);
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate void
krb5_free_princ_dbkey(context,key)527c478bd9Sstevel@tonic-gate krb5_free_princ_dbkey(context, key)
537c478bd9Sstevel@tonic-gate krb5_context context;
547c478bd9Sstevel@tonic-gate krb5_data *key;
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate (void) krb5_free_data_contents(context, key);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate krb5_error_code
krb5_encode_princ_contents(context,content,entry)607c478bd9Sstevel@tonic-gate krb5_encode_princ_contents(context, content, entry)
617c478bd9Sstevel@tonic-gate krb5_context context;
627c478bd9Sstevel@tonic-gate krb5_data * content;
637c478bd9Sstevel@tonic-gate krb5_db_entry * entry;
647c478bd9Sstevel@tonic-gate {
6556a424ccSmp int i, j;
6656a424ccSmp unsigned int unparse_princ_size;
677c478bd9Sstevel@tonic-gate char * unparse_princ;
687c478bd9Sstevel@tonic-gate char * nextloc;
697c478bd9Sstevel@tonic-gate krb5_tl_data * tl_data;
707c478bd9Sstevel@tonic-gate krb5_error_code retval;
717c478bd9Sstevel@tonic-gate krb5_int16 psize16;
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate * Generate one lump of data from the krb5_db_entry.
757c478bd9Sstevel@tonic-gate * This data must be independent of byte order of the machine,
767c478bd9Sstevel@tonic-gate * compact and extensible.
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate
79*1da57d55SToomas Soome /*
80*1da57d55SToomas Soome * First allocate enough space for all the data.
817c478bd9Sstevel@tonic-gate * Need 2 bytes for the length of the base structure
827c478bd9Sstevel@tonic-gate * then 36 [ 8 * 4 + 2 * 2] bytes for the base information
837c478bd9Sstevel@tonic-gate * [ attributes, max_life, max_renewable_life, expiration,
847c478bd9Sstevel@tonic-gate * pw_expiration, last_success, last_failed, fail_auth_count ]
857c478bd9Sstevel@tonic-gate * [ n_key_data, n_tl_data ]
867c478bd9Sstevel@tonic-gate * then XX bytes [ e_length ] for the extra data [ e_data ]
877c478bd9Sstevel@tonic-gate * then XX bytes [ 2 for length + length for string ] for the principal,
887c478bd9Sstevel@tonic-gate * then (4 [type + length] + tl_data_length) bytes per tl_data
897c478bd9Sstevel@tonic-gate * then (4 + (4 + key_data_length) per key_data_contents) bytes per key_data
907c478bd9Sstevel@tonic-gate */
917c478bd9Sstevel@tonic-gate content->length = entry->len + entry->e_length;
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate if ((retval = krb5_unparse_name(context, entry->princ, &unparse_princ)))
947c478bd9Sstevel@tonic-gate return(retval);
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate unparse_princ_size = strlen(unparse_princ) + 1;
977c478bd9Sstevel@tonic-gate content->length += unparse_princ_size;
98*1da57d55SToomas Soome content->length += 2;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate i = 0;
1017c478bd9Sstevel@tonic-gate /* tl_data is a linked list */
1027c478bd9Sstevel@tonic-gate for (tl_data = entry->tl_data; tl_data; tl_data = tl_data->tl_data_next) {
1037c478bd9Sstevel@tonic-gate content->length += tl_data->tl_data_length;
1047c478bd9Sstevel@tonic-gate content->length += 4; /* type, length */
1057c478bd9Sstevel@tonic-gate i++;
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate if (i != entry->n_tl_data) {
1097c478bd9Sstevel@tonic-gate retval = KRB5_KDB_TRUNCATED_RECORD;
1107c478bd9Sstevel@tonic-gate goto epc_error;
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate /* key_data is an array */
1147c478bd9Sstevel@tonic-gate for (i = 0; i < entry->n_key_data; i++) {
1157c478bd9Sstevel@tonic-gate content->length += 4; /* Version, KVNO */
1167c478bd9Sstevel@tonic-gate for (j = 0; j < entry->key_data[i].key_data_ver; j++) {
1177c478bd9Sstevel@tonic-gate content->length += entry->key_data[i].key_data_length[j];
1187c478bd9Sstevel@tonic-gate content->length += 4; /* type + length */
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate }
121*1da57d55SToomas Soome
1227c478bd9Sstevel@tonic-gate if ((content->data = malloc(content->length)) == NULL) {
1237c478bd9Sstevel@tonic-gate retval = ENOMEM;
1247c478bd9Sstevel@tonic-gate goto epc_error;
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
127*1da57d55SToomas Soome /*
128*1da57d55SToomas Soome * Now we go through entry again, this time copying data
12956a424ccSmp * These first entries are always saved regardless of version
1307c478bd9Sstevel@tonic-gate */
1317c478bd9Sstevel@tonic-gate nextloc = content->data;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate /* Base Length */
1347c478bd9Sstevel@tonic-gate krb5_kdb_encode_int16(entry->len, nextloc);
1357c478bd9Sstevel@tonic-gate nextloc += 2;
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /* Attributes */
1387c478bd9Sstevel@tonic-gate krb5_kdb_encode_int32(entry->attributes, nextloc);
1397c478bd9Sstevel@tonic-gate nextloc += 4;
140*1da57d55SToomas Soome
1417c478bd9Sstevel@tonic-gate /* Max Life */
1427c478bd9Sstevel@tonic-gate krb5_kdb_encode_int32(entry->max_life, nextloc);
1437c478bd9Sstevel@tonic-gate nextloc += 4;
144*1da57d55SToomas Soome
1457c478bd9Sstevel@tonic-gate /* Max Renewable Life */
1467c478bd9Sstevel@tonic-gate krb5_kdb_encode_int32(entry->max_renewable_life, nextloc);
1477c478bd9Sstevel@tonic-gate nextloc += 4;
148*1da57d55SToomas Soome
1497c478bd9Sstevel@tonic-gate /* When the client expires */
1507c478bd9Sstevel@tonic-gate krb5_kdb_encode_int32(entry->expiration, nextloc);
1517c478bd9Sstevel@tonic-gate nextloc += 4;
152*1da57d55SToomas Soome
1537c478bd9Sstevel@tonic-gate /* When its passwd expires */
1547c478bd9Sstevel@tonic-gate krb5_kdb_encode_int32(entry->pw_expiration, nextloc);
1557c478bd9Sstevel@tonic-gate nextloc += 4;
156*1da57d55SToomas Soome
1577c478bd9Sstevel@tonic-gate /* Last successful passwd */
1587c478bd9Sstevel@tonic-gate krb5_kdb_encode_int32(entry->last_success, nextloc);
1597c478bd9Sstevel@tonic-gate nextloc += 4;
160*1da57d55SToomas Soome
1617c478bd9Sstevel@tonic-gate /* Last failed passwd attempt */
1627c478bd9Sstevel@tonic-gate krb5_kdb_encode_int32(entry->last_failed, nextloc);
1637c478bd9Sstevel@tonic-gate nextloc += 4;
164*1da57d55SToomas Soome
1657c478bd9Sstevel@tonic-gate /* # of failed passwd attempt */
1667c478bd9Sstevel@tonic-gate krb5_kdb_encode_int32(entry->fail_auth_count, nextloc);
1677c478bd9Sstevel@tonic-gate nextloc += 4;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /* # tl_data strutures */
1707c478bd9Sstevel@tonic-gate krb5_kdb_encode_int16(entry->n_tl_data, nextloc);
1717c478bd9Sstevel@tonic-gate nextloc += 2;
172*1da57d55SToomas Soome
1737c478bd9Sstevel@tonic-gate /* # key_data strutures */
1747c478bd9Sstevel@tonic-gate krb5_kdb_encode_int16(entry->n_key_data, nextloc);
1757c478bd9Sstevel@tonic-gate nextloc += 2;
176*1da57d55SToomas Soome
1777c478bd9Sstevel@tonic-gate /* Put extended fields here */
1787c478bd9Sstevel@tonic-gate if (entry->len != KRB5_KDB_V1_BASE_LENGTH)
1797c478bd9Sstevel@tonic-gate abort();
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate /* Any extra data that this version doesn't understand. */
1827c478bd9Sstevel@tonic-gate if (entry->e_length) {
1837c478bd9Sstevel@tonic-gate memcpy(nextloc, entry->e_data, entry->e_length);
1847c478bd9Sstevel@tonic-gate nextloc += entry->e_length;
1857c478bd9Sstevel@tonic-gate }
186*1da57d55SToomas Soome
187*1da57d55SToomas Soome /*
1887c478bd9Sstevel@tonic-gate * Now we get to the principal.
1897c478bd9Sstevel@tonic-gate * To squeze a few extra bytes out it is always assumed to come
1907c478bd9Sstevel@tonic-gate * after the base type.
1917c478bd9Sstevel@tonic-gate */
1927c478bd9Sstevel@tonic-gate psize16 = (krb5_int16) unparse_princ_size;
1937c478bd9Sstevel@tonic-gate krb5_kdb_encode_int16(psize16, nextloc);
1947c478bd9Sstevel@tonic-gate nextloc += 2;
1957c478bd9Sstevel@tonic-gate (void) memcpy(nextloc, unparse_princ, unparse_princ_size);
1967c478bd9Sstevel@tonic-gate nextloc += unparse_princ_size;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /* tl_data is a linked list, of type, legth, contents */
1997c478bd9Sstevel@tonic-gate for (tl_data = entry->tl_data; tl_data; tl_data = tl_data->tl_data_next) {
2007c478bd9Sstevel@tonic-gate krb5_kdb_encode_int16(tl_data->tl_data_type, nextloc);
2017c478bd9Sstevel@tonic-gate nextloc += 2;
2027c478bd9Sstevel@tonic-gate krb5_kdb_encode_int16(tl_data->tl_data_length, nextloc);
2037c478bd9Sstevel@tonic-gate nextloc += 2;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate memcpy(nextloc, tl_data->tl_data_contents, tl_data->tl_data_length);
2067c478bd9Sstevel@tonic-gate nextloc += tl_data->tl_data_length;
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate /* key_data is an array */
2107c478bd9Sstevel@tonic-gate for (i = 0; i < entry->n_key_data; i++) {
2117c478bd9Sstevel@tonic-gate krb5_kdb_encode_int16(entry->key_data[i].key_data_ver, nextloc);
2127c478bd9Sstevel@tonic-gate nextloc += 2;
2137c478bd9Sstevel@tonic-gate krb5_kdb_encode_int16(entry->key_data[i].key_data_kvno, nextloc);
2147c478bd9Sstevel@tonic-gate nextloc += 2;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate for (j = 0; j < entry->key_data[i].key_data_ver; j++) {
2177c478bd9Sstevel@tonic-gate krb5_int16 type = entry->key_data[i].key_data_type[j];
21856a424ccSmp krb5_ui_2 length = entry->key_data[i].key_data_length[j];
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate krb5_kdb_encode_int16(type, nextloc);
2217c478bd9Sstevel@tonic-gate nextloc += 2;
2227c478bd9Sstevel@tonic-gate krb5_kdb_encode_int16(length, nextloc);
2237c478bd9Sstevel@tonic-gate nextloc += 2;
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate if (length) {
2267c478bd9Sstevel@tonic-gate memcpy(nextloc, entry->key_data[i].key_data_contents[j],length);
2277c478bd9Sstevel@tonic-gate nextloc += length;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate }
231*1da57d55SToomas Soome
2327c478bd9Sstevel@tonic-gate epc_error:;
2337c478bd9Sstevel@tonic-gate free(unparse_princ);
2347c478bd9Sstevel@tonic-gate return retval;
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate void
krb5_free_princ_contents(context,contents)2387c478bd9Sstevel@tonic-gate krb5_free_princ_contents(context, contents)
2397c478bd9Sstevel@tonic-gate krb5_context context;
2407c478bd9Sstevel@tonic-gate krb5_data *contents;
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate krb5_free_data_contents(context, contents);
2437c478bd9Sstevel@tonic-gate return;
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate krb5_error_code
krb5_decode_princ_contents(context,content,entry)2477c478bd9Sstevel@tonic-gate krb5_decode_princ_contents(context, content, entry)
2487c478bd9Sstevel@tonic-gate krb5_context context;
2497c478bd9Sstevel@tonic-gate krb5_data * content;
2507c478bd9Sstevel@tonic-gate krb5_db_entry * entry;
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate int sizeleft, i;
2537c478bd9Sstevel@tonic-gate char * nextloc;
2547c478bd9Sstevel@tonic-gate krb5_tl_data ** tl_data;
2557c478bd9Sstevel@tonic-gate krb5_int16 i16;
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate krb5_error_code retval;
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate /* Zero out entry and NULL pointers */
2607c478bd9Sstevel@tonic-gate memset(entry, 0, sizeof(krb5_db_entry));
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate * undo the effects of encode_princ_contents.
2647c478bd9Sstevel@tonic-gate *
2657c478bd9Sstevel@tonic-gate * The first part is decoding the base type. If the base type is
2667c478bd9Sstevel@tonic-gate * bigger than the original base type then the additional fields
2677c478bd9Sstevel@tonic-gate * need to be filled in. If the base type is larger than any
2687c478bd9Sstevel@tonic-gate * known base type the additional data goes in e_data.
2697c478bd9Sstevel@tonic-gate */
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate /* First do the easy stuff */
2727c478bd9Sstevel@tonic-gate nextloc = content->data;
2737c478bd9Sstevel@tonic-gate sizeleft = content->length;
274*1da57d55SToomas Soome if ((sizeleft -= KRB5_KDB_V1_BASE_LENGTH) < 0)
2757c478bd9Sstevel@tonic-gate return KRB5_KDB_TRUNCATED_RECORD;
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate /* Base Length */
2787c478bd9Sstevel@tonic-gate krb5_kdb_decode_int16(nextloc, entry->len);
2797c478bd9Sstevel@tonic-gate nextloc += 2;
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /* Attributes */
2827c478bd9Sstevel@tonic-gate krb5_kdb_decode_int32(nextloc, entry->attributes);
2837c478bd9Sstevel@tonic-gate nextloc += 4;
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate /* Max Life */
2867c478bd9Sstevel@tonic-gate krb5_kdb_decode_int32(nextloc, entry->max_life);
2877c478bd9Sstevel@tonic-gate nextloc += 4;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate /* Max Renewable Life */
2907c478bd9Sstevel@tonic-gate krb5_kdb_decode_int32(nextloc, entry->max_renewable_life);
2917c478bd9Sstevel@tonic-gate nextloc += 4;
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate /* When the client expires */
2947c478bd9Sstevel@tonic-gate krb5_kdb_decode_int32(nextloc, entry->expiration);
2957c478bd9Sstevel@tonic-gate nextloc += 4;
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate /* When its passwd expires */
2987c478bd9Sstevel@tonic-gate krb5_kdb_decode_int32(nextloc, entry->pw_expiration);
2997c478bd9Sstevel@tonic-gate nextloc += 4;
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate /* Last successful passwd */
3027c478bd9Sstevel@tonic-gate krb5_kdb_decode_int32(nextloc, entry->last_success);
3037c478bd9Sstevel@tonic-gate nextloc += 4;
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate /* Last failed passwd attempt */
3067c478bd9Sstevel@tonic-gate krb5_kdb_decode_int32(nextloc, entry->last_failed);
3077c478bd9Sstevel@tonic-gate nextloc += 4;
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate /* # of failed passwd attempt */
3107c478bd9Sstevel@tonic-gate krb5_kdb_decode_int32(nextloc, entry->fail_auth_count);
3117c478bd9Sstevel@tonic-gate nextloc += 4;
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate /* # tl_data strutures */
3147c478bd9Sstevel@tonic-gate krb5_kdb_decode_int16(nextloc, entry->n_tl_data);
3157c478bd9Sstevel@tonic-gate nextloc += 2;
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate if (entry->n_tl_data < 0)
3187c478bd9Sstevel@tonic-gate return KRB5_KDB_TRUNCATED_RECORD;
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate /* # key_data strutures */
3217c478bd9Sstevel@tonic-gate krb5_kdb_decode_int16(nextloc, entry->n_key_data);
3227c478bd9Sstevel@tonic-gate nextloc += 2;
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate if (entry->n_key_data < 0)
3257c478bd9Sstevel@tonic-gate return KRB5_KDB_TRUNCATED_RECORD;
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate /* Check for extra data */
3287c478bd9Sstevel@tonic-gate if (entry->len > KRB5_KDB_V1_BASE_LENGTH) {
3297c478bd9Sstevel@tonic-gate entry->e_length = entry->len - KRB5_KDB_V1_BASE_LENGTH;
3307c478bd9Sstevel@tonic-gate if ((entry->e_data = (krb5_octet *)malloc(entry->e_length))) {
3317c478bd9Sstevel@tonic-gate memcpy(entry->e_data, nextloc, entry->e_length);
3327c478bd9Sstevel@tonic-gate nextloc += entry->e_length;
3337c478bd9Sstevel@tonic-gate } else {
3347c478bd9Sstevel@tonic-gate return ENOMEM;
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate /*
339*1da57d55SToomas Soome * Get the principal name for the entry
3407c478bd9Sstevel@tonic-gate * (stored as a string which gets unparsed.)
3417c478bd9Sstevel@tonic-gate */
3427c478bd9Sstevel@tonic-gate if ((sizeleft -= 2) < 0) {
3437c478bd9Sstevel@tonic-gate retval = KRB5_KDB_TRUNCATED_RECORD;
3447c478bd9Sstevel@tonic-gate goto error_out;
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate i = 0;
3487c478bd9Sstevel@tonic-gate krb5_kdb_decode_int16(nextloc, i16);
3497c478bd9Sstevel@tonic-gate i = (int) i16;
3507c478bd9Sstevel@tonic-gate nextloc += 2;
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate if ((retval = krb5_parse_name(context, nextloc, &(entry->princ))))
3537c478bd9Sstevel@tonic-gate goto error_out;
3547c478bd9Sstevel@tonic-gate if (((size_t) i != (strlen(nextloc) + 1)) || (sizeleft < i)) {
3557c478bd9Sstevel@tonic-gate retval = KRB5_KDB_TRUNCATED_RECORD;
3567c478bd9Sstevel@tonic-gate goto error_out;
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate sizeleft -= i;
3597c478bd9Sstevel@tonic-gate nextloc += i;
3607c478bd9Sstevel@tonic-gate
3617c478bd9Sstevel@tonic-gate /* tl_data is a linked list */
3627c478bd9Sstevel@tonic-gate tl_data = &entry->tl_data;
3637c478bd9Sstevel@tonic-gate for (i = 0; i < entry->n_tl_data; i++) {
3647c478bd9Sstevel@tonic-gate if ((sizeleft -= 4) < 0) {
3657c478bd9Sstevel@tonic-gate retval = KRB5_KDB_TRUNCATED_RECORD;
3667c478bd9Sstevel@tonic-gate goto error_out;
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate if ((*tl_data = (krb5_tl_data *)
3697c478bd9Sstevel@tonic-gate malloc(sizeof(krb5_tl_data))) == NULL) {
3707c478bd9Sstevel@tonic-gate retval = ENOMEM;
3717c478bd9Sstevel@tonic-gate goto error_out;
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate (*tl_data)->tl_data_next = NULL;
3747c478bd9Sstevel@tonic-gate (*tl_data)->tl_data_contents = NULL;
3757c478bd9Sstevel@tonic-gate krb5_kdb_decode_int16(nextloc, (*tl_data)->tl_data_type);
3767c478bd9Sstevel@tonic-gate nextloc += 2;
3777c478bd9Sstevel@tonic-gate krb5_kdb_decode_int16(nextloc, (*tl_data)->tl_data_length);
3787c478bd9Sstevel@tonic-gate nextloc += 2;
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate if ((sizeleft -= (*tl_data)->tl_data_length) < 0) {
3817c478bd9Sstevel@tonic-gate retval = KRB5_KDB_TRUNCATED_RECORD;
3827c478bd9Sstevel@tonic-gate goto error_out;
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate if (((*tl_data)->tl_data_contents = (krb5_octet *)
3857c478bd9Sstevel@tonic-gate malloc((*tl_data)->tl_data_length)) == NULL) {
3867c478bd9Sstevel@tonic-gate retval = ENOMEM;
3877c478bd9Sstevel@tonic-gate goto error_out;
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate memcpy((*tl_data)->tl_data_contents,nextloc,(*tl_data)->tl_data_length);
3907c478bd9Sstevel@tonic-gate nextloc += (*tl_data)->tl_data_length;
3917c478bd9Sstevel@tonic-gate tl_data = &((*tl_data)->tl_data_next);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate /* key_data is an array */
3957c478bd9Sstevel@tonic-gate if (entry->n_key_data && ((entry->key_data = (krb5_key_data *)
3967c478bd9Sstevel@tonic-gate malloc(sizeof(krb5_key_data) * entry->n_key_data)) == NULL)) {
3977c478bd9Sstevel@tonic-gate retval = ENOMEM;
3987c478bd9Sstevel@tonic-gate goto error_out;
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate for (i = 0; i < entry->n_key_data; i++) {
4017c478bd9Sstevel@tonic-gate krb5_key_data * key_data;
4027c478bd9Sstevel@tonic-gate int j;
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate if ((sizeleft -= 4) < 0) {
4057c478bd9Sstevel@tonic-gate retval = KRB5_KDB_TRUNCATED_RECORD;
4067c478bd9Sstevel@tonic-gate goto error_out;
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate key_data = entry->key_data + i;
4097c478bd9Sstevel@tonic-gate memset(key_data, 0, sizeof(krb5_key_data));
4107c478bd9Sstevel@tonic-gate krb5_kdb_decode_int16(nextloc, key_data->key_data_ver);
4117c478bd9Sstevel@tonic-gate nextloc += 2;
4127c478bd9Sstevel@tonic-gate krb5_kdb_decode_int16(nextloc, key_data->key_data_kvno);
4137c478bd9Sstevel@tonic-gate nextloc += 2;
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate /* key_data_ver determins number of elements and how to unparse them. */
4167c478bd9Sstevel@tonic-gate if (key_data->key_data_ver <= KRB5_KDB_V1_KEY_DATA_ARRAY) {
4177c478bd9Sstevel@tonic-gate for (j = 0; j < key_data->key_data_ver; j++) {
4187c478bd9Sstevel@tonic-gate if ((sizeleft -= 4) < 0) {
4197c478bd9Sstevel@tonic-gate retval = KRB5_KDB_TRUNCATED_RECORD;
4207c478bd9Sstevel@tonic-gate goto error_out;
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate krb5_kdb_decode_int16(nextloc, key_data->key_data_type[j]);
4237c478bd9Sstevel@tonic-gate nextloc += 2;
4247c478bd9Sstevel@tonic-gate krb5_kdb_decode_int16(nextloc, key_data->key_data_length[j]);
4257c478bd9Sstevel@tonic-gate nextloc += 2;
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate if ((sizeleft -= key_data->key_data_length[j]) < 0) {
4287c478bd9Sstevel@tonic-gate retval = KRB5_KDB_TRUNCATED_RECORD;
4297c478bd9Sstevel@tonic-gate goto error_out;
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate if (key_data->key_data_length[j]) {
4327c478bd9Sstevel@tonic-gate if ((key_data->key_data_contents[j] = (krb5_octet *)
4337c478bd9Sstevel@tonic-gate malloc(key_data->key_data_length[j])) == NULL) {
4347c478bd9Sstevel@tonic-gate retval = ENOMEM;
4357c478bd9Sstevel@tonic-gate goto error_out;
4367c478bd9Sstevel@tonic-gate }
437*1da57d55SToomas Soome memcpy(key_data->key_data_contents[j], nextloc,
4387c478bd9Sstevel@tonic-gate key_data->key_data_length[j]);
4397c478bd9Sstevel@tonic-gate nextloc += key_data->key_data_length[j];
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate } else {
4437c478bd9Sstevel@tonic-gate /* This isn't right. I'll fix it later */
4447c478bd9Sstevel@tonic-gate abort();
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate return 0;
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate error_out:;
4507c478bd9Sstevel@tonic-gate krb5_dbe_free_contents(context, entry);
4517c478bd9Sstevel@tonic-gate return retval;
4527c478bd9Sstevel@tonic-gate }
453*1da57d55SToomas Soome
4547c478bd9Sstevel@tonic-gate void
krb5_dbe_free_contents(context,entry)4557c478bd9Sstevel@tonic-gate krb5_dbe_free_contents(context, entry)
456*1da57d55SToomas Soome krb5_context context;
4577c478bd9Sstevel@tonic-gate krb5_db_entry * entry;
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate krb5_tl_data * tl_data_next;
4607c478bd9Sstevel@tonic-gate krb5_tl_data * tl_data;
4617c478bd9Sstevel@tonic-gate int i, j;
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate if (entry->e_data)
4647c478bd9Sstevel@tonic-gate free(entry->e_data);
4657c478bd9Sstevel@tonic-gate if (entry->princ)
4667c478bd9Sstevel@tonic-gate krb5_free_principal(context, entry->princ);
4677c478bd9Sstevel@tonic-gate for (tl_data = entry->tl_data; tl_data; tl_data = tl_data_next) {
4687c478bd9Sstevel@tonic-gate tl_data_next = tl_data->tl_data_next;
4697c478bd9Sstevel@tonic-gate if (tl_data->tl_data_contents)
4707c478bd9Sstevel@tonic-gate free(tl_data->tl_data_contents);
4717c478bd9Sstevel@tonic-gate free(tl_data);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate if (entry->key_data) {
4747c478bd9Sstevel@tonic-gate for (i = 0; i < entry->n_key_data; i++) {
4757c478bd9Sstevel@tonic-gate for (j = 0; j < entry->key_data[i].key_data_ver; j++) {
4767c478bd9Sstevel@tonic-gate if (entry->key_data[i].key_data_length[j]) {
4777c478bd9Sstevel@tonic-gate if (entry->key_data[i].key_data_contents[j]) {
478*1da57d55SToomas Soome memset(entry->key_data[i].key_data_contents[j],
479*1da57d55SToomas Soome 0,
48056a424ccSmp (unsigned) entry->key_data[i].key_data_length[j]);
4817c478bd9Sstevel@tonic-gate free (entry->key_data[i].key_data_contents[j]);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate entry->key_data[i].key_data_contents[j] = NULL;
4857c478bd9Sstevel@tonic-gate entry->key_data[i].key_data_length[j] = 0;
4867c478bd9Sstevel@tonic-gate entry->key_data[i].key_data_type[j] = 0;
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate free(entry->key_data);
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate memset(entry, 0, sizeof(*entry));
4927c478bd9Sstevel@tonic-gate return;
4937c478bd9Sstevel@tonic-gate }
494