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