summaryrefslogtreecommitdiff
path: root/utils/filecap.c
blob: b6f4e2d84110f23d84c71054a9b10fea8e77c466 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * filecap.c - A program that lists running processes with capabilities
 * Copyright (c) 2009-10,2012,2017,2020 Red Hat Inc.
 * 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., 51 Franklin Street, Fifth Floor
 * Boston, MA 02110-1335, 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"
#include <fcntl.h>
#include <ftw.h>

#ifndef FTW_CONTINUE
#define FTW_CONTINUE 0
#endif


static int show_all = 0, header = 0, capabilities = 0, cremove = 0;
static int single_file = 0;

static void usage(void)
{
	fprintf(stderr,
	    "usage: filecap [-a | -d | /dir | /dir/file [cap1 cap2 ...] ]\n");
	exit(1);
}

// Returns 1 on error and 0 otherwise
static int check_file(const char *fpath,
		const struct stat *sb,
		int typeflag_unused __attribute__ ((unused)),
		struct FTW *s_unused __attribute__ ((unused)))
{
	int ret = FTW_CONTINUE;

	if (S_ISREG(sb->st_mode) == 0)
		return ret;

	int fd = open(fpath, O_RDONLY|O_CLOEXEC);
	if (fd >= 0) {
		capng_results_t rc;
		int permitted = 0;

		capng_clear(CAPNG_SELECT_BOTH);
		if (capng_get_caps_fd(fd) < 0 && errno != ENODATA) {
			fprintf(stderr,
				"Unable to get capabilities of %s: %s\n",
				fpath, strerror(errno));
			if (single_file)
				ret = 1;
		}
		rc = capng_have_capabilities(CAPNG_SELECT_CAPS);
		if (rc == CAPNG_NONE) {
			permitted = 1;
			rc = capng_have_permitted_capabilities();
		}
		if (rc > CAPNG_NONE) {
			if (header == 0) {
				header = 1;
				printf("%-9s %-20s capabilities  rootid\n",
				       "set", "file");
			}

			int rootid = capng_get_rootid();
			printf("%s %s    ",
			       permitted ? "permitted" : "effective",  fpath);

			if (rc == CAPNG_FULL)
				printf("full");
			else
				capng_print_caps_text(CAPNG_PRINT_STDOUT,
						CAPNG_PERMITTED);

			if (rootid != CAPNG_UNSET_ROOTID)
				printf(" %d", rootid);
			printf("\n");
		}
		close(fd);
	}
	return ret;
}


// 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) && !defined(HAVE_SYS_XATTR_H))
	fprintf(stderr, "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, rc = 0;

	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) {
				int j;
				for (j=0; j<=CAP_LAST_CAP; j++) {
					const char *n =
						capng_capability_to_name(j);
					if (n == NULL)
						n = "unknown";
					printf("%s\n", n);
				}
				return 0;
			} else if (argv[i][0] == '/') {
				if (lstat(argv[i], &sbuf) != 0) {
					fprintf(stderr,
						"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 {
					fprintf(stderr,
						"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 {
					fprintf(stderr,
						"Unrecognized capability.\n");
					usage();
				}
			}
		}
	}
	if (path == NULL && dir == NULL && show_all == 0) {
		path_env = getenv("PATH");
		if (path_env != NULL) {
			path = strdup(path_env);
			if (!path)
				return 1;
			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
		single_file = 1;
		rc = check_file(path, &sbuf, 0, NULL);
	} else if (path && capabilities == 1) {
		// Write capabilities to file
		int fd = open(path, O_WRONLY|O_NOFOLLOW|O_CLOEXEC);
		if (fd < 0) {
			fprintf(stderr,
				"Could not open %s for writing (%s)\n", path,
				strerror(errno));
			return 1;
		}
		if (capng_apply_caps_fd(fd) < 0) {
			fprintf(stderr,
				"Could not set capabilities on %s: %s\n",
				path, strerror(errno));
			rc = 1;
		}
		close(fd);
	}
#endif
	return rc;
}