Imported Upstream version 0.2.5
[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 _gss_authenticate(rqst, msg, no_dispatch)
86         struct svc_req *rqst;
87         struct rpc_msg *msg;
88         bool_t *no_dispatch;
89 {
90         int cred_flavor;
91         struct authsvc *asp;
92         enum auth_stat dummy;
93         extern mutex_t authsvc_lock;
94
95 /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
96
97         rqst->rq_cred = msg->rm_call.cb_cred;
98         rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
99         rqst->rq_xprt->xp_verf.oa_length = 0;
100         cred_flavor = rqst->rq_cred.oa_flavor;
101         *no_dispatch = FALSE;
102         switch (cred_flavor) {
103         case AUTH_NONE:
104                 dummy = _svcauth_none(rqst, msg);
105                 return (dummy);
106         case AUTH_SYS:
107                 dummy = _svcauth_unix(rqst, msg);
108                 return (dummy);
109         case AUTH_SHORT:
110                 dummy = _svcauth_short(rqst, msg);
111                 return (dummy);
112 #ifdef DES_BUILTIN
113         case AUTH_DES:
114                 dummy = _svcauth_des(rqst, msg);
115                 return (dummy);
116 #endif
117 #ifdef HAVE_RPCSEC_GSS
118         case RPCSEC_GSS:
119                 dummy = _svcauth_gss(rqst, msg, no_dispatch);
120                 return (dummy);
121 #endif
122         default:
123                 break;
124         }
125
126         /* flavor doesn't match any of the builtin types, so try new ones */
127         mutex_lock(&authsvc_lock);
128         for (asp = Auths; asp; asp = asp->next) {
129                 if (asp->flavor == cred_flavor) {
130                         enum auth_stat as;
131
132                         as = (*asp->handler)(rqst, msg);
133                         mutex_unlock(&authsvc_lock);
134                         return (as);
135                 }
136         }
137         mutex_unlock(&authsvc_lock);
138
139         return (AUTH_REJECTEDCRED);
140 }
141
142 enum auth_stat
143 _authenticate(struct svc_req *rqst, struct rpc_msg *msg)
144 {
145         bool_t no_dispatch;
146         return _gss_authenticate(rqst, msg, &no_dispatch);
147 }
148
149 /*
150  *  Allow the rpc service to register new authentication types that it is
151  *  prepared to handle.  When an authentication flavor is registered,
152  *  the flavor is checked against already registered values.  If not
153  *  registered, then a new Auths entry is added on the list.
154  *
155  *  There is no provision to delete a registration once registered.
156  *
157  *  This routine returns:
158  *       0 if registration successful
159  *       1 if flavor already registered
160  *      -1 if can't register (errno set)
161  */
162
163 int
164 svc_auth_reg(cred_flavor, handler)
165         int cred_flavor;
166         enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
167 {
168         struct authsvc *asp;
169         extern mutex_t authsvc_lock;
170
171         switch (cred_flavor) {
172             case AUTH_NULL:
173             case AUTH_SYS:
174             case AUTH_SHORT:
175 #ifdef DES_BUILTIN
176             case AUTH_DES:
177 #endif
178 #ifdef HAVE_RPCSEC_GSS
179             case RPCSEC_GSS:
180 #endif
181                 /* already registered */
182                 return (1);
183
184             default:
185                 mutex_lock(&authsvc_lock);
186                 for (asp = Auths; asp; asp = asp->next) {
187                         if (asp->flavor == cred_flavor) {
188                                 /* already registered */
189                                 mutex_unlock(&authsvc_lock);
190                                 return (1);
191                         }
192                 }
193
194                 /* this is a new one, so go ahead and register it */
195                 asp = mem_alloc(sizeof (*asp));
196                 if (asp == NULL) {
197                         mutex_unlock(&authsvc_lock);
198                         return (-1);
199                 }
200                 asp->flavor = cred_flavor;
201                 asp->handler = handler;
202                 asp->next = Auths;
203                 Auths = asp;
204                 mutex_unlock(&authsvc_lock);
205                 break;
206         }
207         return (0);
208 }