summaryrefslogtreecommitdiff
path: root/source4/ntvfs/posix/pvfs_rename.c
blob: 0ca05bbc170e4cb5ccd1fb332197b6e0d466c024 (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
/* 
   Unix SMB/CIFS implementation.

   POSIX NTVFS backend - rename

   Copyright (C) Andrew Tridgell 2004

   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "vfs_posix.h"


/*
  resolve a wildcard rename pattern. This works on one component of the name
*/
static const char *pvfs_resolve_wildcard_component(TALLOC_CTX *mem_ctx, 
						   const char *fname, 
						   const char *pattern)
{
	const char *p1, *p2;
	char *dest, *d;

	/* the length is bounded by the length of the two strings combined */
	dest = talloc(mem_ctx, strlen(fname) + strlen(pattern) + 1);
	if (dest == NULL) {
		return NULL;
	}

	p1 = fname;
	p2 = pattern;
	d = dest;

	while (*p2) {
		codepoint_t c1, c2;
		size_t c_size1, c_size2;
		c1 = next_codepoint(p1, &c_size1);
		c2 = next_codepoint(p2, &c_size2);
		if (c2 == '?') {
			d += push_codepoint(d, c1);
		} else if (c2 == '*') {
			memcpy(d, p1, strlen(p1));
			d += strlen(p1);
			break;
		} else {
			d += push_codepoint(d, c2);
		}

		p1 += c_size1;
		p2 += c_size2;
	}

	*d = 0;

	return dest;
}

/*
  resolve a wildcard rename pattern.
*/
static const char *pvfs_resolve_wildcard(TALLOC_CTX *mem_ctx, 
					 const char *fname, 
					 const char *pattern)
{
	const char *base1, *base2;
	const char *ext1, *ext2;
	char *p;

	/* break into base part plus extension */
	p = strrchr_m(fname, '.');
	if (p == NULL) {
		ext1 = "";
		base1 = fname;
	} else {
		ext1 = talloc_strdup(mem_ctx, p+1);
		base1 = talloc_strndup(mem_ctx, fname, p-fname);
	}
	if (ext1 == NULL || base1 == NULL) {
		return NULL;
	}

	p = strrchr_m(pattern, '.');
	if (p == NULL) {
		ext2 = "";
		base2 = fname;
	} else {
		ext2 = talloc_strdup(mem_ctx, p+1);
		base2 = talloc_strndup(mem_ctx, pattern, p-pattern);
	}
	if (ext2 == NULL || base2 == NULL) {
		return NULL;
	}

	base1 = pvfs_resolve_wildcard_component(mem_ctx, base1, base2);
	ext1 = pvfs_resolve_wildcard_component(mem_ctx, ext1, ext2);
	if (base1 == NULL || ext1 == NULL) {
		return NULL;
	}

	if (*ext1 == 0) {
		return base1;
	}

	return talloc_asprintf(mem_ctx, "%s.%s", base1, ext1);
}

/*
  rename one file from a wildcard set
*/
static NTSTATUS pvfs_rename_one(struct pvfs_state *pvfs, 
				struct smbsrv_request *req, 
				const char *dir_path,
				const char *fname1,
				const char *fname2,
				uint16_t attrib)
{
	struct pvfs_filename *name1, *name2;
	TALLOC_CTX *mem_ctx = talloc(req, 0);
	NTSTATUS status;

	/* resolve the wildcard pattern for this name */
	fname2 = pvfs_resolve_wildcard(mem_ctx, fname1, fname2);
	if (fname2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	/* get a pvfs_filename source object */
	status = pvfs_resolve_partial(pvfs, mem_ctx, 
				      dir_path, fname1, &name1);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(mem_ctx);
		return status;
	}

	/* make sure its matches the given attributes */
	status = pvfs_match_attrib(pvfs, name1, attrib, 0);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(mem_ctx);
		return status;
	}

	status = pvfs_can_rename(pvfs, name1);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(mem_ctx);
		return status;
	}

	/* get a pvfs_filename dest object */
	status = pvfs_resolve_partial(pvfs, mem_ctx, 
				      dir_path, fname2, &name2);
	if (NT_STATUS_IS_OK(status)) {
		status = pvfs_can_delete(pvfs, req, name2);
		if (!NT_STATUS_IS_OK(status)) {
			talloc_free(mem_ctx);
			return status;
		}
	}

	fname2 = talloc_asprintf(mem_ctx, "%s/%s", dir_path, fname2);
	if (fname2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	if (rename(name1->full_name, fname2) == -1) {
		talloc_free(mem_ctx);
		return pvfs_map_errno(pvfs, errno);
	}

	talloc_free(mem_ctx);

	return NT_STATUS_OK;
}


/*
  rename a set of files with wildcards
*/
static NTSTATUS pvfs_rename_wildcard(struct pvfs_state *pvfs, 
				     struct smbsrv_request *req, 
				     union smb_rename *ren, 
				     struct pvfs_filename *name1, 
				     struct pvfs_filename *name2)
{
	struct pvfs_dir *dir;
	NTSTATUS status;
	uint_t ofs = 0;
	const char *fname, *fname2, *dir_path;
	uint16_t attrib = ren->rename.in.attrib;
	int total_renamed = 0;

