xref: /titanic_41/usr/src/uts/common/rpc/sec/authu_prot.c (revision 1ae0874509b6811fdde1dfd46f0d93fd09867a3f)
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  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * authunix_prot.c
39  * XDR for UNIX style authentication parameters for RPC
40  */
41 
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <sys/cred.h>
45 #include <sys/proc.h>
46 #include <sys/user.h>
47 #include <sys/utsname.h>
48 
49 #include <rpc/types.h>
50 #include <rpc/rpc_sztypes.h>
51 #include <rpc/xdr.h>
52 #include <rpc/auth.h>
53 #include <rpc/auth_unix.h>
54 #include <rpc/clnt.h>
55 
56 /*
57  * XDR for unix authentication parameters.
58  */
59 bool_t
60 xdr_authunix_parms(XDR *xdrs, struct authunix_parms *p)
61 {
62 	if (xdr_u_int(xdrs, &p->aup_time) &&
63 	    xdr_string(xdrs, &p->aup_machname, MAX_MACHINE_NAME) &&
64 	    xdr_int(xdrs, (int *)&(p->aup_uid)) &&
65 	    xdr_int(xdrs, (int *)&(p->aup_gid)) &&
66 	    xdr_array(xdrs, (caddr_t *)&(p->aup_gids),
67 		    &(p->aup_len), NGRPS, sizeof (int),
68 		    (xdrproc_t)xdr_int)) {
69 		return (TRUE);
70 	}
71 	return (FALSE);
72 }
73 
74 /*
75  * XDR user id types (uid_t)
76  */
77 bool_t
78 xdr_uid_t(XDR *xdrs, uid_t *ip)
79 {
80 #ifdef lint
81 	(void) (xdr_short(xdrs, (short *)ip));
82 	return (xdr_int32(xdrs, (int32_t *)ip));
83 #else
84 	if (sizeof (uid_t) == sizeof (int32_t)) {
85 		return (xdr_int(xdrs, (int32_t *)ip));
86 	} else {
87 		return (xdr_short(xdrs, (short *)ip));
88 	}
89 #endif
90 }
91 
92 /*
93  * XDR group id types (gid_t)
94  */
95 bool_t
96 xdr_gid_t(XDR *xdrs, gid_t *ip)
97 {
98 #ifdef lint
99 	(void) (xdr_short(xdrs, (short *)ip));
100 	return (xdr_int32(xdrs, (int32_t *)ip));
101 #else
102 	if (sizeof (gid_t) == sizeof (int32_t)) {
103 		return (xdr_int32(xdrs, (int32_t *)ip));
104 	} else {
105 		return (xdr_short(xdrs, (short *)ip));
106 	}
107 #endif
108 }
109 
110 /*
111  * XDR kernel unix auth parameters.
112  * Goes out of the u struct directly.
113  * NOTE: this is an XDR_ENCODE only routine.
114  */
115 bool_t
116 xdr_authkern(XDR *xdrs)
117 {
118 	uid_t uid;
119 	gid_t gid;
120 	uint_t len;
121 	caddr_t groups;
122 	char *name = uts_nodename();
123 	struct cred *cr;
124 	time_t now;
125 
126 	if (xdrs->x_op != XDR_ENCODE)
127 		return (FALSE);
128 
129 	cr = CRED();
130 	uid = crgetuid(cr);
131 	gid = crgetgid(cr);
132 	len = crgetngroups(cr);
133 	groups = (caddr_t)crgetgroups(cr);
134 	now = gethrestime_sec();
135 	if (xdr_uint32(xdrs, (uint32_t *)&now) &&
136 	    xdr_string(xdrs, &name, MAX_MACHINE_NAME) &&
137 	    xdr_uid_t(xdrs, &uid) &&
138 	    xdr_gid_t(xdrs, &gid) &&
139 	    xdr_array(xdrs, &groups, &len, NGRPS, sizeof (int),
140 	    (xdrproc_t)xdr_int))
141 		return (TRUE);
142 	return (FALSE);
143 }
144 
145 /*
146  * XDR loopback unix auth parameters.
147  * NOTE: this is an XDR_ENCODE only routine.
148  */
149 bool_t
150 xdr_authloopback(XDR *xdrs)
151 {
152 	uid_t uid;
153 	gid_t gid;
154 	int len;
155 	caddr_t groups;
156 	char *name = uts_nodename();
157 	struct cred *cr;
158 	time_t now;
159 
160 	if (xdrs->x_op != XDR_ENCODE)
161 		return (FALSE);
162 
163 	cr = CRED();
164 	uid = crgetuid(cr);
165 	gid = crgetgid(cr);
166 	len = crgetngroups(cr);
167 	groups = (caddr_t)crgetgroups(cr);
168 	now = gethrestime_sec();
169 	if (xdr_uint32(xdrs, (uint32_t *)&now) &&
170 	    xdr_string(xdrs, &name, MAX_MACHINE_NAME) &&
171 	    xdr_uid_t(xdrs, &uid) &&
172 	    xdr_gid_t(xdrs, &gid) &&
173 	    xdr_array(xdrs, &groups, (uint_t *)&len, NGRPS_LOOPBACK,
174 	    sizeof (int), (xdrproc_t)xdr_int))
175 		return (TRUE);
176 	return (FALSE);
177 }
178