1610be5538723277082283587694ace7336871f4
[sfrench/samba-autobuild/.git] / source4 / 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 RCSID("$Id: gss_inquire_cred.c 23025 2008-04-17 10:01:57Z lha $");
31
32 #define AUSAGE 1
33 #define IUSAGE 2
34
35 static void
36 updateusage(gss_cred_usage_t usage, int *usagemask)
37 {
38     if (usage == GSS_C_BOTH)
39         *usagemask |= AUSAGE | IUSAGE;
40     else if (usage == GSS_C_ACCEPT)
41         *usagemask |= AUSAGE;
42     else if (usage == GSS_C_INITIATE)
43         *usagemask |= IUSAGE;
44 }
45
46 OM_uint32 GSSAPI_LIB_FUNCTION
47 gss_inquire_cred(OM_uint32 *minor_status,
48     const gss_cred_id_t cred_handle,
49     gss_name_t *name_ret,
50     OM_uint32 *lifetime,
51     gss_cred_usage_t *cred_usage,
52     gss_OID_set *mechanisms)
53 {
54         OM_uint32 major_status;
55         struct _gss_mech_switch *m;
56         struct _gss_cred *cred = (struct _gss_cred *) cred_handle;
57         struct _gss_name *name;
58         struct _gss_mechanism_name *mn;
59         OM_uint32 min_lifetime;
60         int found = 0;
61         int usagemask = 0;
62         gss_cred_usage_t usage;
63
64         _gss_load_mech();
65
66         *minor_status = 0;
67         if (name_ret)
68                 *name_ret = GSS_C_NO_NAME;
69         if (lifetime)
70                 *lifetime = 0;
71         if (cred_usage)
72                 *cred_usage = 0;
73         if (mechanisms)
74                 *mechanisms = GSS_C_NO_OID_SET;
75
76         if (name_ret) {
77                 name = calloc(1, sizeof(*name));
78                 if (name == NULL) {
79                         *minor_status = ENOMEM;
80                         return (GSS_S_FAILURE);
81                 }
82                 SLIST_INIT(&name->gn_mn);
83         } else {
84                 name = NULL;
85         }
86
87         if (mechanisms) {
88                 major_status = gss_create_empty_oid_set(minor_status,
89                     mechanisms);
90                 if (major_status) {
91                         if (name) free(name);
92                         return (major_status);
93                 }
94         }
95
96         min_lifetime = GSS_C_INDEFINITE;
97         if (cred) {
98                 struct _gss_mechanism_cred *mc;
99
100                 SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
101                         gss_name_t mc_name;
102                         OM_uint32 mc_lifetime;
103
104                         major_status = mc->gmc_mech->gm_inquire_cred(minor_status,
105                             mc->gmc_cred, &mc_name, &mc_lifetime, &usage, NULL);
106                         if (major_status)
107                                 continue;
108
109                         updateusage(usage, &usagemask);
110                         if (name) {
111                                 mn = malloc(sizeof(struct _gss_mechanism_name));
112                                 if (!mn) {
113                                         mc->gmc_mech->gm_release_name(minor_status,
114                                             &mc_name);
115                                         continue;
116                                 }
117                                 mn->gmn_mech = mc->gmc_mech;
118                                 mn->gmn_mech_oid = mc->gmc_mech_oid;
119                                 mn->gmn_name = mc_name;
120                                 SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
121                         } else {
122                                 mc->gmc_mech->gm_release_name(minor_status,
123                                     &mc_name);
124                         }
125
126                         if (mc_lifetime < min_lifetime)
127                                 min_lifetime = mc_lifetime;
128
129                         if (mechanisms)
130                                 gss_add_oid_set_member(minor_status,
131                                     mc->gmc_mech_oid, mechanisms);
132                         found++;
133                 }
134         } else {
135                 SLIST_FOREACH(m, &_gss_mechs, gm_link) {
136                         gss_name_t mc_name;
137                         OM_uint32 mc_lifetime;
138
139                         major_status = m->gm_mech.gm_inquire_cred(minor_status,
140                             GSS_C_NO_CREDENTIAL, &mc_name, &mc_lifetime,
141                             &usage, NULL);
142                         if (major_status)
143                                 continue;
144
145                         updateusage(usage, &usagemask);
146                         if (name && mc_name) {
147                                 mn = malloc(
148                                         sizeof(struct _gss_mechanism_name));
149                                 if (!mn) {
150                                         m->gm_mech.gm_release_name(
151                                                 minor_status, &mc_name);
152                                         continue;
153                                 }
154                                 mn->gmn_mech = &m->gm_mech;
155                                 mn->gmn_mech_oid = &m->gm_mech_oid;
156                                 mn->gmn_name = mc_name;
157                                 SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
158                         } else if (mc_name) {
159                                 m->gm_mech.gm_release_name(minor_status,
160                                     &mc_name);
161                         }
162
163                         if (mc_lifetime < min_lifetime)
164                                 min_lifetime = mc_lifetime;
165
166                         if (mechanisms)
167                                 gss_add_oid_set_member(minor_status,
168                                     &m->gm_mech_oid, mechanisms);
169                         found++;
170                 }
171         }
172
173         if (found == 0) {
174                 gss_name_t n = (gss_name_t)name;
175                 if (n)
176                         gss_release_name(minor_status, &n);
177                 gss_release_oid_set(minor_status, mechanisms);
178                 *minor_status = 0;
179                 return (GSS_S_NO_CRED);
180         }
181
182         *minor_status = 0;
183         if (name_ret)
184                 *name_ret = (gss_name_t) name;
185         if (lifetime)
186                 *lifetime = min_lifetime;
187         if (cred_usage) {
188                 if ((usagemask & (AUSAGE|IUSAGE)) == (AUSAGE|IUSAGE))
189                         *cred_usage = GSS_C_BOTH;
190                 else if (usagemask & IUSAGE)
191                         *cred_usage = GSS_C_INITIATE;
192                 else if (usagemask & AUSAGE)
193                         *cred_usage = GSS_C_ACCEPT;
194         }
195         return (GSS_S_COMPLETE);
196 }