Home | History | Annotate | Line # | Download | only in progs
      1  1.3  christos /*	$NetBSD: slapd-addel.c,v 1.4 2025/09/05 21:16:33 christos Exp $	*/
      2  1.2  christos 
      3  1.2  christos /* $OpenLDAP$ */
      4  1.1     lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      5  1.1     lukem  *
      6  1.4  christos  * Copyright 1999-2024 The OpenLDAP Foundation.
      7  1.1     lukem  * All rights reserved.
      8  1.1     lukem  *
      9  1.1     lukem  * Redistribution and use in source and binary forms, with or without
     10  1.1     lukem  * modification, are permitted only as authorized by the OpenLDAP
     11  1.1     lukem  * Public License.
     12  1.1     lukem  *
     13  1.1     lukem  * A copy of this license is available in file LICENSE in the
     14  1.1     lukem  * top-level directory of the distribution or, alternatively, at
     15  1.1     lukem  * <http://www.OpenLDAP.org/license.html>.
     16  1.1     lukem  */
     17  1.1     lukem /* ACKNOWLEDGEMENTS:
     18  1.1     lukem  * This work was initially developed by Kurt Spanier for inclusion
     19  1.1     lukem  * in OpenLDAP Software.
     20  1.1     lukem  */
     21  1.1     lukem 
     22  1.2  christos #include <sys/cdefs.h>
     23  1.3  christos __RCSID("$NetBSD: slapd-addel.c,v 1.4 2025/09/05 21:16:33 christos Exp $");
     24  1.2  christos 
     25  1.1     lukem #include "portable.h"
     26  1.1     lukem 
     27  1.1     lukem #include <stdio.h>
     28  1.1     lukem 
     29  1.1     lukem #include "ac/stdlib.h"
     30  1.1     lukem 
     31  1.1     lukem #include "ac/ctype.h"
     32  1.1     lukem #include "ac/param.h"
     33  1.1     lukem #include "ac/socket.h"
     34  1.1     lukem #include "ac/string.h"
     35  1.1     lukem #include "ac/unistd.h"
     36  1.1     lukem #include "ac/wait.h"
     37  1.1     lukem 
     38  1.1     lukem #include "ldap.h"
     39  1.1     lukem #include "lutil.h"
     40  1.3  christos #include "ldif.h"
     41  1.1     lukem 
     42  1.1     lukem #include "slapd-common.h"
     43  1.1     lukem 
     44  1.3  christos static LDIFRecord *
     45  1.3  christos get_add_entry( char *filename );
     46  1.1     lukem 
     47  1.1     lukem static void
     48  1.3  christos do_addel( struct tester_conn_args *config,
     49  1.3  christos 	LDIFRecord *record, int friendly );
     50  1.1     lukem 
     51  1.1     lukem static void
     52  1.3  christos usage( char *name, char opt )
     53  1.1     lukem {
     54  1.3  christos 	if ( opt ) {
     55  1.3  christos 		fprintf( stderr, "%s: unable to handle option \'%c\'\n\n",
     56  1.3  christos 			name, opt );
     57  1.3  christos 	}
     58  1.3  christos 
     59  1.3  christos 	fprintf( stderr, "usage: %s " TESTER_COMMON_HELP
     60  1.1     lukem 		"-f <addfile> "
     61  1.3  christos 		"[-F]\n",
     62  1.3  christos 		name );
     63  1.1     lukem 	exit( EXIT_FAILURE );
     64  1.1     lukem }
     65  1.1     lukem 
     66  1.1     lukem int
     67  1.1     lukem main( int argc, char **argv )
     68  1.1     lukem {
     69  1.1     lukem 	int		i;
     70  1.3  christos 	char *filename = NULL, *buf = NULL;
     71  1.1     lukem 	int		friendly = 0;
     72  1.3  christos 	struct LDIFFP *fp;
     73  1.4  christos 	LDIFRecord	record = {0};
     74  1.3  christos 	struct tester_conn_args	*config;
     75  1.4  christos 	struct berval bv = {0};
     76  1.3  christos 	unsigned long lineno = 0;
     77  1.1     lukem 
     78  1.3  christos 	config = tester_init( "slapd-addel", TESTER_ADDEL );
     79  1.1     lukem 
     80  1.3  christos 	while ( ( i = getopt( argc, argv, TESTER_COMMON_OPTS "Ff:" ) ) != EOF )
     81  1.1     lukem 	{
     82  1.1     lukem 		switch ( i ) {
     83  1.1     lukem 		case 'F':
     84  1.1     lukem 			friendly++;
     85  1.1     lukem 			break;
     86  1.1     lukem 
     87  1.1     lukem 		case 'i':
     88  1.1     lukem 			/* ignored (!) by now */
     89  1.1     lukem 			break;
     90  1.1     lukem 
     91  1.1     lukem 		case 'f':		/* file with entry search request */
     92  1.3  christos 			filename = optarg;
     93  1.1     lukem 			break;
     94  1.1     lukem 
     95  1.3  christos 		default:
     96  1.3  christos 			if ( tester_config_opt( config, i, optarg ) == LDAP_SUCCESS ) {
     97  1.3  christos 				break;
     98  1.1     lukem 			}
     99  1.3  christos 			usage( argv[0], i );
    100  1.1     lukem 			break;
    101  1.1     lukem 		}
    102  1.1     lukem 	}
    103  1.1     lukem 
    104  1.3  christos 	if ( filename == NULL )
    105  1.3  christos 		usage( argv[0], 0 );
    106  1.1     lukem 
    107  1.3  christos 	if ( (fp = ldif_open( filename, "r" )) == NULL ) {
    108  1.3  christos 		tester_perror( filename, "while reading ldif file" );
    109  1.3  christos 		exit( EXIT_FAILURE );
    110  1.3  christos 	}
    111  1.1     lukem 
    112  1.3  christos 	i = 0;
    113  1.3  christos 	if ( ldif_read_record( fp, &lineno, &buf, &i ) < 0 ) {
    114  1.3  christos 		tester_error( "ldif_read_record failed" );
    115  1.1     lukem 		exit( EXIT_FAILURE );
    116  1.3  christos 	}
    117  1.3  christos 	bv.bv_val = buf;
    118  1.3  christos 	bv.bv_len = i;
    119  1.1     lukem 
    120  1.3  christos 	if ( ldap_parse_ldif_record( &bv, lineno, &record, "slapd-addel",
    121  1.3  christos 			LDIF_DEFAULT_ADD | LDIF_ENTRIES_ONLY ) ) {
    122  1.3  christos 		tester_error( "ldif_read_record failed" );
    123  1.3  christos 		exit( EXIT_FAILURE );
    124  1.1     lukem 	}
    125  1.3  christos 	ldif_close( fp );
    126  1.1     lukem 
    127  1.3  christos 	if ( ( record.lr_op != LDAP_REQ_ADD ) || ( !record.lrop_mods ) ) {
    128  1.1     lukem 
    129  1.3  christos 		fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
    130  1.1     lukem 				argv[0], filename );
    131  1.1     lukem 		exit( EXIT_FAILURE );
    132  1.1     lukem 
    133  1.1     lukem 	}
    134  1.1     lukem 
    135  1.3  christos 	tester_config_finish( config );
    136  1.1     lukem 
    137  1.3  christos 	for ( i = 0; i < config->outerloops; i++ ) {
    138  1.3  christos 		do_addel( config, &record, friendly );
    139  1.1     lukem 	}
    140  1.1     lukem 
    141  1.3  christos 	free( buf );
    142  1.1     lukem 	exit( EXIT_SUCCESS );
    143  1.1     lukem }
    144  1.1     lukem 
    145  1.1     lukem 
    146  1.1     lukem static void
    147  1.1     lukem addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
    148  1.1     lukem {
    149  1.1     lukem     LDAPMod		**pmods;
    150  1.1     lukem     int			i, j;
    151  1.1     lukem     struct berval	*bvp;
    152  1.1     lukem 
    153  1.1     lukem     pmods = *pmodsp;
    154  1.1     lukem     modop |= LDAP_MOD_BVALUES;
    155  1.1     lukem 
    156  1.1     lukem     i = 0;
    157  1.1     lukem     if ( pmods != NULL ) {
    158  1.1     lukem 		for ( ; pmods[ i ] != NULL; ++i ) {
    159  1.1     lukem 	    	if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
    160  1.1     lukem 		    	pmods[ i ]->mod_op == modop ) {
    161  1.1     lukem 				break;
    162  1.1     lukem 	    	}
    163  1.1     lukem 		}
    164  1.1     lukem     }
    165  1.1     lukem 
    166  1.1     lukem     if ( pmods == NULL || pmods[ i ] == NULL ) {
    167  1.1     lukem 		if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
    168  1.1     lukem 			sizeof( LDAPMod * ))) == NULL ) {
    169  1.1     lukem 	    		tester_perror( "realloc", NULL );
    170  1.1     lukem 	    		exit( EXIT_FAILURE );
    171  1.1     lukem 		}
    172  1.1     lukem 		*pmodsp = pmods;
    173  1.1     lukem 		pmods[ i + 1 ] = NULL;
    174  1.1     lukem 		if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
    175  1.1     lukem 			== NULL ) {
    176  1.1     lukem 	    		tester_perror( "calloc", NULL );
    177  1.1     lukem 	    		exit( EXIT_FAILURE );
    178  1.1     lukem 		}
    179  1.1     lukem 		pmods[ i ]->mod_op = modop;
    180  1.1     lukem 		if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
    181  1.1     lukem 	    	tester_perror( "strdup", NULL );
    182  1.1     lukem 	    	exit( EXIT_FAILURE );
    183  1.1     lukem 		}
    184  1.1     lukem     }
    185  1.1     lukem 
    186  1.1     lukem     if ( value != NULL ) {
    187  1.1     lukem 		j = 0;
    188  1.1     lukem 		if ( pmods[ i ]->mod_bvalues != NULL ) {
    189  1.1     lukem 	    	for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
    190  1.1     lukem 				;
    191  1.1     lukem 	    	}
    192  1.1     lukem 		}
    193  1.1     lukem 		if (( pmods[ i ]->mod_bvalues =
    194  1.1     lukem 			(struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
    195  1.1     lukem 			(j + 2) * sizeof( struct berval * ))) == NULL ) {
    196  1.1     lukem 	    		tester_perror( "ber_memrealloc", NULL );
    197  1.1     lukem 	    		exit( EXIT_FAILURE );
    198  1.1     lukem 		}
    199  1.1     lukem 		pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
    200  1.1     lukem 		if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
    201  1.1     lukem 			== NULL ) {
    202  1.1     lukem 	    		tester_perror( "ber_memalloc", NULL );
    203  1.1     lukem 	    		exit( EXIT_FAILURE );
    204  1.1     lukem 		}
    205  1.1     lukem 		pmods[ i ]->mod_bvalues[ j ] = bvp;
    206  1.1     lukem 
    207  1.1     lukem 	    bvp->bv_len = vlen;
    208  1.1     lukem 	    if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
    209  1.1     lukem 			tester_perror( "malloc", NULL );
    210  1.1     lukem 			exit( EXIT_FAILURE );
    211  1.1     lukem 	    }
    212  1.1     lukem 	    AC_MEMCPY( bvp->bv_val, value, vlen );
    213  1.1     lukem 	    bvp->bv_val[ vlen ] = '\0';
    214  1.1     lukem     }
    215  1.1     lukem }
    216  1.1     lukem 
    217  1.1     lukem 
    218  1.1     lukem static void
    219  1.1     lukem do_addel(
    220  1.3  christos 	struct tester_conn_args *config,
    221  1.3  christos 	LDIFRecord *record,
    222  1.3  christos 	int friendly )
    223  1.1     lukem {
    224  1.1     lukem 	LDAP	*ld = NULL;
    225  1.3  christos 	int  	i = 0, do_retry = config->retries;
    226  1.1     lukem 	int	rc = LDAP_SUCCESS;
    227  1.1     lukem 
    228  1.1     lukem retry:;
    229  1.1     lukem 	if ( ld == NULL ) {
    230  1.3  christos 		tester_init_ld( &ld, config, 0 );
    231  1.1     lukem 	}
    232  1.1     lukem 
    233  1.3  christos 	if ( do_retry == config->retries ) {
    234  1.1     lukem 		fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
    235  1.3  christos 			(long) pid, config->loops, record->lr_dn.bv_val );
    236  1.1     lukem 	}
    237  1.1     lukem 
    238  1.3  christos 	for ( ; i < config->loops; i++ ) {
    239  1.1     lukem 
    240  1.1     lukem 		/* add the entry */
    241  1.3  christos 		rc = ldap_add_ext_s( ld, record->lr_dn.bv_val, record->lrop_mods, NULL, NULL );
    242  1.1     lukem 		if ( rc != LDAP_SUCCESS ) {
    243  1.1     lukem 			tester_ldap_error( ld, "ldap_add_ext_s", NULL );
    244  1.1     lukem 			switch ( rc ) {
    245  1.1     lukem 			case LDAP_ALREADY_EXISTS:
    246  1.1     lukem 				/* NOTE: this likely means
    247  1.1     lukem 				 * the delete failed
    248  1.1     lukem 				 * during the previous round... */
    249  1.1     lukem 				if ( !friendly ) {
    250  1.1     lukem 					goto done;
    251  1.1     lukem 				}
    252  1.1     lukem 				break;
    253  1.1     lukem 
    254  1.1     lukem 			case LDAP_BUSY:
    255  1.1     lukem 			case LDAP_UNAVAILABLE:
    256  1.1     lukem 				if ( do_retry > 0 ) {
    257  1.1     lukem 					do_retry--;
    258  1.1     lukem 					goto retry;
    259  1.1     lukem 				}
    260  1.1     lukem 				/* fall thru */
    261  1.1     lukem 
    262  1.1     lukem 			default:
    263  1.1     lukem 				goto done;
    264  1.1     lukem 			}
    265  1.1     lukem 		}
    266  1.1     lukem 
    267  1.1     lukem #if 0
    268  1.1     lukem 		/* wait a second for the add to really complete */
    269  1.1     lukem 		/* This masks some race conditions though. */
    270  1.1     lukem 		sleep( 1 );
    271  1.1     lukem #endif
    272  1.1     lukem 
    273  1.1     lukem 		/* now delete the entry again */
    274  1.3  christos 		rc = ldap_delete_ext_s( ld, record->lr_dn.bv_val, NULL, NULL );
    275  1.1     lukem 		if ( rc != LDAP_SUCCESS ) {
    276  1.1     lukem 			tester_ldap_error( ld, "ldap_delete_ext_s", NULL );
    277  1.1     lukem 			switch ( rc ) {
    278  1.1     lukem 			case LDAP_NO_SUCH_OBJECT:
    279  1.1     lukem 				/* NOTE: this likely means
    280  1.1     lukem 				 * the add failed
    281  1.1     lukem 				 * during the previous round... */
    282  1.1     lukem 				if ( !friendly ) {
    283  1.1     lukem 					goto done;
    284  1.1     lukem 				}
    285  1.1     lukem 				break;
    286  1.1     lukem 
    287  1.1     lukem 			case LDAP_BUSY:
    288  1.1     lukem 			case LDAP_UNAVAILABLE:
    289  1.1     lukem 				if ( do_retry > 0 ) {
    290  1.1     lukem 					do_retry--;
    291  1.1     lukem 					goto retry;
    292  1.1     lukem 				}
    293  1.1     lukem 				/* fall thru */
    294  1.1     lukem 
    295  1.1     lukem 			default:
    296  1.1     lukem 				goto done;
    297  1.1     lukem 			}
    298  1.1     lukem 		}
    299  1.1     lukem 	}
    300  1.1     lukem 
    301  1.1     lukem done:;
    302  1.2  christos 	fprintf( stderr, "  PID=%ld - Add/Delete done (%d).\n", (long) pid, rc );
    303  1.1     lukem 
    304  1.1     lukem 	ldap_unbind_ext( ld, NULL, NULL );
    305  1.1     lukem }
    306  1.1     lukem 
    307  1.1     lukem 
    308