source: rtems-libbsd/freebsd/lib/libc/rpc/svc_auth.c @ 9880635

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 9880635 was 60b1d40, checked in by Sebastian Huber <sebastian.huber@…>, on 06/09/16 at 08:23:57

RPC(3): Import from FreeBSD

  • Property mode set to 100644
File size: 6.5 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*      $NetBSD: svc_auth.c,v 1.12 2000/07/06 03:10:35 christos Exp $   */
4
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"
38static 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 */
72struct authsvc {
73        int     flavor;
74        enum    auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
75        struct  authsvc   *next;
76};
77static struct authsvc *Auths = NULL;
78
79struct 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 */
99enum auth_stat
100_authenticate(rqst, msg)
101        struct svc_req *rqst;
102        struct rpc_msg *msg;
103{
104        int cred_flavor;
105        struct authsvc *asp;
106        enum auth_stat dummy;
107
108/* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
109
110        rqst->rq_cred = msg->rm_call.cb_cred;
111        SVC_AUTH(rqst->rq_xprt).svc_ah_ops = &svc_auth_null_ops;
112        SVC_AUTH(rqst->rq_xprt).svc_ah_private = NULL;
113        rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
114        rqst->rq_xprt->xp_verf.oa_length = 0;
115        cred_flavor = rqst->rq_cred.oa_flavor;
116        switch (cred_flavor) {
117        case AUTH_NULL:
118                dummy = _svcauth_null(rqst, msg);
119                return (dummy);
120        case AUTH_SYS:
121                dummy = _svcauth_unix(rqst, msg);
122                return (dummy);
123        case AUTH_SHORT:
124                dummy = _svcauth_short(rqst, msg);
125                return (dummy);
126#ifdef DES_BUILTIN
127        case AUTH_DES:
128                dummy = _svcauth_des(rqst, msg);
129                return (dummy);
130#endif
131        default:
132                break;
133        }
134
135        /* flavor doesn't match any of the builtin types, so try new ones */
136        mutex_lock(&authsvc_lock);
137        for (asp = Auths; asp; asp = asp->next) {
138                if (asp->flavor == cred_flavor) {
139                        enum auth_stat as;
140
141                        as = (*asp->handler)(rqst, msg);
142                        mutex_unlock(&authsvc_lock);
143                        return (as);
144                }
145        }
146        mutex_unlock(&authsvc_lock);
147
148        return (AUTH_REJECTEDCRED);
149}
150
151/*
152 * A set of null auth methods used by any authentication protocols
153 * that don't need to inspect or modify the message body.
154 */
155static bool_t
156svcauth_null_wrap(auth, xdrs, xdr_func, xdr_ptr)
157        SVCAUTH *auth;
158        XDR *xdrs;
159        xdrproc_t xdr_func;
160        caddr_t xdr_ptr;
161{
162
163        return (xdr_func(xdrs, xdr_ptr));
164}
165
166struct svc_auth_ops svc_auth_null_ops = {
167        svcauth_null_wrap,
168        svcauth_null_wrap,
169};
170
171/*ARGSUSED*/
172enum auth_stat
173_svcauth_null(rqst, msg)
174        struct svc_req *rqst;
175        struct rpc_msg *msg;
176{
177        return (AUTH_OK);
178}
179
180/*
181 *  Allow the rpc service to register new authentication types that it is
182 *  prepared to handle.  When an authentication flavor is registered,
183 *  the flavor is checked against already registered values.  If not
184 *  registered, then a new Auths entry is added on the list.
185 *
186 *  There is no provision to delete a registration once registered.
187 *
188 *  This routine returns:
189 *       0 if registration successful
190 *       1 if flavor already registered
191 *      -1 if can't register (errno set)
192 */
193
194int
195svc_auth_reg(cred_flavor, handler)
196        int cred_flavor;
197        enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
198{
199        struct authsvc *asp;
200
201        switch (cred_flavor) {
202            case AUTH_NULL:
203            case AUTH_SYS:
204            case AUTH_SHORT:
205#ifdef DES_BUILTIN
206            case AUTH_DES:
207#endif
208                /* already registered */
209                return (1);
210
211            default:
212                mutex_lock(&authsvc_lock);
213                for (asp = Auths; asp; asp = asp->next) {
214                        if (asp->flavor == cred_flavor) {
215                                /* already registered */
216                                mutex_unlock(&authsvc_lock);
217                                return (1);
218                        }
219                }
220
221                /* this is a new one, so go ahead and register it */
222                asp = mem_alloc(sizeof (*asp));
223                if (asp == NULL) {
224                        mutex_unlock(&authsvc_lock);
225                        return (-1);
226                }
227                asp->flavor = cred_flavor;
228                asp->handler = handler;
229                asp->next = Auths;
230                Auths = asp;
231                mutex_unlock(&authsvc_lock);
232                break;
233        }
234        return (0);
235}
Note: See TracBrowser for help on using the repository browser.