17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2161961e0fSrobinson  */
2261961e0fSrobinson 
2361961e0fSrobinson /*
24e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25b1cdc720SAlex Wilson  * Copyright 2017 Joyent Inc
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
28e8031f0aSraf 
297c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
307c478bd9Sstevel@tonic-gate /* All Rights Reserved */
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
337c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
347c478bd9Sstevel@tonic-gate  * California.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * Handles UNIX flavor authentication parameters on the service side of rpc.
397c478bd9Sstevel@tonic-gate  * There are two svc auth implementations here: AUTH_SYS and AUTH_SHORT.
407c478bd9Sstevel@tonic-gate  * __svcauth_sys does full blown unix style uid, gid+gids auth,
417c478bd9Sstevel@tonic-gate  * __svcauth_short uses a shorthand auth to index into a cache of
427c478bd9Sstevel@tonic-gate  *	longhand auths.
437c478bd9Sstevel@tonic-gate  * Note: the shorthand has been gutted for efficiency.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
47e8031f0aSraf #include "mt.h"
487c478bd9Sstevel@tonic-gate #include <stdio.h>
497c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
507c478bd9Sstevel@tonic-gate #include <syslog.h>
517c478bd9Sstevel@tonic-gate #include <sys/types.h>
52b1cdc720SAlex Wilson #include <sys/debug.h>
5361961e0fSrobinson #include <string.h>
547c478bd9Sstevel@tonic-gate 
55b1cdc720SAlex Wilson /*
56b1cdc720SAlex Wilson  * NOTE: this has to fit inside RQCRED_SIZE bytes. If you update this struct,
57b1cdc720SAlex Wilson  * double-check it still fits.
58b1cdc720SAlex Wilson  */
59b1cdc720SAlex Wilson struct authsys_area {
60b1cdc720SAlex Wilson 	struct authsys_parms area_aup;
61b1cdc720SAlex Wilson 	char area_machname[MAX_MACHINE_NAME+1];
62b1cdc720SAlex Wilson 	gid_t area_gids[NGRPS];
63b1cdc720SAlex Wilson };
64b1cdc720SAlex Wilson CTASSERT(sizeof (struct authsys_area) <= RQCRED_SIZE);
65b1cdc720SAlex Wilson 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * System (Unix) longhand authenticator
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate enum auth_stat
__svcauth_sys(struct svc_req * rqst,struct rpc_msg * msg)7061961e0fSrobinson __svcauth_sys(struct svc_req *rqst, struct rpc_msg *msg)
717c478bd9Sstevel@tonic-gate {
7261961e0fSrobinson 	struct authsys_parms *aup;
73*5806135aSMarcel Telka 	int32_t *buf;
74b1cdc720SAlex Wilson 	struct authsys_area *area;
7561961e0fSrobinson 	uint_t auth_len;
7661961e0fSrobinson 	uint_t str_len, gid_len;
7761961e0fSrobinson 	int i;
787c478bd9Sstevel@tonic-gate 
7961961e0fSrobinson 	/* LINTED pointer cast */
80b1cdc720SAlex Wilson 	area = (struct authsys_area *)rqst->rq_clntcred;
817c478bd9Sstevel@tonic-gate 	aup = &area->area_aup;
827c478bd9Sstevel@tonic-gate 	aup->aup_machname = area->area_machname;
837c478bd9Sstevel@tonic-gate 	aup->aup_gids = area->area_gids;
84*5806135aSMarcel Telka 	auth_len = msg->rm_call.cb_cred.oa_length;
8561961e0fSrobinson 	if (auth_len == 0)
867c478bd9Sstevel@tonic-gate 		return (AUTH_BADCRED);
87*5806135aSMarcel Telka 
88*5806135aSMarcel Telka 	/* LINTED pointer cast */
89*5806135aSMarcel Telka 	buf = (int32_t *)msg->rm_call.cb_cred.oa_base;
90*5806135aSMarcel Telka 
91*5806135aSMarcel Telka 	aup->aup_time = IXDR_GET_INT32(buf);
92*5806135aSMarcel Telka 	str_len = IXDR_GET_U_INT32(buf);
93*5806135aSMarcel Telka 	if (str_len > MAX_MACHINE_NAME)
94*5806135aSMarcel Telka 		return (AUTH_BADCRED);
95*5806135aSMarcel Telka 	(void) memcpy(aup->aup_machname, buf, str_len);
96*5806135aSMarcel Telka 	aup->aup_machname[str_len] = 0;
97*5806135aSMarcel Telka 	str_len = RNDUP(str_len);
98*5806135aSMarcel Telka 	buf += str_len / (int)sizeof (int32_t);
99*5806135aSMarcel Telka 	aup->aup_uid = IXDR_GET_INT32(buf);
100*5806135aSMarcel Telka 	aup->aup_gid = IXDR_GET_INT32(buf);
101*5806135aSMarcel Telka 	gid_len = IXDR_GET_U_INT32(buf);
102*5806135aSMarcel Telka 	if (gid_len > NGRPS)
103*5806135aSMarcel Telka 		return (AUTH_BADCRED);
104*5806135aSMarcel Telka 	aup->aup_len = gid_len;
105*5806135aSMarcel Telka 	for (i = 0; i < gid_len; i++) {
106*5806135aSMarcel Telka 		aup->aup_gids[i] = (gid_t)IXDR_GET_INT32(buf);
1077c478bd9Sstevel@tonic-gate 	}
108*5806135aSMarcel Telka 	/*
109*5806135aSMarcel Telka 	 * five is the smallest unix credentials structure -
110*5806135aSMarcel Telka 	 * timestamp, hostname len (0), uid, gid, and gids len (0).
111*5806135aSMarcel Telka 	 */
112*5806135aSMarcel Telka 	if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len)
113*5806135aSMarcel Telka 		return (AUTH_BADCRED);
114*5806135aSMarcel Telka 
1157c478bd9Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
1167c478bd9Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_length = 0;
117*5806135aSMarcel Telka 
118*5806135aSMarcel Telka 	return (AUTH_OK);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Shorthand unix authenticator
1237c478bd9Sstevel@tonic-gate  * Looks up longhand in a cache.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1267c478bd9Sstevel@tonic-gate enum auth_stat
__svcauth_short(struct svc_req * rqst,struct rpc_msg * msg)12761961e0fSrobinson __svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	return (AUTH_REJECTEDCRED);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Unix longhand authenticator. Will be obsoleted
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate enum auth_stat
__svcauth_unix(struct svc_req * rqst,struct rpc_msg * msg)13661961e0fSrobinson __svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
1377c478bd9Sstevel@tonic-gate {
13861961e0fSrobinson 	return (__svcauth_sys(rqst, msg));
1397c478bd9Sstevel@tonic-gate }
140