xref: /freebsd/lib/libgssapi/gss_duplicate_name.c (revision c0b9f4fe659b6839541970eb5675e57f4d814969)
1c0b9f4feSDoug Rabson /*-
2c0b9f4feSDoug Rabson  * Copyright (c) 2005 Doug Rabson
3c0b9f4feSDoug Rabson  * All rights reserved.
4c0b9f4feSDoug Rabson  *
5c0b9f4feSDoug Rabson  * Redistribution and use in source and binary forms, with or without
6c0b9f4feSDoug Rabson  * modification, are permitted provided that the following conditions
7c0b9f4feSDoug Rabson  * are met:
8c0b9f4feSDoug Rabson  * 1. Redistributions of source code must retain the above copyright
9c0b9f4feSDoug Rabson  *    notice, this list of conditions and the following disclaimer.
10c0b9f4feSDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
11c0b9f4feSDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
12c0b9f4feSDoug Rabson  *    documentation and/or other materials provided with the distribution.
13c0b9f4feSDoug Rabson  *
14c0b9f4feSDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15c0b9f4feSDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16c0b9f4feSDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17c0b9f4feSDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18c0b9f4feSDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c0b9f4feSDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20c0b9f4feSDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21c0b9f4feSDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22c0b9f4feSDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23c0b9f4feSDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24c0b9f4feSDoug Rabson  * SUCH DAMAGE.
25c0b9f4feSDoug Rabson  *
26c0b9f4feSDoug Rabson  *	$FreeBSD$
27c0b9f4feSDoug Rabson  */
28c0b9f4feSDoug Rabson 
29c0b9f4feSDoug Rabson #include <gssapi/gssapi.h>
30c0b9f4feSDoug Rabson #include <errno.h>
31c0b9f4feSDoug Rabson 
32c0b9f4feSDoug Rabson #include "mech_switch.h"
33c0b9f4feSDoug Rabson #include "name.h"
34c0b9f4feSDoug Rabson 
35c0b9f4feSDoug Rabson OM_uint32 gss_duplicate_name(OM_uint32 *minor_status,
36c0b9f4feSDoug Rabson     const gss_name_t src_name,
37c0b9f4feSDoug Rabson     gss_name_t *dest_name)
38c0b9f4feSDoug Rabson {
39c0b9f4feSDoug Rabson 	OM_uint32		major_status;
40c0b9f4feSDoug Rabson 	struct _gss_name	*name = (struct _gss_name *) src_name;
41c0b9f4feSDoug Rabson 	struct _gss_name	*new_name;
42c0b9f4feSDoug Rabson 	struct _gss_mechanism_name *mn;
43c0b9f4feSDoug Rabson 
44c0b9f4feSDoug Rabson 	*minor_status = 0;
45c0b9f4feSDoug Rabson 
46c0b9f4feSDoug Rabson 	/*
47c0b9f4feSDoug Rabson 	 * If this name has a value (i.e. it didn't come from
48c0b9f4feSDoug Rabson 	 * gss_canonicalize_name(), we re-import the thing. Otherwise,
49c0b9f4feSDoug Rabson 	 * we make an empty name to hold the MN copy.
50c0b9f4feSDoug Rabson 	 */
51c0b9f4feSDoug Rabson 	if (name->gn_value.value) {
52c0b9f4feSDoug Rabson 		major_status = gss_import_name(minor_status,
53c0b9f4feSDoug Rabson 		    &name->gn_value, &name->gn_type, dest_name);
54c0b9f4feSDoug Rabson 		if (major_status != GSS_S_COMPLETE)
55c0b9f4feSDoug Rabson 			return (major_status);
56c0b9f4feSDoug Rabson 		new_name = (struct _gss_name *) *dest_name;
57c0b9f4feSDoug Rabson 	} else {
58c0b9f4feSDoug Rabson 		new_name = malloc(sizeof(struct _gss_name));
59c0b9f4feSDoug Rabson 		if (!new_name) {
60c0b9f4feSDoug Rabson 			*minor_status = ENOMEM;
61c0b9f4feSDoug Rabson 			return (GSS_S_FAILURE);
62c0b9f4feSDoug Rabson 		}
63c0b9f4feSDoug Rabson 		memset(new_name, 0, sizeof(struct _gss_name));
64c0b9f4feSDoug Rabson 		SLIST_INIT(&name->gn_mn);
65c0b9f4feSDoug Rabson 		*dest_name = (gss_name_t) new_name;
66c0b9f4feSDoug Rabson 	}
67c0b9f4feSDoug Rabson 
68c0b9f4feSDoug Rabson 	/*
69c0b9f4feSDoug Rabson 	 * Import the new name into any mechanisms listed in the
70c0b9f4feSDoug Rabson 	 * original name. We could probably get away with only doing
71c0b9f4feSDoug Rabson 	 * this if the original was canonical.
72c0b9f4feSDoug Rabson 	 */
73c0b9f4feSDoug Rabson 	SLIST_FOREACH(mn, &name->gn_mn, gmn_link) {
74c0b9f4feSDoug Rabson 		_gss_find_mn(new_name, mn->gmn_mech_oid);
75c0b9f4feSDoug Rabson 	}
76c0b9f4feSDoug Rabson 
77c0b9f4feSDoug Rabson 	return (GSS_S_COMPLETE);
78c0b9f4feSDoug Rabson }
79