summaryrefslogtreecommitdiff
path: root/netfs/daemon/daemonVolume.cpp
blob: ff75e06be30b457735a4921dd648bfe4434ed671 (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
#include "daemonVolume.h"
#include "daemon.h"
#include "daemonDirectory.h"
#include "daemonFile.h"
#include "modeCheck.h"
#include <Ice/ObjectAdapter.h>
#include <boost/algorithm/string/predicate.hpp>
#include <cerrno>
#include <climits>
#include <defaultMapper.h>
#include <entCache.h>
#include <fcntl.h>
#include <map>
#include <sys/stat.h>
#include <unistd.h>

extern std::map<Ice::Int, int> files;

VolumeServer::VolumeServer(
		const std::filesystem::path & r, const EntryResolverPtr<User> & u, const EntryResolverPtr<Group> & g) :
	root(std::filesystem::canonical(r)),
	userLookup(*u), groupLookup(*g), converter(std::make_shared<NetFS::Mapping::DefaultMapper>(u, g))
{
}

void
VolumeServer::disconnect(const Ice::Current & ice)
{
	ice.adapter->removeAllFacets(ice.id);
}

Ice::Int
VolumeServer::access(const NetFS::ReqEnv re, std::string path, Ice::Int mode, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	struct stat s {
	};
	std::filesystem::path p(resolvePath(std::move(path)));
	if (::stat(p.c_str(), &s) != 0) {
		return errno;
	}
	if (mode == F_OK) {
		// stat succeeded, file must exist
		return 0;
	}
	mc.AssertReadParent(p);
	if (mode & R_OK && !mc.ReadableBy(s, mc.myu, mc.myg)) {
		return EACCES;
	}
	if (mode & W_OK && !mc.WritableBy(s, mc.myu, mc.myg)) {
		return EACCES;
	}
	if (mode & X_OK && !mc.ExecutableBy(s, mc.myu, mc.myg)) {
		return EACCES;
	}
	return 0;
}

NetFS::Attr
VolumeServer::getattr(const NetFS::ReqEnv re, std::string path, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	struct stat s {
	};
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertReadParent(p);
	if (::lstat(p.c_str(), &s) != 0) {
		throw NetFS::SystemError(errno);
	}
	return converter.convert(s);
}

void
VolumeServer::mknod(const NetFS::ReqEnv re, std::string path, Ice::Int mode, Ice::Int dev, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertWriteParent(p);
	if (::mknod(p.c_str(), mode, dev) != 0) {
		throw NetFS::SystemError(errno);
	}
}

void
VolumeServer::symlink(const NetFS::ReqEnv re, const std::string path1, std::string path2, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path2)));
	mc.AssertWriteParent(p);
	if (::symlink(path1.c_str(), p.c_str()) != 0) {
		throw NetFS::SystemError(errno);
	}
	if (::lchown(p.c_str(), mc.myu, mc.myg) != 0) {
		::unlink(p.c_str());
		throw NetFS::SystemError(errno);
	}
}

void
VolumeServer::link(const NetFS::ReqEnv re, std::string path1, std::string path2, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p1(resolvePath(std::move(path1)));
	std::filesystem::path p2(resolvePath(std::move(path2)));
	if (::link(p1.c_str(), p2.c_str()) != 0) {
		throw NetFS::SystemError(errno);
	}
	if (::chown(p2.c_str(), mc.myu, mc.myg) != 0) {
		::unlink(p2.c_str());
		throw NetFS::SystemError(errno);
	}
}

void
VolumeServer::rename(const NetFS::ReqEnv re, std::string from, std::string to, const Ice::optional<Ice::Int> flags,
		const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path t(resolvePath(std::move(to)));
	if (flags && *flags == RENAME_NOREPLACE && ::access(t.c_str(), F_OK) == 0) {
		throw NetFS::SystemError(EEXIST);
	}
	std::filesystem::path f(resolvePath(std::move(from)));
	mc.AssertWriteParent(f);
	mc.AssertWriteParent(t);
	if (::rename(f.c_str(), t.c_str()) != 0) {
		throw NetFS::SystemError(errno);
	}
}

std::string
VolumeServer::readlink(const NetFS::ReqEnv re, std::string path, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::string buf(PATH_MAX, 0);
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertRead(p);
	ssize_t rc = ::readlink(p.c_str(), buf.data(), PATH_MAX);
	if (rc == -1) {
		throw NetFS::SystemError(errno);
	}
	buf.resize(rc);
	return buf;
}

void
VolumeServer::chmod(const NetFS::ReqEnv re, std::string path, Ice::Int mode, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertWritePerms(p);
	if (::chmod(p.c_str(), mode) != 0) {
		throw NetFS::SystemError(errno);
	}
}

