1 # Copyright 1999-2015 Gentoo Foundation
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 # We can't use libcap-ng atm due to #471414.
37 DEPEND="filecaps? ( sys-libs/libcap )"
39 # @ECLASS-VARIABLE: FILECAPS
42 # An array of fcap arguments to use to automatically execute fcaps. See that
43 # function for more details.
45 # All args are consumed until the '--' marker is found. So if you have:
47 # FILECAPS=( moo cow -- fat cat -- chubby penguin )
50 # This will end up executing:
54 # fcaps chubby penguin
57 # Note: If you override pkg_postinst, you must call fcaps_pkg_postinst yourself.
60 # @USAGE: [-o <owner>] [-g <group>] [-m <mode>] [-M <caps mode>] <capabilities> <file[s]>
62 # Sets the specified capabilities on the specified files.
64 # The caps option takes the form as expected by the cap_from_text(3) man page.
65 # If no action is specified, then "=ep" will be used as a default.
67 # If the file is a relative path (e.g. bin/foo rather than /bin/foo), then the
68 # appropriate path var ($D/$ROOT/etc...) will be prefixed based on the current
71 # The caps mode (default 711) is used to set the permission on the file if
72 # capabilities were properly set on the file.
74 # If the system is unable to set capabilities, it will use the specified user,
75 # group, and mode (presumably to make the binary set*id). The defaults there
76 # are root:0 and 4711. Otherwise, the ownership and permissions will be
79 debug-print-function ${FUNCNAME} "$@"
81 # Process the user options first.
87 while [[ $# -gt 0 ]] ; do
92 -M) caps_mode=$2; shift;;
98 [[ $# -lt 2 ]] && die "${FUNCNAME}: wrong arg count"
101 [[ ${caps} == *[-=+]* ]] || caps+="=ep"
105 case ${EBUILD_PHASE} in
106 compile|install|preinst)
110 root=${EROOT:-${ROOT}}
114 # Process every file!
117 [[ ${file} != /* ]] && file="${root}${file}"
119 if use filecaps ; then
120 # Try to set capabilities. Ignore errors when the
121 # fs doesn't support it, but abort on all others.
122 debug-print "${FUNCNAME}: setting caps '${caps}' on '${file}'"
124 # If everything goes well, we don't want the file to be readable
126 chmod ${caps_mode} "${file}" || die
128 # Set/verify funcs for sys-libs/libcap.
129 _libcap() { setcap "${caps}" "${file}" ; }
130 _libcap_verify() { setcap -v "${caps}" "${file}" >/dev/null ; }
132 # Set/verify funcs for sys-libs/libcap-ng.
133 # Note: filecap only supports =ep mode.
134 # It also expects a different form:
135 # setcap cap_foo,cap_bar
138 local caps=",${caps%=ep}"
139 filecap "${file}" "${caps//,cap_}"
141 _libcap_ng_verify() {
142 # libcap-ng has a crappy interface
143 local rcaps icaps caps=",${caps%=ep}"
144 rcaps=$(filecap "${file}" | \
146 -e "s:^.{${#file}} +::" \
150 [[ ${PIPESTATUS[0]} -eq 0 ]] || return 1
151 icaps=$(echo "${caps//,cap_}" | LC_ALL=C sort)
152 [[ ${rcaps} == ${icaps} ]]
155 local out cmd notfound=0
156 for cmd in _libcap _libcap_ng ; do
157 if ! out=$(LC_ALL=C ${cmd} 2>&1) ; then
159 *"command not found"*)
163 # ENOTSUP and EOPNOTSUPP might be the same value which means
164 # strerror() on them is unstable -- we can get both. #559608
166 *"Operation not supported"*)
167 local fstype=$(stat -f -c %T "${file}")
168 ewarn "Could not set caps on '${file}' due to missing filesystem support:"
169 ewarn "* enable XATTR support for '${fstype}' in your kernel (if configurable)"
170 ewarn "* mount the fs with the user_xattr option (if not the default)"
171 ewarn "* enable the relevant FS_SECURITY option (if configurable)"
175 eerror "Setting caps '${caps}' on file '${file}' failed:"
177 die "could not set caps"
181 # Sanity check that everything took.
182 ${cmd}_verify || die "Checking caps '${caps}' on '${file}' failed"
184 # Everything worked. Move on to the next file.
188 if [[ ${notfound} -eq 2 ]] && [[ -z ${_FCAPS_WARNED} ]] ; then
190 ewarn "Could not find cap utils; make sure libcap or libcap-ng is available."
194 # If we're still here, setcaps failed.
195 debug-print "${FUNCNAME}: setting owner/mode on '${file}'"
196 chown "${owner}:${group}" "${file}" || die
197 chmod ${mode} "${file}" || die
201 # @FUNCTION: fcaps_pkg_postinst
203 # Process the FILECAPS array.
204 fcaps_pkg_postinst() {
206 for arg in "${FILECAPS[@]}" "--" ; do
207 if [[ ${arg} == "--" ]] ; then
216 EXPORT_FUNCTIONS pkg_postinst