1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: fcaps.eclass
6 # base-system@gentoo.org
7 # @BLURB: function to set POSIX file-based capabilities
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.
13 # Due to probable capability-loss on moving or copying, this happens in
14 # pkg_postinst phase (at least for now).
17 # You can manually set the caps on ping and ping6 by doing:
20 # fcaps cap_net_raw bin/ping bin/ping6
24 # Or set it via the global ebuild var FILECAPS:
27 # cap_net_raw bin/ping bin/ping6
31 if [[ -z ${_FCAPS_ECLASS} ]]; then
36 # Since it is needed in pkg_postinst() it must be in RDEPEND
39 RDEPEND="filecaps? ( sys-libs/libcap )"
42 BDEPEND="filecaps? ( sys-libs/libcap )"
47 # @ECLASS-VARIABLE: FILECAPS
50 # An array of fcap arguments to use to automatically execute fcaps. See that
51 # function for more details.
53 # All args are consumed until the '--' marker is found. So if you have:
55 # FILECAPS=( moo cow -- fat cat -- chubby penguin )
58 # This will end up executing:
62 # fcaps chubby penguin
65 # Note: If you override pkg_postinst, you must call fcaps_pkg_postinst yourself.
68 # @USAGE: [-o <owner>] [-g <group>] [-m <mode>] [-M <caps mode>] <capabilities> <file[s]>
70 # Sets the specified capabilities on the specified files.
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.
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
79 # The caps mode (default 711) is used to set the permission on the file if
80 # capabilities were properly set on the file.
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
87 debug-print-function ${FUNCNAME} "$@"
89 if [[ ${EUID} != 0 ]] ; then
90 einfo "Insufficient privileges to execute ${FUNCNAME}, skipping."
94 # Process the user options first.
100 while [[ $# -gt 0 ]] ; do
102 -o) owner=$2; shift;;
103 -g) group=$2; shift;;
105 -M) caps_mode=$2; shift;;
111 [[ $# -lt 2 ]] && die "${FUNCNAME}: wrong arg count"
114 [[ ${caps} == *[-=+]* ]] || caps+="=ep"
118 case ${EBUILD_PHASE} in
119 compile|install|preinst)
123 root=${EROOT:-${ROOT}}
128 # Process every file!
131 [[ ${file} != /* ]] && file="${root}/${file}"
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}'"
138 # If everything goes well, we don't want the file to be readable
140 chmod ${caps_mode} "${file}" || die
142 if ! out=$(LC_ALL=C setcap "${caps}" "${file}" 2>&1) ; then
144 # ENOTSUP and EOPNOTSUPP might be the same value which means
145 # strerror() on them is unstable -- we can get both. #559608
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)"
155 eerror "Setting caps '${caps}' on file '${file}' failed:"
157 die "could not set caps"
161 # Sanity check that everything took.
162 setcap -v "${caps}" "${file}" >/dev/null \
163 || die "Checking caps '${caps}' on '${file}' failed"
165 # Everything worked. Move on to the next file.
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
177 # @FUNCTION: fcaps_pkg_postinst
179 # Process the FILECAPS array.
180 fcaps_pkg_postinst() {
182 for arg in "${FILECAPS[@]}" "--" ; do
183 if [[ ${arg} == "--" ]] ; then
192 EXPORT_FUNCTIONS pkg_postinst