Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / lib / libc / rpc / svc_auth.c
CommitLineData
984263bc
MD
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.
ce0e08e2 8 *
984263bc
MD
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.
ce0e08e2 12 *
984263bc
MD
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.
ce0e08e2 16 *
984263bc
MD
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.
ce0e08e2 20 *
984263bc
MD
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.
ce0e08e2 24 *
984263bc
MD
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
1de703da 28 *
ce0e08e2
PA
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 $
c9b6181e 32 * $DragonFly: src/lib/libc/rpc/svc_auth.c,v 1.4 2005/11/13 12:27:04 swildner Exp $
984263bc
MD
33 */
34/*
ce0e08e2 35 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
984263bc
MD
36 */
37
984263bc
MD
38/*
39 * svc_auth.c, Server-side rpc authenticator interface.
984263bc
MD
40 */
41
ce0e08e2
PA
42#include "namespace.h"
43#include "reentrant.h"
984263bc 44#include <sys/types.h>
ce0e08e2
PA
45#include <rpc/rpc.h>
46#include <stdlib.h>
47#include "un-namespace.h"
48#include "mt_misc.h"
984263bc
MD
49
50/*
51 * svcauthsw is the bdevsw of server side authentication.
52 *
53 * Server side authenticators are called from authenticate by
54 * using the client auth struct flavor field to index into svcauthsw.
55 * The server auth flavors must implement a routine that looks
56 * like:
57 *
58 * enum auth_stat
59 * flavorx_auth(rqst, msg)
660c873b
DR
60 * struct svc_req *rqst;
61 * struct rpc_msg *msg;
984263bc
MD
62 *
63 */
64
984263bc
MD
65/* declarations to allow servers to specify new authentication flavors */
66struct authsvc {
67 int flavor;
ce0e08e2 68 enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
984263bc
MD
69 struct authsvc *next;
70};
71static struct authsvc *Auths = NULL;
72
73/*
74 * The call rpc message, msg has been obtained from the wire. The msg contains
75 * the raw form of credentials and verifiers. authenticate returns AUTH_OK
76 * if the msg is successfully authenticated. If AUTH_OK then the routine also
77 * does the following things:
78 * set rqst->rq_xprt->verf to the appropriate response verifier;
79 * sets rqst->rq_client_cred to the "cooked" form of the credentials.
80 *
81 * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
82 * its length is set appropriately.
83 *
84 * The caller still owns and is responsible for msg->u.cmb.cred and
85 * msg->u.cmb.verf. The authentication system retains ownership of
86 * rqst->rq_client_cred, the cooked credentials.
87 *
88 * There is an assumption that any flavour less than AUTH_NULL is
89 * invalid.
90 */
91enum auth_stat
c9b6181e 92_authenticate(struct svc_req *rqst, struct rpc_msg *msg)
984263bc 93{
660c873b
DR
94 int cred_flavor;
95 struct authsvc *asp;
ce0e08e2
PA
96 enum auth_stat dummy;
97
98/* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
984263bc
MD
99
100 rqst->rq_cred = msg->rm_call.cb_cred;
101 rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
102 rqst->rq_xprt->xp_verf.oa_length = 0;
103 cred_flavor = rqst->rq_cred.oa_flavor;
104 switch (cred_flavor) {
105 case AUTH_NULL:
ce0e08e2
PA
106 dummy = _svcauth_null(rqst, msg);
107 return (dummy);
108 case AUTH_SYS:
109 dummy = _svcauth_unix(rqst, msg);
110 return (dummy);
984263bc 111 case AUTH_SHORT:
ce0e08e2
PA
112 dummy = _svcauth_short(rqst, msg);
113 return (dummy);
984263bc
MD
114#ifdef DES_BUILTIN
115 case AUTH_DES:
ce0e08e2
PA
116 dummy = _svcauth_des(rqst, msg);
117 return (dummy);
984263bc 118#endif
ce0e08e2
PA
119 default:
120 break;
984263bc
MD
121 }
122
123 /* flavor doesn't match any of the builtin types, so try new ones */
ce0e08e2 124 mutex_lock(&authsvc_lock);
984263bc
MD
125 for (asp = Auths; asp; asp = asp->next) {
126 if (asp->flavor == cred_flavor) {
127 enum auth_stat as;
128
129 as = (*asp->handler)(rqst, msg);
ce0e08e2 130 mutex_unlock(&authsvc_lock);
984263bc
MD
131 return (as);
132 }
133 }
ce0e08e2 134 mutex_unlock(&authsvc_lock);
984263bc
MD
135
136 return (AUTH_REJECTEDCRED);
137}
138
139/*ARGSUSED*/
140enum auth_stat
c9b6181e 141_svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
984263bc
MD
142{
143 return (AUTH_OK);
144}
145
146/*
147 * Allow the rpc service to register new authentication types that it is
148 * prepared to handle. When an authentication flavor is registered,
149 * the flavor is checked against already registered values. If not
150 * registered, then a new Auths entry is added on the list.
151 *
152 * There is no provision to delete a registration once registered.
153 *
154 * This routine returns:
155 * 0 if registration successful
156 * 1 if flavor already registered
157 * -1 if can't register (errno set)
158 */
159
160int
ce0e08e2
PA
161svc_auth_reg(int cred_flavor,
162 enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *))
984263bc 163{
660c873b 164 struct authsvc *asp;
984263bc
MD
165
166 switch (cred_flavor) {
167 case AUTH_NULL:
ce0e08e2 168 case AUTH_SYS:
984263bc
MD
169 case AUTH_SHORT:
170#ifdef DES_BUILTIN
171 case AUTH_DES:
172#endif
173 /* already registered */
174 return (1);
175
176 default:
ce0e08e2 177 mutex_lock(&authsvc_lock);
984263bc
MD
178 for (asp = Auths; asp; asp = asp->next) {
179 if (asp->flavor == cred_flavor) {
180 /* already registered */
ce0e08e2 181 mutex_unlock(&authsvc_lock);
984263bc
MD
182 return (1);
183 }
184 }
185
186 /* this is a new one, so go ahead and register it */
ce0e08e2 187 asp = mem_alloc(sizeof (*asp));
984263bc 188 if (asp == NULL) {
ce0e08e2 189 mutex_unlock(&authsvc_lock);
984263bc
MD
190 return (-1);
191 }
192 asp->flavor = cred_flavor;
193 asp->handler = handler;
194 asp->next = Auths;
195 Auths = asp;
ce0e08e2 196 mutex_unlock(&authsvc_lock);
984263bc
MD
197 break;
198 }
199 return (0);
200}