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