xref: /freebsd/lib/libc/rpc/svc_auth_unix.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
12e322d37SHiroki Sato /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
42e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
52e322d37SHiroki Sato  * All rights reserved.
699064799SGarrett Wollman  *
72e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
82e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
92e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
102e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
112e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
122e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
132e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
142e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
152e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
162e322d37SHiroki Sato  *   from this software without specific prior written permission.
1799064799SGarrett Wollman  *
182e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
192e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
222e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
232e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
242e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
252e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
262e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
272e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
282e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
2999064799SGarrett Wollman  */
3099064799SGarrett Wollman 
3199064799SGarrett Wollman /*
3299064799SGarrett Wollman  * svc_auth_unix.c
3399064799SGarrett Wollman  * Handles UNIX flavor authentication parameters on the service side of rpc.
3499064799SGarrett Wollman  * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
3599064799SGarrett Wollman  * _svcauth_unix does full blown unix style uid,gid+gids auth,
3699064799SGarrett Wollman  * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
3799064799SGarrett Wollman  * Note: the shorthand has been gutted for efficiency.
3899064799SGarrett Wollman  *
3999064799SGarrett Wollman  * Copyright (C) 1984, Sun Microsystems, Inc.
4099064799SGarrett Wollman  */
4199064799SGarrett Wollman 
428360efbdSAlfred Perlstein #include "namespace.h"
438360efbdSAlfred Perlstein #include <assert.h>
4499064799SGarrett Wollman #include <stdio.h>
454c3af266SPoul-Henning Kamp #include <string.h>
468360efbdSAlfred Perlstein 
4799064799SGarrett Wollman #include <rpc/rpc.h>
488360efbdSAlfred Perlstein #include "un-namespace.h"
4999064799SGarrett Wollman 
5099064799SGarrett Wollman /*
5199064799SGarrett Wollman  * Unix longhand authenticator
5299064799SGarrett Wollman  */
5399064799SGarrett Wollman enum auth_stat
_svcauth_unix(struct svc_req * rqst,struct rpc_msg * msg)54587cf682SCraig Rodrigues _svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
5599064799SGarrett Wollman {
568360efbdSAlfred Perlstein 	enum auth_stat stat;
5799064799SGarrett Wollman 	XDR xdrs;
588360efbdSAlfred Perlstein 	struct authunix_parms *aup;
598360efbdSAlfred Perlstein 	int32_t *buf;
6099064799SGarrett Wollman 	struct area {
6199064799SGarrett Wollman 		struct authunix_parms area_aup;
6299064799SGarrett Wollman 		char area_machname[MAX_MACHINE_NAME+1];
630d1040e5SPedro F. Giffuni 		u_int area_gids[NGRPS];
6499064799SGarrett Wollman 	} *area;
6599064799SGarrett Wollman 	u_int auth_len;
668360efbdSAlfred Perlstein 	size_t str_len, gid_len;
678360efbdSAlfred Perlstein 	u_int i;
688360efbdSAlfred Perlstein 
698360efbdSAlfred Perlstein 	assert(rqst != NULL);
708360efbdSAlfred Perlstein 	assert(msg != NULL);
7199064799SGarrett Wollman 
7299064799SGarrett Wollman 	area = (struct area *) rqst->rq_clntcred;
7399064799SGarrett Wollman 	aup = &area->area_aup;
7499064799SGarrett Wollman 	aup->aup_machname = area->area_machname;
7599064799SGarrett Wollman 	aup->aup_gids = area->area_gids;
7699064799SGarrett Wollman 	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
7799064799SGarrett Wollman 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
7899064799SGarrett Wollman 	buf = XDR_INLINE(&xdrs, auth_len);
7999064799SGarrett Wollman 	if (buf != NULL) {
808360efbdSAlfred Perlstein 		aup->aup_time = IXDR_GET_INT32(buf);
818360efbdSAlfred Perlstein 		str_len = (size_t)IXDR_GET_U_INT32(buf);
8299064799SGarrett Wollman 		if (str_len > MAX_MACHINE_NAME) {
8399064799SGarrett Wollman 			stat = AUTH_BADCRED;
8499064799SGarrett Wollman 			goto done;
8599064799SGarrett Wollman 		}
868360efbdSAlfred Perlstein 		memmove(aup->aup_machname, buf, str_len);
8799064799SGarrett Wollman 		aup->aup_machname[str_len] = 0;
8899064799SGarrett Wollman 		str_len = RNDUP(str_len);
893a6ebf36SPeter Wemm 		buf += str_len / sizeof (int32_t);
908360efbdSAlfred Perlstein 		aup->aup_uid = (int)IXDR_GET_INT32(buf);
918360efbdSAlfred Perlstein 		aup->aup_gid = (int)IXDR_GET_INT32(buf);
928360efbdSAlfred Perlstein 		gid_len = (size_t)IXDR_GET_U_INT32(buf);
9399064799SGarrett Wollman 		if (gid_len > NGRPS) {
9499064799SGarrett Wollman 			stat = AUTH_BADCRED;
9599064799SGarrett Wollman 			goto done;
9699064799SGarrett Wollman 		}
9799064799SGarrett Wollman 		aup->aup_len = gid_len;
9899064799SGarrett Wollman 		for (i = 0; i < gid_len; i++) {
998360efbdSAlfred Perlstein 			aup->aup_gids[i] = (int)IXDR_GET_INT32(buf);
10099064799SGarrett Wollman 		}
10199064799SGarrett Wollman 		/*
10299064799SGarrett Wollman 		 * five is the smallest unix credentials structure -
10399064799SGarrett Wollman 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
10499064799SGarrett Wollman 		 */
10599064799SGarrett Wollman 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
1068360efbdSAlfred Perlstein 			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
1078360efbdSAlfred Perlstein 			    (long)gid_len, (long)str_len, auth_len);
10899064799SGarrett Wollman 			stat = AUTH_BADCRED;
10999064799SGarrett Wollman 			goto done;
11099064799SGarrett Wollman 		}
11199064799SGarrett Wollman 	} else if (! xdr_authunix_parms(&xdrs, aup)) {
11299064799SGarrett Wollman 		xdrs.x_op = XDR_FREE;
11399064799SGarrett Wollman 		(void)xdr_authunix_parms(&xdrs, aup);
11499064799SGarrett Wollman 		stat = AUTH_BADCRED;
11599064799SGarrett Wollman 		goto done;
11699064799SGarrett Wollman 	}
117ad133ed6SBill Paul 
118ad133ed6SBill Paul        /* get the verifier */
119ad133ed6SBill Paul 	if ((u_int)msg->rm_call.cb_verf.oa_length) {
120ad133ed6SBill Paul 		rqst->rq_xprt->xp_verf.oa_flavor =
121ad133ed6SBill Paul 			msg->rm_call.cb_verf.oa_flavor;
122ad133ed6SBill Paul 		rqst->rq_xprt->xp_verf.oa_base =
123ad133ed6SBill Paul 			msg->rm_call.cb_verf.oa_base;
124ad133ed6SBill Paul 		rqst->rq_xprt->xp_verf.oa_length =
125ad133ed6SBill Paul 			msg->rm_call.cb_verf.oa_length;
126ad133ed6SBill Paul 	} else {
12799064799SGarrett Wollman 		rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
12899064799SGarrett Wollman 		rqst->rq_xprt->xp_verf.oa_length = 0;
129ad133ed6SBill Paul 	}
13099064799SGarrett Wollman 	stat = AUTH_OK;
13199064799SGarrett Wollman done:
13299064799SGarrett Wollman 	XDR_DESTROY(&xdrs);
13399064799SGarrett Wollman 	return (stat);
13499064799SGarrett Wollman }
13599064799SGarrett Wollman 
13699064799SGarrett Wollman 
13799064799SGarrett Wollman /*
13899064799SGarrett Wollman  * Shorthand unix authenticator
13999064799SGarrett Wollman  * Looks up longhand in a cache.
14099064799SGarrett Wollman  */
14199064799SGarrett Wollman /*ARGSUSED*/
14299064799SGarrett Wollman enum auth_stat
_svcauth_short(struct svc_req * rqst,struct rpc_msg * msg)143587cf682SCraig Rodrigues _svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
14499064799SGarrett Wollman {
14599064799SGarrett Wollman 	return (AUTH_REJECTEDCRED);
14699064799SGarrett Wollman }
147