ctdb-server: Clean up connection tracking functions
[samba4-gss.git] / source3 / libsmb / libsmb_path.c
blob12bfd21065cd0333ae62c2ecb7f77d9d169749f3
1 /*
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/>.
25 #include "includes.h"
26 #include "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * smbc_urldecode()
32 * and urldecode_talloc() (internal fn.)
34 * Convert strings of %xx to their single character equivalent. Each 'x' must
35 * be a valid hexadecimal digit, or that % sequence is left undecoded.
37 * dest may, but need not be, the same pointer as src.
39 * Returns the number of % sequences which could not be converted due to lack
40 * of two following hexadecimal digits.
42 static int
43 urldecode_talloc(TALLOC_CTX *ctx, char **pp_dest, const char *src)
45 int old_length = strlen(src);
46 int i = 0;
47 int err_count = 0;
48 size_t newlen = 1;
49 char *p, *dest;
51 if (old_length == 0) {
52 return 0;
55 *pp_dest = NULL;
56 for (i = 0; i < old_length; ) {
57 unsigned char character = src[i++];
59 if (character == '%') {
60 uint8_t v;
61 bool ok = hex_byte(&src[i], &v);
62 if (ok) {
63 character = v;
64 if (character == '\0') {
65 break; /* Stop at %00 */
67 i += 2;
68 } else {
69 err_count++;
72 newlen++;
75 dest = talloc_array(ctx, char, newlen);
76 if (!dest) {
77 return err_count;
80 err_count = 0;
81 for (p = dest, i = 0; i < old_length; ) {
82 unsigned char character = src[i++];
84 if (character == '%') {
85 uint8_t v;
86 bool ok = hex_byte(&src[i], &v);
87 if (ok) {
88 character = v;
89 if (character == '\0') {
90 break; /* Stop at %00 */
92 i += 2;
93 } else {
94 err_count++;
97 *p++ = character;
100 *p = '\0';
101 *pp_dest = dest;
102 return err_count;
106 smbc_urldecode(char *dest,
107 char *src,
108 size_t max_dest_len)
110 TALLOC_CTX *frame = talloc_stackframe();
111 char *pdest;
112 int ret = urldecode_talloc(frame, &pdest, src);
114 if (pdest) {
115 strlcpy(dest, pdest, max_dest_len);
117 TALLOC_FREE(frame);
118 return ret;
122 * smbc_urlencode()
124 * Convert any characters not specifically allowed in a URL into their %xx
125 * equivalent.
127 * Returns the remaining buffer length.
130 smbc_urlencode(char *dest,
131 char *src,
132 int max_dest_len)
134 for (; *src != '\0' && max_dest_len >= 3; src++) {
136 if ((*src < '0' &&
137 *src != '-' &&
138 *src != '.') ||
139 (*src > '9' &&
140 *src < 'A') ||
141 (*src > 'Z' &&
142 *src < 'a' &&
143 *src != '_') ||
144 (*src > 'z')) {
145 *dest++ = '%';
146 *dest++ = nybble_to_hex_upper(*src >> 4);
147 *dest++ = nybble_to_hex_upper(*src);
148 max_dest_len -= 3;
149 } else {
150 *dest++ = *src;
151 max_dest_len--;
155 if (max_dest_len <= 0) {
156 /* Ensure we return -1 if no null termination. */
157 return -1;
160 *dest++ = '\0';
161 max_dest_len--;
163 return max_dest_len;
167 * Function to parse a path and turn it into components
169 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
170 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
171 * general format ("smb:" only; we do not look for "cifs:").
174 * We accept:
175 * smb://[[[domain;]user[:password]@]server[:port][/share[/path[/file]]]]
176 * [?options]
178 * Meaning of URLs:
180 * smb:// Show all workgroups.
182 * The method of locating the list of workgroups varies
183 * depending upon the setting of the context variable
184 * context->options.browse_max_lmb_count. This value
185 * determines the maximum number of local master browsers to
186 * query for the list of workgroups. In order to ensure that
187 * a complete list of workgroups is obtained, all master
188 * browsers must be queried, but if there are many
189 * workgroups, the time spent querying can begin to add up.
190 * For small networks (not many workgroups), it is suggested
191 * that this variable be set to 0, indicating query all local
192 * master browsers. When the network has many workgroups, a
193 * reasonable setting for this variable might be around 3.
195 * smb://name/ if name<1D> or name<1B> exists, list servers in
196 * workgroup, else, if name<20> exists, list all shares
197 * for server ...
199 * If "options" are provided, this function returns the entire option list as a
200 * string, for later parsing by the caller. Note that currently, no options
201 * are supported.
204 #define SMBC_PREFIX "smb:"
207 SMBC_parse_path(TALLOC_CTX *ctx,
208 SMBCCTX *context,
209 const char *fname,
210 char **pp_workgroup,
211 char **pp_server,
212 uint16_t *p_port,
213 char **pp_share,
214 char **pp_path,
215 char **pp_user,
216 char **pp_password,
217 char **pp_options)
219 char *s;
220 const char *p;
221 char *q, *r;
222 char *workgroup = NULL;
223 int len;
225 /* Ensure these returns are at least valid pointers. */
226 *pp_server = talloc_strdup(ctx, "");
227 *p_port = smbc_getPort(context);
228 *pp_share = talloc_strdup(ctx, "");
229 *pp_path = talloc_strdup(ctx, "");
230 *pp_user = talloc_strdup(ctx, "");
231 *pp_password = talloc_strdup(ctx, "");
233 if (!*pp_server || !*pp_share || !*pp_path ||
234 !*pp_user || !*pp_password) {
235 return -1;
239 * Assume we won't find an authentication domain to parse, so default
240 * to the workgroup in the provided context.
242 if (pp_workgroup != NULL) {
243 *pp_workgroup =
244 talloc_strdup(ctx, smbc_getWorkgroup(context));
247 if (pp_options) {
248 *pp_options = talloc_strdup(ctx, "");
250 s = talloc_strdup(ctx, fname);
252 /* see if it has the right prefix */
253 len = strlen(SMBC_PREFIX);
254 if (strncmp(s,SMBC_PREFIX,len) || (s[len] != '/' && s[len] != 0)) {
255 return -1; /* What about no smb: ? */
258 p = s + len;
260 /* Watch the test below, we are testing to see if we should exit */
262 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
263 DEBUG(1, ("Invalid path (does not begin with smb://)\n"));
264 return -1;
267 p += 2; /* Skip the double slash */
269 /* See if any options were specified */
270 if ((q = strrchr(p, '?')) != NULL ) {
271 /* There are options. Null terminate here and point to them */
272 *q++ = '\0';
274 DEBUG(4, ("Found options '%s'\n", q));
276 /* Copy the options */
277 if (pp_options && *pp_options != NULL) {
278 TALLOC_FREE(*pp_options);
279 *pp_options = talloc_strdup(ctx, q);
283 if (*p == '\0') {
284 goto decoding;
287 if (*p == '/') {
288 *pp_server = talloc_strndup(
289 ctx, smbc_getWorkgroup(context), 16);
290 if (!*pp_server) {
291 return -1;
293 return 0;
297 * ok, its for us. Now parse out the server, share etc.
299 * However, we want to parse out [[domain;]user[:password]@] if it
300 * exists ...
303 /* check that '@' occurs before '/', if '/' exists at all */
304 q = strchr_m(p, '@');
305 r = strchr_m(p, '/');
306 if (q && (!r || q < r)) {
307 char *userinfo = NULL;
308 const char *u;
310 next_token_no_ltrim_talloc(ctx, &p, &userinfo, "@");
311 if (!userinfo) {
312 return -1;
314 u = userinfo;
316 if (strchr_m(u, ';')) {
317 next_token_no_ltrim_talloc(ctx, &u, &workgroup, ";");
318 if (!workgroup) {
319 return -1;
321 if (pp_workgroup) {
322 *pp_workgroup = workgroup;
326 if (strchr_m(u, ':')) {
327 next_token_no_ltrim_talloc(ctx, &u, pp_user, ":");
328 if (!*pp_user) {
329 return -1;
331 *pp_password = talloc_strdup(ctx, u);
332 if (!*pp_password) {
333 return -1;
335 } else {
336 *pp_user = talloc_strdup(ctx, u);
337 if (!*pp_user) {
338 return -1;
343 if (!next_token_talloc(ctx, &p, pp_server, "/")) {
344 return -1;
348 * Does *pp_server contain a ':' ? If so
349 * this denotes the port.
351 q = strchr_m(*pp_server, ':');
352 if (q != NULL) {
353 long int port;
354 char *endptr = NULL;
355 *q = '\0';
356 q++;
357 if (*q == '\0') {
358 /* Bad port. */
359 return -1;
361 port = strtol(q, &endptr, 10);
362 if (*endptr != '\0') {
363 /* Bad port. */
364 return -1;
366 *p_port = (uint16_t)port;
369 if (*p == (char)0) {
370 goto decoding; /* That's it ... */
373 if (!next_token_talloc(ctx, &p, pp_share, "/")) {
374 return -1;
378 * Prepend a leading slash if there's a file path, as required by
379 * NetApp filers.
381 if (*p != '\0') {
382 *pp_path = talloc_asprintf(ctx,
383 "\\%s",
385 } else {
386 *pp_path = talloc_strdup(ctx, "");
388 if (!*pp_path) {
389 return -1;
391 string_replace(*pp_path, '/', '\\');
393 decoding:
394 (void) urldecode_talloc(ctx, pp_path, *pp_path);
395 (void) urldecode_talloc(ctx, pp_server, *pp_server);
396 (void) urldecode_talloc(ctx, pp_share, *pp_share);
397 (void) urldecode_talloc(ctx, pp_user, *pp_user);
398 (void) urldecode_talloc(ctx, pp_password, *pp_password);
400 if (!workgroup) {
401 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
403 if (!workgroup) {
404 return -1;
407 /* set the credentials to make DFS work */
408 smbc_set_credentials_with_fallback(context,
409 workgroup,
410 *pp_user,
411 *pp_password);
412 return 0;