Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libc / rpc / svc_auth.c
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.
8  * 
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.
12  * 
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.
16  * 
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.
20  * 
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.
24  * 
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 /*
30  * Copyright (c) 1986-1991 by Sun Microsystems Inc. 
31  */
32
33 #ident  "@(#)svc_auth.c 1.16    94/04/24 SMI"
34
35 #if !defined(lint) && defined(SCCSIDS)
36 #if 0
37 static char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro";
38 #else
39 static const char rcsid[] =
40  "$FreeBSD: src/lib/libc/rpc/svc_auth.c,v 1.7 1999/12/29 05:04:16 peter Exp $";
41 #endif
42 #endif
43
44 /*
45  * svc_auth.c, Server-side rpc authenticator interface.
46  *
47  */
48
49 #ifdef _KERNEL
50 #include <sys/param.h>
51 #include <rpc/types.h>
52 #include <rpc/xdr.h>
53 #include <rpc/auth.h>
54 #include <rpc/clnt.h>
55 #include <rpc/rpc_msg.h>
56 #include <rpc/svc.h>
57 #include <rpc/svc_auth.h>
58 #else
59 #include <stdlib.h>
60 #include <rpc/rpc.h>
61 #endif
62 #include <sys/types.h>
63
64 /*
65  * svcauthsw is the bdevsw of server side authentication.
66  *
67  * Server side authenticators are called from authenticate by
68  * using the client auth struct flavor field to index into svcauthsw.
69  * The server auth flavors must implement a routine that looks
70  * like:
71  *
72  *      enum auth_stat
73  *      flavorx_auth(rqst, msg)
74  *              register struct svc_req *rqst;
75  *              register struct rpc_msg *msg;
76  *
77  */
78
79 enum auth_stat _svcauth_null(); /* no authentication */
80 enum auth_stat _svcauth_unix();         /* (system) unix style (uid, gids) */
81 enum auth_stat _svcauth_short();        /* short hand unix style */
82 enum auth_stat _svcauth_des();          /* des style */
83
84 /* declarations to allow servers to specify new authentication flavors */
85 struct authsvc {
86         int     flavor;
87         enum    auth_stat (*handler)();
88         struct  authsvc   *next;
89 };
90 static struct authsvc *Auths = NULL;
91
92 /*
93  * The call rpc message, msg has been obtained from the wire.  The msg contains
94  * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
95  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
96  * does the following things:
97  * set rqst->rq_xprt->verf to the appropriate response verifier;
98  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
99  *
100  * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
101  * its length is set appropriately.
102  *
103  * The caller still owns and is responsible for msg->u.cmb.cred and
104  * msg->u.cmb.verf.  The authentication system retains ownership of
105  * rqst->rq_client_cred, the cooked credentials.
106  *
107  * There is an assumption that any flavour less than AUTH_NULL is
108  * invalid.
109  */
110 enum auth_stat
111 _authenticate(rqst, msg)
112         register struct svc_req *rqst;
113         struct rpc_msg *msg;
114 {
115         register int cred_flavor;
116         register struct authsvc *asp;
117
118         rqst->rq_cred = msg->rm_call.cb_cred;
119         rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
120         rqst->rq_xprt->xp_verf.oa_length = 0;
121         cred_flavor = rqst->rq_cred.oa_flavor;
122         switch (cred_flavor) {
123         case AUTH_NULL:
124                 return(_svcauth_null(rqst, msg));
125         case AUTH_UNIX:
126                 return(_svcauth_unix(rqst, msg));
127         case AUTH_SHORT:
128                 return(_svcauth_short(rqst, msg));
129         /*
130          * We leave AUTH_DES turned off by default because svcauth_des()
131          * needs getpublickey(), which is in librpcsvc, not libc. If we
132          * included AUTH_DES as a built-in flavor, programs that don't
133          * have -lrpcsvc in their Makefiles wouldn't link correctly, even
134          * though they don't use AUTH_DES. And I'm too lazy to go through
135          * the tree looking for all of them.
136          */
137 #ifdef DES_BUILTIN
138         case AUTH_DES:
139                 return(_svcauth_des(rqst, msg));
140 #endif
141         }
142
143         /* flavor doesn't match any of the builtin types, so try new ones */
144         for (asp = Auths; asp; asp = asp->next) {
145                 if (asp->flavor == cred_flavor) {
146                         enum auth_stat as;
147
148                         as = (*asp->handler)(rqst, msg);
149                         return (as);
150                 }
151         }
152
153         return (AUTH_REJECTEDCRED);
154 }
155
156 /*ARGSUSED*/
157 enum auth_stat
158 _svcauth_null(rqst, msg)
159         struct svc_req *rqst;
160         struct rpc_msg *msg;
161 {
162         return (AUTH_OK);
163 }
164
165 /*
166  *  Allow the rpc service to register new authentication types that it is
167  *  prepared to handle.  When an authentication flavor is registered,
168  *  the flavor is checked against already registered values.  If not
169  *  registered, then a new Auths entry is added on the list.
170  *
171  *  There is no provision to delete a registration once registered.
172  *
173  *  This routine returns:
174  *       0 if registration successful
175  *       1 if flavor already registered
176  *      -1 if can't register (errno set)
177  */
178
179 int
180 svc_auth_reg(cred_flavor, handler)
181         register int cred_flavor;
182         enum auth_stat (*handler)();
183 {
184         register struct authsvc *asp;
185
186         switch (cred_flavor) {
187             case AUTH_NULL:
188             case AUTH_UNIX:
189             case AUTH_SHORT:
190 #ifdef DES_BUILTIN
191             case AUTH_DES:
192 #endif
193                 /* already registered */
194                 return (1);
195
196             default:
197                 for (asp = Auths; asp; asp = asp->next) {
198                         if (asp->flavor == cred_flavor) {
199                                 /* already registered */
200                                 return (1);
201                         }
202                 }
203
204                 /* this is a new one, so go ahead and register it */
205                 asp = (struct authsvc *)mem_alloc(sizeof (*asp));
206                 if (asp == NULL) {
207                         return (-1);
208                 }
209                 asp->flavor = cred_flavor;
210                 asp->handler = handler;
211                 asp->next = Auths;
212                 Auths = asp;
213                 break;
214         }
215         return (0);
216 }