s3-talloc Change TALLOC_ARRAY() to talloc_array()
[samba.git] / source3 / libsmb / libsmb_path.c
index 6706a59ba8faf45445e00dd6e5de4fe4ae40d8c3..1dcf2dd51672cd9c9b15f78938bf37a5b929696b 100644 (file)
@@ -7,17 +7,17 @@
    Copyright (C) Tom Jansen (Ninja ISD) 2002 
    Copyright (C) Derrell Lipman 2003-2008
    Copyright (C) Jeremy Allison 2007, 2008
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 static int 
 hex2int( unsigned int _char )
 {
-    if ( _char >= 'A' && _char <='F')
-       return _char - 'A' + 10;
-    if ( _char >= 'a' && _char <='f')
-       return _char - 'a' + 10;
-    if ( _char >= '0' && _char <='9')
-       return _char - '0';
-    return -1;
+        if ( _char >= 'A' && _char <='F')
+                return _char - 'A' + 10;
+        if ( _char >= 'a' && _char <='f')
+                return _char - 'a' + 10;
+        if ( _char >= '0' && _char <='9')
+                return _char - '0';
+        return -1;
 }
 
 /*
- * SMBC_urldecode()
+ * smbc_urldecode()
  * and urldecode_talloc() (internal fn.)
  *
  * Convert strings of %xx to their single character equivalent.  Each 'x' must
@@ -88,7 +88,7 @@ urldecode_talloc(TALLOC_CTX *ctx, char **pp_dest, const char *src)
                newlen++;
        }
 
-       dest = TALLOC_ARRAY(ctx, char, newlen);
+       dest = talloc_array(ctx, char, newlen);
        if (!dest) {
                return err_count;
        }
@@ -122,7 +122,7 @@ urldecode_talloc(TALLOC_CTX *ctx, char **pp_dest, const char *src)
 }
 
 int
-SMBC_urldecode(char *dest,
+smbc_urldecode(char *dest,
                char *src,
                size_t max_dest_len)
 {
@@ -138,7 +138,7 @@ SMBC_urldecode(char *dest,
 }
 
 /*
- * SMBC_urlencode()
+ * smbc_urlencode()
  *
  * Convert any characters not specifically allowed in a URL into their %xx
  * equivalent.
@@ -146,7 +146,7 @@ SMBC_urldecode(char *dest,
  * Returns the remaining buffer length.
  */
 int
-SMBC_urlencode(char *dest,
+smbc_urlencode(char *dest,
                char *src,
                int max_dest_len)
 {
@@ -196,9 +196,9 @@ SMBC_urlencode(char *dest,
  *
  *                  The method of locating the list of workgroups varies
  *                  depending upon the setting of the context variable
- *                  context->browse_max_lmb_count.  This value determines
- *                  the maximum number of local master browsers to query
- *                  for the list of workgroups.  In order to ensure that
+ *                  context->options.browse_max_lmb_count.  This value
+ *                  determines the maximum number of local master browsers to
+ *                  query for the list of workgroups.  In order to ensure that
  *                  a complete list of workgroups is obtained, all master
  *                  browsers must be queried, but if there are many
  *                  workgroups, the time spent querying can begin to add up.
@@ -216,7 +216,7 @@ SMBC_urlencode(char *dest,
  * are supported.
  */
 
-static const char *smbc_prefix = "smb:";
+#define SMBC_PREFIX "smb:"
 
 int
 SMBC_parse_path(TALLOC_CTX *ctx,
@@ -233,6 +233,7 @@ SMBC_parse_path(TALLOC_CTX *ctx,
        char *s;
        const char *p;
        char *q, *r;
+       char *workgroup = NULL;
        int len;
 
        /* Ensure these returns are at least valid pointers. */
@@ -243,7 +244,7 @@ SMBC_parse_path(TALLOC_CTX *ctx,
        *pp_password = talloc_strdup(ctx, "");
 
        if (!*pp_server || !*pp_share || !*pp_path ||
-                       !*pp_user || !*pp_password) {
+            !*pp_user || !*pp_password) {
                return -1;
        }
 
@@ -253,7 +254,7 @@ SMBC_parse_path(TALLOC_CTX *ctx,
          */
        if (pp_workgroup != NULL) {
                *pp_workgroup =
-                    talloc_strdup(ctx, context->config.workgroup);
+                        talloc_strdup(ctx, smbc_getWorkgroup(context));
        }
 
        if (pp_options) {
@@ -262,8 +263,8 @@ SMBC_parse_path(TALLOC_CTX *ctx,
        s = talloc_strdup(ctx, fname);
 
        /* see if it has the right prefix */
-       len = strlen(smbc_prefix);
-       if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
+       len = strlen(SMBC_PREFIX);
+       if (strncmp(s,SMBC_PREFIX,len) || (s[len] != '/' && s[len] != 0)) {
                 return -1; /* What about no smb: ? */
         }
 
@@ -286,7 +287,7 @@ SMBC_parse_path(TALLOC_CTX *ctx,
                 DEBUG(4, ("Found options '%s'", q));
 
                /* Copy the options */
-               if (*pp_options != NULL) {
+               if (pp_options && *pp_options != NULL) {
                        TALLOC_FREE(*pp_options);
                        *pp_options = talloc_strdup(ctx, q);
                }
@@ -297,17 +298,17 @@ SMBC_parse_path(TALLOC_CTX *ctx,
        }
 
        if (*p == '/') {
-               int wl = strlen(context->config.workgroup);
+               int wl = strlen(smbc_getWorkgroup(context));
 
                if (wl > 16) {
                        wl = 16;
                }
 
-               *pp_server = talloc_strdup(ctx, context->config.workgroup);
+               *pp_server = talloc_strdup(ctx, smbc_getWorkgroup(context));
                if (!*pp_server) {
                        return -1;
                }
-                       *pp_server[wl] = '\0';
+               (*pp_server)[wl] = '\0';
                return 0;
        }
 
@@ -332,7 +333,6 @@ SMBC_parse_path(TALLOC_CTX *ctx,
                u = userinfo;
 
                if (strchr_m(u, ';')) {
-                       char *workgroup;
                        next_token_no_ltrim_talloc(ctx, &u, &workgroup, ";");
                        if (!workgroup) {
                                return -1;
@@ -377,8 +377,8 @@ SMBC_parse_path(TALLOC_CTX *ctx,
          */
         if (*p != '\0') {
                *pp_path = talloc_asprintf(ctx,
-                                       "\\%s",
-                                       p);
+                                           "\\%s",
+                                           p);
         } else {
                *pp_path = talloc_strdup(ctx, "");
        }
@@ -387,14 +387,25 @@ SMBC_parse_path(TALLOC_CTX *ctx,
        }
        string_replace(*pp_path, '/', '\\');
 
- decoding:
-
+decoding:
        (void) urldecode_talloc(ctx, pp_path, *pp_path);
        (void) urldecode_talloc(ctx, pp_server, *pp_server);
        (void) urldecode_talloc(ctx, pp_share, *pp_share);
        (void) urldecode_talloc(ctx, pp_user, *pp_user);
        (void) urldecode_talloc(ctx, pp_password, *pp_password);
 
+       if (!workgroup) {
+               workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
+       }
+       if (!workgroup) {
+               return -1;
+       }
+
+       /* set the credentials to make DFS work */
+       smbc_set_credentials_with_fallback(context,
+                                          workgroup,
+                                          *pp_user,
+                                          *pp_password);
        return 0;
 }