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