1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* $NetBSD: svc_auth.c,v 1.12 2000/07/06 03:10:35 christos Exp $ */ |
---|
4 | |
---|
5 | /*- |
---|
6 | * SPDX-License-Identifier: BSD-3-Clause |
---|
7 | * |
---|
8 | * Copyright (c) 2009, Sun Microsystems, Inc. |
---|
9 | * All rights reserved. |
---|
10 | * |
---|
11 | * Redistribution and use in source and binary forms, with or without |
---|
12 | * modification, are permitted provided that the following conditions are met: |
---|
13 | * - Redistributions of source code must retain the above copyright notice, |
---|
14 | * this list of conditions and the following disclaimer. |
---|
15 | * - Redistributions in binary form must reproduce the above copyright notice, |
---|
16 | * this list of conditions and the following disclaimer in the documentation |
---|
17 | * and/or other materials provided with the distribution. |
---|
18 | * - Neither the name of Sun Microsystems, Inc. nor the names of its |
---|
19 | * contributors may be used to endorse or promote products derived |
---|
20 | * from this software without specific prior written permission. |
---|
21 | * |
---|
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
32 | * POSSIBILITY OF SUCH DAMAGE. |
---|
33 | */ |
---|
34 | /* |
---|
35 | * Copyright (c) 1986-1991 by Sun Microsystems Inc. |
---|
36 | */ |
---|
37 | |
---|
38 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
39 | #ident "@(#)svc_auth.c 1.16 94/04/24 SMI" |
---|
40 | static char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro"; |
---|
41 | #endif |
---|
42 | #include <sys/cdefs.h> |
---|
43 | __FBSDID("$FreeBSD$"); |
---|
44 | |
---|
45 | /* |
---|
46 | * svc_auth.c, Server-side rpc authenticator interface. |
---|
47 | * |
---|
48 | */ |
---|
49 | |
---|
50 | #include "namespace.h" |
---|
51 | #include "reentrant.h" |
---|
52 | #include <sys/types.h> |
---|
53 | #include <rpc/rpc.h> |
---|
54 | #include <stdlib.h> |
---|
55 | #include "un-namespace.h" |
---|
56 | #include "mt_misc.h" |
---|
57 | |
---|
58 | /* |
---|
59 | * svcauthsw is the bdevsw of server side authentication. |
---|
60 | * |
---|
61 | * Server side authenticators are called from authenticate by |
---|
62 | * using the client auth struct flavor field to index into svcauthsw. |
---|
63 | * The server auth flavors must implement a routine that looks |
---|
64 | * like: |
---|
65 | * |
---|
66 | * enum auth_stat |
---|
67 | * flavorx_auth(rqst, msg) |
---|
68 | * struct svc_req *rqst; |
---|
69 | * struct rpc_msg *msg; |
---|
70 | * |
---|
71 | */ |
---|
72 | |
---|
73 | /* declarations to allow servers to specify new authentication flavors */ |
---|
74 | struct authsvc { |
---|
75 | int flavor; |
---|
76 | enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *); |
---|
77 | struct authsvc *next; |
---|
78 | }; |
---|
79 | static struct authsvc *Auths = NULL; |
---|
80 | |
---|
81 | struct svc_auth_ops svc_auth_null_ops; |
---|
82 | |
---|
83 | /* |
---|
84 | * The call rpc message, msg has been obtained from the wire. The msg contains |
---|
85 | * the raw form of credentials and verifiers. authenticate returns AUTH_OK |
---|
86 | * if the msg is successfully authenticated. If AUTH_OK then the routine also |
---|
87 | * does the following things: |
---|
88 | * set rqst->rq_xprt->verf to the appropriate response verifier; |
---|
89 | * sets rqst->rq_client_cred to the "cooked" form of the credentials. |
---|
90 | * |
---|
91 | * NB: rqst->rq_cxprt->verf must be pre-alloctaed; |
---|
92 | * its length is set appropriately. |
---|
93 | * |
---|
94 | * The caller still owns and is responsible for msg->u.cmb.cred and |
---|
95 | * msg->u.cmb.verf. The authentication system retains ownership of |
---|
96 | * rqst->rq_client_cred, the cooked credentials. |
---|
97 | * |
---|
98 | * There is an assumption that any flavour less than AUTH_NULL is |
---|
99 | * invalid. |
---|
100 | */ |
---|
101 | enum auth_stat |
---|
102 | _authenticate(struct svc_req *rqst, struct rpc_msg *msg) |
---|
103 | { |
---|
104 | int cred_flavor; |
---|
105 | struct authsvc *asp; |
---|
106 | enum auth_stat dummy; |
---|
107 | |
---|
108 | /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */ |
---|
109 | |
---|
110 | rqst->rq_cred = msg->rm_call.cb_cred; |
---|
111 | SVC_AUTH(rqst->rq_xprt).svc_ah_ops = &svc_auth_null_ops; |
---|
112 | SVC_AUTH(rqst->rq_xprt).svc_ah_private = NULL; |
---|
113 | rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor; |
---|
114 | rqst->rq_xprt->xp_verf.oa_length = 0; |
---|
115 | cred_flavor = rqst->rq_cred.oa_flavor; |
---|
116 | switch (cred_flavor) { |
---|
117 | case AUTH_NULL: |
---|
118 | dummy = _svcauth_null(rqst, msg); |
---|
119 | return (dummy); |
---|
120 | case AUTH_SYS: |
---|
121 | dummy = _svcauth_unix(rqst, msg); |
---|
122 | return (dummy); |
---|
123 | case AUTH_SHORT: |
---|
124 | dummy = _svcauth_short(rqst, msg); |
---|
125 | return (dummy); |
---|
126 | #ifdef DES_BUILTIN |
---|
127 | case AUTH_DES: |
---|
128 | dummy = _svcauth_des(rqst, msg); |
---|
129 | return (dummy); |
---|
130 | #endif |
---|
131 | default: |
---|
132 | break; |
---|
133 | } |
---|
134 | |
---|
135 | /* flavor doesn't match any of the builtin types, so try new ones */ |
---|
136 | mutex_lock(&authsvc_lock); |
---|
137 | for (asp = Auths; asp; asp = asp->next) { |
---|
138 | if (asp->flavor == cred_flavor) { |
---|
139 | enum auth_stat as; |
---|
140 | |
---|
141 | as = (*asp->handler)(rqst, msg); |
---|
142 | mutex_unlock(&authsvc_lock); |
---|
143 | return (as); |
---|
144 | } |
---|
145 | } |
---|
146 | mutex_unlock(&authsvc_lock); |
---|
147 | |
---|
148 | return (AUTH_REJECTEDCRED); |
---|
149 | } |
---|
150 | |
---|
151 | /* |
---|
152 | * A set of null auth methods used by any authentication protocols |
---|
153 | * that don't need to inspect or modify the message body. |
---|
154 | */ |
---|
155 | static bool_t |
---|
156 | svcauth_null_wrap(SVCAUTH *auth, XDR *xdrs, xdrproc_t xdr_func, caddr_t xdr_ptr) |
---|
157 | { |
---|
158 | |
---|
159 | return (xdr_func(xdrs, xdr_ptr)); |
---|
160 | } |
---|
161 | |
---|
162 | struct svc_auth_ops svc_auth_null_ops = { |
---|
163 | svcauth_null_wrap, |
---|
164 | svcauth_null_wrap, |
---|
165 | }; |
---|
166 | |
---|
167 | /*ARGSUSED*/ |
---|
168 | enum auth_stat |
---|
169 | _svcauth_null(struct svc_req *rqst, struct rpc_msg *msg) |
---|
170 | { |
---|
171 | return (AUTH_OK); |
---|
172 | } |
---|
173 | |
---|
174 | /* |
---|
175 | * Allow the rpc service to register new authentication types that it is |
---|
176 | * prepared to handle. When an authentication flavor is registered, |
---|
177 | * the flavor is checked against already registered values. If not |
---|
178 | * registered, then a new Auths entry is added on the list. |
---|
179 | * |
---|
180 | * There is no provision to delete a registration once registered. |
---|
181 | * |
---|
182 | * This routine returns: |
---|
183 | * 0 if registration successful |
---|
184 | * 1 if flavor already registered |
---|
185 | * -1 if can't register (errno set) |
---|
186 | */ |
---|
187 | |
---|
188 | int |
---|
189 | svc_auth_reg(int cred_flavor, |
---|
190 | enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *)) |
---|
191 | { |
---|
192 | struct authsvc *asp; |
---|
193 | |
---|
194 | switch (cred_flavor) { |
---|
195 | case AUTH_NULL: |
---|
196 | case AUTH_SYS: |
---|
197 | case AUTH_SHORT: |
---|
198 | #ifdef DES_BUILTIN |
---|
199 | case AUTH_DES: |
---|
200 | #endif |
---|
201 | /* already registered */ |
---|
202 | return (1); |
---|
203 | |
---|
204 | default: |
---|
205 | mutex_lock(&authsvc_lock); |
---|
206 | for (asp = Auths; asp; asp = asp->next) { |
---|
207 | if (asp->flavor == cred_flavor) { |
---|
208 | /* already registered */ |
---|
209 | mutex_unlock(&authsvc_lock); |
---|
210 | return (1); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | /* this is a new one, so go ahead and register it */ |
---|
215 | asp = mem_alloc(sizeof (*asp)); |
---|
216 | if (asp == NULL) { |
---|
217 | mutex_unlock(&authsvc_lock); |
---|
218 | return (-1); |
---|
219 | } |
---|
220 | asp->flavor = cred_flavor; |
---|
221 | asp->handler = handler; |
---|
222 | asp->next = Auths; |
---|
223 | Auths = asp; |
---|
224 | mutex_unlock(&authsvc_lock); |
---|
225 | break; |
---|
226 | } |
---|
227 | return (0); |
---|
228 | } |
---|