File: nfsd_path.c

package info (click to toggle)
nfs-utils 1%3A2.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,276 kB
  • sloc: ansic: 52,182; sh: 5,371; python: 2,151; makefile: 971
file content (306 lines) | stat: -rw-r--r-- 6,139 bytes parent folder | download
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
#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 = NULL;

struct nfsd_task_t {
        int             ret;
        void*           data;
};
/* Function used to offload tasks that must be ran within the correct
 * chroot environment.
 */
static void
nfsd_run_task(void (*func)(void*), void* data){
        nfsd_wq ? xthread_work_run_sync(nfsd_wq, func, data) : func(data);
};


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             (*stat_handler)(const char*, struct stat*);
};

static void
nfsd_handle_stat(void *data)
{
        struct nfsd_task_t*     t = data;
	struct nfsd_stat_data*  d = t->data;
        t->ret = d->stat_handler(d->pathname, d->statbuf);
}

static int
nfsd_run_stat(const char *pathname,
	        struct stat *statbuf,
                int (*handler)(const char*, struct stat*))
{
        struct nfsd_task_t      t;
        struct nfsd_stat_data   d = { pathname, statbuf, handler };
        t.data = &d;
        nfsd_run_task(nfsd_handle_stat, &t);
	return t.ret;
}

int
nfsd_path_stat(const char *pathname, struct stat *statbuf)
{
        return nfsd_run_stat(pathname, statbuf, stat);
}

int
nfsd_path_lstat(const char* pathname, struct stat* statbuf){
        return nfsd_run_stat(pathname, statbuf, lstat);
};

int
nfsd_path_statfs(const char* pathname, struct statfs* statbuf)
{
        return nfsd_run_stat(pathname, (struct stat*)statbuf, (int (*)(const char*, struct stat*))statfs);
};

struct nfsd_realpath_t {
        const char*     path;
        char*           resolved_buf;
        char*           res_ptr;
};

static void
nfsd_realpathfunc(void *data)
{
        struct nfsd_realpath_t *d = data;
        d->res_ptr = realpath(d->path, d->resolved_buf);
}

char*
nfsd_realpath(const char *path, char *resolved_buf)
{
        struct nfsd_realpath_t realpath_buf = {
                .path = path,
                .resolved_buf = resolved_buf
        };
        nfsd_run_task(nfsd_realpathfunc, &realpath_buf);
        return realpath_buf.res_ptr;
}

struct nfsd_rw_data {
	int             fd;
	void*           buf;
	size_t          len;
        ssize_t         bytes_read;
};

static void
nfsd_readfunc(void *data)
{
        struct nfsd_rw_data* t = (struct nfsd_rw_data*)data;
        t->bytes_read = read(t->fd, t->buf, t->len);
}

static ssize_t
nfsd_run_read(int fd, void* buf, size_t len)
{
        struct nfsd_rw_data d = { .fd = fd, .buf = buf, .len = len };
        nfsd_run_task(nfsd_readfunc, &d);
	return d.bytes_read;
}

ssize_t
nfsd_path_read(int fd, void* buf, size_t len)
{
	return nfsd_run_read(fd, buf, len);
}

static void
nfsd_writefunc(void *data)
{
	struct nfsd_rw_data* d = data;
	d->bytes_read = write(d->fd, d->buf, d->len);
}

static ssize_t
nfsd_run_write(int fd, void* buf, size_t len)
{
        struct nfsd_rw_data d = { .fd = fd, .buf = buf, .len = len };
        nfsd_run_task(nfsd_writefunc, &d);
	return d.bytes_read;
}

ssize_t
nfsd_path_write(int fd, void* buf, size_t len)
{
	return nfsd_run_write(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;
};

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);
}

static int
nfsd_run_name_to_handle_at(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
	};

	nfsd_run_task(nfsd_name_to_handle_func, &data);
	return data.ret;
}

int
nfsd_name_to_handle_at(int fd, const char *path,
                struct file_handle *fh,
		int *mount_id, int flags)
{
        return nfsd_run_name_to_handle_at(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