xref: /illumos-gate/usr/src/stand/lib/fs/nfs/auth_unix.c (revision b531f6d1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * Adapted for use by the boot program.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * auth_unix.c, Implements UNIX style authentication parameters.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * The system is very weak.  The client uses no encryption for its
417c478bd9Sstevel@tonic-gate  * credentials and only sends null verifiers.  The server sends backs
427c478bd9Sstevel@tonic-gate  * null verifiers or optionally a verifier that suggests a new short hand
437c478bd9Sstevel@tonic-gate  * for the credentials.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
487c478bd9Sstevel@tonic-gate #include <rpc/types.h>
497c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
507c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
517c478bd9Sstevel@tonic-gate #include "clnt.h"
527c478bd9Sstevel@tonic-gate #include <rpc/auth_unix.h>
537c478bd9Sstevel@tonic-gate #include <sys/promif.h>
547c478bd9Sstevel@tonic-gate #include <sys/salib.h>
557c478bd9Sstevel@tonic-gate #include <sys/bootdebug.h>
567c478bd9Sstevel@tonic-gate #include "nfs_inet.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static struct auth_ops *authunix_ops();
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * This struct is pointed to by the ah_private field of an auth_handle.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate struct audata {
637c478bd9Sstevel@tonic-gate 	struct opaque_auth	au_origcred;	/* original credentials */
647c478bd9Sstevel@tonic-gate 	struct opaque_auth	au_shcred;	/* short hand cred */
657c478bd9Sstevel@tonic-gate 	uint_t			au_shfaults;	/* short hand cache faults */
667c478bd9Sstevel@tonic-gate 	char			au_marshed[MAX_AUTH_BYTES];
677c478bd9Sstevel@tonic-gate 	uint_t			au_mpos;	/* xdr pos at end of marshed */
687c478bd9Sstevel@tonic-gate };
697c478bd9Sstevel@tonic-gate #define	AUTH_PRIVATE(auth)	((struct audata *)auth->ah_private)
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static void marshal_new_auth(AUTH *);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	dprintf	if (boothowto & RB_DEBUG) printf
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Create a unix style authenticator.
777c478bd9Sstevel@tonic-gate  * Returns an auth handle with the given stuff in it.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate AUTH *
authunix_create(char * machname,uid_t uid,gid_t gid,int len,gid_t * aup_gids)807c478bd9Sstevel@tonic-gate authunix_create(char *machname, uid_t uid, gid_t gid, int len, gid_t *aup_gids)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	struct authunix_parms aup;
837c478bd9Sstevel@tonic-gate 	char mymem[MAX_AUTH_BYTES];
847c478bd9Sstevel@tonic-gate 	XDR xdrs;
857c478bd9Sstevel@tonic-gate 	AUTH *auth;
867c478bd9Sstevel@tonic-gate 	struct audata *au;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * Allocate and set up auth handle
907c478bd9Sstevel@tonic-gate 	 */
917c478bd9Sstevel@tonic-gate 	auth = (AUTH *) bkmem_alloc(sizeof (*auth));
927c478bd9Sstevel@tonic-gate 	if (auth == NULL) {
937c478bd9Sstevel@tonic-gate 		prom_panic("authunix_create: Cannot allocate memory.");
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	au = (struct audata *)bkmem_alloc(sizeof (*au));
977c478bd9Sstevel@tonic-gate 	if (au == NULL) {
987c478bd9Sstevel@tonic-gate 		prom_panic("authunix_create: Cannot allocate memory.");
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/* setup authenticator. */
1027c478bd9Sstevel@tonic-gate 	auth->ah_ops = authunix_ops();
1037c478bd9Sstevel@tonic-gate 	auth->ah_private = (caddr_t)au;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* structure copies */
1067c478bd9Sstevel@tonic-gate 	auth->ah_verf = au->au_shcred = _null_auth;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	au->au_shfaults = 0;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * fill in param struct from the given params
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	aup.aup_time = prom_gettime() / 1000;
1147c478bd9Sstevel@tonic-gate 	aup.aup_machname = machname;
1157c478bd9Sstevel@tonic-gate 	aup.aup_uid = uid;
1167c478bd9Sstevel@tonic-gate 	aup.aup_gid = gid;
1177c478bd9Sstevel@tonic-gate 	aup.aup_len = (uint_t)len;
1187c478bd9Sstevel@tonic-gate 	aup.aup_gids = (gid_t *)aup_gids;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * Serialize the parameters into origcred
1227c478bd9Sstevel@tonic-gate 	 */
1237c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
1247c478bd9Sstevel@tonic-gate 	if (!xdr_authunix_parms(&xdrs, &aup)) {
1257c478bd9Sstevel@tonic-gate 		prom_panic("authunix_create:  xdr_authunix_parms failed");
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
1287c478bd9Sstevel@tonic-gate 	au->au_origcred.oa_flavor = (uint_t)AUTH_UNIX;
1297c478bd9Sstevel@tonic-gate 	if ((au->au_origcred.oa_base = bkmem_alloc((uint_t)len)) == NULL) {
1307c478bd9Sstevel@tonic-gate 		prom_panic("authunix_create: memory alloc failed");
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	(void) bcopy(mymem, au->au_origcred.oa_base, (uint_t)len);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/*
1357c478bd9Sstevel@tonic-gate 	 * set auth handle to reflect new cred.
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate 	auth->ah_cred = au->au_origcred;
1387c478bd9Sstevel@tonic-gate 	marshal_new_auth(auth);
1397c478bd9Sstevel@tonic-gate 	return (auth);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * authunix operations
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /* ARGSUSED */
1477c478bd9Sstevel@tonic-gate static void
authunix_nextverf(AUTH * auth)1487c478bd9Sstevel@tonic-gate authunix_nextverf(AUTH *auth)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /* ARGSUSED */
1537c478bd9Sstevel@tonic-gate static bool_t
authunix_marshal(AUTH * auth,XDR * xdrs,cred_t * cr)1547c478bd9Sstevel@tonic-gate authunix_marshal(AUTH *auth, XDR *xdrs, cred_t *cr)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	struct audata *au = AUTH_PRIVATE(auth);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static bool_t
authunix_validate(AUTH * auth,struct opaque_auth * verf)1627c478bd9Sstevel@tonic-gate authunix_validate(AUTH *auth, struct opaque_auth *verf)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	struct audata *au;
1657c478bd9Sstevel@tonic-gate 	XDR xdrs;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (verf->oa_flavor == AUTH_SHORT) {
1687c478bd9Sstevel@tonic-gate 		au = AUTH_PRIVATE(auth);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
1727c478bd9Sstevel@tonic-gate 		    XDR_DECODE);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
1757c478bd9Sstevel@tonic-gate 			auth->ah_cred = au->au_shcred;
1767c478bd9Sstevel@tonic-gate 		} else {
1777c478bd9Sstevel@tonic-gate 			xdrs.x_op = XDR_FREE;
1787c478bd9Sstevel@tonic-gate 			(void) xdr_opaque_auth(&xdrs, &au->au_shcred);
1797c478bd9Sstevel@tonic-gate 			au->au_shcred.oa_base = 0;
1807c478bd9Sstevel@tonic-gate 			auth->ah_cred = au->au_origcred;
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 		marshal_new_auth(auth);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	return (TRUE);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1897c478bd9Sstevel@tonic-gate static bool_t
authunix_refresh(AUTH * auth,struct rpc_msg * msg,cred_t * cr)1907c478bd9Sstevel@tonic-gate authunix_refresh(AUTH *auth, struct rpc_msg *msg, cred_t *cr)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	struct audata *au = AUTH_PRIVATE(auth);
1937c478bd9Sstevel@tonic-gate 	struct authunix_parms aup;
1947c478bd9Sstevel@tonic-gate 	XDR xdrs;
1957c478bd9Sstevel@tonic-gate 	int stat;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
1987c478bd9Sstevel@tonic-gate 		/* there is no hope.  Punt */
1997c478bd9Sstevel@tonic-gate 		return (FALSE);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	au->au_shfaults ++;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* first deserialize the creds back into a struct authunix_parms */
2047c478bd9Sstevel@tonic-gate 	aup.aup_machname = (char *)0;
2057c478bd9Sstevel@tonic-gate 	aup.aup_gids = (gid_t *)0;
2067c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
2077c478bd9Sstevel@tonic-gate 			au->au_origcred.oa_length, XDR_DECODE);
2087c478bd9Sstevel@tonic-gate 	stat = xdr_authunix_parms(&xdrs, &aup);
2097c478bd9Sstevel@tonic-gate 	if (!stat)
2107c478bd9Sstevel@tonic-gate 		goto done;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* update the time and serialize in place */
2137c478bd9Sstevel@tonic-gate 	aup.aup_time = (prom_gettime() / 1000);
2147c478bd9Sstevel@tonic-gate 	xdrs.x_op = XDR_ENCODE;
215*b531f6d1SToomas Soome 	(void) XDR_SETPOS(&xdrs, 0);
2167c478bd9Sstevel@tonic-gate 	stat = xdr_authunix_parms(&xdrs, &aup);
2177c478bd9Sstevel@tonic-gate 	if (!stat)
2187c478bd9Sstevel@tonic-gate 		goto done;
2197c478bd9Sstevel@tonic-gate 	auth->ah_cred = au->au_origcred;
2207c478bd9Sstevel@tonic-gate 	marshal_new_auth(auth);
2217c478bd9Sstevel@tonic-gate done:
2227c478bd9Sstevel@tonic-gate 	/* free the struct authunix_parms created by deserializing */
2237c478bd9Sstevel@tonic-gate 	xdrs.x_op = XDR_FREE;
2247c478bd9Sstevel@tonic-gate 	(void) xdr_authunix_parms(&xdrs, &aup);
2257c478bd9Sstevel@tonic-gate 	XDR_DESTROY(&xdrs);
2267c478bd9Sstevel@tonic-gate 	return (stat);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static void
authunix_destroy(AUTH * auth)2307c478bd9Sstevel@tonic-gate authunix_destroy(AUTH *auth)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	struct audata *au = AUTH_PRIVATE(auth);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (au->au_shcred.oa_base != NULL)
2357c478bd9Sstevel@tonic-gate 		bkmem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
2367c478bd9Sstevel@tonic-gate 	bkmem_free(auth->ah_private, sizeof (struct audata));
2377c478bd9Sstevel@tonic-gate 	if (auth->ah_verf.oa_base != NULL)
2387c478bd9Sstevel@tonic-gate 		bkmem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
2397c478bd9Sstevel@tonic-gate 	bkmem_free((caddr_t)auth, sizeof (*auth));
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * Marshals (pre-serializes) an auth struct.
2447c478bd9Sstevel@tonic-gate  * sets private data, au_marshed and au_mpos
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate static void
marshal_new_auth(AUTH * auth)2477c478bd9Sstevel@tonic-gate marshal_new_auth(AUTH *auth)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	XDR xdr_stream;
2507c478bd9Sstevel@tonic-gate 	XDR *xdrs = &xdr_stream;
2517c478bd9Sstevel@tonic-gate 	struct audata *au = AUTH_PRIVATE(auth);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
2547c478bd9Sstevel@tonic-gate 	if ((!xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
2557c478bd9Sstevel@tonic-gate 	    (!xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
2567c478bd9Sstevel@tonic-gate 		dprintf("marshal_new_auth - Fatal marshalling problem");
2577c478bd9Sstevel@tonic-gate 	} else {
2587c478bd9Sstevel@tonic-gate 		au->au_mpos = XDR_GETPOS(xdrs);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	XDR_DESTROY(xdrs);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static struct auth_ops *
authunix_ops(void)2657c478bd9Sstevel@tonic-gate authunix_ops(void)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	static struct auth_ops ops;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if (ops.ah_nextverf == 0) {
2707c478bd9Sstevel@tonic-gate 		ops.ah_nextverf = authunix_nextverf;
2717c478bd9Sstevel@tonic-gate 		ops.ah_marshal = authunix_marshal;
2727c478bd9Sstevel@tonic-gate 		ops.ah_validate = authunix_validate;
2737c478bd9Sstevel@tonic-gate 		ops.ah_refresh = authunix_refresh;
2747c478bd9Sstevel@tonic-gate 		ops.ah_destroy = authunix_destroy;
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	return (&ops);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * XDR for unix authentication parameters.
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate bool_t
xdr_authunix_parms(XDR * xdrs,struct authunix_parms * p)2837c478bd9Sstevel@tonic-gate xdr_authunix_parms(XDR *xdrs, struct authunix_parms *p)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	if (xdr_u_int(xdrs, &(p->aup_time)) &&
2867c478bd9Sstevel@tonic-gate 	    xdr_string(xdrs, &(p->aup_machname), MAX_MACHINE_NAME) &&
2877c478bd9Sstevel@tonic-gate 	    xdr_int(xdrs, (int *)&(p->aup_uid)) &&
2887c478bd9Sstevel@tonic-gate 	    xdr_int(xdrs, (int *)&(p->aup_gid)) &&
2897c478bd9Sstevel@tonic-gate 	    xdr_array(xdrs, (caddr_t *)&(p->aup_gids),
2907c478bd9Sstevel@tonic-gate 		    &(p->aup_len), NGRPS, sizeof (int), xdr_int)) {
2917c478bd9Sstevel@tonic-gate 		return (TRUE);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	return (FALSE);
2947c478bd9Sstevel@tonic-gate }
295