summaryrefslogtreecommitdiffstats
path: root/support/misc/nfsd_path.c
blob: c3dea4f029838ef8248d2ed1cf685837cedeaac2 (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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>

#include "conffile.h"
#include "xmalloc.h"
#include "xlog.h"
#include "xstat.h"
#include "nfslib.h"
#include "nfsd_path.h"
#include "workqueue.h"

static struct xthread_workqueue *nfsd_wq;

static int
nfsd_path_isslash(const char *path)
{
	return path[0] == '/' && path[1] == '/';
}

static int
nfsd_path_isdot(const char *path)
{
	return path[0] == '.' && path[1] == '/';
}

static const char *
nfsd_path_strip(const char *path)
{
	if (!path || *path == '\0')
		goto out;
	for (;;) {
		if (nfsd_path_isslash(path)) {
			path++;
			continue;
		}
		if (nfsd_path_isdot(path)) {
			path += 2;
			continue;
		}
		break;
	}
out:
	return path;
}

const char *
nfsd_path_nfsd_rootdir(void)
{
	const char *rootdir;

	rootdir = nfsd_path_strip(conf_get_str("exports", "rootdir"));
	if (!rootdir || rootdir[0] ==  '\0')
		return NULL;
	if (rootdir[0] == '/' && rootdir[1] == '\0')
		return NULL;
	return rootdir;
}

char *
nfsd_path_strip_root(char *pathname)
{
	char buffer[PATH_MAX];
	const char *dir = nfsd_path_nfsd_rootdir();

	if (!dir)
		goto out;

	if (realpath(dir, buffer))
		return strstr(pathname, buffer) == pathname ?
			pathname + strlen(buffer) : NULL;

	xlog(D_GENERAL, "%s: failed to resolve path %s: %m", __func__, dir);
out:
	return pathname;
}

char *
nfsd_path_prepend_dir(const char *dir, const char *pathname)
{
	size_t len, dirlen;
	char *ret;

	dirlen = strlen(dir);
	while (dirlen > 0 && dir[dirlen - 1] == '/')
		dirlen--;
	if (!dirlen)
		return NULL;
	while (pathname[0] == '/')
		pathname++;
	len = dirlen + strlen(pathname) + 1;
	ret = xmalloc(len + 1);
	snprintf(ret, len+1, "%.*s/%s", (int)dirlen, dir, pathname);
	return ret;
}

static void
nfsd_setup_workqueue(void)
{
	const char *rootdir = nfsd_path_nfsd_rootdir();

	if (!rootdir)
		return;

	nfsd_wq = xthread_workqueue_alloc();
	if (!nfsd_wq)
		return;
	xthread_workqueue_chroot(nfsd_wq, rootdir);
}

void
nfsd_path_init(void)
{
	nfsd_setup_workqueue();
}

struct nfsd_stat_data {
	const char *pathname;
	struct stat *statbuf;
	int ret;
	int err;
};

static void
nfsd_statfunc(void *data)
{
	struct nfsd_stat_data *d = data;

	d->ret = xstat(d->pathname, d->statbuf);
	if (d->ret < 0)
		d->err = errno;
}

static void
nfsd_lstatfunc(void *data)
{
	struct nfsd_stat_data *d = data;

	d->ret = xlstat(d->pathname, d->statbuf);
	if (d->ret < 0)
		d->err = errno;
}

static int
nfsd_run_stat(struct xthread_workqueue *wq,
		void (*func)(void *),
		const char *pathname,
		struct stat *statbuf)
{
	struct nfsd_stat_data data = {
		pathname,
		statbuf,
		0,
		0
	};
	xthread_work_run_sync(wq, func, &data);
	if (data.ret < 0)
		errno = data.err;
	return data.ret;
}

int
nfsd_path_stat(const char *pathname, struct stat *statbuf)
{
	if (!nfsd_wq)
		return xstat(pathname, statbuf);
	return nfsd_run_stat(nfsd_wq, nfsd_statfunc, pathname, statbuf);
}

int
nfsd_path_lstat(const char *pathname, struct stat *statbuf)
{
	if (!nfsd_wq)
		return xlstat(pathname, statbuf);
	return nfsd_run_stat(nfsd_wq, nfsd_lstatfunc, pathname, statbuf);
}

struct nfsd_statfs_data {
	const char *pathname;
	struct statfs *statbuf;
	int ret;
	int err;
};

static void
nfsd_statfsfunc(void *data)
{
	struct nfsd_statfs_data *d = data;

	d->ret = statfs(d->pathname, d->statbuf);
	if (d->ret < 0)
		d->err = errno;
}

static int
nfsd_run_statfs(struct xthread_workqueue *wq,
		  const char *pathname,
		  struct statfs *statbuf)
{
	struct nfsd_statfs_data data = {
		pathname,
		statbuf,
		0,
		0
	};
	xthread_work_run_sync(wq, nfsd_statfsfunc, &data);
	if (data.ret < 0)
		errno = data.err;
	return data.ret;
}

int
nfsd_path_statfs(const char *pathname, struct statfs *statbuf)
{
	if (!nfsd_wq)
		return statfs(pathname, statbuf);
	return nfsd_run_statfs(nfsd_wq, pathname, statbuf);
}

struct nfsd_realpath_data {
	const char *pathname;
	char *resolved;
	int err;
};

static void
nfsd_realpathfunc(void *data)
{
	struct nfsd_realpath_data *d = data;

	d->resolved = realpath(d->pathname, d->resolved);
	if (!d->resolved)
		d->err = errno;
}

char *
nfsd_realpath(const char *path, char *resolved_path)
{
	struct nfsd_realpath_data data = {
		path,
		resolved_path,
		0
	};

	if (!nfsd_wq)
		return realpath(path, resolved_path);

	xthread_work_run_sync(nfsd_wq, nfsd_realpathfunc, &data);
	if (!data.resolved)
		errno = data.err;
	return data.resolved;
}

struct nfsd_read_data {
	int fd;
	char *buf;
	size_t len;
	ssize_t ret;
	int err;
};

static void
nfsd_readfunc(void *data)
{
	struct nfsd_read_data *d = data;

	d->ret = read(d->fd, d->buf, d->len);
	if (d->ret < 0)
		d->err = errno;
}

static ssize_t
nfsd_run_read(struct xthread_workqueue *wq, int fd, char *buf, size_t len)
{
	struct nfsd_read_data data = {
		fd,
		buf,
		len,
		0,
		0
	};
	xthread_work_run_sync(wq, nfsd_readfunc, &data);
	if (data.ret < 0)
		errno = data.err;
	return data.ret;
}

ssize_t
nfsd_path_read(int fd, char *buf, size_t len)
{
	if (!nfsd_wq)
		return read(fd, buf, len);
	return nfsd_run_read(nfsd_wq, fd, buf, len);
}

struct nfsd_write_data {
	int fd;
	const char *buf;
	size_t len;
	ssize_t ret;
	int err;
};

static void
nfsd_writefunc(void *data)
{
	struct nfsd_write_data *d = data;

	d->ret = write(d->fd, d->buf, d->len);
	if (d->ret < 0)
		d->err = errno;
}

static ssize_t
nfsd_run_write(struct xthread_workqueue *wq, int fd, const char *buf, size_t len)
{
	struct nfsd_write_data data = {
		fd,
		buf,
		len,
		0,
		0
	};
	xthread_work_run_sync(wq, nfsd_writefunc, &data);
	if (data.ret < 0)
		errno = data.err;
	return data.ret;
}

ssize_t
nfsd_path_write(int fd, const char *buf, size_t len)
{
	if (!nfsd_wq)
		return write(fd, buf, len);
	return nfsd_run_write(nfsd_wq, fd, buf, len);
}

#if defined(HAVE_NAME_TO_HANDLE_AT)
struct nfsd_handle_data {
	int fd;
	const char *path;
	struct file_handle *fh;
	int *mount_id;
	int flags;
	int ret;
	int err;
};

static void
nfsd_name_to_handle_func(void *data)
{
	struct nfsd_handle_data *d = data;

	d->ret = name_to_handle_at(d->fd, d->path,
			d->fh, d->mount_id, d->flags);
	if (d->ret < 0)
		d->err = errno;
}

static int
nfsd_run_name_to_handle_at(struct xthread_workqueue *wq,
		int fd, const char *path, struct file_handle *fh,
		int *mount_id, int flags)
{
	struct nfsd_handle_data data = {
		fd,
		path,
		fh,
		mount_id,
		flags,
		0,
		0
	};

	xthread_work_run_sync(wq, nfsd_name_to_handle_func, &data);
	if (data.ret < 0)
		errno = data.err;
	return data.ret;
}

int
nfsd_name_to_handle_at(int fd, const char *path, struct file_handle *fh,
		int *mount_id, int flags)
{
	if (!nfsd_wq)
		return name_to_handle_at(fd, path, fh, mount_id, flags);

	return nfsd_run_name_to_handle_at(nfsd_wq, fd, path, fh,
			mount_id, flags);
}
#else
int
nfsd_name_to_handle_at(int UNUSED(fd), const char *UNUSED(path),
		struct file_handle *UNUSED(fh),
		int *UNUSED(mount_id), int UNUSED(flags))
{
	errno = ENOSYS;
	return -1;
}
#endif