aboutsummaryrefslogtreecommitdiff
path: root/src/lib/gssapi/mechglue/g_canon_name.c
blob: 03d36ad5e4db339c403d321a0e32f5036cc56965 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/* #pragma ident	"@(#)g_canon_name.c	1.15	04/02/23 SMI" */

/*
 * routine gss_canonicalize_name
 *
 * This routine is used to produce a mechanism specific
 * representation of name that has been previously
 * imported with gss_import_name.  The routine uses the mechanism
 * specific implementation of gss_import_name to implement this
 * function.
 *
 * We allow a NULL output_name, in which case we modify the
 * input_name to include the mechanism specific name.
 */

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

static OM_uint32
val_canon_name_args(
	OM_uint32 *minor_status,
	const gss_name_t input_name,
	const gss_OID mech_type,
	gss_name_t *output_name)
{

	/* Initialize outputs. */

	if (minor_status != NULL)
		*minor_status = 0;

	if (output_name != NULL)
		*output_name = GSS_C_NO_NAME;

	/* Validate arguments. */

	if (minor_status == NULL)
		return (GSS_S_CALL_INACCESSIBLE_WRITE);

	if (input_name == GSS_C_NO_NAME || mech_type == GSS_C_NULL_OID)
		return (GSS_S_CALL_INACCESSIBLE_READ);

	return (GSS_S_COMPLETE);
}


OM_uint32 KRB5_CALLCONV
gss_canonicalize_name(minor_status,
				input_name,
				mech_type,
				output_name)
OM_uint32 *minor_status;
const gss_name_t input_name;
const gss_OID mech_type;
gss_name_t *output_name;
{
	gss_union_name_t in_union, out_union = NULL, dest_union = NULL;
	OM_uint32 major_status = GSS_S_FAILURE;

	major_status = val_canon_name_args(minor_status,
					   input_name,
					   mech_type,
					   output_name);
	if (major_status != GSS_S_COMPLETE)
		return (major_status);

	/* Initial value needed below. */
	major_status = GSS_S_FAILURE;

	in_union = (gss_union_name_t)input_name;
	/*
	 * If the caller wants to reuse the name, and the name has already
	 * been converted, then there is nothing for us to do.
	 */
	if (!output_name && in_union->mech_type &&
		g_OID_equal(in_union->mech_type, mech_type))
		return (GSS_S_COMPLETE);

	/* ok, then we need to do something - start by creating data struct */
	if (output_name) {
		out_union =
			(gss_union_name_t)malloc(sizeof (gss_union_name_desc));
		if (!out_union)
			goto allocation_failure;

		out_union->mech_type = 0;
		out_union->mech_name = 0;
		out_union->name_type = 0;
		out_union->external_name = 0;
		out_union->loopback = out_union;
		out_union->attributes = NULL;

		/* Allocate the buffer for the user specified representation */
		if (gssint_create_copy_buffer(in_union->external_name,
				&out_union->external_name, 1))
			goto allocation_failure;

		if (in_union->name_type != GSS_C_NULL_OID) {
		    major_status = generic_gss_copy_oid(minor_status,
							in_union->name_type,
							&out_union->name_type);
		    if (major_status) {
			map_errcode(minor_status);
			goto allocation_failure;
		    }
		}
		if (in_union->attributes) {
			major_status = gssint_duplicate_name_attributes(minor_status,
							in_union->attributes,
							&out_union->attributes);
			if (major_status != GSS_S_COMPLETE)
				goto allocation_failure;
		}
	}

	/*
	 * might need to delete any old mechanism names if we are
	 * reusing the buffer.
	 */
	if (!output_name) {
		if (in_union->mech_type) {
			(void) gssint_release_internal_name(minor_status,
							in_union->mech_type,
							&in_union->mech_name);
			(void) gss_release_oid(minor_status,
					    &in_union->mech_type);
			in_union->mech_type = 0;
		}
		dest_union = in_union;
	} else
		dest_union = out_union;

	/* now let's create the new mech name */
	if ((major_status = generic_gss_copy_oid(minor_status, mech_type,
						 &dest_union->mech_type))) {
	    map_errcode(minor_status);
	    goto allocation_failure;
	}

	if ((major_status =
		gssint_import_internal_name(minor_status, mech_type,
						dest_union,
						&dest_union->mech_name)))
		goto allocation_failure;

	if (output_name)
		*output_name = (gss_name_t)dest_union;

	return (GSS_S_COMPLETE);

allocation_failure:
	/* do not delete the src name external name format */
	if (output_name) {
		if (out_union->external_name) {
			if (out_union->external_name->value)
				free(out_union->external_name->value);
			free(out_union->external_name);
		}
		if (out_union->name_type)
			(void) gss_release_oid(minor_status,
					    &out_union->name_type);

		dest_union = out_union;
	} else
		dest_union = in_union;

	/*
	 * delete the partially created mech specific name
	 * applies for both src and dest which ever is being used for output
	 */

	if (dest_union->mech_name) {
		(void) gssint_release_internal_name(minor_status,
						dest_union->mech_type,
						&dest_union->mech_name);
	}

	if (dest_union->mech_type)
		(void) gss_release_oid(minor_status, &dest_union->mech_type);


	if (output_name)
		free(out_union);

	return (major_status);
} /**********  gss_canonicalize_name ********/