fetch.c 2.36 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* fetch.c - routines for fetching data at URLs */
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 1999-2006 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
15
16
17
18
 * 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.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
19
 */
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#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"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
37
#include "ldap_pvt.h"
38
#include "ldap_config.h"
39
40
#include "ldif.h"

41
42
FILE *
ldif_open_url(
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
43
	LDAP_CONST char *urlstr )
44
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
45
	FILE *url;
46
	char *p = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
47
48
49
50
51
52
53
#ifdef HAVE_FETCH
	url = fetchGetURL( (char*) urlstr, "" );

#else
	if( strncasecmp( "file://", urlstr, sizeof("file://")-1 ) == 0 ) {
		p = strchr( &urlstr[sizeof("file://")-1], '/' );
		if( p == NULL ) {
54
			return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
55
56
		}

57
		/* we don't check for LDAP_DIRSEP since URLs should contain '/' */
58
59
60
61
		if( p[1] == '.' && ( p[2] == '/' || ( p[2] == '.' && p[3] == '/' ))) {
			/* skip over false root */
			p++;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
62
63
64
65

		p = ber_strdup( p );
		ldap_pvt_hex_unescape( p );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
66
		url = fopen( p, "rb" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
67

68
		ber_memfree( p );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
69
	} else {
70
		return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
71
72
	}
#endif
73
74
75
76
77
78
79
	return url;
}

int
ldif_fetch_url(
    LDAP_CONST char	*urlstr,
    char	**valuep,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
80
    ber_len_t *vlenp )
81
82
83
84
85
86
87
88
89
90
91
{
	FILE *url;
	char buffer[1024];
	char *p = NULL;
	size_t total;
	size_t bytes;

	*valuep = NULL;
	*vlenp = 0;

	url = ldif_open_url( urlstr );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
92

93
94
95
96
97
98
	if( url == NULL ) {
		return -1;
	}

	total = 0;

Hallvard Furuseth's avatar
Hallvard Furuseth committed
99
	while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
100
		char *newp = ber_memrealloc( p, total + bytes + 1 );
101
102
103
104
105
		if( newp == NULL ) {
			ber_memfree( p );
			fclose( url );
			return -1;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
106
		p = newp;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
107
		AC_MEMCPY( &p[total], buffer, bytes );
108
109
110
111
112
		total += bytes;
	}

	fclose( url );

113
114
115
116
117
118
119
120
121
	if( total == 0 ) {
		char *newp = ber_memrealloc( p, 1 );
		if( newp == NULL ) {
			ber_memfree( p );
			return -1;
		}
		p = newp;
	}

122
	p[total] = '\0';
123
124
125
126
127
	*valuep = p;
	*vlenp = total;

	return 0;
}