3aa63bd2cad7259a4596611301e837b55a508f24
[tprouty/samba.git] / source4 / ntvfs / posix / pvfs_rename.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - rename
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "vfs_posix.h"
24 #include "librpc/gen_ndr/security.h"
25
26
27 /*
28   do a file rename, and send any notify triggers
29 */
30 NTSTATUS pvfs_do_rename(struct pvfs_state *pvfs, const struct pvfs_filename *name1, 
31                         const char *name2)
32 {
33         const char *r1, *r2;
34         uint32_t mask;
35
36         if (rename(name1->full_name, name2) == -1) {
37                 return pvfs_map_errno(pvfs, errno);
38         }
39
40         if (name1->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
41                 mask = FILE_NOTIFY_CHANGE_DIR_NAME;
42         } else {
43                 mask = FILE_NOTIFY_CHANGE_FILE_NAME;
44         }
45         /* 
46            renames to the same directory cause a OLD_NAME->NEW_NAME notify.
47            renames to a different directory are considered a remove/add 
48         */
49         r1 = strrchr_m(name1->full_name, '/');
50         r2 = strrchr_m(name2, '/');
51
52         if ((r1-name1->full_name) != (r2-name2) ||
53             strncmp(name1->full_name, name2, r1-name1->full_name) != 0) {
54                 notify_trigger(pvfs->notify_context, 
55                                NOTIFY_ACTION_REMOVED, 
56                                mask,
57                                name1->full_name);
58                 notify_trigger(pvfs->notify_context, 
59                                NOTIFY_ACTION_ADDED, 
60                                mask,
61                                name2);
62         } else {
63                 notify_trigger(pvfs->notify_context, 
64                                NOTIFY_ACTION_OLD_NAME, 
65                                mask,
66                                name1->full_name);
67                 notify_trigger(pvfs->notify_context, 
68                                NOTIFY_ACTION_NEW_NAME, 
69                                mask,
70                                name2);
71         }
72
73         /* this is a strange one. w2k3 gives an additional event for CHANGE_ATTRIBUTES
74            and CHANGE_CREATION on the new file when renaming files, but not 
75            directories */
76         if ((name1->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) == 0) {
77                 notify_trigger(pvfs->notify_context, 
78                                NOTIFY_ACTION_MODIFIED, 
79                                FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION,
80                                name2);
81         }
82         
83         return NT_STATUS_OK;
84 }
85
86
87 /*
88   resolve a wildcard rename pattern. This works on one component of the name
89 */
90 static const char *pvfs_resolve_wildcard_component(TALLOC_CTX *mem_ctx, 
91                                                    const char *fname, 
92                                                    const char *pattern)
93 {
94         const char *p1, *p2;
95         char *dest, *d;
96
97         /* the length is bounded by the length of the two strings combined */
98         dest = talloc_size(mem_ctx, strlen(fname) + strlen(pattern) + 1);
99         if (dest == NULL) {
100                 return NULL;
101         }
102
103         p1 = fname;
104         p2 = pattern;
105         d = dest;
106
107         while (*p2) {
108                 codepoint_t c1, c2;
109                 size_t c_size1, c_size2;
110                 c1 = next_codepoint(p1, &c_size1);
111                 c2 = next_codepoint(p2, &c_size2);
112                 if (c2 == '?') {
113                         d += push_codepoint(d, c1);
114                 } else if (c2 == '*') {
115                         memcpy(d, p1, strlen(p1));
116                         d += strlen(p1);
117                         break;
118                 } else {
119                         d += push_codepoint(d, c2);
120                 }
121
122                 p1 += c_size1;
123                 p2 += c_size2;
124         }
125
126         *d = 0;
127
128         return dest;
129 }
130
131 /*
132   resolve a wildcard rename pattern.
133 */
134 static const char *pvfs_resolve_wildcard(TALLOC_CTX *mem_ctx, 
135                                          const char *fname, 
136                                          const char *pattern)
137 {
138         const char *base1, *base2;
139         const char *ext1, *ext2;
140         char *p;
141
142         /* break into base part plus extension */
143         p = strrchr_m(fname, '.');
144         if (p == NULL) {
145                 ext1 = "";
146                 base1 = fname;
147         } else {
148                 ext1 = talloc_strdup(mem_ctx, p+1);
149                 base1 = talloc_strndup(mem_ctx, fname, p-fname);
150         }
151         if (ext1 == NULL || base1 == NULL) {
152                 return NULL;
153         }
154
155         p = strrchr_m(pattern, '.');
156         if (p == NULL) {
157                 ext2 = "";
158                 base2 = fname;
159         } else {
160                 ext2 = talloc_strdup(mem_ctx, p+1);
161                 base2 = talloc_strndup(mem_ctx, pattern, p-pattern);
162         }
163         if (ext2 == NULL || base2 == NULL) {
164                 return NULL;
165         }
166
167         base1 = pvfs_resolve_wildcard_component(mem_ctx, base1, base2);
168         ext1 = pvfs_resolve_wildcard_component(mem_ctx, ext1, ext2);
169         if (base1 == NULL || ext1 == NULL) {
170                 return NULL;
171         }
172
173         if (*ext1 == 0) {
174                 return base1;
175         }
176
177         return talloc_asprintf(mem_ctx, "%s.%s", base1, ext1);
178 }
179
180 /*
181   rename one file from a wildcard set
182 */
183 static NTSTATUS pvfs_rename_one(struct pvfs_state *pvfs, 
184                                 struct ntvfs_request *req, 
185                                 const char *dir_path,
186                                 const char *fname1,
187                                 const char *fname2,
188                                 uint16_t attrib)
189 {
190         struct pvfs_filename *name1, *name2;
191         TALLOC_CTX *mem_ctx = talloc_new(req);
192         NTSTATUS status;
193         struct odb_lock *lck, *lck2;
194
195         /* resolve the wildcard pattern for this name */
196         fname2 = pvfs_resolve_wildcard(mem_ctx, fname1, fname2);
197         if (fname2 == NULL) {
198                 return NT_STATUS_NO_MEMORY;
199         }
200
201         /* get a pvfs_filename source object */
202         status = pvfs_resolve_partial(pvfs, mem_ctx, 
203                                       dir_path, fname1, &name1);
204         if (!NT_STATUS_IS_OK(status)) {
205                 goto failed;
206         }
207
208         /* make sure its matches the given attributes */
209         status = pvfs_match_attrib(pvfs, name1, attrib, 0);
210         if (!NT_STATUS_IS_OK(status)) {
211                 goto failed;
212         }
213
214         status = pvfs_can_rename(pvfs, req, name1, &lck);
215         if (!NT_STATUS_IS_OK(status)) {
216                 goto failed;
217         }
218
219         /* get a pvfs_filename dest object */
220         status = pvfs_resolve_partial(pvfs, mem_ctx, 
221                                       dir_path, fname2, &name2);
222         if (NT_STATUS_IS_OK(status)) {
223                 status = pvfs_can_delete(pvfs, req, name2, &lck2);
224                 if (!NT_STATUS_IS_OK(status)) {
225                         goto failed;
226                 }
227         }
228
229         status = NT_STATUS_OK;
230
231         fname2 = talloc_asprintf(mem_ctx, "%s/%s", dir_path, fname2);
232         if (fname2 == NULL) {
233                 return NT_STATUS_NO_MEMORY;
234         }
235
236         status = pvfs_do_rename(pvfs, name1, fname2);
237
238         if (NT_STATUS_IS_OK(status)) {
239                 status = odb_rename(lck, fname2);
240         }
241
242 failed:
243         talloc_free(mem_ctx);
244         return status;
245 }
246
247
248 /*
249   rename a set of files with wildcards
250 */
251 static NTSTATUS pvfs_rename_wildcard(struct pvfs_state *pvfs, 
252                                      struct ntvfs_request *req, 
253                                      union smb_rename *ren, 
254                                      struct pvfs_filename *name1, 
255                                      struct pvfs_filename *name2)
256 {
257         struct pvfs_dir *dir;
258         NTSTATUS status;
259         off_t ofs = 0;
260         const char *fname, *fname2, *dir_path;
261         uint16_t attrib = ren->rename.in.attrib;
262         int total_renamed = 0;
263
264         /* get list of matching files */
265         status = pvfs_list_start(pvfs, name1, req, &dir);
266         if (!NT_STATUS_IS_OK(status)) {
267                 return status;
268         }
269
270         status = NT_STATUS_NO_SUCH_FILE;
271
272         dir_path = pvfs_list_unix_path(dir);
273
274         /* only allow wildcard renames within a directory */
275         if (strncmp(dir_path, name2->full_name, strlen(dir_path)) != 0 ||
276             name2->full_name[strlen(dir_path)] != '/' ||
277             strchr(name2->full_name + strlen(dir_path) + 1, '/')) {
278                 return NT_STATUS_INVALID_PARAMETER;
279         }
280
281         fname2 = talloc_strdup(name2, name2->full_name + strlen(dir_path) + 1);
282         if (fname2 == NULL) {
283                 return NT_STATUS_NO_MEMORY;
284         }
285
286         while ((fname = pvfs_list_next(dir, &ofs))) {
287                 status = pvfs_rename_one(pvfs, req, 
288                                          dir_path,
289                                          fname, fname2, attrib);
290                 if (NT_STATUS_IS_OK(status)) {
291                         total_renamed++;
292                 }
293         }
294
295         if (total_renamed == 0) {
296                 return status;
297         }
298
299         return NT_STATUS_OK;
300 }
301
302 /*
303   rename a set of files - SMBmv interface
304 */
305 static NTSTATUS pvfs_rename_mv(struct ntvfs_module_context *ntvfs,
306                                struct ntvfs_request *req, union smb_rename *ren)
307 {
308         struct pvfs_state *pvfs = ntvfs->private_data;
309         NTSTATUS status;
310         struct pvfs_filename *name1, *name2;
311         struct odb_lock *lck;
312
313         /* resolve the cifs name to a posix name */
314         status = pvfs_resolve_name(pvfs, req, ren->rename.in.pattern1, 
315                                    PVFS_RESOLVE_WILDCARD, &name1);
316         if (!NT_STATUS_IS_OK(status)) {
317                 return status;
318         }
319
320         status = pvfs_resolve_name(pvfs, req, ren->rename.in.pattern2, 
321                                    PVFS_RESOLVE_WILDCARD, &name2);
322         if (!NT_STATUS_IS_OK(status)) {
323                 return status;
324         }
325
326         if (name1->has_wildcard || name2->has_wildcard) {
327                 return pvfs_rename_wildcard(pvfs, req, ren, name1, name2);
328         }
329
330         if (!name1->exists) {
331                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
332         }
333
334         if (strcmp(name1->full_name, name2->full_name) == 0) {
335                 return NT_STATUS_OK;
336         }
337
338         if (name2->exists) {
339                 return NT_STATUS_OBJECT_NAME_COLLISION;
340         }
341
342         status = pvfs_match_attrib(pvfs, name1, ren->rename.in.attrib, 0);
343         if (!NT_STATUS_IS_OK(status)) {
344                 return status;
345         }
346
347         status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
348         if (!NT_STATUS_IS_OK(status)) {
349                 return status;
350         }
351
352         status = pvfs_can_rename(pvfs, req, name1, &lck);
353         if (!NT_STATUS_IS_OK(status)) {
354                 return status;
355         }
356
357         status = pvfs_do_rename(pvfs, name1, name2->full_name);
358         if (NT_STATUS_IS_OK(status)) {
359                 status = odb_rename(lck, name2->full_name);
360         }
361         
362         return NT_STATUS_OK;
363 }
364
365
366 /*
367   rename a set of files - ntrename interface
368 */
369 static NTSTATUS pvfs_rename_nt(struct ntvfs_module_context *ntvfs,
370                                struct ntvfs_request *req, union smb_rename *ren)
371 {
372         struct pvfs_state *pvfs = ntvfs->private_data;
373         NTSTATUS status;
374         struct pvfs_filename *name1, *name2;
375         struct odb_lock *lck;
376
377         switch (ren->ntrename.in.flags) {
378         case RENAME_FLAG_RENAME:
379         case RENAME_FLAG_HARD_LINK:
380         case RENAME_FLAG_COPY:
381         case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
382                 break;
383         default:
384                 return NT_STATUS_ACCESS_DENIED;
385         }
386
387         /* resolve the cifs name to a posix name */
388         status = pvfs_resolve_name(pvfs, req, ren->ntrename.in.old_name, 
389                                    PVFS_RESOLVE_WILDCARD, &name1);
390         if (!NT_STATUS_IS_OK(status)) {
391                 return status;
392         }
393
394         status = pvfs_resolve_name(pvfs, req, ren->ntrename.in.new_name, 
395                                    PVFS_RESOLVE_WILDCARD, &name2);
396         if (!NT_STATUS_IS_OK(status)) {
397                 return status;
398         }
399
400         if (name1->has_wildcard || name2->has_wildcard) {
401                 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
402         }
403
404         if (!name1->exists) {
405                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
406         }
407
408         if (strcmp(name1->full_name, name2->full_name) == 0) {
409                 return NT_STATUS_OK;
410         }
411
412         if (name2->exists) {
413                 return NT_STATUS_OBJECT_NAME_COLLISION;
414         }
415
416         status = pvfs_match_attrib(pvfs, name1, ren->ntrename.in.attrib, 0);
417         if (!NT_STATUS_IS_OK(status)) {
418                 return status;
419         }
420
421         status = pvfs_can_rename(pvfs, req, name1, &lck);
422         if (!NT_STATUS_IS_OK(status)) {
423                 return status;
424         }
425
426         switch (ren->ntrename.in.flags) {
427         case RENAME_FLAG_RENAME:
428                 status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
429                 NT_STATUS_NOT_OK_RETURN(status);
430                 status = pvfs_do_rename(pvfs, name1, name2->full_name);
431                 NT_STATUS_NOT_OK_RETURN(status);
432                 break;
433
434         case RENAME_FLAG_HARD_LINK:
435                 status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
436                 NT_STATUS_NOT_OK_RETURN(status);
437                 if (link(name1->full_name, name2->full_name) == -1) {
438                         return pvfs_map_errno(pvfs, errno);
439                 }
440                 break;
441
442         case RENAME_FLAG_COPY:
443                 status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
444                 NT_STATUS_NOT_OK_RETURN(status);
445                 return pvfs_copy_file(pvfs, name1, name2);
446
447         case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
448                 return NT_STATUS_INVALID_PARAMETER;
449
450         default:
451                 return NT_STATUS_ACCESS_DENIED;
452         }
453
454         
455         return NT_STATUS_OK;
456 }
457
458 /*
459   rename a set of files - ntrename interface
460 */
461 NTSTATUS pvfs_rename(struct ntvfs_module_context *ntvfs,
462                      struct ntvfs_request *req, union smb_rename *ren)
463 {
464         switch (ren->generic.level) {
465         case RAW_RENAME_RENAME:
466                 return pvfs_rename_mv(ntvfs, req, ren);
467
468         case RAW_RENAME_NTRENAME:
469                 return pvfs_rename_nt(ntvfs, req, ren);
470
471         default:
472                 break;
473         }
474
475         return NT_STATUS_INVALID_LEVEL;
476 }
477