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
|
/*
* filecap.c - A program that lists running processes with capabilities
* Copyright (c) 2009-10 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved.
*
* This software may be freely redistributed and/or modified under the
* terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, 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; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Authors:
* Steve Grubb <sgrubb@redhat.com>
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "cap-ng.h"
#define __USE_GNU 1
#include <fcntl.h>
#define __USE_XOPEN_EXTENDED 1
#include <ftw.h>
static int show_all = 0, header = 0, capabilities = 0, cremove = 0;
static void usage(void)
{
fprintf(stderr,
"usage: filecap [-a | -d | /dir | /dir/file [cap1 cap2 ...] ]\n");
exit(1);
}
static int check_file(const char *fpath,
const struct stat *sb,
int typeflag_unused __attribute__ ((unused)),
struct FTW *s_unused __attribute__ ((unused)))
{
if (S_ISREG(sb->st_mode) == 0)
return FTW_CONTINUE;
int fd = open(fpath, O_RDONLY);
if (fd >= 0) {
capng_results_t rc;
capng_clear(CAPNG_SELECT_BOTH);
capng_get_caps_fd(fd);
rc = capng_have_capabilities(CAPNG_SELECT_CAPS);
if (rc > CAPNG_NONE) {
if (header == 0) {
header = 1;
printf("%-20s capabilities\n", "file");
}
printf("%s ", fpath);
if (rc == CAPNG_FULL)
printf("full");
else
capng_print_caps_text(CAPNG_PRINT_STDOUT,
CAPNG_PERMITTED);
printf("\n");
}
close(fd);
}
return FTW_CONTINUE;
}
// Use cases:
// filecap
// filecap -a
// filecap /path/dir
// filecap /path/file
// filecap /path/file capability1 capability2 capability 3 ...
//
int main(int argc, char *argv[])
{
#if CAP_LAST_CAP < 31 || !defined (VFS_CAP_U32) || !defined (HAVE_ATTR_XATTR_H)
printf("File based capabilities are not supported\n");
#else
char *path_env, *path = NULL, *dir = NULL;
struct stat sbuf;
int nftw_flags = FTW_PHYS;
int i;
if (argc >1) {
for (i=1; i<argc; i++) {
if (strcmp(argv[i], "-a") == 0) {
show_all = 1;
if (argc != 2)
usage();
} else if (strcmp(argv[i], "-d") == 0) {
for (i=0; i<=CAP_LAST_CAP; i++) {
printf("%s\n",
capng_capability_to_name(i));
}
return 0;
} else if (argv[i][0] == '/') {
if (lstat(argv[i], &sbuf) != 0) {
printf("Error checking path %s (%s)\n",
argv[i], strerror(errno));
exit(1);
}
// Clear all capabilities in case cap strings
// follow. If we get a second file we err out
// so this is safe
if (S_ISREG(sbuf.st_mode) && path == NULL &&
dir == NULL) {
path = argv[i];
capng_clear(CAPNG_SELECT_BOTH);
} else if (S_ISDIR(sbuf.st_mode) && path == NULL
&& dir == NULL)
dir = argv[i];
else {
printf("Must be one regular file or "
"directory\n");
exit(1);
}
} else {
int cap = capng_name_to_capability(argv[i]);
if (cap >= 0) {
if (path == NULL)
usage();
capng_update(CAPNG_ADD,
CAPNG_PERMITTED|CAPNG_EFFECTIVE,
cap);
capabilities = 1;
} else if (strcmp("none", argv[i]) == 0) {
capng_clear(CAPNG_SELECT_BOTH);
capabilities = 1;
cremove = 1;
} else {
printf("Unrecognized capability.\n");
usage();
}
}
}
}
if (path == NULL && dir == NULL && show_all == 0) {
path_env = getenv("PATH");
if (path_env != NULL) {
path = strdup(path_env);
for (dir=strtok(path,":"); dir!=NULL;
dir=strtok(NULL,":")) {
nftw(dir, check_file, 1024, nftw_flags);
}
free(path);
}
} else if (path == NULL && dir == NULL && show_all == 1) {
// Find files
nftw("/", check_file, 1024, nftw_flags);
} else if (dir) {
// Print out the dir
nftw(dir, check_file, 1024, nftw_flags);
}else if (path && capabilities == 0) {
// Print out specific file
check_file(path, &sbuf, 0, NULL);
} else if (path && capabilities == 1) {
// Write capabilities to file
int fd = open(path, O_WRONLY|O_NOFOLLOW);
if (fd < 0) {
printf("Could not open %s for writing (%s)\n", path,
strerror(errno));
return 1;
}
capng_apply_caps_fd(fd);
close(fd);
}
#endif
return 0;
}
|