87db240df3dbaf5049bd752f5de9d3cb7ee5f780
[platform/upstream/krb5.git] / src / lib / gssapi / mechglue / g_set_context_option.c
1 /*
2  * Copyright 2008 by the Massachusetts Institute of Technology.
3  * All Rights Reserved.
4  *
5  * Export of this software from the United States of America may
6  *   require a specific license from the United States Government.
7  *   It is the responsibility of any person or organization contemplating
8  *   export to obtain such a license before exporting.
9  *
10  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
11  * distribute this software and its documentation for any purpose and
12  * without fee is hereby granted, provided that the above copyright
13  * notice appear in all copies and that both that copyright notice and
14  * this permission notice appear in supporting documentation, and that
15  * the name of M.I.T. not be used in advertising or publicity pertaining
16  * to distribution of the software without specific, written prior
17  * permission.  Furthermore if you modify this software you must label
18  * your software as modified software and not distribute it in such a
19  * fashion that it might be confused with the original M.I.T. software.
20  * M.I.T. makes no representations about the suitability of
21  * this software for any purpose.  It is provided "as is" without express
22  * or implied warranty.
23  */
24
25 /* Glue routine for gss_set_sec_context_option */
26
27 #include "mglueP.h"
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #include <string.h>
32 #include <errno.h>
33
34 OM_uint32 KRB5_CALLCONV
35 gss_set_sec_context_option (OM_uint32 *minor_status,
36                             gss_ctx_id_t *context_handle,
37                             const gss_OID desired_object,
38                             const gss_buffer_t value)
39 {
40     OM_uint32           status, minor;
41     gss_union_ctx_id_t  ctx;
42     gss_mechanism       mech;
43     gss_ctx_id_t        internal_ctx = GSS_C_NO_CONTEXT;
44
45     if (minor_status == NULL)
46         return GSS_S_CALL_INACCESSIBLE_WRITE;
47
48     if (context_handle == NULL)
49         return GSS_S_CALL_INACCESSIBLE_WRITE;
50
51     *minor_status = 0;
52
53     /*
54      * select the approprate underlying mechanism routine and
55      * call it.
56      */
57
58     ctx = (gss_union_ctx_id_t) *context_handle;
59     if (ctx == NULL) {
60         mech = gssint_get_mechanism (GSS_C_NO_OID);
61     } else {
62         mech = gssint_get_mechanism (ctx->mech_type);
63     }
64
65     if (mech == NULL)
66         return GSS_S_BAD_MECH;
67     if (mech->gss_set_sec_context_option == NULL)
68         return GSS_S_UNAVAILABLE;
69
70     status = mech->gss_set_sec_context_option(minor_status,
71                                               ctx ? &ctx->internal_ctx_id :
72                                               &internal_ctx,
73                                               desired_object,
74                                               value);
75     if (status == GSS_S_COMPLETE) {
76         if (ctx == NULL && internal_ctx != GSS_C_NO_CONTEXT) {
77             /* Allocate a union context handle to wrap new context */
78             ctx = (gss_union_ctx_id_t)malloc(sizeof(*ctx));
79             if (ctx == NULL) {
80                 *minor_status = ENOMEM;
81                 gssint_delete_internal_sec_context(&minor,
82                                                    &mech->mech_type,
83                                                    &internal_ctx,
84                                                    GSS_C_NO_BUFFER);
85                 return GSS_S_FAILURE;
86             }
87
88             status = generic_gss_copy_oid(minor_status,
89                                           &mech->mech_type,
90                                           &ctx->mech_type);
91             if (status != GSS_S_COMPLETE) {
92                 gssint_delete_internal_sec_context(&minor,
93                                                    ctx->mech_type,
94                                                    &internal_ctx,
95                                                    GSS_C_NO_BUFFER);
96                 free(ctx);
97                 return status;
98             }
99
100             ctx->internal_ctx_id = internal_ctx;
101             *context_handle = (gss_ctx_id_t)ctx;
102         }
103     } else
104         map_error(minor_status, mech);
105
106     return status;
107 }