xref: /freebsd/sys/rpc/svc_auth.c (revision 7750ad47a9a7dbc83f87158464170c8640723293)
1 /*	$NetBSD: svc_auth.c,v 1.12 2000/07/06 03:10:35 christos Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 /*
32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33  */
34 
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #ident	"@(#)svc_auth.c	1.16	94/04/24 SMI"
37 static char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro";
38 #endif
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 /*
43  * svc_auth.c, Server-side rpc authenticator interface.
44  *
45  */
46 
47 #include <sys/param.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/systm.h>
51 #include <sys/jail.h>
52 #include <sys/ucred.h>
53 
54 #include <rpc/rpc.h>
55 
56 static enum auth_stat (*_svcauth_rpcsec_gss)(struct svc_req *,
57     struct rpc_msg *) = NULL;
58 static int (*_svcauth_rpcsec_gss_getcred)(struct svc_req *,
59     struct ucred **, int *);
60 
61 static struct svc_auth_ops svc_auth_null_ops;
62 
63 /*
64  * The call rpc message, msg has been obtained from the wire.  The msg contains
65  * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
66  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
67  * does the following things:
68  * set rqst->rq_xprt->verf to the appropriate response verifier;
69  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
70  *
71  * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
72  * its length is set appropriately.
73  *
74  * The caller still owns and is responsible for msg->u.cmb.cred and
75  * msg->u.cmb.verf.  The authentication system retains ownership of
76  * rqst->rq_client_cred, the cooked credentials.
77  *
78  * There is an assumption that any flavour less than AUTH_NULL is
79  * invalid.
80  */
81 enum auth_stat
82 _authenticate(struct svc_req *rqst, struct rpc_msg *msg)
83 {
84 	int cred_flavor;
85 	enum auth_stat dummy;
86 
87 	rqst->rq_cred = msg->rm_call.cb_cred;
88 	rqst->rq_auth.svc_ah_ops = &svc_auth_null_ops;
89 	rqst->rq_auth.svc_ah_private = NULL;
90 	cred_flavor = rqst->rq_cred.oa_flavor;
91 	switch (cred_flavor) {
92 	case AUTH_NULL:
93 		dummy = _svcauth_null(rqst, msg);
94 		return (dummy);
95 	case AUTH_SYS:
96 		dummy = _svcauth_unix(rqst, msg);
97 		return (dummy);
98 	case AUTH_SHORT:
99 		dummy = _svcauth_short(rqst, msg);
100 		return (dummy);
101 	case RPCSEC_GSS:
102 		if (!_svcauth_rpcsec_gss)
103 			return (AUTH_REJECTEDCRED);
104 		dummy = _svcauth_rpcsec_gss(rqst, msg);
105 		return (dummy);
106 	default:
107 		break;
108 	}
109 
110 	return (AUTH_REJECTEDCRED);
111 }
112 
113 /*
114  * A set of null auth methods used by any authentication protocols
115  * that don't need to inspect or modify the message body.
116  */
117 static bool_t
118 svcauth_null_wrap(SVCAUTH *auth, struct mbuf **mp)
119 {
120 
121 	return (TRUE);
122 }
123 
124 static bool_t
125 svcauth_null_unwrap(SVCAUTH *auth, struct mbuf **mp)
126 {
127 
128 	return (TRUE);
129 }
130 
131 static void
132 svcauth_null_release(SVCAUTH *auth)
133 {
134 
135 }
136 
137 static struct svc_auth_ops svc_auth_null_ops = {
138 	svcauth_null_wrap,
139 	svcauth_null_unwrap,
140 	svcauth_null_release,
141 };
142 
143 /*ARGSUSED*/
144 enum auth_stat
145 _svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
146 {
147 
148 	rqst->rq_verf = _null_auth;
149 	return (AUTH_OK);
150 }
151 
152 int
153 svc_auth_reg(int flavor,
154     enum auth_stat (*svcauth)(struct svc_req *, struct rpc_msg *),
155     int (*getcred)(struct svc_req *, struct ucred **, int *))
156 {
157 
158 	if (flavor == RPCSEC_GSS) {
159 		_svcauth_rpcsec_gss = svcauth;
160 		_svcauth_rpcsec_gss_getcred = getcred;
161 	}
162 	return (TRUE);
163 }
164 
165 int
166 svc_getcred(struct svc_req *rqst, struct ucred **crp, int *flavorp)
167 {
168 	struct ucred *cr = NULL;
169 	int flavor;
170 	struct xucred *xcr;
171 
172 	flavor = rqst->rq_cred.oa_flavor;
173 	if (flavorp)
174 		*flavorp = flavor;
175 
176 	switch (flavor) {
177 	case AUTH_UNIX:
178 		xcr = (struct xucred *) rqst->rq_clntcred;
179 		cr = crget();
180 		cr->cr_uid = cr->cr_ruid = cr->cr_svuid = xcr->cr_uid;
181 		crsetgroups(cr, xcr->cr_ngroups, xcr->cr_groups);
182 		cr->cr_rgid = cr->cr_svgid = cr->cr_groups[0];
183 		cr->cr_prison = &prison0;
184 		prison_hold(cr->cr_prison);
185 		*crp = cr;
186 		return (TRUE);
187 
188 	case RPCSEC_GSS:
189 		if (!_svcauth_rpcsec_gss_getcred)
190 			return (FALSE);
191 		return (_svcauth_rpcsec_gss_getcred(rqst, crp, flavorp));
192 
193 	default:
194 		return (FALSE);
195 	}
196 }
197 
198