HEIMDAL: move code from source4/heimdal* to third_party/heimdal*
[samba.git] / third_party / heimdal / lib / gssapi / mech / gss_inquire_cred.c
1 /*-
2  * Copyright (c) 2005 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/lib/libgssapi/gss_inquire_cred.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
27  */
28
29 #include "mech_locl.h"
30
31 #define AUSAGE 1
32 #define IUSAGE 2
33
34 static void
35 updateusage(gss_cred_usage_t usage, int *usagemask)
36 {
37     if (usage == GSS_C_BOTH)
38         *usagemask |= AUSAGE | IUSAGE;
39     else if (usage == GSS_C_ACCEPT)
40         *usagemask |= AUSAGE;
41     else if (usage == GSS_C_INITIATE)
42         *usagemask |= IUSAGE;
43 }
44
45 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
46 gss_inquire_cred(OM_uint32 *minor_status,
47     gss_const_cred_id_t cred_handle,
48     gss_name_t *name_ret,
49     OM_uint32 *lifetime,
50     gss_cred_usage_t *cred_usage,
51     gss_OID_set *mechanisms)
52 {
53         OM_uint32 major_status;
54         struct _gss_mech_switch *m;
55         struct _gss_cred *cred = (struct _gss_cred *) cred_handle;
56         struct _gss_name *name;
57         struct _gss_mechanism_name *mn;
58         OM_uint32 min_lifetime;
59         int found = FALSE;
60         int usagemask = 0;
61         gss_cred_usage_t usage;
62
63         _gss_load_mech();
64
65         *minor_status = 0;
66         if (name_ret)
67                 *name_ret = GSS_C_NO_NAME;
68         if (lifetime)
69                 *lifetime = 0;
70         if (cred_usage)
71                 *cred_usage = 0;
72         if (mechanisms)
73                 *mechanisms = GSS_C_NO_OID_SET;
74
75         if (name_ret) {
76                 name = _gss_create_name(NULL, NULL);
77                 if (name == NULL) {
78                         *minor_status = ENOMEM;
79                         return (GSS_S_FAILURE);
80                 }
81         } else {
82                 name = NULL;
83         }
84
85         if (mechanisms) {
86                 major_status = gss_create_empty_oid_set(minor_status,
87                     mechanisms);
88                 if (major_status) {
89                         if (name) free(name);
90                         return (major_status);
91                 }
92         }
93
94         min_lifetime = GSS_C_INDEFINITE;
95         if (cred) {
96                 struct _gss_mechanism_cred *mc;
97
98                 HEIM_TAILQ_FOREACH(mc, &cred->gc_mc, gmc_link) {
99                         gss_name_t mc_name = GSS_C_NO_NAME;
100                         OM_uint32 mc_lifetime = GSS_C_INDEFINITE;
101
102                         heim_assert((mc->gmc_mech->gm_flags & GM_USE_MG_CRED) == 0,
103                                     "should not have mech creds for GM_USE_MG_CRED mechs");
104
105                         if (mc->gmc_mech->gm_inquire_cred == NULL)
106                                 continue;
107
108                         major_status = mc->gmc_mech->gm_inquire_cred(minor_status,
109                             mc->gmc_cred, &mc_name, &mc_lifetime, &usage, NULL);
110                         if (major_status)
111                                 continue;
112
113                         updateusage(usage, &usagemask);
114                         if (name) {
115                                 mn = malloc(sizeof(struct _gss_mechanism_name));
116                                 if (!mn) {
117                                         mc->gmc_mech->gm_release_name(minor_status,
118                                             &mc_name);
119                                         continue;
120                                 }
121                                 mn->gmn_mech = mc->gmc_mech;
122                                 mn->gmn_mech_oid = mc->gmc_mech_oid;
123                                 mn->gmn_name = mc_name;
124                                 HEIM_TAILQ_INSERT_TAIL(&name->gn_mn, mn, gmn_link);
125                         } else {
126                                 mc->gmc_mech->gm_release_name(minor_status,
127                                     &mc_name);
128                         }
129
130                         if (mc_lifetime < min_lifetime)
131                                 min_lifetime = mc_lifetime;
132
133                         if (mechanisms)
134                                 gss_add_oid_set_member(minor_status,
135                                     mc->gmc_mech_oid, mechanisms);
136                         found = TRUE;
137                 }
138         } else {
139                 HEIM_TAILQ_FOREACH(m, &_gss_mechs, gm_link) {
140                         gss_name_t mc_name;
141                         OM_uint32 mc_lifetime;
142
143                         if (m->gm_mech.gm_inquire_cred == NULL ||
144                             (m->gm_mech.gm_flags & GM_USE_MG_CRED))
145                                 continue;
146
147                         major_status = m->gm_mech.gm_inquire_cred(minor_status,
148                             GSS_C_NO_CREDENTIAL, &mc_name, &mc_lifetime,
149                             &usage, NULL);
150                         if (major_status)
151                                 continue;
152
153                         updateusage(usage, &usagemask);
154                         if (name && mc_name) {
155                                 mn = malloc(
156                                         sizeof(struct _gss_mechanism_name));
157                                 if (!mn) {
158                                         m->gm_mech.gm_release_name(
159                                                 minor_status, &mc_name);
160                                         continue;
161                                 }
162                                 mn->gmn_mech = &m->gm_mech;
163                                 mn->gmn_mech_oid = m->gm_mech_oid;
164                                 mn->gmn_name = mc_name;
165                                 HEIM_TAILQ_INSERT_TAIL(&name->gn_mn, mn, gmn_link);
166                         } else if (mc_name) {
167                                 m->gm_mech.gm_release_name(minor_status,
168                                     &mc_name);
169                         }
170
171                         if (mc_lifetime < min_lifetime)
172                                 min_lifetime = mc_lifetime;
173
174                         if (mechanisms)
175                                 gss_add_oid_set_member(minor_status,
176                                     m->gm_mech_oid, mechanisms);
177                         found = TRUE;
178                 }
179         }
180
181         if (found && mechanisms) {
182                 /* GM_USE_MG_CRED mechs (SPNEGO) always can be used */
183                 HEIM_TAILQ_FOREACH(m, &_gss_mechs, gm_link) {
184                         if ((m->gm_mech.gm_flags & GM_USE_MG_CRED) == 0)
185                                 continue;
186
187                         gss_add_oid_set_member(minor_status,
188                                                m->gm_mech_oid, mechanisms);
189                 }
190         }
191
192         if (found == FALSE || min_lifetime == 0) {
193                 gss_name_t n = (gss_name_t)name;
194                 if (n)
195                         gss_release_name(minor_status, &n);
196                 gss_release_oid_set(minor_status, mechanisms);
197                 *minor_status = 0;
198                 if (min_lifetime == 0)
199                         return (GSS_S_CREDENTIALS_EXPIRED);
200
201                 return (GSS_S_NO_CRED);
202         }
203
204         *minor_status = 0;
205         if (name_ret)
206                 *name_ret = (gss_name_t) name;
207         if (lifetime)
208                 *lifetime = min_lifetime;
209         if (cred_usage) {
210                 if ((usagemask & (AUSAGE|IUSAGE)) == (AUSAGE|IUSAGE))
211                         *cred_usage = GSS_C_BOTH;
212                 else if (usagemask & IUSAGE)
213                         *cred_usage = GSS_C_INITIATE;
214                 else if (usagemask & AUSAGE)
215                         *cred_usage = GSS_C_ACCEPT;
216         }
217         return (GSS_S_COMPLETE);
218 }