1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* LDAP tools fileurl.c -- functions for handling file URLs.
* Used by ldapmodify.
*/
#include "ldaptool.h"
#include "fileurl.h"
#include <ctype.h> /* for isalpha() */
static int str_starts_with( const char *s, char *prefix );
static void hex_unescape( char *s );
static int unhex( char c );
static void strcpy_escaped_and_convert( char *s1, char *s2 );
static int berval_from_file( const char *path, struct berval *bvp,
int reporterrs );
/*
* Convert a file URL to a local path.
*
* If successful, LDAPTOOL_FILEURL_SUCCESS is returned and *localpathp is
* set point to an allocated string. If not, an different LDAPTOOL_FILEURL_
* error code is returned.
*
* See RFCs 1738 and 2396 for a specification for file URLs... but
* Netscape Navigator seems to be a bit more lenient in what it will
* accept, especially on Windows).
*
* This function parses file URLs of these three forms:
*
* file:///path
* file:/path
* file://localhost/path
* file://host/path (rejected with a ...NONLOCAL error)
*
* On Windows, we convert leading drive letters of the form C| to C:
* and if a drive letter is present we strip off the slash that precedes
* path. Otherwise, the leading slash is returned.
*
*/
int
ldaptool_fileurl2path( const char *fileurl, char **localpathp )
{
const char *path;
char *pathcopy;
/*
* Make sure this is a file URL we can handle.
*/
if ( !str_starts_with( fileurl, "file:" )) {
return( LDAPTOOL_FILEURL_NOTAFILEURL );
}
path = fileurl + 5; /* skip past "file:" scheme prefix */
if ( *path != '/' ) {
return( LDAPTOOL_FILEURL_MISSINGPATH );
}
++path; /* skip past '/' at end of "file:/" */
if ( *path == '/' ) {
++path; /* remainder is now host/path or /path */
if ( *path != '/' ) {
/*
* Make sure it is for the local host.
*/
if ( str_starts_with( path, "localhost/" )) {
path += 9;
} else {
return( LDAPTOOL_FILEURL_NONLOCAL );
}
}
} else { /* URL is of the form file:/path */
--path;
}
/*
* The remainder is now of the form /path. On Windows, skip past the
* leading slash if a drive letter is present.
*/
#ifdef _WINDOWS
if ( isalpha( path[1] ) && ( path[2] == '|' || path[2] == ':' )) {
++path;
}
#endif /* _WINDOWS */
/*
* Duplicate the path so we can safely alter it.
* Unescape any %HH sequences.
*/
if (( pathcopy = strdup( path )) == NULL ) {
return( LDAPTOOL_FILEURL_NOMEMORY );
}
hex_unescape( pathcopy );
#ifdef _WINDOWS
/*
* Convert forward slashes to backslashes for Windows. Also,
* if we see a drive letter / vertical bar combination (e.g., c|)
* at the beginning of the path, replace the '|' with a ':'.
*/
{
char *p;
for ( p = pathcopy; *p != '\0'; ++p ) {
if ( *p == '/' ) {
*p = '\\';
}
}
}
if ( isalpha( pathcopy[0] ) && pathcopy[1] == '|' ) {
pathcopy[1] = ':';
}
#endif /* _WINDOWS */
*localpathp = pathcopy;
return( LDAPTOOL_FILEURL_SUCCESS );
}
/*
* Convert a local path to a file URL.
*
* If successful, LDAPTOOL_FILEURL_SUCCESS is returned and *urlp is
* set point to an allocated string. If not, an different LDAPTOOL_FILEURL_
* error code is returned. At present, the only possible error is
* LDAPTOOL_FILEURL_NOMEMORY.
*
* This function produces file URLs of the form file:path.
*
* On Windows, we convert leading drive letters to C|.
*
*/
int
ldaptool_path2fileurl( char *path, char **urlp )
{
char *p, *url, *prefix ="file:";
if ( NULL == path ) {
path = "/";
}
/*
* Allocate space for the URL, taking into account that path may
* expand during the hex escaping process.
*/
if (( url = malloc( strlen( prefix ) + 3 * strlen( path ) + 1 )) == NULL ) {
return( LDAPTOOL_FILEURL_NOMEMORY );
}
strcpy( url, prefix );
p = url + strlen( prefix );
#ifdef _WINDOWS
/*
* On Windows, convert leading drive letters (e.g., C:) to the correct URL
* syntax (e.g., C|).
*/
if ( isalpha( path[0] ) && path[1] == ':' ) {
*p++ = path[0];
*p++ = '|';
path += 2;
*p = '\0';
}
#endif /* _WINDOWS */
/*
* Append the path, encoding any URL-special characters using the %HH
* convention.
* On Windows, convert backwards slashes in the path to forward ones.
*/
strcpy_escaped_and_convert( p, path );
*urlp = url;
return( LDAPTOOL_FILEURL_SUCCESS );
}
/*
* Populate *bvp from "value" of length "vlen."
*
* If recognize_url_syntax is non-zero, :<fileurl is recognized.
* If always_try_file is recognized and no file URL was found, an
* attempt is made to stat and read the value as if it were the name
* of a file.
*
* If reporterrs is non-zero, specific error messages are printed to
* stderr.
*
* If successful, LDAPTOOL_FILEURL_SUCCESS is returned and bvp->bv_len
* and bvp->bv_val are set (the latter is set to malloc'd memory).
* Upon failure, a different LDAPTOOL_FILEURL_ error code is returned.
*/
int
ldaptool_berval_from_ldif_value( const char *value, int vlen,
struct berval *bvp, int recognize_url_syntax, int always_try_file,
int reporterrs )
{
int rc = LDAPTOOL_FILEURL_SUCCESS; /* optimistic */
const char *url = NULL;
struct stat fstats;
/* recognize "attr :< url" syntax if LDIF version is >= 1 */
if ( recognize_url_syntax && *value == '<' ) {
for ( url = value + 1; isspace( *url ); ++url ) {
; /* NULL */
}
if (strlen(url) < 6 || strncasecmp(url, "file:/", 6) != 0) {
/*
* We only support file:// like URLs for now.
*/
url = NULL;
}
}
if ( NULL != url ) {
char *path;
rc = ldaptool_fileurl2path( url, &path );
switch( rc ) {
case LDAPTOOL_FILEURL_NOTAFILEURL:
if ( reporterrs ) fprintf( stderr, "%s: unsupported URL \"%s\";"
" use a file:// URL instead.\n", ldaptool_progname, url );
break;
case LDAPTOOL_FILEURL_MISSINGPATH:
if ( reporterrs ) fprintf( stderr,
"%s: unable to process URL \"%s\" --"
" missing path.\n", ldaptool_progname, url );
break;
case LDAPTOOL_FILEURL_NONLOCAL:
if ( reporterrs ) fprintf( stderr,
"%s: unable to process URL \"%s\" -- only"
" local file:// URLs are supported.\n",
ldaptool_progname, url );
break;
case LDAPTOOL_FILEURL_NOMEMORY:
if ( reporterrs ) perror( "ldaptool_fileurl2path" );
break;
case LDAPTOOL_FILEURL_SUCCESS:
if ( stat( path, &fstats ) != 0 ) {
if ( reporterrs ) perror( path );
} else if ( fstats.st_mode & S_IFDIR ) {
if ( reporterrs ) fprintf( stderr,
"%s: %s is a directory, not a file\n",
ldaptool_progname, path );
rc = LDAPTOOL_FILEURL_FILEIOERROR;
} else {
rc = berval_from_file( path, bvp, reporterrs );
}
free( path );
break;
default:
if ( reporterrs ) fprintf( stderr,
"%s: unable to process URL \"%s\""
" -- unknown error\n", ldaptool_progname, url );
}
} else if ( always_try_file && (stat( value, &fstats ) == 0) &&
!(fstats.st_mode & S_IFDIR)) { /* get value from file */
rc = berval_from_file( value, bvp, reporterrs );
} else { /* use value as-is */
bvp->bv_len = vlen;
if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
if ( reporterrs ) perror( "malloc" );
rc = LDAPTOOL_FILEURL_NOMEMORY;
} else {
SAFEMEMCPY( bvp->bv_val, value, vlen );
bvp->bv_val[ vlen ] = '\0';
}
}
return( rc );
}
/*
* Map an LDAPTOOL_FILEURL_ error code to an LDAP error code (crude).
*/
int
ldaptool_fileurlerr2ldaperr( int lderr )
{
int rc;
switch( lderr ) {
case LDAPTOOL_FILEURL_SUCCESS:
rc = LDAP_SUCCESS;
break;
case LDAPTOOL_FILEURL_NOMEMORY:
rc = LDAP_NO_MEMORY;
break;
default:
rc = LDAP_PARAM_ERROR;
}
return( rc );
}
/*
* Populate *bvp with the contents of the file named by "path".
*
* If reporterrs is non-zero, specific error messages are printed to
* stderr.
*
* If successful, LDAPTOOL_FILEURL_SUCCESS is returned and bvp->bv_len
* and bvp->bv_val are set (the latter is set to malloc'd memory).
* Upon failure, a different LDAPTOOL_FILEURL_ error code is returned.
*/
static int
berval_from_file( const char *path, struct berval *bvp, int reporterrs )
{
FILE *fp;
long rlen;
#if defined( XP_WIN32 )
char mode[20] = "r+b";
#else
char mode[20] = "r";
#endif
if (( fp = ldaptool_open_file( path, mode )) == NULL ) {
if ( reporterrs ) perror( path );
return( LDAPTOOL_FILEURL_FILEIOERROR );
}
if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
if ( reporterrs ) perror( path );
fclose( fp );
return( LDAPTOOL_FILEURL_FILEIOERROR );
}
bvp->bv_len = ftell( fp );
if (( bvp->bv_val = (char *)malloc( bvp->bv_len + 1 )) == NULL ) {
if ( reporterrs ) perror( "malloc" );
fclose( fp );
return( LDAPTOOL_FILEURL_NOMEMORY );
}
if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
if ( reporterrs ) perror( path );
fclose( fp );
return( LDAPTOOL_FILEURL_FILEIOERROR );
}
rlen = fread( bvp->bv_val, 1, bvp->bv_len, fp );
fclose( fp );
if ( rlen != (long)bvp->bv_len ) {
if ( reporterrs ) perror( path );
free( bvp->bv_val );
return( LDAPTOOL_FILEURL_FILEIOERROR );
}
bvp->bv_val[ bvp->bv_len ] = '\0';
return( LDAPTOOL_FILEURL_SUCCESS );
}
/*
* Return a non-zero value if the string s begins with prefix and zero if not.
*/
static int
str_starts_with( const char *s, char *prefix )
{
size_t prefix_len;
if ( s == NULL || prefix == NULL ) {
return( 0 );
}
prefix_len = strlen( prefix );
if ( strlen( s ) < prefix_len ) {
return( 0 );
}
return( strncmp( s, prefix, prefix_len ) == 0 );
}
/*
* Remove URL hex escapes from s... done in place. The basic concept for
* this routine is borrowed from the WWW library HTUnEscape() routine.
*
* A similar function called nsldapi_hex_unescape can be found in
* ../../libraries/libldap/unescape.c
*/
static void
hex_unescape( char *s )
{
char *p;
for ( p = s; *s != '\0'; ++s ) {
if ( *s == '%' ) {
if ( *++s != '\0' ) {
*p = unhex( *s ) << 4;
}
if ( *++s != '\0' ) {
*p++ += unhex( *s );
}
} else {
*p++ = *s;
}
}
*p = '\0';
}
/*
* Return the integer equivalent of one hex digit (in c).
*
* A similar function can be found in ../../libraries/libldap/unescape.c
*/
static int
unhex( char c )
{
return( c >= '0' && c <= '9' ? c - '0'
: c >= 'A' && c <= 'F' ? c - 'A' + 10
: c - 'a' + 10 );
}
#define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
( c >= '@' && c <= 'Z' ) || \
( c == '_' ) || \
( c >= 'a' && c <= 'z' ))
/*
* Like strcat(), except if any URL-special characters are found in s2
* they are escaped using the %HH convention and backslash characters are
* converted to forward slashes on Windows.
*
* Maximum space needed in s1 is 3 * strlen( s2 ) + 1.
*
* A similar function that does not convert the slashes called
* strcat_escaped() can be found in ../../libraries/libldap/tmplout.c
*/
static void
strcpy_escaped_and_convert( char *s1, char *s2 )
{
char *p, *q;
char *hexdig = "0123456789ABCDEF";
p = s1 + strlen( s1 );
for ( q = s2; *q != '\0'; ++q ) {
#ifdef _WINDOWS
if ( *q == '\\' ) {
*p++ = '/';
} else
#endif /* _WINDOWS */
if ( HREF_CHAR_ACCEPTABLE( *q )) {
*p++ = *q;
} else {
*p++ = '%';
*p++ = hexdig[ 0x0F & ((*(unsigned char*)q) >> 4) ];
*p++ = hexdig[ 0x0F & *q ];
}
}
*p = '\0';
}
|