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