1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Solaris Kerberos:
28  * Iterate through a keytab (keytab) looking for an entry which matches
29  * the components of a principal (princ) but match on any realm. When a
30  * suitable entry is found return the entry's realm.
31  */
32 
33 #include "k5-int.h"
34 
krb5_kt_find_realm(krb5_context context,krb5_keytab keytab,krb5_principal princ,krb5_data * realm)35 krb5_error_code krb5_kt_find_realm(krb5_context context, krb5_keytab keytab,
36     krb5_principal princ, krb5_data *realm) {
37 
38 	krb5_kt_cursor cur;
39 	krb5_keytab_entry ent;
40 	krb5_boolean match;
41 	krb5_data tmp_realm;
42 	krb5_error_code ret, ret2;
43 
44 	ret = krb5_kt_start_seq_get(context, keytab, &cur);
45 	if (ret != 0) {
46 		return (ret);
47 	}
48 
49 	while ((ret = krb5_kt_next_entry(context, keytab, &ent, &cur)) == 0) {
50 		/* For the comparison the realms should be the same. */
51 		memcpy(&tmp_realm, &ent.principal->realm, sizeof (krb5_data));
52 		memcpy(&ent.principal->realm, &princ->realm,
53 		    sizeof (krb5_data));
54 
55 		match = krb5_principal_compare(context, ent.principal, princ);
56 
57 		/* Copy the realm back */
58 		memcpy(&ent.principal->realm, &tmp_realm, sizeof (krb5_data));
59 
60 		if (match) {
61 			/*
62 			 * A suitable entry was found in the keytab.
63 			 * Copy its realm
64 			 */
65 			ret = krb5int_copy_data_contents(context,
66 			    &ent.principal->realm, realm);
67 			if (ret) {
68 				krb5_kt_free_entry(context, &ent);
69 				krb5_kt_end_seq_get(context, keytab, &cur);
70 				return (ret);
71 			}
72 
73 			krb5_kt_free_entry(context, &ent);
74 			break;
75 		}
76 
77 		krb5_kt_free_entry(context, &ent);
78 	}
79 
80 	ret2 = krb5_kt_end_seq_get(context, keytab, &cur);
81 
82 	if (ret == KRB5_KT_END) {
83 		return (KRB5_KT_NOTFOUND);
84 	}
85 
86 	return (ret ? ret : ret2);
87 }
88