Always define PATH_MAX. Makes code simpler (removes
[samba.git] / source3 / smbd / msdfs.c
index 56089fbe95d9f787fcef3737b471948c45b45664..98a41e4ec3172eb7884a6ac654f31e9636e84ab6 100644 (file)
@@ -43,9 +43,9 @@ extern uint32 global_client_caps;
 **********************************************************************/
 
 static NTSTATUS parse_dfs_path(const char *pathname,
-                               BOOL allow_wcards,
+                               bool allow_wcards,
                                struct dfs_path *pdp, /* MUST BE TALLOCED */
-                               BOOL *ppath_contains_wcard)
+                               bool *ppath_contains_wcard)
 {
        char *pathname_local;
        char *p,*temp;
@@ -196,16 +196,26 @@ static NTSTATUS parse_dfs_path(const char *pathname,
  Note this CHANGES CWD !!!! JRA.
 *********************************************************/
 
-static NTSTATUS create_conn_struct(connection_struct *conn,
+static NTSTATUS create_conn_struct(TALLOC_CTX *ctx,
+                               connection_struct *conn,
                                int snum,
                                const char *path)
 {
-       pstring connpath;
+       char *connpath;
 
        ZERO_STRUCTP(conn);
 
-       pstrcpy(connpath, path);
-       pstring_sub(connpath , "%S", lp_servicename(snum));
+       connpath = talloc_strdup(ctx, path);
+       if (!connpath) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       connpath = talloc_string_sub(ctx,
+                               connpath,
+                               "%S",
+                               lp_servicename(snum));
+       if (!connpath) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        /* needed for smbd_vfs_init() */
 
@@ -268,7 +278,7 @@ static NTSTATUS create_conn_struct(connection_struct *conn,
  server we're referring to understands posix paths.
  **********************************************************************/
 
-static BOOL parse_msdfs_symlink(TALLOC_CTX *ctx,
+static bool parse_msdfs_symlink(TALLOC_CTX *ctx,
                                const char *target,
                                struct referral **preflist,
                                int *refcount)
@@ -349,7 +359,7 @@ static BOOL parse_msdfs_symlink(TALLOC_CTX *ctx,
  returns the target string from inside the link.
 **********************************************************************/
 
-static BOOL is_msdfs_link_internal(TALLOC_CTX *ctx,
+static bool is_msdfs_link_internal(TALLOC_CTX *ctx,
                        connection_struct *conn,
                        const char *path,
                        char **pp_link_target,
@@ -418,7 +428,7 @@ static BOOL is_msdfs_link_internal(TALLOC_CTX *ctx,
  Returns true if the unix path is a valid msdfs symlink.
 **********************************************************************/
 
-BOOL is_msdfs_link(connection_struct *conn,
+bool is_msdfs_link(connection_struct *conn,
                const char *path,
                SMB_STRUCT_STAT *sbufp)
 {
@@ -449,7 +459,7 @@ static NTSTATUS dfs_path_lookup(TALLOC_CTX *ctx,
                const char *dfspath, /* Incoming complete dfs path */
                const struct dfs_path *pdp, /* Parsed out
                                               server+share+extrapath. */
-               BOOL search_flag, /* Called from a findfirst ? */
+               bool search_flag, /* Called from a findfirst ? */
                int *consumedcntp,
                char **pp_targetpath)
 {
@@ -587,9 +597,9 @@ static NTSTATUS dfs_path_lookup(TALLOC_CTX *ctx,
 static NTSTATUS dfs_redirect(TALLOC_CTX *ctx,
                        connection_struct *conn,
                        const char *path_in,
-                       BOOL search_wcard_flag,
+                       bool search_wcard_flag,
                        char **pp_path_out,
-                       BOOL *ppath_contains_wcard)
+                       bool *ppath_contains_wcard)
 {
        NTSTATUS status;
        struct dfs_path *pdp = TALLOC_P(ctx, struct dfs_path);
@@ -687,7 +697,7 @@ static NTSTATUS self_ref(TALLOC_CTX *ctx,
                        const char *dfs_path,
                        struct junction_map *jucn,
                        int *consumedcntp,
-                       BOOL *self_referralp)
+                       bool *self_referralp)
 {
        struct referral *ref;
 
@@ -718,14 +728,14 @@ NTSTATUS get_referred_path(TALLOC_CTX *ctx,
                        const char *dfs_path,
                        struct junction_map *jucn,
                        int *consumedcntp,
-                       BOOL *self_referralp)
+                       bool *self_referralp)
 {
        struct connection_struct conns;
        struct connection_struct *conn = &conns;
        char *targetpath = NULL;
        int snum;
        NTSTATUS status = NT_STATUS_NOT_FOUND;
-       BOOL dummy;
+       bool dummy;
        struct dfs_path *pdp = TALLOC_P(ctx, struct dfs_path);
 
        if (!pdp) {
@@ -771,7 +781,7 @@ NTSTATUS get_referred_path(TALLOC_CTX *ctx,
                }
        }
 
-       if (!lp_msdfs_root(snum)) {
+       if (!lp_msdfs_root(snum) && (*lp_msdfs_proxy(snum) == '\0')) {
                DEBUG(3,("get_referred_path: |%s| in dfs path %s is not "
                        "a dfs root.\n",
                        pdp->servicename, dfs_path));
@@ -788,6 +798,7 @@ NTSTATUS get_referred_path(TALLOC_CTX *ctx,
         */
 
        if (pdp->reqpath[0] == '\0') {
+               char *tmp;
                struct referral *ref;
 
                if (*lp_msdfs_proxy(snum) == '\0') {
@@ -810,7 +821,16 @@ NTSTATUS get_referred_path(TALLOC_CTX *ctx,
                        return NT_STATUS_NO_MEMORY;
                }
 
-               ref->alternate_path = talloc_strdup(ctx, lp_msdfs_proxy(snum));
+               if (!(tmp = talloc_strdup(ctx, lp_msdfs_proxy(snum)))) {
+                       TALLOC_FREE(pdp);
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               trim_string(tmp, "\\", 0);
+
+               ref->alternate_path = talloc_asprintf(ctx, "\\%s", tmp);
+               TALLOC_FREE(tmp);
+
                if (!ref->alternate_path) {
                        TALLOC_FREE(pdp);
                        return NT_STATUS_NO_MEMORY;
@@ -834,7 +854,7 @@ NTSTATUS get_referred_path(TALLOC_CTX *ctx,
                return NT_STATUS_OK;
        }
 
-       status = create_conn_struct(conn, snum, lp_pathname(snum));
+       status = create_conn_struct(ctx, conn, snum, lp_pathname(snum));
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(pdp);
                return status;
@@ -874,11 +894,11 @@ static int setup_ver2_dfs_referral(const char *pathname,
                                char **ppdata,
                                struct junction_map *junction,
                                int consumedcnt,
-                               BOOL self_referral)
+                               bool self_referral)
 {
        char* pdata = *ppdata;
 
-       unsigned char uni_requestedpath[sizeof(pstring)];
+       smb_ucs2_t *uni_requestedpath = NULL;
        int uni_reqpathoffset1,uni_reqpathoffset2;
        int uni_curroffset;
        int requestedpathlen=0;
@@ -888,12 +908,15 @@ static int setup_ver2_dfs_referral(const char *pathname,
 
        DEBUG(10,("Setting up version2 referral\nRequested path:\n"));
 
-       requestedpathlen = rpcstr_push(uni_requestedpath,
-                                       pathname, sizeof(pstring),
-                                       STR_TERMINATE);
+       requestedpathlen = rpcstr_push_talloc(talloc_tos(),
+                                       &uni_requestedpath, pathname);
+       if (uni_requestedpath == NULL || requestedpathlen == 0) {
+               return -1;
+       }
 
        if (DEBUGLVL(10)) {
-               dump_data(0, uni_requestedpath,requestedpathlen);
+               dump_data(0, (unsigned char *)uni_requestedpath,
+                       requestedpathlen);
        }
 
        DEBUG(10,("ref count = %u\n",junction->referral_count));
@@ -966,8 +989,10 @@ static int setup_ver2_dfs_referral(const char *pathname,
                SSVAL(pdata,offset+16,uni_reqpathoffset1-offset);
                SSVAL(pdata,offset+18,uni_reqpathoffset2-offset);
                /* copy referred path into current offset */
-               unilen = rpcstr_push(pdata+uni_curroffset, ref->alternate_path,
-                                    sizeof(pstring), STR_UNICODE);
+               unilen = rpcstr_push(pdata+uni_curroffset,
+                                       ref->alternate_path,
+                                       reply_size - uni_curroffset,
+                                       STR_UNICODE);
 
                SSVAL(pdata,offset+20,uni_curroffset-offset);
 
@@ -983,11 +1008,11 @@ static int setup_ver3_dfs_referral(const char *pathname,
                                char **ppdata,
                                struct junction_map *junction,
                                int consumedcnt,
-                               BOOL self_referral)
+                               bool self_referral)
 {
        char *pdata = *ppdata;
 
-       unsigned char uni_reqpath[sizeof(pstring)];
+       smb_ucs2_t *uni_reqpath = NULL;
        int uni_reqpathoffset1, uni_reqpathoffset2;
        int uni_curroffset;
        int reply_size = 0;
@@ -997,11 +1022,14 @@ static int setup_ver3_dfs_referral(const char *pathname,
 
        DEBUG(10,("setting up version3 referral\n"));
 
-       reqpathlen = rpcstr_push(uni_reqpath, pathname,
-                       sizeof(pstring), STR_TERMINATE);
+       reqpathlen = rpcstr_push_talloc(talloc_tos(), &uni_reqpath, pathname);
+       if (uni_reqpath == NULL || reqpathlen == 0) {
+               return -1;
+       }
 
        if (DEBUGLVL(10)) {
-               dump_data(0, uni_reqpath,reqpathlen);
+               dump_data(0, (unsigned char *)uni_reqpath,
+                       reqpathlen);
        }
 
        uni_reqpathoffset1 = REFERRAL_HEADER_SIZE +
@@ -1059,8 +1087,8 @@ static int setup_ver3_dfs_referral(const char *pathname,
                SSVAL(pdata,offset+14,uni_reqpathoffset2-offset);
                /* copy referred path into current offset */
                unilen = rpcstr_push(pdata+uni_curroffset,ref->alternate_path,
-                                    sizeof(pstring),
-                                    STR_UNICODE | STR_TERMINATE);
+                                       reply_size - uni_curroffset,
+                                       STR_UNICODE | STR_TERMINATE);
                SSVAL(pdata,offset+16,uni_curroffset-offset);
                /* copy 0x10 bytes of 00's in the ServiceSite GUID */
                memset(pdata+offset+18,'\0',16);
@@ -1085,7 +1113,7 @@ int setup_dfs_referral(connection_struct *orig_conn,
 {
        struct junction_map *junction = NULL;
        int consumedcnt = 0;
-       BOOL self_referral = False;
+       bool self_referral = False;
        int reply_size = 0;
        char *pathnamep = NULL;
        char *local_dfs_path = NULL;
@@ -1199,12 +1227,12 @@ int setup_dfs_referral(connection_struct *orig_conn,
  Creates a junction structure from a DFS pathname
 **********************************************************************/
 
-BOOL create_junction(TALLOC_CTX *ctx,
+bool create_junction(TALLOC_CTX *ctx,
                const char *dfs_path,
                struct junction_map *jucn)
 {
        int snum;
-       BOOL dummy;
+       bool dummy;
        struct dfs_path *pdp = TALLOC_P(ctx,struct dfs_path);
        NTSTATUS status;
 
@@ -1250,7 +1278,7 @@ BOOL create_junction(TALLOC_CTX *ctx,
  Forms a valid Unix pathname from the junction
  **********************************************************************/
 
-static BOOL junction_to_local_path(const struct junction_map *jucn,
+static bool junction_to_local_path(const struct junction_map *jucn,
                                char **pp_path_out,
                                connection_struct *conn_out)
 {
@@ -1260,7 +1288,8 @@ static BOOL junction_to_local_path(const struct junction_map *jucn,
        if(snum < 0) {
                return False;
        }
-       if (!NT_STATUS_IS_OK(create_conn_struct(conn_out, snum,
+       if (!NT_STATUS_IS_OK(create_conn_struct(talloc_tos(),
+                                       conn_out, snum,
                                        lp_pathname(snum)))) {
                return False;
        }
@@ -1275,16 +1304,16 @@ static BOOL junction_to_local_path(const struct junction_map *jucn,
        return True;
 }
 
-BOOL create_msdfs_link(const struct junction_map *jucn,
-               BOOL exists)
+bool create_msdfs_link(const struct junction_map *jucn,
+               bool exists)
 {
        char *path = NULL;
        char *msdfs_link = NULL;
        connection_struct conns;
        connection_struct *conn = &conns;
        int i=0;
-       BOOL insert_comma = False;
-       BOOL ret = False;
+       bool insert_comma = False;
+       bool ret = False;
 
        ZERO_STRUCT(conns);
 
@@ -1350,12 +1379,12 @@ out:
        return ret;
 }
 
-BOOL remove_msdfs_link(const struct junction_map *jucn)
+bool remove_msdfs_link(const struct junction_map *jucn)
 {
        char *path = NULL;
        connection_struct conns;
        connection_struct *conn = &conns;
-       BOOL ret = False;
+       bool ret = False;
 
        ZERO_STRUCT(conns);
 
@@ -1392,7 +1421,8 @@ 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(&conn, snum, connect_path))) {
+       if (!NT_STATUS_IS_OK(create_conn_struct(talloc_tos(),
+                                       &conn, snum, connect_path))) {
                return 0;
        }
 
@@ -1457,7 +1487,7 @@ static int form_junctions(TALLOC_CTX *ctx,
         * Fake up a connection struct for the VFS layer.
         */
 
-       if (!NT_STATUS_IS_OK(create_conn_struct(&conn, snum, connect_path))) {
+       if (!NT_STATUS_IS_OK(create_conn_struct(ctx, &conn, snum, connect_path))) {
                return 0;
        }
 
@@ -1549,7 +1579,7 @@ struct junction_map *enum_msdfs_links(TALLOC_CTX *ctx, size_t *p_num_jn)
 {
        struct junction_map *jn = NULL;
        int i=0;
-       size_t jn_count;
+       size_t jn_count = 0;
        int sharecount = 0;
 
        *p_num_jn = 0;
@@ -1594,12 +1624,12 @@ struct junction_map *enum_msdfs_links(TALLOC_CTX *ctx, size_t *p_num_jn)
 
 NTSTATUS resolve_dfspath(TALLOC_CTX *ctx,
                        connection_struct *conn,
-                       BOOL dfs_pathnames,
+                       bool dfs_pathnames,
                        const char *name_in,
                        char **pp_name_out)
 {
        NTSTATUS status = NT_STATUS_OK;
-       BOOL dummy;
+       bool dummy;
        if (dfs_pathnames) {
                status = dfs_redirect(ctx,
                                        conn,
@@ -1620,17 +1650,17 @@ NTSTATUS resolve_dfspath(TALLOC_CTX *ctx,
 
 /******************************************************************************
  Core function to resolve a dfs pathname possibly containing a wildcard.
- This function is identical to the above except for the BOOL param to
+ This function is identical to the above except for the bool param to
  dfs_redirect but I need this to be separate so it's really clear when
  we're allowing wildcards and when we're not. JRA.
 ******************************************************************************/
 
 NTSTATUS resolve_dfspath_wcard(TALLOC_CTX *ctx,
                                connection_struct *conn,
-                               BOOL dfs_pathnames,
+                               bool dfs_pathnames,
                                const char *name_in,
                                char **pp_name_out,
-                               BOOL *ppath_contains_wcard)
+                               bool *ppath_contains_wcard)
 {
        NTSTATUS status = NT_STATUS_OK;
        if (dfs_pathnames) {