]> git.sur5r.net Git - openldap/blob - libraries/libldap/fetch.c
Happy New Year (belated)
[openldap] / libraries / libldap / fetch.c
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-2014 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                         if ( urlstr[0] != '/' )
55                                 return NULL;
56                 }
57
58                 p = ber_strdup( urlstr );
59
60                 /* But we should convert to LDAP_DIRSEP before use */
61                 if ( LDAP_DIRSEP[0] != '/' ) {
62                         char *s = p;
63                         while (( s = strchr( s, '/' )))
64                                 *s++ = LDAP_DIRSEP[0];
65                 }
66
67                 ldap_pvt_hex_unescape( p );
68
69                 url = fopen( p, "rb" );
70
71                 ber_memfree( p );
72         } else {
73 #ifdef HAVE_FETCH
74                 url = fetchGetURL( (char*) urlstr, "" );
75 #else
76                 url = NULL;
77 #endif
78         }
79         return url;
80 }
81
82 int
83 ldif_fetch_url(
84     LDAP_CONST char     *urlstr,
85     char        **valuep,
86     ber_len_t *vlenp )
87 {
88         FILE *url;
89         char buffer[1024];
90         char *p = NULL;
91         size_t total;
92         size_t bytes;
93
94         *valuep = NULL;
95         *vlenp = 0;
96
97         url = ldif_open_url( urlstr );
98
99         if( url == NULL ) {
100                 return -1;
101         }
102
103         total = 0;
104
105         while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
106                 char *newp = ber_memrealloc( p, total + bytes + 1 );
107                 if( newp == NULL ) {
108                         ber_memfree( p );
109                         fclose( url );
110                         return -1;
111                 }
112                 p = newp;
113                 AC_MEMCPY( &p[total], buffer, bytes );
114                 total += bytes;
115         }
116
117         fclose( url );
118
119         if( total == 0 ) {
120                 char *newp = ber_memrealloc( p, 1 );
121                 if( newp == NULL ) {
122                         ber_memfree( p );
123                         return -1;
124                 }
125                 p = newp;
126         }
127
128         p[total] = '\0';
129         *valuep = p;
130         *vlenp = total;
131
132         return 0;
133 }