summaryrefslogtreecommitdiffstats
path: root/support/junction/junction.c
blob: c1ec8ff84317942ebcee0b66f51ebef835be662e (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/**
 * @file support/junction/junction.c
 * @brief Common utilities for managing junctions on the local file system
 */

/*
 * Copyright 2010, 2018 Oracle.  All rights reserved.
 *
 * This file is part of nfs-utils.
 *
 * nfs-utils is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2.0 as
 * published by the Free Software Foundation.
 *
 * nfs-utils 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 version 2.0 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2.0 along with nfs-utils.  If not, see:
 *
 *	http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <wchar.h>
#include <memory.h>
#include <signal.h>
#include <errno.h>
#include <dirent.h>

#include <sys/xattr.h>

#include "junction.h"
#include "junction-internal.h"
#include "xlog.h"

/**
 * Open a file system object
 *
 * @param pathname NUL-terminated C string containing pathname of an object
 * @param fd OUT: a file descriptor number is filled in
 * @return a FedFsStatus code
 */
FedFsStatus
junction_open_path(const char *pathname, int *fd)
{
	int tmp;

	if (pathname == NULL || fd == NULL)
		return FEDFS_ERR_INVAL;

	tmp = open(pathname, O_DIRECTORY);
	if (tmp == -1) {
		switch (errno) {
		case EPERM:
			return FEDFS_ERR_ACCESS;
		case EACCES:
			return FEDFS_ERR_PERM;
		default:
			xlog(D_GENERAL, "%s: Failed to open path %s: %m",
				__func__, pathname);
			return FEDFS_ERR_INVAL;
		}
	}

	*fd = tmp;
	return FEDFS_OK;
}

/**
 * Predicate: is object a directory?
 *
 * @param fd an open file descriptor
 * @param path NUL-terminated C string containing pathname of a directory
 * @return a FedFsStatus code
 */
FedFsStatus
junction_is_directory(int fd, const char *path)
{
	struct stat stb;

	if (fstatat(fd, "", &stb, AT_NO_AUTOMOUNT|AT_EMPTY_PATH) == -1) {
		xlog(D_GENERAL, "%s: failed to stat %s: %m",
				__func__, path);
		return FEDFS_ERR_ACCESS;
	}

	if (!S_ISDIR(stb.st_mode)) {
		xlog(D_CALL, "%s: %s is not a directory",
				__func__, path);
		return FEDFS_ERR_INVAL;
	}

	xlog(D_CALL, "%s: %s is a directory", __func__, path);
	return FEDFS_OK;
}

/**
 * Predicate: is a directory's sticky bit set?
 *
 * @param fd an open file descriptor
 * @param path NUL-terminated C string containing pathname of a directory
 * @return a FedFsStatus code
 */
FedFsStatus
junction_is_sticky_bit_set(int fd, const char *path)
{
	struct stat stb;

	if (fstatat(fd, "", &stb, AT_NO_AUTOMOUNT|AT_EMPTY_PATH) == -1) {
		xlog(D_GENERAL, "%s: failed to stat %s: %m",
				__func__, path);
		return FEDFS_ERR_ACCESS;
	}

	if (stb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) {
		xlog(D_CALL, "%s: execute bit set on %s",
				__func__, path);
		return FEDFS_ERR_NOTJUNCT;
	}

	if (!(stb.st_mode & S_ISVTX)) {
		xlog(D_CALL, "%s: sticky bit not set on %s",
				__func__, path);
		return FEDFS_ERR_NOTJUNCT;
	}

	xlog(D_CALL, "%s: sticky bit is set on %s", __func__, path);
	return FEDFS_OK;
}

/**
 * Set just a directory's sticky bit
 *
 * @param fd an open file descriptor
 * @param path NUL-terminated C string containing pathname of a directory
 * @return a FedFsStatus code
 */
FedFsStatus
junction_set_sticky_bit(int fd, const char *path)
{
	struct stat stb;

	if (fstatat(fd, "", &stb, AT_NO_AUTOMOUNT|AT_EMPTY_PATH) == -1) {
		xlog(D_GENERAL, "%s: failed to stat %s: %m",
			__func__, path);
		return FEDFS_ERR_ACCESS;
	}

	stb.st_mode &= (unsigned int)~ALLPERMS;
	stb.st_mode |= S_ISVTX;

	if (fchmod(fd, stb.st_mode) == -1) {
		xlog(D_GENERAL, "%s: failed to set sticky bit on %s: %m",
			__func__, path);
		return FEDFS_ERR_ROFS;
	}

	xlog(D_CALL, "%s: set sticky bit on %s", __func__, path);
	return FEDFS_OK;
}

/**
 * Predicate: does a directory have an xattr named "name"?
 *
 * @param fd an open file descriptor
 * @param path NUL-terminated C string containing pathname of a directory
 * @param name NUL-terminated C string containing name of xattr to check
 * @return a FedFsStatus code
 *
 * @note Access to trusted attributes requires CAP_SYS_ADMIN.
 */
FedFsStatus
junction_is_xattr_present(int fd, const char *path, const char *name)
{
	ssize_t rc;

	/*
	 * Do not assume the total number of extended attributes
	 * this object may have.
	 */
	rc = fgetxattr(fd, name, NULL, 0);
	if (rc == -1) {
		switch (errno) {
		case EPERM:
			xlog(D_CALL, "%s: no access to xattr %s on %s",
				__func__, name, path);
			return FEDFS_ERR_PERM;
		case ENODATA:
			xlog(D_CALL, "%s: no xattr %s present on %s",
				__func__, name, path);
			return FEDFS_ERR_NOTJUNCT;
		default:
			xlog(D_CALL, "%s: xattr %s not found on %s: %m",
				__func__, name, path);
			return FEDFS_ERR_IO;
		}
	}

	xlog(D_CALL, "%s: xattr %s found on %s",
			__func__, name, path);
	return FEDFS_OK;
}

/**
 * Read the contents of xattr "name"
 *
 * @param fd an open file descriptor
 * @param path NUL-terminated C string containing pathname of a directory
 * @param name NUL-terminated C string containing name of xattr to retrieve
 * @param contents OUT: NUL-terminated C string containing contents of xattr
 * @return a FedFsStatus code
 *
 * If junction_read_xattr() returns FEDFS_OK, the caller must free "*contents"
 * with free(3).
 *
 * @note Access to trusted attributes requires CAP_SYS_ADMIN.
 */
FedFsStatus
junction_read_xattr(int fd, const char *path, const char *name, char **contents)
{
	char *xattrbuf = NULL;
	ssize_t len;

	len = fgetxattr(fd, name, xattrbuf, 0);
	if (len < 0) {
		xlog(D_GENERAL, "%s: failed to get size of xattr %s on %s: %m",
			__func__, name, path);
		return FEDFS_ERR_ACCESS;
	}

	xattrbuf = malloc((size_t)len + 1);
	if (xattrbuf == NULL) {
		xlog(D_GENERAL, "%s: failed to get buffer for xattr %s on %s",
			__func__, name, path);
		return FEDFS_ERR_SVRFAULT;
	}

	if (fgetxattr(fd, name, xattrbuf, (size_t)len) == -1) {
		xlog(D_GENERAL, "%s: failed to get xattr %s on %s: %m",
			__func__, name, path);
		free(xattrbuf);
		return FEDFS_ERR_ACCESS;
	}
	xattrbuf[len] = '\0';

	xlog(D_CALL, "%s: read xattr %s from path %s",
			__func__, name, path);
	*contents = xattrbuf;
	return FEDFS_OK;
}

/**
 * Retrieve the contents of xattr "name"
 *
 * @param fd an open file descriptor
 * @param path NUL-terminated C string containing pathname of a directory
 * @param name NUL-terminated C string containing name of xattr to retrieve
 * @param contents OUT: opaque byte array containing contents of xattr
 * @param contentlen OUT: size of "contents"
 * @return a FedFsStatus code
 *
 * If junction_get_xattr() returns FEDFS_OK, the caller must free "*contents"
 * with free(3).
 *
 * @note Access to trusted attributes requires CAP_SYS_ADMIN.
 */
FedFsStatus
junction_get_xattr(int fd, const char *path, const char *name, void **contents,
		size_t *contentlen)
{
	void *xattrbuf = NULL;
	ssize_t len;

	len = fgetxattr(fd, name, xattrbuf, 0);
	if (len < 0) {
		xlog(D_GENERAL, "%s: failed to get size of xattr %s on %s: %m",
			__func__, name, path);
		return FEDFS_ERR_ACCESS;
	}

	xattrbuf = malloc((size_t)len);
	if (xattrbuf == NULL) {
		xlog(D_GENERAL, "%s: failed to get buffer for xattr %s on %s",
			__func__, name, path);
		return FEDFS_ERR_SVRFAULT;
	}

	if (fgetxattr(fd, name, xattrbuf, (size_t)len) == -1) {
		xlog(D_GENERAL, "%s: failed to get xattr %s on %s: %m",
			__func__, name, path);
		free(xattrbuf);
		return FEDFS_ERR_ACCESS;
	}

	xlog(D_CALL, "%s: read xattr %s from path %s",
			__func__, name, path);
	*contents = xattrbuf;
	*contentlen = (size_t)len;
	return FEDFS_OK;
}

/**
 * Update the contents of an xattr
 *
 * @param fd an open file descriptor
 * @param path NUL-terminated C string containing pathname of a directory
 * @param name NUL-terminated C string containing name of xattr to set
 * @param contents opaque byte array containing contents of xattr
 * @param contentlen size of "contents"
 * @return a FedFsStatus code
 *
 * The extended attribute is created if it does not exist.
 * Its contents are replaced if it does.
 *
 * @note Access to trusted attributes requires CAP_SYS_ADMIN.
 */
FedFsStatus
junction_set_xattr(int fd, const char *path, const char *name,
			const void *contents, const size_t contentlen)
{
	/*
	 * XXX: Eventually should distinguish among several errors:
	 *	object isn't there, no root access, some other issue
	 */
	if (fsetxattr(fd, name, contents, contentlen, 0) == -1) {
		xlog(D_GENERAL, "%s: Failed to set xattr %s on %s: %m",
			__func__, name, path);
		return FEDFS_ERR_IO;
	}

	xlog(D_CALL, "%s: Wrote xattr %s from path %s",
			__func__, name, path);
	return FEDFS_OK;
}

/**
 * Remove one xattr
 *
 * @param fd an open file descriptor
 * @param pathname NUL-terminated C string containing pathname of a directory
 * @param name NUL-terminated C string containing name of xattr to set
 * @return a FedFsStatus code
 *
 * @note Access to trusted attributes requires CAP_SYS_ADMIN.
 */
FedFsStatus
junction_remove_xattr(int fd, const char *pathname, const char *name)
{
	/*
	 * XXX: Eventually should distinguish among several errors:
	 *	object isn't there, no root access, some other issue
	 */
	if (fremovexattr(fd, name) == -1) {
		xlog(D_GENERAL, "%s: failed to remove xattr %s from %s: %m",
			__func__, name, pathname);
		return FEDFS_ERR_ACCESS;
	}
	xlog(D_CALL, "%s: removed xattr %s from path %s",
			__func__, name, pathname);
	return FEDFS_OK;
}

/**
 * Retrieve object's mode bits.
 *
 * @param pathname NUL-terminated C string containing pathname of a directory
 * @param mode OUT: mode bits
 * @return a FedFsStatus code
 */
FedFsStatus
junction_get_mode(const char *pathname, mode_t *mode)
{
	FedFsStatus retval;
	struct stat stb;
	int fd;

	retval = junction_open_path(pathname, &fd);
	if (retval != FEDFS_OK)
		return retval;

	if (fstatat(fd, "", &stb, AT_NO_AUTOMOUNT|AT_EMPTY_PATH) == -1) {
		xlog(D_GENERAL, "%s: failed to stat %s: %m",
			__func__, pathname);
		(void)close(fd);
		return FEDFS_ERR_ACCESS;
	}
	(void)close(fd);

	xlog(D_CALL, "%s: pathname %s has mode %o",
		__func__, pathname, stb.st_mode);
	*mode = stb.st_mode;
	return FEDFS_OK;

}

/**
 * Save the object's mode in an xattr.  Saved mode is human-readable.
 *
 * @param pathname NUL-terminated C string containing pathname of a directory
 * @return a FedFsStatus code
 */
FedFsStatus
junction_save_mode(const char *pathname)
{
	FedFsStatus retval;
	mode_t mode;
	char buf[8];
	int fd;

	retval = junction_get_mode(pathname, &mode);
	if (retval != FEDFS_OK)
		return retval;
	(void)snprintf(buf, sizeof(buf), "%o", ALLPERMS & mode);

	retval = junction_open_path(pathname, &fd);
	if (retval != FEDFS_OK)
		return retval;

	retval = junction_set_xattr(fd, pathname, JUNCTION_XATTR_NAME_MODE,
				buf, strlen(buf));
	if (retval != FEDFS_OK)
		goto out;

	retval = junction_set_sticky_bit(fd, pathname);
	if (retval != FEDFS_OK) {
		(void)junction_remove_xattr(fd, pathname,
						JUNCTION_XATTR_NAME_MODE);
		goto out;
	}

	xlog(D_CALL, "%s: saved mode %o to %s", __func__, mode, pathname);
	retval = FEDFS_OK;

out:
	(void)close(fd);
	return retval;

}

/**
 * Restore an object's mode bits
 *
 * @param pathname NUL-terminated C string containing pathname of a directory
 * @return a FedFsStatus code
 */
FedFsStatus
junction_restore_mode(const char *pathname)
{
	FedFsStatus retval;
	char *buf = NULL;
	mode_t mode;
	int fd;

	retval = junction_open_path(pathname, &fd);
	if (retval != FEDFS_OK)
		return retval;

	retval = junction_read_xattr(fd, pathname, JUNCTION_XATTR_NAME_MODE, &buf);
	if (retval != FEDFS_OK)
		goto out;

	retval = FEDFS_ERR_SVRFAULT;
	if (sscanf((char *)buf, "%o", &mode) != 1) {
		xlog(D_GENERAL, "%s: failed to parse saved mode on %s",
			__func__, pathname);
		goto out;
	}

	retval = FEDFS_ERR_ROFS;
	if (fchmod(fd, mode) == -1) {
		xlog(D_GENERAL, "%s: failed to set mode of %s to %o: %m",
			__func__, pathname, mode);
		goto out;
	}

	xlog(D_CALL, "%s: restored mode %o to %s", __func__, mode, pathname);
	retval = FEDFS_OK;

out:
	free(buf);
	(void)close(fd);
	return retval;
}