summaryrefslogtreecommitdiff
path: root/common.attr
blob: 6e2c004bc0cbee14d37201d26a349ddcce0d8598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
##/bin/bash
#-----------------------------------------------------------------------
#  Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
#  USA
#
#  Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
#  Mountain View, CA 94043, USA, or: http://www.sgi.com
#-----------------------------------------------------------------------
# common extended attribute and ACL support

# pick three unused user/group ids, store them as $acl[1-3]
#
_acl_setup_ids()
{
    eval `(_cat_passwd; _cat_group) | awk -F: '
      { ids[$3]=1 }
      END {
        j=1
        for(i=1; i<1000000 && j<=3;i++){
          if (! (i in ids)) {
	     printf "acl%d=%d;", j, i;		 
	     j++
          }
        }	
      }'`
}

# filter for the acl ids selected above
#
_acl_filter_id()
{
    sed \
       -e "s/u:$acl1/u:id1/" \
       -e "s/u:$acl2/u:id2/" \
       -e "s/u:$acl3/u:id3/" \
       -e "s/g:$acl1/g:id1/" \
       -e "s/g:$acl2/g:id2/" \
       -e "s/g:$acl3/g:id3/" \
       -e "s/ $acl1 / id1 /" \
       -e "s/ $acl2 / id2 /" \
       -e "s/ $acl3 / id3 /"
}

# filtered ls
#
_acl_ls()
{
    _ls_l -n $* | awk '{ print $1, $3, $4, $NF }' | _acl_filter_id
} 

#
_acl_list()
{
    _file1=$1

    if [ $HOSTOS = "IRIX" ]; then
	ls -dD $_file1 | _acl_filter_id
    else 
        chacl -l $_file1 | _acl_filter_id
    fi
}

# create an ACL with n ACEs in it
#
_create_n_aces()
{
    let n=$1-4
    acl='u::rwx,g::rwx,o::rwx,m::rwx' # 4 ace acl start
    while [ $n -ne 0 ]; do
	acl="$acl,u:$n:rwx"
	let n=$n-1
    done
    echo $acl
}

# filter user ace names to user ids
#
_filter_aces()
{
    tmp_file=`mktemp /tmp/ace.XXXXXX`

    (_cat_passwd; _cat_group) > $tmp_file

    $AWK_PROG -v tmpfile=$tmp_file '
	BEGIN {
	    FS=":"
	    while ( getline <tmpfile > 0 ) {
		idlist[$1] = $3 
	    }
	}
	/^user/ { if ($2 in idlist) sub($2, idlist[$2]); print; next}
	/^u/ { if ($2 in idlist) sub($2, idlist[$2]); print; next}
	/^default:user/ { if ($3 in idlist) sub($3, idlist[$3]); print; next}
	{print}
    '
    rm -f $tmp_file
}

_filter_aces_notypes()
{
    tr '\[' '\012' | tr ']' '\012' | tr ',' '\012' | _filter_aces|\
    sed -e 's/u:/user:/' -e 's/g:/group:/' -e 's/o:/other:/' -e 's/m:/mask:/'
}

_require_acls()
{
    if [ ! -x /bin/chacl -a ! -x /usr/bin/chacl -a ! -x /sbin/chacl ]; then
    	_notrun "chacl command not found"
    fi

    #
    # Test if chacl is able to list ACLs on the target filesystems.  On really
    # old kernels the system calls might not be implemented at all, but the
    # more common case is that the tested filesystem simply doesn't support
    # ACLs.
    #
    touch $TEST_DIR/syscalltest
    chacl -l $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
    cat $TEST_DIR/syscalltest.out >> $here/$seq.full

    if grep -q 'Function not implemented' $TEST_DIR/syscalltest.out; then
      _notrun "kernel does not support ACLs"
    fi
    if grep -q 'Operation not supported' $TEST_DIR/syscalltest.out; then
      _notrun "ACLs not supported by this filesystem type: $FSTYP"
    fi

    rm -f $TEST_DIR/syscalltest.out
}

_list_acl()
{
    file=$1

    ls -dD $file | _acl_filter_id
}

_require_attrs()
{
    [ -n $ATTR_PROG ] || _notrun "attr command not found"
    [ -n $GETFATTR_PROG ] || _notrun "getfattr command not found"
    [ -n $SETFATTR_PROG ] || _notrun "setfattr command not found"

    #
    # Test if chacl is able to write an attribute on the target filesystems.
    # On really old kernels the system calls might not be implemented at all,
    # but the more common case is that the tested filesystem simply doesn't
    # support attributes.  Note that we can't simply list attributes as
    # various security modules generate synthetic attributes not actually
    # stored on disk.
    #
    touch $TEST_DIR/syscalltest
    attr -s "user.xfstests" -V "attr" $TEST_DIR > $TEST_DIR/syscalltest.out 2>&1
    cat $TEST_DIR/syscalltest.out >> $here/$seq.full

    if grep -q 'Function not implemented' $TEST_DIR/syscalltest.out; then
      _notrun "kernel does not support attrs"
    fi
    if grep -q 'Operation not supported' $TEST_DIR/syscalltest.out; then
      _notrun "attrs not supported by this filesystem type: $FSTYP"
    fi

    rm -f $TEST_DIR/syscalltest.out
}

# getfattr -R returns info in readdir order which varies from fs to fs.
# This sorts the output by filename
_sort_getfattr_output()
{
    awk '{a[FNR]=$0}END{n = asort(a); for(i=1; i <= n; i++) print a[i]"\n"}' RS=''
}

# set maximum total attr space based on fs type
if [ "$FSTYP" == "xfs" -o "$FSTYP" == "udf" ]; then
	MAX_ATTRS=1000
else # Assume max ~1 block of attrs
	BLOCK_SIZE=`stat -f $TEST_DIR | grep "Block size" | cut -d " " -f3`
	# user.attribute_XXX="value.XXX" is about 32 bytes; leave some overhead
	let MAX_ATTRS=$BLOCK_SIZE/40
fi

export MAX_ATTRS

# Set max attr value size based on fs type
if [ "$FSTYP" == "xfs" -o "$FSTYP" == "udf" -o "$FSTYP" == "btrfs" ]; then
	MAX_ATTRVAL_SIZE=64
else # Assume max ~1 block of attrs
	BLOCK_SIZE=`stat -f $TEST_DIR | grep "Block size" | cut -d " " -f3`
	# leave a little overhead
	let MAX_ATTRVAL_SIZE=$BLOCK_SIZE-256
fi

export MAX_ATTRVAL_SIZE
# make sure this script returns success
/bin/true