1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 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 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kobj.h>
32 #include <sys/lock.h>
33 #include <sys/malloc.h>
34 #include <sys/mutex.h>
35 
36 #include <rpc/rpc.h>
37 #include <rpc/rpcsec_gss.h>
38 
39 #include "rpcsec_gss_int.h"
40 
41 bool_t
42 rpc_gss_mech_to_oid(const char *mech, gss_OID *oid_ret)
43 {
44 	gss_OID oid = kgss_find_mech_by_name(mech);
45 
46 	if (oid) {
47 		*oid_ret = oid;
48 		return (TRUE);
49 	}
50 	_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOENT);
51 	return (FALSE);
52 }
53 
54 bool_t
55 rpc_gss_oid_to_mech(gss_OID oid, const char **mech_ret)
56 {
57 	const char *name = kgss_find_mech_by_oid(oid);
58 
59 	if (name) {
60 		*mech_ret = name;
61 		return (TRUE);
62 	}
63 	_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOENT);
64 	return (FALSE);
65 }
66 
67 bool_t
68 rpc_gss_qop_to_num(const char *qop, const char *mech, u_int *num_ret)
69 {
70 
71 	if (!strcmp(qop, "default")) {
72 		*num_ret = GSS_C_QOP_DEFAULT;
73 		return (TRUE);
74 	}
75 	_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOENT);
76 	return (FALSE);
77 }
78 
79 const char *
80 _rpc_gss_num_to_qop(const char *mech, u_int num)
81 {
82 
83 	if (num == GSS_C_QOP_DEFAULT)
84 		return "default";
85 
86 	return (NULL);
87 }
88 
89 const char **
90 rpc_gss_get_mechanisms(void)
91 {
92 	static const char **mech_names = NULL;
93 	struct kgss_mech *km;
94 	int count;
95 
96 	if (mech_names)
97 		return (mech_names);
98 
99 	count = 0;
100 	LIST_FOREACH(km, &kgss_mechs, km_link) {
101 		count++;
102 	}
103 	count++;
104 
105 	mech_names = malloc(count * sizeof(const char *), M_RPC, M_WAITOK);
106 	count = 0;
107 	LIST_FOREACH(km, &kgss_mechs, km_link) {
108 		mech_names[count++] = km->km_mech_name;
109 	}
110 	mech_names[count++] = NULL;
111 
112 	return (mech_names);
113 }
114 
115 #if 0
116 const char **
117 rpc_gss_get_mech_info(const char *mech, rpc_gss_service_t *service)
118 {
119 	struct mech_info *info;
120 
121 	_rpc_gss_load_mech();
122 	_rpc_gss_load_qop();
123 	SLIST_FOREACH(info, &mechs, link) {
124 		if (!strcmp(mech, info->name)) {
125 			/*
126 			 * I'm not sure what to do with service
127 			 * here. The Solaris manpages are not clear on
128 			 * the subject and the OpenSolaris code just
129 			 * sets it to rpc_gss_svc_privacy
130 			 * unconditionally with a comment noting that
131 			 * it is bogus.
132 			 */
133 			*service = rpc_gss_svc_privacy;
134 			return info->qops;
135 		}
136 	}
137 
138 	_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOENT);
139 	return (NULL);
140 }
141 #endif
142 
143 bool_t
144 rpc_gss_get_versions(u_int *vers_hi, u_int *vers_lo)
145 {
146 
147 	*vers_hi = 1;
148 	*vers_lo = 1;
149 	return (TRUE);
150 }
151 
152 bool_t
153 rpc_gss_is_installed(const char *mech)
154 {
155 	gss_OID oid = kgss_find_mech_by_name(mech);
156 
157 	if (oid)
158 		return (TRUE);
159 	else
160 		return (FALSE);
161 }
162 
163