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