struct berval *value,
int *freeval ));
+LDAP_LDIF_F( FILE * )
+ldif_open_url LDAP_P(( LDAP_CONST char *urlstr ));
+
LDAP_LDIF_F( int )
ldif_fetch_url LDAP_P((
LDAP_CONST char *line,
LDAP_LDIF_F( int )
ldif_countlines LDAP_P(( LDAP_CONST char *line ));
+/* ldif_ropen, rclose, read_record - just for reading LDIF files,
+ * no special open/close needed to write LDIF files.
+ */
+typedef struct LDIFFP {
+ FILE *fp;
+ struct LDIFFP *prev;
+} LDIFFP;
+
+LDAP_LDIF_F( LDIFFP * )
+ldif_open LDAP_P(( char *file, char *mode ));
+
+LDAP_LDIF_F( void )
+ldif_close LDAP_P(( LDIFFP * ));
+
LDAP_LDIF_F( int )
ldif_read_record LDAP_P((
- FILE *fp,
+ LDIFFP *fp,
int *lineno,
char **bufp,
int *buflen ));
#include "ldap_config.h"
#include "ldif.h"
-int
-ldif_fetch_url(
- LDAP_CONST char *urlstr,
- char **valuep,
- ber_len_t *vlenp
+FILE *
+ldif_open_url(
+ LDAP_CONST char *urlstr
)
{
FILE *url;
- char buffer[1024];
char *p = NULL;
- size_t total;
- size_t bytes;
-
- *valuep = NULL;
- *vlenp = 0;
-
#ifdef HAVE_FETCH
url = fetchGetURL( (char*) urlstr, "" );
if( strncasecmp( "file://", urlstr, sizeof("file://")-1 ) == 0 ) {
p = strchr( &urlstr[sizeof("file://")-1], '/' );
if( p == NULL ) {
- return -1;
+ return NULL;
}
/* we don't check for LDAP_DIRSEP since URLs should contain '/' */
url = fopen( p, "rb" );
+ ber_memfree( p );
} else {
- return -1;
+ return NULL;
}
#endif
+ return url;
+}
+
+int
+ldif_fetch_url(
+ LDAP_CONST char *urlstr,
+ char **valuep,
+ ber_len_t *vlenp
+)
+{
+ FILE *url;
+ char buffer[1024];
+ char *p = NULL;
+ size_t total;
+ size_t bytes;
+
+ *valuep = NULL;
+ *vlenp = 0;
+
+ url = ldif_open_url( urlstr );
if( url == NULL ) {
return -1;
return 1;
}
+LDIFFP *
+ldif_open(
+ char *file,
+ char *mode
+)
+{
+ FILE *fp = fopen( file, mode );
+ LDIFFP *lfp = NULL;
+
+ if ( fp ) {
+ lfp = ber_memalloc( sizeof( LDIFFP ));
+ lfp->fp = fp;
+ lfp->prev = NULL;
+ }
+ return lfp;
+}
+
+void
+ldif_close(
+ LDIFFP *lfp
+)
+{
+ LDIFFP *prev;
+
+ while ( lfp ) {
+ fclose( lfp->fp );
+ prev = lfp->prev;
+ ber_memfree( lfp );
+ lfp = prev;
+ }
+}
+
/*
- * slap_read_ldif - read an ldif record. Return 1 for success, 0 for EOF.
+ * ldif_read_record - read an ldif record. Return 1 for success, 0 for EOF.
*/
int
ldif_read_record(
- FILE *fp,
+ LDIFFP *lfp,
int *lno, /* ptr to line number counter */
char **bufp, /* ptr to malloced output buffer */
int *buflenp ) /* ptr to length of *bufp */
line = linebuf;
linesize = sizeof( linebuf );
- for ( stop = feof( fp ); !stop; last_ch = line[len-1] ) {
- if ( fgets( line, linesize, fp ) == NULL ) {
+ for ( stop = 0; !stop; last_ch = line[len-1] ) {
+ /* If we're at the end of this file, see if we should pop
+ * back to a previous file. (return from an include)
+ */
+ while ( feof( lfp->fp )) {
+ if ( lfp->prev ) {
+ LDIFFP *tmp = lfp->prev;
+ fclose( lfp->fp );
+ *lfp = *tmp;
+ ber_memfree( tmp );
+ } else {
+ stop = 1;
+ break;
+ }
+ }
+ if ( stop )
+ break;
+
+ if ( fgets( line, linesize, lfp->fp ) == NULL ) {
stop = 1;
/* Add \n in case the file does not end with newline */
line = "\n";
/* skip index */
continue;
}
+ if ( !strncasecmp( line, "include:",
+ STRLENOF("include:"))) {
+ FILE *fp2;
+ char *ptr;
+ found_entry = 0;
+
+ if ( line[len-1] == '\n' ) {
+ len--;
+ line[len] = '\0';
+ }
+ if ( line[len-1] == '\r' ) {
+ len--;
+ line[len] = '\0';
+ }
+
+ ptr = line + STRLENOF("include:");
+ while (isspace(*ptr)) ptr++;
+ fp2 = ldif_open_url( ptr );
+ if ( fp2 ) {
+ LDIFFP *lnew = ber_memalloc( sizeof( LDIFFP ));
+ lnew->prev = lfp->prev;
+ lnew->fp = lfp->fp;
+ lfp->prev = lnew;
+ lfp->fp = fp2;
+ continue;
+ } else {
+ /* We failed to open the file, this should
+ * be reported as an error somehow.
+ */
+ break;
+ }
+ }
}
}
}
const char *fname )
{
backsql_info *bi = (backsql_info *)be->be_private;
- FILE *fp;
+ LDIFFP *fp;
int rc = 0, lineno = 0, lmax = 0;
char *buf = NULL;
assert( fname );
- fp = fopen( fname, "r" );
+ fp = ldif_open( fname, "r" );
if ( fp == NULL ) {
Debug( LDAP_DEBUG_ANY,
"could not open back-sql baseObject "
if ( bi->sql_baseObject == NULL ) {
Debug( LDAP_DEBUG_ANY,
"read_baseObject_file: SLAP_CALLOC failed", 0, 0, 0 );
- fclose( fp );
+ ldif_close( fp );
return LDAP_NO_MEMORY;
}
bi->sql_baseObject->e_name = be->be_suffix[0];
ch_free( buf );
- fclose( fp );
+ ldif_close( fp );
Debug( LDAP_DEBUG_CONFIG, "back-sql baseObject file \"%s\" read.\n",
fname, 0, 0 );
*/
int read_root_dse_file( const char *fname )
{
- FILE *fp;
+ struct LDIFFP *fp;
int rc = 0, lineno = 0, lmax = 0;
char *buf = NULL;
- if ( (fp = fopen( fname, "r" )) == NULL ) {
+ if ( (fp = ldif_open( fname, "r" )) == NULL ) {
Debug( LDAP_DEBUG_ANY,
"could not open rootdse attr file \"%s\" - absolute path?\n",
fname, 0, 0 );
if( usr_attr == NULL ) {
Debug( LDAP_DEBUG_ANY,
"read_root_dse_file: SLAP_CALLOC failed", 0, 0, 0 );
- fclose( fp );
+ ldif_close( fp );
return LDAP_OTHER;
}
usr_attr->e_attrs = NULL;
ch_free( buf );
- fclose( fp );
+ ldif_close( fp );
Debug(LDAP_DEBUG_CONFIG, "rootDSE file %s read.\n", fname, 0, 0);
return rc;
#include <ac/string.h>
#include "slapcommon.h"
+#include "ldif.h"
int
slapcat( int argc, char **argv )
break;
}
- fputs( data, ldiffp );
- fputs( "\n", ldiffp );
+ fputs( data, ldiffp->fp );
+ fputs( "\n", ldiffp->fp );
}
be->be_entry_close( be );
#include "slapcommon.h"
#include "lutil.h"
+#include "ldif.h"
tool_vars tool_globals;
static FILE *leakfile;
#endif
+static LDIFFP dummy;
+
static void
usage( int tool, const char *progname )
{
ldap_syslog = 0;
if ( ldiffile == NULL ) {
- ldiffp = tool == SLAPCAT ? stdout : stdin;
+ dummy.fp = tool == SLAPCAT ? stdout : stdin;
+ ldiffp = &dummy;
- } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
+ } else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
== NULL )
{
perror( ldiffile );
int tv_dryrun;
Filter *tv_filter;
struct berval tv_sub_ndn;
- FILE *tv_ldiffp;
+ struct LDIFFP *tv_ldiffp;
struct berval tv_baseDN;
struct berval tv_authcDN;
struct berval tv_authzDN;