acl_to_text.c revision 70841
156055Srwatson/*-
256055Srwatson * Copyright (c) 1999 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 *	$FreeBSD: head/lib/libc/posix1e/acl_to_text.c 70841 2001-01-09 05:45:03Z rwatson $
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>
3456055Srwatson#include <sys/acl.h>
3556055Srwatson#include <sys/errno.h>
3656055Srwatson#include <stdio.h>
3756055Srwatson#include <stdlib.h>
3856055Srwatson#include <string.h>
3956055Srwatson#include <utmp.h>
4056055Srwatson
4156055Srwatson#include "acl_support.h"
4256055Srwatson
4356055Srwatson/*
4456055Srwatson * acl_to_text - generate a text form of an acl
4556055Srwatson * spec says nothing about output ordering, so leave in acl order
4656055Srwatson *
4756625Srwatson * This function will not produce nice results if it is called with
4856625Srwatson * a non-POSIX.1e semantics ACL.
4956055Srwatson */
5056055Srwatsonchar *
5156055Srwatsonacl_to_text(acl_t acl, ssize_t *len_p)
5256055Srwatson{
5356055Srwatson	char	*buf, *tmpbuf;
5456055Srwatson	char	name_buf[UT_NAMESIZE+1];
5556055Srwatson	char	perm_buf[ACL_STRING_PERM_MAXSIZE+1],
5656055Srwatson		effective_perm_buf[ACL_STRING_PERM_MAXSIZE+1];
5756055Srwatson	int	i, error, len;
5856055Srwatson	uid_t	ae_id;
5956055Srwatson	acl_tag_t	ae_tag;
6056055Srwatson	acl_perm_t	ae_perm, effective_perm, mask_perm;
6156055Srwatson
6256055Srwatson	buf = strdup("");
6370841Srwatson	if (!buf)
6470841Srwatson		return(0);
6556055Srwatson
6656055Srwatson	mask_perm = ACL_PERM_BITS;	/* effective is regular if no mask */
6756055Srwatson	for (i = 0; i < acl->acl_cnt; i++)
6856055Srwatson		if (acl->acl_entry[i].ae_tag == ACL_MASK)
6956055Srwatson			mask_perm = acl->acl_entry[i].ae_perm;
7056055Srwatson
7156055Srwatson	for (i = 0; i < acl->acl_cnt; i++) {
7256055Srwatson		ae_tag = acl->acl_entry[i].ae_tag;
7356055Srwatson		ae_id = acl->acl_entry[i].ae_id;
7456055Srwatson		ae_perm = acl->acl_entry[i].ae_perm;
7556055Srwatson
7656055Srwatson		switch(ae_tag) {
7756055Srwatson		case ACL_USER_OBJ:
7856055Srwatson			error = acl_perm_to_string(ae_perm,
7956055Srwatson			    ACL_STRING_PERM_MAXSIZE+1, perm_buf);
8056055Srwatson			if (error)
8156055Srwatson				goto error_label;
8256055Srwatson			len = asprintf(&tmpbuf, "%suser::%s\n", buf,
8356055Srwatson			    perm_buf);
8470841Srwatson			if (len == -1)
8556055Srwatson				goto error_label;
8656055Srwatson			free(buf);
8756055Srwatson			buf = tmpbuf;
8856055Srwatson			break;
8956055Srwatson
9056055Srwatson		case ACL_USER:
9156055Srwatson			error = acl_perm_to_string(ae_perm,
9256055Srwatson			    ACL_STRING_PERM_MAXSIZE+1, perm_buf);
9356055Srwatson			if (error)
9456055Srwatson				goto error_label;
9556055Srwatson
9656055Srwatson			error = acl_id_to_name(ae_tag, ae_id, UT_NAMESIZE+1,
9756055Srwatson			    name_buf);
9856055Srwatson			if (error)
9956055Srwatson				goto error_label;
10056055Srwatson
10156055Srwatson			effective_perm = ae_perm & mask_perm;
10256055Srwatson			if (effective_perm != ae_perm) {
10356055Srwatson				error = acl_perm_to_string(effective_perm,
10456055Srwatson				    ACL_STRING_PERM_MAXSIZE+1,
10556055Srwatson				    effective_perm_buf);
10656055Srwatson				if (error)
10756055Srwatson					goto error_label;
10856055Srwatson				len = asprintf(&tmpbuf, "%suser:%s:%s\t\t# "
10956055Srwatson				    "effective: %s\n",
11056055Srwatson				    buf, name_buf, perm_buf,
11156055Srwatson				    effective_perm_buf);
11256055Srwatson			} else {
11356055Srwatson				len = asprintf(&tmpbuf, "%suser:%s:%s\n", buf,
11456055Srwatson				    name_buf, perm_buf);
11556055Srwatson			}
11670841Srwatson			if (len == -1)
11756055Srwatson				goto error_label;
11856055Srwatson			free(buf);
11956055Srwatson			buf = tmpbuf;
12056055Srwatson			break;
12156055Srwatson
12256055Srwatson		case ACL_GROUP_OBJ:
12356055Srwatson			error = acl_perm_to_string(ae_perm,
12456055Srwatson			    ACL_STRING_PERM_MAXSIZE+1, perm_buf);
12556055Srwatson			if (error)
12656055Srwatson				goto error_label;
12756055Srwatson
12856055Srwatson			effective_perm = ae_perm & mask_perm;
12956055Srwatson			if (effective_perm != ae_perm) {
13056055Srwatson				error = acl_perm_to_string(effective_perm,
13156055Srwatson				    ACL_STRING_PERM_MAXSIZE+1,
13256055Srwatson				    effective_perm_buf);
13356055Srwatson				if (error)
13456055Srwatson					goto error_label;
13556055Srwatson				len = asprintf(&tmpbuf, "%sgroup::%s\t\t# "
13656055Srwatson				    "effective: %s\n",
13756055Srwatson				    buf, perm_buf, effective_perm_buf);
13856055Srwatson			} else {
13956055Srwatson				len = asprintf(&tmpbuf, "%sgroup::%s\n", buf,
14056055Srwatson				    perm_buf);
14156055Srwatson			}
14270841Srwatson			if (len == -1)
14356055Srwatson				goto error_label;
14456055Srwatson			free(buf);
14556055Srwatson			buf = tmpbuf;
14656055Srwatson			break;
14756055Srwatson
14856055Srwatson		case ACL_GROUP:
14956055Srwatson			error = acl_perm_to_string(ae_perm,
15056055Srwatson			    ACL_STRING_PERM_MAXSIZE+1, perm_buf);
15156055Srwatson			if (error)
15256055Srwatson				goto error_label;
15356055Srwatson
15456055Srwatson			error = acl_id_to_name(ae_tag, ae_id, UT_NAMESIZE+1,
15556055Srwatson			    name_buf);
15656055Srwatson			if (error)
15756055Srwatson				goto error_label;
15856055Srwatson
15956055Srwatson			effective_perm = ae_perm & mask_perm;
16056055Srwatson			if (effective_perm != ae_perm) {
16156055Srwatson				error = acl_perm_to_string(effective_perm,
16256055Srwatson				    ACL_STRING_PERM_MAXSIZE+1,
16356055Srwatson				    effective_perm_buf);
16456055Srwatson				if (error)
16556055Srwatson					goto error_label;
16656055Srwatson				len = asprintf(&tmpbuf, "%sgroup::%s\t\t# "
16756055Srwatson				    "effective: %s\n",
16856055Srwatson				    buf, perm_buf, effective_perm_buf);
16956055Srwatson			} else {
17056055Srwatson				len = asprintf(&tmpbuf, "%sgroup:%s:%s\n", buf,
17156055Srwatson				    name_buf, perm_buf);
17256055Srwatson			}
17370841Srwatson			if (len == -1)
17456055Srwatson				goto error_label;
17556055Srwatson			free(buf);
17656055Srwatson			buf = tmpbuf;
17756055Srwatson			break;
17856055Srwatson
17956055Srwatson		case ACL_MASK:
18056055Srwatson			error = acl_perm_to_string(ae_perm,
18156055Srwatson			    ACL_STRING_PERM_MAXSIZE+1, perm_buf);
18256055Srwatson			if (error)
18356055Srwatson				goto error_label;
18456055Srwatson
18556055Srwatson			len = asprintf(&tmpbuf, "%smask::%s\n", buf,
18656055Srwatson			    perm_buf);
18770841Srwatson			if (len == -1)
18856055Srwatson				goto error_label;
18956055Srwatson			free(buf);
19056055Srwatson			buf = tmpbuf;
19156055Srwatson			break;
19256055Srwatson
19356055Srwatson		case ACL_OTHER:
19456055Srwatson			error = acl_perm_to_string(ae_perm,
19556055Srwatson			    ACL_STRING_PERM_MAXSIZE+1, perm_buf);
19656055Srwatson			if (error)
19756055Srwatson				goto error_label;
19856055Srwatson
19956055Srwatson			len = asprintf(&tmpbuf, "%sother::%s\n", buf,
20056055Srwatson			    perm_buf);
20170841Srwatson			if (len == -1)
20256055Srwatson				goto error_label;
20356055Srwatson			free(buf);
20456055Srwatson			buf = tmpbuf;
20556055Srwatson			break;
20656055Srwatson
20756055Srwatson		default:
20856055Srwatson			errno = EINVAL;
20970841Srwatson			goto error_label;
21056055Srwatson		}
21156055Srwatson	}
21256055Srwatson
21356055Srwatson	if (len_p) {
21456055Srwatson		*len_p = strlen(buf);
21556055Srwatson	}
21656055Srwatson	return (buf);
21756055Srwatson
21856055Srwatsonerror_label:
21956055Srwatson	/* jump to here sets errno already, we just clean up */
22056055Srwatson	if (buf) free(buf);
22156055Srwatson	return (0);
22256055Srwatson}
223