lib: Remove global xfile.h includes
[samba.git] / source3 / modules / vfs_expand_msdfs.c
index 62222c48ffbd1d265810527257a5fe1c24e1a115..40c57c2d1079796acb7a5a6fb6318100310a489f 100644 (file)
  */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "smbd/smbd.h"
+#include "../librpc/gen_ndr/ndr_netlogon.h"
+#include "smbd/globals.h"
+#include "auth.h"
+#include "../lib/tsocket/tsocket.h"
+#include "lib/util/xfile.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_VFS
 
-extern userdom_struct current_user_info;
-
 /**********************************************************
   Under mapfile we expect a table of the following format:
 
@@ -37,7 +42,8 @@ extern userdom_struct current_user_info;
   This is to redirect a DFS client to a host close to it.
 ***********************************************************/
 
-static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile)
+static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile,
+                             const char *clientaddr)
 {
        XFILE *f;
        char buf[1024];
@@ -55,7 +61,6 @@ static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile)
        DEBUG(10, ("Scanning mapfile [%s]\n", mapfile));
 
        while (x_fgets(buf, sizeof(buf), f) != NULL) {
-               char addr[INET6_ADDRSTRLEN];
 
                if ((strlen(buf) > 0) && (buf[strlen(buf)-1] == '\n'))
                        buf[strlen(buf)-1] = '\0';
@@ -71,8 +76,7 @@ static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile)
 
                *space = '\0';
 
-               if (strncmp(client_addr(get_client_fd(),addr,sizeof(addr)),
-                               buf, strlen(buf)) == 0) {
+               if (strncmp(clientaddr, buf, strlen(buf)) == 0) {
                        found = true;
                        break;
                }
@@ -115,6 +119,7 @@ static char *expand_msdfs_target(TALLOC_CTX *ctx,
        int filename_len = 0;
        char *targethost = NULL;
        char *new_target = NULL;
+       char *raddr;
 
        if (filename_start == NULL) {
                DEBUG(10, ("No filename start in %s\n", target));
@@ -137,19 +142,27 @@ static char *expand_msdfs_target(TALLOC_CTX *ctx,
 
        DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
 
-       if ((targethost = read_target_host(ctx, mapfilename)) == NULL) {
+       raddr = tsocket_address_inet_addr_string(conn->sconn->remote_address,
+                                                ctx);
+       if (raddr == NULL) {
+               return NULL;
+       }
+
+       targethost = read_target_host(
+               ctx, raddr, mapfilename);
+       if (targethost == NULL) {
                DEBUG(1, ("Could not expand target host from file %s\n",
                          mapfilename));
                return NULL;
        }
 
        targethost = talloc_sub_advanced(ctx,
-                               lp_servicename(SNUM(conn)),
-                               conn->user,
+                               lp_servicename(talloc_tos(), SNUM(conn)),
+                               conn->session_info->unix_info->unix_name,
                                conn->connectpath,
-                               conn->server_info->gid,
-                               get_current_username(),
-                               current_user_info.domain,
+                               conn->session_info->unix_token->gid,
+                               conn->session_info->unix_info->sanitized_username,
+                               conn->session_info->info->domain_name,
                                targethost);
 
        DEBUG(10, ("Expanded targethost to %s\n", targethost));
@@ -174,21 +187,27 @@ static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
 {
        TALLOC_CTX *ctx = talloc_tos();
        int result;
-       char *target = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
+       char *target = talloc_array(ctx, char, PATH_MAX+1);
+       size_t len;
 
        if (!target) {
                errno = ENOMEM;
                return -1;
        }
+       if (bufsiz == 0) {
+               errno = EINVAL;
+               return -1;
+       }
+
        result = SMB_VFS_NEXT_READLINK(handle, path, target,
                                       PATH_MAX);
 
-       if (result < 0)
+       if (result <= 0)
                return result;
 
        target[result] = '\0';
 
-       if ((strncmp(target, "msdfs:", strlen("msdfs:")) == 0) &&
+       if ((strncmp(target, "msdfs:", 6) == 0) &&
            (strchr_m(target, '@') != NULL)) {
                target = expand_msdfs_target(ctx, handle->conn, target);
                if (!target) {
@@ -197,21 +216,21 @@ static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
                }
        }
 
-       safe_strcpy(buf, target, bufsiz-1);
-       return strlen(buf);
-}
+       len = MIN(bufsiz, strlen(target));
 
-/* VFS operations structure */
+       memcpy(buf, target, len);
+
+       TALLOC_FREE(target);
+       return len;
+}
 
-static vfs_op_tuple expand_msdfs_ops[] = {
-       {SMB_VFS_OP(expand_msdfs_readlink), SMB_VFS_OP_READLINK,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
+static struct vfs_fn_pointers vfs_expand_msdfs_fns = {
+       .readlink_fn = expand_msdfs_readlink
 };
 
 NTSTATUS vfs_expand_msdfs_init(void);
 NTSTATUS vfs_expand_msdfs_init(void)
 {
        return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
-                               expand_msdfs_ops);
+                               &vfs_expand_msdfs_fns);
 }