sys-libs/libblockdev: Python 3.8 compatibility
[gentoo.git] / eclass / fcaps.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: fcaps.eclass
5 # @MAINTAINER:
6 # base-system@gentoo.org
7 # @BLURB: function to set POSIX file-based capabilities
8 # @DESCRIPTION:
9 # This eclass provides a function to set file-based capabilities on binaries.
10 # This is not the same as USE=caps which controls runtime capability changes,
11 # often via packages like libcap.
12 #
13 # Due to probable capability-loss on moving or copying, this happens in
14 # pkg_postinst phase (at least for now).
15 #
16 # @EXAMPLE:
17 # You can manually set the caps on ping and ping6 by doing:
18 # @CODE
19 # pkg_postinst() {
20 #       fcaps cap_net_raw bin/ping bin/ping6
21 # }
22 # @CODE
23 #
24 # Or set it via the global ebuild var FILECAPS:
25 # @CODE
26 # FILECAPS=(
27 #       cap_net_raw bin/ping bin/ping6
28 # )
29 # @CODE
30
31 if [[ -z ${_FCAPS_ECLASS} ]]; then
32 _FCAPS_ECLASS=1
33
34 IUSE="+filecaps"
35
36 # Since it is needed in pkg_postinst() it must be in RDEPEND
37 case "${EAPI:-0}" in
38         [0-6])
39                 RDEPEND="filecaps? ( sys-libs/libcap )"
40         ;;
41         *)
42                 BDEPEND="filecaps? ( sys-libs/libcap )"
43                 RDEPEND="${BDEPEND}"
44         ;;
45 esac
46
47 # @ECLASS-VARIABLE: FILECAPS
48 # @DEFAULT_UNSET
49 # @DESCRIPTION:
50 # An array of fcap arguments to use to automatically execute fcaps.  See that
51 # function for more details.
52 #
53 # All args are consumed until the '--' marker is found.  So if you have:
54 # @CODE
55 #       FILECAPS=( moo cow -- fat cat -- chubby penguin )
56 # @CODE
57 #
58 # This will end up executing:
59 # @CODE
60 #       fcaps moo cow
61 #       fcaps fat cat
62 #       fcaps chubby penguin
63 # @CODE
64 #
65 # Note: If you override pkg_postinst, you must call fcaps_pkg_postinst yourself.
66
67 # @FUNCTION: fcaps
68 # @USAGE: [-o <owner>] [-g <group>] [-m <mode>] [-M <caps mode>] <capabilities> <file[s]>
69 # @DESCRIPTION:
70 # Sets the specified capabilities on the specified files.
71 #
72 # The caps option takes the form as expected by the cap_from_text(3) man page.
73 # If no action is specified, then "=ep" will be used as a default.
74 #
75 # If the file is a relative path (e.g. bin/foo rather than /bin/foo), then the
76 # appropriate path var ($D/$ROOT/etc...) will be prefixed based on the current
77 # ebuild phase.
78 #
79 # The caps mode (default 711) is used to set the permission on the file if
80 # capabilities were properly set on the file.
81 #
82 # If the system is unable to set capabilities, it will use the specified user,
83 # group, and mode (presumably to make the binary set*id).  The defaults there
84 # are root:0 and 4711.  Otherwise, the ownership and permissions will be
85 # unchanged.
86 fcaps() {
87         debug-print-function ${FUNCNAME} "$@"
88
89         if [[ ${EUID} != 0 ]] ; then
90                 einfo "Insufficient privileges to execute ${FUNCNAME}, skipping."
91                 return 0
92         fi
93
94         # Process the user options first.
95         local owner='root'
96         local group='0'
97         local mode='4711'
98         local caps_mode='711'
99
100         while [[ $# -gt 0 ]] ; do
101                 case $1 in
102                 -o) owner=$2; shift;;
103                 -g) group=$2; shift;;
104                 -m) mode=$2; shift;;
105                 -M) caps_mode=$2; shift;;
106                 *) break;;
107                 esac
108                 shift
109         done
110
111         [[ $# -lt 2 ]] && die "${FUNCNAME}: wrong arg count"
112
113         local caps=$1
114         [[ ${caps} == *[-=+]* ]] || caps+="=ep"
115         shift
116
117         local root
118         case ${EBUILD_PHASE} in
119         compile|install|preinst)
120                 root=${ED:-${D}}
121                 ;;
122         postinst)
123                 root=${EROOT:-${ROOT}}
124                 ;;
125         esac
126         root=${root%/}
127
128         # Process every file!
129         local file
130         for file ; do
131                 [[ ${file} != /* ]] && file="${root}/${file}"
132
133                 if use filecaps ; then
134                         # Try to set capabilities.  Ignore errors when the
135                         # fs doesn't support it, but abort on all others.
136                         debug-print "${FUNCNAME}: setting caps '${caps}' on '${file}'"
137
138                         # If everything goes well, we don't want the file to be readable
139                         # by people.
140                         chmod ${caps_mode} "${file}" || die
141
142                         if ! out=$(LC_ALL=C setcap "${caps}" "${file}" 2>&1) ; then
143                                 case ${out} in
144                                 # ENOTSUP and EOPNOTSUPP might be the same value which means
145                                 # strerror() on them is unstable -- we can get both. #559608
146                                 *"Not supported"*|\
147                                 *"Operation not supported"*)
148                                         local fstype=$(stat -f -c %T "${file}")
149                                         ewarn "Could not set caps on '${file}' due to missing filesystem support:"
150                                         ewarn "* enable XATTR support for '${fstype}' in your kernel (if configurable)"
151                                         ewarn "* mount the fs with the user_xattr option (if not the default)"
152                                         ewarn "* enable the relevant FS_SECURITY option (if configurable)"
153                                         ;;
154                                 *)
155                                         eerror "Setting caps '${caps}' on file '${file}' failed:"
156                                         eerror "${out}"
157                                         die "could not set caps"
158                                         ;;
159                                 esac
160                         else
161                                 # Sanity check that everything took.
162                                 setcap -v "${caps}" "${file}" >/dev/null \
163                                         || die "Checking caps '${caps}' on '${file}' failed"
164
165                                 # Everything worked.  Move on to the next file.
166                                 continue
167                         fi
168                 fi
169
170                 # If we're still here, setcaps failed.
171                 debug-print "${FUNCNAME}: setting owner/mode on '${file}'"
172                 chown "${owner}:${group}" "${file}" || die
173                 chmod ${mode} "${file}" || die
174         done
175 }
176
177 # @FUNCTION: fcaps_pkg_postinst
178 # @DESCRIPTION:
179 # Process the FILECAPS array.
180 fcaps_pkg_postinst() {
181         local arg args=()
182         for arg in "${FILECAPS[@]}" "--" ; do
183                 if [[ ${arg} == "--" ]] ; then
184                         fcaps "${args[@]}"
185                         args=()
186                 else
187                         args+=( "${arg}" )
188                 fi
189         done
190 }
191
192 EXPORT_FUNCTIONS pkg_postinst
193
194 fi