Home | History | Annotate | Line # | Download | only in posix1e
      1  1.1  christos /*-
      2  1.1  christos  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      3  1.1  christos  *
      4  1.1  christos  * Copyright (c) 1999, 2000, 2001 Robert N. M. Watson
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  christos  * SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos /*
     29  1.1  christos  * acl_from_text: Convert a text-form ACL from a string to an acl_t.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.1  christos #include <sys/cdefs.h>
     33  1.1  christos #if 0
     34  1.1  christos __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_from_text.c 326193 2017-11-25 17:12:48Z pfg $");
     35  1.1  christos #else
     36  1.2  christos __RCSID("$NetBSD: acl_from_text.c,v 1.2 2024/01/20 14:52:48 christos Exp $");
     37  1.1  christos #endif
     38  1.1  christos 
     39  1.1  christos #include "namespace.h"
     40  1.1  christos #include <sys/types.h>
     41  1.1  christos #include <sys/acl.h>
     42  1.1  christos #include <errno.h>
     43  1.1  christos #include <grp.h>
     44  1.1  christos #include <pwd.h>
     45  1.1  christos #include <stdio.h>
     46  1.1  christos #include <stdlib.h>
     47  1.1  christos #include <string.h>
     48  1.1  christos #include <assert.h>
     49  1.1  christos 
     50  1.1  christos #include "acl_support.h"
     51  1.1  christos 
     52  1.1  christos static acl_tag_t acl_string_to_tag(char *tag, char *qualifier);
     53  1.1  christos 
     54  1.1  christos static acl_tag_t
     55  1.1  christos acl_string_to_tag(char *tag, char *qualifier)
     56  1.1  christos {
     57  1.1  christos 
     58  1.1  christos 	if (*qualifier == '\0') {
     59  1.1  christos 		if ((!strcmp(tag, "user")) || (!strcmp(tag, "u"))) {
     60  1.1  christos 			return (ACL_USER_OBJ);
     61  1.1  christos 		} else
     62  1.1  christos 		if ((!strcmp(tag, "group")) || (!strcmp(tag, "g"))) {
     63  1.1  christos 			return (ACL_GROUP_OBJ);
     64  1.1  christos 		} else
     65  1.1  christos 		if ((!strcmp(tag, "mask")) || (!strcmp(tag, "m"))) {
     66  1.1  christos 			return (ACL_MASK);
     67  1.1  christos 		} else
     68  1.1  christos 		if ((!strcmp(tag, "other")) || (!strcmp(tag, "o"))) {
     69  1.1  christos 			return (ACL_OTHER);
     70  1.1  christos 		} else
     71  1.2  christos 			return((acl_tag_t)-1);
     72  1.1  christos 	} else {
     73  1.1  christos 		if ((!strcmp(tag, "user")) || (!strcmp(tag, "u"))) {
     74  1.1  christos 			return(ACL_USER);
     75  1.1  christos 		} else
     76  1.1  christos 		if ((!strcmp(tag, "group")) || (!strcmp(tag, "g"))) {
     77  1.1  christos 			return(ACL_GROUP);
     78  1.1  christos 		} else
     79  1.2  christos 			return((acl_tag_t)-1);
     80  1.1  christos 	}
     81  1.1  christos }
     82  1.1  christos 
     83  1.1  christos static int
     84  1.1  christos _posix1e_acl_entry_from_text(acl_t aclp, char *entry)
     85  1.1  christos {
     86  1.1  christos 	acl_tag_t	 t;
     87  1.1  christos 	acl_perm_t	 p;
     88  1.1  christos 	char		*tag, *qualifier, *permission;
     89  1.1  christos 	uid_t		 id;
     90  1.1  christos 	int		 error;
     91  1.1  christos 
     92  1.1  christos 	assert(_acl_brand(aclp) == ACL_BRAND_POSIX);
     93  1.1  christos 
     94  1.1  christos 	/* Split into three ':' delimited fields. */
     95  1.1  christos 	tag = strsep(&entry, ":");
     96  1.1  christos 	if (tag == NULL) {
     97  1.1  christos 		errno = EINVAL;
     98  1.1  christos 		return (-1);
     99  1.1  christos 	}
    100  1.1  christos 	tag = string_skip_whitespace(tag);
    101  1.1  christos 	if ((*tag == '\0') && (!entry)) {
    102  1.1  christos 		/*
    103  1.1  christos 		 * Is an entirely comment line, skip to next
    104  1.1  christos 		 * comma.
    105  1.1  christos 		 */
    106  1.1  christos 		return (0);
    107  1.1  christos 	}
    108  1.1  christos 	string_trim_trailing_whitespace(tag);
    109  1.1  christos 
    110  1.1  christos 	qualifier = strsep(&entry, ":");
    111  1.1  christos 	if (qualifier == NULL) {
    112  1.1  christos 		errno = EINVAL;
    113  1.1  christos 		return (-1);
    114  1.1  christos 	}
    115  1.1  christos 	qualifier = string_skip_whitespace(qualifier);
    116  1.1  christos 	string_trim_trailing_whitespace(qualifier);
    117  1.1  christos 
    118  1.1  christos 	permission = strsep(&entry, ":");
    119  1.1  christos 	if (permission == NULL || entry) {
    120  1.1  christos 		errno = EINVAL;
    121  1.1  christos 		return (-1);
    122  1.1  christos 	}
    123  1.1  christos 	permission = string_skip_whitespace(permission);
    124  1.1  christos 	string_trim_trailing_whitespace(permission);
    125  1.1  christos 
    126  1.1  christos 	t = acl_string_to_tag(tag, qualifier);
    127  1.1  christos 	if (t == (acl_tag_t)-1) {
    128  1.1  christos 		errno = EINVAL;
    129  1.1  christos 		return (-1);
    130  1.1  christos 	}
    131  1.1  christos 
    132  1.1  christos 	error = _posix1e_acl_string_to_perm(permission, &p);
    133  1.1  christos 	if (error == -1) {
    134  1.1  christos 		errno = EINVAL;
    135  1.1  christos 		return (-1);
    136  1.1  christos 	}
    137  1.1  christos 
    138  1.1  christos 	switch(t) {
    139  1.1  christos 		case ACL_USER_OBJ:
    140  1.1  christos 		case ACL_GROUP_OBJ:
    141  1.1  christos 		case ACL_MASK:
    142  1.1  christos 		case ACL_OTHER:
    143  1.1  christos 			if (*qualifier != '\0') {
    144  1.1  christos 				errno = EINVAL;
    145  1.1  christos 				return (-1);
    146  1.1  christos 			}
    147  1.1  christos 			id = 0;
    148  1.1  christos 			break;
    149  1.1  christos 
    150  1.1  christos 		case ACL_USER:
    151  1.1  christos 		case ACL_GROUP:
    152  1.1  christos 			error = _acl_name_to_id(t, qualifier, &id);
    153  1.1  christos 			if (error == -1)
    154  1.1  christos 				return (-1);
    155  1.1  christos 			break;
    156  1.1  christos 
    157  1.1  christos 		default:
    158  1.1  christos 			errno = EINVAL;
    159  1.1  christos 			return (-1);
    160  1.1  christos 	}
    161  1.1  christos 
    162  1.1  christos 	error = _posix1e_acl_add_entry(aclp, t, id, p);
    163  1.1  christos 	if (error == -1)
    164  1.1  christos 		return (-1);
    165  1.1  christos 
    166  1.1  christos 	return (0);
    167  1.1  christos }
    168  1.1  christos 
    169  1.1  christos static int
    170  1.1  christos _text_is_nfs4_entry(const char *entry)
    171  1.1  christos {
    172  1.1  christos 	int count = 0;
    173  1.1  christos 
    174  1.1  christos 	assert(strlen(entry) > 0);
    175  1.1  christos 
    176  1.1  christos 	while (*entry != '\0') {
    177  1.1  christos 		if (*entry == ':' || *entry == '@')
    178  1.1  christos 			count++;
    179  1.1  christos 		entry++;
    180  1.1  christos 	}
    181  1.1  christos 
    182  1.1  christos 	if (count <= 2)
    183  1.1  christos 		return (0);
    184  1.1  christos 
    185  1.1  christos 	return (1);
    186  1.1  christos }
    187  1.1  christos 
    188  1.1  christos /*
    189  1.1  christos  * acl_from_text -- Convert a string into an ACL.
    190  1.1  christos  * Postpone most validity checking until the end and call acl_valid() to do
    191  1.1  christos  * that.
    192  1.1  christos  */
    193  1.1  christos acl_t
    194  1.1  christos acl_from_text(const char *buf_p)
    195  1.1  christos {
    196  1.1  christos 	acl_t		 acl;
    197  1.1  christos 	char		*mybuf_p, *line, *cur, *notcomment, *comment, *entry;
    198  1.1  christos 	int		 error;
    199  1.1  christos 
    200  1.1  christos 	/* Local copy we can mess up. */
    201  1.1  christos 	mybuf_p = strdup(buf_p);
    202  1.1  christos 	if (mybuf_p == NULL)
    203  1.1  christos 		return(NULL);
    204  1.1  christos 
    205  1.1  christos 	acl = acl_init(3); /* XXX: WTF, 3? */
    206  1.1  christos 	if (acl == NULL) {
    207  1.1  christos 		free(mybuf_p);
    208  1.1  christos 		return(NULL);
    209  1.1  christos 	}
    210  1.1  christos 
    211  1.1  christos 	/* Outer loop: delimit at \n boundaries. */
    212  1.1  christos 	cur = mybuf_p;
    213  1.1  christos 	while ((line = strsep(&cur, "\n"))) {
    214  1.1  christos 		/* Now split the line on the first # to strip out comments. */
    215  1.1  christos 		comment = line;
    216  1.1  christos 		notcomment = strsep(&comment, "#");
    217  1.1  christos 
    218  1.1  christos 		/* Inner loop: delimit at ',' boundaries. */
    219  1.1  christos 		while ((entry = strsep(&notcomment, ","))) {
    220  1.1  christos 
    221  1.1  christos 			/* Skip empty lines. */
    222  1.1  christos 			if (strlen(string_skip_whitespace(entry)) == 0)
    223  1.1  christos 				continue;
    224  1.1  christos 
    225  1.1  christos 			if (_acl_brand(acl) == ACL_BRAND_UNKNOWN) {
    226  1.1  christos 				if (_text_is_nfs4_entry(entry))
    227  1.1  christos 					_acl_brand_as(acl, ACL_BRAND_NFS4);
    228  1.1  christos 				else
    229  1.1  christos 					_acl_brand_as(acl, ACL_BRAND_POSIX);
    230  1.1  christos 			}
    231  1.1  christos 
    232  1.1  christos 			switch (_acl_brand(acl)) {
    233  1.1  christos 			case ACL_BRAND_NFS4:
    234  1.1  christos 				error = _nfs4_acl_entry_from_text(acl, entry);
    235  1.1  christos 				break;
    236  1.1  christos 
    237  1.1  christos 			case ACL_BRAND_POSIX:
    238  1.1  christos 				error = _posix1e_acl_entry_from_text(acl, entry);
    239  1.1  christos 				break;
    240  1.1  christos 
    241  1.1  christos 			default:
    242  1.1  christos 				error = EINVAL;
    243  1.1  christos 				break;
    244  1.1  christos 			}
    245  1.1  christos 
    246  1.1  christos 			if (error)
    247  1.1  christos 				goto error_label;
    248  1.1  christos 		}
    249  1.1  christos 	}
    250  1.1  christos 
    251  1.1  christos #if 0
    252  1.1  christos 	/* XXX Should we only return ACLs valid according to acl_valid? */
    253  1.1  christos 	/* Verify validity of the ACL we read in. */
    254  1.1  christos 	if (acl_valid(acl) == -1) {
    255  1.1  christos 		errno = EINVAL;
    256  1.1  christos 		goto error_label;
    257  1.1  christos 	}
    258  1.1  christos #endif
    259  1.1  christos 
    260  1.1  christos 	free(mybuf_p);
    261  1.1  christos 	return(acl);
    262  1.1  christos 
    263  1.1  christos error_label:
    264  1.1  christos 	acl_free(acl);
    265  1.1  christos 	free(mybuf_p);
    266  1.1  christos 	return(NULL);
    267  1.1  christos }
    268  1.1  christos 
    269  1.1  christos /*
    270  1.1  christos  * Given a username/groupname from a text form of an ACL, return the uid/gid
    271  1.1  christos  * XXX NOT THREAD SAFE, RELIES ON GETPWNAM, GETGRNAM
    272  1.1  christos  * XXX USES *PW* AND *GR* WHICH ARE STATEFUL AND THEREFORE THIS ROUTINE
    273  1.1  christos  * MAY HAVE SIDE-EFFECTS
    274  1.1  christos  */
    275  1.1  christos int
    276  1.1  christos _acl_name_to_id(acl_tag_t tag, char *name, uid_t *id)
    277  1.1  christos {
    278  1.1  christos 	struct group	*g;
    279  1.1  christos 	struct passwd	*p;
    280  1.1  christos 	unsigned long	l;
    281  1.1  christos 	char 		*endp;
    282  1.1  christos 
    283  1.1  christos 	switch(tag) {
    284  1.1  christos 	case ACL_USER:
    285  1.1  christos 		p = getpwnam(name);
    286  1.1  christos 		if (p == NULL) {
    287  1.1  christos 			l = strtoul(name, &endp, 0);
    288  1.1  christos 			if (*endp != '\0' || l != (unsigned long)(uid_t)l) {
    289  1.1  christos 				errno = EINVAL;
    290  1.1  christos 				return (-1);
    291  1.1  christos 			}
    292  1.1  christos 			*id = (uid_t)l;
    293  1.1  christos 			return (0);
    294  1.1  christos 		}
    295  1.1  christos 		*id = p->pw_uid;
    296  1.1  christos 		return (0);
    297  1.1  christos 
    298  1.1  christos 	case ACL_GROUP:
    299  1.1  christos 		g = getgrnam(name);
    300  1.1  christos 		if (g == NULL) {
    301  1.1  christos 			l = strtoul(name, &endp, 0);
    302  1.1  christos 			if (*endp != '\0' || l != (unsigned long)(gid_t)l) {
    303  1.1  christos 				errno = EINVAL;
    304  1.1  christos 				return (-1);
    305  1.1  christos 			}
    306  1.1  christos 			*id = (gid_t)l;
    307  1.1  christos 			return (0);
    308  1.1  christos 		}
    309  1.1  christos 		*id = g->gr_gid;
    310  1.1  christos 		return (0);
    311  1.1  christos 
    312  1.1  christos 	default:
    313  1.1  christos 		return (EINVAL);
    314  1.1  christos 	}
    315  1.1  christos }
    316