1139790Simp/*-
2738Sache * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
34Srgrimes * Authors: Doug Rabson <dfr@rabson.org>
4738Sache * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
5738Sache *
6106323Smdodd * Redistribution and use in source and binary forms, with or without
74Srgrimes * modification, are permitted provided that the following conditions
84Srgrimes * are met:
9115703Sobrien * 1. Redistributions of source code must retain the above copyright
10115703Sobrien *    notice, this list of conditions and the following disclaimer.
11115703Sobrien * 2. Redistributions in binary form must reproduce the above copyright
122056Swollman *    notice, this list of conditions and the following disclaimer in the
132056Swollman *    documentation and/or other materials provided with the distribution.
142056Swollman *
1561994Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
162056Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1712675Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1852843Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1960038Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
207090Sbde * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21152306Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2312675Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2412675Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2512675Sjulian * SUCH DAMAGE.
2612675Sjulian */
2712502Sjulian
2847625Sphk#include <sys/cdefs.h>
29126080Sphk__FBSDID("$FreeBSD$");
30126080Sphk
31111815Sphk#include <sys/param.h>
32111815Sphk#include <sys/kernel.h>
33111815Sphk#include <sys/kobj.h>
34111815Sphk#include <sys/lock.h>
35111815Sphk#include <sys/malloc.h>
3647625Sphk#include <sys/mutex.h>
3712675Sjulian#include <sys/proc.h>
3869774Sphk
3960038Sphk#include <kgssapi/gssapi.h>
40179004Sphk#include <kgssapi/gssapi_impl.h>
41179004Sphk
424Srgrimes#include "gssd.h"
4319174Sbde
444SrgrimesOM_uint32
454Srgrimesgss_acquire_cred(OM_uint32 *minor_status,
464Srgrimes    const gss_name_t desired_name,
474Srgrimes    OM_uint32 time_req,
4819174Sbde    const gss_OID_set desired_mechs,
494Srgrimes    gss_cred_usage_t cred_usage,
504Srgrimes    gss_cred_id_t *output_cred_handle,
514Srgrimes    gss_OID_set *actual_mechs,
524Srgrimes    OM_uint32 *time_rec)
53766Sache{
54766Sache	OM_uint32 major_status;
554Srgrimes	struct acquire_cred_res res;
56170278Sbrian	struct acquire_cred_args args;
57170278Sbrian	enum clnt_stat stat;
5892765Salfred	gss_cred_id_t cred;
5992765Salfred	int i;
6092765Salfred	CLIENT *cl;
6112854Sbde
62179004Sphk	*minor_status = 0;
63179004Sphk	cl = kgss_gssd_client();
64179004Sphk	if (cl == NULL)
6517232Sjoerg		return (GSS_S_FAILURE);
66179004Sphk
674Srgrimes	args.uid = curthread->td_ucred->cr_uid;
68179004Sphk	if (desired_name)
694Srgrimes		args.desired_name = desired_name->handle;
70179004Sphk	else
71179004Sphk		args.desired_name = 0;
728288Sdg	args.time_req = time_req;
734Srgrimes	args.desired_mechs = desired_mechs;
74179004Sphk	args.cred_usage = cred_usage;
754Srgrimes
764Srgrimes	bzero(&res, sizeof(res));
77179004Sphk	stat = gssd_acquire_cred_1(&args, &res, cl);
78179004Sphk	CLNT_RELEASE(cl);
791393Ssos	if (stat != RPC_SUCCESS) {
80179004Sphk		*minor_status = stat;
81179004Sphk		return (GSS_S_FAILURE);
82179004Sphk	}
83179004Sphk
84179004Sphk	if (res.major_status != GSS_S_COMPLETE) {
8517232Sjoerg		*minor_status = res.minor_status;
86179004Sphk		return (res.major_status);
87179004Sphk	}
88179004Sphk
894Srgrimes	cred = malloc(sizeof(struct _gss_cred_id_t), M_GSSAPI, M_WAITOK);
90179004Sphk	cred->handle = res.output_cred;
91179004Sphk	*output_cred_handle = cred;
92179004Sphk	if (actual_mechs) {
93179004Sphk		major_status = gss_create_empty_oid_set(minor_status,
94179004Sphk		    actual_mechs);
95179004Sphk		if (major_status)
96179004Sphk			return (major_status);
97179004Sphk		for (i = 0; i < res.actual_mechs->count; i++) {
98179004Sphk			major_status = gss_add_oid_set_member(minor_status,
99179004Sphk			    &res.actual_mechs->elements[i], actual_mechs);
100179004Sphk			if (major_status)
1014Srgrimes				return (major_status);
1024Srgrimes		}
103179004Sphk	}
104179004Sphk	if (time_rec)
105179004Sphk		*time_rec = res.time_rec;
10617232Sjoerg
107179004Sphk	xdr_free((xdrproc_t) xdr_acquire_cred_res, &res);
1084Srgrimes
109179004Sphk	return (GSS_S_COMPLETE);
110170278Sbrian}
111179004Sphk