lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[sfrench/samba-autobuild/.git] / source3 / modules / vfs_netatalk.c
index 0e20beca410ce991827b2d32c270620ef089ddee..5715c6dd6ac24c49242427f77f1434cb9fe0b2d3 100644 (file)
@@ -19,6 +19,8 @@
  */
 
 #include "includes.h"
+#include "smbd/smbd.h"
+#include "system/filesys.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_VFS
 /* atalk functions */
 
 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
-  const char *fname, char **adbl_path, char **orig_path, 
-  SMB_STRUCT_STAT *adbl_info, SMB_STRUCT_STAT *orig_info);
+                            const char *fname,
+                            char **adbl_path, char **orig_path,
+                            SMB_STRUCT_STAT *adbl_info,
+                            SMB_STRUCT_STAT *orig_info);
 
 static int atalk_unlink_file(const char *path);
 
@@ -52,9 +56,11 @@ static int atalk_get_path_ptr(char *path)
        return ptr;
 }
 
-static int atalk_build_paths(TALLOC_CTX *ctx, const char *path, const char *fname,
-                              char **adbl_path, char **orig_path,
-                              SMB_STRUCT_STAT *adbl_info, SMB_STRUCT_STAT *orig_info)
+static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
+                            const char *fname,
+                            char **adbl_path, char **orig_path,
+                            SMB_STRUCT_STAT *adbl_info,
+                            SMB_STRUCT_STAT *orig_info)
 {
        int ptr0 = 0;
        int ptr1 = 0;
@@ -67,7 +73,7 @@ static int atalk_build_paths(TALLOC_CTX *ctx, const char *path, const char *fnam
 #if 0
        DEBUG(3, ("ATALK: PATH: %s[%s]\n", path, fname));
 #endif
-       if (strstr(path, APPLEDOUBLE) || strstr(fname, APPLEDOUBLE)) {
+       if (strstr_m(path, APPLEDOUBLE) || strstr_m(fname, APPLEDOUBLE)) {
                DEBUG(3, ("ATALK: path %s[%s] already contains %s\n", path, fname, APPLEDOUBLE));
                return -1;
        }
@@ -80,7 +86,7 @@ static int atalk_build_paths(TALLOC_CTX *ctx, const char *path, const char *fnam
        /* get pointer to last '/' */
        ptr1 = atalk_get_path_ptr(*orig_path);
 
-       sys_lstat(*orig_path, orig_info);
+       sys_lstat(*orig_path, orig_info, false);
 
        if (S_ISDIR(orig_info->st_ex_mode)) {
                *adbl_path = talloc_asprintf(ctx, "%s/%s/%s/", 
@@ -95,7 +101,7 @@ static int atalk_build_paths(TALLOC_CTX *ctx, const char *path, const char *fnam
 #if 0
        DEBUG(3, ("ATALK: DEBUG:\n%s\n%s\n", *orig_path, *adbl_path)); 
 #endif
-       sys_lstat(*adbl_path, adbl_info);
+       sys_lstat(*adbl_path, adbl_info, false);
        return 0;
 }
 
@@ -120,12 +126,12 @@ static void atalk_add_to_list(name_compare_entry **list)
 
        if (cur_list) {
                for (i = 0, count = 0; cur_list[i].name; i ++, count ++) {
-                       if (strstr(cur_list[i].name, APPLEDOUBLE))
+                       if (strstr_m(cur_list[i].name, APPLEDOUBLE))
                                return;
                }
        }
 
-       if (!(new_list = SMB_CALLOC_ARRAY(name_compare_entry, (count == 0 ? 1 : count + 1))))
+       if (!(new_list = SMB_CALLOC_ARRAY(name_compare_entry, count + 2)))
                return;
 
        for (i = 0; i < count; i ++) {
@@ -146,15 +152,15 @@ static void atalk_add_to_list(name_compare_entry **list)
 static void atalk_rrmdir(TALLOC_CTX *ctx, char *path)
 {
        char *dpath;
-       SMB_STRUCT_DIRENT *dent = 0;
-       SMB_STRUCT_DIR *dir;
+       struct dirent *dent = 0;
+       DIR *dir;
 
        if (!path) return;
 
-       dir = sys_opendir(path);
+       dir = opendir(path);
        if (!dir) return;
 
-       while (NULL != (dent = sys_readdir(dir))) {
+       while (NULL != (dent = readdir(dir))) {
                if (strcmp(dent->d_name, ".") == 0 ||
                    strcmp(dent->d_name, "..") == 0)
                        continue;
@@ -164,18 +170,21 @@ static void atalk_rrmdir(TALLOC_CTX *ctx, char *path)
                atalk_unlink_file(dpath);
        }
 
-       sys_closedir(dir);
+       closedir(dir);
 }
 
 /* Disk operations */
 
 /* Directory operations */
 
-static SMB_STRUCT_DIR *atalk_opendir(struct vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
+static DIR *atalk_opendir(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       const char *mask,
+                       uint32_t attr)
 {
-       SMB_STRUCT_DIR *ret = 0;
+       DIR *ret = 0;
 
-       ret = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
+       ret = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
 
        /*
         * when we try to perform delete operation upon file which has fork
@@ -192,32 +201,59 @@ static SMB_STRUCT_DIR *atalk_opendir(struct vfs_handle_struct *handle, const cha
        return ret;
 }
 
-static int atalk_rmdir(struct vfs_handle_struct *handle, const char *path)
+static DIR *atalk_fdopendir(struct vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32_t attr)
+{
+       DIR *ret = 0;
+
+       ret = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
+
+       if (ret == NULL) {
+               return ret;
+       }
+
+       /*
+        * when we try to perform delete operation upon file which has fork
+        * in ./.AppleDouble and this directory wasn't hidden by Samba,
+        * MS Windows explorer causes the error: "Cannot find the specified file"
+        * There is some workaround to avoid this situation, i.e. if
+        * connection has not .AppleDouble entry in either veto or hide 
+        * list then it would be nice to add one.
+        */
+
+       atalk_add_to_list(&handle->conn->hide_list);
+       atalk_add_to_list(&handle->conn->veto_list);
+
+       return ret;
+}
+
+static int atalk_rmdir(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname)
 {
        bool add = False;
        TALLOC_CTX *ctx = 0;
+       const char *path = smb_fname->base_name;
        char *dpath;
 
-       if (!handle->conn->origpath || !path) goto exit_rmdir;
+       if (!handle->conn->cwd || !path) goto exit_rmdir;
 
        /* due to there is no way to change bDeleteVetoFiles variable
         * from this module, gotta use talloc stuff..
         */
 
-       strstr(path, APPLEDOUBLE) ? (add = False) : (add = True);
+       strstr_m(path, APPLEDOUBLE) ? (add = False) : (add = True);
 
        if (!(ctx = talloc_init("remove_directory")))
                goto exit_rmdir;
 
        if (!(dpath = talloc_asprintf(ctx, "%s/%s%s", 
-         handle->conn->origpath, path, add ? "/"APPLEDOUBLE : "")))
+         handle->conn->cwd, path, add ? "/"APPLEDOUBLE : "")))
                goto exit_rmdir;
 
        atalk_rrmdir(ctx, dpath);
 
 exit_rmdir:
        talloc_destroy(ctx);
-       return SMB_VFS_NEXT_RMDIR(handle, path);
+       return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
 }
 
 /* File operations */
@@ -241,7 +277,7 @@ static int atalk_rename(struct vfs_handle_struct *handle,
                return ret;
        }
 
-       if (atalk_build_paths(talloc_tos(), handle->conn->origpath, oldname,
+       if (atalk_build_paths(talloc_tos(), handle->conn->cwd, oldname,
                              &adbl_path, &orig_path, &adbl_info,
                              &orig_info) != 0)
                goto exit_rename;
@@ -254,24 +290,29 @@ static int atalk_rename(struct vfs_handle_struct *handle,
        atalk_unlink_file(adbl_path);
 
 exit_rename:
+       TALLOC_FREE(oldname);
        TALLOC_FREE(adbl_path);
        TALLOC_FREE(orig_path);
-       TALLOC_FREE(orig_path);
        return ret;
 }
 
-static int atalk_unlink(struct vfs_handle_struct *handle, const char *path)
+static int atalk_unlink(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname)
 {
        int ret = 0, i;
-       char *adbl_path = 0;
-       char *orig_path = 0;
+       char *path = NULL;
+       char *adbl_path = NULL;
+       char *orig_path = NULL;
        SMB_STRUCT_STAT adbl_info;
        SMB_STRUCT_STAT orig_info;
-       TALLOC_CTX *ctx;
+       NTSTATUS status;
 
-       ret = SMB_VFS_NEXT_UNLINK(handle, path);
+       ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
 
-       if (!path) return ret;
+       status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
+       if (!NT_STATUS_IS_OK(status)) {
+               return ret;
+       }
 
        /* no .AppleDouble sync if veto or hide list is empty,
         * otherwise "Cannot find the specified file" error will be caused
@@ -281,27 +322,25 @@ static int atalk_unlink(struct vfs_handle_struct *handle, const char *path)
        if (!handle->conn->hide_list) return ret;
 
        for (i = 0; handle->conn->veto_list[i].name; i ++) {
-               if (strstr(handle->conn->veto_list[i].name, APPLEDOUBLE))
+               if (strstr_m(handle->conn->veto_list[i].name, APPLEDOUBLE))
                        break;
        }
 
        if (!handle->conn->veto_list[i].name) {
                for (i = 0; handle->conn->hide_list[i].name; i ++) {
-                       if (strstr(handle->conn->hide_list[i].name, APPLEDOUBLE))
+                       if (strstr_m(handle->conn->hide_list[i].name, APPLEDOUBLE))
                                break;
                        else {
                                DEBUG(3, ("ATALK: %s is not hidden, skipped..\n",
                                  APPLEDOUBLE));                
-                               return ret;
+                               goto exit_unlink;
                        }
                }
        }
 
-       if (!(ctx = talloc_init("unlink_file")))
-               return ret;
-
-       if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
-         &adbl_info, &orig_info) != 0)
+       if (atalk_build_paths(talloc_tos(), handle->conn->cwd, path,
+                             &adbl_path, &orig_path,
+                             &adbl_info, &orig_info) != 0)
                goto exit_unlink;
 
        if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
@@ -311,30 +350,40 @@ static int atalk_unlink(struct vfs_handle_struct *handle, const char *path)
 
        atalk_unlink_file(adbl_path);
 
-exit_unlink:   
-       talloc_destroy(ctx);
+exit_unlink:
+       TALLOC_FREE(path);
+       TALLOC_FREE(adbl_path);
+       TALLOC_FREE(orig_path);
        return ret;
 }
 
-static int atalk_chmod(struct vfs_handle_struct *handle, const char *path, mode_t mode)
+static int atalk_chmod(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       mode_t mode)
 {
        int ret = 0;
+       int ret1 = 0;
        char *adbl_path = 0;
        char *orig_path = 0;
        SMB_STRUCT_STAT adbl_info;
        SMB_STRUCT_STAT orig_info;
        TALLOC_CTX *ctx;
 
-       ret = SMB_VFS_NEXT_CHMOD(handle, path, mode);
-
-       if (!path) return ret;
+       ret = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
 
        if (!(ctx = talloc_init("chmod_file")))
                return ret;
 
-       if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
-         &adbl_info, &orig_info) != 0)
+       ret1 = atalk_build_paths(ctx,
+                       handle->conn->cwd,
+                       smb_fname->base_name,
+                       &adbl_path,
+                       &orig_path,
+                       &adbl_info,
+                       &orig_info);
+       if (ret1 != 0) {
                goto exit_chmod;
+       }
 
        if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
                DEBUG(3, ("ATALK: %s has passed..\n", orig_path));              
@@ -348,7 +397,10 @@ exit_chmod:
        return ret;
 }
 
-static int atalk_chown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
+static int atalk_chown(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       uid_t uid,
+                       gid_t gid)
 {
        int ret = 0;
        char *adbl_path = 0;
@@ -357,15 +409,14 @@ static int atalk_chown(struct vfs_handle_struct *handle, const char *path, uid_t
        SMB_STRUCT_STAT orig_info;
        TALLOC_CTX *ctx;
 
-       ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
-
-       if (!path) return ret;
+       ret = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
 
        if (!(ctx = talloc_init("chown_file")))
                return ret;
 
-       if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
-         &adbl_info, &orig_info) != 0)
+       if (atalk_build_paths(ctx, handle->conn->cwd, smb_fname->base_name,
+                             &adbl_path, &orig_path,
+                             &adbl_info, &orig_info) != 0)
                goto exit_chown;
 
        if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
@@ -382,7 +433,10 @@ exit_chown:
        return ret;
 }
 
-static int atalk_lchown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
+static int atalk_lchown(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       uid_t uid,
+                       gid_t gid)
 {
        int ret = 0;
        char *adbl_path = 0;
@@ -391,15 +445,14 @@ static int atalk_lchown(struct vfs_handle_struct *handle, const char *path, uid_
        SMB_STRUCT_STAT orig_info;
        TALLOC_CTX *ctx;
 
-       ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
-
-       if (!path) return ret;
+       ret = SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
 
        if (!(ctx = talloc_init("lchown_file")))
                return ret;
 
-       if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
-         &adbl_info, &orig_info) != 0)
+       if (atalk_build_paths(ctx, handle->conn->cwd, smb_fname->base_name,
+                             &adbl_path, &orig_path,
+                             &adbl_info, &orig_info) != 0)
                goto exit_lchown;
 
        if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
@@ -416,28 +469,20 @@ exit_lchown:
        return ret;
 }
 
-static vfs_op_tuple atalk_ops[] = {
-    
-       /* Directory operations */
-
-       {SMB_VFS_OP(atalk_opendir),             SMB_VFS_OP_OPENDIR,     SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(atalk_rmdir),               SMB_VFS_OP_RMDIR,       SMB_VFS_LAYER_TRANSPARENT},
-
-       /* File operations */
-
-       {SMB_VFS_OP(atalk_rename),              SMB_VFS_OP_RENAME,      SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(atalk_unlink),              SMB_VFS_OP_UNLINK,      SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(atalk_chmod),               SMB_VFS_OP_CHMOD,       SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(atalk_chown),               SMB_VFS_OP_CHOWN,       SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(atalk_lchown),              SMB_VFS_OP_LCHOWN,      SMB_VFS_LAYER_TRANSPARENT},
-       
-       /* Finish VFS operations definition */
-       
-       {SMB_VFS_OP(NULL),                      SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
+static struct vfs_fn_pointers vfs_netatalk_fns = {
+       .opendir_fn = atalk_opendir,
+       .fdopendir_fn = atalk_fdopendir,
+       .rmdir_fn = atalk_rmdir,
+       .rename_fn = atalk_rename,
+       .unlink_fn = atalk_unlink,
+       .chmod_fn = atalk_chmod,
+       .chown_fn = atalk_chown,
+       .lchown_fn = atalk_lchown,
 };
 
-NTSTATUS vfs_netatalk_init(void);
-NTSTATUS vfs_netatalk_init(void)
+NTSTATUS vfs_netatalk_init(TALLOC_CTX *);
+NTSTATUS vfs_netatalk_init(TALLOC_CTX *ctx)
 {
-       return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "netatalk", atalk_ops);
+       return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "netatalk",
+                               &vfs_netatalk_fns);
 }