summaryrefslogtreecommitdiffstats
path: root/third_party/heimdal/lib/gssapi/mech/gssspi_query_meta_data.c
blob: 490bbc9f01ed8e3bb7d63d1e8eba93695fad8427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*-
 * Copyright (c) 2005 Doug Rabson
 * All rights reserved.
 *
 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
 * Portions Copyright (c) 2019 AuriStor, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "mech_locl.h"

GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
gssspi_query_meta_data(
    OM_uint32 *minor_status,
    gss_const_OID input_mech_type,
    gss_cred_id_t input_cred_handle,
    gss_ctx_id_t *context_handle,
    gss_const_name_t target_name,
    OM_uint32 req_flags,
    gss_buffer_t meta_data)
{
    OM_uint32 major_status, junk;
    gssapi_mech_interface m;
    struct _gss_name *name = (struct _gss_name *) target_name;
    struct _gss_mechanism_name *mn;
    struct _gss_context *ctx = (struct _gss_context *) *context_handle;
    gss_cred_id_t cred_handle;
    int allocated_ctx;
    gss_const_OID mech_type = input_mech_type;

    *minor_status = 0;

    _mg_buffer_zero(meta_data);

    if (mech_type == GSS_C_NO_OID)
	return GSS_S_BAD_MECH;

    if (ctx == NULL) {
	ctx = calloc(1, sizeof(struct _gss_context));
	if (ctx == NULL) {
	    *minor_status = ENOMEM;
	    return GSS_S_FAILURE;
	}

	m = ctx->gc_mech = __gss_get_mechanism(mech_type);
	if (m == NULL) {
	    free(ctx);
	    return GSS_S_BAD_MECH;
	}
	allocated_ctx = 1;
    } else {
	m = ctx->gc_mech;
	mech_type = &m->gm_mech_oid;
	allocated_ctx = 0;
    }

    if (m->gm_query_meta_data == NULL) {
	major_status = GSS_S_BAD_MECH;
	goto cleanup;
    }

    major_status = _gss_find_mn(minor_status, name, mech_type, &mn);
    if (major_status != GSS_S_COMPLETE)
	goto cleanup;

    if (m->gm_flags & GM_USE_MG_CRED)
	cred_handle = input_cred_handle;
    else
	cred_handle = _gss_mg_find_mech_cred(input_cred_handle, mech_type);

    if (input_cred_handle != GSS_C_NO_CREDENTIAL &&
	cred_handle == NULL) {
	major_status = GSS_S_NO_CRED;
	goto cleanup;
    }

    /* note: mechanism is not obligated to allocate a context on success */
    major_status = m->gm_query_meta_data(minor_status,
	    mech_type,
	    cred_handle,
	    &ctx->gc_ctx,
	    mn ? mn->gmn_name : GSS_C_NO_NAME,
	    req_flags,
	    meta_data);
    if (major_status != GSS_S_COMPLETE)
	_gss_mg_error(m, *minor_status);

cleanup:
    if (allocated_ctx && major_status != GSS_S_COMPLETE)
	gss_delete_sec_context(&junk, (gss_ctx_id_t *)&ctx, GSS_C_NO_BUFFER);

    *context_handle = (gss_ctx_id_t) ctx;

    _gss_mg_log(10, "gss-qmd: return %d/%d", (int)major_status, (int)*minor_status);

    return major_status;
}