Home | History | Annotate | Line # | Download | only in posix1e
acl_from_text_nfs4.c revision 1.1
      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) 2008, 2009 Edward Tomasz Napieraa <trasz (at) FreeBSD.org>
      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 #include <sys/cdefs.h>
     30  1.1  christos #if 0
     31  1.1  christos __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_from_text_nfs4.c 326193 2017-11-25 17:12:48Z pfg $");
     32  1.1  christos #else
     33  1.1  christos __RCSID("$NetBSD: acl_from_text_nfs4.c,v 1.1 2020/05/16 18:31:47 christos Exp $");
     34  1.1  christos #endif
     35  1.1  christos 
     36  1.1  christos #include <stdio.h>
     37  1.1  christos #include <stdlib.h>
     38  1.1  christos #include <unistd.h>
     39  1.1  christos #include <errno.h>
     40  1.1  christos #include <assert.h>
     41  1.1  christos #include <string.h>
     42  1.1  christos #include <pwd.h>
     43  1.1  christos #include <grp.h>
     44  1.1  christos #include <ctype.h>
     45  1.1  christos #include <err.h>
     46  1.1  christos #include <sys/syscall.h>
     47  1.1  christos #include <sys/types.h>
     48  1.1  christos #include <sys/acl.h>
     49  1.1  christos 
     50  1.1  christos #include "acl_support.h"
     51  1.1  christos 
     52  1.1  christos #define MAX_ENTRY_LENGTH 512
     53  1.1  christos 
     54  1.1  christos /*
     55  1.1  christos  * Parse the tag field of ACL entry passed as "str".  If qualifier
     56  1.1  christos  * needs to follow, then the variable referenced by "need_qualifier"
     57  1.1  christos  * is set to 1, otherwise it's set to 0.
     58  1.1  christos  */
     59  1.1  christos static int
     60  1.1  christos parse_tag(const char *str, acl_entry_t entry, int *need_qualifier)
     61  1.1  christos {
     62  1.1  christos 
     63  1.1  christos 	assert(need_qualifier != NULL);
     64  1.1  christos 	*need_qualifier = 0;
     65  1.1  christos 
     66  1.1  christos 	if (strcmp(str, "owner@") == 0)
     67  1.1  christos 		return (acl_set_tag_type(entry, ACL_USER_OBJ));
     68  1.1  christos 	if (strcmp(str, "group@") == 0)
     69  1.1  christos 		return (acl_set_tag_type(entry, ACL_GROUP_OBJ));
     70  1.1  christos 	if (strcmp(str, "everyone@") == 0)
     71  1.1  christos 		return (acl_set_tag_type(entry, ACL_EVERYONE));
     72  1.1  christos 
     73  1.1  christos 	*need_qualifier = 1;
     74  1.1  christos 
     75  1.1  christos 	if (strcmp(str, "user") == 0 || strcmp(str, "u") == 0)
     76  1.1  christos 		return (acl_set_tag_type(entry, ACL_USER));
     77  1.1  christos 	if (strcmp(str, "group") == 0 || strcmp(str, "g") == 0)
     78  1.1  christos 		return (acl_set_tag_type(entry, ACL_GROUP));
     79  1.1  christos 
     80  1.1  christos 	warnx("malformed ACL: invalid \"tag\" field");
     81  1.1  christos 
     82  1.1  christos 	return (-1);
     83  1.1  christos }
     84  1.1  christos 
     85  1.1  christos /*
     86  1.1  christos  * Parse the qualifier field of ACL entry passed as "str".
     87  1.1  christos  * If user or group name cannot be resolved, then the variable
     88  1.1  christos  * referenced by "need_qualifier" is set to 1; it will be checked
     89  1.1  christos  * later to figure out whether the appended_id is required.
     90  1.1  christos  */
     91  1.1  christos static int
     92  1.1  christos parse_qualifier(char *str, acl_entry_t entry, int *need_qualifier)
     93  1.1  christos {
     94  1.1  christos 	int qualifier_length, error;
     95  1.1  christos 	uid_t id;
     96  1.1  christos 	acl_tag_t tag;
     97  1.1  christos 
     98  1.1  christos 	assert(need_qualifier != NULL);
     99  1.1  christos 	*need_qualifier = 0;
    100  1.1  christos 
    101  1.1  christos 	qualifier_length = strlen(str);
    102  1.1  christos 
    103  1.1  christos 	if (qualifier_length == 0) {
    104  1.1  christos 		warnx("malformed ACL: empty \"qualifier\" field");
    105  1.1  christos 		return (-1);
    106  1.1  christos 	}
    107  1.1  christos 
    108  1.1  christos 	error = acl_get_tag_type(entry, &tag);
    109  1.1  christos 	if (error)
    110  1.1  christos 		return (error);
    111  1.1  christos 
    112  1.1  christos 	error = _acl_name_to_id(tag, str, &id);
    113  1.1  christos 	if (error) {
    114  1.1  christos 		*need_qualifier = 1;
    115  1.1  christos 		return (0);
    116  1.1  christos 	}
    117  1.1  christos 
    118  1.1  christos 	return (acl_set_qualifier(entry, &id));
    119  1.1  christos }
    120  1.1  christos 
    121  1.1  christos static int
    122  1.1  christos parse_access_mask(char *str, acl_entry_t entry)
    123  1.1  christos {
    124  1.1  christos 	int error;
    125  1.1  christos 	acl_perm_t perm;
    126  1.1  christos 
    127  1.1  christos 	error = _nfs4_parse_access_mask(str, &perm);
    128  1.1  christos 	if (error)
    129  1.1  christos 		return (error);
    130  1.1  christos 
    131  1.1  christos 	error = acl_set_permset(entry, &perm);
    132  1.1  christos 
    133  1.1  christos 	return (error);
    134  1.1  christos }
    135  1.1  christos 
    136  1.1  christos static int
    137  1.1  christos parse_flags(char *str, acl_entry_t entry)
    138  1.1  christos {
    139  1.1  christos 	int error;
    140  1.1  christos 	acl_flag_t flags;
    141  1.1  christos 
    142  1.1  christos 	error = _nfs4_parse_flags(str, &flags);
    143  1.1  christos 	if (error)
    144  1.1  christos 		return (error);
    145  1.1  christos 
    146  1.1  christos 	error = acl_set_flagset_np(entry, &flags);
    147  1.1  christos 
    148  1.1  christos 	return (error);
    149  1.1  christos }
    150  1.1  christos 
    151  1.1  christos static int
    152  1.1  christos parse_entry_type(const char *str, acl_entry_t entry)
    153  1.1  christos {
    154  1.1  christos 
    155  1.1  christos 	if (strcmp(str, "allow") == 0)
    156  1.1  christos 		return (acl_set_entry_type_np(entry, ACL_ENTRY_TYPE_ALLOW));
    157  1.1  christos 	if (strcmp(str, "deny") == 0)
    158  1.1  christos 		return (acl_set_entry_type_np(entry, ACL_ENTRY_TYPE_DENY));
    159  1.1  christos 	if (strcmp(str, "audit") == 0)
    160  1.1  christos 		return (acl_set_entry_type_np(entry, ACL_ENTRY_TYPE_AUDIT));
    161  1.1  christos 	if (strcmp(str, "alarm") == 0)
    162  1.1  christos 		return (acl_set_entry_type_np(entry, ACL_ENTRY_TYPE_ALARM));
    163  1.1  christos 
    164  1.1  christos 	warnx("malformed ACL: invalid \"type\" field");
    165  1.1  christos 
    166  1.1  christos 	return (-1);
    167  1.1  christos }
    168  1.1  christos 
    169  1.1  christos static int
    170  1.1  christos parse_appended_id(char *str, acl_entry_t entry)
    171  1.1  christos {
    172  1.1  christos 	int qualifier_length;
    173  1.1  christos 	char *end;
    174  1.1  christos 	id_t id;
    175  1.1  christos 
    176  1.1  christos 	qualifier_length = strlen(str);
    177  1.1  christos 	if (qualifier_length == 0) {
    178  1.1  christos 		warnx("malformed ACL: \"appended id\" field present, "
    179  1.1  christos 	           "but empty");
    180  1.1  christos 		return (-1);
    181  1.1  christos 	}
    182  1.1  christos 
    183  1.1  christos 	id = strtod(str, &end);
    184  1.1  christos 	if (end - str != qualifier_length) {
    185  1.1  christos 		warnx("malformed ACL: appended id is not a number");
    186  1.1  christos 		return (-1);
    187  1.1  christos 	}
    188  1.1  christos 
    189  1.1  christos 	return (acl_set_qualifier(entry, &id));
    190  1.1  christos }
    191  1.1  christos 
    192  1.1  christos static int
    193  1.1  christos number_of_colons(const char *str)
    194  1.1  christos {
    195  1.1  christos 	int count = 0;
    196  1.1  christos 
    197  1.1  christos 	while (*str != '\0') {
    198  1.1  christos 		if (*str == ':')
    199  1.1  christos 			count++;
    200  1.1  christos 
    201  1.1  christos 		str++;
    202  1.1  christos 	}
    203  1.1  christos 
    204  1.1  christos 	return (count);
    205  1.1  christos }
    206  1.1  christos 
    207  1.1  christos int
    208  1.1  christos _nfs4_acl_entry_from_text(acl_t aclp, char *str)
    209  1.1  christos {
    210  1.1  christos 	int error, need_qualifier;
    211  1.1  christos 	acl_entry_t entry;
    212  1.1  christos 	char *field, *qualifier_field = NULL;
    213  1.1  christos 
    214  1.1  christos 	error = acl_create_entry(&aclp, &entry);
    215  1.1  christos 	if (error)
    216  1.1  christos 		return (error);
    217  1.1  christos 
    218  1.1  christos 	assert(_entry_brand(entry) == ACL_BRAND_NFS4);
    219  1.1  christos 
    220  1.1  christos 	if (str == NULL)
    221  1.1  christos 		goto truncated_entry;
    222  1.1  christos 	field = strsep(&str, ":");
    223  1.1  christos 
    224  1.1  christos 	field = string_skip_whitespace(field);
    225  1.1  christos 	if ((*field == '\0') && (!str)) {
    226  1.1  christos 		/*
    227  1.1  christos 		 * Is an entirely comment line, skip to next
    228  1.1  christos 		 * comma.
    229  1.1  christos 		 */
    230  1.1  christos 		return (0);
    231  1.1  christos 	}
    232  1.1  christos 
    233  1.1  christos 	error = parse_tag(field, entry, &need_qualifier);
    234  1.1  christos 	if (error)
    235  1.1  christos 		goto malformed_field;
    236  1.1  christos 
    237  1.1  christos 	if (need_qualifier) {
    238  1.1  christos 		if (str == NULL)
    239  1.1  christos 			goto truncated_entry;
    240  1.1  christos 		qualifier_field = field = strsep(&str, ":");
    241  1.1  christos 		error = parse_qualifier(field, entry, &need_qualifier);
    242  1.1  christos 		if (error)
    243  1.1  christos 			goto malformed_field;
    244  1.1  christos 	}
    245  1.1  christos 
    246  1.1  christos 	if (str == NULL)
    247  1.1  christos 		goto truncated_entry;
    248  1.1  christos 	field = strsep(&str, ":");
    249  1.1  christos 	error = parse_access_mask(field, entry);
    250  1.1  christos 	if (error)
    251  1.1  christos 		goto malformed_field;
    252  1.1  christos 
    253  1.1  christos 	if (str == NULL)
    254  1.1  christos 		goto truncated_entry;
    255  1.1  christos 	/* Do we have "flags" field? */
    256  1.1  christos 	if (number_of_colons(str) > 0) {
    257  1.1  christos 		field = strsep(&str, ":");
    258  1.1  christos 		error = parse_flags(field, entry);
    259  1.1  christos 		if (error)
    260  1.1  christos 			goto malformed_field;
    261  1.1  christos 	}
    262  1.1  christos 
    263  1.1  christos 	if (str == NULL)
    264  1.1  christos 		goto truncated_entry;
    265  1.1  christos 	field = strsep(&str, ":");
    266  1.1  christos 	error = parse_entry_type(field, entry);
    267  1.1  christos 	if (error)
    268  1.1  christos 		goto malformed_field;
    269  1.1  christos 
    270  1.1  christos 	if (need_qualifier) {
    271  1.1  christos 		if (str == NULL) {
    272  1.1  christos 			warnx("malformed ACL: unknown user or group name "
    273  1.1  christos 			    "\"%s\"", qualifier_field);
    274  1.1  christos 			goto truncated_entry;
    275  1.1  christos 		}
    276  1.1  christos 
    277  1.1  christos 		error = parse_appended_id(str, entry);
    278  1.1  christos 		if (error)
    279  1.1  christos 			goto malformed_field;
    280  1.1  christos 	}
    281  1.1  christos 
    282  1.1  christos 	return (0);
    283  1.1  christos 
    284  1.1  christos truncated_entry:
    285  1.1  christos malformed_field:
    286  1.1  christos 	acl_delete_entry(aclp, entry);
    287  1.1  christos 	errno = EINVAL;
    288  1.1  christos 	return (-1);
    289  1.1  christos }
    290