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 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * Portions of this source code were derived from Berkeley
31  * 4.3 BSD under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * svc_auth_sys.c
39  * Handles UNIX flavor authentication parameters on the service side of rpc.
40  * There are two svc auth implementations here: AUTH_SYS and AUTH_SHORT.
41  * __svcauth_sys does full blown unix style uid, gid+gids auth,
42  * __svcauth_short uses a shorthand auth to index into a cache of
43  *	longhand auths.
44  * Note: the shorthand has been gutted for efficiency.
45  *
46  */
47 
48 #include <stdio.h>
49 #include <rpc/rpc.h>
50 #include <syslog.h>
51 #include <sys/types.h>
52 #include <string.h>
53 
54 /*
55  * System (Unix) longhand authenticator
56  */
57 enum auth_stat
58 __svcauth_sys(struct svc_req *rqst, struct rpc_msg *msg)
59 {
60 	enum auth_stat stat;
61 	XDR xdrs;
62 	struct authsys_parms *aup;
63 	rpc_inline_t *buf;
64 	struct area {
65 		struct authsys_parms area_aup;
66 		char area_machname[MAX_MACHINE_NAME+1];
67 		gid_t area_gids[NGRPS];
68 	} *area;
69 	uint_t auth_len;
70 	uint_t str_len, gid_len;
71 	int i;
72 
73 	/* LINTED pointer cast */
74 	area = (struct area *)rqst->rq_clntcred;
75 	aup = &area->area_aup;
76 	aup->aup_machname = area->area_machname;
77 	aup->aup_gids = area->area_gids;
78 	auth_len = (uint_t)msg->rm_call.cb_cred.oa_length;
79 	if (auth_len == 0)
80 		return (AUTH_BADCRED);
81 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,
82 			XDR_DECODE);
83 	buf = XDR_INLINE(&xdrs, auth_len);
84 	if (buf != NULL) {
85 		aup->aup_time = IXDR_GET_INT32(buf);
86 		str_len = IXDR_GET_U_INT32(buf);
87 		if (str_len > MAX_MACHINE_NAME) {
88 			stat = AUTH_BADCRED;
89 			goto done;
90 		}
91 		(void) memcpy(aup->aup_machname, buf, str_len);
92 		aup->aup_machname[str_len] = 0;
93 		str_len = RNDUP(str_len);
94 		buf += str_len / (int)sizeof (int32_t);
95 		aup->aup_uid = IXDR_GET_INT32(buf);
96 		aup->aup_gid = IXDR_GET_INT32(buf);
97 		gid_len = IXDR_GET_U_INT32(buf);
98 		if (gid_len > NGRPS) {
99 			stat = AUTH_BADCRED;
100 			goto done;
101 		}
102 		aup->aup_len = gid_len;
103 		for (i = 0; i < gid_len; i++) {
104 			aup->aup_gids[i] = (gid_t)IXDR_GET_INT32(buf);
105 		}
106 		/*
107 		 * five is the smallest unix credentials structure -
108 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
109 		 */
110 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
111 			(void) syslog(LOG_ERR,
112 				"bad auth_len gid %d str %d auth %d",
113 					gid_len, str_len, auth_len);
114 			stat = AUTH_BADCRED;
115 			goto done;
116 		}
117 	} else if (! xdr_authsys_parms(&xdrs, aup)) {
118 		xdrs.x_op = XDR_FREE;
119 		(void) xdr_authsys_parms(&xdrs, aup);
120 		stat = AUTH_BADCRED;
121 		goto done;
122 	}
123 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
124 	rqst->rq_xprt->xp_verf.oa_length = 0;
125 	stat = AUTH_OK;
126 done:
127 	XDR_DESTROY(&xdrs);
128 	return (stat);
129 }
130 
131 /*
132  * Shorthand unix authenticator
133  * Looks up longhand in a cache.
134  */
135 /*ARGSUSED*/
136 enum auth_stat
137 __svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
138 {
139 	return (AUTH_REJECTEDCRED);
140 }
141 
142 /*
143  * Unix longhand authenticator. Will be obsoleted
144  */
145 enum auth_stat
146 __svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
147 {
148 	return (__svcauth_sys(rqst, msg));
149 }
150