1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Copyright 2017 Joyent Inc
26  * Use is subject to license terms.
27  */
28 
29 /*
30  * Handles the loopback UNIX flavor authentication parameters on the
31  * service side of rpc.
32  */
33 
34 #include "mt.h"
35 #include <stdio.h>
36 #include <rpc/rpc.h>
37 #include <syslog.h>
38 #include <sys/types.h>
39 #include <sys/debug.h>
40 
41 /*
42  * NOTE: this has to fit inside RQCRED_SIZE bytes. If you update this struct,
43  * double-check it still fits.
44  */
45 struct authlpbk_area {
46 	struct authsys_parms area_aup;
47 	char area_machname[MAX_MACHINE_NAME+1];
48 	gid_t area_gids[NGRPS_LOOPBACK];
49 };
50 CTASSERT(sizeof (struct authlpbk_area) <= RQCRED_SIZE);
51 
52 /*
53  * Loopback system (Unix) longhand authenticator
54  */
55 enum auth_stat
56 __svcauth_loopback(struct svc_req *rqst, struct rpc_msg *msg)
57 {
58 	enum auth_stat stat;
59 	XDR xdrs;
60 	struct authsys_parms *aup;
61 	rpc_inline_t *buf;
62 	struct authlpbk_area *area;
63 	size_t auth_len;
64 	size_t str_len, gid_len;
65 	int i;
66 
67 	/* LINTED pointer cast */
68 	area = (struct authlpbk_area *)rqst->rq_clntcred;
69 	aup = &area->area_aup;
70 	aup->aup_machname = area->area_machname;
71 	aup->aup_gids = area->area_gids;
72 	auth_len = (size_t)msg->rm_call.cb_cred.oa_length;
73 	if (auth_len == 0)
74 		return (AUTH_BADCRED);
75 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,
76 	    XDR_DECODE);
77 	buf = XDR_INLINE(&xdrs, auth_len);
78 	if (buf != NULL) {
79 		aup->aup_time = IXDR_GET_INT32(buf);
80 		str_len = IXDR_GET_U_INT32(buf);
81 		if (str_len > MAX_MACHINE_NAME) {
82 			stat = AUTH_BADCRED;
83 			goto done;
84 		}
85 		if (str_len > auth_len) {
86 			stat = AUTH_BADCRED;
87 			goto done;
88 		}
89 		(void) memcpy(aup->aup_machname, buf, str_len);
90 		aup->aup_machname[str_len] = 0;
91 		str_len = RNDUP(str_len);
92 		buf += str_len / sizeof (int);
93 		aup->aup_uid = IXDR_GET_INT32(buf);
94 		aup->aup_gid = IXDR_GET_INT32(buf);
95 		gid_len = IXDR_GET_U_INT32(buf);
96 		if (gid_len > NGRPS_LOOPBACK) {
97 			stat = AUTH_BADCRED;
98 			goto done;
99 		}
100 		/*
101 		 * five is the smallest unix credentials structure -
102 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
103 		 */
104 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
105 			(void) syslog(LOG_ERR,
106 			    "bad auth_len gid %lu str %lu auth %lu",
107 			    gid_len, str_len, auth_len);
108 			stat = AUTH_BADCRED;
109 			goto done;
110 		}
111 		aup->aup_len = gid_len;
112 		for (i = 0; i < gid_len; i++) {
113 			aup->aup_gids[i] = (gid_t)IXDR_GET_INT32(buf);
114 		}
115 	} else if (!xdr_authloopback_parms(&xdrs, aup)) {
116 		xdrs.x_op = XDR_FREE;
117 		(void) xdr_authloopback_parms(&xdrs, aup);
118 		stat = AUTH_BADCRED;
119 		goto done;
120 	}
121 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
122 	rqst->rq_xprt->xp_verf.oa_length = 0;
123 	stat = AUTH_OK;
124 done:
125 	XDR_DESTROY(&xdrs);
126 	return (stat);
127 }
128