2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003-2008
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "libsmbclient.h"
27 #include "libsmb_internal.h"
30 /* Used by urldecode_talloc() */
32 hex2int( unsigned int _char
)
34 if ( _char
>= 'A' && _char
<='F')
35 return _char
- 'A' + 10;
36 if ( _char
>= 'a' && _char
<='f')
37 return _char
- 'a' + 10;
38 if ( _char
>= '0' && _char
<='9')
45 * and urldecode_talloc() (internal fn.)
47 * Convert strings of %xx to their single character equivalent. Each 'x' must
48 * be a valid hexadecimal digit, or that % sequence is left undecoded.
50 * dest may, but need not be, the same pointer as src.
52 * Returns the number of % sequences which could not be converted due to lack
53 * of two following hexadecimal digits.
56 urldecode_talloc(TALLOC_CTX
*ctx
, char **pp_dest
, const char *src
)
58 int old_length
= strlen(src
);
64 if (old_length
== 0) {
69 for (i
= 0; i
< old_length
; ) {
70 unsigned char character
= src
[i
++];
72 if (character
== '%') {
73 int a
= i
+1 < old_length
? hex2int(src
[i
]) : -1;
74 int b
= i
+1 < old_length
? hex2int(src
[i
+1]) : -1;
76 /* Replace valid sequence */
77 if (a
!= -1 && b
!= -1) {
78 /* Replace valid %xx sequence with %dd */
79 character
= (a
* 16) + b
;
80 if (character
== '\0') {
81 break; /* Stop at %00 */
91 dest
= talloc_array(ctx
, char, newlen
);
97 for (p
= dest
, i
= 0; i
< old_length
; ) {
98 unsigned char character
= src
[i
++];
100 if (character
== '%') {
101 int a
= i
+1 < old_length
? hex2int(src
[i
]) : -1;
102 int b
= i
+1 < old_length
? hex2int(src
[i
+1]) : -1;
104 /* Replace valid sequence */
105 if (a
!= -1 && b
!= -1) {
106 /* Replace valid %xx sequence with %dd */
107 character
= (a
* 16) + b
;
108 if (character
== '\0') {
109 break; /* Stop at %00 */
125 smbc_urldecode(char *dest
,
129 TALLOC_CTX
*frame
= talloc_stackframe();
131 int ret
= urldecode_talloc(frame
, &pdest
, src
);
134 strlcpy(dest
, pdest
, max_dest_len
);
143 * Convert any characters not specifically allowed in a URL into their %xx
146 * Returns the remaining buffer length.
149 smbc_urlencode(char *dest
,
153 char hex
[] = "0123456789ABCDEF";
155 for (; *src
!= '\0' && max_dest_len
>= 3; src
++) {
167 *dest
++ = hex
[(*src
>> 4) & 0x0f];
168 *dest
++ = hex
[*src
& 0x0f];
176 if (max_dest_len
<= 0) {
177 /* Ensure we return -1 if no null termination. */
188 * Function to parse a path and turn it into components
190 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
191 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
192 * general format ("smb:" only; we do not look for "cifs:").
196 * smb://[[[domain;]user[:password]@]server[:port][/share[/path[/file]]]]
201 * smb:// Show all workgroups.
203 * The method of locating the list of workgroups varies
204 * depending upon the setting of the context variable
205 * context->options.browse_max_lmb_count. This value
206 * determines the maximum number of local master browsers to
207 * query for the list of workgroups. In order to ensure that
208 * a complete list of workgroups is obtained, all master
209 * browsers must be queried, but if there are many
210 * workgroups, the time spent querying can begin to add up.
211 * For small networks (not many workgroups), it is suggested
212 * that this variable be set to 0, indicating query all local
213 * master browsers. When the network has many workgroups, a
214 * reasonable setting for this variable might be around 3.
216 * smb://name/ if name<1D> or name<1B> exists, list servers in
217 * workgroup, else, if name<20> exists, list all shares
220 * If "options" are provided, this function returns the entire option list as a
221 * string, for later parsing by the caller. Note that currently, no options
225 #define SMBC_PREFIX "smb:"
228 SMBC_parse_path(TALLOC_CTX
*ctx
,
243 char *workgroup
= NULL
;
246 /* Ensure these returns are at least valid pointers. */
247 *pp_server
= talloc_strdup(ctx
, "");
248 *p_port
= smbc_getPort(context
);
249 *pp_share
= talloc_strdup(ctx
, "");
250 *pp_path
= talloc_strdup(ctx
, "");
251 *pp_user
= talloc_strdup(ctx
, "");
252 *pp_password
= talloc_strdup(ctx
, "");
254 if (!*pp_server
|| !*pp_share
|| !*pp_path
||
255 !*pp_user
|| !*pp_password
) {
260 * Assume we wont find an authentication domain to parse, so default
261 * to the workgroup in the provided context.
263 if (pp_workgroup
!= NULL
) {
265 talloc_strdup(ctx
, smbc_getWorkgroup(context
));
269 *pp_options
= talloc_strdup(ctx
, "");
271 s
= talloc_strdup(ctx
, fname
);
273 /* see if it has the right prefix */
274 len
= strlen(SMBC_PREFIX
);
275 if (strncmp(s
,SMBC_PREFIX
,len
) || (s
[len
] != '/' && s
[len
] != 0)) {
276 return -1; /* What about no smb: ? */
281 /* Watch the test below, we are testing to see if we should exit */
283 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
284 DEBUG(1, ("Invalid path (does not begin with smb://"));
288 p
+= 2; /* Skip the double slash */
290 /* See if any options were specified */
291 if ((q
= strrchr(p
, '?')) != NULL
) {
292 /* There are options. Null terminate here and point to them */
295 DEBUG(4, ("Found options '%s'", q
));
297 /* Copy the options */
298 if (pp_options
&& *pp_options
!= NULL
) {
299 TALLOC_FREE(*pp_options
);
300 *pp_options
= talloc_strdup(ctx
, q
);
309 int wl
= strlen(smbc_getWorkgroup(context
));
315 *pp_server
= talloc_strdup(ctx
, smbc_getWorkgroup(context
));
319 (*pp_server
)[wl
] = '\0';
324 * ok, its for us. Now parse out the server, share etc.
326 * However, we want to parse out [[domain;]user[:password]@] if it
330 /* check that '@' occurs before '/', if '/' exists at all */
331 q
= strchr_m(p
, '@');
332 r
= strchr_m(p
, '/');
333 if (q
&& (!r
|| q
< r
)) {
334 char *userinfo
= NULL
;
337 next_token_no_ltrim_talloc(ctx
, &p
, &userinfo
, "@");
343 if (strchr_m(u
, ';')) {
344 next_token_no_ltrim_talloc(ctx
, &u
, &workgroup
, ";");
349 *pp_workgroup
= workgroup
;
353 if (strchr_m(u
, ':')) {
354 next_token_no_ltrim_talloc(ctx
, &u
, pp_user
, ":");
358 *pp_password
= talloc_strdup(ctx
, u
);
363 *pp_user
= talloc_strdup(ctx
, u
);
370 if (!next_token_talloc(ctx
, &p
, pp_server
, "/")) {
375 * Does *pp_server contain a ':' ? If so
376 * this denotes the port.
378 q
= strchr_m(*pp_server
, ':');
388 port
= strtol(q
, &endptr
, 10);
389 if (*endptr
!= '\0') {
393 *p_port
= (uint16_t)port
;
397 goto decoding
; /* That's it ... */
400 if (!next_token_talloc(ctx
, &p
, pp_share
, "/")) {
405 * Prepend a leading slash if there's a file path, as required by
409 *pp_path
= talloc_asprintf(ctx
,
413 *pp_path
= talloc_strdup(ctx
, "");
418 string_replace(*pp_path
, '/', '\\');
421 (void) urldecode_talloc(ctx
, pp_path
, *pp_path
);
422 (void) urldecode_talloc(ctx
, pp_server
, *pp_server
);
423 (void) urldecode_talloc(ctx
, pp_share
, *pp_share
);
424 (void) urldecode_talloc(ctx
, pp_user
, *pp_user
);
425 (void) urldecode_talloc(ctx
, pp_password
, *pp_password
);
428 workgroup
= talloc_strdup(ctx
, smbc_getWorkgroup(context
));
434 /* set the credentials to make DFS work */
435 smbc_set_credentials_with_fallback(context
,