2 * AppleTalk VFS module for Samba-3.x
4 * Copyright (C) Alexei Kotovich, 2002
5 * Copyright (C) Stefan (metze) Metzmacher, 2003
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.
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.
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/>.
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
26 #define DBGC_CLASS DBGC_VFS
28 #define APPLEDOUBLE ".AppleDouble"
29 #define ADOUBLEMODE 0777
33 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
35 char **adbl_path, char **orig_path,
36 SMB_STRUCT_STAT *adbl_info,
37 SMB_STRUCT_STAT *orig_info);
39 static int atalk_unlink_file(const char *path);
41 static int atalk_get_path_ptr(char *path)
46 for (i = 0; path[i]; i ++) {
49 /* get out some 'spam';) from win32's file name */
50 else if (path[i] == ':') {
59 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
61 char **adbl_path, char **orig_path,
62 SMB_STRUCT_STAT *adbl_info,
63 SMB_STRUCT_STAT *orig_info)
70 if (!ctx || !path || !fname || !adbl_path || !orig_path ||
71 !adbl_info || !orig_info)
74 DEBUG(3, ("ATALK: PATH: %s[%s]\n", path, fname));
76 if (strstr_m(path, APPLEDOUBLE) || strstr_m(fname, APPLEDOUBLE)) {
77 DEBUG(3, ("ATALK: path %s[%s] already contains %s\n", path, fname, APPLEDOUBLE));
81 if (fname[0] == '.') ptr0 ++;
82 if (fname[1] == '/') ptr0 ++;
84 *orig_path = talloc_asprintf(ctx, "%s/%s", path, &fname[ptr0]);
86 /* get pointer to last '/' */
87 ptr1 = atalk_get_path_ptr(*orig_path);
89 sys_lstat(*orig_path, orig_info, false);
91 if (S_ISDIR(orig_info->st_ex_mode)) {
92 *adbl_path = talloc_asprintf(ctx, "%s/%s/%s/",
93 path, &fname[ptr0], APPLEDOUBLE);
95 dname = talloc_strdup(ctx, *orig_path);
98 *adbl_path = talloc_asprintf(ctx, "%s/%s/%s",
99 dname, APPLEDOUBLE, &name[ptr1 + 1]);
102 DEBUG(3, ("ATALK: DEBUG:\n%s\n%s\n", *orig_path, *adbl_path));
104 sys_lstat(*adbl_path, adbl_info, false);
108 static int atalk_unlink_file(const char *path)
119 static void atalk_add_to_list(name_compare_entry **list)
122 name_compare_entry *new_list = 0;
123 name_compare_entry *cur_list = 0;
128 for (i = 0, count = 0; cur_list[i].name; i ++, count ++) {
129 if (strstr_m(cur_list[i].name, APPLEDOUBLE))
134 if (!(new_list = SMB_CALLOC_ARRAY(name_compare_entry, count + 2)))
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;
142 new_list[i].name = SMB_STRDUP(APPLEDOUBLE);
143 new_list[i].is_wild = False;
145 free_namearray(*list);
152 static void atalk_rrmdir(TALLOC_CTX *ctx, char *path)
155 struct dirent *dent = 0;
163 while (NULL != (dent = readdir(dir))) {
164 if (strcmp(dent->d_name, ".") == 0 ||
165 strcmp(dent->d_name, "..") == 0)
167 if (!(dpath = talloc_asprintf(ctx, "%s/%s",
168 path, dent->d_name)))
170 atalk_unlink_file(dpath);
176 /* Disk operations */
178 /* Directory operations */
180 static DIR *atalk_opendir(struct vfs_handle_struct *handle, const char *fname, const char *mask, uint32_t attr)
184 ret = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
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.
195 atalk_add_to_list(&handle->conn->hide_list);
196 atalk_add_to_list(&handle->conn->veto_list);
201 static DIR *atalk_fdopendir(struct vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32_t attr)
205 ret = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
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.
220 atalk_add_to_list(&handle->conn->hide_list);
221 atalk_add_to_list(&handle->conn->veto_list);
226 static int atalk_rmdir(struct vfs_handle_struct *handle,
227 const struct smb_filename *smb_fname)
231 const char *path = smb_fname->base_name;
234 if (!handle->conn->cwd || !path) goto exit_rmdir;
236 /* due to there is no way to change bDeleteVetoFiles variable
237 * from this module, gotta use talloc stuff..
240 strstr_m(path, APPLEDOUBLE) ? (add = False) : (add = True);
242 if (!(ctx = talloc_init("remove_directory")))
245 if (!(dpath = talloc_asprintf(ctx, "%s/%s%s",
246 handle->conn->cwd, path, add ? "/"APPLEDOUBLE : "")))
249 atalk_rrmdir(ctx, dpath);
253 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
256 /* File operations */
258 static int atalk_rename(struct vfs_handle_struct *handle,
259 const struct smb_filename *smb_fname_src,
260 const struct smb_filename *smb_fname_dst)
263 char *oldname = NULL;
264 char *adbl_path = NULL;
265 char *orig_path = NULL;
266 SMB_STRUCT_STAT adbl_info;
267 SMB_STRUCT_STAT orig_info;
270 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
272 status = get_full_smb_filename(talloc_tos(), smb_fname_src, &oldname);
273 if (!NT_STATUS_IS_OK(status)) {
277 if (atalk_build_paths(talloc_tos(), handle->conn->cwd, oldname,
278 &adbl_path, &orig_path, &adbl_info,
282 if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
283 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
287 atalk_unlink_file(adbl_path);
290 TALLOC_FREE(oldname);
291 TALLOC_FREE(adbl_path);
292 TALLOC_FREE(orig_path);
296 static int atalk_unlink(struct vfs_handle_struct *handle,
297 const struct smb_filename *smb_fname)
301 char *adbl_path = NULL;
302 char *orig_path = NULL;
303 SMB_STRUCT_STAT adbl_info;
304 SMB_STRUCT_STAT orig_info;
307 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
309 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
310 if (!NT_STATUS_IS_OK(status)) {
314 /* no .AppleDouble sync if veto or hide list is empty,
315 * otherwise "Cannot find the specified file" error will be caused
318 if (!handle->conn->veto_list) return ret;
319 if (!handle->conn->hide_list) return ret;
321 for (i = 0; handle->conn->veto_list[i].name; i ++) {
322 if (strstr_m(handle->conn->veto_list[i].name, APPLEDOUBLE))
326 if (!handle->conn->veto_list[i].name) {
327 for (i = 0; handle->conn->hide_list[i].name; i ++) {
328 if (strstr_m(handle->conn->hide_list[i].name, APPLEDOUBLE))
331 DEBUG(3, ("ATALK: %s is not hidden, skipped..\n",
338 if (atalk_build_paths(talloc_tos(), handle->conn->cwd, path,
339 &adbl_path, &orig_path,
340 &adbl_info, &orig_info) != 0)
343 if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
344 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
348 atalk_unlink_file(adbl_path);
352 TALLOC_FREE(adbl_path);
353 TALLOC_FREE(orig_path);
357 static int atalk_chmod(struct vfs_handle_struct *handle, const char *path, mode_t mode)
362 SMB_STRUCT_STAT adbl_info;
363 SMB_STRUCT_STAT orig_info;
366 ret = SMB_VFS_NEXT_CHMOD(handle, path, mode);
368 if (!path) return ret;
370 if (!(ctx = talloc_init("chmod_file")))
373 if (atalk_build_paths(ctx, handle->conn->cwd, path, &adbl_path,
374 &orig_path, &adbl_info, &orig_info) != 0)
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));
382 chmod(adbl_path, ADOUBLEMODE);
389 static int atalk_chown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
394 SMB_STRUCT_STAT adbl_info;
395 SMB_STRUCT_STAT orig_info;
398 ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
400 if (!path) return ret;
402 if (!(ctx = talloc_init("chown_file")))
405 if (atalk_build_paths(ctx, handle->conn->cwd, path,
406 &adbl_path, &orig_path,
407 &adbl_info, &orig_info) != 0)
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));
415 if (chown(adbl_path, uid, gid) == -1) {
416 DEBUG(3, ("ATALK: chown error %s\n", strerror(errno)));
424 static int atalk_lchown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
429 SMB_STRUCT_STAT adbl_info;
430 SMB_STRUCT_STAT orig_info;
433 ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
435 if (!path) return ret;
437 if (!(ctx = talloc_init("lchown_file")))
440 if (atalk_build_paths(ctx, handle->conn->cwd, path,
441 &adbl_path, &orig_path,
442 &adbl_info, &orig_info) != 0)
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));
450 if (lchown(adbl_path, uid, gid) == -1) {
451 DEBUG(3, ("ATALK: lchown error %s\n", strerror(errno)));
459 static struct vfs_fn_pointers vfs_netatalk_fns = {
460 .opendir_fn = atalk_opendir,
461 .fdopendir_fn = atalk_fdopendir,
462 .rmdir_fn = atalk_rmdir,
463 .rename_fn = atalk_rename,
464 .unlink_fn = atalk_unlink,
465 .chmod_fn = atalk_chmod,
466 .chown_fn = atalk_chown,
467 .lchown_fn = atalk_lchown,
470 NTSTATUS vfs_netatalk_init(void);
471 NTSTATUS vfs_netatalk_init(void)
473 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "netatalk",