xref: /illumos-gate/usr/src/uts/common/rpc/sec/authu_prot.c (revision cd61ae21816e53b94bc1673f3f1aa651fc3115e8)
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, cred_t *cr)
114 {
115 	uid_t uid;
116 	gid_t gid;
117 	uint_t len;
118 	caddr_t groups;
119 	char *name = uts_nodename();
120 	time_t now;
121 
122 	if (xdrs->x_op != XDR_ENCODE)
123 		return (FALSE);
124 
125 	uid = crgetuid(cr);
126 	gid = crgetgid(cr);
127 	len = crgetngroups(cr);
128 
129 	if (len > NGRPS)
130 		len = NGRPS;
131 
132 	groups = (caddr_t)crgetgroups(cr);
133 	now = gethrestime_sec();
134 	if (xdr_uint32(xdrs, (uint32_t *)&now) &&
135 	    xdr_string(xdrs, &name, MAX_MACHINE_NAME) &&
136 	    xdr_uid_t(xdrs, &uid) &&
137 	    xdr_gid_t(xdrs, &gid) &&
138 	    xdr_array(xdrs, &groups, &len, NGRPS, sizeof (gid_t), xdr_gid_t))
139 		return (TRUE);
140 	return (FALSE);
141 }
142 
143 /*
144  * XDR loopback unix auth parameters.
145  * NOTE: this is an XDR_ENCODE only routine.
146  */
147 bool_t
148 xdr_authloopback(XDR *xdrs, cred_t *cr)
149 {
150 	uid_t uid;
151 	gid_t gid;
152 	uint_t len;
153 	caddr_t groups;
154 	char *name = uts_nodename();
155 	time_t now;
156 
157 	if (xdrs->x_op != XDR_ENCODE)
158 		return (FALSE);
159 
160 	uid = crgetuid(cr);
161 	gid = crgetgid(cr);
162 	len = crgetngroups(cr);
163 	groups = (caddr_t)crgetgroups(cr);
164 	now = gethrestime_sec();
165 	if (xdr_uint32(xdrs, (uint32_t *)&now) &&
166 	    xdr_string(xdrs, &name, MAX_MACHINE_NAME) &&
167 	    xdr_uid_t(xdrs, &uid) &&
168 	    xdr_gid_t(xdrs, &gid) &&
169 	    xdr_array(xdrs, &groups, &len, NGROUPS_UMAX, sizeof (gid_t),
170 	    xdr_gid_t))
171 		return (TRUE);
172 	return (FALSE);
173 }
174