File: cap_file.c

package info (click to toggle)
libcap2 0.cvs.20010529-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 496 kB
  • ctags: 240
  • sloc: ansic: 1,606; makefile: 147
file content (236 lines) | stat: -rw-r--r-- 4,837 bytes parent folder | download | duplicates (2)
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
226
227
228
229
230
231
232
233
234
235
236
/*
 * $Id: cap_file.c,v 1.1.1.1.4.3 2001/01/16 07:38:25 agmorgan Exp $
 *
 * Copyright (c) 1997-2000 Andrew G Morgan <morgan@linux.kernel.org>
 *
 * This file deals with setting capabilities on files.
 */

#include "libcap.h"

#if (_LINUX_CAPABILITY_VERSION == 0x19980330)

/* This kernel does not support filesystem capabilities later ones do */

cap_t cap_get_fd(int fildes)
{
    _cap_debug("compilation does not support filecaps");
    errno = ENOSYS;
    return NULL;
}

cap_t cap_get_file(const char *filename)
{
    _cap_debug("compilation does not support filecaps");
    errno = ENOSYS;
    return NULL;
}

int cap_set_fd(int fildes, cap_t cap_d)
{
    _cap_debug("compilation does not support filecaps");
    errno = ENOSYS;
    return -1;
}

int cap_set_file(const char *filename, cap_t cap_d)
{
    _cap_debug("compilation does not support filecaps");
    errno = ENOSYS;
    return -1;
}

#else /* ie. (_LINUX_CAPABILITY_VERSION != 0x19980330) */

/*
 * attempt to have defined behavior when running with an older kernel
 */

static int _libcap_capfile(cap_t cap_d)
{
    cap_d->head.version = _libcap_kernel_version;

    _cap_debug("version: %x, features: %x",
	       cap_d->head.version, cap_d->features);

    if (cap_d->features & CAP_FEATURE_FILE) {
	_cap_debug("ok");
	return 0;
    } else {
	_cap_debug("library compiled with a kernel not supporting filecaps");
	errno = ENOSYS;
	return -1;
    }
}

/*
 * Get the capabilities of an open file, as specified by its file
 * descriptor.
 */

cap_t cap_get_fd(int fildes)
{
    int retval;
    cap_t result;

    /* allocate a new capability set */
    result = cap_init();
    if (result == NULL) {
	_cap_debug("failed to allocate a result");
	return NULL;
    }

    if (_libcap_capfile(result)) {
	_cap_debug("running kernel has no support for filecaps");
	goto fail;
    }

    result->head.type = CAP_USERHEADER_FILEDES;
    result->head.u.fd = fildes;

    retval = capget(&result->head, &result->set);

    result->head.type = 0;
    result->head.u.pid = 0;

    if (!retval) {
	if (_libcap_kernel_features & CAP_FEATURE_TO_32) {
	    if (_libcap_n1_to_n2(result, 1, __CAP_BLKS)) {
		goto fail;
	    }
	}
	return result;
    }

fail:
    cap_free(result);
    result = NULL;

    return NULL;
}

/*
 * Set the capabilities on a named file.
 */

cap_t cap_get_file(const char *filename)
{
    int retval;
    cap_t result;

    /* allocate a new capability set */
    result = cap_init();
    if (result == NULL) {
	_cap_debug("failed to allocate a result");
	return NULL;
    }

    if (_libcap_capfile(result)) {
	_cap_debug("running kernel has no support for filecaps");
	goto fail;
    }

    result->head.type = CAP_USERHEADER_FILE;
    result->head.u.file = filename;

    retval = capget(&result->head, &result->set);

    result->head.type = 0;
    result->head.u.pid = 0;

    if (!retval) {
	if (_libcap_kernel_features & CAP_FEATURE_TO_32) {
	    if (_libcap_n1_to_n2(result, 1, __CAP_BLKS)) {
		goto fail;
	    }
	}
	return result;
    }

fail:
    cap_free(result);
    result = NULL;

    return NULL;
}

/*
 * Set the capabilities of an open file, as specified by its file
 * descriptor.
 */

int cap_set_fd(int fildes, cap_t cap_d)
{
    int retval;

    if (!good_cap_t(cap_d)) {
	errno = EINVAL;
	return -1;
    }

    if (_libcap_capfile(cap_d)) {
	_cap_debug("running kernel has no support for filecaps");
	return -1;
    }

    cap_d->head.type = CAP_USERHEADER_FILEDES;
    cap_d->head.u.fd = fildes;

    if (_libcap_kernel_features & CAP_FEATURE_TO_32) {
	retval = _libcap_n1_to_n2(cap_d, __CAP_BLKS, 1);
    }
    
    retval = capset(&cap_d->head, &cap_d->set);
    _cap_debug("capset returned %d", retval);

    if (_libcap_kernel_features & CAP_FEATURE_TO_32) {
	(void) _libcap_n1_to_n2(cap_d, 1, __CAP_BLKS);
    }

    cap_d->head.type = 0;
    cap_d->head.u.pid = 0;

    return retval;
}

/*
 * Set the capabilities of a named file.
 */

int cap_set_file(const char *filename, cap_t cap_d)
{
    int retval;

    if (!good_cap_t(cap_d)) {
	errno = EINVAL;
	return -1;
    }

    if (_libcap_capfile(cap_d)) {
	_cap_debug("running kernel has no support for filecaps");
	return -1;
    }

    cap_d->head.type = CAP_USERHEADER_FILE;
    cap_d->head.u.file = filename;

    if (_libcap_kernel_features & CAP_FEATURE_TO_32) {
	retval = _libcap_n1_to_n2(cap_d, __CAP_BLKS, 1);
    }
    
    _cap_debug("capset version 0x%x", cap_d->head.version);
    _cap_debug("capset type %d", cap_d->head.type);
    retval = capset(&cap_d->head, &cap_d->set);
    _cap_debug("capset returned %d", retval);

    if (_libcap_kernel_features & CAP_FEATURE_TO_32) {
	(void) _libcap_n1_to_n2(cap_d, 1, __CAP_BLKS);
    }

    cap_d->head.type = 0;
    cap_d->head.u.pid = 0;

    return retval;
}

#endif /* (_LINUX_CAPABILITY_VERSION == 0x19980330) */