	/* get list of matching files */
	status = pvfs_list_start(pvfs, name1, req, &dir);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	status = NT_STATUS_NO_SUCH_FILE;

	dir_path = pvfs_list_unix_path(dir);

	/* only allow wildcard renames within a directory */
	if (strncmp(dir_path, name2->full_name, strlen(dir_path)) != 0 ||
	    name2->full_name[strlen(dir_path)] != '/' ||
	    strchr(name2->full_name + strlen(dir_path) + 1, '/')) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	fname2 = talloc_strdup(name2, name2->full_name + strlen(dir_path) + 1);
	if (fname2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	while ((fname = pvfs_list_next(dir, &ofs))) {
		status = pvfs_rename_one(pvfs, req, 
					 dir_path,
					 fname, fname2, attrib);
		if (NT_STATUS_IS_OK(status)) {
			total_renamed++;
		}
	}

	if (total_renamed == 0) {
		return status;
	}

	return NT_STATUS_OK;
}

/*
  rename a set of files - SMBmv interface
*/
static NTSTATUS pvfs_rename_mv(struct ntvfs_module_context *ntvfs,
			       struct smbsrv_request *req, union smb_rename *ren)
{
	struct pvfs_state *pvfs = ntvfs->private_data;
	NTSTATUS status;
	struct pvfs_filename *name1, *name2;

	/* resolve the cifs name to a posix name */
	status = pvfs_resolve_name(pvfs, req, ren->rename.in.pattern1, 
				   PVFS_RESOLVE_WILDCARD, &name1);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	status = pvfs_resolve_name(pvfs, req, ren->rename.in.pattern2, 
				   PVFS_RESOLVE_WILDCARD, &name2);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	if (name1->has_wildcard || name2->has_wildcard) {
		return pvfs_rename_wildcard(pvfs, req, ren, name1, name2);
	}

	if (!name1->exists) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	if (strcmp(name1->full_name, name2->full_name) == 0) {
		return NT_STATUS_OK;
	}

	if (name2->exists) {
		return NT_STATUS_OBJECT_NAME_COLLISION;
	}

	status = pvfs_match_attrib(pvfs, name1, ren->rename.in.attrib, 0);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	status = pvfs_can_rename(pvfs, name1);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	if (rename(name1->full_name, name2->full_name) == -1) {
		return pvfs_map_errno(pvfs, errno);
	}
	
	return NT_STATUS_OK;
}


/*
  rename a set of files - ntrename interface
*/
static NTSTATUS pvfs_rename_nt(struct ntvfs_module_context *ntvfs,
			       struct smbsrv_request *req, union smb_rename *ren)
{
	struct pvfs_state *pvfs = ntvfs->private_data;
	NTSTATUS status;
	struct pvfs_filename *name1, *name2;

	switch (ren->ntrename.in.flags) {
	case RENAME_FLAG_RENAME:
	case RENAME_FLAG_HARD_LINK:
	case RENAME_FLAG_COPY:
	case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
		break;
	default:
		return NT_STATUS_ACCESS_DENIED;
	}

	/* resolve the cifs name to a posix name */
	status = pvfs_resolve_name(pvfs, req, ren->ntrename.in.old_name, 
				   PVFS_RESOLVE_WILDCARD, &name1);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	status = pvfs_resolve_name(pvfs, req, ren->ntrename.in.new_name, 
				   PVFS_RESOLVE_WILDCARD, &name2);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	if (name1->has_wildcard || name2->has_wildcard) {
		return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
	}

	if (!name1->exists) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	if (strcmp(name1->full_name, name2->full_name) == 0) {
		return NT_STATUS_OK;
	}

	if (name2->exists) {
		return NT_STATUS_OBJECT_NAME_COLLISION;
	}

	status = pvfs_match_attrib(pvfs, name1, ren->ntrename.in.attrib, 0);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	status = pvfs_can_rename(pvfs, name1);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	switch (ren->ntrename.in.flags) {
	case RENAME_FLAG_RENAME:
		if (rename(name1->full_name, name2->full_name) == -1) {
			return pvfs_map_errno(pvfs, errno);
		}
		break;

	case RENAME_FLAG_HARD_LINK:
		if (link(name1->full_name, name2->full_name) == -1) {
			return pvfs_map_errno(pvfs, errno);
		}
		break;

	case RENAME_FLAG_COPY:
		return pvfs_copy_file(pvfs, name1, name2);

	case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
		return NT_STATUS_INVALID_PARAMETER;

	default:
		return NT_STATUS_ACCESS_DENIED;
	}

	
	return NT_STATUS_OK;
}

/*
  rename a set of files - ntrename interface
*/
NTSTATUS pvfs_rename(struct ntvfs_module_context *ntvfs,
		     struct smbsrv_request *req, union smb_rename *ren)
{
	switch (ren->generic.level) {
	case RAW_RENAME_RENAME:
		return pvfs_rename_mv(ntvfs, req, ren);

	case RAW_RENAME_NTRENAME:
		return pvfs_rename_nt(ntvfs, req, ren);

	default:
		break;
	}

	return NT_STATUS_INVALID_LEVEL;
}