#include "k5-int.h"
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "kdb_xdr.h"
krb5_error_code
krb5_encode_princ_dbkey(krb5_context context, krb5_data *key,
krb5_const_principal principal)
{
char *princ_name;
krb5_error_code retval;
if (!(retval = krb5_unparse_name(context, principal, &princ_name))) {
key->length = strlen(princ_name)+1;
key->data = princ_name;
}
return(retval);
}
krb5_error_code
krb5_encode_princ_entry(krb5_context context, krb5_data *content,
krb5_db_entry *entry)
{
int i, j;
unsigned int unparse_princ_size;
char * unparse_princ;
unsigned char * nextloc;
krb5_tl_data * tl_data;
krb5_error_code retval;
krb5_int16 psize16;
content->length = entry->len + entry->e_length;
if ((retval = krb5_unparse_name(context, entry->princ, &unparse_princ)))
return(retval);
unparse_princ_size = strlen(unparse_princ) + 1;
content->length += unparse_princ_size;
content->length += 2;
i = 0;
for (tl_data = entry->tl_data; tl_data; tl_data = tl_data->tl_data_next) {
content->length += tl_data->tl_data_length;
content->length += 4;
i++;
}
if (i != entry->n_tl_data) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto epc_error;
}
for (i = 0; i < entry->n_key_data; i++) {
content->length += 4;
for (j = 0; j < entry->key_data[i].key_data_ver; j++) {
content->length += entry->key_data[i].key_data_length[j];
content->length += 4;
}
}
if ((content->data = malloc(content->length)) == NULL) {
retval = ENOMEM;
goto epc_error;
}
nextloc = (unsigned char *)content->data;
krb5_kdb_encode_int16(entry->len, nextloc);
nextloc += 2;
krb5_kdb_encode_int32(entry->attributes, nextloc);
nextloc += 4;
krb5_kdb_encode_int32(entry->max_life, nextloc);
nextloc += 4;
krb5_kdb_encode_int32(entry->max_renewable_life, nextloc);
nextloc += 4;
krb5_kdb_encode_int32(entry->expiration, nextloc);
nextloc += 4;
krb5_kdb_encode_int32(entry->pw_expiration, nextloc);
nextloc += 4;
krb5_kdb_encode_int32(entry->last_success, nextloc);
nextloc += 4;
krb5_kdb_encode_int32(entry->last_failed, nextloc);
nextloc += 4;
krb5_kdb_encode_int32(entry->fail_auth_count, nextloc);
nextloc += 4;
krb5_kdb_encode_int16(entry->n_tl_data, nextloc);
nextloc += 2;
krb5_kdb_encode_int16(entry->n_key_data, nextloc);
nextloc += 2;
if (entry->len != KRB5_KDB_V1_BASE_LENGTH)
abort();
if (entry->e_length) {
memcpy(nextloc, entry->e_data, entry->e_length);
nextloc += entry->e_length;
}
psize16 = (krb5_int16) unparse_princ_size;
krb5_kdb_encode_int16(psize16, nextloc);
nextloc += 2;
(void) memcpy(nextloc, unparse_princ, unparse_princ_size);
nextloc += unparse_princ_size;
for (tl_data = entry->tl_data; tl_data; tl_data = tl_data->tl_data_next) {
krb5_kdb_encode_int16(tl_data->tl_data_type, nextloc);
nextloc += 2;
krb5_kdb_encode_int16(tl_data->tl_data_length, nextloc);
nextloc += 2;
memcpy(nextloc, tl_data->tl_data_contents, tl_data->tl_data_length);
nextloc += tl_data->tl_data_length;
}
for (i = 0; i < entry->n_key_data; i++) {
krb5_kdb_encode_int16(entry->key_data[i].key_data_ver, nextloc);
nextloc += 2;
krb5_kdb_encode_int16(entry->key_data[i].key_data_kvno, nextloc);
nextloc += 2;
for (j = 0; j < entry->key_data[i].key_data_ver; j++) {
krb5_int16 type = entry->key_data[i].key_data_type[j];
krb5_ui_2 length = entry->key_data[i].key_data_length[j];
krb5_kdb_encode_int16(type, nextloc);
nextloc += 2;
krb5_kdb_encode_int16(length, nextloc);
nextloc += 2;
if (length) {
memcpy(nextloc, entry->key_data[i].key_data_contents[j],length);
nextloc += length;
}
}
}
epc_error:;
free(unparse_princ);
return retval;
}
krb5_error_code
krb5_decode_princ_entry(krb5_context context, krb5_data *content,
krb5_db_entry **entry_ptr)
{
int sizeleft, i;
unsigned char * nextloc;
krb5_tl_data ** tl_data;
krb5_int16 i16;
krb5_db_entry * entry;
krb5_error_code retval;
*entry_ptr = NULL;
entry = k5alloc(sizeof(*entry), &retval);
if (entry == NULL)
return retval;
nextloc = (unsigned char *)content->data;
sizeleft = content->length;
if (sizeleft < KRB5_KDB_V1_BASE_LENGTH) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto error_out;
}
sizeleft -= KRB5_KDB_V1_BASE_LENGTH;
krb5_kdb_decode_int16(nextloc, entry->len);
nextloc += 2;
krb5_kdb_decode_int32(nextloc, entry->attributes);
nextloc += 4;
krb5_kdb_decode_int32(nextloc, entry->max_life);
nextloc += 4;
krb5_kdb_decode_int32(nextloc, entry->max_renewable_life);
nextloc += 4;
krb5_kdb_decode_int32(nextloc, entry->expiration);
nextloc += 4;
krb5_kdb_decode_int32(nextloc, entry->pw_expiration);
nextloc += 4;
krb5_kdb_decode_int32(nextloc, entry->last_success);
nextloc += 4;
krb5_kdb_decode_int32(nextloc, entry->last_failed);
nextloc += 4;
krb5_kdb_decode_int32(nextloc, entry->fail_auth_count);
nextloc += 4;
krb5_kdb_decode_int16(nextloc, entry->n_tl_data);
nextloc += 2;
if (entry->n_tl_data < 0) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto error_out;
}
krb5_kdb_decode_int16(nextloc, entry->n_key_data);
nextloc += 2;
if (entry->n_key_data < 0) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto error_out;
}
if (entry->len > KRB5_KDB_V1_BASE_LENGTH) {
entry->e_length = entry->len - KRB5_KDB_V1_BASE_LENGTH;
entry->e_data = k5memdup(nextloc, entry->e_length, &retval);
if (entry->e_data == NULL)
goto error_out;
nextloc += entry->e_length;
}
if (sizeleft < 2) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto error_out;
}
sizeleft -= 2;
i = 0;
krb5_kdb_decode_int16(nextloc, i16);
i = (int) i16;
nextloc += 2;
if (i <= 0 || i > sizeleft || nextloc[i - 1] != '\0' ||
memchr((char *)nextloc, '\0', i - 1) != NULL) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto error_out;
}
if ((retval = krb5_parse_name(context, (char *)nextloc, &(entry->princ))))
goto error_out;
sizeleft -= i;
nextloc += i;
tl_data = &entry->tl_data;
for (i = 0; i < entry->n_tl_data; i++) {
if (sizeleft < 4) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto error_out;
}
sizeleft -= 4;
if ((*tl_data = (krb5_tl_data *)
malloc(sizeof(krb5_tl_data))) == NULL) {
retval = ENOMEM;
goto error_out;
}
(*tl_data)->tl_data_next = NULL;
(*tl_data)->tl_data_contents = NULL;
krb5_kdb_decode_int16(nextloc, (*tl_data)->tl_data_type);
nextloc += 2;
krb5_kdb_decode_int16(nextloc, (*tl_data)->tl_data_length);
nextloc += 2;
if ((*tl_data)->tl_data_length > sizeleft) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto error_out;
}
sizeleft -= (*tl_data)->tl_data_length;
(*tl_data)->tl_data_contents =
k5memdup(nextloc, (*tl_data)->tl_data_length, &retval);
if ((*tl_data)->tl_data_contents == NULL)
goto error_out;
nextloc += (*tl_data)->tl_data_length;
tl_data = &((*tl_data)->tl_data_next);
}
if (entry->n_key_data && ((entry->key_data = (krb5_key_data *)
malloc(sizeof(krb5_key_data) * entry->n_key_data)) == NULL)) {
retval = ENOMEM;
goto error_out;
}
for (i = 0; i < entry->n_key_data; i++) {
krb5_key_data * key_data;
int j;
if (sizeleft < 4) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto error_out;
}
sizeleft -= 4;
key_data = entry->key_data + i;
memset(key_data, 0, sizeof(krb5_key_data));
krb5_kdb_decode_int16(nextloc, key_data->key_data_ver);
nextloc += 2;
krb5_kdb_decode_int16(nextloc, key_data->key_data_kvno);
nextloc += 2;
if (key_data->key_data_ver >= 0 &&
key_data->key_data_ver <= KRB5_KDB_V1_KEY_DATA_ARRAY) {
for (j = 0; j < key_data->key_data_ver; j++) {
if (sizeleft < 4) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto error_out;
}
sizeleft -= 4;
krb5_kdb_decode_int16(nextloc, key_data->key_data_type[j]);
nextloc += 2;
krb5_kdb_decode_int16(nextloc, key_data->key_data_length[j]);
nextloc += 2;
if (key_data->key_data_length[j] > sizeleft) {
retval = KRB5_KDB_TRUNCATED_RECORD;
goto error_out;
}
sizeleft -= key_data->key_data_length[j];
if (key_data->key_data_length[j]) {
key_data->key_data_contents[j] =
k5memdup(nextloc, key_data->key_data_length[j],
&retval);
if (key_data->key_data_contents[j] == NULL)
goto error_out;
nextloc += key_data->key_data_length[j];
}
}
} else {
retval = KRB5_KDB_BAD_VERSION;
goto error_out;
}
}
*entry_ptr = entry;
return 0;
error_out:
krb5_db_free_principal(context, entry);
return retval;
}