2N/A/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2N/A/*
2N/A * lib/kdb/kdb_xdr.c
2N/A *
2N/A * Copyright 1995 by the Massachusetts Institute of Technology.
2N/A * All Rights Reserved.
2N/A *
2N/A * Export of this software from the United States of America may
2N/A * require a specific license from the United States Government.
2N/A * It is the responsibility of any person or organization contemplating
2N/A * export to obtain such a license before exporting.
2N/A *
2N/A * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
2N/A * distribute this software and its documentation for any purpose and
2N/A * without fee is hereby granted, provided that the above copyright
2N/A * notice appear in all copies and that both that copyright notice and
2N/A * this permission notice appear in supporting documentation, and that
2N/A * the name of M.I.T. not be used in advertising or publicity pertaining
2N/A * to distribution of the software without specific, written prior
2N/A * permission. Furthermore if you modify this software you must label
2N/A * your software as modified software and not distribute it in such a
2N/A * fashion that it might be confused with the original M.I.T. software.
2N/A * M.I.T. makes no representations about the suitability of
2N/A * this software for any purpose. It is provided "as is" without express
2N/A * or implied warranty.
2N/A *
2N/A */
2N/A
2N/A#include "k5-int.h"
2N/A#include <string.h>
2N/A#include <stdio.h>
2N/A#include <errno.h>
2N/A#include "kdb_xdr.h"
2N/A
2N/Akrb5_error_code
2N/Akrb5_encode_princ_dbkey(krb5_context context, krb5_data *key,
2N/A krb5_const_principal principal)
2N/A{
2N/A char *princ_name;
2N/A krb5_error_code retval;
2N/A
2N/A if (!(retval = krb5_unparse_name(context, principal, &princ_name))) {
2N/A /* need to store the NULL for decoding */
2N/A key->length = strlen(princ_name)+1;
2N/A key->data = princ_name;
2N/A }
2N/A return(retval);
2N/A}
2N/A
2N/Avoid
2N/Akrb5_free_princ_dbkey(krb5_context context, krb5_data *key)
2N/A{
2N/A (void) krb5_free_data_contents(context, key);
2N/A}
2N/A
2N/Akrb5_error_code
2N/Akrb5_encode_princ_contents(krb5_context context, krb5_data *content,
2N/A krb5_db_entry *entry)
2N/A{
2N/A int i, j;
2N/A unsigned int unparse_princ_size;
2N/A char * unparse_princ;
2N/A unsigned char * nextloc;
2N/A krb5_tl_data * tl_data;
2N/A krb5_error_code retval;
2N/A krb5_int16 psize16;
2N/A
2N/A /*
2N/A * Generate one lump of data from the krb5_db_entry.
2N/A * This data must be independent of byte order of the machine,
2N/A * compact and extensible.
2N/A */
2N/A
2N/A /*
2N/A * First allocate enough space for all the data.
2N/A * Need 2 bytes for the length of the base structure
2N/A * then 36 [ 8 * 4 + 2 * 2] bytes for the base information
2N/A * [ attributes, max_life, max_renewable_life, expiration,
2N/A * pw_expiration, last_success, last_failed, fail_auth_count ]
2N/A * [ n_key_data, n_tl_data ]
2N/A * then XX bytes [ e_length ] for the extra data [ e_data ]
2N/A * then XX bytes [ 2 for length + length for string ] for the principal,
2N/A * then (4 [type + length] + tl_data_length) bytes per tl_data
2N/A * then (4 + (4 + key_data_length) per key_data_contents) bytes per key_data
2N/A */
2N/A content->length = entry->len + entry->e_length;
2N/A
2N/A if ((retval = krb5_unparse_name(context, entry->princ, &unparse_princ)))
2N/A return(retval);
2N/A
2N/A unparse_princ_size = strlen(unparse_princ) + 1;
2N/A content->length += unparse_princ_size;
2N/A content->length += 2;
2N/A
2N/A i = 0;
2N/A /* tl_data is a linked list */
2N/A for (tl_data = entry->tl_data; tl_data; tl_data = tl_data->tl_data_next) {
2N/A content->length += tl_data->tl_data_length;
2N/A content->length += 4; /* type, length */
2N/A i++;
2N/A }
2N/A
2N/A if (i != entry->n_tl_data) {
2N/A retval = KRB5_KDB_TRUNCATED_RECORD;
2N/A goto epc_error;
2N/A }
2N/A
2N/A /* key_data is an array */
2N/A for (i = 0; i < entry->n_key_data; i++) {
2N/A content->length += 4; /* Version, KVNO */
2N/A for (j = 0; j < entry->key_data[i].key_data_ver; j++) {
2N/A content->length += entry->key_data[i].key_data_length[j];
2N/A content->length += 4; /* type + length */
2N/A }
2N/A }
2N/A
2N/A if ((content->data = malloc(content->length)) == NULL) {
2N/A retval = ENOMEM;
2N/A goto epc_error;
2N/A }
2N/A
2N/A /*
2N/A * Now we go through entry again, this time copying data
2N/A * These first entries are always saved regardless of version
2N/A */
2N/A nextloc = (unsigned char *)content->data;
2N/A
2N/A /* Base Length */
2N/A krb5_kdb_encode_int16(entry->len, nextloc);
2N/A nextloc += 2;
2N/A
2N/A /* Attributes */
2N/A krb5_kdb_encode_int32(entry->attributes, nextloc);
2N/A nextloc += 4;
2N/A
2N/A /* Max Life */
2N/A krb5_kdb_encode_int32(entry->max_life, nextloc);
2N/A nextloc += 4;
2N/A
2N/A /* Max Renewable Life */
2N/A krb5_kdb_encode_int32(entry->max_renewable_life, nextloc);
2N/A nextloc += 4;
2N/A
2N/A /* When the client expires */
2N/A krb5_kdb_encode_int32(entry->expiration, nextloc);
2N/A nextloc += 4;
2N/A
2N/A /* When its passwd expires */
2N/A krb5_kdb_encode_int32(entry->pw_expiration, nextloc);
2N/A nextloc += 4;
2N/A
2N/A /* Last successful passwd */
2N/A krb5_kdb_encode_int32(entry->last_success, nextloc);
2N/A nextloc += 4;
2N/A
2N/A /* Last failed passwd attempt */
2N/A krb5_kdb_encode_int32(entry->last_failed, nextloc);
2N/A nextloc += 4;
2N/A
2N/A /* # of failed passwd attempt */
2N/A krb5_kdb_encode_int32(entry->fail_auth_count, nextloc);
2N/A nextloc += 4;
2N/A
2N/A /* # tl_data strutures */
2N/A krb5_kdb_encode_int16(entry->n_tl_data, nextloc);
2N/A nextloc += 2;
2N/A
2N/A /* # key_data strutures */
2N/A krb5_kdb_encode_int16(entry->n_key_data, nextloc);
2N/A nextloc += 2;
2N/A
2N/A /* Put extended fields here */
2N/A if (entry->len != KRB5_KDB_V1_BASE_LENGTH)
2N/A abort();
2N/A
2N/A /* Any extra data that this version doesn't understand. */
2N/A if (entry->e_length) {
2N/A memcpy(nextloc, entry->e_data, entry->e_length);
2N/A nextloc += entry->e_length;
2N/A }
2N/A
2N/A /*
2N/A * Now we get to the principal.
2N/A * To squeze a few extra bytes out it is always assumed to come
2N/A * after the base type.
2N/A */
2N/A psize16 = (krb5_int16) unparse_princ_size;
2N/A krb5_kdb_encode_int16(psize16, nextloc);
2N/A nextloc += 2;
2N/A (void) memcpy(nextloc, unparse_princ, unparse_princ_size);
2N/A nextloc += unparse_princ_size;
2N/A
2N/A /* tl_data is a linked list, of type, legth, contents */
2N/A for (tl_data = entry->tl_data; tl_data; tl_data = tl_data->tl_data_next) {
2N/A krb5_kdb_encode_int16(tl_data->tl_data_type, nextloc);
2N/A nextloc += 2;
2N/A krb5_kdb_encode_int16(tl_data->tl_data_length, nextloc);
2N/A nextloc += 2;
2N/A
2N/A memcpy(nextloc, tl_data->tl_data_contents, tl_data->tl_data_length);
2N/A nextloc += tl_data->tl_data_length;
2N/A }
2N/A
2N/A /* key_data is an array */
2N/A for (i = 0; i < entry->n_key_data; i++) {
2N/A krb5_kdb_encode_int16(entry->key_data[i].key_data_ver, nextloc);
2N/A nextloc += 2;
2N/A krb5_kdb_encode_int16(entry->key_data[i].key_data_kvno, nextloc);
2N/A nextloc += 2;
2N/A
2N/A for (j = 0; j < entry->key_data[i].key_data_ver; j++) {
2N/A krb5_int16 type = entry->key_data[i].key_data_type[j];
2N/A krb5_ui_2 length = entry->key_data[i].key_data_length[j];
2N/A
2N/A krb5_kdb_encode_int16(type, nextloc);
2N/A nextloc += 2;
2N/A krb5_kdb_encode_int16(length, nextloc);
2N/A nextloc += 2;
2N/A
2N/A if (length) {
2N/A memcpy(nextloc, entry->key_data[i].key_data_contents[j],length);
2N/A nextloc += length;
2N/A }
2N/A }
2N/A }
2N/A
2N/Aepc_error:;
2N/A free(unparse_princ);
2N/A return retval;
2N/A}
2N/A
2N/Avoid
2N/Akrb5_free_princ_contents(krb5_context context, krb5_data *contents)
2N/A{
2N/A krb5_free_data_contents(context, contents);
2N/A return;
2N/A}
2N/A
2N/Akrb5_error_code
2N/Akrb5_decode_princ_contents(krb5_context context, krb5_data *content,
2N/A krb5_db_entry *entry)
2N/A{
2N/A int sizeleft, i;
2N/A unsigned char * nextloc;
2N/A krb5_tl_data ** tl_data;
2N/A krb5_int16 i16;
2N/A
2N/A krb5_error_code retval;
2N/A
2N/A /* Zero out entry and NULL pointers */
2N/A memset(entry, 0, sizeof(krb5_db_entry));
2N/A
2N/A /*
2N/A * undo the effects of encode_princ_contents.
2N/A *
2N/A * The first part is decoding the base type. If the base type is
2N/A * bigger than the original base type then the additional fields
2N/A * need to be filled in. If the base type is larger than any
2N/A * known base type the additional data goes in e_data.
2N/A */
2N/A
2N/A /* First do the easy stuff */
2N/A nextloc = (unsigned char *)content->data;
2N/A sizeleft = content->length;
2N/A if ((sizeleft -= KRB5_KDB_V1_BASE_LENGTH) < 0)
2N/A return KRB5_KDB_TRUNCATED_RECORD;
2N/A
2N/A /* Base Length */
2N/A krb5_kdb_decode_int16(nextloc, entry->len);
2N/A nextloc += 2;
2N/A
2N/A /* Attributes */
2N/A krb5_kdb_decode_int32(nextloc, entry->attributes);
2N/A nextloc += 4;
2N/A
2N/A /* Max Life */
2N/A krb5_kdb_decode_int32(nextloc, entry->max_life);
2N/A nextloc += 4;
2N/A
2N/A /* Max Renewable Life */
2N/A krb5_kdb_decode_int32(nextloc, entry->max_renewable_life);
2N/A nextloc += 4;
2N/A
2N/A /* When the client expires */
2N/A krb5_kdb_decode_int32(nextloc, entry->expiration);
2N/A nextloc += 4;
2N/A
2N/A /* When its passwd expires */
2N/A krb5_kdb_decode_int32(nextloc, entry->pw_expiration);
2N/A nextloc += 4;
2N/A
2N/A /* Last successful passwd */
2N/A krb5_kdb_decode_int32(nextloc, entry->last_success);
2N/A nextloc += 4;
2N/A
2N/A /* Last failed passwd attempt */
2N/A krb5_kdb_decode_int32(nextloc, entry->last_failed);
2N/A nextloc += 4;
2N/A
2N/A /* # of failed passwd attempt */
2N/A krb5_kdb_decode_int32(nextloc, entry->fail_auth_count);
2N/A nextloc += 4;
2N/A
2N/A /* # tl_data strutures */
2N/A krb5_kdb_decode_int16(nextloc, entry->n_tl_data);
2N/A nextloc += 2;
2N/A
2N/A if (entry->n_tl_data < 0)
2N/A return KRB5_KDB_TRUNCATED_RECORD;
2N/A
2N/A /* # key_data strutures */
2N/A krb5_kdb_decode_int16(nextloc, entry->n_key_data);
2N/A nextloc += 2;
2N/A
2N/A if (entry->n_key_data < 0)
2N/A return KRB5_KDB_TRUNCATED_RECORD;
2N/A
2N/A /* Check for extra data */
2N/A if (entry->len > KRB5_KDB_V1_BASE_LENGTH) {
2N/A entry->e_length = entry->len - KRB5_KDB_V1_BASE_LENGTH;
2N/A if ((entry->e_data = (krb5_octet *)malloc(entry->e_length))) {
2N/A memcpy(entry->e_data, nextloc, entry->e_length);
2N/A nextloc += entry->e_length;
2N/A } else {
2N/A return ENOMEM;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Get the principal name for the entry
2N/A * (stored as a string which gets unparsed.)
2N/A */
2N/A if ((sizeleft -= 2) < 0) {
2N/A retval = KRB5_KDB_TRUNCATED_RECORD;
2N/A goto error_out;
2N/A }
2N/A
2N/A i = 0;
2N/A krb5_kdb_decode_int16(nextloc, i16);
2N/A i = (int) i16;
2N/A nextloc += 2;
2N/A
2N/A if ((retval = krb5_parse_name(context, (char *)nextloc, &(entry->princ))))
2N/A goto error_out;
2N/A if (((size_t) i != (strlen((char *)nextloc) + 1)) || (sizeleft < i)) {
2N/A retval = KRB5_KDB_TRUNCATED_RECORD;
2N/A goto error_out;
2N/A }
2N/A sizeleft -= i;
2N/A nextloc += i;
2N/A
2N/A /* tl_data is a linked list */
2N/A tl_data = &entry->tl_data;
2N/A for (i = 0; i < entry->n_tl_data; i++) {
2N/A if ((sizeleft -= 4) < 0) {
2N/A retval = KRB5_KDB_TRUNCATED_RECORD;
2N/A goto error_out;
2N/A }
2N/A if ((*tl_data = (krb5_tl_data *)
2N/A malloc(sizeof(krb5_tl_data))) == NULL) {
2N/A retval = ENOMEM;
2N/A goto error_out;
2N/A }
2N/A (*tl_data)->tl_data_next = NULL;
2N/A (*tl_data)->tl_data_contents = NULL;
2N/A krb5_kdb_decode_int16(nextloc, (*tl_data)->tl_data_type);
2N/A nextloc += 2;
2N/A krb5_kdb_decode_int16(nextloc, (*tl_data)->tl_data_length);
2N/A nextloc += 2;
2N/A
2N/A if ((sizeleft -= (*tl_data)->tl_data_length) < 0) {
2N/A retval = KRB5_KDB_TRUNCATED_RECORD;
2N/A goto error_out;
2N/A }
2N/A if (((*tl_data)->tl_data_contents = (krb5_octet *)
2N/A malloc((*tl_data)->tl_data_length)) == NULL) {
2N/A retval = ENOMEM;
2N/A goto error_out;
2N/A }
2N/A memcpy((*tl_data)->tl_data_contents,nextloc,(*tl_data)->tl_data_length);
2N/A nextloc += (*tl_data)->tl_data_length;
2N/A tl_data = &((*tl_data)->tl_data_next);
2N/A }
2N/A
2N/A /* key_data is an array */
2N/A if (entry->n_key_data && ((entry->key_data = (krb5_key_data *)
2N/A malloc(sizeof(krb5_key_data) * entry->n_key_data)) == NULL)) {
2N/A retval = ENOMEM;
2N/A goto error_out;
2N/A }
2N/A for (i = 0; i < entry->n_key_data; i++) {
2N/A krb5_key_data * key_data;
2N/A int j;
2N/A
2N/A if ((sizeleft -= 4) < 0) {
2N/A retval = KRB5_KDB_TRUNCATED_RECORD;
2N/A goto error_out;
2N/A }
2N/A key_data = entry->key_data + i;
2N/A memset(key_data, 0, sizeof(krb5_key_data));
2N/A krb5_kdb_decode_int16(nextloc, key_data->key_data_ver);
2N/A nextloc += 2;
2N/A krb5_kdb_decode_int16(nextloc, key_data->key_data_kvno);
2N/A nextloc += 2;
2N/A
2N/A /* key_data_ver determins number of elements and how to unparse them. */
2N/A if (key_data->key_data_ver <= KRB5_KDB_V1_KEY_DATA_ARRAY) {
2N/A for (j = 0; j < key_data->key_data_ver; j++) {
2N/A if ((sizeleft -= 4) < 0) {
2N/A retval = KRB5_KDB_TRUNCATED_RECORD;
2N/A goto error_out;
2N/A }
2N/A krb5_kdb_decode_int16(nextloc, key_data->key_data_type[j]);
2N/A nextloc += 2;
2N/A krb5_kdb_decode_int16(nextloc, key_data->key_data_length[j]);
2N/A nextloc += 2;
2N/A
2N/A if ((sizeleft -= key_data->key_data_length[j]) < 0) {
2N/A retval = KRB5_KDB_TRUNCATED_RECORD;
2N/A goto error_out;
2N/A }
2N/A if (key_data->key_data_length[j]) {
2N/A if ((key_data->key_data_contents[j] = (krb5_octet *)
2N/A malloc(key_data->key_data_length[j])) == NULL) {
2N/A retval = ENOMEM;
2N/A goto error_out;
2N/A }
2N/A memcpy(key_data->key_data_contents[j], nextloc,
2N/A key_data->key_data_length[j]);
2N/A nextloc += key_data->key_data_length[j];
2N/A }
2N/A }
2N/A } else {
2N/A /* This isn't right. I'll fix it later */
2N/A abort();
2N/A }
2N/A }
2N/A return 0;
2N/A
2N/Aerror_out:;
2N/A krb5_dbe_free_contents(context, entry);
2N/A return retval;
2N/A}
2N/A
2N/Avoid
2N/Akrb5_dbe_free_contents(krb5_context context, krb5_db_entry *entry)
2N/A{
2N/A krb5_tl_data * tl_data_next;
2N/A krb5_tl_data * tl_data;
2N/A int i, j;
2N/A
2N/A if (entry->e_data)
2N/A free(entry->e_data);
2N/A if (entry->princ)
2N/A krb5_free_principal(context, entry->princ);
2N/A for (tl_data = entry->tl_data; tl_data; tl_data = tl_data_next) {
2N/A tl_data_next = tl_data->tl_data_next;
2N/A if (tl_data->tl_data_contents)
2N/A free(tl_data->tl_data_contents);
2N/A free(tl_data);
2N/A }
2N/A if (entry->key_data) {
2N/A for (i = 0; i < entry->n_key_data; i++) {
2N/A for (j = 0; j < entry->key_data[i].key_data_ver; j++) {
2N/A if (entry->key_data[i].key_data_length[j]) {
2N/A if (entry->key_data[i].key_data_contents[j]) {
2N/A memset(entry->key_data[i].key_data_contents[j],
2N/A 0,
2N/A (unsigned) entry->key_data[i].key_data_length[j]);
2N/A free (entry->key_data[i].key_data_contents[j]);
2N/A }
2N/A }
2N/A entry->key_data[i].key_data_contents[j] = NULL;
2N/A entry->key_data[i].key_data_length[j] = 0;
2N/A entry->key_data[i].key_data_type[j] = 0;
2N/A }
2N/A }
2N/A free(entry->key_data);
2N/A }
2N/A memset(entry, 0, sizeof(*entry));
2N/A return;
2N/A}