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