acl_to_text.c revision 91034
156055Srwatson/*-
289831Sjedgar * Copyright (c) 1999-2002 Robert N. M. Watson
356055Srwatson * All rights reserved.
456055Srwatson *
556055Srwatson * Redistribution and use in source and binary forms, with or without
656055Srwatson * modification, are permitted provided that the following conditions
756055Srwatson * are met:
856055Srwatson * 1. Redistributions of source code must retain the above copyright
956055Srwatson *    notice, this list of conditions and the following disclaimer.
1056055Srwatson * 2. Redistributions in binary form must reproduce the above copyright
1156055Srwatson *    notice, this list of conditions and the following disclaimer in the
1256055Srwatson *    documentation and/or other materials provided with the distribution.
1356055Srwatson *
1456055Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1556055Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1656055Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1756055Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1856055Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1956055Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2056055Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2156055Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2256055Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2356055Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2456055Srwatson * SUCH DAMAGE.
2556055Srwatson *
2674191Srwatson * $FreeBSD: head/lib/libc/posix1e/acl_to_text.c 91034 2002-02-21 23:17:19Z jedgar $
2756055Srwatson */
2856055Srwatson/*
2956055Srwatson * acl_to_text - return a text string with a text representation of the acl
3056055Srwatson * in it.
3156055Srwatson */
3256055Srwatson
3356055Srwatson#include <sys/types.h>
3475185Stmm#include "namespace.h"
3556055Srwatson#include <sys/acl.h>
3675185Stmm#include "un-namespace.h"
3756055Srwatson#include <sys/errno.h>
3856055Srwatson#include <stdio.h>
3956055Srwatson#include <stdlib.h>
4056055Srwatson#include <string.h>
4156055Srwatson#include <utmp.h>
4256055Srwatson
4356055Srwatson#include "acl_support.h"
4456055Srwatson
4556055Srwatson/*
4656055Srwatson * acl_to_text - generate a text form of an acl
4756055Srwatson * spec says nothing about output ordering, so leave in acl order
4856055Srwatson *
4956625Srwatson * This function will not produce nice results if it is called with
5056625Srwatson * a non-POSIX.1e semantics ACL.
5156055Srwatson */
5256055Srwatsonchar *
5356055Srwatsonacl_to_text(acl_t acl, ssize_t *len_p)
5456055Srwatson{
5575928Sjedgar	struct acl	*acl_int;
5675928Sjedgar	char		*buf, *tmpbuf;
5775928Sjedgar	char		 name_buf[UT_NAMESIZE+1];
5875928Sjedgar	char		 perm_buf[_POSIX1E_ACL_STRING_PERM_MAXSIZE+1],
5975928Sjedgar			 effective_perm_buf[_POSIX1E_ACL_STRING_PERM_MAXSIZE+1];
6075928Sjedgar	int		 i, error, len;
6175928Sjedgar	uid_t		 ae_id;
6275928Sjedgar	acl_tag_t	 ae_tag;
6375928Sjedgar	acl_perm_t	 ae_perm, effective_perm, mask_perm;
6456055Srwatson
6556055Srwatson	buf = strdup("");
6691034Sjedgar	if (buf == NULL)
6771142Srwatson		return(NULL);
6856055Srwatson
6989831Sjedgar	if (acl == NULL) {
7089831Sjedgar		errno = EINVAL;
7189831Sjedgar		return(NULL);
7289831Sjedgar	}
7389831Sjedgar
7475928Sjedgar	acl_int = &acl->ats_acl;
7575928Sjedgar
7656055Srwatson	mask_perm = ACL_PERM_BITS;	/* effective is regular if no mask */
7775928Sjedgar	for (i = 0; i < acl_int->acl_cnt; i++)
7875928Sjedgar		if (acl_int->acl_entry[i].ae_tag == ACL_MASK)
7975928Sjedgar			mask_perm = acl_int->acl_entry[i].ae_perm;
8056055Srwatson
8175928Sjedgar	for (i = 0; i < acl_int->acl_cnt; i++) {
8275928Sjedgar		ae_tag = acl_int->acl_entry[i].ae_tag;
8375928Sjedgar		ae_id = acl_int->acl_entry[i].ae_id;
8475928Sjedgar		ae_perm = acl_int->acl_entry[i].ae_perm;
8556055Srwatson
8656055Srwatson		switch(ae_tag) {
8756055Srwatson		case ACL_USER_OBJ:
8874191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
8974191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
9056055Srwatson			if (error)
9156055Srwatson				goto error_label;
9256055Srwatson			len = asprintf(&tmpbuf, "%suser::%s\n", buf,
9356055Srwatson			    perm_buf);
9470841Srwatson			if (len == -1)
9556055Srwatson				goto error_label;
9656055Srwatson			free(buf);
9756055Srwatson			buf = tmpbuf;
9856055Srwatson			break;
9956055Srwatson
10056055Srwatson		case ACL_USER:
10174191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
10274191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
10356055Srwatson			if (error)
10456055Srwatson				goto error_label;
10556055Srwatson
10674191Srwatson			error = _posix1e_acl_id_to_name(ae_tag, ae_id,
10774191Srwatson			    UT_NAMESIZE+1, name_buf);
10856055Srwatson			if (error)
10956055Srwatson				goto error_label;
11056055Srwatson
11156055Srwatson			effective_perm = ae_perm & mask_perm;
11256055Srwatson			if (effective_perm != ae_perm) {
11374191Srwatson				error = _posix1e_acl_perm_to_string(
11474191Srwatson				    effective_perm,
11574191Srwatson				    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1,
11656055Srwatson				    effective_perm_buf);
11756055Srwatson				if (error)
11856055Srwatson					goto error_label;
11956055Srwatson				len = asprintf(&tmpbuf, "%suser:%s:%s\t\t# "
12056055Srwatson				    "effective: %s\n",
12156055Srwatson				    buf, name_buf, perm_buf,
12256055Srwatson				    effective_perm_buf);
12356055Srwatson			} else {
12456055Srwatson				len = asprintf(&tmpbuf, "%suser:%s:%s\n", buf,
12556055Srwatson				    name_buf, perm_buf);
12656055Srwatson			}
12770841Srwatson			if (len == -1)
12856055Srwatson				goto error_label;
12956055Srwatson			free(buf);
13056055Srwatson			buf = tmpbuf;
13156055Srwatson			break;
13256055Srwatson
13356055Srwatson		case ACL_GROUP_OBJ:
13474191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
13574191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
13656055Srwatson			if (error)
13756055Srwatson				goto error_label;
13856055Srwatson
13956055Srwatson			effective_perm = ae_perm & mask_perm;
14056055Srwatson			if (effective_perm != ae_perm) {
14174191Srwatson				error = _posix1e_acl_perm_to_string(
14274191Srwatson				    effective_perm,
14374191Srwatson				    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1,
14456055Srwatson				    effective_perm_buf);
14556055Srwatson				if (error)
14656055Srwatson					goto error_label;
14756055Srwatson				len = asprintf(&tmpbuf, "%sgroup::%s\t\t# "
14856055Srwatson				    "effective: %s\n",
14956055Srwatson				    buf, perm_buf, effective_perm_buf);
15056055Srwatson			} else {
15156055Srwatson				len = asprintf(&tmpbuf, "%sgroup::%s\n", buf,
15256055Srwatson				    perm_buf);
15356055Srwatson			}
15470841Srwatson			if (len == -1)
15556055Srwatson				goto error_label;
15656055Srwatson			free(buf);
15756055Srwatson			buf = tmpbuf;
15856055Srwatson			break;
15956055Srwatson
16056055Srwatson		case ACL_GROUP:
16174191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
16274191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
16356055Srwatson			if (error)
16456055Srwatson				goto error_label;
16556055Srwatson
16674191Srwatson			error = _posix1e_acl_id_to_name(ae_tag, ae_id,
16774191Srwatson			    UT_NAMESIZE+1, name_buf);
16856055Srwatson			if (error)
16956055Srwatson				goto error_label;
17056055Srwatson
17156055Srwatson			effective_perm = ae_perm & mask_perm;
17256055Srwatson			if (effective_perm != ae_perm) {
17374191Srwatson				error = _posix1e_acl_perm_to_string(
17474191Srwatson				    effective_perm,
17574191Srwatson				    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1,
17656055Srwatson				    effective_perm_buf);
17756055Srwatson				if (error)
17856055Srwatson					goto error_label;
17956055Srwatson				len = asprintf(&tmpbuf, "%sgroup::%s\t\t# "
18056055Srwatson				    "effective: %s\n",
18156055Srwatson				    buf, perm_buf, effective_perm_buf);
18256055Srwatson			} else {
18356055Srwatson				len = asprintf(&tmpbuf, "%sgroup:%s:%s\n", buf,
18456055Srwatson				    name_buf, perm_buf);
18556055Srwatson			}
18670841Srwatson			if (len == -1)
18756055Srwatson				goto error_label;
18856055Srwatson			free(buf);
18956055Srwatson			buf = tmpbuf;
19056055Srwatson			break;
19156055Srwatson
19256055Srwatson		case ACL_MASK:
19374191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
19474191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
19556055Srwatson			if (error)
19656055Srwatson				goto error_label;
19756055Srwatson
19856055Srwatson			len = asprintf(&tmpbuf, "%smask::%s\n", buf,
19956055Srwatson			    perm_buf);
20070841Srwatson			if (len == -1)
20156055Srwatson				goto error_label;
20256055Srwatson			free(buf);
20356055Srwatson			buf = tmpbuf;
20456055Srwatson			break;
20556055Srwatson
20656055Srwatson		case ACL_OTHER:
20774191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
20874191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
20956055Srwatson			if (error)
21056055Srwatson				goto error_label;
21156055Srwatson
21256055Srwatson			len = asprintf(&tmpbuf, "%sother::%s\n", buf,
21356055Srwatson			    perm_buf);
21470841Srwatson			if (len == -1)
21556055Srwatson				goto error_label;
21656055Srwatson			free(buf);
21756055Srwatson			buf = tmpbuf;
21856055Srwatson			break;
21956055Srwatson
22056055Srwatson		default:
22156055Srwatson			errno = EINVAL;
22270841Srwatson			goto error_label;
22356055Srwatson		}
22456055Srwatson	}
22556055Srwatson
22656055Srwatson	if (len_p) {
22756055Srwatson		*len_p = strlen(buf);
22856055Srwatson	}
22956055Srwatson	return (buf);
23056055Srwatson
23156055Srwatsonerror_label:
23256055Srwatson	/* jump to here sets errno already, we just clean up */
23356055Srwatson	if (buf) free(buf);
23471142Srwatson	return (NULL);
23556055Srwatson}
236