2003-11-26 07:19:47 +08:00
|
|
|
/* fetch.c - routines for fetching data at URLs */
|
1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:19:47 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2008-01-08 08:19:56 +08:00
|
|
|
* Copyright 1999-2008 The OpenLDAP Foundation.
|
2003-11-26 07:19:47 +08:00
|
|
|
* Portions Copyright 1999-2003 Kurt D. Zeilenga.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* This work was initially developed by Kurt D. Zeilenga for
|
|
|
|
* inclusion in OpenLDAP Software.
|
1999-08-05 07:57:27 +08:00
|
|
|
*/
|
1999-07-31 11:23:26 +08:00
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_FETCH
|
|
|
|
#include <fetch.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ldap_log.h"
|
|
|
|
#include "lber_pvt.h"
|
1999-08-04 02:14:24 +08:00
|
|
|
#include "ldap_pvt.h"
|
1999-08-04 03:04:02 +08:00
|
|
|
#include "ldap_config.h"
|
1999-07-31 11:23:26 +08:00
|
|
|
#include "ldif.h"
|
|
|
|
|
2005-05-12 08:46:39 +08:00
|
|
|
FILE *
|
|
|
|
ldif_open_url(
|
2005-07-10 02:57:10 +08:00
|
|
|
LDAP_CONST char *urlstr )
|
1999-07-31 11:23:26 +08:00
|
|
|
{
|
1999-08-04 02:14:24 +08:00
|
|
|
FILE *url;
|
|
|
|
|
2006-11-28 02:59:46 +08:00
|
|
|
if( strncasecmp( "file:", urlstr, sizeof("file:")-1 ) == 0 ) {
|
2007-01-11 20:06:31 +08:00
|
|
|
char *p;
|
|
|
|
urlstr += sizeof("file:")-1;
|
1999-08-04 02:14:24 +08:00
|
|
|
|
2001-12-07 09:46:26 +08:00
|
|
|
/* we don't check for LDAP_DIRSEP since URLs should contain '/' */
|
2007-01-11 20:06:31 +08:00
|
|
|
if ( urlstr[0] == '/' && urlstr[1] == '/' ) {
|
|
|
|
urlstr += 2;
|
2006-11-28 03:03:59 +08:00
|
|
|
/* path must be absolute if authority is present */
|
2007-01-11 20:06:31 +08:00
|
|
|
if ( urlstr[0] != '/' )
|
2006-11-28 03:03:59 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1999-08-04 02:14:24 +08:00
|
|
|
|
2007-01-11 20:06:31 +08:00
|
|
|
p = ber_strdup( urlstr );
|
2007-12-02 03:19:20 +08:00
|
|
|
|
|
|
|
/* But we should convert to LDAP_DIRSEP before use */
|
|
|
|
if ( LDAP_DIRSEP[0] != '/' ) {
|
|
|
|
char *s = p;
|
|
|
|
while (( s = strchr( s, '/' )))
|
|
|
|
*s++ = LDAP_DIRSEP[0];
|
|
|
|
}
|
|
|
|
|
1999-08-04 02:14:24 +08:00
|
|
|
ldap_pvt_hex_unescape( p );
|
|
|
|
|
2000-06-21 08:40:10 +08:00
|
|
|
url = fopen( p, "rb" );
|
1999-08-04 02:14:24 +08:00
|
|
|
|
2005-05-12 08:46:39 +08:00
|
|
|
ber_memfree( p );
|
1999-08-04 02:14:24 +08:00
|
|
|
} else {
|
2007-09-02 11:43:58 +08:00
|
|
|
#ifdef HAVE_FETCH
|
|
|
|
url = fetchGetURL( (char*) urlstr, "" );
|
|
|
|
#else
|
|
|
|
url = NULL;
|
1999-08-04 02:14:24 +08:00
|
|
|
#endif
|
2007-09-02 11:43:58 +08:00
|
|
|
}
|
2005-05-12 08:46:39 +08:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldif_fetch_url(
|
|
|
|
LDAP_CONST char *urlstr,
|
|
|
|
char **valuep,
|
2005-07-10 02:57:10 +08:00
|
|
|
ber_len_t *vlenp )
|
2005-05-12 08:46:39 +08:00
|
|
|
{
|
|
|
|
FILE *url;
|
|
|
|
char buffer[1024];
|
|
|
|
char *p = NULL;
|
|
|
|
size_t total;
|
|
|
|
size_t bytes;
|
|
|
|
|
|
|
|
*valuep = NULL;
|
|
|
|
*vlenp = 0;
|
|
|
|
|
|
|
|
url = ldif_open_url( urlstr );
|
1999-08-04 02:14:24 +08:00
|
|
|
|
1999-07-31 11:23:26 +08:00
|
|
|
if( url == NULL ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
total = 0;
|
|
|
|
|
1999-09-06 12:42:20 +08:00
|
|
|
while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
|
2002-12-03 06:08:26 +08:00
|
|
|
char *newp = ber_memrealloc( p, total + bytes + 1 );
|
1999-07-31 11:23:26 +08:00
|
|
|
if( newp == NULL ) {
|
|
|
|
ber_memfree( p );
|
|
|
|
fclose( url );
|
|
|
|
return -1;
|
|
|
|
}
|
2000-06-21 08:40:10 +08:00
|
|
|
p = newp;
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY( &p[total], buffer, bytes );
|
1999-07-31 11:23:26 +08:00
|
|
|
total += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( url );
|
|
|
|
|
2002-12-03 05:46:58 +08:00
|
|
|
if( total == 0 ) {
|
|
|
|
char *newp = ber_memrealloc( p, 1 );
|
|
|
|
if( newp == NULL ) {
|
|
|
|
ber_memfree( p );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p = newp;
|
|
|
|
}
|
|
|
|
|
2002-12-03 06:08:26 +08:00
|
|
|
p[total] = '\0';
|
1999-07-31 11:23:26 +08:00
|
|
|
*valuep = p;
|
|
|
|
*vlenp = total;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|