summaryrefslogtreecommitdiffstats
path: root/src/lib/gssapi/mechglue/g_compare_name.c
blob: 851be4a85b65c7473d50ab99fac6c03087c05cbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* #ident  "@(#)gss_compare_name.c 1.13     95/08/02 SMI" */

/*
 * Copyright 1996 by Sun Microsystems, Inc.
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appears in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of Sun Microsystems not be used
 * in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission. Sun Microsystems makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 * 
 * SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/*
 *  glue routine for gss_compare_name
 *
 */

#include "mglueP.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <string.h>

#define g_OID_equal(o1,o2) \
   (((o1)->length == (o2)->length) && \
    (memcmp((o1)->elements,(o2)->elements,(int) (o1)->length) == 0))

OM_uint32 KRB5_CALLCONV
gss_compare_name (minor_status,
                  name1,
                  name2,
                  name_equal)

OM_uint32 *		minor_status;
gss_name_t		name1;
gss_name_t		name2;
int *			name_equal;

{
    OM_uint32		major_status, temp_minor;
    gss_union_name_t	union_name1, union_name2;
    gss_mechanism	mech;
    gss_name_t		internal_name;
    
    gss_initialize();

    if (name1 == 0 || name2 == 0) {
	if (name_equal)
	    *name_equal = 0;
	return GSS_S_BAD_NAME;
    }

    union_name1 = (gss_union_name_t) name1;
    union_name2 = (gss_union_name_t) name2;
    /*
     * Try our hardest to make union_name1 be the mechanism-specific
     * name.  (Of course we can't if both names aren't
     * mechanism-specific.)
     */
    if (union_name1->mech_type == 0) {
	union_name1 = (gss_union_name_t) name2;
	union_name2 = (gss_union_name_t) name1;
    }
    /*
     * If union_name1 is mechanism specific, then fetch its mechanism
     * information.
     */
    if (union_name1->mech_type) {
	mech = __gss_get_mechanism (union_name1->mech_type);
	if (!mech)
	    return (GSS_S_BAD_MECH);
	if (!mech->gss_compare_name)
	    return (GSS_S_BAD_BINDINGS);
    }
	
    if (name_equal == NULL)
	return GSS_S_COMPLETE;

    *name_equal = 0;		/* Default to *not* equal.... */

    /*
     * First case... both names are mechanism-specific
     */
    if (union_name1->mech_type && union_name2->mech_type) {
	if (!g_OID_equal(union_name1->mech_type, union_name2->mech_type))
	    return (GSS_S_COMPLETE);
	if ((union_name1->mech_name == 0) || (union_name2->mech_name == 0))
	    /* should never happen */
	    return (GSS_S_BAD_NAME);
	return (mech->gss_compare_name(mech->context, minor_status,
				       union_name1->mech_name,
				       union_name2->mech_name, name_equal));
	
    }

    /*
     * Second case... both names are NOT mechanism specific.
     * 
     * All we do here is make sure the two name_types are equal and then
     * that the external_names are equal. Note the we do not take care
     * of the case where two different external names map to the same
     * internal name. We cannot determine this, since we as yet do not
     * know what mechanism to use for calling the underlying
     * gss_import_name().
     */
    if (!union_name1->mech_type && !union_name2->mech_type) {
	if (!g_OID_equal(union_name1->name_type, union_name2->name_type))
	    return (GSS_S_COMPLETE);
	if ((union_name1->external_name->length !=
	     union_name2->external_name->length) ||
	    (memcmp(union_name1->external_name->value,
		    union_name2->external_name->value,
		    union_name1->external_name->length) != 0))
	    return (GSS_S_COMPLETE);
	*name_equal = 1;
	return (GSS_S_COMPLETE);
    }

    /*
     * Final case... one name is mechanism specific, the other isn't.
     * 
     * We attempt to convert the general name to the mechanism type of
     * the mechanism-specific name, and then do the compare.  If we
     * can't import the general name, then we return that the name is
     * _NOT_ equal.
     */
    if (union_name2->mech_type) {
	/* We make union_name1 the mechanism specific name. */
	union_name1 = (gss_union_name_t) name2;
	union_name2 = (gss_union_name_t) name1;
    }
    major_status = __gss_import_internal_name(minor_status,
					      union_name1->mech_type,
					      union_name2,
					      &internal_name);
    if (major_status != GSS_S_COMPLETE)
	return (GSS_S_COMPLETE);
    major_status = mech->gss_compare_name(mech->context, minor_status,
					  union_name1->mech_name,
					  internal_name, name_equal);
    __gss_release_internal_name(&temp_minor, union_name1->mech_type,
				&internal_name);
    return (major_status);
    
}