dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libgss / g_imp_sec_context.c
blobe7ac27fc566ab1b3d304ac6eb6a6b84d988c01ef
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
26 * glue routine gss_export_sec_context
29 #ifndef LEAN_CLIENT
31 #include <mechglueP.h>
32 #include "gssapiP_generic.h"
33 #include <stdio.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <string.h>
38 static OM_uint32
39 val_imp_sec_ctx_args(
40 OM_uint32 *minor_status,
41 gss_buffer_t interprocess_token,
42 gss_ctx_id_t *context_handle)
45 /* Initialize outputs. */
46 if (minor_status != NULL)
47 *minor_status = 0;
49 if (context_handle != NULL)
50 *context_handle = GSS_C_NO_CONTEXT;
52 /* Validate arguments. */
54 if (minor_status == NULL)
55 return (GSS_S_CALL_INACCESSIBLE_WRITE);
57 if (context_handle == NULL)
58 return (GSS_S_CALL_INACCESSIBLE_WRITE);
60 if (interprocess_token == GSS_C_NO_BUFFER)
61 return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
63 if (GSS_EMPTY_BUFFER(interprocess_token))
64 return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
66 return (GSS_S_COMPLETE);
69 OM_uint32
70 gss_import_sec_context(minor_status,
71 interprocess_token,
72 context_handle)
74 OM_uint32 * minor_status;
75 const gss_buffer_t interprocess_token;
76 gss_ctx_id_t *context_handle;
79 OM_uint32 length = 0;
80 OM_uint32 status;
81 char *p;
82 gss_union_ctx_id_t ctx;
83 gss_buffer_desc token;
84 gss_mechanism mech;
86 status = val_imp_sec_ctx_args(minor_status,
87 interprocess_token, context_handle);
88 if (status != GSS_S_COMPLETE)
89 return (status);
91 /* Initial value needed below. */
92 status = GSS_S_FAILURE;
94 ctx = (gss_union_ctx_id_t)malloc(sizeof (gss_union_ctx_id_desc));
95 if (!ctx)
96 return (GSS_S_FAILURE);
98 ctx->mech_type = (gss_OID) malloc(sizeof (gss_OID_desc));
99 if (!ctx->mech_type) {
100 free(ctx);
101 return (GSS_S_FAILURE);
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++;
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);
118 ctx->mech_type->length = length;
119 ctx->mech_type->elements = malloc(length);
120 if (!ctx->mech_type->elements) {
121 goto error_out;
123 (void) memcpy(ctx->mech_type->elements, p, length);
124 p += length;
126 token.length = interprocess_token->length - sizeof (OM_uint32) - length;
127 token.value = p;
130 * select the approprate underlying mechanism routine and
131 * call it.
134 mech = __gss_get_mechanism(ctx->mech_type);
135 if (!mech) {
136 status = GSS_S_BAD_MECH;
137 goto error_out;
139 if (!mech->gss_import_sec_context) {
140 status = GSS_S_UNAVAILABLE;
141 goto error_out;
144 status = mech->gss_import_sec_context(mech->context, minor_status,
145 &token, &ctx->internal_ctx_id);
147 if (status == GSS_S_COMPLETE) {
148 *context_handle = (gss_ctx_id_t)ctx;
149 return (GSS_S_COMPLETE);
151 map_error(minor_status, mech);
153 error_out:
154 if (ctx) {
155 if (ctx->mech_type) {
156 if (ctx->mech_type->elements)
157 free(ctx->mech_type->elements);
158 free(ctx->mech_type);
160 free(ctx);
162 return (status);
164 #endif /* LEAN_CLIENT */