xref: /freebsd/lib/libc/rpc/svc_auth.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 /*
41  * svc_auth.c, Server-side rpc authenticator interface.
42  *
43  */
44 
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include <sys/types.h>
48 #include <rpc/rpc.h>
49 #include <stdlib.h>
50 #include "un-namespace.h"
51 #include "mt_misc.h"
52 
53 /*
54  * svcauthsw is the bdevsw of server side authentication.
55  *
56  * Server side authenticators are called from authenticate by
57  * using the client auth struct flavor field to index into svcauthsw.
58  * The server auth flavors must implement a routine that looks
59  * like:
60  *
61  *	enum auth_stat
62  *	flavorx_auth(rqst, msg)
63  *		struct svc_req *rqst;
64  *		struct rpc_msg *msg;
65  *
66  */
67 
68 /* declarations to allow servers to specify new authentication flavors */
69 struct authsvc {
70 	int	flavor;
71 	enum	auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
72 	struct	authsvc	  *next;
73 };
74 static struct authsvc *Auths = NULL;
75 
76 struct svc_auth_ops svc_auth_null_ops;
77 
78 /*
79  * The call rpc message, msg has been obtained from the wire.  The msg contains
80  * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
81  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
82  * does the following things:
83  * set rqst->rq_xprt->verf to the appropriate response verifier;
84  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
85  *
86  * NB: rqst->rq_cxprt->verf must be pre-allocated;
87  * its length is set appropriately.
88  *
89  * The caller still owns and is responsible for msg->u.cmb.cred and
90  * msg->u.cmb.verf.  The authentication system retains ownership of
91  * rqst->rq_client_cred, the cooked credentials.
92  *
93  * There is an assumption that any flavour less than AUTH_NULL is
94  * invalid.
95  */
96 enum auth_stat
97 _authenticate(struct svc_req *rqst, struct rpc_msg *msg)
98 {
99 	int cred_flavor;
100 	struct authsvc *asp;
101 	enum auth_stat dummy;
102 
103 /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
104 
105 	rqst->rq_cred = msg->rm_call.cb_cred;
106 	SVC_AUTH(rqst->rq_xprt).svc_ah_ops = &svc_auth_null_ops;
107 	SVC_AUTH(rqst->rq_xprt).svc_ah_private = NULL;
108 	rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
109 	rqst->rq_xprt->xp_verf.oa_length = 0;
110 	cred_flavor = rqst->rq_cred.oa_flavor;
111 	switch (cred_flavor) {
112 	case AUTH_NULL:
113 		dummy = _svcauth_null(rqst, msg);
114 		return (dummy);
115 	case AUTH_SYS:
116 		dummy = _svcauth_unix(rqst, msg);
117 		return (dummy);
118 	case AUTH_SHORT:
119 		dummy = _svcauth_short(rqst, msg);
120 		return (dummy);
121 #ifdef DES_BUILTIN
122 	case AUTH_DES:
123 		dummy = _svcauth_des(rqst, msg);
124 		return (dummy);
125 #endif
126 	default:
127 		break;
128 	}
129 
130 	/* flavor doesn't match any of the builtin types, so try new ones */
131 	mutex_lock(&authsvc_lock);
132 	for (asp = Auths; asp; asp = asp->next) {
133 		if (asp->flavor == cred_flavor) {
134 			enum auth_stat as;
135 
136 			as = (*asp->handler)(rqst, msg);
137 			mutex_unlock(&authsvc_lock);
138 			return (as);
139 		}
140 	}
141 	mutex_unlock(&authsvc_lock);
142 
143 	return (AUTH_REJECTEDCRED);
144 }
145 
146 /*
147  * A set of null auth methods used by any authentication protocols
148  * that don't need to inspect or modify the message body.
149  */
150 static bool_t
151 svcauth_null_wrap(SVCAUTH *auth, XDR *xdrs, xdrproc_t xdr_func, caddr_t xdr_ptr)
152 {
153 
154 	return (xdr_func(xdrs, xdr_ptr));
155 }
156 
157 struct svc_auth_ops svc_auth_null_ops = {
158 	svcauth_null_wrap,
159 	svcauth_null_wrap,
160 };
161 
162 /*ARGSUSED*/
163 enum auth_stat
164 _svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
165 {
166 	return (AUTH_OK);
167 }
168 
169 /*
170  *  Allow the rpc service to register new authentication types that it is
171  *  prepared to handle.  When an authentication flavor is registered,
172  *  the flavor is checked against already registered values.  If not
173  *  registered, then a new Auths entry is added on the list.
174  *
175  *  There is no provision to delete a registration once registered.
176  *
177  *  This routine returns:
178  *	 0 if registration successful
179  *	 1 if flavor already registered
180  *	-1 if can't register (errno set)
181  */
182 
183 int
184 svc_auth_reg(int cred_flavor,
185     enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *))
186 {
187 	struct authsvc *asp;
188 
189 	switch (cred_flavor) {
190 	    case AUTH_NULL:
191 	    case AUTH_SYS:
192 	    case AUTH_SHORT:
193 #ifdef DES_BUILTIN
194 	    case AUTH_DES:
195 #endif
196 		/* already registered */
197 		return (1);
198 
199 	    default:
200 		mutex_lock(&authsvc_lock);
201 		for (asp = Auths; asp; asp = asp->next) {
202 			if (asp->flavor == cred_flavor) {
203 				/* already registered */
204 				mutex_unlock(&authsvc_lock);
205 				return (1);
206 			}
207 		}
208 
209 		/* this is a new one, so go ahead and register it */
210 		asp = mem_alloc(sizeof (*asp));
211 		if (asp == NULL) {
212 			mutex_unlock(&authsvc_lock);
213 			return (-1);
214 		}
215 		asp->flavor = cred_flavor;
216 		asp->handler = handler;
217 		asp->next = Auths;
218 		Auths = asp;
219 		mutex_unlock(&authsvc_lock);
220 		break;
221 	}
222 	return (0);
223 }
224