]> git.sur5r.net Git - openldap/blob - libraries/liblutil/fetch.c
Fix TLS default for clients
[openldap] / libraries / liblutil / 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-2007 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 "ldap_log.h"
36 #include "lber_pvt.h"
37 #include "ldap_pvt.h"
38 #include "ldap_config.h"
39 #include "ldif.h"
40
41 FILE *
42 ldif_open_url(
43         LDAP_CONST char *urlstr )
44 {
45         FILE *url;
46 #ifdef HAVE_FETCH
47         url = fetchGetURL( (char*) urlstr, "" );
48
49 #else
50         if( strncasecmp( "file:", urlstr, sizeof("file:")-1 ) == 0 ) {
51                 char *p;
52                 urlstr += sizeof("file:")-1;
53
54                 /* we don't check for LDAP_DIRSEP since URLs should contain '/' */
55                 if ( urlstr[0] == '/' && urlstr[1] == '/' ) {
56                         urlstr += 2;
57                         /* path must be absolute if authority is present */
58                         if ( urlstr[0] != '/' )
59                                 return NULL;
60                 }
61
62                 p = ber_strdup( urlstr );
63                 ldap_pvt_hex_unescape( p );
64
65                 url = fopen( p, "rb" );
66
67                 ber_memfree( p );
68         } else {
69                 return NULL;
70         }
71 #endif
72         return url;
73 }
74
75 int
76 ldif_fetch_url(
77     LDAP_CONST char     *urlstr,
78     char        **valuep,
79     ber_len_t *vlenp )
80 {
81         FILE *url;
82         char buffer[1024];
83         char *p = NULL;
84         size_t total;
85         size_t bytes;
86
87         *valuep = NULL;
88         *vlenp = 0;
89
90         url = ldif_open_url( urlstr );
91
92         if( url == NULL ) {
93                 return -1;
94         }
95
96         total = 0;
97
98         while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
99                 char *newp = ber_memrealloc( p, total + bytes + 1 );
100                 if( newp == NULL ) {
101                         ber_memfree( p );
102                         fclose( url );
103                         return -1;
104                 }
105                 p = newp;
106                 AC_MEMCPY( &p[total], buffer, bytes );
107                 total += bytes;
108         }
109
110         fclose( url );
111
112         if( total == 0 ) {
113                 char *newp = ber_memrealloc( p, 1 );
114                 if( newp == NULL ) {
115                         ber_memfree( p );
116                         return -1;
117                 }
118                 p = newp;
119         }
120
121         p[total] = '\0';
122         *valuep = p;
123         *vlenp = total;
124
125         return 0;
126 }