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