summaryrefslogtreecommitdiff
path: root/source3/modules/vfs_fileid.c
blob: d7e9090bc18ca8eae79c7911ef131e51afbbe8f9 (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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
 * VFS module to alter the algorithm to calculate
 * the struct file_id used as key for the share mode
 * and byte range locking db's.
 *
 * Copyright (C) 2007, Stefan Metzmacher
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "smbd/smbd.h"
#include "system/filesys.h"

static int vfs_fileid_debug_level = DBGC_VFS;

#undef DBGC_CLASS
#define DBGC_CLASS vfs_fileid_debug_level

struct fileid_mount_entry {
	SMB_DEV_T device;
	const char *mnt_fsname;
	fsid_t fsid;
	uint64_t devid;
};

struct fileid_handle_data {
	uint64_t (*device_mapping_fn)(struct fileid_handle_data *data,
				      const SMB_STRUCT_STAT *sbuf);
	uint64_t (*extid_mapping_fn)(struct fileid_handle_data *data,
				      const SMB_STRUCT_STAT *sbuf);
	char **fstype_deny_list;
	char **fstype_allow_list;
	char **mntdir_deny_list;
	char **mntdir_allow_list;
	unsigned num_mount_entries;
	struct fileid_mount_entry *mount_entries;
	ino_t nolockinode;
};

/* check if a mount entry is allowed based on fstype and mount directory */
static bool fileid_mount_entry_allowed(struct fileid_handle_data *data,
				       struct mntent *m)
{
	int i;
	char **fstype_deny = data->fstype_deny_list;
	char **fstype_allow = data->fstype_allow_list;
	char **mntdir_deny = data->mntdir_deny_list;
	char **mntdir_allow = data->mntdir_allow_list;

	if (fstype_deny != NULL) {
		for (i = 0; fstype_deny[i] != NULL; i++) {
			if (strcmp(m->mnt_type, fstype_deny[i]) == 0) {
				return false;
			}
		}
	}
	if (fstype_allow != NULL) {
		for (i = 0; fstype_allow[i] != NULL; i++) {
			if (strcmp(m->mnt_type, fstype_allow[i]) == 0) {
				break;
			}
		}
		if (fstype_allow[i] == NULL) {
			return false;
		}
	}
	if (mntdir_deny != NULL) {
		for (i=0; mntdir_deny[i] != NULL; i++) {
			if (strcmp(m->mnt_dir, mntdir_deny[i]) == 0) {
				return false;
			}
		}
	}
	if (mntdir_allow != NULL) {
		for (i=0; mntdir_allow[i] != NULL; i++) {
			if (strcmp(m->mnt_dir, mntdir_allow[i]) == 0) {
				break;
			}
		}
		if (mntdir_allow[i] == NULL) {
			return false;
		}
	}
	return true;
}


/* load all the mount entries from the mtab */
static void fileid_load_mount_entries(struct fileid_handle_data *data)
{
	FILE *f;
	struct mntent *m;

	data->num_mount_entries = 0;
	TALLOC_FREE(data->mount_entries);

	f = setmntent("/etc/mtab", "r");
	if (!f) return;

	while ((m = getmntent(f))) {
		struct stat st;
		struct statfs sfs;
		struct fileid_mount_entry *cur;
		bool allowed;

		allowed = fileid_mount_entry_allowed(data, m);
		if (!allowed) {
			DBG_DEBUG("skipping mount entry %s\n", m->mnt_dir);
			continue;
		}
		if (stat(m->mnt_dir, &st) != 0) continue;
		if (statfs(m->mnt_dir, &sfs) != 0) continue;

		if (strncmp(m->mnt_fsname, "/dev/", 5) == 0) {
			m->mnt_fsname += 5;
		}

		data->mount_entries = talloc_realloc(data,
							   data->mount_entries,
							   struct fileid_mount_entry,
							   data->num_mount_entries+1);
		if (data->mount_entries == NULL) {
			goto nomem;
		}

		cur = &data->mount_entries[data->num_mount_entries];
		cur->device	= st.st_dev;
		cur->mnt_fsname = talloc_strdup(data->mount_entries,
						m->mnt_fsname);
		if (!cur->mnt_fsname) goto nomem;
		cur->fsid	= sfs.f_fsid;
		cur->devid	= (uint64_t)-1;

		data->num_mount_entries++;
	}
	endmntent(f);
	return;
	
nomem:
	if (f) endmntent(f);

	data->num_mount_entries = 0;
	TALLOC_FREE(data->mount_entries);

	return;
}

/* find a mount entry given a dev_t */
static struct fileid_mount_entry *fileid_find_mount_entry(struct fileid_handle_data *data,
							  SMB_DEV_T dev)
{
	unsigned i;

	if (data->num_mount_entries == 0) {
		fileid_load_mount_entries(data);
	}
	for (i=0;i<data->num_mount_entries;i++) {
		if (data->mount_entries[i].device == dev) {
			return &data->mount_entries[i];
		}
	}
	/* 2nd pass after reloading */
	fileid_load_mount_entries(data);
	for (i=0;i<data->num_mount_entries;i++) {
		if (data->mount_entries[i].device == dev) {
			return &data->mount_entries[i];
		}
	}	
	return NULL;
}


/* a 64 bit hash, based on the one in tdb */
static uint64_t fileid_uint64_hash(const uint8_t *s, size_t len)
{
	uint64_t value;	/* Used to compute the hash value.  */
	uint32_t i;	/* Used to cycle through random values. */

	/* Set the initial value from the key size. */
	for (value = 0x238F13AFLL * len, i=0; i < len; i++)
		value = (value + (((uint64_t)s[i]) << (i*5 % 24)));

	return (1103515243LL * value + 12345LL);
}

/* a device mapping using a fsname */
static uint64_t fileid_device_mapping_fsname(struct fileid_handle_data *data,
					     const SMB_STRUCT_STAT *sbuf)
{
	struct fileid_mount_entry *m;

	m = fileid_find_mount_entry(data, sbuf->st_ex_dev);
	if (!m) return sbuf->st_ex_dev;

	if (m->devid == (uint64_t)-1) {
		m->devid = fileid_uint64_hash((const uint8_t *)m->mnt_fsname,
					      strlen(m->mnt_fsname));
	}

	return m->devid;
}

/* a device mapping using a hostname */
static uint64_t fileid_device_mapping_hostname(struct fileid_handle_data *data,
					       const SMB_STRUCT_STAT *sbuf)
{
	char hostname[HOST_NAME_MAX+1];
	char *devname = NULL;
	uint64_t id;
	size_t devname_len;
	int rc;

	rc = gethostname(hostname, HOST_NAME_MAX+1);
	if (rc != 0) {
		DBG_ERR("gethostname failed\n");
		return UINT64_MAX;
	}

	devname = talloc_asprintf(talloc_tos(), "%s%ju",
				  hostname, (uintmax_t)sbuf->st_ex_dev);
	if (devname == NULL) {
		DBG_ERR("talloc_asprintf failed\n");
		return UINT64_MAX;
	}
	devname_len = talloc_array_length(devname) - 1;

	id = fileid_uint64_hash((uint8_t *)devname, devname_len);

	TALLOC_FREE(devname);

	return id;
}

/* a device mapping using a fsname for files and hostname for dirs */
static uint64_t fileid_device_mapping_fsname_nodirs(
	struct fileid_handle_data *data,
	const SMB_STRUCT_STAT *sbuf)
{
	if (S_ISDIR(sbuf->st_ex_mode)) {
		return fileid_device_mapping_hostname(data, sbuf);
	}

	return fileid_device_mapping_fsname(data, sbuf);
}

/* device mapping functions using a fsid */
static uint64_t fileid_device_mapping_fsid(struct fileid_handle_data *data,
					   const SMB_STRUCT_STAT *sbuf)
{
	struct fileid_mount_entry *m;

	m = fileid_find_mount_entry(data, sbuf->st_ex_dev);
	if (!m) return sbuf->st_ex_dev;

	if (m->devid == (uint64_t)-1) {
		if (sizeof(fsid_t) > sizeof(uint64_t)) {
			m->devid = fileid_uint64_hash((uint8_t *)&m->fsid,
						      sizeof(m->fsid));
		} else {
			union {
				uint64_t ret;
				fsid_t fsid;
			} u;
			ZERO_STRUCT(u);
			u.fsid = m->fsid;
			m->devid = u.ret;
		}
	}

	return m->devid;
}

static uint64_t fileid_extid_mapping_zero(struct fileid_handle_data *data,
					  const SMB_STRUCT_STAT *sbuf)
{
	return 0;
}

static uint64_t fileid_extid_mapping_pid(struct fileid_handle_data *data,
					 const SMB_STRUCT_STAT *sbuf)
{
	return getpid();
}

static int get_connectpath_ino(struct vfs_handle_struct *handle,
			       ino_t *ino)
{
	struct smb_filename *fname = NULL;
	int ret;

	fname = synthetic_smb_fname(talloc_tos(),
				    handle->conn->connectpath,
				    NULL,
				    NULL,
				    0);
	if (fname == NULL) {
		DBG_ERR("synthetic_smb_fname failed\n");
		return -1;
	}

	ret = SMB_VFS_NEXT_STAT(handle, fname);
	if (ret != 0) {
		DBG_ERR("stat failed for %s with %s\n",
			handle->conn->connectpath, strerror(errno));
		TALLOC_FREE(fname);
		return -1;
	}
	*ino = fname->st.st_ex_ino;
	TALLOC_FREE(fname);

	return 0;
}

static int fileid_connect(struct vfs_handle_struct *handle,
			  const char *service, const char *user)
{
	struct fileid_handle_data *data;
	const char *algorithm;
	const char **fstype_deny_list = NULL;
	const char **fstype_allow_list = NULL;
	const char **mntdir_deny_list = NULL;
	const char **mntdir_allow_list = NULL;
	int saved_errno;
	int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);

	if (ret < 0) {
		return ret;
	}

	data = talloc_zero(handle->conn, struct fileid_handle_data);
	if (!data) {
		saved_errno = errno;
		SMB_VFS_NEXT_DISCONNECT(handle);
		DEBUG(0, ("talloc_zero() failed\n"));
		errno = saved_errno;
		return -1;
	}

	data->nolockinode = 0;

	/*
	 * "fileid:mapping" is only here as fallback for old setups
	 * "fileid:algorithm" is the option new setups should use
	 */
	algorithm = lp_parm_const_string(SNUM(handle->conn),
					 "fileid", "mapping",
					 "fsname");
	algorithm = lp_parm_const_string(SNUM(handle->conn),
					 "fileid", "algorithm",
					 algorithm);
	if (strcmp("fsname", algorithm) == 0) {
		data->device_mapping_fn	= fileid_device_mapping_fsname;
		data->extid_mapping_fn = fileid_extid_mapping_zero;
	} else if (strcmp("fsname_nodirs", algorithm) == 0) {
		data->device_mapping_fn = fileid_device_mapping_fsname_nodirs;
		data->extid_mapping_fn = fileid_extid_mapping_zero;
	} else if (strcmp("fsid", algorithm) == 0) {
		data->device_mapping_fn	= fileid_device_mapping_fsid;
		data->extid_mapping_fn = fileid_extid_mapping_zero;
	} else if (strcmp("hostname", algorithm) == 0) {
		data->device_mapping_fn = fileid_device_mapping_hostname;
		data->extid_mapping_fn = fileid_extid_mapping_zero;
	} else if (strcmp("fsname_norootdir", algorithm) == 0) {
		data->device_mapping_fn	= fileid_device_mapping_fsname;
		data->extid_mapping_fn = fileid_extid_mapping_zero;

		ret = get_connectpath_ino(handle, &data->nolockinode);
		if (ret != 0) {
			saved_errno = errno;
			SMB_VFS_NEXT_DISCONNECT(handle);
			errno = saved_errno;
			return -1;
		}
	} else if (strcmp("fsname_norootdir_ext", algorithm) == 0) {
		data->device_mapping_fn	= fileid_device_mapping_fsname;
		data->extid_mapping_fn = fileid_extid_mapping_pid;

		ret = get_connectpath_ino(handle, &data->nolockinode);
		if (ret != 0) {
			saved_errno = errno;
			SMB_VFS_NEXT_DISCONNECT(handle);
			errno = saved_errno;
			return -1;
		}
	} else {
		SMB_VFS_NEXT_DISCONNECT(handle);
		DEBUG(0,("fileid_connect(): unknown algorithm[%s]\n", algorithm));
		return -1;
	}

	fstype_deny_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
					       "fstype deny", NULL);
	if (fstype_deny_list != NULL) {
		data->fstype_deny_list = str_list_copy(data, fstype_deny_list);
		if (data->fstype_deny_list == NULL) {
			saved_errno = errno;
			DBG_ERR("str_list_copy failed\n");
			SMB_VFS_NEXT_DISCONNECT(handle);
			errno = saved_errno;
			return -1;
		}
	}

	fstype_allow_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
						"fstype allow", NULL);
	if (fstype_allow_list != NULL) {
		data->fstype_allow_list = str_list_copy(data, fstype_allow_list);
		if (data->fstype_allow_list == NULL) {
			saved_errno = errno;
			DBG_ERR("str_list_copy failed\n");
			SMB_VFS_NEXT_DISCONNECT(handle);
			errno = saved_errno;
			return -1;
		}
	}

	mntdir_deny_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
					       "mntdir deny", NULL);
	if (mntdir_deny_list != NULL) {
		data->mntdir_deny_list = str_list_copy(data, mntdir_deny_list);
		if (data->mntdir_deny_list == NULL) {
			saved_errno = errno;
			DBG_ERR("str_list_copy failed\n");
			SMB_VFS_NEXT_DISCONNECT(handle);
			errno = saved_errno;
			return -1;
		}
	}

	mntdir_allow_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
						"mntdir allow", NULL);
	if (mntdir_allow_list != NULL) {
		data->mntdir_allow_list = str_list_copy(data, mntdir_allow_list);
		if (data->mntdir_allow_list == NULL) {
			saved_errno = errno;
			DBG_ERR("str_list_copy failed\n");
			SMB_VFS_NEXT_DISCONNECT(handle);
			errno = saved_errno;
			return -1;
		}
	}

	data->nolockinode = lp_parm_ulong(SNUM(handle->conn), "fileid",
					  "nolockinode", data->nolockinode);

	SMB_VFS_HANDLE_SET_DATA(handle, data, NULL,
				struct fileid_handle_data,
				return -1);

	DBG_DEBUG("connect to service[%s] with algorithm[%s] nolockinode %lli\n",
		  service, algorithm, (long long) data->nolockinode);

	return 0;
}

