1 /* @(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
9 *
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 *
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
17 *
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
21 *
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
29 */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35 * auth_unix.c, Implements UNIX style authentication parameters.
36 *
37 * Copyright (C) 1984, Sun Microsystems, Inc.
38 *
39 * The system is very weak. The client uses no encryption for it's
40 * credentials and only sends null verifiers. The server sends backs
41 * null verifiers or optionally a verifier that suggests a new short hand
42 * for the credentials.
43 *
44 */
45
46 #include <stdio.h>
47
48 #include <rpc/types.h>
49 #include <rpc/xdr.h>
50 #include <rpc/auth.h>
51 #include <rpc/auth_unix.h>
52
53 /*
54 * Unix authenticator operations vector
55 */
56 static void authunix_nextverf();
57 static bool_t authunix_marshal();
58 static bool_t authunix_validate();
59 static bool_t authunix_refresh();
60 static void authunix_destroy();
61
62 static struct auth_ops auth_unix_ops = {
63 authunix_nextverf,
64 authunix_marshal,
65 authunix_validate,
66 authunix_refresh,
67 authunix_destroy
68 };
69
70 /*
71 * This struct is pointed to by the ah_private field of an auth_handle.
72 */
73 struct audata {
74 struct opaque_auth au_origcred; /* original credentials */
75 struct opaque_auth au_shcred; /* short hand cred */
76 u_long au_shfaults; /* short hand cache faults */
77 char au_marshed[MAX_AUTH_BYTES];
78 u_int au_mpos; /* xdr pos at end of marshed */
79 };
80 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
81
82 static bool_t marshal_new_auth();
83
84
85 /*
86 * Create a unix style authenticator.
87 * Returns an auth handle with the given stuff in it.
88 */
89 AUTH *
authunix_create(machname,uid,gid,len,aup_gids)90 authunix_create(machname, uid, gid, len, aup_gids)
91 char *machname;
92 int uid;
93 int gid;
94 register int len;
95 int *aup_gids;
96 {
97 struct authunix_parms aup;
98 char mymem[MAX_AUTH_BYTES];
99 struct timeval now;
100 XDR xdrs;
101 register AUTH *auth;
102 register struct audata *au;
103
104 /*
105 * Allocate and set up auth handle
106 */
107 auth = (AUTH *)mem_alloc(sizeof(*auth));
108 #ifndef KERNEL
109 if (auth == NULL) {
110 (void)fprintf(stderr, "authunix_create: out of memory\n");
111 return (NULL);
112 }
113 #endif
114 au = (struct audata *)mem_alloc(sizeof(*au));
115 #ifndef KERNEL
116 if (au == NULL) {
117 (void)fprintf(stderr, "authunix_create: out of memory\n");
118 return (NULL);
119 }
120 #endif
121 auth->ah_ops = &auth_unix_ops;
122 auth->ah_private = (caddr_t)au;
123 auth->ah_verf = au->au_shcred = _null_auth;
124 au->au_shfaults = 0;
125
126 /*
127 * fill in param struct from the given params
128 */
129 (void)gettimeofday(&now, (struct timezone *)0);
130 aup.aup_time = now.tv_sec;
131 aup.aup_machname = machname;
132 aup.aup_uid = uid;
133 aup.aup_gid = gid;
134 aup.aup_len = (u_int)len;
135 aup.aup_gids = aup_gids;
136
137 /*
138 * Serialize the parameters into origcred
139 */
140 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
141 if (! xdr_authunix_parms(&xdrs, &aup))
142 abort();
143 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
144 au->au_origcred.oa_flavor = AUTH_UNIX;
145 #ifdef KERNEL
146 au->au_origcred.oa_base = mem_alloc((u_int) len);
147 #else
148 if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
149 (void)fprintf(stderr, "authunix_create: out of memory\n");
150 return (NULL);
151 }
152 #endif
153 bcopy(mymem, au->au_origcred.oa_base, (u_int)len);
154
155 /*
156 * set auth handle to reflect new cred.
157 */
158 auth->ah_cred = au->au_origcred;
159 marshal_new_auth(auth);
160 return (auth);
161 }
162
163 /*
164 * Some servers will refuse mounts if the group list is larger
165 * than it expects (like 8). This allows the application to set
166 * the maximum size of the group list that will be sent.
167 */
168
169 static maxgrplist = NGRPS;
170
set_rpc_maxgrouplist(num)171 set_rpc_maxgrouplist(num)
172 int num;
173 {
174
175 if (num < NGRPS)
176 maxgrplist = num;
177 }
178
179 /*
180 * Returns an auth handle with parameters determined by doing lots of
181 * syscalls.
182 */
183 AUTH *
authunix_create_default()184 authunix_create_default()
185 {
186 register int len;
187 char machname[MAX_MACHINE_NAME + 1];
188 register int uid;
189 register int gid;
190 int gids[NGRPS];
191
192 if (gethostname(machname, MAX_MACHINE_NAME) == -1)
193 abort();
194 machname[MAX_MACHINE_NAME] = 0;
195 uid = geteuid();
196 gid = getegid();
197 if ((len = getgroups(NGRPS, gids)) < 0)
198 abort();
199 if (len > maxgrplist)
200 len = maxgrplist;
201 return (authunix_create(machname, uid, gid, len, gids));
202 }
203
204 /*
205 * authunix operations
206 */
207
208 static void
authunix_nextverf(auth)209 authunix_nextverf(auth)
210 AUTH *auth;
211 {
212 /* no action necessary */
213 }
214
215 static bool_t
authunix_marshal(auth,xdrs)216 authunix_marshal(auth, xdrs)
217 AUTH *auth;
218 XDR *xdrs;
219 {
220 register struct audata *au = AUTH_PRIVATE(auth);
221
222 return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
223 }
224
225 static bool_t
authunix_validate(auth,verf)226 authunix_validate(auth, verf)
227 register AUTH *auth;
228 struct opaque_auth verf;
229 {
230 register struct audata *au;
231 XDR xdrs;
232
233 if (verf.oa_flavor == AUTH_SHORT) {
234 au = AUTH_PRIVATE(auth);
235 xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
236
237 if (au->au_shcred.oa_base != NULL) {
238 mem_free(au->au_shcred.oa_base,
239 au->au_shcred.oa_length);
240 au->au_shcred.oa_base = NULL;
241 }
242 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
243 auth->ah_cred = au->au_shcred;
244 } else {
245 xdrs.x_op = XDR_FREE;
246 (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
247 au->au_shcred.oa_base = NULL;
248 auth->ah_cred = au->au_origcred;
249 }
250 marshal_new_auth(auth);
251 }
252 return (TRUE);
253 }
254
255 static bool_t
authunix_refresh(auth)256 authunix_refresh(auth)
257 register AUTH *auth;
258 {
259 register struct audata *au = AUTH_PRIVATE(auth);
260 struct authunix_parms aup;
261 struct timeval now;
262 XDR xdrs;
263 register int stat;
264
265 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
266 /* there is no hope. Punt */
267 return (FALSE);
268 }
269 au->au_shfaults ++;
270
271 /* first deserialize the creds back into a struct authunix_parms */
272 aup.aup_machname = NULL;
273 aup.aup_gids = (int *)NULL;
274 xdrmem_create(&xdrs, au->au_origcred.oa_base,
275 au->au_origcred.oa_length, XDR_DECODE);
276 stat = xdr_authunix_parms(&xdrs, &aup);
277 if (! stat)
278 goto done;
279
280 /* update the time and serialize in place */
281 (void)gettimeofday(&now, (struct timezone *)0);
282 aup.aup_time = now.tv_sec;
283 xdrs.x_op = XDR_ENCODE;
284 XDR_SETPOS(&xdrs, 0);
285 stat = xdr_authunix_parms(&xdrs, &aup);
286 if (! stat)
287 goto done;
288 auth->ah_cred = au->au_origcred;
289 marshal_new_auth(auth);
290 done:
291 /* free the struct authunix_parms created by deserializing */
292 xdrs.x_op = XDR_FREE;
293 (void)xdr_authunix_parms(&xdrs, &aup);
294 XDR_DESTROY(&xdrs);
295 return (stat);
296 }
297
298 static void
authunix_destroy(auth)299 authunix_destroy(auth)
300 register AUTH *auth;
301 {
302 register struct audata *au = AUTH_PRIVATE(auth);
303
304 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
305
306 if (au->au_shcred.oa_base != NULL)
307 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
308
309 mem_free(auth->ah_private, sizeof(struct audata));
310
311 if (auth->ah_verf.oa_base != NULL)
312 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
313
314 mem_free((caddr_t)auth, sizeof(*auth));
315 }
316
317 /*
318 * Marshals (pre-serializes) an auth struct.
319 * sets private data, au_marshed and au_mpos
320 */
321 static bool_t
marshal_new_auth(auth)322 marshal_new_auth(auth)
323 register AUTH *auth;
324 {
325 XDR xdr_stream;
326 register XDR *xdrs = &xdr_stream;
327 register struct audata *au = AUTH_PRIVATE(auth);
328
329 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
330 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
331 (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
332 perror("auth_none.c - Fatal marshalling problem");
333 } else {
334 au->au_mpos = XDR_GETPOS(xdrs);
335 }
336 XDR_DESTROY(xdrs);
337 }
338