5488N/A/*
5488N/A * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
5488N/A */
5488N/A/*
5488N/A * Solaris Kerberos:
5488N/A * Iterate through a keytab (keytab) looking for an entry which matches
5488N/A * the components of a principal (princ) but match on any realm. When a
5488N/A * suitable entry is found return the entry's realm.
5488N/A */
5488N/A
5488N/A#include "k5-int.h"
5488N/A
5488N/Akrb5_error_code krb5_kt_find_realm(krb5_context context, krb5_keytab keytab,
5488N/A krb5_principal princ, krb5_data *realm) {
5488N/A
5488N/A krb5_kt_cursor cur;
5488N/A krb5_keytab_entry ent;
5488N/A krb5_boolean match;
5488N/A krb5_data tmp_realm;
5488N/A krb5_error_code ret, ret2;
5488N/A
5488N/A ret = krb5_kt_start_seq_get(context, keytab, &cur);
5488N/A if (ret != 0) {
5488N/A return (ret);
5488N/A }
5488N/A
5488N/A while ((ret = krb5_kt_next_entry(context, keytab, &ent, &cur)) == 0) {
5488N/A /* For the comparison the realms should be the same. */
5488N/A memcpy(&tmp_realm, &ent.principal->realm, sizeof (krb5_data));
5488N/A memcpy(&ent.principal->realm, &princ->realm,
5488N/A sizeof (krb5_data));
5488N/A
5488N/A match = krb5_principal_compare(context, ent.principal, princ);
5488N/A
5488N/A /* Copy the realm back */
5488N/A memcpy(&ent.principal->realm, &tmp_realm, sizeof (krb5_data));
5488N/A
5488N/A if (match) {
5488N/A /*
5488N/A * A suitable entry was found in the keytab.
5488N/A * Copy its realm
5488N/A */
5488N/A ret = krb5int_copy_data_contents(context,
5488N/A &ent.principal->realm, realm);
5488N/A if (ret) {
5488N/A krb5_kt_free_entry(context, &ent);
5488N/A krb5_kt_end_seq_get(context, keytab, &cur);
5488N/A return (ret);
5488N/A }
5488N/A
5488N/A krb5_kt_free_entry(context, &ent);
5488N/A break;
5488N/A }
5488N/A
5488N/A krb5_kt_free_entry(context, &ent);
5488N/A }
5488N/A
5488N/A ret2 = krb5_kt_end_seq_get(context, keytab, &cur);
5488N/A
5488N/A if (ret == KRB5_KT_END) {
5488N/A return (KRB5_KT_NOTFOUND);
5488N/A }
5488N/A
5488N/A return (ret ? ret : ret2);
5488N/A}