static void fileid_disconnect(struct vfs_handle_struct *handle)
{
	DEBUG(10,("fileid_disconnect() connect to service[%s].\n",
		  lp_servicename(talloc_tos(), SNUM(handle->conn))));

	SMB_VFS_NEXT_DISCONNECT(handle);
}

static struct file_id fileid_file_id_create(struct vfs_handle_struct *handle,
					    const SMB_STRUCT_STAT *sbuf)
{
	struct fileid_handle_data *data;
	struct file_id id;
	uint64_t devid;

	ZERO_STRUCT(id);

	SMB_VFS_HANDLE_GET_DATA(handle, data,
				struct fileid_handle_data,
				return id);

	if ((data->nolockinode != 0) &&
	    (sbuf->st_ex_ino == data->nolockinode)) {
		devid = fileid_device_mapping_hostname(data, sbuf);
		id.extid = data->extid_mapping_fn(data, sbuf);
	} else {
		devid = data->device_mapping_fn(data, sbuf);
	}

	id.inode	= sbuf->st_ex_ino;
	id.devid        = devid;

	DBG_DEBUG("Returning dev [%jx] inode [%jx] extid [%jx]\n",
		  (uintmax_t)id.devid, (uintmax_t)id.inode, (uintmax_t)id.extid);

	return id;
}

static struct vfs_fn_pointers vfs_fileid_fns = {
	.connect_fn = fileid_connect,
	.disconnect_fn = fileid_disconnect,
	.file_id_create_fn = fileid_file_id_create
};

static_decl_vfs;
NTSTATUS vfs_fileid_init(TALLOC_CTX *ctx)
{
	NTSTATUS ret;

	ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fileid",
			       &vfs_fileid_fns);
	if (!NT_STATUS_IS_OK(ret)) {
		return ret;
	}

	vfs_fileid_debug_level = debug_add_class("fileid");
	if (vfs_fileid_debug_level == -1) {
		vfs_fileid_debug_level = DBGC_VFS;
		DEBUG(0, ("vfs_fileid: Couldn't register custom debugging class!\n"));
	} else {
		DEBUG(10, ("vfs_fileid: Debug class number of 'fileid': %d\n", vfs_fileid_debug_level));
	}

	return ret;
}