ca7085ca18a5e65aed0255f797a53ebe36456318
[jra/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
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_VFS
25
26 #define APPLEDOUBLE     ".AppleDouble"
27 #define ADOUBLEMODE     0777
28
29 /* atalk functions */
30
31 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
32   const char *fname, char **adbl_path, char **orig_path, 
33   SMB_STRUCT_STAT *adbl_info, SMB_STRUCT_STAT *orig_info);
34
35 static int atalk_unlink_file(const char *path);
36
37 static int atalk_get_path_ptr(char *path)
38 {
39         int i   = 0;
40         int ptr = 0;
41         
42         for (i = 0; path[i]; i ++) {
43                 if (path[i] == '/')
44                         ptr = i;
45                 /* get out some 'spam';) from win32's file name */
46                 else if (path[i] == ':') {
47                         path[i] = '\0';
48                         break;
49                 }
50         }
51         
52         return ptr;
53 }
54
55 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path, const char *fname,
56                               char **adbl_path, char **orig_path,
57                               SMB_STRUCT_STAT *adbl_info, SMB_STRUCT_STAT *orig_info)
58 {
59         int ptr0 = 0;
60         int ptr1 = 0;
61         char *dname = 0;
62         char *name  = 0;
63
64         if (!ctx || !path || !fname || !adbl_path || !orig_path ||
65                 !adbl_info || !orig_info)
66                 return -1;
67 #if 0
68         DEBUG(3, ("ATALK: PATH: %s[%s]\n", path, fname));
69 #endif
70         if (strstr(path, APPLEDOUBLE) || strstr(fname, APPLEDOUBLE)) {
71                 DEBUG(3, ("ATALK: path %s[%s] already contains %s\n", path, fname, APPLEDOUBLE));
72                 return -1;
73         }
74
75         if (fname[0] == '.') ptr0 ++;
76         if (fname[1] == '/') ptr0 ++;
77
78         *orig_path = talloc_asprintf(ctx, "%s/%s", path, &fname[ptr0]);
79
80         /* get pointer to last '/' */
81         ptr1 = atalk_get_path_ptr(*orig_path);
82
83         sys_lstat(*orig_path, orig_info);
84
85         if (S_ISDIR(orig_info->st_mode)) {
86                 *adbl_path = talloc_asprintf(ctx, "%s/%s/%s/", 
87                   path, &fname[ptr0], APPLEDOUBLE);
88         } else {
89                 dname = talloc_strdup(ctx, *orig_path);
90                 dname[ptr1] = '\0';
91                 name = *orig_path;
92                 *adbl_path = talloc_asprintf(ctx, "%s/%s/%s", 
93                   dname, APPLEDOUBLE, &name[ptr1 + 1]);
94         }
95 #if 0
96         DEBUG(3, ("ATALK: DEBUG:\n%s\n%s\n", *orig_path, *adbl_path)); 
97 #endif
98         sys_lstat(*adbl_path, adbl_info);
99         return 0;
100 }
101
102 static int atalk_unlink_file(const char *path)
103 {
104         int ret = 0;
105
106         become_root();
107         ret = unlink(path);
108         unbecome_root();
109         
110         return ret;
111 }
112
113 static void atalk_add_to_list(name_compare_entry **list)
114 {
115         int i, count = 0;
116         name_compare_entry *new_list = 0;
117         name_compare_entry *cur_list = 0;
118
119         cur_list = *list;
120
121         if (cur_list) {
122                 for (i = 0, count = 0; cur_list[i].name; i ++, count ++) {
123                         if (strstr(cur_list[i].name, APPLEDOUBLE))
124                                 return;
125                 }
126         }
127
128         if (!(new_list = SMB_CALLOC_ARRAY(name_compare_entry, (count == 0 ? 1 : count + 1))))
129                 return;
130
131         for (i = 0; i < count; i ++) {
132                 new_list[i].name    = SMB_STRDUP(cur_list[i].name);
133                 new_list[i].is_wild = cur_list[i].is_wild;
134         }
135
136         new_list[i].name    = SMB_STRDUP(APPLEDOUBLE);
137         new_list[i].is_wild = False;
138
139         free_namearray(*list);
140
141         *list = new_list;
142         new_list = 0;
143         cur_list = 0;
144 }
145
146 static void atalk_rrmdir(TALLOC_CTX *ctx, char *path)
147 {
148         char *dpath;
149         SMB_STRUCT_DIRENT *dent = 0;
150         SMB_STRUCT_DIR *dir;
151
152         if (!path) return;
153
154         dir = sys_opendir(path);
155         if (!dir) return;
156
157         while (NULL != (dent = sys_readdir(dir))) {
158                 if (strcmp(dent->d_name, ".") == 0 ||
159                     strcmp(dent->d_name, "..") == 0)
160                         continue;
161                 if (!(dpath = talloc_asprintf(ctx, "%s/%s", 
162                                               path, dent->d_name)))
163                         continue;
164                 atalk_unlink_file(dpath);
165         }
166
167         sys_closedir(dir);
168 }
169
170 /* Disk operations */
171
172 /* Directory operations */
173
174 static SMB_STRUCT_DIR *atalk_opendir(struct vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
175 {
176         SMB_STRUCT_DIR *ret = 0;
177
178         ret = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
179
180         /*
181          * when we try to perform delete operation upon file which has fork
182          * in ./.AppleDouble and this directory wasn't hidden by Samba,
183          * MS Windows explorer causes the error: "Cannot find the specified file"
184          * There is some workaround to avoid this situation, i.e. if
185          * connection has not .AppleDouble entry in either veto or hide 
186          * list then it would be nice to add one.
187          */
188
189         atalk_add_to_list(&handle->conn->hide_list);
190         atalk_add_to_list(&handle->conn->veto_list);
191
192         return ret;
193 }
194
195 static int atalk_rmdir(struct vfs_handle_struct *handle, const char *path)
196 {
197         bool add = False;
198         TALLOC_CTX *ctx = 0;
199         char *dpath;
200
201         if (!handle->conn->origpath || !path) goto exit_rmdir;
202
203         /* due to there is no way to change bDeleteVetoFiles variable
204          * from this module, gotta use talloc stuff..
205          */
206
207         strstr(path, APPLEDOUBLE) ? (add = False) : (add = True);
208
209         if (!(ctx = talloc_init("remove_directory")))
210                 goto exit_rmdir;
211
212         if (!(dpath = talloc_asprintf(ctx, "%s/%s%s", 
213           handle->conn->origpath, path, add ? "/"APPLEDOUBLE : "")))
214                 goto exit_rmdir;
215
216         atalk_rrmdir(ctx, dpath);
217
218 exit_rmdir:
219         talloc_destroy(ctx);
220         return SMB_VFS_NEXT_RMDIR(handle, path);
221 }
222
223 /* File operations */
224
225 static int atalk_rename(struct vfs_handle_struct *handle, const char *oldname, const char *newname)
226 {
227         int ret = 0;
228         char *adbl_path = 0;
229         char *orig_path = 0;
230         SMB_STRUCT_STAT adbl_info;
231         SMB_STRUCT_STAT orig_info;
232         TALLOC_CTX *ctx;
233
234         ret = SMB_VFS_NEXT_RENAME(handle, oldname, newname);
235
236         if (!oldname) return ret;
237
238         if (!(ctx = talloc_init("rename_file")))
239                 return ret;
240
241         if (atalk_build_paths(ctx, handle->conn->origpath, oldname, &adbl_path, &orig_path,
242           &adbl_info, &orig_info) != 0)
243                 goto exit_rename;
244
245         if (S_ISDIR(orig_info.st_mode) || S_ISREG(orig_info.st_mode)) {
246                 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));              
247                 goto exit_rename;
248         }
249
250         atalk_unlink_file(adbl_path);
251
252 exit_rename:
253         talloc_destroy(ctx);
254         return ret;
255 }
256
257 static int atalk_unlink(struct vfs_handle_struct *handle, const char *path)
258 {
259         int ret = 0, i;
260         char *adbl_path = 0;
261         char *orig_path = 0;
262         SMB_STRUCT_STAT adbl_info;
263         SMB_STRUCT_STAT orig_info;
264         TALLOC_CTX *ctx;
265
266         ret = SMB_VFS_NEXT_UNLINK(handle, path);
267
268         if (!path) return ret;
269
270         /* no .AppleDouble sync if veto or hide list is empty,
271          * otherwise "Cannot find the specified file" error will be caused
272          */
273
274         if (!handle->conn->veto_list) return ret;
275         if (!handle->conn->hide_list) return ret;
276
277         for (i = 0; handle->conn->veto_list[i].name; i ++) {
278                 if (strstr(handle->conn->veto_list[i].name, APPLEDOUBLE))
279                         break;
280         }
281
282         if (!handle->conn->veto_list[i].name) {
283                 for (i = 0; handle->conn->hide_list[i].name; i ++) {
284                         if (strstr(handle->conn->hide_list[i].name, APPLEDOUBLE))
285                                 break;
286                         else {
287                                 DEBUG(3, ("ATALK: %s is not hidden, skipped..\n",
288                                   APPLEDOUBLE));                
289                                 return ret;
290                         }
291                 }
292         }
293
294         if (!(ctx = talloc_init("unlink_file")))
295                 return ret;
296
297         if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
298           &adbl_info, &orig_info) != 0)
299                 goto exit_unlink;
300
301         if (S_ISDIR(orig_info.st_mode) || S_ISREG(orig_info.st_mode)) {
302                 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
303                 goto exit_unlink;
304         }
305
306         atalk_unlink_file(adbl_path);
307
308 exit_unlink:    
309         talloc_destroy(ctx);
310         return ret;
311 }
312
313 static int atalk_chmod(struct vfs_handle_struct *handle, const char *path, mode_t mode)
314 {
315         int ret = 0;
316         char *adbl_path = 0;
317         char *orig_path = 0;
318         SMB_STRUCT_STAT adbl_info;
319         SMB_STRUCT_STAT orig_info;
320         TALLOC_CTX *ctx;
321
322         ret = SMB_VFS_NEXT_CHMOD(handle, path, mode);
323
324         if (!path) return ret;
325
326         if (!(ctx = talloc_init("chmod_file")))
327                 return ret;
328
329         if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
330           &adbl_info, &orig_info) != 0)
331                 goto exit_chmod;
332
333         if (!S_ISDIR(orig_info.st_mode) && !S_ISREG(orig_info.st_mode)) {
334                 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));              
335                 goto exit_chmod;
336         }
337
338         chmod(adbl_path, ADOUBLEMODE);
339
340 exit_chmod:     
341         talloc_destroy(ctx);
342         return ret;
343 }
344
345 static int atalk_chown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
346 {
347         int ret = 0;
348         char *adbl_path = 0;
349         char *orig_path = 0;
350         SMB_STRUCT_STAT adbl_info;
351         SMB_STRUCT_STAT orig_info;
352         TALLOC_CTX *ctx;
353
354         ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
355
356         if (!path) return ret;
357
358         if (!(ctx = talloc_init("chown_file")))
359                 return ret;
360
361         if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
362           &adbl_info, &orig_info) != 0)
363                 goto exit_chown;
364
365         if (!S_ISDIR(orig_info.st_mode) && !S_ISREG(orig_info.st_mode)) {
366                 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));              
367                 goto exit_chown;
368         }
369
370         chown(adbl_path, uid, gid);
371
372 exit_chown:     
373         talloc_destroy(ctx);
374         return ret;
375 }
376
377 static int atalk_lchown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
378 {
379         int ret = 0;
380         char *adbl_path = 0;
381         char *orig_path = 0;
382         SMB_STRUCT_STAT adbl_info;
383         SMB_STRUCT_STAT orig_info;
384         TALLOC_CTX *ctx;
385
386         ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
387
388         if (!path) return ret;
389
390         if (!(ctx = talloc_init("lchown_file")))
391                 return ret;
392
393         if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
394           &adbl_info, &orig_info) != 0)
395                 goto exit_lchown;
396
397         if (!S_ISDIR(orig_info.st_mode) && !S_ISREG(orig_info.st_mode)) {
398                 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));              
399                 goto exit_lchown;
400         }
401
402         lchown(adbl_path, uid, gid);
403
404 exit_lchown:    
405         talloc_destroy(ctx);
406         return ret;
407 }
408
409 static vfs_op_tuple atalk_ops[] = {
410     
411         /* Directory operations */
412
413         {SMB_VFS_OP(atalk_opendir),             SMB_VFS_OP_OPENDIR,     SMB_VFS_LAYER_TRANSPARENT},
414         {SMB_VFS_OP(atalk_rmdir),               SMB_VFS_OP_RMDIR,       SMB_VFS_LAYER_TRANSPARENT},
415
416         /* File operations */
417
418         {SMB_VFS_OP(atalk_rename),              SMB_VFS_OP_RENAME,      SMB_VFS_LAYER_TRANSPARENT},
419         {SMB_VFS_OP(atalk_unlink),              SMB_VFS_OP_UNLINK,      SMB_VFS_LAYER_TRANSPARENT},
420         {SMB_VFS_OP(atalk_chmod),               SMB_VFS_OP_CHMOD,       SMB_VFS_LAYER_TRANSPARENT},
421         {SMB_VFS_OP(atalk_chown),               SMB_VFS_OP_CHOWN,       SMB_VFS_LAYER_TRANSPARENT},
422         {SMB_VFS_OP(atalk_lchown),              SMB_VFS_OP_LCHOWN,      SMB_VFS_LAYER_TRANSPARENT},
423         
424         /* Finish VFS operations definition */
425         
426         {SMB_VFS_OP(NULL),                      SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
427 };
428
429 NTSTATUS vfs_netatalk_init(void);
430 NTSTATUS vfs_netatalk_init(void)
431 {
432         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "netatalk", atalk_ops);
433 }