Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/util/fchmodat.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2019-2020 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <sys/stat.h>
22
23
#include <errno.h>
24
#include <fcntl.h>
25
#include <unistd.h>
26
27
#include <sudo_compat.h>
28
29
#ifndef HAVE_FCHMODAT
30
int
31
sudo_fchmodat(int dfd, const char *path, mode_t mode, int flag)
32
{
33
int odfd, ret = -1;
34
35
if (ISSET(flag, AT_SYMLINK_NOFOLLOW)) {
36
errno = ENOTSUP;
37
return -1;
38
}
39
40
if (dfd == (int)AT_FDCWD)
41
return chmod(path, mode);
42
43
/* Save cwd (cannot use O_PATH on older Linux kernels). */
44
if ((odfd = open(".", O_RDONLY|O_DIRECTORY)) == -1)
45
goto done;
46
47
if (fchdir(dfd) == -1)
48
goto done;
49
50
ret = chmod(path, mode);
51
52
/* Restore cwd */
53
if (fchdir(odfd) == -1) {
54
/* Should not happen */
55
ret = -1;
56
}
57
58
done:
59
if (odfd != -1)
60
close(odfd);
61
62
return ret;
63
}
64
#endif /* HAVE_FCHMODAT */
65
66