c6b3a0bd75f55497ba4d68ff17dc114207688822
[platform/upstream/libtirpc.git] / src / svc_auth.c
1
2 /*
3  * Copyright (c) 2009, Sun Microsystems, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  * - Neither the name of Sun Microsystems, Inc. nor the names of its
14  *   contributors may be used to endorse or promote products derived
15  *   from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 /*
30  * Copyright (c) 1986-1991 by Sun Microsystems Inc. 
31  */
32
33 /*
34  * svc_auth.c, Server-side rpc authenticator interface.
35  *
36  */
37 #include <pthread.h>
38 #include <reentrant.h>
39 #include <sys/types.h>
40 #include <rpc/rpc.h>
41 #include <stdlib.h>
42
43 /*
44  * svcauthsw is the bdevsw of server side authentication.
45  *
46  * Server side authenticators are called from authenticate by
47  * using the client auth struct flavor field to index into svcauthsw.
48  * The server auth flavors must implement a routine that looks
49  * like:
50  *
51  *      enum auth_stat
52  *      flavorx_auth(rqst, msg)
53  *              struct svc_req *rqst;
54  *              struct rpc_msg *msg;
55  *
56  */
57
58 /* declarations to allow servers to specify new authentication flavors */
59 struct authsvc {
60         int     flavor;
61         enum    auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
62         struct  authsvc   *next;
63 };
64 static struct authsvc *Auths = NULL;
65
66 /*
67  * The call rpc message, msg has been obtained from the wire.  The msg contains
68  * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
69  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
70  * does the following things:
71  * set rqst->rq_xprt->verf to the appropriate response verifier;
72  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
73  *
74  * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
75  * its length is set appropriately.
76  *
77  * The caller still owns and is responsible for msg->u.cmb.cred and
78  * msg->u.cmb.verf.  The authentication system retains ownership of
79  * rqst->rq_client_cred, the cooked credentials.
80  *
81  * There is an assumption that any flavour less than AUTH_NULL is
82  * invalid.
83  */
84 enum auth_stat
85 _authenticate(rqst, msg)
86         struct svc_req *rqst;
87         struct rpc_msg *msg;
88 {
89         int cred_flavor;
90         struct authsvc *asp;
91         enum auth_stat dummy;
92         extern mutex_t authsvc_lock;
93
94 /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
95
96         rqst->rq_cred = msg->rm_call.cb_cred;
97         rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
98         rqst->rq_xprt->xp_verf.oa_length = 0;
99         cred_flavor = rqst->rq_cred.oa_flavor;
100         switch (cred_flavor) {
101         case AUTH_NULL:
102                 dummy = _svcauth_null(rqst, msg);
103                 return (dummy);
104         case AUTH_SYS:
105                 dummy = _svcauth_unix(rqst, msg);
106                 return (dummy);
107         case AUTH_SHORT:
108                 dummy = _svcauth_short(rqst, msg);
109                 return (dummy);
110 #ifdef DES_BUILTIN
111         case AUTH_DES:
112                 dummy = _svcauth_des(rqst, msg);
113                 return (dummy);
114 #endif
115         default:
116                 break;
117         }
118
119         /* flavor doesn't match any of the builtin types, so try new ones */
120         mutex_lock(&authsvc_lock);
121         for (asp = Auths; asp; asp = asp->next) {
122                 if (asp->flavor == cred_flavor) {
123                         enum auth_stat as;
124
125                         as = (*asp->handler)(rqst, msg);
126                         mutex_unlock(&authsvc_lock);
127                         return (as);
128                 }
129         }
130         mutex_unlock(&authsvc_lock);
131
132         return (AUTH_REJECTEDCRED);
133 }
134
135 /*ARGSUSED*/
136 enum auth_stat
137 _svcauth_null(rqst, msg)
138         struct svc_req *rqst;
139         struct rpc_msg *msg;
140 {
141         return (AUTH_OK);
142 }
143
144 /*
145  *  Allow the rpc service to register new authentication types that it is
146  *  prepared to handle.  When an authentication flavor is registered,
147  *  the flavor is checked against already registered values.  If not
148  *  registered, then a new Auths entry is added on the list.
149  *
150  *  There is no provision to delete a registration once registered.
151  *
152  *  This routine returns:
153  *       0 if registration successful
154  *       1 if flavor already registered
155  *      -1 if can't register (errno set)
156  */
157
158 int
159 svc_auth_reg(cred_flavor, handler)
160         int cred_flavor;
161         enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
162 {
163         struct authsvc *asp;
164         extern mutex_t authsvc_lock;
165
166         switch (cred_flavor) {
167             case AUTH_NULL:
168             case AUTH_SYS:
169             case AUTH_SHORT:
170 #ifdef DES_BUILTIN
171             case AUTH_DES:
172 #endif
173                 /* already registered */
174                 return (1);
175
176             default:
177                 mutex_lock(&authsvc_lock);
178                 for (asp = Auths; asp; asp = asp->next) {
179                         if (asp->flavor == cred_flavor) {
180                                 /* already registered */
181                                 mutex_unlock(&authsvc_lock);
182                                 return (1);
183                         }
184                 }
185
186                 /* this is a new one, so go ahead and register it */
187                 asp = mem_alloc(sizeof (*asp));
188                 if (asp == NULL) {
189                         mutex_unlock(&authsvc_lock);
190                         return (-1);
191                 }
192                 asp->flavor = cred_flavor;
193                 asp->handler = handler;
194                 asp->next = Auths;
195                 Auths = asp;
196                 mutex_unlock(&authsvc_lock);
197                 break;
198         }
199         return (0);
200 }