xref: /titanic_50/usr/src/uts/common/rpc/sec/authu_prot.c (revision 37b285d61e57561b538d999834714df1f5c2db53)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * Portions of this source code were derived from Berkeley 4.3 BSD
31  * under license from the Regents of the University of California.
32  */
33 
34 /*
35  * authunix_prot.c
36  * XDR for UNIX style authentication parameters for RPC
37  */
38 
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/cred.h>
42 #include <sys/proc.h>
43 #include <sys/user.h>
44 #include <sys/utsname.h>
45 
46 #include <rpc/types.h>
47 #include <rpc/rpc_sztypes.h>
48 #include <rpc/xdr.h>
49 #include <rpc/auth.h>
50 #include <rpc/auth_unix.h>
51 #include <rpc/clnt.h>
52 
53 /*
54  * XDR for unix authentication parameters.
55  */
56 bool_t
57 xdr_authunix_parms(XDR *xdrs, struct authunix_parms *p)
58 {
59 	if (xdr_u_int(xdrs, &p->aup_time) &&
60 	    xdr_string(xdrs, &p->aup_machname, MAX_MACHINE_NAME) &&
61 	    xdr_int(xdrs, (int *)&(p->aup_uid)) &&
62 	    xdr_int(xdrs, (int *)&(p->aup_gid)) &&
63 	    xdr_array(xdrs, (caddr_t *)&(p->aup_gids),
64 	    &(p->aup_len), NGRPS, sizeof (int),
65 	    (xdrproc_t)xdr_int)) {
66 		return (TRUE);
67 	}
68 	return (FALSE);
69 }
70 
71 /*
72  * XDR user id types (uid_t)
73  */
74 bool_t
75 xdr_uid_t(XDR *xdrs, uid_t *ip)
76 {
77 #ifdef lint
78 	(void) (xdr_short(xdrs, (short *)ip));
79 	return (xdr_int32(xdrs, (int32_t *)ip));
80 #else
81 	if (sizeof (uid_t) == sizeof (int32_t)) {
82 		return (xdr_int(xdrs, (int32_t *)ip));
83 	} else {
84 		return (xdr_short(xdrs, (short *)ip));
85 	}
86 #endif
87 }
88 
89 /*
90  * XDR group id types (gid_t)
91  */
92 bool_t
93 xdr_gid_t(XDR *xdrs, gid_t *ip)
94 {
95 #ifdef lint
96 	(void) (xdr_short(xdrs, (short *)ip));
97 	return (xdr_int32(xdrs, (int32_t *)ip));
98 #else
99 	if (sizeof (gid_t) == sizeof (int32_t)) {
100 		return (xdr_int32(xdrs, (int32_t *)ip));
101 	} else {
102 		return (xdr_short(xdrs, (short *)ip));
103 	}
104 #endif
105 }
106 
107 /*
108  * XDR kernel unix auth parameters.
109  * Goes out of the u struct directly.
110  * NOTE: this is an XDR_ENCODE only routine.
111  */
112 bool_t
113 xdr_authkern(XDR *xdrs)
114 {
115 	uid_t uid;
116 	gid_t gid;
117 	uint_t len;
118 	caddr_t groups;
119 	char *name = uts_nodename();
120 	struct cred *cr;
121 	time_t now;
122 
123 	if (xdrs->x_op != XDR_ENCODE)
124 		return (FALSE);
125 
126 	cr = CRED();
127 	uid = crgetuid(cr);
128 	gid = crgetgid(cr);
129 	len = crgetngroups(cr);
130 
131 	if (len > NGRPS)
132 		len = NGRPS;
133 
134 	groups = (caddr_t)crgetgroups(cr);
135 	now = gethrestime_sec();
136 	if (xdr_uint32(xdrs, (uint32_t *)&now) &&
137 	    xdr_string(xdrs, &name, MAX_MACHINE_NAME) &&
138 	    xdr_uid_t(xdrs, &uid) &&
139 	    xdr_gid_t(xdrs, &gid) &&
140 	    xdr_array(xdrs, &groups, &len, NGRPS, sizeof (int),
141 	    (xdrproc_t)xdr_int))
142 		return (TRUE);
143 	return (FALSE);
144 }
145 
146 /*
147  * XDR loopback unix auth parameters.
148  * NOTE: this is an XDR_ENCODE only routine.
149  */
150 bool_t
151 xdr_authloopback(XDR *xdrs)
152 {
153 	uid_t uid;
154 	gid_t gid;
155 	int len;
156 	caddr_t groups;
157 	char *name = uts_nodename();
158 	struct cred *cr;
159 	time_t now;
160 
161 	if (xdrs->x_op != XDR_ENCODE)
162 		return (FALSE);
163 
164 	cr = CRED();
165 	uid = crgetuid(cr);
166 	gid = crgetgid(cr);
167 	len = crgetngroups(cr);
168 	groups = (caddr_t)crgetgroups(cr);
169 	now = gethrestime_sec();
170 	if (xdr_uint32(xdrs, (uint32_t *)&now) &&
171 	    xdr_string(xdrs, &name, MAX_MACHINE_NAME) &&
172 	    xdr_uid_t(xdrs, &uid) &&
173 	    xdr_gid_t(xdrs, &gid) &&
174 	    xdr_array(xdrs, &groups, (uint_t *)&len, NGRPS_LOOPBACK,
175 	    sizeof (int), (xdrproc_t)xdr_int))
176 		return (TRUE);
177 	return (FALSE);
178 }
179