xref: /freebsd/crypto/krb5/src/lib/gssapi/mechglue/g_compare_name.c (revision d0ff5773cefaf3fa41b1be3e44ca35bd9d5f68ee)
1 /* #pragma ident	"@(#)g_compare_name.c	1.16	04/02/23 SMI" */
2 
3 /*
4  * Copyright 1996 by Sun Microsystems, Inc.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appears in all copies and
9  * that both that copyright notice and this permission notice appear in
10  * supporting documentation, and that the name of Sun Microsystems not be used
11  * in advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission. Sun Microsystems makes no
13  * representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
20  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22  * PERFORMANCE OF THIS SOFTWARE.
23  */
24 
25 /*
26  *  glue routine for gss_compare_name
27  *
28  */
29 
30 #include "mglueP.h"
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34 #include <string.h>
35 
36 static OM_uint32
37 val_comp_name_args(
38     OM_uint32 *minor_status,
39     gss_name_t name1,
40     gss_name_t name2,
41     int *name_equal)
42 {
43 
44     /* Initialize outputs. */
45 
46     if (minor_status != NULL)
47 	*minor_status = 0;
48 
49     /* Validate arguments. */
50 
51     if (name1 == GSS_C_NO_NAME || name2 == GSS_C_NO_NAME)
52 	return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME);
53 
54     if (name_equal == NULL)
55 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
56 
57     return (GSS_S_COMPLETE);
58 }
59 
60 
61 OM_uint32 KRB5_CALLCONV
62 gss_compare_name (minor_status,
63                   name1,
64                   name2,
65                   name_equal)
66 
67 OM_uint32 *		minor_status;
68 gss_name_t		name1;
69 gss_name_t		name2;
70 int *			name_equal;
71 
72 {
73     OM_uint32		major_status, temp_minor;
74     gss_union_name_t	union_name1, union_name2;
75     gss_mechanism	mech = NULL;
76     gss_name_t		internal_name;
77 
78     major_status = val_comp_name_args(minor_status,
79 				      name1, name2, name_equal);
80     if (major_status != GSS_S_COMPLETE)
81 	return (major_status);
82 
83     union_name1 = (gss_union_name_t) name1;
84     union_name2 = (gss_union_name_t) name2;
85     /*
86      * Try our hardest to make union_name1 be the mechanism-specific
87      * name.  (Of course we can't if both names aren't
88      * mechanism-specific.)
89      */
90     if (union_name1->mech_type == 0) {
91 	union_name1 = (gss_union_name_t) name2;
92 	union_name2 = (gss_union_name_t) name1;
93     }
94     /*
95      * If union_name1 is mechanism specific, then fetch its mechanism
96      * information.
97      */
98     if (union_name1->mech_type) {
99 	mech = gssint_get_mechanism (union_name1->mech_type);
100 	if (!mech)
101 	    return (GSS_S_BAD_MECH);
102 	if (!mech->gss_compare_name)
103 			return (GSS_S_UNAVAILABLE);
104     }
105 
106     *name_equal = 0;		/* Default to *not* equal.... */
107 
108     /*
109      * First case... both names are mechanism-specific
110      */
111     if (union_name1->mech_type && union_name2->mech_type) {
112 	if (!g_OID_equal(union_name1->mech_type, union_name2->mech_type))
113 	    return (GSS_S_COMPLETE);
114 	if ((union_name1->mech_name == 0) || (union_name2->mech_name == 0))
115 	    /* should never happen */
116 	    return (GSS_S_BAD_NAME);
117 	if (!mech)
118 	    return (GSS_S_BAD_MECH);
119 	if (!mech->gss_compare_name)
120 	    return (GSS_S_UNAVAILABLE);
121 	major_status = mech->gss_compare_name(minor_status,
122 					      union_name1->mech_name,
123 					      union_name2->mech_name,
124 					      name_equal);
125 	if (major_status != GSS_S_COMPLETE)
126 	    map_error(minor_status, mech);
127 	return major_status;
128     }
129 
130     /*
131      * Second case... both names are NOT mechanism specific.
132      *
133      * All we do here is make sure the two name_types are equal and then
134      * that the external_names are equal. Note the we do not take care
135      * of the case where two different external names map to the same
136      * internal name. We cannot determine this, since we as yet do not
137      * know what mechanism to use for calling the underlying
138      * gss_import_name().
139      */
140     if (!union_name1->mech_type && !union_name2->mech_type) {
141 		/*
142 		 * Second case, first sub-case... one name has null
143 		 * name_type, the other doesn't.
144 		 *
145 		 * Not knowing a mech_type we can't import the name with
146 		 * null name_type so we can't compare.
147 		 */
148 		if ((union_name1->name_type == GSS_C_NULL_OID &&
149 		    union_name2->name_type != GSS_C_NULL_OID) ||
150 		    (union_name1->name_type != GSS_C_NULL_OID &&
151 		    union_name2->name_type == GSS_C_NULL_OID))
152 			return (GSS_S_COMPLETE);
153 		/*
154 		 * Second case, second sub-case... both names have
155 		 * name_types, but they are different.
156 		 */
157 		if ((union_name1->name_type != GSS_C_NULL_OID &&
158 		    union_name2->name_type != GSS_C_NULL_OID) &&
159 		    !g_OID_equal(union_name1->name_type,
160 					union_name2->name_type))
161 	    return (GSS_S_COMPLETE);
162 		/*
163 		 * Second case, third sub-case... both names have equal
164 		 * name_types (and both have no mech_types) so we just
165 		 * compare the external_names.
166 		 */
167 	if ((union_name1->external_name->length !=
168 	     union_name2->external_name->length) ||
169 	    (memcmp(union_name1->external_name->value,
170 		    union_name2->external_name->value,
171 		    union_name1->external_name->length) != 0))
172 	    return (GSS_S_COMPLETE);
173 	*name_equal = 1;
174 	return (GSS_S_COMPLETE);
175     }
176 
177     /*
178      * Final case... one name is mechanism specific, the other isn't.
179      *
180      * We attempt to convert the general name to the mechanism type of
181      * the mechanism-specific name, and then do the compare.  If we
182      * can't import the general name, then we return that the name is
183      * _NOT_ equal.
184      */
185     if (union_name2->mech_type) {
186 	/* We make union_name1 the mechanism specific name. */
187 	union_name1 = (gss_union_name_t) name2;
188 	union_name2 = (gss_union_name_t) name1;
189     }
190     major_status = gssint_import_internal_name(minor_status,
191 					      union_name1->mech_type,
192 					      union_name2,
193 					      &internal_name);
194     if (major_status != GSS_S_COMPLETE)
195 	return (GSS_S_COMPLETE); /* return complete, but not equal */
196 
197     if (!mech)
198 	return (GSS_S_BAD_MECH);
199     if (!mech->gss_compare_name)
200 	return (GSS_S_UNAVAILABLE);
201     major_status = mech->gss_compare_name(minor_status,
202 					  union_name1->mech_name,
203 					  internal_name, name_equal);
204     if (major_status != GSS_S_COMPLETE)
205 	map_error(minor_status, mech);
206     gssint_release_internal_name(&temp_minor, union_name1->mech_type,
207 				&internal_name);
208     return (major_status);
209 
210 }
211