Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/iolog/iolog_mkdirs.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2009-2022 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
#include <stdio.h>
23
#include <stdlib.h>
24
#ifdef HAVE_STDBOOL_H
25
# include <stdbool.h>
26
#else
27
# include <compat/stdbool.h>
28
#endif
29
#include <unistd.h>
30
#include <errno.h>
31
#include <fcntl.h>
32
33
#include <sudo_compat.h>
34
#include <sudo_debug.h>
35
#include <sudo_fatal.h>
36
#include <sudo_gettext.h>
37
#include <sudo_iolog.h>
38
#include <sudo_util.h>
39
40
/*
41
* Create directory and any parent directories as needed.
42
*/
43
bool
44
iolog_mkdirs(const char *path)
45
{
46
const mode_t iolog_filemode = iolog_get_file_mode();
47
const mode_t iolog_dirmode = iolog_get_dir_mode();
48
const uid_t iolog_uid = iolog_get_uid();
49
const gid_t iolog_gid = iolog_get_gid();
50
bool ok = true, uid_changed = false;
51
struct stat sb;
52
mode_t omask;
53
int dfd;
54
debug_decl(iolog_mkdirs, SUDO_DEBUG_UTIL);
55
56
dfd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY);
57
if (dfd == -1 && errno == EACCES) {
58
/* Try again as the I/O log owner (for NFS). */
59
if (iolog_swapids(false)) {
60
dfd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY);
61
if (!iolog_swapids(true)) {
62
ok = false;
63
goto done;
64
}
65
}
66
}
67
if (dfd != -1 && fstat(dfd, &sb) != -1) {
68
if (S_ISDIR(sb.st_mode)) {
69
if (sb.st_uid != iolog_uid || sb.st_gid != iolog_gid) {
70
if (fchown(dfd, iolog_uid, iolog_gid) != 0) {
71
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
72
"%s: unable to chown %d:%d %s", __func__,
73
(int)iolog_uid, (int)iolog_gid, path);
74
}
75
}
76
if ((sb.st_mode & ALLPERMS) != iolog_dirmode) {
77
if (fchmod(dfd, iolog_dirmode) != 0) {
78
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
79
"%s: unable to chmod 0%o %s", __func__,
80
(int)iolog_dirmode, path);
81
}
82
}
83
} else {
84
sudo_warnx(U_("%s exists but is not a directory (0%o)"),
85
path, (unsigned int) sb.st_mode);
86
errno = ENOTDIR;
87
ok = false;
88
}
89
goto done;
90
}
91
92
/* umask must not be more restrictive than the file modes. */
93
omask = umask(ACCESSPERMS & ~(iolog_filemode|iolog_dirmode));
94
95
ok = false;
96
if (dfd != -1)
97
close(dfd);
98
dfd = sudo_open_parent_dir(path, iolog_uid, iolog_gid, iolog_dirmode, true);
99
if (dfd == -1 && errno == EACCES) {
100
/* Try again as the I/O log owner (for NFS). */
101
uid_changed = iolog_swapids(false);
102
if (uid_changed)
103
dfd = sudo_open_parent_dir(path, (uid_t)-1, (gid_t)-1,
104
iolog_dirmode, false);
105
}
106
if (dfd != -1) {
107
/* Create final path component. */
108
const char *base = sudo_basename(path);
109
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
110
"mkdir %s, mode 0%o", path, (unsigned int) iolog_dirmode);
111
ok = mkdirat(dfd, base, iolog_dirmode) == 0 || errno == EEXIST;
112
if (!ok) {
113
if (errno == EACCES && !uid_changed) {
114
/* Try again as the I/O log owner (for NFS). */
115
uid_changed = iolog_swapids(false);
116
if (uid_changed)
117
ok = mkdirat(dfd, base, iolog_dirmode) == 0 || errno == EEXIST;
118
}
119
if (!ok)
120
sudo_warn(U_("unable to mkdir %s"), path);
121
} else {
122
if (fchownat(dfd, base, iolog_uid, iolog_gid, AT_SYMLINK_NOFOLLOW) != 0) {
123
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
124
"%s: unable to chown %d:%d %s", __func__,
125
(int)iolog_uid, (int)iolog_gid, path);
126
}
127
}
128
}
129
if (uid_changed) {
130
if (!iolog_swapids(true))
131
ok = false;
132
}
133
134
umask(omask);
135
136
done:
137
if (dfd != -1)
138
close(dfd);
139
debug_return_bool(ok);
140
}
141
142