s3: VFS: Change SMB_VFS_SYS_ACL_BLOB_GET_FILE to use const struct smb_filename *...
[Samba.git] / source3 / modules / vfs_netatalk.c
blob5715c6dd6ac24c49242427f77f1434cb9fe0b2d3
1 /*
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/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_VFS
28 #define APPLEDOUBLE ".AppleDouble"
29 #define ADOUBLEMODE 0777
31 /* atalk functions */
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);
39 static int atalk_unlink_file(const char *path);
41 static int atalk_get_path_ptr(char *path)
43 int i = 0;
44 int ptr = 0;
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;
56 return ptr;
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)
65 int ptr0 = 0;
66 int ptr1 = 0;
67 char *dname = 0;
68 char *name = 0;
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;
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);
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]);
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;
108 static int atalk_unlink_file(const char *path)
110 int ret = 0;
112 become_root();
113 ret = unlink(path);
114 unbecome_root();
116 return ret;
119 static void atalk_add_to_list(name_compare_entry **list)
121 int i, count = 0;
122 name_compare_entry *new_list = 0;
123 name_compare_entry *cur_list = 0;
125 cur_list = *list;
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;
134 if (!(new_list = SMB_CALLOC_ARRAY(name_compare_entry, count + 2)))
135 return;
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);
147 *list = new_list;
148 new_list = 0;
149 cur_list = 0;
152 static void atalk_rrmdir(TALLOC_CTX *ctx, char *path)
154 char *dpath;
155 struct dirent *dent = 0;
156 DIR *dir;
158 if (!path) return;
160 dir = opendir(path);
161 if (!dir) return;
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);
173 closedir(dir);
176 /* Disk operations */
178 /* Directory operations */
180 static DIR *atalk_opendir(struct vfs_handle_struct *handle,
181 const struct smb_filename *smb_fname,
182 const char *mask,
183 uint32_t attr)
185 DIR *ret = 0;
187 ret = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
190 * when we try to perform delete operation upon file which has fork
191 * in ./.AppleDouble and this directory wasn't hidden by Samba,
192 * MS Windows explorer causes the error: "Cannot find the specified file"
193 * There is some workaround to avoid this situation, i.e. if
194 * connection has not .AppleDouble entry in either veto or hide
195 * list then it would be nice to add one.
198 atalk_add_to_list(&handle->conn->hide_list);
199 atalk_add_to_list(&handle->conn->veto_list);
201 return ret;
204 static DIR *atalk_fdopendir(struct vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32_t attr)
206 DIR *ret = 0;
208 ret = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
210 if (ret == NULL) {
211 return ret;
215 * when we try to perform delete operation upon file which has fork
216 * in ./.AppleDouble and this directory wasn't hidden by Samba,
217 * MS Windows explorer causes the error: "Cannot find the specified file"
218 * There is some workaround to avoid this situation, i.e. if
219 * connection has not .AppleDouble entry in either veto or hide
220 * list then it would be nice to add one.
223 atalk_add_to_list(&handle->conn->hide_list);
224 atalk_add_to_list(&handle->conn->veto_list);
226 return ret;
229 static int atalk_rmdir(struct vfs_handle_struct *handle,
230 const struct smb_filename *smb_fname)
232 bool add = False;
233 TALLOC_CTX *ctx = 0;
234 const char *path = smb_fname->base_name;
235 char *dpath;
237 if (!handle->conn->cwd || !path) goto exit_rmdir;
239 /* due to there is no way to change bDeleteVetoFiles variable
240 * from this module, gotta use talloc stuff..
243 strstr_m(path, APPLEDOUBLE) ? (add = False) : (add = True);
245 if (!(ctx = talloc_init("remove_directory")))
246 goto exit_rmdir;
248 if (!(dpath = talloc_asprintf(ctx, "%s/%s%s",
249 handle->conn->cwd, path, add ? "/"APPLEDOUBLE : "")))
250 goto exit_rmdir;
252 atalk_rrmdir(ctx, dpath);
254 exit_rmdir:
255 talloc_destroy(ctx);
256 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
259 /* File operations */
261 static int atalk_rename(struct vfs_handle_struct *handle,
262 const struct smb_filename *smb_fname_src,
263 const struct smb_filename *smb_fname_dst)
265 int ret = 0;
266 char *oldname = NULL;
267 char *adbl_path = NULL;
268 char *orig_path = NULL;
269 SMB_STRUCT_STAT adbl_info;
270 SMB_STRUCT_STAT orig_info;
271 NTSTATUS status;
273 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
275 status = get_full_smb_filename(talloc_tos(), smb_fname_src, &oldname);
276 if (!NT_STATUS_IS_OK(status)) {
277 return ret;
280 if (atalk_build_paths(talloc_tos(), handle->conn->cwd, oldname,
281 &adbl_path, &orig_path, &adbl_info,
282 &orig_info) != 0)
283 goto exit_rename;
285 if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
286 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
287 goto exit_rename;
290 atalk_unlink_file(adbl_path);
292 exit_rename:
293 TALLOC_FREE(oldname);
294 TALLOC_FREE(adbl_path);
295 TALLOC_FREE(orig_path);
296 return ret;
299 static int atalk_unlink(struct vfs_handle_struct *handle,
300 const struct smb_filename *smb_fname)
302 int ret = 0, i;
303 char *path = NULL;
304 char *adbl_path = NULL;
305 char *orig_path = NULL;
306 SMB_STRUCT_STAT adbl_info;
307 SMB_STRUCT_STAT orig_info;
308 NTSTATUS status;
310 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
312 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
313 if (!NT_STATUS_IS_OK(status)) {
314 return ret;
317 /* no .AppleDouble sync if veto or hide list is empty,
318 * otherwise "Cannot find the specified file" error will be caused
321 if (!handle->conn->veto_list) return ret;
322 if (!handle->conn->hide_list) return ret;
324 for (i = 0; handle->conn->veto_list[i].name; i ++) {
325 if (strstr_m(handle->conn->veto_list[i].name, APPLEDOUBLE))
326 break;
329 if (!handle->conn->veto_list[i].name) {
330 for (i = 0; handle->conn->hide_list[i].name; i ++) {
331 if (strstr_m(handle->conn->hide_list[i].name, APPLEDOUBLE))
332 break;
333 else {
334 DEBUG(3, ("ATALK: %s is not hidden, skipped..\n",
335 APPLEDOUBLE));
336 goto exit_unlink;
341 if (atalk_build_paths(talloc_tos(), handle->conn->cwd, path,
342 &adbl_path, &orig_path,
343 &adbl_info, &orig_info) != 0)
344 goto exit_unlink;
346 if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
347 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
348 goto exit_unlink;
351 atalk_unlink_file(adbl_path);
353 exit_unlink:
354 TALLOC_FREE(path);
355 TALLOC_FREE(adbl_path);
356 TALLOC_FREE(orig_path);
357 return ret;
360 static int atalk_chmod(struct vfs_handle_struct *handle,
361 const struct smb_filename *smb_fname,
362 mode_t mode)
364 int ret = 0;
365 int ret1 = 0;
366 char *adbl_path = 0;
367 char *orig_path = 0;
368 SMB_STRUCT_STAT adbl_info;
369 SMB_STRUCT_STAT orig_info;
370 TALLOC_CTX *ctx;
372 ret = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
374 if (!(ctx = talloc_init("chmod_file")))
375 return ret;
377 ret1 = atalk_build_paths(ctx,
378 handle->conn->cwd,
379 smb_fname->base_name,
380 &adbl_path,
381 &orig_path,
382 &adbl_info,
383 &orig_info);
384 if (ret1 != 0) {
385 goto exit_chmod;
388 if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
389 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
390 goto exit_chmod;
393 chmod(adbl_path, ADOUBLEMODE);
395 exit_chmod:
396 talloc_destroy(ctx);
397 return ret;
400 static int atalk_chown(struct vfs_handle_struct *handle,
401 const struct smb_filename *smb_fname,
402 uid_t uid,
403 gid_t gid)
405 int ret = 0;
406 char *adbl_path = 0;
407 char *orig_path = 0;
408 SMB_STRUCT_STAT adbl_info;
409 SMB_STRUCT_STAT orig_info;
410 TALLOC_CTX *ctx;
412 ret = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
414 if (!(ctx = talloc_init("chown_file")))
415 return ret;
417 if (atalk_build_paths(ctx, handle->conn->cwd, smb_fname->base_name,
418 &adbl_path, &orig_path,
419 &adbl_info, &orig_info) != 0)
420 goto exit_chown;
422 if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
423 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
424 goto exit_chown;
427 if (chown(adbl_path, uid, gid) == -1) {
428 DEBUG(3, ("ATALK: chown error %s\n", strerror(errno)));
431 exit_chown:
432 talloc_destroy(ctx);
433 return ret;
436 static int atalk_lchown(struct vfs_handle_struct *handle,
437 const struct smb_filename *smb_fname,
438 uid_t uid,
439 gid_t gid)
441 int ret = 0;
442 char *adbl_path = 0;
443 char *orig_path = 0;
444 SMB_STRUCT_STAT adbl_info;
445 SMB_STRUCT_STAT orig_info;
446 TALLOC_CTX *ctx;
448 ret = SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
450 if (!(ctx = talloc_init("lchown_file")))
451 return ret;
453 if (atalk_build_paths(ctx, handle->conn->cwd, smb_fname->base_name,
454 &adbl_path, &orig_path,
455 &adbl_info, &orig_info) != 0)
456 goto exit_lchown;
458 if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
459 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
460 goto exit_lchown;
463 if (lchown(adbl_path, uid, gid) == -1) {
464 DEBUG(3, ("ATALK: lchown error %s\n", strerror(errno)));
467 exit_lchown:
468 talloc_destroy(ctx);
469 return ret;
472 static struct vfs_fn_pointers vfs_netatalk_fns = {
473 .opendir_fn = atalk_opendir,
474 .fdopendir_fn = atalk_fdopendir,
475 .rmdir_fn = atalk_rmdir,
476 .rename_fn = atalk_rename,
477 .unlink_fn = atalk_unlink,
478 .chmod_fn = atalk_chmod,
479 .chown_fn = atalk_chown,
480 .lchown_fn = atalk_lchown,
483 NTSTATUS vfs_netatalk_init(TALLOC_CTX *);
484 NTSTATUS vfs_netatalk_init(TALLOC_CTX *ctx)
486 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "netatalk",
487 &vfs_netatalk_fns);