acl_id_to_name.c revision 209147
1209147Skientzle/*- 2209147Skientzle * Copyright (c) 1999-2001, 2008 Robert N. M. Watson 3209147Skientzle * All rights reserved. 4209147Skientzle * 5209147Skientzle * Redistribution and use in source and binary forms, with or without 6209147Skientzle * modification, are permitted provided that the following conditions 7209147Skientzle * are met: 8209147Skientzle * 1. Redistributions of source code must retain the above copyright 9209147Skientzle * notice, this list of conditions and the following disclaimer. 10209147Skientzle * 2. Redistributions in binary form must reproduce the above copyright 11209147Skientzle * notice, this list of conditions and the following disclaimer in the 12209147Skientzle * documentation and/or other materials provided with the distribution. 13209147Skientzle * 14209147Skientzle * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15209147Skientzle * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16209147Skientzle * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17209147Skientzle * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18209147Skientzle * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19209147Skientzle * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20209147Skientzle * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21209147Skientzle * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22209147Skientzle * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23209147Skientzle * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24209147Skientzle * SUCH DAMAGE. 25209147Skientzle */ 26209147Skientzle/* 27209147Skientzle * Support functionality for the POSIX.1e ACL interface 28209147Skientzle * These calls are intended only to be called within the library. 29209147Skientzle */ 30209147Skientzle 31209147Skientzle#include <sys/cdefs.h> 32209147Skientzle__FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_id_to_name.c 209147 2010-06-14 02:26:13Z kientzle $"); 33209147Skientzle 34209147Skientzle#include <sys/types.h> 35209147Skientzle#include "namespace.h" 36209147Skientzle#include <sys/acl.h> 37209147Skientzle#include "un-namespace.h" 38209147Skientzle#include <errno.h> 39209147Skientzle#include <grp.h> 40209147Skientzle#include <pwd.h> 41209147Skientzle#include <stdio.h> 42209147Skientzle#include <stdlib.h> 43209147Skientzle#include <string.h> 44209147Skientzle#include <assert.h> 45209147Skientzle 46209147Skientzle#include "acl_support.h" 47209147Skientzle 48209147Skientzle/* 49209147Skientzle * Given a uid/gid, return a username/groupname for the text form of an ACL. 50209147Skientzle * Note that we truncate user and group names, rather than error out, as 51209147Skientzle * this is consistent with other tools manipulating user and group names. 52209147Skientzle * XXX NOT THREAD SAFE, RELIES ON GETPWUID, GETGRGID 53209147Skientzle * XXX USES *PW* AND *GR* WHICH ARE STATEFUL AND THEREFORE THIS ROUTINE 54209147Skientzle * MAY HAVE SIDE-EFFECTS 55209147Skientzle */ 56209147Skientzleint 57209147Skientzle_posix1e_acl_id_to_name(acl_tag_t tag, uid_t id, ssize_t buf_len, char *buf, 58209147Skientzle int flags) 59209147Skientzle{ 60209147Skientzle struct group *g; 61209147Skientzle struct passwd *p; 62209147Skientzle int i; 63209147Skientzle 64209147Skientzle switch(tag) { 65209147Skientzle case ACL_USER: 66209147Skientzle if (flags & ACL_TEXT_NUMERIC_IDS) 67209147Skientzle p = NULL; 68209147Skientzle else 69209147Skientzle p = getpwuid(id); 70209147Skientzle if (!p) 71209147Skientzle i = snprintf(buf, buf_len, "%d", id); 72209147Skientzle else 73209147Skientzle i = snprintf(buf, buf_len, "%s", p->pw_name); 74209147Skientzle 75209147Skientzle if (i < 0) { 76209147Skientzle errno = ENOMEM; 77209147Skientzle return (-1); 78209147Skientzle } 79209147Skientzle return (0); 80209147Skientzle 81209147Skientzle case ACL_GROUP: 82209147Skientzle if (flags & ACL_TEXT_NUMERIC_IDS) 83209147Skientzle g = NULL; 84209147Skientzle else 85209147Skientzle g = getgrgid(id); 86209147Skientzle if (g == NULL) 87209147Skientzle i = snprintf(buf, buf_len, "%d", id); 88209147Skientzle else 89209147Skientzle i = snprintf(buf, buf_len, "%s", g->gr_name); 90209147Skientzle 91209147Skientzle if (i < 0) { 92209147Skientzle errno = ENOMEM; 93209147Skientzle return (-1); 94209147Skientzle } 95209147Skientzle return (0); 96209147Skientzle 97209147Skientzle default: 98209147Skientzle return (EINVAL); 99209147Skientzle } 100209147Skientzle} 101