Commit just a little more infrastructure for HAVE_GETDIRENTRIES
[Samba/gebeck_regimport.git] / source3 / modules / vfs_netatalk.c
blobc869922a4c7543e1d4e05575983bded05a0e9918
1 /*
2 * AppleTalk VFS module for Samba-3.x
4 * Copyright (C) Alexei Kotovich, 2002
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "config.h"
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #ifdef HAVE_UTIME_H
25 #include <utime.h>
26 #endif
27 #ifdef HAVE_DIRENT_H
28 #include <dirent.h>
29 #endif
30 #ifdef HAVE_FCNTL_H
31 #include <fcntl.h>
32 #endif
33 #include <errno.h>
34 #include <string.h>
35 #include <includes.h>
36 #include <vfs.h>
38 #define APPLEDOUBLE ".AppleDouble"
39 #define ADOUBLEMODE 0777
41 /* atalk functions */
43 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
44 const char *fname, char **adbl_path, char **orig_path,
45 SMB_STRUCT_STAT *adbl_info, SMB_STRUCT_STAT *orig_info);
47 static int atalk_unlink_file(const char *path);
49 static struct vfs_ops default_vfs_ops; /* For passthrough operation */
50 static struct smb_vfs_handle_struct *atalk_handle;
52 static int atalk_get_path_ptr(char *path)
54 int i = 0;
55 int ptr = 0;
57 for (i = 0; path[i]; i ++) {
58 if (path[i] == '/')
59 ptr = i;
60 /* get out some 'spam';) from win32's file name */
61 else if (path[i] == ':') {
62 path[i] = '\0';
63 break;
67 return ptr;
70 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path, const char *fname,
71 char **adbl_path, char **orig_path,
72 SMB_STRUCT_STAT *adbl_info, SMB_STRUCT_STAT *orig_info)
74 int ptr0 = 0;
75 int ptr1 = 0;
76 char *dname = 0;
77 char *name = 0;
79 if (!ctx || !path || !fname || !adbl_path || !orig_path ||
80 !adbl_info || !orig_info)
81 return -1;
82 #if 0
83 DEBUG(3, ("ATALK: PATH: %s[%s]\n", path, fname));
84 #endif
85 if (strstr(path, APPLEDOUBLE) || strstr(fname, APPLEDOUBLE)) {
86 DEBUG(3, ("ATALK: path %s[%s] already contains %s\n", path, fname, APPLEDOUBLE));
87 return -1;
90 if (fname[0] == '.') ptr0 ++;
91 if (fname[1] == '/') ptr0 ++;
93 *orig_path = talloc_asprintf(ctx, "%s/%s", path, &fname[ptr0]);
95 /* get pointer to last '/' */
96 ptr1 = atalk_get_path_ptr(*orig_path);
98 sys_lstat(*orig_path, orig_info);
100 if (S_ISDIR(orig_info->st_mode)) {
101 *adbl_path = talloc_asprintf(ctx, "%s/%s/%s/",
102 path, &fname[ptr0], APPLEDOUBLE);
103 } else {
104 dname = talloc_strdup(ctx, *orig_path);
105 dname[ptr1] = '\0';
106 name = *orig_path;
107 *adbl_path = talloc_asprintf(ctx, "%s/%s/%s",
108 dname, APPLEDOUBLE, &name[ptr1 + 1]);
110 #if 0
111 DEBUG(3, ("ATALK: DEBUG:\n%s\n%s\n", *orig_path, *adbl_path));
112 #endif
113 sys_lstat(*adbl_path, adbl_info);
114 return 0;
117 static int atalk_unlink_file(const char *path)
119 int ret = 0;
121 become_root();
122 ret = unlink(path);
123 unbecome_root();
125 return ret;
128 static void atalk_add_to_list(name_compare_entry **list)
130 int i, count = 0;
131 name_compare_entry *new_list = 0;
132 name_compare_entry *cur_list = 0;
134 cur_list = *list;
136 if (cur_list) {
137 for (i = 0, count = 0; cur_list[i].name; i ++, count ++) {
138 if (strstr(cur_list[i].name, APPLEDOUBLE))
139 return;
143 if (!(new_list = calloc(1,
144 (count == 0 ? 1 : count + 1) * sizeof(name_compare_entry))))
145 return;
147 for (i = 0; i < count; i ++) {
148 new_list[i].name = strdup(cur_list[i].name);
149 new_list[i].is_wild = cur_list[i].is_wild;
152 new_list[i].name = strdup(APPLEDOUBLE);
153 new_list[i].is_wild = False;
155 free_namearray(*list);
157 *list = new_list;
158 new_list = 0;
159 cur_list = 0;
162 static void atalk_rrmdir(TALLOC_CTX *ctx, char *path)
164 int n;
165 char *dpath;
166 struct dirent **namelist;
168 if (!path) return;
170 n = scandir(path, &namelist, 0, alphasort);
171 if (n < 0) {
172 return;
173 } else {
174 while (n --) {
175 if (strcmp(namelist[n]->d_name, ".") == 0 ||
176 strcmp(namelist[n]->d_name, "..") == 0)
177 continue;
178 if (!(dpath = talloc_asprintf(ctx, "%s/%s",
179 path, namelist[n]->d_name)))
180 continue;
181 atalk_unlink_file(dpath);
182 free(namelist[n]);
187 /* Disk operations */
189 /* Directory operations */
191 DIR *atalk_opendir(struct connection_struct *conn, const char *fname)
193 DIR *ret = 0;
195 ret = default_vfs_ops.opendir(conn, fname);
198 * when we try to perform delete operation upon file which has fork
199 * in ./.AppleDouble and this directory wasn't hidden by Samba,
200 * MS Windows explorer causes the error: "Cannot find the specified file"
201 * There is some workaround to avoid this situation, i.e. if
202 * connection has not .AppleDouble entry in either veto or hide
203 * list then it would be nice to add one.
206 atalk_add_to_list(&conn->hide_list);
207 atalk_add_to_list(&conn->veto_list);
209 return ret;
212 static int atalk_rmdir(struct connection_struct *conn, const char *path)
214 BOOL add = False;
215 TALLOC_CTX *ctx = 0;
216 char *dpath;
218 if (!conn || !conn->origpath || !path) goto exit_rmdir;
220 /* due to there is no way to change bDeleteVetoFiles variable
221 * from this module, gotta use talloc stuff..
224 strstr(path, APPLEDOUBLE) ? (add = False) : (add = True);
226 if (!(ctx = talloc_init("remove_directory")))
227 goto exit_rmdir;
229 if (!(dpath = talloc_asprintf(ctx, "%s/%s%s",
230 conn->origpath, path, add ? "/"APPLEDOUBLE : "")))
231 goto exit_rmdir;
233 atalk_rrmdir(ctx, dpath);
235 exit_rmdir:
236 talloc_destroy(ctx);
237 return default_vfs_ops.rmdir(conn, path);
240 /* File operations */
242 static int atalk_rename(struct connection_struct *conn, const char *old, const char *new)
244 int ret = 0;
245 char *adbl_path = 0;
246 char *orig_path = 0;
247 SMB_STRUCT_STAT adbl_info;
248 SMB_STRUCT_STAT orig_info;
249 TALLOC_CTX *ctx;
251 ret = default_vfs_ops.rename(conn, old, new);
253 if (!conn || !old) return ret;
255 if (!(ctx = talloc_init("rename_file")))
256 return ret;
258 if (atalk_build_paths(ctx, conn->origpath, old, &adbl_path, &orig_path,
259 &adbl_info, &orig_info) != 0)
260 return ret;
262 if (S_ISDIR(orig_info.st_mode) || S_ISREG(orig_info.st_mode)) {
263 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
264 goto exit_rename;
267 atalk_unlink_file(adbl_path);
269 exit_rename:
270 talloc_destroy(ctx);
271 return ret;
274 static int atalk_unlink(struct connection_struct *conn, const char *path)
276 int ret = 0, i;
277 char *adbl_path = 0;
278 char *orig_path = 0;
279 SMB_STRUCT_STAT adbl_info;
280 SMB_STRUCT_STAT orig_info;
281 TALLOC_CTX *ctx;
283 ret = default_vfs_ops.unlink(conn, path);
285 if (!conn || !path) return ret;
287 /* no .AppleDouble sync if veto or hide list is empty,
288 * otherwise "Cannot find the specified file" error will be caused
291 if (!conn->veto_list) return ret;
292 if (!conn->hide_list) return ret;
294 for (i = 0; conn->veto_list[i].name; i ++) {
295 if (strstr(conn->veto_list[i].name, APPLEDOUBLE))
296 break;
299 if (!conn->veto_list[i].name) {
300 for (i = 0; conn->hide_list[i].name; i ++) {
301 if (strstr(conn->hide_list[i].name, APPLEDOUBLE))
302 break;
303 else {
304 DEBUG(3, ("ATALK: %s is not hidden, skipped..\n",
305 APPLEDOUBLE));
306 return ret;
311 if (!(ctx = talloc_init("unlink_file")))
312 return ret;
314 if (atalk_build_paths(ctx, conn->origpath, path, &adbl_path, &orig_path,
315 &adbl_info, &orig_info) != 0)
316 return ret;
318 if (S_ISDIR(orig_info.st_mode) || S_ISREG(orig_info.st_mode)) {
319 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
320 goto exit_unlink;
323 atalk_unlink_file(adbl_path);
325 exit_unlink:
326 talloc_destroy(ctx);
327 return ret;
330 static int atalk_chmod(struct connection_struct *conn, const char *path, mode_t mode)
332 int ret = 0;
333 char *adbl_path = 0;
334 char *orig_path = 0;
335 SMB_STRUCT_STAT adbl_info;
336 SMB_STRUCT_STAT orig_info;
337 TALLOC_CTX *ctx;
339 ret = default_vfs_ops.chmod(conn, path, mode);
341 if (!conn || !path) return ret;
343 if (!(ctx = talloc_init("chmod_file")))
344 return ret;
346 if (atalk_build_paths(ctx, conn->origpath, path, &adbl_path, &orig_path,
347 &adbl_info, &orig_info) != 0)
348 return ret;
350 if (!S_ISDIR(orig_info.st_mode) && !S_ISREG(orig_info.st_mode)) {
351 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
352 goto exit_chmod;
355 chmod(adbl_path, ADOUBLEMODE);
357 exit_chmod:
358 talloc_destroy(ctx);
359 return ret;
362 static int atalk_chown(struct connection_struct *conn, const char *path, uid_t uid, gid_t gid)
364 int ret = 0;
365 char *adbl_path = 0;
366 char *orig_path = 0;
367 SMB_STRUCT_STAT adbl_info;
368 SMB_STRUCT_STAT orig_info;
369 TALLOC_CTX *ctx;
371 ret = default_vfs_ops.chown(conn, path, uid, gid);
373 if (!conn || !path) return ret;
375 if (!(ctx = talloc_init("chown_file")))
376 return ret;
378 if (atalk_build_paths(ctx, conn->origpath, path, &adbl_path, &orig_path,
379 &adbl_info, &orig_info) != 0)
380 return ret;
382 if (!S_ISDIR(orig_info.st_mode) && !S_ISREG(orig_info.st_mode)) {
383 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
384 goto exit_chown;
387 chown(adbl_path, uid, gid);
389 exit_chown:
390 talloc_destroy(ctx);
391 return ret;
394 static vfs_op_tuple atalk_ops[] = {
396 /* Directory operations */
398 {atalk_opendir, SMB_VFS_OP_OPENDIR, SMB_VFS_LAYER_TRANSPARENT},
399 {atalk_rmdir, SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_TRANSPARENT},
401 /* File operations */
403 {atalk_rename, SMB_VFS_OP_RENAME, SMB_VFS_LAYER_TRANSPARENT},
404 {atalk_unlink, SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_TRANSPARENT},
405 {atalk_chmod, SMB_VFS_OP_CHMOD, SMB_VFS_LAYER_TRANSPARENT},
406 {atalk_chown, SMB_VFS_OP_CHOWN, SMB_VFS_LAYER_TRANSPARENT},
408 /* Finish VFS operations definition */
410 {NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
413 /* VFS initialisation function. Return vfs_op_tuple array back to SAMBA. */
414 vfs_op_tuple *vfs_init(int *vfs_version, struct vfs_ops *def_vfs_ops,
415 struct smb_vfs_handle_struct *vfs_handle)
417 *vfs_version = SMB_VFS_INTERFACE_VERSION;
418 memcpy(&default_vfs_ops, def_vfs_ops, sizeof(struct vfs_ops));
420 atalk_handle = vfs_handle;
422 DEBUG(3, ("ATALK: vfs module loaded\n"));
423 return atalk_ops;
426 /* VFS finalization function. */
427 void vfs_done(connection_struct *conn)
429 DEBUG(3, ("ATALK: vfs module unloaded\n"));