Back to index

openldap  2.4.31
fetch.c
Go to the documentation of this file.
00001 /* fetch.c - routines for fetching data at URLs */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1999-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 1999-2003 Kurt D. Zeilenga.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* This work was initially developed by Kurt D. Zeilenga for
00018  * inclusion in OpenLDAP Software.
00019  */
00020 
00021 #include "portable.h"
00022 
00023 #include <stdio.h>
00024 
00025 #include <ac/stdlib.h>
00026 
00027 #include <ac/string.h>
00028 #include <ac/socket.h>
00029 #include <ac/time.h>
00030 
00031 #ifdef HAVE_FETCH
00032 #include <fetch.h>
00033 #endif
00034 
00035 #include "lber_pvt.h"
00036 #include "ldap_pvt.h"
00037 #include "ldap_config.h"
00038 #include "ldif.h"
00039 
00040 FILE *
00041 ldif_open_url(
00042        LDAP_CONST char *urlstr )
00043 {
00044        FILE *url;
00045 
00046        if( strncasecmp( "file:", urlstr, sizeof("file:")-1 ) == 0 ) {
00047               char *p;
00048               urlstr += sizeof("file:")-1;
00049 
00050               /* we don't check for LDAP_DIRSEP since URLs should contain '/' */
00051               if ( urlstr[0] == '/' && urlstr[1] == '/' ) {
00052                      urlstr += 2;
00053                      /* path must be absolute if authority is present */
00054                      if ( urlstr[0] != '/' )
00055                             return NULL;
00056               }
00057 
00058               p = ber_strdup( urlstr );
00059 
00060               /* But we should convert to LDAP_DIRSEP before use */
00061               if ( LDAP_DIRSEP[0] != '/' ) {
00062                      char *s = p;
00063                      while (( s = strchr( s, '/' )))
00064                             *s++ = LDAP_DIRSEP[0];
00065               }
00066 
00067               ldap_pvt_hex_unescape( p );
00068 
00069               url = fopen( p, "rb" );
00070 
00071               ber_memfree( p );
00072        } else {
00073 #ifdef HAVE_FETCH
00074               url = fetchGetURL( (char*) urlstr, "" );
00075 #else
00076               url = NULL;
00077 #endif
00078        }
00079        return url;
00080 }
00081 
00082 int
00083 ldif_fetch_url(
00084     LDAP_CONST char  *urlstr,
00085     char      **valuep,
00086     ber_len_t *vlenp )
00087 {
00088        FILE *url;
00089        char buffer[1024];
00090        char *p = NULL;
00091        size_t total;
00092        size_t bytes;
00093 
00094        *valuep = NULL;
00095        *vlenp = 0;
00096 
00097        url = ldif_open_url( urlstr );
00098 
00099        if( url == NULL ) {
00100               return -1;
00101        }
00102 
00103        total = 0;
00104 
00105        while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
00106               char *newp = ber_memrealloc( p, total + bytes + 1 );
00107               if( newp == NULL ) {
00108                      ber_memfree( p );
00109                      fclose( url );
00110                      return -1;
00111               }
00112               p = newp;
00113               AC_MEMCPY( &p[total], buffer, bytes );
00114               total += bytes;
00115        }
00116 
00117        fclose( url );
00118 
00119        if( total == 0 ) {
00120               char *newp = ber_memrealloc( p, 1 );
00121               if( newp == NULL ) {
00122                      ber_memfree( p );
00123                      return -1;
00124               }
00125               p = newp;
00126        }
00127 
00128        p[total] = '\0';
00129        *valuep = p;
00130        *vlenp = total;
00131 
00132        return 0;
00133 }