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