s3-includes: only include system/filesys.h when needed.
[samba.git] / source3 / modules / vfs_netatalk.c
1 /* 
2  * AppleTalk VFS module for Samba-3.x
3  *
4  * Copyright (C) Alexei Kotovich, 2002
5  * Copyright (C) Stefan (metze) Metzmacher, 2003
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *  
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *  
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_VFS
26
27 #define APPLEDOUBLE     ".AppleDouble"
28 #define ADOUBLEMODE     0777
29
30 /* atalk functions */
31
32 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
33                              const char *fname,
34                              char **adbl_path, char **orig_path,
35                              SMB_STRUCT_STAT *adbl_info,
36                              SMB_STRUCT_STAT *orig_info,
37                              bool fake_dir_create_times);
38
39 static int atalk_unlink_file(const char *path);
40
41 static int atalk_get_path_ptr(char *path)
42 {
43         int i   = 0;
44         int ptr = 0;
45         
46         for (i = 0; path[i]; i ++) {
47                 if (path[i] == '/')
48                         ptr = i;
49                 /* get out some 'spam';) from win32's file name */
50                 else if (path[i] == ':') {
51                         path[i] = '\0';
52                         break;
53                 }
54         }
55         
56         return ptr;
57 }
58
59 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
60                              const char *fname,
61                              char **adbl_path, char **orig_path,
62                              SMB_STRUCT_STAT *adbl_info,
63                              SMB_STRUCT_STAT *orig_info,
64                              bool fake_dir_create_times)
65 {
66         int ptr0 = 0;
67         int ptr1 = 0;
68         char *dname = 0;
69         char *name  = 0;
70
71         if (!ctx || !path || !fname || !adbl_path || !orig_path ||
72                 !adbl_info || !orig_info)
73                 return -1;
74 #if 0
75         DEBUG(3, ("ATALK: PATH: %s[%s]\n", path, fname));
76 #endif
77         if (strstr(path, APPLEDOUBLE) || strstr(fname, APPLEDOUBLE)) {
78                 DEBUG(3, ("ATALK: path %s[%s] already contains %s\n", path, fname, APPLEDOUBLE));
79                 return -1;
80         }
81
82         if (fname[0] == '.') ptr0 ++;
83         if (fname[1] == '/') ptr0 ++;
84
85         *orig_path = talloc_asprintf(ctx, "%s/%s", path, &fname[ptr0]);
86
87         /* get pointer to last '/' */
88         ptr1 = atalk_get_path_ptr(*orig_path);
89
90         sys_lstat(*orig_path, orig_info, fake_dir_create_times);
91
92         if (S_ISDIR(orig_info->st_ex_mode)) {
93                 *adbl_path = talloc_asprintf(ctx, "%s/%s/%s/", 
94                   path, &fname[ptr0], APPLEDOUBLE);
95         } else {
96                 dname = talloc_strdup(ctx, *orig_path);
97                 dname[ptr1] = '\0';
98                 name = *orig_path;
99                 *adbl_path = talloc_asprintf(ctx, "%s/%s/%s", 
100                   dname, APPLEDOUBLE, &name[ptr1 + 1]);
101         }
102 #if 0
103         DEBUG(3, ("ATALK: DEBUG:\n%s\n%s\n", *orig_path, *adbl_path)); 
104 #endif
105         sys_lstat(*adbl_path, adbl_info, fake_dir_create_times);
106         return 0;
107 }
108
109 static int atalk_unlink_file(const char *path)
110 {
111         int ret = 0;
112
113         become_root();
114         ret = unlink(path);
115         unbecome_root();
116         
117         return ret;
118 }
119
120 static void atalk_add_to_list(name_compare_entry **list)
121 {
122         int i, count = 0;
123         name_compare_entry *new_list = 0;
124         name_compare_entry *cur_list = 0;
125
126         cur_list = *list;
127
128         if (cur_list) {
129                 for (i = 0, count = 0; cur_list[i].name; i ++, count ++) {
130                         if (strstr(cur_list[i].name, APPLEDOUBLE))
131                                 return;
132                 }
133         }
134
135         if (!(new_list = SMB_CALLOC_ARRAY(name_compare_entry, count + 2)))
136                 return;
137
138         for (i = 0; i < count; i ++) {
139                 new_list[i].name    = SMB_STRDUP(cur_list[i].name);
140                 new_list[i].is_wild = cur_list[i].is_wild;
141         }
142
143         new_list[i].name    = SMB_STRDUP(APPLEDOUBLE);
144         new_list[i].is_wild = False;
145
146         free_namearray(*list);
147
148         *list = new_list;
149         new_list = 0;
150         cur_list = 0;
151 }
152
153 static void atalk_rrmdir(TALLOC_CTX *ctx, char *path)
154 {
155         char *dpath;
156         SMB_STRUCT_DIRENT *dent = 0;
157         SMB_STRUCT_DIR *dir;
158
159         if (!path) return;
160
161         dir = sys_opendir(path);
162         if (!dir) return;
163
164         while (NULL != (dent = sys_readdir(dir))) {
165                 if (strcmp(dent->d_name, ".") == 0 ||
166                     strcmp(dent->d_name, "..") == 0)
167                         continue;
168                 if (!(dpath = talloc_asprintf(ctx, "%s/%s", 
169                                               path, dent->d_name)))
170                         continue;
171                 atalk_unlink_file(dpath);
172         }
173
174         sys_closedir(dir);
175 }
176
177 /* Disk operations */
178
179 /* Directory operations */
180
181 static SMB_STRUCT_DIR *atalk_opendir(struct vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
182 {
183         SMB_STRUCT_DIR *ret = 0;
184
185         ret = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
186
187         /*
188          * when we try to perform delete operation upon file which has fork
189          * in ./.AppleDouble and this directory wasn't hidden by Samba,
190          * MS Windows explorer causes the error: "Cannot find the specified file"
191          * There is some workaround to avoid this situation, i.e. if
192          * connection has not .AppleDouble entry in either veto or hide 
193          * list then it would be nice to add one.
194          */
195
196         atalk_add_to_list(&handle->conn->hide_list);
197         atalk_add_to_list(&handle->conn->veto_list);
198
199         return ret;
200 }
201
202 static SMB_STRUCT_DIR *atalk_fdopendir(struct vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32 attr)
203 {
204         SMB_STRUCT_DIR *ret = 0;
205
206         ret = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
207
208         if (ret == NULL) {
209                 return ret;
210         }
211
212         /*
213          * when we try to perform delete operation upon file which has fork
214          * in ./.AppleDouble and this directory wasn't hidden by Samba,
215          * MS Windows explorer causes the error: "Cannot find the specified file"
216          * There is some workaround to avoid this situation, i.e. if
217          * connection has not .AppleDouble entry in either veto or hide 
218          * list then it would be nice to add one.
219          */
220
221         atalk_add_to_list(&handle->conn->hide_list);
222         atalk_add_to_list(&handle->conn->veto_list);
223
224         return ret;
225 }
226
227 static int atalk_rmdir(struct vfs_handle_struct *handle, const char *path)
228 {
229         bool add = False;
230         TALLOC_CTX *ctx = 0;
231         char *dpath;
232
233         if (!handle->conn->origpath || !path) goto exit_rmdir;
234
235         /* due to there is no way to change bDeleteVetoFiles variable
236          * from this module, gotta use talloc stuff..
237          */
238
239         strstr(path, APPLEDOUBLE) ? (add = False) : (add = True);
240
241         if (!(ctx = talloc_init("remove_directory")))
242                 goto exit_rmdir;
243
244         if (!(dpath = talloc_asprintf(ctx, "%s/%s%s", 
245           handle->conn->origpath, path, add ? "/"APPLEDOUBLE : "")))
246                 goto exit_rmdir;
247
248         atalk_rrmdir(ctx, dpath);
249
250 exit_rmdir:
251         talloc_destroy(ctx);
252         return SMB_VFS_NEXT_RMDIR(handle, path);
253 }
254
255 /* File operations */
256
257 static int atalk_rename(struct vfs_handle_struct *handle,
258                         const struct smb_filename *smb_fname_src,
259                         const struct smb_filename *smb_fname_dst)
260 {
261         int ret = 0;
262         char *oldname = NULL;
263         char *adbl_path = NULL;
264         char *orig_path = NULL;
265         SMB_STRUCT_STAT adbl_info;
266         SMB_STRUCT_STAT orig_info;
267         NTSTATUS status;
268
269         ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
270
271         status = get_full_smb_filename(talloc_tos(), smb_fname_src, &oldname);
272         if (!NT_STATUS_IS_OK(status)) {
273                 return ret;
274         }
275
276         if (atalk_build_paths(talloc_tos(), handle->conn->origpath, oldname,
277                               &adbl_path, &orig_path, &adbl_info,
278                               &orig_info, false) != 0)
279                 goto exit_rename;
280
281         if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
282                 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));              
283                 goto exit_rename;
284         }
285
286         atalk_unlink_file(adbl_path);
287
288 exit_rename:
289         TALLOC_FREE(oldname);
290         TALLOC_FREE(adbl_path);
291         TALLOC_FREE(orig_path);
292         return ret;
293 }
294
295 static int atalk_unlink(struct vfs_handle_struct *handle,
296                         const struct smb_filename *smb_fname)
297 {
298         int ret = 0, i;
299         char *path = NULL;
300         char *adbl_path = NULL;
301         char *orig_path = NULL;
302         SMB_STRUCT_STAT adbl_info;
303         SMB_STRUCT_STAT orig_info;
304         NTSTATUS status;
305
306         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
307
308         status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
309         if (!NT_STATUS_IS_OK(status)) {
310                 return ret;
311         }
312
313         /* no .AppleDouble sync if veto or hide list is empty,
314          * otherwise "Cannot find the specified file" error will be caused
315          */
316
317         if (!handle->conn->veto_list) return ret;
318         if (!handle->conn->hide_list) return ret;
319
320         for (i = 0; handle->conn->veto_list[i].name; i ++) {
321                 if (strstr(handle->conn->veto_list[i].name, APPLEDOUBLE))
322                         break;
323         }
324
325         if (!handle->conn->veto_list[i].name) {
326                 for (i = 0; handle->conn->hide_list[i].name; i ++) {
327                         if (strstr(handle->conn->hide_list[i].name, APPLEDOUBLE))
328                                 break;
329                         else {
330                                 DEBUG(3, ("ATALK: %s is not hidden, skipped..\n",
331                                   APPLEDOUBLE));                
332                                 goto exit_unlink;
333                         }
334                 }
335         }
336
337         if (atalk_build_paths(talloc_tos(), handle->conn->origpath, path,
338                               &adbl_path, &orig_path,
339                               &adbl_info, &orig_info, false) != 0)
340                 goto exit_unlink;
341
342         if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
343                 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
344                 goto exit_unlink;
345         }
346
347         atalk_unlink_file(adbl_path);
348
349 exit_unlink:
350         TALLOC_FREE(path);
351         TALLOC_FREE(adbl_path);
352         TALLOC_FREE(orig_path);
353         return ret;
354 }
355
356 static int atalk_chmod(struct vfs_handle_struct *handle, const char *path, mode_t mode)
357 {
358         int ret = 0;
359         char *adbl_path = 0;
360         char *orig_path = 0;
361         SMB_STRUCT_STAT adbl_info;
362         SMB_STRUCT_STAT orig_info;
363         TALLOC_CTX *ctx;
364
365         ret = SMB_VFS_NEXT_CHMOD(handle, path, mode);
366
367         if (!path) return ret;
368
369         if (!(ctx = talloc_init("chmod_file")))
370                 return ret;
371
372         if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path,
373                               &orig_path, &adbl_info, &orig_info,
374                               false) != 0)
375                 goto exit_chmod;
376
377         if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
378                 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));              
379                 goto exit_chmod;
380         }
381
382         chmod(adbl_path, ADOUBLEMODE);
383
384 exit_chmod:     
385         talloc_destroy(ctx);
386         return ret;
387 }
388
389 static int atalk_chown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
390 {
391         int ret = 0;
392         char *adbl_path = 0;
393         char *orig_path = 0;
394         SMB_STRUCT_STAT adbl_info;
395         SMB_STRUCT_STAT orig_info;
396         TALLOC_CTX *ctx;
397
398         ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
399
400         if (!path) return ret;
401
402         if (!(ctx = talloc_init("chown_file")))
403                 return ret;
404
405         if (atalk_build_paths(ctx, handle->conn->origpath, path,
406                               &adbl_path, &orig_path,
407                               &adbl_info, &orig_info, false) != 0)
408                 goto exit_chown;
409
410         if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
411                 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));              
412                 goto exit_chown;
413         }
414
415         if (chown(adbl_path, uid, gid) == -1) {
416                 DEBUG(3, ("ATALK: chown error %s\n", strerror(errno)));
417         }
418
419 exit_chown:     
420         talloc_destroy(ctx);
421         return ret;
422 }
423
424 static int atalk_lchown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
425 {
426         int ret = 0;
427         char *adbl_path = 0;
428         char *orig_path = 0;
429         SMB_STRUCT_STAT adbl_info;
430         SMB_STRUCT_STAT orig_info;
431         TALLOC_CTX *ctx;
432
433         ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
434
435         if (!path) return ret;
436
437         if (!(ctx = talloc_init("lchown_file")))
438                 return ret;
439
440         if (atalk_build_paths(ctx, handle->conn->origpath, path,
441                               &adbl_path, &orig_path,
442                               &adbl_info, &orig_info, false) != 0)
443                 goto exit_lchown;
444
445         if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
446                 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));              
447                 goto exit_lchown;
448         }
449
450         if (lchown(adbl_path, uid, gid) == -1) {
451                 DEBUG(3, ("ATALK: lchown error %s\n", strerror(errno)));
452         }
453
454 exit_lchown:    
455         talloc_destroy(ctx);
456         return ret;
457 }
458
459 static struct vfs_fn_pointers vfs_netatalk_fns = {
460         .opendir = atalk_opendir,
461         .fdopendir = atalk_fdopendir,
462         .rmdir = atalk_rmdir,
463         .rename = atalk_rename,
464         .unlink = atalk_unlink,
465         .chmod = atalk_chmod,
466         .chown = atalk_chown,
467         .lchown = atalk_lchown,
468 };
469
470 NTSTATUS vfs_netatalk_init(void);
471 NTSTATUS vfs_netatalk_init(void)
472 {
473         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "netatalk",
474                                 &vfs_netatalk_fns);
475 }