| 1 |
/* |
| 2 |
* filecap.c - A program that lists running processes with capabilities |
| 3 |
* Copyright (c) 2009-10,2012,2017,2020 Red Hat Inc. |
| 4 |
* All Rights Reserved. |
| 5 |
* |
| 6 |
* This software may be freely redistributed and/or modified under the |
| 7 |
* terms of the GNU General Public License as published by the Free |
| 8 |
* Software Foundation; either version 2, or (at your option) any |
| 9 |
* later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; see the file COPYING. If not, write to the |
| 18 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor |
| 19 |
* Boston, MA 02110-1335, USA. |
| 20 |
* |
| 21 |
* Authors: |
| 22 |
* Steve Grubb <[email protected]> |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <errno.h> |
| 30 |
#include "cap-ng.h" |
| 31 |
#include <fcntl.h> |
| 32 |
#include <ftw.h> |
| 33 |
|
| 34 |
#ifndef FTW_CONTINUE |
| 35 |
#define FTW_CONTINUE 0 |
| 36 |
#endif |
| 37 |
|
| 38 |
|
| 39 |
static int show_all = 0, header = 0, capabilities = 0, cremove = 0; |
| 40 |
static int single_file = 0; |
| 41 |
|
| 42 |
static void usage(void) |
| 43 |
{ |
| 44 |
fprintf(stderr, |
| 45 |
"usage: filecap [-a | -d | /dir | /dir/file [cap1 cap2 ...] ]\n"); |
| 46 |
exit(1); |
| 47 |
} |
| 48 |
|
| 49 |
// Returns 1 on error and 0 otherwise |
| 50 |
static int check_file(const char *fpath, |
| 51 |
const struct stat *sb, |
| 52 |
int typeflag_unused __attribute__ ((unused)), |
| 53 |
struct FTW *s_unused __attribute__ ((unused))) |
| 54 |
{ |
| 55 |
int ret = FTW_CONTINUE; |
| 56 |
|
| 57 |
if (S_ISREG(sb->st_mode) == 0) |
| 58 |
return ret; |
| 59 |
|
| 60 |
int fd = open(fpath, O_RDONLY|O_CLOEXEC); |
| 61 |
if (fd >= 0) { |
| 62 |
capng_results_t rc; |
| 63 |
int permitted = 0; |
| 64 |
|
| 65 |
capng_clear(CAPNG_SELECT_BOTH); |
| 66 |
if (capng_get_caps_fd(fd) < 0 && errno != ENODATA) { |
| 67 |
fprintf(stderr, |
| 68 |
"Unable to get capabilities of %s: %s\n", |
| 69 |
fpath, strerror(errno)); |
| 70 |
if (single_file) |
| 71 |
ret = 1; |
| 72 |
} |
| 73 |
rc = capng_have_capabilities(CAPNG_SELECT_CAPS); |
| 74 |
if (rc == CAPNG_NONE) { |
| 75 |
permitted = 1; |
| 76 |
rc = capng_have_permitted_capabilities(); |
| 77 |
} |
| 78 |
if (rc > CAPNG_NONE) { |
| 79 |
if (header == 0) { |
| 80 |
header = 1; |
| 81 |
printf("%-9s %-22s capabilities rootid\n", |
| 82 |
"set", "file"); |
| 83 |
} |
| 84 |
|
| 85 |
int rootid = capng_get_rootid(); |
| 86 |
printf("%s %-22s ", |
| 87 |
permitted ? "permitted" : "effective", fpath); |
| 88 |
|
| 89 |
if (rc == CAPNG_FULL) |
| 90 |
printf("full"); |
| 91 |
else |
| 92 |
capng_print_caps_text(CAPNG_PRINT_STDOUT, |
| 93 |
CAPNG_PERMITTED); |
| 94 |
|
| 95 |
if (rootid != CAPNG_UNSET_ROOTID) |
| 96 |
printf(" %d", rootid); |
| 97 |
printf("\n"); |
| 98 |
} |
| 99 |
close(fd); |
| 100 |
} |
| 101 |
return ret; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
// Use cases: |
| 106 |
// filecap |
| 107 |
// filecap -a |
| 108 |
// filecap /path/dir |
| 109 |
// filecap /path/file |
| 110 |
// filecap /path/file capability1 capability2 capability 3 ... |
| 111 |
// |
| 112 |
int main(int argc, char *argv[]) |
| 113 |
{ |
| 114 |
#if CAP_LAST_CAP < 31 || !defined (VFS_CAP_U32) || \ |
| 115 |
(!defined (HAVE_ATTR_XATTR_H) && !defined(HAVE_SYS_XATTR_H)) |
| 116 |
fprintf(stderr, "File based capabilities are not supported\n"); |
| 117 |
#else |
| 118 |
char *path_env, *path = NULL, *dir = NULL; |
| 119 |
struct stat sbuf; |
| 120 |
struct stat path_sbuf; |
| 121 |
int nftw_flags = FTW_PHYS; |
| 122 |
int i, rc = 0; |
| 123 |
int have_path_stat = 0; |
| 124 |
|
| 125 |
if (argc >1) { |
| 126 |
for (i=1; i<argc; i++) { |
| 127 |
if (strcmp(argv[i], "-a") == 0) { |
| 128 |
show_all = 1; |
| 129 |
if (argc != 2) |
| 130 |
usage(); |
| 131 |
} else if (strcmp(argv[i], "-d") == 0) { |
| 132 |
int j; |
| 133 |
for (j=0; j<=CAP_LAST_CAP; j++) { |
| 134 |
const char *n = |
| 135 |
capng_capability_to_name(j); |
| 136 |
if (n == NULL) |
| 137 |
n = "unknown"; |
| 138 |
printf("%s\n", n); |
| 139 |
} |
| 140 |
return 0; |
| 141 |
} else if (argv[i][0] == '/') { |
| 142 |
if (lstat(argv[i], &sbuf) != 0) { |
| 143 |
fprintf(stderr, |
| 144 |
"Error checking path %s (%s)\n", |
| 145 |
argv[i], strerror(errno)); |
| 146 |
exit(1); |
| 147 |
} |
| 148 |
// Clear all capabilities in case cap strings |
| 149 |
// follow. If we get a second file we err out |
| 150 |
// so this is safe |
| 151 |
if (S_ISREG(sbuf.st_mode) && path == NULL && |
| 152 |
dir == NULL) { |
| 153 |
path = argv[i]; |
| 154 |
path_sbuf = sbuf; |
| 155 |
have_path_stat = 1; |
| 156 |
capng_clear(CAPNG_SELECT_BOTH); |
| 157 |
} else if (S_ISDIR(sbuf.st_mode) && path == NULL |
| 158 |
&& dir == NULL) |
| 159 |
dir = argv[i]; |
| 160 |
else { |
| 161 |
fprintf(stderr, |
| 162 |
"Must be one regular file or " |
| 163 |
"directory\n"); |
| 164 |
exit(1); |
| 165 |
} |
| 166 |
} else { |
| 167 |
int cap = capng_name_to_capability(argv[i]); |
| 168 |
if (cap >= 0) { |
| 169 |
if (path == NULL) |
| 170 |
usage(); |
| 171 |
capng_update(CAPNG_ADD, |
| 172 |
CAPNG_PERMITTED|CAPNG_EFFECTIVE, |
| 173 |
cap); |
| 174 |
capabilities = 1; |
| 175 |
} else if (strcmp("none", argv[i]) == 0) { |
| 176 |
capng_clear(CAPNG_SELECT_BOTH); |
| 177 |
capabilities = 1; |
| 178 |
cremove = 1; |
| 179 |
} else { |
| 180 |
fprintf(stderr, |
| 181 |
"Unrecognized capability.\n"); |
| 182 |
usage(); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
if (path == NULL && dir == NULL && show_all == 0) { |
| 188 |
path_env = getenv("PATH"); |
| 189 |
if (path_env != NULL) { |
| 190 |
path = strdup(path_env); |
| 191 |
if (!path) |
| 192 |
return 1; |
| 193 |
for (dir=strtok(path,":"); dir!=NULL; |
| 194 |
dir=strtok(NULL,":")) { |
| 195 |
nftw(dir, check_file, 1024, nftw_flags); |
| 196 |
} |
| 197 |
free(path); |
| 198 |
} |
| 199 |
} else if (path == NULL && dir == NULL && show_all == 1) { |
| 200 |
// Find files |
| 201 |
nftw("/", check_file, 1024, nftw_flags); |
| 202 |
} else if (dir) { |
| 203 |
// Print out the dir |
| 204 |
nftw(dir, check_file, 1024, nftw_flags); |
| 205 |
}else if (path && capabilities == 0) { |
| 206 |
// Print out specific file |
| 207 |
single_file = 1; |
| 208 |
rc = check_file(path, &sbuf, 0, NULL); |
| 209 |
} else if (path && capabilities == 1) { |
| 210 |
// Write capabilities to file |
| 211 |
struct stat fbuf; |
| 212 |
int fd = open(path, O_WRONLY|O_NOFOLLOW|O_CLOEXEC); |
| 213 |
if (fd < 0) { |
| 214 |
fprintf(stderr, |
| 215 |
"Could not open %s for writing (%s)\n", path, |
| 216 |
strerror(errno)); |
| 217 |
return 1; |
| 218 |
} |
| 219 |
if (have_path_stat && fstat(fd, &fbuf) == 0 && |
| 220 |
(fbuf.st_dev != path_sbuf.st_dev || |
| 221 |
fbuf.st_ino != path_sbuf.st_ino)) { |
| 222 |
fprintf(stderr, |
| 223 |
"File changed during operation, refusing " |
| 224 |
"to continue\n"); |
| 225 |
close(fd); |
| 226 |
return 1; |
| 227 |
} |
| 228 |
if (capng_apply_caps_fd(fd) < 0) { |
| 229 |
fprintf(stderr, |
| 230 |
"Could not set capabilities on %s: %s\n", |
| 231 |
path, strerror(errno)); |
| 232 |
rc = 1; |
| 233 |
} |
| 234 |
close(fd); |
| 235 |
} |
| 236 |
#endif |
| 237 |
return rc; |
| 238 |
} |