12ca090b3STim Kientzle /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
42ca090b3STim Kientzle * Copyright (c) 1999-2001, 2008 Robert N. M. Watson
52ca090b3STim Kientzle * All rights reserved.
62ca090b3STim Kientzle *
72ca090b3STim Kientzle * Redistribution and use in source and binary forms, with or without
82ca090b3STim Kientzle * modification, are permitted provided that the following conditions
92ca090b3STim Kientzle * are met:
102ca090b3STim Kientzle * 1. Redistributions of source code must retain the above copyright
112ca090b3STim Kientzle * notice, this list of conditions and the following disclaimer.
122ca090b3STim Kientzle * 2. Redistributions in binary form must reproduce the above copyright
132ca090b3STim Kientzle * notice, this list of conditions and the following disclaimer in the
142ca090b3STim Kientzle * documentation and/or other materials provided with the distribution.
152ca090b3STim Kientzle *
162ca090b3STim Kientzle * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172ca090b3STim Kientzle * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182ca090b3STim Kientzle * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192ca090b3STim Kientzle * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202ca090b3STim Kientzle * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212ca090b3STim Kientzle * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222ca090b3STim Kientzle * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232ca090b3STim Kientzle * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242ca090b3STim Kientzle * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252ca090b3STim Kientzle * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262ca090b3STim Kientzle * SUCH DAMAGE.
272ca090b3STim Kientzle */
282ca090b3STim Kientzle /*
292ca090b3STim Kientzle * Support functionality for the POSIX.1e ACL interface
302ca090b3STim Kientzle * These calls are intended only to be called within the library.
312ca090b3STim Kientzle */
322ca090b3STim Kientzle
332ca090b3STim Kientzle #include <sys/types.h>
342ca090b3STim Kientzle #include "namespace.h"
352ca090b3STim Kientzle #include <sys/acl.h>
362ca090b3STim Kientzle #include "un-namespace.h"
372ca090b3STim Kientzle #include <errno.h>
382ca090b3STim Kientzle #include <grp.h>
392ca090b3STim Kientzle #include <pwd.h>
402ca090b3STim Kientzle #include <stdio.h>
412ca090b3STim Kientzle #include <stdlib.h>
422ca090b3STim Kientzle #include <string.h>
432ca090b3STim Kientzle #include <assert.h>
442ca090b3STim Kientzle
452ca090b3STim Kientzle #include "acl_support.h"
462ca090b3STim Kientzle
472ca090b3STim Kientzle /*
482ca090b3STim Kientzle * Given a uid/gid, return a username/groupname for the text form of an ACL.
492ca090b3STim Kientzle * Note that we truncate user and group names, rather than error out, as
502ca090b3STim Kientzle * this is consistent with other tools manipulating user and group names.
512ca090b3STim Kientzle * XXX NOT THREAD SAFE, RELIES ON GETPWUID, GETGRGID
522ca090b3STim Kientzle * XXX USES *PW* AND *GR* WHICH ARE STATEFUL AND THEREFORE THIS ROUTINE
532ca090b3STim Kientzle * MAY HAVE SIDE-EFFECTS
542ca090b3STim Kientzle */
552ca090b3STim Kientzle int
_posix1e_acl_id_to_name(acl_tag_t tag,uid_t id,ssize_t buf_len,char * buf,int flags)562ca090b3STim Kientzle _posix1e_acl_id_to_name(acl_tag_t tag, uid_t id, ssize_t buf_len, char *buf,
572ca090b3STim Kientzle int flags)
582ca090b3STim Kientzle {
592ca090b3STim Kientzle struct group *g;
602ca090b3STim Kientzle struct passwd *p;
612ca090b3STim Kientzle int i;
622ca090b3STim Kientzle
632ca090b3STim Kientzle switch(tag) {
642ca090b3STim Kientzle case ACL_USER:
652ca090b3STim Kientzle if (flags & ACL_TEXT_NUMERIC_IDS)
662ca090b3STim Kientzle p = NULL;
672ca090b3STim Kientzle else
682ca090b3STim Kientzle p = getpwuid(id);
692ca090b3STim Kientzle if (!p)
702ca090b3STim Kientzle i = snprintf(buf, buf_len, "%d", id);
712ca090b3STim Kientzle else
722ca090b3STim Kientzle i = snprintf(buf, buf_len, "%s", p->pw_name);
732ca090b3STim Kientzle
742ca090b3STim Kientzle if (i < 0) {
752ca090b3STim Kientzle errno = ENOMEM;
762ca090b3STim Kientzle return (-1);
772ca090b3STim Kientzle }
782ca090b3STim Kientzle return (0);
792ca090b3STim Kientzle
802ca090b3STim Kientzle case ACL_GROUP:
812ca090b3STim Kientzle if (flags & ACL_TEXT_NUMERIC_IDS)
822ca090b3STim Kientzle g = NULL;
832ca090b3STim Kientzle else
842ca090b3STim Kientzle g = getgrgid(id);
852ca090b3STim Kientzle if (g == NULL)
862ca090b3STim Kientzle i = snprintf(buf, buf_len, "%d", id);
872ca090b3STim Kientzle else
882ca090b3STim Kientzle i = snprintf(buf, buf_len, "%s", g->gr_name);
892ca090b3STim Kientzle
902ca090b3STim Kientzle if (i < 0) {
912ca090b3STim Kientzle errno = ENOMEM;
922ca090b3STim Kientzle return (-1);
932ca090b3STim Kientzle }
942ca090b3STim Kientzle return (0);
952ca090b3STim Kientzle
962ca090b3STim Kientzle default:
972ca090b3STim Kientzle return (EINVAL);
982ca090b3STim Kientzle }
992ca090b3STim Kientzle }
100