source: vendor/current/source3/libsmb/libsmb_path.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 11.2 KB
Line 
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
10
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.
15
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.
20
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/>.
23*/
24
25#include "includes.h"
26#include "libsmbclient.h"
27#include "libsmb_internal.h"
28
29
30/* Used by urldecode_talloc() */
31static int
32hex2int( unsigned int _char )
33{
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')
39 return _char - '0';
40 return -1;
41}
42
43/*
44 * smbc_urldecode()
45 * and urldecode_talloc() (internal fn.)
46 *
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.
49 *
50 * dest may, but need not be, the same pointer as src.
51 *
52 * Returns the number of % sequences which could not be converted due to lack
53 * of two following hexadecimal digits.
54 */
55static int
56urldecode_talloc(TALLOC_CTX *ctx, char **pp_dest, const char *src)
57{
58 int old_length = strlen(src);
59 int i = 0;
60 int err_count = 0;
61 size_t newlen = 1;
62 char *p, *dest;
63
64 if (old_length == 0) {
65 return 0;
66 }
67
68 *pp_dest = NULL;
69 for (i = 0; i < old_length; ) {
70 unsigned char character = src[i++];
71
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;
75
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 */
82 }
83 i += 2;
84 } else {
85 err_count++;
86 }
87 }
88 newlen++;
89 }
90
91 dest = talloc_array(ctx, char, newlen);
92 if (!dest) {
93 return err_count;
94 }
95
96 err_count = 0;
97 for (p = dest, i = 0; i < old_length; ) {
98 unsigned char character = src[i++];
99
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;
103
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 */
110 }
111 i += 2;
112 } else {
113 err_count++;
114 }
115 }
116 *p++ = character;
117 }
118
119 *p = '\0';
120 *pp_dest = dest;
121 return err_count;
122}
123
124int
125smbc_urldecode(char *dest,
126 char *src,
127 size_t max_dest_len)
128{
129 TALLOC_CTX *frame = talloc_stackframe();
130 char *pdest;
131 int ret = urldecode_talloc(frame, &pdest, src);
132
133 if (pdest) {
134 strlcpy(dest, pdest, max_dest_len);
135 }
136 TALLOC_FREE(frame);
137 return ret;
138}
139
140/*
141 * smbc_urlencode()
142 *
143 * Convert any characters not specifically allowed in a URL into their %xx
144 * equivalent.
145 *
146 * Returns the remaining buffer length.
147 */
148int
149smbc_urlencode(char *dest,
150 char *src,
151 int max_dest_len)
152{
153 char hex[] = "0123456789ABCDEF";
154
155 for (; *src != '\0' && max_dest_len >= 3; src++) {
156
157 if ((*src < '0' &&
158 *src != '-' &&
159 *src != '.') ||
160 (*src > '9' &&
161 *src < 'A') ||
162 (*src > 'Z' &&
163 *src < 'a' &&
164 *src != '_') ||
165 (*src > 'z')) {
166 *dest++ = '%';
167 *dest++ = hex[(*src >> 4) & 0x0f];
168 *dest++ = hex[*src & 0x0f];
169 max_dest_len -= 3;
170 } else {
171 *dest++ = *src;
172 max_dest_len--;
173 }
174 }
175
176 *dest++ = '\0';
177 max_dest_len--;
178
179 return max_dest_len;
180}
181
182/*
183 * Function to parse a path and turn it into components
184 *
185 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
186 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
187 * general format ("smb:" only; we do not look for "cifs:").
188 *
189 *
190 * We accept:
191 * smb://[[[domain;]user[:password]@]server[:port][/share[/path[/file]]]]
192 * [?options]
193 *
194 * Meaning of URLs:
195 *
196 * smb:// Show all workgroups.
197 *
198 * The method of locating the list of workgroups varies
199 * depending upon the setting of the context variable
200 * context->options.browse_max_lmb_count. This value
201 * determines the maximum number of local master browsers to
202 * query for the list of workgroups. In order to ensure that
203 * a complete list of workgroups is obtained, all master
204 * browsers must be queried, but if there are many
205 * workgroups, the time spent querying can begin to add up.
206 * For small networks (not many workgroups), it is suggested
207 * that this variable be set to 0, indicating query all local
208 * master browsers. When the network has many workgroups, a
209 * reasonable setting for this variable might be around 3.
210 *
211 * smb://name/ if name<1D> or name<1B> exists, list servers in
212 * workgroup, else, if name<20> exists, list all shares
213 * for server ...
214 *
215 * If "options" are provided, this function returns the entire option list as a
216 * string, for later parsing by the caller. Note that currently, no options
217 * are supported.
218 */
219
220#define SMBC_PREFIX "smb:"
221
222int
223SMBC_parse_path(TALLOC_CTX *ctx,
224 SMBCCTX *context,
225 const char *fname,
226 char **pp_workgroup,
227 char **pp_server,
228 uint16_t *p_port,
229 char **pp_share,
230 char **pp_path,
231 char **pp_user,
232 char **pp_password,
233 char **pp_options)
234{
235 char *s;
236 const char *p;
237 char *q, *r;
238 char *workgroup = NULL;
239 int len;
240
241 /* Ensure these returns are at least valid pointers. */
242 *pp_server = talloc_strdup(ctx, "");
243 *p_port = smbc_getPort(context);
244 *pp_share = talloc_strdup(ctx, "");
245 *pp_path = talloc_strdup(ctx, "");
246 *pp_user = talloc_strdup(ctx, "");
247 *pp_password = talloc_strdup(ctx, "");
248
249 if (!*pp_server || !*pp_share || !*pp_path ||
250 !*pp_user || !*pp_password) {
251 return -1;
252 }
253
254 /*
255 * Assume we wont find an authentication domain to parse, so default
256 * to the workgroup in the provided context.
257 */
258 if (pp_workgroup != NULL) {
259 *pp_workgroup =
260 talloc_strdup(ctx, smbc_getWorkgroup(context));
261 }
262
263 if (pp_options) {
264 *pp_options = talloc_strdup(ctx, "");
265 }
266 s = talloc_strdup(ctx, fname);
267
268 /* see if it has the right prefix */
269 len = strlen(SMBC_PREFIX);
270 if (strncmp(s,SMBC_PREFIX,len) || (s[len] != '/' && s[len] != 0)) {
271 return -1; /* What about no smb: ? */
272 }
273
274 p = s + len;
275
276 /* Watch the test below, we are testing to see if we should exit */
277
278 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
279 DEBUG(1, ("Invalid path (does not begin with smb://"));
280 return -1;
281 }
282
283 p += 2; /* Skip the double slash */
284
285 /* See if any options were specified */
286 if ((q = strrchr(p, '?')) != NULL ) {
287 /* There are options. Null terminate here and point to them */
288 *q++ = '\0';
289
290 DEBUG(4, ("Found options '%s'", q));
291
292 /* Copy the options */
293 if (pp_options && *pp_options != NULL) {
294 TALLOC_FREE(*pp_options);
295 *pp_options = talloc_strdup(ctx, q);
296 }
297 }
298
299 if (*p == '\0') {
300 goto decoding;
301 }
302
303 if (*p == '/') {
304 int wl = strlen(smbc_getWorkgroup(context));
305
306 if (wl > 16) {
307 wl = 16;
308 }
309
310 *pp_server = talloc_strdup(ctx, smbc_getWorkgroup(context));
311 if (!*pp_server) {
312 return -1;
313 }
314 (*pp_server)[wl] = '\0';
315 return 0;
316 }
317
318 /*
319 * ok, its for us. Now parse out the server, share etc.
320 *
321 * However, we want to parse out [[domain;]user[:password]@] if it
322 * exists ...
323 */
324
325 /* check that '@' occurs before '/', if '/' exists at all */
326 q = strchr_m(p, '@');
327 r = strchr_m(p, '/');
328 if (q && (!r || q < r)) {
329 char *userinfo = NULL;
330 const char *u;
331
332 next_token_no_ltrim_talloc(ctx, &p, &userinfo, "@");
333 if (!userinfo) {
334 return -1;
335 }
336 u = userinfo;
337
338 if (strchr_m(u, ';')) {
339 next_token_no_ltrim_talloc(ctx, &u, &workgroup, ";");
340 if (!workgroup) {
341 return -1;
342 }
343 if (pp_workgroup) {
344 *pp_workgroup = workgroup;
345 }
346 }
347
348 if (strchr_m(u, ':')) {
349 next_token_no_ltrim_talloc(ctx, &u, pp_user, ":");
350 if (!*pp_user) {
351 return -1;
352 }
353 *pp_password = talloc_strdup(ctx, u);
354 if (!*pp_password) {
355 return -1;
356 }
357 } else {
358 *pp_user = talloc_strdup(ctx, u);
359 if (!*pp_user) {
360 return -1;
361 }
362 }
363 }
364
365 if (!next_token_talloc(ctx, &p, pp_server, "/")) {
366 return -1;
367 }
368
369 /*
370 * Does *pp_server contain a ':' ? If so
371 * this denotes the port.
372 */
373 q = strchr_m(*pp_server, ':');
374 if (q != NULL) {
375 long int port;
376 char *endptr = NULL;
377 *q = '\0';
378 q++;
379 if (*q == '\0') {
380 /* Bad port. */
381 return -1;
382 }
383 port = strtol(q, &endptr, 10);
384 if (*endptr != '\0') {
385 /* Bad port. */
386 return -1;
387 }
388 *p_port = (uint16_t)port;
389 }
390
391 if (*p == (char)0) {
392 goto decoding; /* That's it ... */
393 }
394
395 if (!next_token_talloc(ctx, &p, pp_share, "/")) {
396 return -1;
397 }
398
399 /*
400 * Prepend a leading slash if there's a file path, as required by
401 * NetApp filers.
402 */
403 if (*p != '\0') {
404 *pp_path = talloc_asprintf(ctx,
405 "\\%s",
406 p);
407 } else {
408 *pp_path = talloc_strdup(ctx, "");
409 }
410 if (!*pp_path) {
411 return -1;
412 }
413 string_replace(*pp_path, '/', '\\');
414
415decoding:
416 (void) urldecode_talloc(ctx, pp_path, *pp_path);
417 (void) urldecode_talloc(ctx, pp_server, *pp_server);
418 (void) urldecode_talloc(ctx, pp_share, *pp_share);
419 (void) urldecode_talloc(ctx, pp_user, *pp_user);
420 (void) urldecode_talloc(ctx, pp_password, *pp_password);
421
422 if (!workgroup) {
423 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
424 }
425 if (!workgroup) {
426 return -1;
427 }
428
429 /* set the credentials to make DFS work */
430 smbc_set_credentials_with_fallback(context,
431 workgroup,
432 *pp_user,
433 *pp_password);
434 return 0;
435}
436
Note: See TracBrowser for help on using the repository browser.