void
VolumeServer::chown(const NetFS::ReqEnv re, std::string path, Ice::Int uid, Ice::Int gid, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertWrite(p);
	if (::lchown(p.c_str(), uid, gid) != 0) {
		throw NetFS::SystemError(errno);
	}
}

void
VolumeServer::utimens(const NetFS::ReqEnv re, std::string path, Ice::Long s0, Ice::Long ns0, Ice::Long s1,
		Ice::Long ns1, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::array<struct timespec, 2> times {{{s0, ns0}, {s1, ns1}}};
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertWrite(p);
	if (::utimensat(0, p.c_str(), times.data(), AT_SYMLINK_NOFOLLOW) != 0) {
		throw NetFS::SystemError(errno);
	}
}

NetFS::VFS
VolumeServer::statfs(const NetFS::ReqEnv re, std::string path, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	struct statvfs s {
	};
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertRead(p);
	if (::statvfs(p.c_str(), &s) != 0) {
		throw NetFS::SystemError(errno);
	}
	return converter.TypeConverter::convert(s);
}

void
VolumeServer::truncate(const NetFS::ReqEnv re, std::string path, Ice::Long size, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertWrite(p);
	if (::truncate(p.c_str(), size) != 0) {
		throw NetFS::SystemError(errno);
	}
}

void
VolumeServer::unlink(const NetFS::ReqEnv re, std::string path, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertWriteParent(p);
	if (::unlink(p.c_str()) != 0) {
		throw NetFS::SystemError(errno);
	}
}

NetFS::FilePrxPtr
VolumeServer::open(const NetFS::ReqEnv re, std::string path, Ice::Int flags, const Ice::Current & ice)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertRead(p);
	if (flags & O_CREAT) {
		throw NetFS::SystemError(EINVAL);
	}
	int fd = ::open(p.c_str(), flags);
	if (fd == -1) {
		throw NetFS::SystemError(errno);
	}
	return Ice::uncheckedCast<NetFS::FilePrx>(
			ice.adapter->addFacetWithUUID(std::make_shared<FileServer>(fd, converter), "v01"));
}

NetFS::FilePrxPtr
VolumeServer::create(const NetFS::ReqEnv re, std::string path, Ice::Int flags, Ice::Int mode, const Ice::Current & ice)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertWriteParent(p);
	int fd = ::open(p.c_str(), O_CREAT | O_EXCL | flags, mode);
	if (fd == -1) {
		throw NetFS::SystemError(errno);
	}
	if (fchown(fd, mc.myu, mc.myg) != 0) {
		::close(fd);
		::unlink(p.c_str());
		throw NetFS::SystemError(errno);
	}
	return Ice::uncheckedCast<NetFS::FilePrx>(
			ice.adapter->addFacetWithUUID(std::make_shared<FileServer>(fd, converter), "v01"));
}

NetFS::DirectoryPrxPtr
VolumeServer::opendir(const NetFS::ReqEnv re, std::string path, const Ice::Current & ice)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertRead(p);
	DIR * od = ::opendir(p.c_str());
	if (!od) {
		throw NetFS::SystemError(errno);
	}
	return Ice::uncheckedCast<NetFS::DirectoryPrx>(
			ice.adapter->addWithUUID(std::make_shared<DirectoryServer>(od, converter)));
}

void
VolumeServer::mkdir(const NetFS::ReqEnv re, std::string path, Ice::Int mode, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertWriteParent(p);
	if (::mkdir(p.c_str(), mode) != 0) {
		throw NetFS::SystemError(errno);
	}
	if (::chown(p.c_str(), mc.myu, mc.myg) != 0) {
		::rmdir(p.c_str());
		throw NetFS::SystemError(errno);
	}
}

void
VolumeServer::rmdir(const NetFS::ReqEnv re, std::string path, const Ice::Current &)
{
	ModeCheck mc(re, root, userLookup, groupLookup);
	errno = 0;
	std::filesystem::path p(resolvePath(std::move(path)));
	mc.AssertWriteParent(p);
	if (::rmdir(p.c_str()) != 0) {
		throw NetFS::SystemError(errno);
	}
}

std::filesystem::path
normalizedAppend(std::filesystem::path out, const std::filesystem::path && in)
{
	unsigned int depth = 0;
	for (const auto & e : in) {
		if (e.empty() || e == "." || e == "/") {
			continue;
		}
		else if (e == "..") {
			if (depth == 0) {
				throw NetFS::SystemError(EPERM);
			}
			out.remove_filename();
			depth -= 1;
		}
		else {
			out /= e;
			depth += 1;
		}
	}
	return out;
}

std::filesystem::path
VolumeServer::resolvePath(std::string && path) const
{
	return normalizedAppend(root, std::move(path));
}