acl_to_text.c revision 92986
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 */
2656055Srwatson/*
2756055Srwatson * acl_to_text - return a text string with a text representation of the acl
2856055Srwatson * in it.
2956055Srwatson */
3056055Srwatson
3192986Sobrien#include <sys/cdefs.h>
3292986Sobrien__FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_to_text.c 92986 2002-03-22 21:53:29Z obrien $");
3392986Sobrien
3456055Srwatson#include <sys/types.h>
3575185Stmm#include "namespace.h"
3656055Srwatson#include <sys/acl.h>
3775185Stmm#include "un-namespace.h"
3856055Srwatson#include <sys/errno.h>
3956055Srwatson#include <stdio.h>
4056055Srwatson#include <stdlib.h>
4156055Srwatson#include <string.h>
4256055Srwatson#include <utmp.h>
4356055Srwatson
4456055Srwatson#include "acl_support.h"
4556055Srwatson
4656055Srwatson/*
4756055Srwatson * acl_to_text - generate a text form of an acl
4856055Srwatson * spec says nothing about output ordering, so leave in acl order
4956055Srwatson *
5056625Srwatson * This function will not produce nice results if it is called with
5156625Srwatson * a non-POSIX.1e semantics ACL.
5256055Srwatson */
5356055Srwatsonchar *
5456055Srwatsonacl_to_text(acl_t acl, ssize_t *len_p)
5556055Srwatson{
5675928Sjedgar	struct acl	*acl_int;
5775928Sjedgar	char		*buf, *tmpbuf;
5875928Sjedgar	char		 name_buf[UT_NAMESIZE+1];
5975928Sjedgar	char		 perm_buf[_POSIX1E_ACL_STRING_PERM_MAXSIZE+1],
6075928Sjedgar			 effective_perm_buf[_POSIX1E_ACL_STRING_PERM_MAXSIZE+1];
6175928Sjedgar	int		 i, error, len;
6275928Sjedgar	uid_t		 ae_id;
6375928Sjedgar	acl_tag_t	 ae_tag;
6475928Sjedgar	acl_perm_t	 ae_perm, effective_perm, mask_perm;
6556055Srwatson
6656055Srwatson	buf = strdup("");
6791034Sjedgar	if (buf == NULL)
6871142Srwatson		return(NULL);
6956055Srwatson
7089831Sjedgar	if (acl == NULL) {
7189831Sjedgar		errno = EINVAL;
7289831Sjedgar		return(NULL);
7389831Sjedgar	}
7489831Sjedgar
7575928Sjedgar	acl_int = &acl->ats_acl;
7675928Sjedgar
7756055Srwatson	mask_perm = ACL_PERM_BITS;	/* effective is regular if no mask */
7875928Sjedgar	for (i = 0; i < acl_int->acl_cnt; i++)
7975928Sjedgar		if (acl_int->acl_entry[i].ae_tag == ACL_MASK)
8075928Sjedgar			mask_perm = acl_int->acl_entry[i].ae_perm;
8156055Srwatson
8275928Sjedgar	for (i = 0; i < acl_int->acl_cnt; i++) {
8375928Sjedgar		ae_tag = acl_int->acl_entry[i].ae_tag;
8475928Sjedgar		ae_id = acl_int->acl_entry[i].ae_id;
8575928Sjedgar		ae_perm = acl_int->acl_entry[i].ae_perm;
8656055Srwatson
8756055Srwatson		switch(ae_tag) {
8856055Srwatson		case ACL_USER_OBJ:
8974191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
9074191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
9156055Srwatson			if (error)
9256055Srwatson				goto error_label;
9356055Srwatson			len = asprintf(&tmpbuf, "%suser::%s\n", buf,
9456055Srwatson			    perm_buf);
9570841Srwatson			if (len == -1)
9656055Srwatson				goto error_label;
9756055Srwatson			free(buf);
9856055Srwatson			buf = tmpbuf;
9956055Srwatson			break;
10056055Srwatson
10156055Srwatson		case ACL_USER:
10274191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
10374191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
10456055Srwatson			if (error)
10556055Srwatson				goto error_label;
10656055Srwatson
10774191Srwatson			error = _posix1e_acl_id_to_name(ae_tag, ae_id,
10874191Srwatson			    UT_NAMESIZE+1, name_buf);
10956055Srwatson			if (error)
11056055Srwatson				goto error_label;
11156055Srwatson
11256055Srwatson			effective_perm = ae_perm & mask_perm;
11356055Srwatson			if (effective_perm != ae_perm) {
11474191Srwatson				error = _posix1e_acl_perm_to_string(
11574191Srwatson				    effective_perm,
11674191Srwatson				    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1,
11756055Srwatson				    effective_perm_buf);
11856055Srwatson				if (error)
11956055Srwatson					goto error_label;
12056055Srwatson				len = asprintf(&tmpbuf, "%suser:%s:%s\t\t# "
12156055Srwatson				    "effective: %s\n",
12256055Srwatson				    buf, name_buf, perm_buf,
12356055Srwatson				    effective_perm_buf);
12456055Srwatson			} else {
12556055Srwatson				len = asprintf(&tmpbuf, "%suser:%s:%s\n", buf,
12656055Srwatson				    name_buf, perm_buf);
12756055Srwatson			}
12870841Srwatson			if (len == -1)
12956055Srwatson				goto error_label;
13056055Srwatson			free(buf);
13156055Srwatson			buf = tmpbuf;
13256055Srwatson			break;
13356055Srwatson
13456055Srwatson		case ACL_GROUP_OBJ:
13574191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
13674191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
13756055Srwatson			if (error)
13856055Srwatson				goto error_label;
13956055Srwatson
14056055Srwatson			effective_perm = ae_perm & mask_perm;
14156055Srwatson			if (effective_perm != ae_perm) {
14274191Srwatson				error = _posix1e_acl_perm_to_string(
14374191Srwatson				    effective_perm,
14474191Srwatson				    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1,
14556055Srwatson				    effective_perm_buf);
14656055Srwatson				if (error)
14756055Srwatson					goto error_label;
14856055Srwatson				len = asprintf(&tmpbuf, "%sgroup::%s\t\t# "
14956055Srwatson				    "effective: %s\n",
15056055Srwatson				    buf, perm_buf, effective_perm_buf);
15156055Srwatson			} else {
15256055Srwatson				len = asprintf(&tmpbuf, "%sgroup::%s\n", buf,
15356055Srwatson				    perm_buf);
15456055Srwatson			}
15570841Srwatson			if (len == -1)
15656055Srwatson				goto error_label;
15756055Srwatson			free(buf);
15856055Srwatson			buf = tmpbuf;
15956055Srwatson			break;
16056055Srwatson
16156055Srwatson		case ACL_GROUP:
16274191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
16374191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
16456055Srwatson			if (error)
16556055Srwatson				goto error_label;
16656055Srwatson
16774191Srwatson			error = _posix1e_acl_id_to_name(ae_tag, ae_id,
16874191Srwatson			    UT_NAMESIZE+1, name_buf);
16956055Srwatson			if (error)
17056055Srwatson				goto error_label;
17156055Srwatson
17256055Srwatson			effective_perm = ae_perm & mask_perm;
17356055Srwatson			if (effective_perm != ae_perm) {
17474191Srwatson				error = _posix1e_acl_perm_to_string(
17574191Srwatson				    effective_perm,
17674191Srwatson				    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1,
17756055Srwatson				    effective_perm_buf);
17856055Srwatson				if (error)
17956055Srwatson					goto error_label;
18056055Srwatson				len = asprintf(&tmpbuf, "%sgroup::%s\t\t# "
18156055Srwatson				    "effective: %s\n",
18256055Srwatson				    buf, perm_buf, effective_perm_buf);
18356055Srwatson			} else {
18456055Srwatson				len = asprintf(&tmpbuf, "%sgroup:%s:%s\n", buf,
18556055Srwatson				    name_buf, perm_buf);
18656055Srwatson			}
18770841Srwatson			if (len == -1)
18856055Srwatson				goto error_label;
18956055Srwatson			free(buf);
19056055Srwatson			buf = tmpbuf;
19156055Srwatson			break;
19256055Srwatson
19356055Srwatson		case ACL_MASK:
19474191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
19574191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
19656055Srwatson			if (error)
19756055Srwatson				goto error_label;
19856055Srwatson
19956055Srwatson			len = asprintf(&tmpbuf, "%smask::%s\n", buf,
20056055Srwatson			    perm_buf);
20170841Srwatson			if (len == -1)
20256055Srwatson				goto error_label;
20356055Srwatson			free(buf);
20456055Srwatson			buf = tmpbuf;
20556055Srwatson			break;
20656055Srwatson
20756055Srwatson		case ACL_OTHER:
20874191Srwatson			error = _posix1e_acl_perm_to_string(ae_perm,
20974191Srwatson			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
21056055Srwatson			if (error)
21156055Srwatson				goto error_label;
21256055Srwatson
21356055Srwatson			len = asprintf(&tmpbuf, "%sother::%s\n", buf,
21456055Srwatson			    perm_buf);
21570841Srwatson			if (len == -1)
21656055Srwatson				goto error_label;
21756055Srwatson			free(buf);
21856055Srwatson			buf = tmpbuf;
21956055Srwatson			break;
22056055Srwatson
22156055Srwatson		default:
22256055Srwatson			errno = EINVAL;
22370841Srwatson			goto error_label;
22456055Srwatson		}
22556055Srwatson	}
22656055Srwatson
22756055Srwatson	if (len_p) {
22856055Srwatson		*len_p = strlen(buf);
22956055Srwatson	}
23056055Srwatson	return (buf);
23156055Srwatson
23256055Srwatsonerror_label:
23356055Srwatson	/* jump to here sets errno already, we just clean up */
23456055Srwatson	if (buf) free(buf);
23571142Srwatson	return (NULL);
23656055Srwatson}
237