xref: /dragonfly/lib/libc/rpc/svc_auth.c (revision 0dace59e)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)svc_auth.c	1.16	94/04/24 SMI; 1.26 89/02/07 Copyr 1984 Sun Micro
30  * $NetBSD: svc_auth.c,v 1.12 2000/07/06 03:10:35 christos Exp $
31  * $FreeBSD: src/lib/libc/rpc/svc_auth.c,v 1.13 2006/02/27 22:10:59 deischen Exp $
32  */
33 /*
34  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35  */
36 
37 /*
38  * svc_auth.c, Server-side rpc authenticator interface.
39  */
40 
41 #include "namespace.h"
42 #include "reentrant.h"
43 #include <sys/types.h>
44 #include <rpc/rpc.h>
45 #include <stdlib.h>
46 #include "un-namespace.h"
47 #include "mt_misc.h"
48 
49 /*
50  * svcauthsw is the bdevsw of server side authentication.
51  *
52  * Server side authenticators are called from authenticate by
53  * using the client auth struct flavor field to index into svcauthsw.
54  * The server auth flavors must implement a routine that looks
55  * like:
56  *
57  *	enum auth_stat
58  *	flavorx_auth(rqst, msg)
59  *		struct svc_req *rqst;
60  *		struct rpc_msg *msg;
61  *
62  */
63 
64 /* declarations to allow servers to specify new authentication flavors */
65 struct authsvc {
66 	int	flavor;
67 	enum	auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
68 	struct	authsvc	  *next;
69 };
70 static struct authsvc *Auths = NULL;
71 
72 /*
73  * The call rpc message, msg has been obtained from the wire.  The msg contains
74  * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
75  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
76  * does the following things:
77  * set rqst->rq_xprt->verf to the appropriate response verifier;
78  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
79  *
80  * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
81  * its length is set appropriately.
82  *
83  * The caller still owns and is responsible for msg->u.cmb.cred and
84  * msg->u.cmb.verf.  The authentication system retains ownership of
85  * rqst->rq_client_cred, the cooked credentials.
86  *
87  * There is an assumption that any flavour less than AUTH_NULL is
88  * invalid.
89  */
90 enum auth_stat
91 _authenticate(struct svc_req *rqst, struct rpc_msg *msg)
92 {
93 	int cred_flavor;
94 	struct authsvc *asp;
95 	enum auth_stat dummy;
96 
97 /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
98 
99 	rqst->rq_cred = msg->rm_call.cb_cred;
100 	rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
101 	rqst->rq_xprt->xp_verf.oa_length = 0;
102 	cred_flavor = rqst->rq_cred.oa_flavor;
103 	switch (cred_flavor) {
104 	case AUTH_NULL:
105 		dummy = _svcauth_null(rqst, msg);
106 		return (dummy);
107 	case AUTH_SYS:
108 		dummy = _svcauth_unix(rqst, msg);
109 		return (dummy);
110 	case AUTH_SHORT:
111 		dummy = _svcauth_short(rqst, msg);
112 		return (dummy);
113 #ifdef DES_BUILTIN
114 	case AUTH_DES:
115 		dummy = _svcauth_des(rqst, msg);
116 		return (dummy);
117 #endif
118 	default:
119 		break;
120 	}
121 
122 	/* flavor doesn't match any of the builtin types, so try new ones */
123 	mutex_lock(&authsvc_lock);
124 	for (asp = Auths; asp; asp = asp->next) {
125 		if (asp->flavor == cred_flavor) {
126 			enum auth_stat as;
127 
128 			as = (*asp->handler)(rqst, msg);
129 			mutex_unlock(&authsvc_lock);
130 			return (as);
131 		}
132 	}
133 	mutex_unlock(&authsvc_lock);
134 
135 	return (AUTH_REJECTEDCRED);
136 }
137 
138 /*ARGSUSED*/
139 enum auth_stat
140 _svcauth_null(struct svc_req *rqst __unused, struct rpc_msg *msg __unused)
141 {
142 	return (AUTH_OK);
143 }
144 
145 /*
146  *  Allow the rpc service to register new authentication types that it is
147  *  prepared to handle.  When an authentication flavor is registered,
148  *  the flavor is checked against already registered values.  If not
149  *  registered, then a new Auths entry is added on the list.
150  *
151  *  There is no provision to delete a registration once registered.
152  *
153  *  This routine returns:
154  *	 0 if registration successful
155  *	 1 if flavor already registered
156  *	-1 if can't register (errno set)
157  */
158 
159 int
160 svc_auth_reg(int cred_flavor,
161 	     enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *))
162 {
163 	struct authsvc *asp;
164 
165 	switch (cred_flavor) {
166 	    case AUTH_NULL:
167 	    case AUTH_SYS:
168 	    case AUTH_SHORT:
169 #ifdef DES_BUILTIN
170 	    case AUTH_DES:
171 #endif
172 		/* already registered */
173 		return (1);
174 
175 	    default:
176 		mutex_lock(&authsvc_lock);
177 		for (asp = Auths; asp; asp = asp->next) {
178 			if (asp->flavor == cred_flavor) {
179 				/* already registered */
180 				mutex_unlock(&authsvc_lock);
181 				return (1);
182 			}
183 		}
184 
185 		/* this is a new one, so go ahead and register it */
186 		asp = mem_alloc(sizeof (*asp));
187 		if (asp == NULL) {
188 			mutex_unlock(&authsvc_lock);
189 			return (-1);
190 		}
191 		asp->flavor = cred_flavor;
192 		asp->handler = handler;
193 		asp->next = Auths;
194 		Auths = asp;
195 		mutex_unlock(&authsvc_lock);
196 		break;
197 	}
198 	return (0);
199 }
200