Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/svc_auth.c

raw symbols SCIP
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 <rpc/auth_des.h>
42 #include <stdlib.h>
43
44 /*
45 * svcauthsw is the bdevsw of server side authentication.
46 *
47 * Server side authenticators are called from authenticate by
48 * using the client auth struct flavor field to index into svcauthsw.
49 * The server auth flavors must implement a routine that looks
50 * like:
51 *
52 * enum auth_stat
53 * flavorx_auth(rqst, msg)
54 * struct svc_req *rqst;
55 * struct rpc_msg *msg;
56 *
57 */
58
59 /* declarations to allow servers to specify new authentication flavors */
60 struct authsvc {
61 int flavor;
62 enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
63 struct authsvc *next;
64 };
65 static struct authsvc *Auths = NULL;
66
67 extern SVCAUTH svc_auth_none;
68
69 #ifdef AUTHDES_SUPPORT
70 extern enum auth_stat _svcauth_des(struct svc_req *rqst, struct rpc_msg *msg);
71 #endif
72 /*
73 * The call rpc message, msg has been obtained from the wire. The msg contains
74 * the raw form of credentials and verifiers. authenticate returns AUTH_OK
75 * if the msg is successfully authenticated. If AUTH_OK then the routine also
76 * does the following things:
77 * set rqst->rq_xprt->verf to the appropriate response verifier;
78 * sets rqst->rq_client_cred to the "cooked" form of the credentials.
79 *
80 * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
81 * its length is set appropriately.
82 *
83 * The caller still owns and is responsible for msg->u.cmb.cred and
84 * msg->u.cmb.verf. The authentication system retains ownership of
85 * rqst->rq_client_cred, the cooked credentials.
86 *
87 * There is an assumption that any flavour less than AUTH_NULL is
88 * invalid.
89 */
90 enum auth_stat
91 _gss_authenticate(rqst, msg, no_dispatch)
92 struct svc_req *rqst;
93 struct rpc_msg *msg;
94 bool_t *no_dispatch;
95 {
96 int cred_flavor;
97 struct authsvc *asp;
98 enum auth_stat dummy;
99 extern mutex_t authsvc_lock;
100
101 /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
102
103 rqst->rq_cred = msg->rm_call.cb_cred;
104 SVC_XP_AUTH(rqst->rq_xprt).svc_ah_ops = svc_auth_none.svc_ah_ops;
105 SVC_XP_AUTH(rqst->rq_xprt).svc_ah_private = NULL;
106 rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
107 rqst->rq_xprt->xp_verf.oa_length = 0;
108 cred_flavor = rqst->rq_cred.oa_flavor;
109 *no_dispatch = FALSE;
110 switch (cred_flavor) {
111 case AUTH_NONE:
112 dummy = _svcauth_none(rqst, msg);
113 return (dummy);
114 case AUTH_SYS:
115 dummy = _svcauth_unix(rqst, msg);
116 return (dummy);
117 case AUTH_SHORT:
118 dummy = _svcauth_short(rqst, msg);
119 return (dummy);
120 case AUTH_DES:
121 #ifdef AUTHDES_SUPPORT
122 dummy = _svcauth_des(rqst, msg);
123 return (dummy);
124 #else
125 return (AUTH_FAILED);
126 #endif
127 #ifdef HAVE_RPCSEC_GSS
128 case RPCSEC_GSS:
129 dummy = _svcauth_gss(rqst, msg, no_dispatch);
130 return (dummy);
131 #endif
132 default:
133 break;
134 }
135
136 /* flavor doesn't match any of the builtin types, so try new ones */
137 mutex_lock(&authsvc_lock);
138 for (asp = Auths; asp; asp = asp->next) {
139 if (asp->flavor == cred_flavor) {
140 enum auth_stat as;
141
142 as = (*asp->handler)(rqst, msg);
143 mutex_unlock(&authsvc_lock);
144 return (as);
145 }
146 }
147 mutex_unlock(&authsvc_lock);
148
149 return (AUTH_REJECTEDCRED);
150 }
151
152 enum auth_stat
153 _authenticate(struct svc_req *rqst, struct rpc_msg *msg)
154 {
155 bool_t no_dispatch;
156 return _gss_authenticate(rqst, msg, &no_dispatch);
157 }
158
159 /*
160 * Allow the rpc service to register new authentication types that it is
161 * prepared to handle. When an authentication flavor is registered,
162 * the flavor is checked against already registered values. If not
163 * registered, then a new Auths entry is added on the list.
164 *
165 * There is no provision to delete a registration once registered.
166 *
167 * This routine returns:
168 * 0 if registration successful
169 * 1 if flavor already registered
170 * -1 if can't register (errno set)
171 */
172
173 int
174 svc_auth_reg(cred_flavor, handler)
175 int cred_flavor;
176 enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
177 {
178 struct authsvc *asp;
179 extern mutex_t authsvc_lock;
180
181 switch (cred_flavor) {
182 case AUTH_NULL:
183 case AUTH_SYS:
184 case AUTH_SHORT:
185 case AUTH_DES:
186 #ifndef AUTHDES_SUPPORT
187 return(-1);
188 #endif
189 #ifdef HAVE_RPCSEC_GSS
190 case RPCSEC_GSS:
191 #endif
192 /* already registered */
193 return (1);
194
195 default:
196 mutex_lock(&authsvc_lock);
197 for (asp = Auths; asp; asp = asp->next) {
198 if (asp->flavor == cred_flavor) {
199 /* already registered */
200 mutex_unlock(&authsvc_lock);
201 return (1);
202 }
203 }
204
205 /* this is a new one, so go ahead and register it */
206 asp = mem_alloc(sizeof (*asp));
207 if (asp == NULL) {
208 mutex_unlock(&authsvc_lock);
209 return (-1);
210 }
211 asp->flavor = cred_flavor;
212 asp->handler = handler;
213 asp->next = Auths;
214 Auths = asp;
215 mutex_unlock(&authsvc_lock);
216 break;
217 }
218 return (0);
219 }