18360efbdSAlfred Perlstein /* $NetBSD: auth_unix.c,v 1.18 2000/07/06 03:03:30 christos Exp $ */
28360efbdSAlfred Perlstein
32e322d37SHiroki Sato /*-
4*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
5*8a16b7a1SPedro F. Giffuni *
62e322d37SHiroki Sato * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato * All rights reserved.
899064799SGarrett Wollman *
92e322d37SHiroki Sato * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato * this list of conditions and the following disclaimer.
132e322d37SHiroki Sato * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato * this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato * and/or other materials provided with the distribution.
162e322d37SHiroki Sato * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato * contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato * from this software without specific prior written permission.
1999064799SGarrett Wollman *
202e322d37SHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato * POSSIBILITY OF SUCH DAMAGE.
3199064799SGarrett Wollman */
3299064799SGarrett Wollman
3399064799SGarrett Wollman /*
3499064799SGarrett Wollman * auth_unix.c, Implements UNIX style authentication parameters.
3599064799SGarrett Wollman *
3699064799SGarrett Wollman * Copyright (C) 1984, Sun Microsystems, Inc.
3799064799SGarrett Wollman *
3899064799SGarrett Wollman * The system is very weak. The client uses no encryption for it's
3999064799SGarrett Wollman * credentials and only sends null verifiers. The server sends backs
4099064799SGarrett Wollman * null verifiers or optionally a verifier that suggests a new short hand
4199064799SGarrett Wollman * for the credentials.
4299064799SGarrett Wollman *
4399064799SGarrett Wollman */
4499064799SGarrett Wollman
458360efbdSAlfred Perlstein #include "namespace.h"
469f5afc13SIan Dowse #include "reentrant.h"
478360efbdSAlfred Perlstein #include <sys/param.h>
488360efbdSAlfred Perlstein
498360efbdSAlfred Perlstein #include <assert.h>
508360efbdSAlfred Perlstein #include <err.h>
5199064799SGarrett Wollman #include <stdio.h>
5299064799SGarrett Wollman #include <stdlib.h>
534c3af266SPoul-Henning Kamp #include <unistd.h>
544c3af266SPoul-Henning Kamp #include <string.h>
5599064799SGarrett Wollman
5699064799SGarrett Wollman #include <rpc/types.h>
5799064799SGarrett Wollman #include <rpc/xdr.h>
5899064799SGarrett Wollman #include <rpc/auth.h>
5999064799SGarrett Wollman #include <rpc/auth_unix.h>
608360efbdSAlfred Perlstein #include "un-namespace.h"
61235baf26SDaniel Eischen #include "mt_misc.h"
6299064799SGarrett Wollman
638360efbdSAlfred Perlstein /* auth_unix.c */
648360efbdSAlfred Perlstein static void authunix_nextverf (AUTH *);
658360efbdSAlfred Perlstein static bool_t authunix_marshal (AUTH *, XDR *);
668360efbdSAlfred Perlstein static bool_t authunix_validate (AUTH *, struct opaque_auth *);
678360efbdSAlfred Perlstein static bool_t authunix_refresh (AUTH *, void *);
688360efbdSAlfred Perlstein static void authunix_destroy (AUTH *);
698360efbdSAlfred Perlstein static void marshal_new_auth (AUTH *);
708360efbdSAlfred Perlstein static struct auth_ops *authunix_ops (void);
7199064799SGarrett Wollman
7299064799SGarrett Wollman /*
7399064799SGarrett Wollman * This struct is pointed to by the ah_private field of an auth_handle.
7499064799SGarrett Wollman */
7599064799SGarrett Wollman struct audata {
7699064799SGarrett Wollman struct opaque_auth au_origcred; /* original credentials */
7799064799SGarrett Wollman struct opaque_auth au_shcred; /* short hand cred */
7899064799SGarrett Wollman u_long au_shfaults; /* short hand cache faults */
7999064799SGarrett Wollman char au_marshed[MAX_AUTH_BYTES];
8099064799SGarrett Wollman u_int au_mpos; /* xdr pos at end of marshed */
8199064799SGarrett Wollman };
8299064799SGarrett Wollman #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
8399064799SGarrett Wollman
8499064799SGarrett Wollman /*
8599064799SGarrett Wollman * Create a unix style authenticator.
8699064799SGarrett Wollman * Returns an auth handle with the given stuff in it.
8799064799SGarrett Wollman */
8899064799SGarrett Wollman AUTH *
authunix_create(char * machname,u_int uid,u_int gid,int len,u_int * aup_gids)89587cf682SCraig Rodrigues authunix_create(char *machname, u_int uid, u_int gid, int len, u_int *aup_gids)
9099064799SGarrett Wollman {
9199064799SGarrett Wollman struct authunix_parms aup;
9299064799SGarrett Wollman char mymem[MAX_AUTH_BYTES];
9399064799SGarrett Wollman struct timeval now;
9499064799SGarrett Wollman XDR xdrs;
958360efbdSAlfred Perlstein AUTH *auth;
968360efbdSAlfred Perlstein struct audata *au;
9799064799SGarrett Wollman
9899064799SGarrett Wollman /*
9999064799SGarrett Wollman * Allocate and set up auth handle
10099064799SGarrett Wollman */
1018360efbdSAlfred Perlstein au = NULL;
1028360efbdSAlfred Perlstein auth = mem_alloc(sizeof(*auth));
103c4473420SPeter Wemm #ifndef _KERNEL
10499064799SGarrett Wollman if (auth == NULL) {
1058360efbdSAlfred Perlstein warnx("authunix_create: out of memory");
1068360efbdSAlfred Perlstein goto cleanup_authunix_create;
10799064799SGarrett Wollman }
10899064799SGarrett Wollman #endif
1098360efbdSAlfred Perlstein au = mem_alloc(sizeof(*au));
110c4473420SPeter Wemm #ifndef _KERNEL
11199064799SGarrett Wollman if (au == NULL) {
1128360efbdSAlfred Perlstein warnx("authunix_create: out of memory");
1138360efbdSAlfred Perlstein goto cleanup_authunix_create;
11499064799SGarrett Wollman }
11599064799SGarrett Wollman #endif
1168360efbdSAlfred Perlstein auth->ah_ops = authunix_ops();
11799064799SGarrett Wollman auth->ah_private = (caddr_t)au;
11899064799SGarrett Wollman auth->ah_verf = au->au_shcred = _null_auth;
11999064799SGarrett Wollman au->au_shfaults = 0;
1208360efbdSAlfred Perlstein au->au_origcred.oa_base = NULL;
12199064799SGarrett Wollman
12299064799SGarrett Wollman /*
12399064799SGarrett Wollman * fill in param struct from the given params
12499064799SGarrett Wollman */
1258360efbdSAlfred Perlstein (void)gettimeofday(&now, NULL);
12699064799SGarrett Wollman aup.aup_time = now.tv_sec;
12799064799SGarrett Wollman aup.aup_machname = machname;
12899064799SGarrett Wollman aup.aup_uid = uid;
12999064799SGarrett Wollman aup.aup_gid = gid;
13099064799SGarrett Wollman aup.aup_len = (u_int)len;
13199064799SGarrett Wollman aup.aup_gids = aup_gids;
13299064799SGarrett Wollman
13399064799SGarrett Wollman /*
13499064799SGarrett Wollman * Serialize the parameters into origcred
13599064799SGarrett Wollman */
13699064799SGarrett Wollman xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
13799064799SGarrett Wollman if (! xdr_authunix_parms(&xdrs, &aup))
13899064799SGarrett Wollman abort();
13999064799SGarrett Wollman au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
14099064799SGarrett Wollman au->au_origcred.oa_flavor = AUTH_UNIX;
141c4473420SPeter Wemm #ifdef _KERNEL
14299064799SGarrett Wollman au->au_origcred.oa_base = mem_alloc((u_int) len);
14399064799SGarrett Wollman #else
14499064799SGarrett Wollman if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
1458360efbdSAlfred Perlstein warnx("authunix_create: out of memory");
1468360efbdSAlfred Perlstein goto cleanup_authunix_create;
14799064799SGarrett Wollman }
14899064799SGarrett Wollman #endif
1498360efbdSAlfred Perlstein memmove(au->au_origcred.oa_base, mymem, (size_t)len);
15099064799SGarrett Wollman
15199064799SGarrett Wollman /*
15299064799SGarrett Wollman * set auth handle to reflect new cred.
15399064799SGarrett Wollman */
15499064799SGarrett Wollman auth->ah_cred = au->au_origcred;
15599064799SGarrett Wollman marshal_new_auth(auth);
15699064799SGarrett Wollman return (auth);
1578360efbdSAlfred Perlstein #ifndef _KERNEL
1588360efbdSAlfred Perlstein cleanup_authunix_create:
1598360efbdSAlfred Perlstein if (auth)
1608360efbdSAlfred Perlstein mem_free(auth, sizeof(*auth));
1618360efbdSAlfred Perlstein if (au) {
1628360efbdSAlfred Perlstein if (au->au_origcred.oa_base)
1638360efbdSAlfred Perlstein mem_free(au->au_origcred.oa_base, (u_int)len);
1648360efbdSAlfred Perlstein mem_free(au, sizeof(*au));
1658360efbdSAlfred Perlstein }
1668360efbdSAlfred Perlstein return (NULL);
1678360efbdSAlfred Perlstein #endif
16899064799SGarrett Wollman }
16999064799SGarrett Wollman
17099064799SGarrett Wollman /*
17199064799SGarrett Wollman * Returns an auth handle with parameters determined by doing lots of
17299064799SGarrett Wollman * syscalls.
17399064799SGarrett Wollman */
17499064799SGarrett Wollman AUTH *
authunix_create_default(void)175587cf682SCraig Rodrigues authunix_create_default(void)
17699064799SGarrett Wollman {
1779a36eaa3SKonstantin Belousov AUTH *auth;
17854404cfbSBrooks Davis int ngids;
17954404cfbSBrooks Davis long ngids_max;
1808360efbdSAlfred Perlstein char machname[MAXHOSTNAMELEN + 1];
1818360efbdSAlfred Perlstein uid_t uid;
1828360efbdSAlfred Perlstein gid_t gid;
18354404cfbSBrooks Davis gid_t *gids;
18454404cfbSBrooks Davis
18554404cfbSBrooks Davis ngids_max = sysconf(_SC_NGROUPS_MAX) + 1;
18654404cfbSBrooks Davis gids = malloc(sizeof(gid_t) * ngids_max);
18754404cfbSBrooks Davis if (gids == NULL)
18854404cfbSBrooks Davis return (NULL);
18999064799SGarrett Wollman
1908360efbdSAlfred Perlstein if (gethostname(machname, sizeof machname) == -1)
19199064799SGarrett Wollman abort();
1928360efbdSAlfred Perlstein machname[sizeof(machname) - 1] = 0;
1938360efbdSAlfred Perlstein uid = geteuid();
1948360efbdSAlfred Perlstein gid = getegid();
19554404cfbSBrooks Davis if ((ngids = getgroups(ngids_max, gids)) < 0)
19699064799SGarrett Wollman abort();
19754404cfbSBrooks Davis if (ngids > NGRPS)
19854404cfbSBrooks Davis ngids = NGRPS;
1990d1040e5SPedro F. Giffuni /* XXX: interface problem; we should translate from uid_t and gid_t */
200f3c3ef7bSPedro F. Giffuni auth = authunix_create(machname, uid, gid, ngids, gids);
2019a36eaa3SKonstantin Belousov free(gids);
2029a36eaa3SKonstantin Belousov return (auth);
20399064799SGarrett Wollman }
20499064799SGarrett Wollman
20599064799SGarrett Wollman /*
20699064799SGarrett Wollman * authunix operations
20799064799SGarrett Wollman */
20899064799SGarrett Wollman
2098360efbdSAlfred Perlstein /* ARGSUSED */
21099064799SGarrett Wollman static void
authunix_nextverf(AUTH * auth)211587cf682SCraig Rodrigues authunix_nextverf(AUTH *auth)
21299064799SGarrett Wollman {
21399064799SGarrett Wollman /* no action necessary */
21499064799SGarrett Wollman }
21599064799SGarrett Wollman
21699064799SGarrett Wollman static bool_t
authunix_marshal(AUTH * auth,XDR * xdrs)217587cf682SCraig Rodrigues authunix_marshal(AUTH *auth, XDR *xdrs)
21899064799SGarrett Wollman {
2198360efbdSAlfred Perlstein struct audata *au;
22099064799SGarrett Wollman
2218360efbdSAlfred Perlstein assert(auth != NULL);
2228360efbdSAlfred Perlstein assert(xdrs != NULL);
2238360efbdSAlfred Perlstein
2248360efbdSAlfred Perlstein au = AUTH_PRIVATE(auth);
22599064799SGarrett Wollman return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
22699064799SGarrett Wollman }
22799064799SGarrett Wollman
22899064799SGarrett Wollman static bool_t
authunix_validate(AUTH * auth,struct opaque_auth * verf)229587cf682SCraig Rodrigues authunix_validate(AUTH *auth, struct opaque_auth *verf)
23099064799SGarrett Wollman {
2318360efbdSAlfred Perlstein struct audata *au;
23299064799SGarrett Wollman XDR xdrs;
23399064799SGarrett Wollman
2348360efbdSAlfred Perlstein assert(auth != NULL);
2358360efbdSAlfred Perlstein assert(verf != NULL);
2368360efbdSAlfred Perlstein
2378360efbdSAlfred Perlstein if (verf->oa_flavor == AUTH_SHORT) {
23899064799SGarrett Wollman au = AUTH_PRIVATE(auth);
2398360efbdSAlfred Perlstein xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
2408360efbdSAlfred Perlstein XDR_DECODE);
24199064799SGarrett Wollman
24299064799SGarrett Wollman if (au->au_shcred.oa_base != NULL) {
24399064799SGarrett Wollman mem_free(au->au_shcred.oa_base,
24499064799SGarrett Wollman au->au_shcred.oa_length);
24599064799SGarrett Wollman au->au_shcred.oa_base = NULL;
24699064799SGarrett Wollman }
24799064799SGarrett Wollman if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
24899064799SGarrett Wollman auth->ah_cred = au->au_shcred;
24999064799SGarrett Wollman } else {
25099064799SGarrett Wollman xdrs.x_op = XDR_FREE;
25199064799SGarrett Wollman (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
25299064799SGarrett Wollman au->au_shcred.oa_base = NULL;
25399064799SGarrett Wollman auth->ah_cred = au->au_origcred;
25499064799SGarrett Wollman }
25599064799SGarrett Wollman marshal_new_auth(auth);
25699064799SGarrett Wollman }
25799064799SGarrett Wollman return (TRUE);
25899064799SGarrett Wollman }
25999064799SGarrett Wollman
26099064799SGarrett Wollman static bool_t
authunix_refresh(AUTH * auth,void * dummy)2618360efbdSAlfred Perlstein authunix_refresh(AUTH *auth, void *dummy)
26299064799SGarrett Wollman {
2638360efbdSAlfred Perlstein struct audata *au = AUTH_PRIVATE(auth);
26499064799SGarrett Wollman struct authunix_parms aup;
26599064799SGarrett Wollman struct timeval now;
26699064799SGarrett Wollman XDR xdrs;
2678360efbdSAlfred Perlstein int stat;
2688360efbdSAlfred Perlstein
2698360efbdSAlfred Perlstein assert(auth != NULL);
27099064799SGarrett Wollman
27199064799SGarrett Wollman if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
27299064799SGarrett Wollman /* there is no hope. Punt */
27399064799SGarrett Wollman return (FALSE);
27499064799SGarrett Wollman }
27599064799SGarrett Wollman au->au_shfaults ++;
27699064799SGarrett Wollman
27799064799SGarrett Wollman /* first deserialize the creds back into a struct authunix_parms */
27899064799SGarrett Wollman aup.aup_machname = NULL;
2798360efbdSAlfred Perlstein aup.aup_gids = NULL;
28099064799SGarrett Wollman xdrmem_create(&xdrs, au->au_origcred.oa_base,
28199064799SGarrett Wollman au->au_origcred.oa_length, XDR_DECODE);
28299064799SGarrett Wollman stat = xdr_authunix_parms(&xdrs, &aup);
28399064799SGarrett Wollman if (! stat)
28499064799SGarrett Wollman goto done;
28599064799SGarrett Wollman
28699064799SGarrett Wollman /* update the time and serialize in place */
2878360efbdSAlfred Perlstein (void)gettimeofday(&now, NULL);
28899064799SGarrett Wollman aup.aup_time = now.tv_sec;
28999064799SGarrett Wollman xdrs.x_op = XDR_ENCODE;
29099064799SGarrett Wollman XDR_SETPOS(&xdrs, 0);
29199064799SGarrett Wollman stat = xdr_authunix_parms(&xdrs, &aup);
29299064799SGarrett Wollman if (! stat)
29399064799SGarrett Wollman goto done;
29499064799SGarrett Wollman auth->ah_cred = au->au_origcred;
29599064799SGarrett Wollman marshal_new_auth(auth);
29699064799SGarrett Wollman done:
29799064799SGarrett Wollman /* free the struct authunix_parms created by deserializing */
29899064799SGarrett Wollman xdrs.x_op = XDR_FREE;
29999064799SGarrett Wollman (void)xdr_authunix_parms(&xdrs, &aup);
30099064799SGarrett Wollman XDR_DESTROY(&xdrs);
30199064799SGarrett Wollman return (stat);
30299064799SGarrett Wollman }
30399064799SGarrett Wollman
30499064799SGarrett Wollman static void
authunix_destroy(AUTH * auth)305587cf682SCraig Rodrigues authunix_destroy(AUTH *auth)
30699064799SGarrett Wollman {
3078360efbdSAlfred Perlstein struct audata *au;
30899064799SGarrett Wollman
3098360efbdSAlfred Perlstein assert(auth != NULL);
3108360efbdSAlfred Perlstein
3118360efbdSAlfred Perlstein au = AUTH_PRIVATE(auth);
31299064799SGarrett Wollman mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
31399064799SGarrett Wollman
31499064799SGarrett Wollman if (au->au_shcred.oa_base != NULL)
31599064799SGarrett Wollman mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
31699064799SGarrett Wollman
31799064799SGarrett Wollman mem_free(auth->ah_private, sizeof(struct audata));
31899064799SGarrett Wollman
31999064799SGarrett Wollman if (auth->ah_verf.oa_base != NULL)
32099064799SGarrett Wollman mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
32199064799SGarrett Wollman
3228360efbdSAlfred Perlstein mem_free(auth, sizeof(*auth));
32399064799SGarrett Wollman }
32499064799SGarrett Wollman
32599064799SGarrett Wollman /*
32699064799SGarrett Wollman * Marshals (pre-serializes) an auth struct.
32799064799SGarrett Wollman * sets private data, au_marshed and au_mpos
32899064799SGarrett Wollman */
3294c3af266SPoul-Henning Kamp static void
marshal_new_auth(AUTH * auth)330587cf682SCraig Rodrigues marshal_new_auth(AUTH *auth)
33199064799SGarrett Wollman {
33299064799SGarrett Wollman XDR xdr_stream;
3338360efbdSAlfred Perlstein XDR *xdrs = &xdr_stream;
3348360efbdSAlfred Perlstein struct audata *au;
33599064799SGarrett Wollman
3368360efbdSAlfred Perlstein assert(auth != NULL);
3378360efbdSAlfred Perlstein
3388360efbdSAlfred Perlstein au = AUTH_PRIVATE(auth);
33999064799SGarrett Wollman xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
34099064799SGarrett Wollman if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
3418360efbdSAlfred Perlstein (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
3428360efbdSAlfred Perlstein warnx("auth_none.c - Fatal marshalling problem");
3438360efbdSAlfred Perlstein else
34499064799SGarrett Wollman au->au_mpos = XDR_GETPOS(xdrs);
34599064799SGarrett Wollman XDR_DESTROY(xdrs);
34699064799SGarrett Wollman }
3478360efbdSAlfred Perlstein
3488360efbdSAlfred Perlstein static struct auth_ops *
authunix_ops(void)349587cf682SCraig Rodrigues authunix_ops(void)
3508360efbdSAlfred Perlstein {
3518360efbdSAlfred Perlstein static struct auth_ops ops;
3528360efbdSAlfred Perlstein
3538360efbdSAlfred Perlstein /* VARIABLES PROTECTED BY ops_lock: ops */
3548360efbdSAlfred Perlstein
3558360efbdSAlfred Perlstein mutex_lock(&ops_lock);
3568360efbdSAlfred Perlstein if (ops.ah_nextverf == NULL) {
3578360efbdSAlfred Perlstein ops.ah_nextverf = authunix_nextverf;
3588360efbdSAlfred Perlstein ops.ah_marshal = authunix_marshal;
3598360efbdSAlfred Perlstein ops.ah_validate = authunix_validate;
3608360efbdSAlfred Perlstein ops.ah_refresh = authunix_refresh;
3618360efbdSAlfred Perlstein ops.ah_destroy = authunix_destroy;
3628360efbdSAlfred Perlstein }
3638360efbdSAlfred Perlstein mutex_unlock(&ops_lock);
3648360efbdSAlfred Perlstein return (&ops);
3658360efbdSAlfred Perlstein }
366