/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2009, Sun Microsystems, Inc.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8* - Redistributions of source code must retain the above copyright notice,9* this list of conditions and the following disclaimer.10* - Redistributions in binary form must reproduce the above copyright notice,11* this list of conditions and the following disclaimer in the documentation12* and/or other materials provided with the distribution.13* - Neither the name of Sun Microsystems, Inc. nor the names of its14* contributors may be used to endorse or promote products derived15* from this software without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE21* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*/2930#include <sys/cdefs.h>31/*32* svc_auth_unix.c33* Handles UNIX flavor authentication parameters on the service side of rpc.34* There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.35* _svcauth_unix does full blown unix style uid,gid+gids auth,36* _svcauth_short uses a shorthand auth to index into a cache of longhand auths.37* Note: the shorthand has been gutted for efficiency.38*39* Copyright (C) 1984, Sun Microsystems, Inc.40*/4142#include <sys/param.h>43#include <sys/lock.h>44#include <sys/mutex.h>45#include <sys/systm.h>46#include <sys/ucred.h>4748#include <rpc/rpc.h>4950#include <rpc/rpc_com.h>5152#define MAX_MACHINE_NAME 25553#define NGRPS 165455/*56* Unix longhand authenticator57*/58enum auth_stat59_svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)60{61enum auth_stat stat;62XDR xdrs;63int32_t *buf;64uint32_t time;65struct xucred *xcr;66u_int auth_len;67size_t str_len, gid_len;68u_int i;6970xcr = rqst->rq_clntcred;71auth_len = (u_int)msg->rm_call.cb_cred.oa_length;72xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,73XDR_DECODE);74buf = XDR_INLINE(&xdrs, auth_len);75if (buf != NULL) {76time = IXDR_GET_UINT32(buf);77str_len = (size_t)IXDR_GET_UINT32(buf);78if (str_len > MAX_MACHINE_NAME) {79stat = AUTH_BADCRED;80goto done;81}82str_len = RNDUP(str_len);83buf += str_len / sizeof (int32_t);84xcr->cr_uid = IXDR_GET_UINT32(buf);85xcr->cr_gid = IXDR_GET_UINT32(buf);86gid_len = (size_t)IXDR_GET_UINT32(buf);87if (gid_len > NGRPS) {88stat = AUTH_BADCRED;89goto done;90}91for (i = 0; i < gid_len; i++) {92/*93* Note that this is a `struct xucred`, which maintains94* its historical layout of preserving the egid in95* cr_ngroups and cr_groups[0] == egid.96*/97if (i + 1 < XU_NGROUPS)98xcr->cr_groups[i + 1] = IXDR_GET_INT32(buf);99else100buf++;101}102if (gid_len + 1 > XU_NGROUPS)103xcr->cr_ngroups = XU_NGROUPS;104else105xcr->cr_ngroups = gid_len + 1;106107/*108* five is the smallest unix credentials structure -109* timestamp, hostname len (0), uid, gid, and gids len (0).110*/111if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {112(void) printf("bad auth_len gid %ld str %ld auth %u\n",113(long)gid_len, (long)str_len, auth_len);114stat = AUTH_BADCRED;115goto done;116}117} else if (! xdr_authunix_parms(&xdrs, &time, xcr)) {118stat = AUTH_BADCRED;119goto done;120}121122rqst->rq_verf = _null_auth;123stat = AUTH_OK;124done:125XDR_DESTROY(&xdrs);126127return (stat);128}129130131/*132* Shorthand unix authenticator133* Looks up longhand in a cache.134*/135/*ARGSUSED*/136enum auth_stat137_svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)138{139return (AUTH_REJECTEDCRED);140}141142143