Home | History | Annotate | Line # | Download | only in rpc
auth_unix.c revision 1.14
      1 /*	$NetBSD: auth_unix.c,v 1.14 1999/01/20 11:37:34 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.14 1999/01/20 11:37:34 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 
     56 #include <sys/param.h>
     57 
     58 #include <err.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 
     64 #include <rpc/types.h>
     65 #include <rpc/xdr.h>
     66 #include <rpc/auth.h>
     67 #include <rpc/auth_unix.h>
     68 
     69 #ifdef __weak_alias
     70 __weak_alias(authunix_create,_authunix_create);
     71 __weak_alias(authunix_create_default,_authunix_create_default);
     72 #endif
     73 
     74 
     75 /* auth_unix.c */
     76 static void authunix_nextverf __P((AUTH *));
     77 static bool_t authunix_marshal __P((AUTH *, XDR *));
     78 static bool_t authunix_validate __P((AUTH *, struct opaque_auth *));
     79 static bool_t authunix_refresh __P((AUTH *));
     80 static void authunix_destroy __P((AUTH *));
     81 static void marshal_new_auth __P((AUTH *));
     82 
     83 /*
     84  * Unix authenticator operations vector
     85  */
     86 static const struct auth_ops auth_unix_ops = {
     87 	authunix_nextverf,
     88 	authunix_marshal,
     89 	authunix_validate,
     90 	authunix_refresh,
     91 	authunix_destroy
     92 };
     93 
     94 /*
     95  * This struct is pointed to by the ah_private field of an auth_handle.
     96  */
     97 struct audata {
     98 	struct opaque_auth	au_origcred;	/* original credentials */
     99 	struct opaque_auth	au_shcred;	/* short hand cred */
    100 	u_long			au_shfaults;	/* short hand cache faults */
    101 	char			au_marshed[MAX_AUTH_BYTES];
    102 	u_int			au_mpos;	/* xdr pos at end of marshed */
    103 };
    104 #define	AUTH_PRIVATE(auth)	((struct audata *)auth->ah_private)
    105 
    106 /*
    107  * Create a unix style authenticator.
    108  * Returns an auth handle with the given stuff in it.
    109  */
    110 AUTH *
    111 authunix_create(machname, uid, gid, len, aup_gids)
    112 	char *machname;
    113 	int uid;
    114 	int gid;
    115 	int len;
    116 	int *aup_gids;
    117 {
    118 	struct authunix_parms aup;
    119 	char mymem[MAX_AUTH_BYTES];
    120 	struct timeval now;
    121 	XDR xdrs;
    122 	AUTH *auth;
    123 	struct audata *au;
    124 
    125 	/*
    126 	 * Allocate and set up auth handle
    127 	 */
    128 	au = NULL;
    129 	auth = (AUTH *)mem_alloc(sizeof(*auth));
    130 #ifndef KERNEL
    131 	if (auth == NULL) {
    132 		warnx("authunix_create: out of memory");
    133 		goto cleanup_authunix_create;
    134 	}
    135 #endif
    136 	au = (struct audata *)mem_alloc(sizeof(*au));
    137 #ifndef KERNEL
    138 	if (au == NULL) {
    139 		warnx("authunix_create: out of memory");
    140 		goto cleanup_authunix_create;
    141 	}
    142 #endif
    143 	auth->ah_ops = &auth_unix_ops;
    144 	auth->ah_private = au;
    145 	auth->ah_verf = au->au_shcred = _null_auth;
    146 	au->au_shfaults = 0;
    147 	au->au_origcred.oa_base = NULL;
    148 
    149 	/*
    150 	 * fill in param struct from the given params
    151 	 */
    152 	(void)gettimeofday(&now,  (struct timezone *)0);
    153 	aup.aup_time = now.tv_sec;
    154 	aup.aup_machname = machname;
    155 	aup.aup_uid = uid;
    156 	aup.aup_gid = gid;
    157 	aup.aup_len = (u_int)len;
    158 	aup.aup_gids = aup_gids;
    159 
    160 	/*
    161 	 * Serialize the parameters into origcred
    162 	 */
    163 	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
    164 	if (! xdr_authunix_parms(&xdrs, &aup))
    165 		abort();
    166 	au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
    167 	au->au_origcred.oa_flavor = AUTH_UNIX;
    168 #ifdef KERNEL
    169 	au->au_origcred.oa_base = mem_alloc((u_int) len);
    170 #else
    171 	if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
    172 		warnx("authunix_create: out of memory");
    173 		goto cleanup_authunix_create;
    174 	}
    175 #endif
    176 	memmove(au->au_origcred.oa_base, mymem, (size_t)len);
    177 
    178 	/*
    179 	 * set auth handle to reflect new cred.
    180 	 */
    181 	auth->ah_cred = au->au_origcred;
    182 	marshal_new_auth(auth);
    183 	return (auth);
    184 #ifndef KERNEL
    185  cleanup_authunix_create:
    186 	if (auth)
    187 		mem_free(auth, sizeof(*auth));
    188 	if (au) {
    189 		if (au->au_origcred.oa_base)
    190 			mem_free(au->au_origcred.oa_base, (u_int)len);
    191 		mem_free(au, sizeof(*au));
    192 	}
    193 	return (NULL);
    194 #endif
    195 }
    196 
    197 /*
    198  * Returns an auth handle with parameters determined by doing lots of
    199  * syscalls.
    200  */
    201 AUTH *
    202 authunix_create_default()
    203 {
    204 	int len;
    205 	char machname[MAXHOSTNAMELEN + 1];
    206 	uid_t uid;
    207 	gid_t gid;
    208 	gid_t gids[NGRPS];
    209 
    210 	if (gethostname(machname, sizeof machname) == -1)
    211 		abort();
    212 	machname[sizeof(machname) - 1] = 0;
    213 	uid = geteuid();
    214 	gid = getegid();
    215 	if ((len = getgroups(NGRPS, gids)) < 0)
    216 		abort();
    217 	/* XXX: interface problem; those should all have been unsigned */
    218 	return (authunix_create(machname, (int)uid, (int)gid, len,
    219 	    (int *)gids));
    220 }
    221 
    222 /*
    223  * authunix operations
    224  */
    225 
    226 /* ARGSUSED */
    227 static void
    228 authunix_nextverf(auth)
    229 	AUTH *auth;
    230 {
    231 	/* no action necessary */
    232 }
    233 
    234 static bool_t
    235 authunix_marshal(auth, xdrs)
    236 	AUTH *auth;
    237 	XDR *xdrs;
    238 {
    239 	struct audata *au = AUTH_PRIVATE(auth);
    240 
    241 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
    242 }
    243 
    244 static bool_t
    245 authunix_validate(auth, verf)
    246 	AUTH *auth;
    247 	struct opaque_auth *verf;
    248 {
    249 	struct audata *au;
    250 	XDR xdrs;
    251 
    252 	if (verf->oa_flavor == AUTH_SHORT) {
    253 		au = AUTH_PRIVATE(auth);
    254 		xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
    255 		    XDR_DECODE);
    256 
    257 		if (au->au_shcred.oa_base != NULL) {
    258 			mem_free(au->au_shcred.oa_base,
    259 			    au->au_shcred.oa_length);
    260 			au->au_shcred.oa_base = NULL;
    261 		}
    262 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
    263 			auth->ah_cred = au->au_shcred;
    264 		} else {
    265 			xdrs.x_op = XDR_FREE;
    266 			(void)xdr_opaque_auth(&xdrs, &au->au_shcred);
    267 			au->au_shcred.oa_base = NULL;
    268 			auth->ah_cred = au->au_origcred;
    269 		}
    270 		marshal_new_auth(auth);
    271 	}
    272 	return (TRUE);
    273 }
    274 
    275 static bool_t
    276 authunix_refresh(auth)
    277 	AUTH *auth;
    278 {
    279 	struct audata *au = AUTH_PRIVATE(auth);
    280 	struct authunix_parms aup;
    281 	struct timeval now;
    282 	XDR xdrs;
    283 	int stat;
    284 
    285 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
    286 		/* there is no hope.  Punt */
    287 		return (FALSE);
    288 	}
    289 	au->au_shfaults ++;
    290 
    291 	/* first deserialize the creds back into a struct authunix_parms */
    292 	aup.aup_machname = NULL;
    293 	aup.aup_gids = (int *)NULL;
    294 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
    295 	    au->au_origcred.oa_length, XDR_DECODE);
    296 	stat = xdr_authunix_parms(&xdrs, &aup);
    297 	if (! stat)
    298 		goto done;
    299 
    300 	/* update the time and serialize in place */
    301 	(void)gettimeofday(&now, (struct timezone *)0);
    302 	aup.aup_time = now.tv_sec;
    303 	xdrs.x_op = XDR_ENCODE;
    304 	XDR_SETPOS(&xdrs, 0);
    305 	stat = xdr_authunix_parms(&xdrs, &aup);
    306 	if (! stat)
    307 		goto done;
    308 	auth->ah_cred = au->au_origcred;
    309 	marshal_new_auth(auth);
    310 done:
    311 	/* free the struct authunix_parms created by deserializing */
    312 	xdrs.x_op = XDR_FREE;
    313 	(void)xdr_authunix_parms(&xdrs, &aup);
    314 	XDR_DESTROY(&xdrs);
    315 	return (stat);
    316 }
    317 
    318 static void
    319 authunix_destroy(auth)
    320 	AUTH *auth;
    321 {
    322 	struct audata *au = AUTH_PRIVATE(auth);
    323 
    324 	mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
    325 
    326 	if (au->au_shcred.oa_base != NULL)
    327 		mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
    328 
    329 	mem_free(auth->ah_private, sizeof(struct audata));
    330 
    331 	if (auth->ah_verf.oa_base != NULL)
    332 		mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
    333 
    334 	mem_free(auth, sizeof(*auth));
    335 }
    336 
    337 /*
    338  * Marshals (pre-serializes) an auth struct.
    339  * sets private data, au_marshed and au_mpos
    340  */
    341 static void
    342 marshal_new_auth(auth)
    343 	AUTH *auth;
    344 {
    345 	XDR	xdr_stream;
    346 	XDR	*xdrs = &xdr_stream;
    347 	struct audata *au = AUTH_PRIVATE(auth);
    348 
    349 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
    350 	if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
    351 	    (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
    352 		warnx("auth_none.c - Fatal marshalling problem");
    353 	else
    354 		au->au_mpos = XDR_GETPOS(xdrs);
    355 	XDR_DESTROY(xdrs);
    356 }
    357