xref: /illumos-gate/usr/src/lib/libnsl/rpc/svc_auth_sys.c (revision c5bab7026b8e0ac44b25ee08507ea360f177d844)
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 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
30 /* All Rights Reserved */
31 /*
32  * Portions of this source code were derived from Berkeley
33  * 4.3 BSD under license from the Regents of the University of
34  * California.
35  */
36 
37 /*
38  * Handles UNIX flavor authentication parameters on the service side of rpc.
39  * There are two svc auth implementations here: AUTH_SYS and AUTH_SHORT.
40  * __svcauth_sys does full blown unix style uid, gid+gids auth,
41  * __svcauth_short uses a shorthand auth to index into a cache of
42  *	longhand auths.
43  * Note: the shorthand has been gutted for efficiency.
44  *
45  */
46 
47 #include "mt.h"
48 #include <stdio.h>
49 #include <rpc/rpc.h>
50 #include <syslog.h>
51 #include <sys/types.h>
52 #include <sys/debug.h>
53 #include <string.h>
54 
55 /*
56  * NOTE: this has to fit inside RQCRED_SIZE bytes. If you update this struct,
57  * double-check it still fits.
58  */
59 struct authsys_area {
60 	struct authsys_parms area_aup;
61 	char area_machname[MAX_MACHINE_NAME+1];
62 	gid_t area_gids[NGRPS];
63 };
64 CTASSERT(sizeof (struct authsys_area) <= RQCRED_SIZE);
65 
66 /*
67  * System (Unix) longhand authenticator
68  */
69 enum auth_stat
70 __svcauth_sys(struct svc_req *rqst, struct rpc_msg *msg)
71 {
72 	struct authsys_parms *aup;
73 	int32_t *buf;
74 	struct authsys_area *area;
75 	uint_t auth_len;
76 	uint_t str_len, gid_len;
77 	int i;
78 
79 	/* LINTED pointer cast */
80 	area = (struct authsys_area *)rqst->rq_clntcred;
81 	aup = &area->area_aup;
82 	aup->aup_machname = area->area_machname;
83 	aup->aup_gids = area->area_gids;
84 	auth_len = msg->rm_call.cb_cred.oa_length;
85 	if (auth_len == 0)
86 		return (AUTH_BADCRED);
87 
88 	/* LINTED pointer cast */
89 	buf = (int32_t *)msg->rm_call.cb_cred.oa_base;
90 
91 	aup->aup_time = IXDR_GET_INT32(buf);
92 	str_len = IXDR_GET_U_INT32(buf);
93 	if (str_len > MAX_MACHINE_NAME)
94 		return (AUTH_BADCRED);
95 	(void) memcpy(aup->aup_machname, buf, str_len);
96 	aup->aup_machname[str_len] = 0;
97 	str_len = RNDUP(str_len);
98 	buf += str_len / (int)sizeof (int32_t);
99 	aup->aup_uid = IXDR_GET_INT32(buf);
100 	aup->aup_gid = IXDR_GET_INT32(buf);
101 	gid_len = IXDR_GET_U_INT32(buf);
102 	if (gid_len > NGRPS)
103 		return (AUTH_BADCRED);
104 	aup->aup_len = gid_len;
105 	for (i = 0; i < gid_len; i++) {
106 		aup->aup_gids[i] = (gid_t)IXDR_GET_INT32(buf);
107 	}
108 	/*
109 	 * five is the smallest unix credentials structure -
110 	 * timestamp, hostname len (0), uid, gid, and gids len (0).
111 	 */
112 	if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len)
113 		return (AUTH_BADCRED);
114 
115 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
116 	rqst->rq_xprt->xp_verf.oa_length = 0;
117 
118 	return (AUTH_OK);
119 }
120 
121 /*
122  * Shorthand unix authenticator
123  * Looks up longhand in a cache.
124  */
125 /*ARGSUSED*/
126 enum auth_stat
127 __svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
128 {
129 	return (AUTH_REJECTEDCRED);
130 }
131 
132 /*
133  * Unix longhand authenticator. Will be obsoleted
134  */
135 enum auth_stat
136 __svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
137 {
138 	return (__svcauth_sys(rqst, msg));
139 }
140