fetch.c 2.35 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-2007 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
46
	FILE *url;

47
	if( strncasecmp( "file:", urlstr, sizeof("file:")-1 ) == 0 ) {
48
49
		char *p;
		urlstr += sizeof("file:")-1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
50

51
		/* we don't check for LDAP_DIRSEP since URLs should contain '/' */
52
53
		if ( urlstr[0] == '/' && urlstr[1] == '/' ) {
			urlstr += 2;
54
			/* path must be absolute if authority is present */
55
			if ( urlstr[0] != '/' )
56
57
				return NULL;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
58

59
		p = ber_strdup( urlstr );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
60
61
		ldap_pvt_hex_unescape( p );

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

64
		ber_memfree( p );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
65
	} else {
66
67
68
69
#ifdef HAVE_FETCH
		url = fetchGetURL( (char*) urlstr, "" );
#else
		url = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
70
#endif
71
	}
72
73
74
75
76
77
78
	return url;
}

int
ldif_fetch_url(
    LDAP_CONST char	*urlstr,
    char	**valuep,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
79
    ber_len_t *vlenp )
80
81
82
83
84
85
86
87
88
89
90
{
	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
91

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

	total = 0;

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

	fclose( url );

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

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

	return 0;
}