]> git.decadent.org.uk Git - nfs-utils.git/blob - support/gssapi/g_imp_sec_context.c
2005-08-26 Kevin Coffman <kwc@citi.umich.edu>
[nfs-utils.git] / support / gssapi / g_imp_sec_context.c
1 /* #ident  "@(#)g_imp_sec_context.c 1.2     96/01/18 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_export_sec_context
27  */
28
29 #include "mglueP.h"
30 #include <stdio.h>
31 #include <errno.h>
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #include <string.h>
36
37 OM_uint32 KRB5_CALLCONV
38 gss_import_sec_context(minor_status,
39                        interprocess_token,
40                        context_handle)
41
42 OM_uint32 *             minor_status;
43 gss_buffer_t            interprocess_token;
44 gss_ctx_id_t *          context_handle;
45
46 {
47     size_t              length;
48     OM_uint32           status;
49     char                *p;
50     gss_union_ctx_id_t  ctx;
51     gss_buffer_desc     token;
52     gss_mechanism       mech;
53
54     gss_initialize();
55
56     *minor_status = 0;
57
58     if (interprocess_token->length == 0 || interprocess_token->value == 0)
59         return (GSS_S_DEFECTIVE_TOKEN);
60
61     status = GSS_S_FAILURE;
62
63     ctx = (gss_union_ctx_id_t) malloc(sizeof(gss_union_ctx_id_desc));
64     if (!ctx) {
65         *minor_status = ENOMEM;
66         goto error_out;
67     }
68     ctx->mech_type = (gss_OID) malloc(sizeof(gss_OID_desc));
69     if (!ctx->mech_type) {
70         *minor_status = ENOMEM;
71         goto error_out;
72     }
73     p = interprocess_token->value;
74     length = *p++;
75     length = (length << 8) + *p++;
76     length = (length << 8) + *p++;
77     length = (length << 8) + *p++;
78
79     ctx->mech_type->length = length;
80     ctx->mech_type->elements = malloc(length);
81     if (!ctx->mech_type->elements) {
82         *minor_status = ENOMEM;
83         goto error_out;
84     }
85     memcpy(ctx->mech_type->elements, p, length);
86     p += length;
87
88     token.length = interprocess_token->length - 4 - length;
89     token.value = p;
90
91     /*
92      * select the approprate underlying mechanism routine and
93      * call it.
94      */
95
96     mech = __gss_get_mechanism (ctx->mech_type);
97     if (!mech) {
98         status = GSS_S_BAD_MECH;
99         goto error_out;
100     }
101     if (!mech->gss_import_sec_context) {
102         status = GSS_S_BAD_BINDINGS;
103         goto error_out;
104     }
105
106 #ifdef USE_MECH_CONTEXT
107     status = mech->gss_import_sec_context(mech->context, minor_status,
108 #else
109     status = mech->gss_import_sec_context(minor_status,
110 #endif
111                                           &token, &ctx->internal_ctx_id);
112
113     if (status == GSS_S_COMPLETE) {
114         *context_handle = ctx;
115         return (GSS_S_COMPLETE);
116     }
117
118 error_out:
119     if (ctx) {
120         if (ctx->mech_type) {
121             if (ctx->mech_type->elements)
122                 free(ctx->mech_type->elements);
123             free(ctx->mech_type);
124         }
125         free(ctx);
126     }
127     return status;
128 }