Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/ldap/libldap/fetch.c
4393 views
1
/* fetch.c - routines for fetching data at URLs */
2
/* $OpenLDAP$ */
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4
*
5
* Copyright 1999-2024 The OpenLDAP Foundation.
6
* Portions Copyright 1999-2003 Kurt D. Zeilenga.
7
* All rights reserved.
8
*
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted only as authorized by the OpenLDAP
11
* Public License.
12
*
13
* A copy of this license is available in the file LICENSE in the
14
* top-level directory of the distribution or, alternatively, at
15
* <http://www.OpenLDAP.org/license.html>.
16
*/
17
/* This work was initially developed by Kurt D. Zeilenga for
18
* inclusion in OpenLDAP Software.
19
*/
20
21
#include "portable.h"
22
23
#include <stdio.h>
24
25
#include <ac/stdlib.h>
26
27
#include <ac/string.h>
28
#include <ac/socket.h>
29
#include <ac/time.h>
30
31
#ifdef HAVE_FETCH
32
#include <fetch.h>
33
#endif
34
35
#include "lber_pvt.h"
36
#include "ldap_pvt.h"
37
#include "ldap_config.h"
38
#include "ldif.h"
39
40
FILE *
41
ldif_open_url(
42
LDAP_CONST char *urlstr )
43
{
44
FILE *url;
45
46
if( strncasecmp( "file:", urlstr, sizeof("file:")-1 ) == 0 ) {
47
char *p;
48
urlstr += sizeof("file:")-1;
49
50
/* we don't check for LDAP_DIRSEP since URLs should contain '/' */
51
if ( urlstr[0] == '/' && urlstr[1] == '/' ) {
52
urlstr += 2;
53
/* path must be absolute if authority is present
54
* technically, file://hostname/path is also legal but we don't
55
* accept a non-empty hostname
56
*/
57
if ( urlstr[0] != '/' ) {
58
#ifdef _WIN32
59
/* An absolute path in improper file://C:/foo/bar format */
60
if ( urlstr[1] != ':' )
61
#endif
62
return NULL;
63
}
64
#ifdef _WIN32
65
/* An absolute path in proper file:///C:/foo/bar format */
66
if ( urlstr[2] == ':' )
67
urlstr++;
68
#endif
69
}
70
71
p = ber_strdup( urlstr );
72
if ( p == NULL )
73
return NULL;
74
75
/* But we should convert to LDAP_DIRSEP before use */
76
if ( LDAP_DIRSEP[0] != '/' ) {
77
char *s = p;
78
while (( s = strchr( s, '/' )))
79
*s++ = LDAP_DIRSEP[0];
80
}
81
82
ldap_pvt_hex_unescape( p );
83
84
url = fopen( p, "rb" );
85
86
ber_memfree( p );
87
} else {
88
#ifdef HAVE_FETCH
89
url = fetchGetURL( (char*) urlstr, "" );
90
#else
91
url = NULL;
92
#endif
93
}
94
return url;
95
}
96
97
int
98
ldif_fetch_url(
99
LDAP_CONST char *urlstr,
100
char **valuep,
101
ber_len_t *vlenp )
102
{
103
FILE *url;
104
char buffer[1024];
105
char *p = NULL;
106
size_t total;
107
size_t bytes;
108
109
*valuep = NULL;
110
*vlenp = 0;
111
112
url = ldif_open_url( urlstr );
113
114
if( url == NULL ) {
115
return -1;
116
}
117
118
total = 0;
119
120
while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
121
char *newp = ber_memrealloc( p, total + bytes + 1 );
122
if( newp == NULL ) {
123
ber_memfree( p );
124
fclose( url );
125
return -1;
126
}
127
p = newp;
128
AC_MEMCPY( &p[total], buffer, bytes );
129
total += bytes;
130
}
131
132
fclose( url );
133
134
if( total == 0 ) {
135
char *newp = ber_memrealloc( p, 1 );
136
if( newp == NULL ) {
137
ber_memfree( p );
138
return -1;
139
}
140
p = newp;
141
}
142
143
p[total] = '\0';
144
*valuep = p;
145
*vlenp = total;
146
147
return 0;
148
}
149
150