Merge branch 'master' of ssh://git.samba.org/data/git/samba
[tprouty/samba.git] / source3 / smbd / msdfs.c
index 062ab804cc967f069f9cc35f8b738b342eb6ce2b..adeaf96bd2d78e7793614cd36dfcd1a9254f7576 100644 (file)
@@ -22,8 +22,7 @@
 
 #define DBGC_CLASS DBGC_MSDFS
 #include "includes.h"
-
-extern uint32 global_client_caps;
+#include "smbd/globals.h"
 
 /**********************************************************************
  Parse a DFS pathname of the form \hostname\service\reqpath
@@ -215,13 +214,16 @@ static NTSTATUS parse_dfs_path(connection_struct *conn,
  Note this CHANGES CWD !!!! JRA.
 *********************************************************/
 
-static NTSTATUS create_conn_struct(TALLOC_CTX *ctx,
+NTSTATUS create_conn_struct(TALLOC_CTX *ctx,
                                connection_struct **pconn,
                                int snum,
-                               const char *path)
+                               const char *path,
+                               struct auth_serversupplied_info *server_info,
+                               char **poldcwd)
 {
        connection_struct *conn;
        char *connpath;
+       char *oldcwd;
 
        conn = TALLOC_ZERO_P(ctx, connection_struct);
        if (conn == NULL) {
@@ -252,6 +254,15 @@ static NTSTATUS create_conn_struct(TALLOC_CTX *ctx,
 
        conn->params->service = snum;
 
+       if (server_info != NULL) {
+               conn->server_info = copy_serverinfo(conn, server_info);
+               if (conn->server_info == NULL) {
+                       DEBUG(0, ("copy_serverinfo failed\n"));
+                       TALLOC_FREE(conn);
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
+
        set_conn_connectpath(conn, connpath);
 
        if (!smbd_vfs_init(conn)) {
@@ -267,6 +278,14 @@ static NTSTATUS create_conn_struct(TALLOC_CTX *ctx,
         * user we will fail.... WTF ? JRA.
         */
 
+       oldcwd = vfs_GetWd(ctx, conn);
+       if (oldcwd == NULL) {
+               NTSTATUS status = map_nt_error_from_unix(errno);
+               DEBUG(3, ("vfs_GetWd failed: %s\n", strerror(errno)));
+               conn_free_internal(conn);
+               return status;
+       }
+
        if (vfs_ChDir(conn,conn->connectpath) != 0) {
                NTSTATUS status = map_nt_error_from_unix(errno);
                DEBUG(3,("create_conn_struct: Can't ChDir to new conn path %s. "
@@ -277,6 +296,7 @@ static NTSTATUS create_conn_struct(TALLOC_CTX *ctx,
        }
 
        *pconn = conn;
+       *poldcwd = oldcwd;
 
        return NT_STATUS_OK;
 }
@@ -760,6 +780,7 @@ NTSTATUS get_referred_path(TALLOC_CTX *ctx,
        NTSTATUS status = NT_STATUS_NOT_FOUND;
        bool dummy;
        struct dfs_path *pdp = TALLOC_P(ctx, struct dfs_path);
+       char *oldpath;
 
        if (!pdp) {
                return NT_STATUS_NO_MEMORY;
@@ -868,7 +889,8 @@ NTSTATUS get_referred_path(TALLOC_CTX *ctx,
                return NT_STATUS_OK;
        }
 
-       status = create_conn_struct(ctx, &conn, snum, lp_pathname(snum));
+       status = create_conn_struct(ctx, &conn, snum, lp_pathname(snum),
+                                   NULL, &oldpath);
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(pdp);
                return status;
@@ -883,6 +905,7 @@ NTSTATUS get_referred_path(TALLOC_CTX *ctx,
        if (!NT_STATUS_EQUAL(status, NT_STATUS_PATH_NOT_COVERED)) {
                DEBUG(3,("get_referred_path: No valid referrals for path %s\n",
                        dfs_path));
+               vfs_ChDir(conn, oldpath);
                conn_free_internal(conn);
                TALLOC_FREE(pdp);
                return status;
@@ -894,11 +917,13 @@ NTSTATUS get_referred_path(TALLOC_CTX *ctx,
                                &jucn->referral_count)) {
                DEBUG(3,("get_referred_path: failed to parse symlink "
                        "target %s\n", targetpath ));
+               vfs_ChDir(conn, oldpath);
                conn_free_internal(conn);
                TALLOC_FREE(pdp);
                return NT_STATUS_NOT_FOUND;
        }
 
+       vfs_ChDir(conn, oldpath);
        conn_free_internal(conn);
        TALLOC_FREE(pdp);
        return NT_STATUS_OK;
@@ -907,7 +932,6 @@ NTSTATUS get_referred_path(TALLOC_CTX *ctx,
 static int setup_ver2_dfs_referral(const char *pathname,
                                char **ppdata,
                                struct junction_map *junction,
-                               int consumedcnt,
                                bool self_referral)
 {
        char* pdata = *ppdata;
@@ -972,7 +996,8 @@ static int setup_ver2_dfs_referral(const char *pathname,
        memcpy(pdata+uni_reqpathoffset2,uni_requestedpath,requestedpathlen);
 
        /* create the header */
-       SSVAL(pdata,0,consumedcnt * 2); /* path consumed */
+       SSVAL(pdata,0,requestedpathlen - 2); /* UCS2 of path consumed minus
+                                               2 byte null */
        /* number of referral in this pkt */
        SSVAL(pdata,2,junction->referral_count);
        if(self_referral) {
@@ -1021,7 +1046,6 @@ static int setup_ver2_dfs_referral(const char *pathname,
 static int setup_ver3_dfs_referral(const char *pathname,
                                char **ppdata,
                                struct junction_map *junction,
-                               int consumedcnt,
                                bool self_referral)
 {
        char *pdata = *ppdata;
@@ -1068,7 +1092,8 @@ static int setup_ver3_dfs_referral(const char *pathname,
        *ppdata = pdata;
 
        /* create the header */
-       SSVAL(pdata,0,consumedcnt * 2); /* path consumed */
+       SSVAL(pdata,0,reqpathlen - 2); /* UCS2 of path consumed minus
+                                         2 byte null */
        SSVAL(pdata,2,junction->referral_count); /* number of referral */
        if(self_referral) {
                SIVAL(pdata,4,DFSREF_REFERRAL_SERVER | DFSREF_STORAGE_SERVER);
@@ -1208,11 +1233,11 @@ int setup_dfs_referral(connection_struct *orig_conn,
        case 2:
                reply_size = setup_ver2_dfs_referral(pathnamep,
                                        ppdata, junction,
-                                       consumedcnt, self_referral);
+                                       self_referral);
                break;
        case 3:
                reply_size = setup_ver3_dfs_referral(pathnamep, ppdata,
-                                       junction, consumedcnt, self_referral);
+                                       junction, self_referral);
                break;
        default:
                DEBUG(0,("setup_dfs_referral: Invalid dfs referral "
@@ -1293,18 +1318,20 @@ bool create_junction(TALLOC_CTX *ctx,
  **********************************************************************/
 
 static bool junction_to_local_path(const struct junction_map *jucn,
-                               char **pp_path_out,
-                               connection_struct **conn_out)
+                                  char **pp_path_out,
+                                  connection_struct **conn_out,
+                                  char **oldpath)
 {
        int snum;
+       NTSTATUS status;
 
        snum = lp_servicenumber(jucn->service_name);
        if(snum < 0) {
                return False;
        }
-       if (!NT_STATUS_IS_OK(create_conn_struct(talloc_tos(),
-                                       conn_out, snum,
-                                       lp_pathname(snum)))) {
+       status = create_conn_struct(talloc_tos(), conn_out, snum,
+                                   lp_pathname(snum), NULL, oldpath);
+       if (!NT_STATUS_IS_OK(status)) {
                return False;
        }
 
@@ -1313,6 +1340,8 @@ static bool junction_to_local_path(const struct junction_map *jucn,
                        lp_pathname(snum),
                        jucn->volume_name);
        if (!*pp_path_out) {
+               vfs_ChDir(*conn_out, *oldpath);
+               conn_free_internal(*conn_out);
                return False;
        }
        return True;
@@ -1321,13 +1350,14 @@ static bool junction_to_local_path(const struct junction_map *jucn,
 bool create_msdfs_link(const struct junction_map *jucn)
 {
        char *path = NULL;
+       char *cwd;
        char *msdfs_link = NULL;
        connection_struct *conn;
        int i=0;
        bool insert_comma = False;
        bool ret = False;
 
-       if(!junction_to_local_path(jucn, &path, &conn)) {
+       if(!junction_to_local_path(jucn, &path, &conn, &cwd)) {
                return False;
        }
 
@@ -1385,7 +1415,7 @@ bool create_msdfs_link(const struct junction_map *jucn)
        ret = True;
 
 out:
-
+       vfs_ChDir(conn, cwd);
        conn_free_internal(conn);
        return ret;
 }
@@ -1393,15 +1423,19 @@ out:
 bool remove_msdfs_link(const struct junction_map *jucn)
 {
        char *path = NULL;
+       char *cwd;
        connection_struct *conn;
        bool ret = False;
 
-       if( junction_to_local_path(jucn, &path, &conn) ) {
-               if( SMB_VFS_UNLINK(conn, path) == 0 ) {
-                       ret = True;
-               }
+       if (!junction_to_local_path(jucn, &path, &conn, &cwd)) {
+               return false;
+       }
+
+       if( SMB_VFS_UNLINK(conn, path) == 0 ) {
+               ret = True;
        }
 
+       vfs_ChDir(conn, cwd);
        conn_free_internal(conn);
        return ret;
 }
@@ -1418,6 +1452,8 @@ static int count_dfs_links(TALLOC_CTX *ctx, int snum)
        const char *connect_path = lp_pathname(snum);
        const char *msdfs_proxy = lp_msdfs_proxy(snum);
        connection_struct *conn;
+       NTSTATUS status;
+       char *cwd;
 
        if(*connect_path == '\0') {
                return 0;
@@ -1427,8 +1463,11 @@ static int count_dfs_links(TALLOC_CTX *ctx, int snum)
         * Fake up a connection struct for the VFS layer.
         */
 
-       if (!NT_STATUS_IS_OK(create_conn_struct(talloc_tos(),
-                                       &conn, snum, connect_path))) {
+       status = create_conn_struct(talloc_tos(), &conn, snum, connect_path,
+                                   NULL, &cwd);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(3, ("create_conn_struct failed: %s\n",
+                         nt_errstr(status)));
                return 0;
        }
 
@@ -1457,7 +1496,7 @@ static int count_dfs_links(TALLOC_CTX *ctx, int snum)
        SMB_VFS_CLOSEDIR(conn,dirp);
 
 out:
-
+       vfs_ChDir(conn, cwd);
        conn_free_internal(conn);
        return cnt;
 }
@@ -1478,6 +1517,8 @@ static int form_junctions(TALLOC_CTX *ctx,
        const char *msdfs_proxy = lp_msdfs_proxy(snum);
        connection_struct *conn;
        struct referral *ref = NULL;
+       char *cwd;
+       NTSTATUS status;
 
        if (jn_remain == 0) {
                return 0;
@@ -1491,7 +1532,11 @@ static int form_junctions(TALLOC_CTX *ctx,
         * Fake up a connection struct for the VFS layer.
         */
 
-       if (!NT_STATUS_IS_OK(create_conn_struct(ctx, &conn, snum, connect_path))) {
+       status = create_conn_struct(ctx, &conn, snum, connect_path, NULL,
+                                   &cwd);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(3, ("create_conn_struct failed: %s\n",
+                         nt_errstr(status)));
                return 0;
        }
 
@@ -1577,6 +1622,7 @@ out:
                SMB_VFS_CLOSEDIR(conn,dirp);
        }
 
+       vfs_ChDir(conn, cwd);
        conn_free_internal(conn);
        return cnt;
 }