]> git.decadent.org.uk Git - nfs-utils.git/blob - support/gssapi/g_imp_name.c
43f9c500c0c051dfeabadd55367da40daebbd217
[nfs-utils.git] / support / gssapi / g_imp_name.c
1 /* #ident  "@(#)g_imp_name.c 1.2     96/02/06 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 gss_import_name
27  *
28  */
29
30 #include "mglueP.h"
31 #include <stdio.h>
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #include <string.h>
36 #include <errno.h>
37
38 OM_uint32 KRB5_CALLCONV
39 gss_import_name(minor_status,
40                 input_name_buffer,
41                 input_name_type,
42                 output_name)
43
44 OM_uint32 *             minor_status;
45 gss_buffer_t            input_name_buffer;
46 gss_OID                 input_name_type;
47 gss_name_t *            output_name;
48
49 {
50     gss_union_name_t    union_name;
51     OM_uint32           tmp, major_status = GSS_S_FAILURE;
52     gss_OID             mech;
53
54     gss_initialize();
55
56     if (minor_status)
57         *minor_status = 0;
58
59     /* if output_name is NULL, simply return */
60
61     if(output_name == NULL)
62         return (GSS_S_COMPLETE);
63
64     *output_name = 0;
65
66     if (input_name_buffer == GSS_C_NO_BUFFER)
67         return (GSS_S_BAD_NAME);
68
69     /*
70      * First create the union name struct that will hold the external
71      * name and the name type.
72      */
73
74     union_name = (gss_union_name_t) malloc (sizeof(gss_union_name_desc));
75     if (!union_name) {
76             *minor_status = ENOMEM;
77             goto allocation_failure;
78     }
79     union_name->mech_type = 0;
80     union_name->mech_name = 0;
81     union_name->name_type = 0;
82     union_name->external_name = 0;
83     union_name->gss_mech = NULL;
84
85     /*
86      * All we do here is record the external name and name_type.
87      * When the name is actually used, the underlying gss_import_name()
88      * is called for the appropriate mechanism. Note that the name type
89      * is assumed to be constant, so only a pointer to it is stored in
90      * union_name
91      */
92     union_name->external_name =
93         (gss_buffer_t) malloc(sizeof(gss_buffer_desc));
94     if (!union_name->external_name) {
95             *minor_status = ENOMEM;
96             goto allocation_failure;
97     }
98
99     union_name->external_name->length = input_name_buffer->length;
100     /* we malloc length+1 to stick a NULL on the end, just in case */
101     /* Note that this NULL is not included in ->length for a reason! */
102     union_name->external_name->value =
103         (void  *) malloc(input_name_buffer->length+1);
104     if (!union_name->external_name->value) {
105         *minor_status = ENOMEM;
106         goto allocation_failure;
107     }
108
109     memcpy(union_name->external_name->value, input_name_buffer->value,
110            input_name_buffer->length);
111
112     /* add NULL to end of external_name->value, just in case... */
113     ((char *)union_name->external_name->value)
114                                 [input_name_buffer->length] = '\0';
115
116     major_status = generic_gss_copy_oid(minor_status, input_name_type,
117                                         &union_name->name_type);
118     if (major_status != GSS_S_COMPLETE)
119         goto allocation_failure;
120
121     /*
122      * See if this is a mechanism-specific name.  If so, let's import
123      * it now so we can get any error messages, and to avoid trouble
124      * later...
125      */
126     mech = gss_find_mechanism_from_name_type(input_name_type);
127     if (mech) {
128         major_status = generic_gss_copy_oid(minor_status, mech,
129                                             &union_name->mech_type);
130         if (major_status != GSS_S_COMPLETE)
131             goto allocation_failure;
132
133         major_status = __gss_import_internal_name(minor_status, mech,
134                                                   union_name,
135                                                   &union_name->mech_name);
136         if (major_status)
137             goto allocation_failure;
138     }
139
140     *output_name = (gss_name_t) union_name;
141
142     return(GSS_S_COMPLETE);
143
144 allocation_failure:
145     if (union_name) {
146         if (union_name->external_name) {
147             if (union_name->external_name->value)
148                 free(union_name->external_name->value);
149             free(union_name->external_name);
150         }
151         if (union_name->name_type)
152             generic_gss_release_oid(&tmp, &union_name->name_type);
153         if (union_name->mech_name)
154             __gss_release_internal_name(minor_status, union_name->mech_type,
155                                         &union_name->mech_name);
156         if (union_name->mech_type)
157             generic_gss_release_oid(&tmp, &union_name->mech_type);
158         free(union_name);
159     }
160     return (major_status);
161 }