e4517bee449cbcb364d4997c7b141ee22a130813
[sfrench/samba-autobuild/.git] / source4 / heimdal / lib / gssapi / mech / context.c
1 #include "mech/mech_locl.h"
2 #include "heim_threads.h"
3
4 RCSID("$Id: context.c 21248 2007-06-21 00:45:13Z lha $");
5
6 struct mg_thread_ctx {
7     gss_OID mech;
8     OM_uint32 maj_stat;
9     OM_uint32 min_stat;
10     gss_buffer_desc maj_error;
11     gss_buffer_desc min_error;
12 };
13
14 static HEIMDAL_MUTEX context_mutex = HEIMDAL_MUTEX_INITIALIZER;
15 static int created_key;
16 static HEIMDAL_thread_key context_key;
17
18
19 static void
20 destroy_context(void *ptr)
21 {
22     struct mg_thread_ctx *mg = ptr;
23     OM_uint32 junk;
24
25     if (mg == NULL)
26         return;
27
28     gss_release_buffer(&junk, &mg->maj_error);
29     gss_release_buffer(&junk, &mg->min_error);
30     free(mg);
31 }
32
33
34 static struct mg_thread_ctx *
35 _gss_mechglue_thread(void)
36 {
37     struct mg_thread_ctx *ctx;
38     int ret = 0;
39
40     HEIMDAL_MUTEX_lock(&context_mutex);
41
42     if (!created_key) {
43         HEIMDAL_key_create(&context_key, destroy_context, ret);
44         if (ret) {
45             HEIMDAL_MUTEX_unlock(&context_mutex);
46             return NULL;
47         }
48         created_key = 1;
49     }
50     HEIMDAL_MUTEX_unlock(&context_mutex);
51
52     ctx = HEIMDAL_getspecific(context_key);
53     if (ctx == NULL) {
54
55         ctx = calloc(1, sizeof(*ctx));
56         if (ctx == NULL)
57             return NULL;
58         HEIMDAL_setspecific(context_key, ctx, ret);
59         if (ret) {
60             free(ctx);
61             return NULL;
62         }
63     }
64     return ctx;
65 }
66
67 OM_uint32
68 _gss_mg_get_error(const gss_OID mech, OM_uint32 type,
69                   OM_uint32 value, gss_buffer_t string)
70 {
71     struct mg_thread_ctx *mg;
72
73     mg = _gss_mechglue_thread();
74     if (mg == NULL)
75         return GSS_S_BAD_STATUS;
76
77     if (mech != NULL && gss_oid_equal(mg->mech, mech) == 0)
78         return GSS_S_BAD_STATUS;
79
80     switch (type) {
81     case GSS_C_GSS_CODE: {
82         if (value != mg->maj_stat || mg->maj_error.length == 0)
83             break;
84         string->value = malloc(mg->maj_error.length);
85         string->length = mg->maj_error.length;
86         memcpy(string->value, mg->maj_error.value, mg->maj_error.length);
87         return GSS_S_COMPLETE;
88     }
89     case GSS_C_MECH_CODE: {
90         if (value != mg->min_stat || mg->min_error.length == 0)
91             break;
92         string->value = malloc(mg->min_error.length);
93         string->length = mg->min_error.length;
94         memcpy(string->value, mg->min_error.value, mg->min_error.length);
95         return GSS_S_COMPLETE;
96     }
97     }
98     string->value = NULL;
99     string->length = 0;
100     return GSS_S_BAD_STATUS;
101 }
102
103 void
104 _gss_mg_error(gssapi_mech_interface m, OM_uint32 maj, OM_uint32 min)
105 {
106     OM_uint32 major_status, minor_status;
107     OM_uint32 message_content;
108     struct mg_thread_ctx *mg;
109
110     mg = _gss_mechglue_thread();
111     if (mg == NULL)
112         return;
113
114     gss_release_buffer(&minor_status, &mg->maj_error);
115     gss_release_buffer(&minor_status, &mg->min_error);
116
117     mg->mech = &m->gm_mech_oid;
118     mg->maj_stat = maj;
119     mg->min_stat = min;
120
121     major_status = m->gm_display_status(&minor_status,
122                                         maj, 
123                                         GSS_C_GSS_CODE,
124                                         &m->gm_mech_oid,
125                                         &message_content,
126                                         &mg->maj_error);
127     if (GSS_ERROR(major_status)) {
128         mg->maj_error.value = NULL;
129         mg->maj_error.length = 0;
130     }
131     major_status = m->gm_display_status(&minor_status,
132                                         min, 
133                                         GSS_C_MECH_CODE,
134                                         &m->gm_mech_oid,
135                                         &message_content,
136                                         &mg->min_error);
137     if (GSS_ERROR(major_status)) {
138         mg->min_error.value = NULL;
139         mg->min_error.length = 0;
140     }
141 }