1*85f5e301Schristos /* $NetBSD: auth_unix.c,v 1.28 2024/01/23 17:24:38 christos Exp $ */
29e15c989Scgd
363d7b677Scgd /*
447c0e0c3Stron * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd *
647c0e0c3Stron * Redistribution and use in source and binary forms, with or without
747c0e0c3Stron * modification, are permitted provided that the following conditions are
847c0e0c3Stron * met:
963d7b677Scgd *
1047c0e0c3Stron * * Redistributions of source code must retain the above copyright
1147c0e0c3Stron * notice, this list of conditions and the following disclaimer.
1247c0e0c3Stron * * Redistributions in binary form must reproduce the above
1347c0e0c3Stron * copyright notice, this list of conditions and the following
1447c0e0c3Stron * disclaimer in the documentation and/or other materials
1547c0e0c3Stron * provided with the distribution.
1647c0e0c3Stron * * Neither the name of the "Oracle America, Inc." nor the names of its
1747c0e0c3Stron * contributors may be used to endorse or promote products derived
1847c0e0c3Stron * from this software without specific prior written permission.
1963d7b677Scgd *
2047c0e0c3Stron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2147c0e0c3Stron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2247c0e0c3Stron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2347c0e0c3Stron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2447c0e0c3Stron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2547c0e0c3Stron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2647c0e0c3Stron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2747c0e0c3Stron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2847c0e0c3Stron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2947c0e0c3Stron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3047c0e0c3Stron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3147c0e0c3Stron * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3263d7b677Scgd */
3363d7b677Scgd
341f542a9aSchristos #include <sys/cdefs.h>
3563d7b677Scgd #if defined(LIBC_SCCS) && !defined(lint)
361f542a9aSchristos #if 0
371f542a9aSchristos static char *sccsid = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
381f542a9aSchristos static char *sccsid = "@(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC";
391f542a9aSchristos #else
40*85f5e301Schristos __RCSID("$NetBSD: auth_unix.c,v 1.28 2024/01/23 17:24:38 christos Exp $");
411f542a9aSchristos #endif
4263d7b677Scgd #endif
4363d7b677Scgd
4463d7b677Scgd /*
4563d7b677Scgd * auth_unix.c, Implements UNIX style authentication parameters.
4663d7b677Scgd *
4763d7b677Scgd * Copyright (C) 1984, Sun Microsystems, Inc.
4863d7b677Scgd *
49f0a7346dSsnj * The system is very weak. The client uses no encryption for its
5063d7b677Scgd * credentials and only sends null verifiers. The server sends backs
5163d7b677Scgd * null verifiers or optionally a verifier that suggests a new short hand
5263d7b677Scgd * for the credentials.
5363d7b677Scgd *
5463d7b677Scgd */
5563d7b677Scgd
5643fa6fe3Sjtc #include "namespace.h"
577df0ccbaSfvdl #include "reentrant.h"
5832f51971Smrg #include <sys/param.h>
5932f51971Smrg
60b48252f3Slukem #include <assert.h>
6146e6c5e8Slukem #include <err.h>
6263d7b677Scgd #include <stdio.h>
6363d7b677Scgd #include <stdlib.h>
64d3b76936Scgd #include <string.h>
65d3b76936Scgd #include <unistd.h>
6663d7b677Scgd
6763d7b677Scgd #include <rpc/types.h>
6863d7b677Scgd #include <rpc/xdr.h>
69*85f5e301Schristos #include <rpc/rpc.h>
7063d7b677Scgd #include <rpc/auth.h>
7163d7b677Scgd #include <rpc/auth_unix.h>
7263d7b677Scgd
73*85f5e301Schristos #include "rpc_internal.h"
74*85f5e301Schristos
7543fa6fe3Sjtc #ifdef __weak_alias
7660549036Smycroft __weak_alias(authunix_create,_authunix_create)
7760549036Smycroft __weak_alias(authunix_create_default,_authunix_create_default)
7843fa6fe3Sjtc #endif
7943fa6fe3Sjtc
801f542a9aSchristos
811f542a9aSchristos /* auth_unix.c */
82adb74221Smatt static void authunix_nextverf(AUTH *);
83adb74221Smatt static bool_t authunix_marshal(AUTH *, XDR *);
84adb74221Smatt static bool_t authunix_validate(AUTH *, struct opaque_auth *);
85adb74221Smatt static bool_t authunix_refresh(AUTH *);
86adb74221Smatt static void authunix_destroy(AUTH *);
87adb74221Smatt static void marshal_new_auth(AUTH *);
88adb74221Smatt static const struct auth_ops *authunix_ops(void);
8963d7b677Scgd
9063d7b677Scgd /*
9163d7b677Scgd * This struct is pointed to by the ah_private field of an auth_handle.
9263d7b677Scgd */
9363d7b677Scgd struct audata {
9463d7b677Scgd struct opaque_auth au_origcred; /* original credentials */
9563d7b677Scgd struct opaque_auth au_shcred; /* short hand cred */
96ce147c1cSlukem u_long au_shfaults; /* short hand cache faults */
9763d7b677Scgd char au_marshed[MAX_AUTH_BYTES];
98ce147c1cSlukem u_int au_mpos; /* xdr pos at end of marshed */
9963d7b677Scgd };
10063d7b677Scgd #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
10163d7b677Scgd
10263d7b677Scgd /*
10363d7b677Scgd * Create a unix style authenticator.
10463d7b677Scgd * Returns an auth handle with the given stuff in it.
10563d7b677Scgd */
10663d7b677Scgd AUTH *
authunix_create(char * machname,int uid,int gid,int len,int * aup_gids)107adb74221Smatt authunix_create(char *machname, int uid, int gid, int len, int *aup_gids)
10863d7b677Scgd {
10963d7b677Scgd struct authunix_parms aup;
11063d7b677Scgd char mymem[MAX_AUTH_BYTES];
11163d7b677Scgd struct timeval now;
11263d7b677Scgd XDR xdrs;
11346e6c5e8Slukem AUTH *auth;
11446e6c5e8Slukem struct audata *au;
11563d7b677Scgd
11663d7b677Scgd /*
11763d7b677Scgd * Allocate and set up auth handle
11863d7b677Scgd */
1196c13a3b8Slukem au = NULL;
120ea5394abSchristos auth = mem_alloc(sizeof(*auth));
12163d7b677Scgd #ifndef KERNEL
12263d7b677Scgd if (auth == NULL) {
123e24729deSchristos warn("%s: out of memory", __func__);
1246c13a3b8Slukem goto cleanup_authunix_create;
12563d7b677Scgd }
12663d7b677Scgd #endif
127ea5394abSchristos au = mem_alloc(sizeof(*au));
12863d7b677Scgd #ifndef KERNEL
12963d7b677Scgd if (au == NULL) {
130e24729deSchristos warn("%s: out of memory", __func__);
1316c13a3b8Slukem goto cleanup_authunix_create;
13263d7b677Scgd }
13363d7b677Scgd #endif
1347df0ccbaSfvdl auth->ah_ops = authunix_ops();
135ae3564f2Schristos auth->ah_private = au;
13663d7b677Scgd auth->ah_verf = au->au_shcred = _null_auth;
13763d7b677Scgd au->au_shfaults = 0;
1386c13a3b8Slukem au->au_origcred.oa_base = NULL;
13963d7b677Scgd
14063d7b677Scgd /*
14163d7b677Scgd * fill in param struct from the given params
14263d7b677Scgd */
143ea5394abSchristos (void)gettimeofday(&now, NULL);
144461a86f9Schristos aup.aup_time = (u_long)now.tv_sec; /* XXX: truncate on 32 bit */
145ce147c1cSlukem aup.aup_machname = machname;
14663d7b677Scgd aup.aup_uid = uid;
14763d7b677Scgd aup.aup_gid = gid;
148ce147c1cSlukem aup.aup_len = (u_int)len;
149ce147c1cSlukem aup.aup_gids = aup_gids;
15063d7b677Scgd
15163d7b677Scgd /*
15263d7b677Scgd * Serialize the parameters into origcred
15363d7b677Scgd */
15463d7b677Scgd xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
15563d7b677Scgd if (! xdr_authunix_parms(&xdrs, &aup))
15663d7b677Scgd abort();
157ce147c1cSlukem au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
15863d7b677Scgd au->au_origcred.oa_flavor = AUTH_UNIX;
15963d7b677Scgd #ifdef KERNEL
160c9cdc302Schristos au->au_origcred.oa_base = mem_alloc((size_t)len);
16163d7b677Scgd #else
162c9cdc302Schristos if ((au->au_origcred.oa_base = mem_alloc((size_t)len)) == NULL) {
163e24729deSchristos warn("%s: out of memory", __func__);
1646c13a3b8Slukem goto cleanup_authunix_create;
16563d7b677Scgd }
16663d7b677Scgd #endif
16746e6c5e8Slukem memmove(au->au_origcred.oa_base, mymem, (size_t)len);
16863d7b677Scgd
16963d7b677Scgd /*
17063d7b677Scgd * set auth handle to reflect new cred.
17163d7b677Scgd */
17263d7b677Scgd auth->ah_cred = au->au_origcred;
17363d7b677Scgd marshal_new_auth(auth);
17463d7b677Scgd return (auth);
1756c13a3b8Slukem #ifndef KERNEL
1766c13a3b8Slukem cleanup_authunix_create:
1776c13a3b8Slukem if (auth)
1786c13a3b8Slukem mem_free(auth, sizeof(*auth));
1796c13a3b8Slukem if (au) {
1806c13a3b8Slukem if (au->au_origcred.oa_base)
1816c13a3b8Slukem mem_free(au->au_origcred.oa_base, (u_int)len);
1826c13a3b8Slukem mem_free(au, sizeof(*au));
1836c13a3b8Slukem }
1846c13a3b8Slukem return (NULL);
1856c13a3b8Slukem #endif
18663d7b677Scgd }
18763d7b677Scgd
18863d7b677Scgd /*
189fb8fd3efSchristos * Some servers will refuse mounts if the group list is larger
190fb8fd3efSchristos * than it expects (like 8). This allows the application to set
191fb8fd3efSchristos * the maximum size of the group list that will be sent.
192fb8fd3efSchristos */
193fb8fd3efSchristos static int maxgrplist = NGROUPS;
194fb8fd3efSchristos
195fb8fd3efSchristos void
set_rpc_maxgrouplist(int num)196fb8fd3efSchristos set_rpc_maxgrouplist(int num)
197fb8fd3efSchristos {
198fb8fd3efSchristos if (num < NGROUPS)
199fb8fd3efSchristos maxgrplist = num;
200fb8fd3efSchristos }
201fb8fd3efSchristos
202fb8fd3efSchristos /*
20363d7b677Scgd * Returns an auth handle with parameters determined by doing lots of
20463d7b677Scgd * syscalls.
20563d7b677Scgd */
20663d7b677Scgd AUTH *
authunix_create_default(void)207adb74221Smatt authunix_create_default(void)
20863d7b677Scgd {
20946e6c5e8Slukem int len;
21032f51971Smrg char machname[MAXHOSTNAMELEN + 1];
211ae3564f2Schristos uid_t uid;
212ae3564f2Schristos gid_t gid;
213ae3564f2Schristos gid_t gids[NGRPS];
21463d7b677Scgd
21532f51971Smrg if (gethostname(machname, sizeof machname) == -1)
21663d7b677Scgd abort();
21732f51971Smrg machname[sizeof(machname) - 1] = 0;
21863d7b677Scgd uid = geteuid();
21963d7b677Scgd gid = getegid();
22063d7b677Scgd if ((len = getgroups(NGRPS, gids)) < 0)
22163d7b677Scgd abort();
222fb8fd3efSchristos if (len > maxgrplist)
223fb8fd3efSchristos len = maxgrplist;
224ae3564f2Schristos /* XXX: interface problem; those should all have been unsigned */
225ae3564f2Schristos return (authunix_create(machname, (int)uid, (int)gid, len,
226ae3564f2Schristos (int *)gids));
22763d7b677Scgd }
22863d7b677Scgd
22963d7b677Scgd /*
23063d7b677Scgd * authunix operations
23163d7b677Scgd */
23263d7b677Scgd
233ae3564f2Schristos /* ARGSUSED */
23463d7b677Scgd static void
authunix_nextverf(AUTH * auth)235adb74221Smatt authunix_nextverf(AUTH *auth)
23663d7b677Scgd {
23763d7b677Scgd /* no action necessary */
23863d7b677Scgd }
23963d7b677Scgd
24063d7b677Scgd static bool_t
authunix_marshal(AUTH * auth,XDR * xdrs)241adb74221Smatt authunix_marshal(AUTH *auth, XDR *xdrs)
24263d7b677Scgd {
243b48252f3Slukem struct audata *au;
24463d7b677Scgd
245b48252f3Slukem _DIAGASSERT(auth != NULL);
246b48252f3Slukem _DIAGASSERT(xdrs != NULL);
247b48252f3Slukem
248b48252f3Slukem au = AUTH_PRIVATE(auth);
24963d7b677Scgd return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
25063d7b677Scgd }
25163d7b677Scgd
25263d7b677Scgd static bool_t
authunix_validate(AUTH * auth,struct opaque_auth * verf)253adb74221Smatt authunix_validate(AUTH *auth, struct opaque_auth *verf)
25463d7b677Scgd {
25546e6c5e8Slukem struct audata *au;
25663d7b677Scgd XDR xdrs;
25763d7b677Scgd
258b48252f3Slukem _DIAGASSERT(auth != NULL);
259b48252f3Slukem _DIAGASSERT(verf != NULL);
260b48252f3Slukem
2611f542a9aSchristos if (verf->oa_flavor == AUTH_SHORT) {
26263d7b677Scgd au = AUTH_PRIVATE(auth);
2636c13a3b8Slukem xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
2646c13a3b8Slukem XDR_DECODE);
26563d7b677Scgd
26663d7b677Scgd if (au->au_shcred.oa_base != NULL) {
26763d7b677Scgd mem_free(au->au_shcred.oa_base,
26863d7b677Scgd au->au_shcred.oa_length);
26963d7b677Scgd au->au_shcred.oa_base = NULL;
27063d7b677Scgd }
27163d7b677Scgd if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
27263d7b677Scgd auth->ah_cred = au->au_shcred;
27363d7b677Scgd } else {
27463d7b677Scgd xdrs.x_op = XDR_FREE;
27563d7b677Scgd (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
27663d7b677Scgd au->au_shcred.oa_base = NULL;
27763d7b677Scgd auth->ah_cred = au->au_origcred;
27863d7b677Scgd }
27963d7b677Scgd marshal_new_auth(auth);
28063d7b677Scgd }
28163d7b677Scgd return (TRUE);
28263d7b677Scgd }
28363d7b677Scgd
28463d7b677Scgd static bool_t
authunix_refresh(AUTH * auth)285adb74221Smatt authunix_refresh(AUTH *auth)
28663d7b677Scgd {
28746e6c5e8Slukem struct audata *au = AUTH_PRIVATE(auth);
28863d7b677Scgd struct authunix_parms aup;
28963d7b677Scgd struct timeval now;
29063d7b677Scgd XDR xdrs;
29146e6c5e8Slukem int stat;
29263d7b677Scgd
293b48252f3Slukem _DIAGASSERT(auth != NULL);
294b48252f3Slukem
29563d7b677Scgd if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
29663d7b677Scgd /* there is no hope. Punt */
29763d7b677Scgd return (FALSE);
29863d7b677Scgd }
29963d7b677Scgd au->au_shfaults++;
30063d7b677Scgd
30163d7b677Scgd /* first deserialize the creds back into a struct authunix_parms */
30263d7b677Scgd aup.aup_machname = NULL;
303ea5394abSchristos aup.aup_gids = NULL;
30463d7b677Scgd xdrmem_create(&xdrs, au->au_origcred.oa_base,
30563d7b677Scgd au->au_origcred.oa_length, XDR_DECODE);
30663d7b677Scgd stat = xdr_authunix_parms(&xdrs, &aup);
30763d7b677Scgd if (! stat)
30863d7b677Scgd goto done;
30963d7b677Scgd
31063d7b677Scgd /* update the time and serialize in place */
311ea5394abSchristos (void)gettimeofday(&now, NULL);
312461a86f9Schristos aup.aup_time = (u_long)now.tv_sec; /* XXX: truncate on 32 bit */
31363d7b677Scgd xdrs.x_op = XDR_ENCODE;
31463d7b677Scgd XDR_SETPOS(&xdrs, 0);
31563d7b677Scgd stat = xdr_authunix_parms(&xdrs, &aup);
31663d7b677Scgd if (! stat)
31763d7b677Scgd goto done;
31863d7b677Scgd auth->ah_cred = au->au_origcred;
31963d7b677Scgd marshal_new_auth(auth);
32063d7b677Scgd done:
32163d7b677Scgd /* free the struct authunix_parms created by deserializing */
32263d7b677Scgd xdrs.x_op = XDR_FREE;
32363d7b677Scgd (void)xdr_authunix_parms(&xdrs, &aup);
32463d7b677Scgd XDR_DESTROY(&xdrs);
32563d7b677Scgd return (stat);
32663d7b677Scgd }
32763d7b677Scgd
32863d7b677Scgd static void
authunix_destroy(AUTH * auth)329adb74221Smatt authunix_destroy(AUTH *auth)
33063d7b677Scgd {
331b48252f3Slukem struct audata *au;
33263d7b677Scgd
333b48252f3Slukem _DIAGASSERT(auth != NULL);
334b48252f3Slukem
335b48252f3Slukem au = AUTH_PRIVATE(auth);
33663d7b677Scgd mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
33763d7b677Scgd
33863d7b677Scgd if (au->au_shcred.oa_base != NULL)
33963d7b677Scgd mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
34063d7b677Scgd
34163d7b677Scgd mem_free(auth->ah_private, sizeof(struct audata));
34263d7b677Scgd
34363d7b677Scgd if (auth->ah_verf.oa_base != NULL)
34463d7b677Scgd mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
34563d7b677Scgd
346ae3564f2Schristos mem_free(auth, sizeof(*auth));
34763d7b677Scgd }
34863d7b677Scgd
34963d7b677Scgd /*
35063d7b677Scgd * Marshals (pre-serializes) an auth struct.
35163d7b677Scgd * sets private data, au_marshed and au_mpos
35263d7b677Scgd */
35318ec2ba0Sjtc static void
marshal_new_auth(AUTH * auth)354adb74221Smatt marshal_new_auth(AUTH *auth)
35563d7b677Scgd {
35663d7b677Scgd XDR xdr_stream;
35746e6c5e8Slukem XDR *xdrs = &xdr_stream;
358b48252f3Slukem struct audata *au;
35963d7b677Scgd
360b48252f3Slukem _DIAGASSERT(auth != NULL);
361b48252f3Slukem
362b48252f3Slukem au = AUTH_PRIVATE(auth);
36363d7b677Scgd xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
36463d7b677Scgd if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
36546e6c5e8Slukem (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
366e24729deSchristos warnx("%s: Fatal marshalling problem", __func__);
36746e6c5e8Slukem else
36863d7b677Scgd au->au_mpos = XDR_GETPOS(xdrs);
36963d7b677Scgd XDR_DESTROY(xdrs);
37063d7b677Scgd }
3717df0ccbaSfvdl
3727df0ccbaSfvdl static const struct auth_ops *
authunix_ops(void)373adb74221Smatt authunix_ops(void)
3747df0ccbaSfvdl {
3757df0ccbaSfvdl static struct auth_ops ops;
3767df0ccbaSfvdl
3777df0ccbaSfvdl /* VARIABLES PROTECTED BY ops_lock: ops */
3787df0ccbaSfvdl
3797df0ccbaSfvdl mutex_lock(&ops_lock);
3807df0ccbaSfvdl if (ops.ah_nextverf == NULL) {
3817df0ccbaSfvdl ops.ah_nextverf = authunix_nextverf;
3827df0ccbaSfvdl ops.ah_marshal = authunix_marshal;
3837df0ccbaSfvdl ops.ah_validate = authunix_validate;
3847df0ccbaSfvdl ops.ah_refresh = authunix_refresh;
3857df0ccbaSfvdl ops.ah_destroy = authunix_destroy;
3867df0ccbaSfvdl }
3877df0ccbaSfvdl mutex_unlock(&ops_lock);
3887df0ccbaSfvdl return (&ops);
3897df0ccbaSfvdl }
390