]> git.ipfire.org Git - thirdparty/openldap.git/commitdiff
Use LDIF API in slapd-addel
authorOndřej Kuzník <ondra@openldap.org>
Wed, 31 Oct 2018 18:12:04 +0000 (18:12 +0000)
committerOndřej Kuzník <ondra@openldap.org>
Wed, 31 Oct 2018 18:14:52 +0000 (18:14 +0000)
tests/progs/slapd-addel.c

index fd1ecbf2d90d97ccf4ba72a1824e2e9152a17a30..2a8004a8598e9ee206bf3244985a92d4551896e8 100644 (file)
 
 #include "ldap.h"
 #include "lutil.h"
+#include "ldif.h"
 
 #include "slapd-common.h"
 
-static char *
-get_add_entry( char *filename, LDAPMod ***mods );
+static LDIFRecord *
+get_add_entry( char *filename );
 
 static void
 do_addel( struct tester_conn_args *config,
-       char *dn, LDAPMod **attrs, int friendly );
+       LDIFRecord *record, int friendly );
 
 static void
 usage( char *name, char opt )
@@ -61,11 +62,13 @@ int
 main( int argc, char **argv )
 {
        int             i;
-       char            *filename = NULL;
-       char            *entry = NULL;
+       char *filename = NULL, *buf = NULL;
        int             friendly = 0;
-       LDAPMod         **attrs = NULL;
+       struct LDIFFP *fp;
+       LDIFRecord      record = {};
        struct tester_conn_args *config;
+       struct berval bv = {};
+       unsigned long lineno = 0;
 
        config = tester_init( "slapd-addel", TESTER_ADDEL );
 
@@ -96,18 +99,29 @@ main( int argc, char **argv )
        if ( filename == NULL )
                usage( argv[0], 0 );
 
-       entry = get_add_entry( filename, &attrs );
-       if (( entry == NULL ) || ( *entry == '\0' )) {
+       if ( (fp = ldif_open( filename, "r" )) == NULL ) {
+               tester_perror( filename, "while reading ldif file" );
+               exit( EXIT_FAILURE );
+       }
 
-               fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
-                               argv[0], filename );
+       i = 0;
+       if ( ldif_read_record( fp, &lineno, &buf, &i ) < 0 ) {
+               tester_error( "ldif_read_record failed" );
                exit( EXIT_FAILURE );
+       }
+       bv.bv_val = buf;
+       bv.bv_len = i;
 
+       if ( ldap_parse_ldif_record( &bv, lineno, &record, "slapd-addel",
+                       LDIF_DEFAULT_ADD | LDIF_ENTRIES_ONLY ) ) {
+               tester_error( "ldif_read_record failed" );
+               exit( EXIT_FAILURE );
        }
+       ldif_close( fp );
 
-       if (( attrs == NULL ) || ( *attrs == '\0' )) {
+       if ( ( record.lr_op != LDAP_REQ_ADD ) || ( !record.lrop_mods ) ) {
 
-               fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
+               fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
                                argv[0], filename );
                exit( EXIT_FAILURE );
 
@@ -116,9 +130,10 @@ main( int argc, char **argv )
        tester_config_finish( config );
 
        for ( i = 0; i < config->outerloops; i++ ) {
-               do_addel( config, entry, attrs, friendly );
+               do_addel( config, &record, friendly );
        }
 
+       free( buf );
        exit( EXIT_SUCCESS );
 }
 
@@ -195,56 +210,10 @@ addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
 }
 
 
-static char *
-get_add_entry( char *filename, LDAPMod ***mods )
-{
-       FILE    *fp;
-       char    *entry = NULL;
-
-       if ( (fp = fopen( filename, "r" )) != NULL ) {
-               char  line[BUFSIZ];
-
-               if ( fgets( line, BUFSIZ, fp )) {
-                       char *nl;
-
-                       if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
-                               *nl = '\0';
-                       nl = line;
-                       if ( !strncasecmp( nl, "dn: ", 4 ))
-                               nl += 4;
-                       entry = strdup( nl );
-
-               }
-
-               while ( fgets( line, BUFSIZ, fp )) {
-                       char    *nl;
-                       char    *value;
-
-                       if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
-                               *nl = '\0';
-
-                       if ( *line == '\0' ) break;
-                       if ( !( value = strchr( line, ':' ))) break;
-
-                       *value++ = '\0'; 
-                       while ( *value && isspace( (unsigned char) *value ))
-                               value++;
-
-                       addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
-
-               }
-               fclose( fp );
-       }
-
-       return( entry );
-}
-
-
 static void
 do_addel(
        struct tester_conn_args *config,
-       char *entry,
-       LDAPMod **attrs,
+       LDIFRecord *record,
        int friendly )
 {
        LDAP    *ld = NULL;
@@ -258,13 +227,13 @@ retry:;
 
        if ( do_retry == config->retries ) {
                fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
-                       (long) pid, config->loops, entry );
+                       (long) pid, config->loops, record->lr_dn.bv_val );
        }
 
        for ( ; i < config->loops; i++ ) {
 
                /* add the entry */
-               rc = ldap_add_ext_s( ld, entry, attrs, NULL, NULL );
+               rc = ldap_add_ext_s( ld, record->lr_dn.bv_val, record->lrop_mods, NULL, NULL );
                if ( rc != LDAP_SUCCESS ) {
                        tester_ldap_error( ld, "ldap_add_ext_s", NULL );
                        switch ( rc ) {
@@ -297,7 +266,7 @@ retry:;
 #endif
 
                /* now delete the entry again */
-               rc = ldap_delete_ext_s( ld, entry, NULL, NULL );
+               rc = ldap_delete_ext_s( ld, record->lr_dn.bv_val, NULL, NULL );
                if ( rc != LDAP_SUCCESS ) {
                        tester_ldap_error( ld, "ldap_delete_ext_s", NULL );
                        switch ( rc ) {