Fix autoconf 2.70 compatibility
[platform/upstream/krb5.git] / src / lib / gssapi / mechglue / g_imp_sec_context.c
1 /* #pragma ident        "@(#)g_imp_sec_context.c        1.18    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 gss_export_sec_context
27  */
28
29 #ifndef LEAN_CLIENT
30
31 #include "mglueP.h"
32 #include <stdio.h>
33 #include <errno.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #include <string.h>
38
39 static OM_uint32
40 val_imp_sec_ctx_args(
41     OM_uint32 *minor_status,
42     gss_buffer_t interprocess_token,
43     gss_ctx_id_t *context_handle)
44 {
45
46     /* Initialize outputs. */
47     if (minor_status != NULL)
48         *minor_status = 0;
49
50     if (context_handle != NULL)
51         *context_handle = GSS_C_NO_CONTEXT;
52
53     /* Validate arguments. */
54
55     if (minor_status == NULL)
56         return (GSS_S_CALL_INACCESSIBLE_WRITE);
57
58     if (context_handle == NULL)
59         return (GSS_S_CALL_INACCESSIBLE_WRITE);
60
61     if (interprocess_token == GSS_C_NO_BUFFER)
62         return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
63
64     if (GSS_EMPTY_BUFFER(interprocess_token))
65         return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
66
67     return (GSS_S_COMPLETE);
68 }
69
70
71 OM_uint32 KRB5_CALLCONV
72 gss_import_sec_context(minor_status,
73                        interprocess_token,
74                        context_handle)
75
76 OM_uint32 *             minor_status;
77 gss_buffer_t            interprocess_token;
78 gss_ctx_id_t *          context_handle;
79
80 {
81     OM_uint32           length = 0;
82     OM_uint32           status;
83     char                *p;
84     gss_union_ctx_id_t  ctx;
85     gss_ctx_id_t        mctx;
86     gss_buffer_desc     token;
87     gss_OID_desc        token_mech;
88     gss_OID             selected_mech = GSS_C_NO_OID;
89     gss_OID             public_mech;
90     gss_mechanism       mech;
91
92     status = val_imp_sec_ctx_args(minor_status,
93                                   interprocess_token, context_handle);
94     if (status != GSS_S_COMPLETE)
95         return (status);
96
97     /* Initial value needed below. */
98     status = GSS_S_FAILURE;
99
100     ctx = (gss_union_ctx_id_t) malloc(sizeof(gss_union_ctx_id_desc));
101     if (!ctx)
102         return (GSS_S_FAILURE);
103
104     if (interprocess_token->length >= sizeof (OM_uint32)) {
105         p = interprocess_token->value;
106         length = (OM_uint32)*p++;
107         length = (OM_uint32)(length << 8) + *p++;
108         length = (OM_uint32)(length << 8) + *p++;
109         length = (OM_uint32)(length << 8) + *p++;
110     }
111
112     if (length == 0 ||
113         length > (interprocess_token->length - sizeof (OM_uint32))) {
114         free(ctx);
115         return (GSS_S_CALL_BAD_STRUCTURE | GSS_S_DEFECTIVE_TOKEN);
116     }
117
118     token_mech.length = length;
119     token_mech.elements = p;
120
121     p += length;
122
123     token.length = interprocess_token->length - sizeof (OM_uint32) - length;
124     token.value = p;
125
126     /*
127      * select the approprate underlying mechanism routine and
128      * call it.
129      */
130
131     status = gssint_select_mech_type(minor_status, &token_mech,
132                                      &selected_mech);
133     if (status != GSS_S_COMPLETE)
134         goto error_out;
135
136     mech = gssint_get_mechanism(selected_mech);
137     if (!mech) {
138         status = GSS_S_BAD_MECH;
139         goto error_out;
140     }
141     if (!mech->gssspi_import_sec_context_by_mech &&
142         !mech->gss_import_sec_context) {
143         status = GSS_S_UNAVAILABLE;
144         goto error_out;
145     }
146
147     if (generic_gss_copy_oid(minor_status, selected_mech,
148                              &ctx->mech_type) != GSS_S_COMPLETE) {
149         status = GSS_S_FAILURE;
150         goto error_out;
151     }
152
153     if (mech->gssspi_import_sec_context_by_mech) {
154         public_mech = gssint_get_public_oid(selected_mech);
155         status = mech->gssspi_import_sec_context_by_mech(minor_status,
156                                                          public_mech,
157                                                          &token, &mctx);
158     } else {
159         status = mech->gss_import_sec_context(minor_status, &token, &mctx);
160     }
161     if (status == GSS_S_COMPLETE) {
162         ctx->internal_ctx_id = mctx;
163         ctx->loopback = ctx;
164         *context_handle = (gss_ctx_id_t)ctx;
165         return (GSS_S_COMPLETE);
166     }
167     map_error(minor_status, mech);
168     free(ctx->mech_type->elements);
169     free(ctx->mech_type);
170
171 error_out:
172     free(ctx);
173     return status;
174 }
175 #endif /* LEAN_CLIENT */