/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 1999-2001, 2008 Robert N. M. Watson4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/27/*28* Support functionality for the POSIX.1e ACL interface29* These calls are intended only to be called within the library.30*/3132#include <sys/types.h>33#include "namespace.h"34#include <sys/acl.h>35#include "un-namespace.h"36#include <errno.h>37#include <grp.h>38#include <pwd.h>39#include <stdio.h>40#include <stdlib.h>41#include <string.h>42#include <assert.h>4344#include "acl_support.h"4546/*47* Given a uid/gid, return a username/groupname for the text form of an ACL.48* Note that we truncate user and group names, rather than error out, as49* this is consistent with other tools manipulating user and group names.50* XXX NOT THREAD SAFE, RELIES ON GETPWUID, GETGRGID51* XXX USES *PW* AND *GR* WHICH ARE STATEFUL AND THEREFORE THIS ROUTINE52* MAY HAVE SIDE-EFFECTS53*/54int55_posix1e_acl_id_to_name(acl_tag_t tag, uid_t id, ssize_t buf_len, char *buf,56int flags)57{58struct group *g;59struct passwd *p;60int i;6162switch(tag) {63case ACL_USER:64if (flags & ACL_TEXT_NUMERIC_IDS)65p = NULL;66else67p = getpwuid(id);68if (!p)69i = snprintf(buf, buf_len, "%d", id);70else71i = snprintf(buf, buf_len, "%s", p->pw_name);7273if (i < 0) {74errno = ENOMEM;75return (-1);76}77return (0);7879case ACL_GROUP:80if (flags & ACL_TEXT_NUMERIC_IDS)81g = NULL;82else83g = getgrgid(id);84if (g == NULL)85i = snprintf(buf, buf_len, "%d", id);86else87i = snprintf(buf, buf_len, "%s", g->gr_name);8889if (i < 0) {90errno = ENOMEM;91return (-1);92}93return (0);9495default:96return (EINVAL);97}98}99100101