1/*
2 * Copyright (c) 2011 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2011 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of KTH nor the names of its contributors may be
20 *    used to endorse or promote products derived from this software without
21 *    specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#include "mech_locl.h"
37#include "heim_threads.h"
38#include "heimbase.h"
39
40
41void
42_gss_mg_release_cred(struct _gss_cred *cred)
43{
44	struct _gss_mechanism_cred *mc;
45	OM_uint32 junk;
46
47	while (HEIM_SLIST_FIRST(&cred->gc_mc)) {
48		mc = HEIM_SLIST_FIRST(&cred->gc_mc);
49		HEIM_SLIST_REMOVE_HEAD(&cred->gc_mc, gmc_link);
50		mc->gmc_mech->gm_release_cred(&junk, &mc->gmc_cred);
51		free(mc);
52	}
53	free(cred);
54}
55
56struct _gss_cred *
57_gss_mg_alloc_cred(void)
58{
59	struct _gss_cred *cred;
60	cred = malloc(sizeof(struct _gss_cred));
61	if (!cred)
62		return NULL;
63	HEIM_SLIST_INIT(&cred->gc_mc);
64
65	return cred;
66}
67
68void
69_gss_mg_check_credential(gss_cred_id_t credential)
70{
71	if (credential == NULL) return;
72}
73
74gss_name_t
75_gss_cred_copy_name(OM_uint32 *minor_status,
76		    gss_cred_id_t credential,
77		    gss_const_OID mech)
78{
79	struct _gss_cred *cred = (struct _gss_cred *)credential;
80	struct _gss_mechanism_cred *mc;
81	struct _gss_name *name;
82	OM_uint32 major_status;
83
84	name = _gss_create_name(NULL, NULL);
85	if (name == NULL)
86		return NULL;
87
88	HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
89		struct _gss_mechanism_name *mn;
90		gss_name_t mc_name;
91
92		if (mech && !gss_oid_equal(mech, mc->gmc_mech_oid))
93			continue;
94
95		major_status = mc->gmc_mech->gm_inquire_cred(minor_status,
96			mc->gmc_cred, &mc_name, NULL, NULL, NULL);
97		if (major_status)
98			continue;
99
100		mn = malloc(sizeof(struct _gss_mechanism_name));
101		if (!mn) {
102			mc->gmc_mech->gm_release_name(minor_status, &mc_name);
103			continue;
104		}
105		mn->gmn_mech = mc->gmc_mech;
106		mn->gmn_mech_oid = mc->gmc_mech_oid;
107		mn->gmn_name = mc_name;
108		HEIM_SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
109	}
110	if (HEIM_SLIST_EMPTY(&name->gn_mn)) {
111		_gss_mg_release_name(name);
112		return NULL;
113	}
114
115	return (gss_name_t)name;
116}
117