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