gss_duplicate_name.c revision 168340
1153838Sdfr/*-
2153838Sdfr * Copyright (c) 2005 Doug Rabson
3153838Sdfr * All rights reserved.
4153838Sdfr *
5153838Sdfr * Redistribution and use in source and binary forms, with or without
6153838Sdfr * modification, are permitted provided that the following conditions
7153838Sdfr * are met:
8153838Sdfr * 1. Redistributions of source code must retain the above copyright
9153838Sdfr *    notice, this list of conditions and the following disclaimer.
10153838Sdfr * 2. Redistributions in binary form must reproduce the above copyright
11153838Sdfr *    notice, this list of conditions and the following disclaimer in the
12153838Sdfr *    documentation and/or other materials provided with the distribution.
13153838Sdfr *
14153838Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15153838Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16153838Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17153838Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18153838Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19153838Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20153838Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21153838Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22153838Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23153838Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24153838Sdfr * SUCH DAMAGE.
25153838Sdfr *
26153838Sdfr *	$FreeBSD: head/lib/libgssapi/gss_duplicate_name.c 168340 2007-04-04 02:40:59Z kan $
27153838Sdfr */
28153838Sdfr
29153838Sdfr#include <gssapi/gssapi.h>
30168340Skan#include <stdlib.h>
31168340Skan#include <string.h>
32153838Sdfr#include <errno.h>
33153838Sdfr
34153838Sdfr#include "mech_switch.h"
35153838Sdfr#include "name.h"
36153838Sdfr
37153838SdfrOM_uint32 gss_duplicate_name(OM_uint32 *minor_status,
38153838Sdfr    const gss_name_t src_name,
39153838Sdfr    gss_name_t *dest_name)
40153838Sdfr{
41153838Sdfr	OM_uint32		major_status;
42153838Sdfr	struct _gss_name	*name = (struct _gss_name *) src_name;
43153838Sdfr	struct _gss_name	*new_name;
44153838Sdfr	struct _gss_mechanism_name *mn;
45153838Sdfr
46153838Sdfr	*minor_status = 0;
47153838Sdfr
48153838Sdfr	/*
49153838Sdfr	 * If this name has a value (i.e. it didn't come from
50153838Sdfr	 * gss_canonicalize_name(), we re-import the thing. Otherwise,
51153838Sdfr	 * we make an empty name to hold the MN copy.
52153838Sdfr	 */
53153838Sdfr	if (name->gn_value.value) {
54153838Sdfr		major_status = gss_import_name(minor_status,
55153838Sdfr		    &name->gn_value, &name->gn_type, dest_name);
56153838Sdfr		if (major_status != GSS_S_COMPLETE)
57153838Sdfr			return (major_status);
58153838Sdfr		new_name = (struct _gss_name *) *dest_name;
59153838Sdfr	} else {
60153838Sdfr		new_name = malloc(sizeof(struct _gss_name));
61153838Sdfr		if (!new_name) {
62153838Sdfr			*minor_status = ENOMEM;
63153838Sdfr			return (GSS_S_FAILURE);
64153838Sdfr		}
65153838Sdfr		memset(new_name, 0, sizeof(struct _gss_name));
66153838Sdfr		SLIST_INIT(&name->gn_mn);
67153838Sdfr		*dest_name = (gss_name_t) new_name;
68153838Sdfr	}
69153838Sdfr
70153838Sdfr	/*
71153838Sdfr	 * Import the new name into any mechanisms listed in the
72153838Sdfr	 * original name. We could probably get away with only doing
73153838Sdfr	 * this if the original was canonical.
74153838Sdfr	 */
75153838Sdfr	SLIST_FOREACH(mn, &name->gn_mn, gmn_link) {
76153838Sdfr		_gss_find_mn(new_name, mn->gmn_mech_oid);
77153838Sdfr	}
78153838Sdfr
79153838Sdfr	return (GSS_S_COMPLETE);
80153838Sdfr}
81