1 /* ldapmodify.c - generic program to modify or add entries using LDAP */
10 #include <ac/unistd.h>
14 #ifdef HAVE_SYS_FILE_H
25 #include "ldapconfig.h"
28 static char *binddn = LDAPMODIFY_BINDDN;
29 static char *passwd = LDAPMODIFY_BIND_CRED;
30 static char *ldaphost = LDAPHOST;
31 static int ldapport = LDAP_PORT;
32 static int new, replace, not, verbose, contoper, force, valsfromfiles;
36 extern int ldap_debug, lber_debug;
37 #endif /* LDAP_DEBUG */
39 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
42 #define LDAPMOD_MAXLINE 4096
44 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
45 #define T_REPLICA_STR "replica"
47 #define T_CHANGETYPESTR "changetype"
48 #define T_ADDCTSTR "add"
49 #define T_MODIFYCTSTR "modify"
50 #define T_DELETECTSTR "delete"
51 #define T_MODRDNCTSTR "modrdn"
52 #define T_MODOPADDSTR "add"
53 #define T_MODOPREPLACESTR "replace"
54 #define T_MODOPDELETESTR "delete"
55 #define T_MODSEPSTR "-"
56 #define T_NEWRDNSTR "newrdn"
57 #define T_DELETEOLDRDNSTR "deleteoldrdn"
60 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
61 static int process_ldif_rec LDAP_P(( char *rbuf ));
62 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
63 char *value, int vlen ));
64 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
65 static int dodelete LDAP_P(( char *dn ));
66 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
67 static void freepmods LDAP_P(( LDAPMod **pmods ));
68 static int fromfile LDAP_P(( char *path, struct berval *bv ));
69 static char *read_one_record LDAP_P(( FILE *fp ));
73 main( int argc, char **argv )
75 char *infile, *rbuf, *start, *p, *q;
77 int rc, i, kerberos, use_ldif, authmethod;
78 char *usage = "usage: %s [-abcknrvF] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n";
83 if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
88 new = ( strcmp( prog, "ldapadd" ) == 0 );
91 kerberos = not = verbose = valsfromfiles = 0;
93 while (( i = getopt( argc, argv, "FabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
98 case 'b': /* read values from files (for binary attributes) */
101 case 'c': /* continuous operation */
104 case 'r': /* default is to replace rather than add values */
107 case 'k': /* kerberos bind */
110 case 'K': /* kerberos bind, part 1 only */
113 case 'F': /* force all changes records to be used */
116 case 'h': /* ldap host */
117 ldaphost = strdup( optarg );
119 case 'D': /* bind DN */
120 binddn = strdup( optarg );
122 case 'w': /* password */
123 passwd = strdup( optarg );
127 ldap_debug = lber_debug = atoi( optarg ); /* */
128 #else /* LDAP_DEBUG */
129 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
131 #endif /* LDAP_DEBUG */
133 case 'f': /* read from file */
134 infile = strdup( optarg );
137 ldapport = atoi( optarg );
139 case 'n': /* print adds, don't actually do them */
142 case 'v': /* verbose mode */
146 fprintf( stderr, usage, prog );
151 if ( argc - optind != 0 ) {
152 fprintf( stderr, usage, prog );
156 if ( infile != NULL ) {
157 if (( fp = fopen( infile, "r" )) == NULL ) {
167 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
168 perror( "ldap_open" );
172 /* this seems prudent */
173 ldap_set_option( ld, LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
176 authmethod = LDAP_AUTH_SIMPLE;
177 } else if ( kerberos == 1 ) {
178 authmethod = LDAP_AUTH_KRBV41;
180 authmethod = LDAP_AUTH_KRBV4;
182 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
183 ldap_perror( ld, "ldap_bind" );
190 while (( rc == 0 || contoper ) &&
191 ( rbuf = read_one_record( fp )) != NULL ) {
193 * we assume record is ldif/slapd.replog if the first line
194 * has a colon that appears to the left of any equal signs, OR
195 * if the first line consists entirely of digits (an entry id)
197 use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
198 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
199 (( q = strchr( rbuf, '=' )) == NULL || p < q );
203 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
204 for ( p = rbuf; p < q; ++p ) {
205 if ( !isdigit( *p )) {
216 rc = process_ldif_rec( start );
218 rc = process_ldapmod_rec( start );
233 process_ldif_rec( char *rbuf )
235 char *line, *dn, *type, *value, *newrdn, *p;
236 int rc, linenum, vlen, modop, replicaport;
237 int expect_modop, expect_sep, expect_ct, expect_newrdn;
238 int expect_deleteoldrdn, deleteoldrdn;
239 int saw_replica, use_record, new_entry, delete_entry, got_all;
244 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
245 expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
252 while ( rc == 0 && ( line = str_getline( &rbuf )) != NULL ) {
254 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
260 if ( str_parse_line( line, &type, &value, &vlen ) < 0 ) {
261 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
262 prog, linenum, dn == NULL ? "" : dn );
263 rc = LDAP_PARAM_ERROR;
268 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
270 if (( p = strchr( value, ':' )) == NULL ) {
271 replicaport = LDAP_PORT;
274 replicaport = atoi( p );
276 if ( strcasecmp( value, ldaphost ) == 0 &&
277 replicaport == ldapport ) {
280 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
281 if (( dn = strdup( value )) == NULL ) {
287 continue; /* skip all lines until we see "dn:" */
292 if ( !use_record && saw_replica ) {
293 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
299 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
300 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
303 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
305 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
307 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
308 got_all = delete_entry = 1;
311 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
312 prog, T_CHANGETYPESTR, value, linenum, dn );
313 rc = LDAP_PARAM_ERROR;
316 } else if ( new ) { /* missing changetype => add */
318 modop = LDAP_MOD_ADD;
320 expect_modop = 1; /* missing changetype => modify */
324 if ( expect_modop ) {
327 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
328 modop = LDAP_MOD_ADD;
330 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
331 modop = LDAP_MOD_REPLACE;
333 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
334 modop = LDAP_MOD_DELETE;
335 addmodifyop( &pmods, modop, value, NULL, 0 );
337 } else { /* no modify op: use default */
338 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
342 if ( expect_newrdn ) {
343 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
344 if (( newrdn = strdup( value )) == NULL ) {
348 expect_deleteoldrdn = 1;
351 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
352 prog, T_NEWRDNSTR, type, linenum, dn );
353 rc = LDAP_PARAM_ERROR;
355 } else if ( expect_deleteoldrdn ) {
356 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
357 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
360 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
361 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
362 rc = LDAP_PARAM_ERROR;
364 } else if ( got_all ) {
366 "%s: extra lines at end (line %d of entry %s)\n",
368 rc = LDAP_PARAM_ERROR;
370 addmodifyop( &pmods, modop, type, value, vlen );
375 if ( delete_entry ) {
377 } else if ( newrdn != NULL ) {
378 rc = domodrdn( dn, newrdn, deleteoldrdn );
380 rc = domodify( dn, pmods, new_entry );
383 if ( rc == LDAP_SUCCESS ) {
391 if ( newrdn != NULL ) {
394 if ( pmods != NULL ) {
403 process_ldapmod_rec( char *rbuf )
405 char *line, *dn, *p, *q, *attr, *value;
406 int rc, linenum, modop;
415 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
417 if (( p = strchr( rbuf, '\n' )) == NULL ) {
420 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
429 if ( dn == NULL ) { /* first line contains DN */
430 if (( dn = strdup( line )) == NULL ) {
435 if (( p = strchr( line, '=' )) == NULL ) {
437 p = line + strlen( line );
443 for ( attr = line; *attr != '\0' && isspace( *attr ); ++attr ) {
444 ; /* skip attribute leading white space */
447 for ( q = p - 1; q > attr && isspace( *q ); --q ) {
448 *q = '\0'; /* remove attribute trailing white space */
451 if ( value != NULL ) {
452 while ( isspace( *value )) {
453 ++value; /* skip value leading white space */
455 for ( q = value + strlen( value ) - 1; q > value &&
456 isspace( *q ); --q ) {
457 *q = '\0'; /* remove value trailing white space */
459 if ( *value == '\0' ) {
465 if ( value == NULL && new ) {
466 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
467 prog, linenum, attr );
468 rc = LDAP_PARAM_ERROR;
472 modop = LDAP_MOD_DELETE;
476 modop = LDAP_MOD_ADD;
480 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
483 addmodifyop( &pmods, modop, attr, value,
484 ( value == NULL ) ? 0 : strlen( value ));
493 rc = LDAP_PARAM_ERROR;
494 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
499 if ( pmods != NULL ) {
511 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
518 modop |= LDAP_MOD_BVALUES;
521 if ( pmods != NULL ) {
522 for ( ; pmods[ i ] != NULL; ++i ) {
523 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
524 pmods[ i ]->mod_op == modop ) {
530 if ( pmods == NULL || pmods[ i ] == NULL ) {
531 if (( pmods = (LDAPMod **)safe_realloc( pmods, (i + 2) *
532 sizeof( LDAPMod * ))) == NULL ) {
533 perror( "safe_realloc" );
537 pmods[ i + 1 ] = NULL;
538 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
543 pmods[ i ]->mod_op = modop;
544 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
550 if ( value != NULL ) {
552 if ( pmods[ i ]->mod_bvalues != NULL ) {
553 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
557 if (( pmods[ i ]->mod_bvalues =
558 (struct berval **)safe_realloc( pmods[ i ]->mod_bvalues,
559 (j + 2) * sizeof( struct berval * ))) == NULL ) {
560 perror( "safe_realloc" );
563 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
564 if (( bvp = (struct berval *)malloc( sizeof( struct berval )))
569 pmods[ i ]->mod_bvalues[ j ] = bvp;
571 if ( valsfromfiles && *value == '/' ) { /* get value from file */
572 if ( fromfile( value, bvp ) < 0 ) {
577 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
581 SAFEMEMCPY( bvp->bv_val, value, vlen );
582 bvp->bv_val[ vlen ] = '\0';
589 domodify( char *dn, LDAPMod **pmods, int newentry )
591 int i, j, k, notascii, op;
594 if ( pmods == NULL ) {
595 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
597 return( LDAP_PARAM_ERROR );
601 for ( i = 0; pmods[ i ] != NULL; ++i ) {
602 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
603 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
604 "replace" : op == LDAP_MOD_ADD ?
605 "add" : "delete", pmods[ i ]->mod_type );
606 if ( pmods[ i ]->mod_bvalues != NULL ) {
607 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
608 bvp = pmods[ i ]->mod_bvalues[ j ];
610 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
611 if ( !isascii( bvp->bv_val[ k ] )) {
617 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
619 printf( "\t%s\n", bvp->bv_val );
627 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
629 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
634 i = ldap_add_s( ld, dn, pmods );
636 i = ldap_modify_s( ld, dn, pmods );
638 if ( i != LDAP_SUCCESS ) {
639 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
640 } else if ( verbose ) {
641 printf( "modify complete\n" );
658 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
660 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
661 ldap_perror( ld, "ldap_delete" );
662 } else if ( verbose ) {
663 printf( "delete complete" );
676 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
681 printf( "new RDN: %s (%skeep existing values)\n",
682 newrdn, deleteoldrdn ? "do not " : "" );
685 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
687 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
689 ldap_perror( ld, "ldap_modrdn" );
691 printf( "modrdn completed\n" );
705 freepmods( LDAPMod **pmods )
709 for ( i = 0; pmods[ i ] != NULL; ++i ) {
710 if ( pmods[ i ]->mod_bvalues != NULL ) {
711 ber_bvecfree( pmods[ i ]->mod_bvalues );
713 if ( pmods[ i ]->mod_type != NULL ) {
714 free( pmods[ i ]->mod_type );
723 fromfile( char *path, struct berval *bv )
729 if (( fp = fopen( path, "r" )) == NULL ) {
734 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
740 bv->bv_len = ftell( fp );
742 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
748 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
754 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
758 if ( (unsigned long) rlen != bv->bv_len ) {
764 return( bv->bv_len );
769 read_one_record( FILE *fp )
772 char *buf, line[ LDAPMOD_MAXLINE ];
778 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
779 (( len = strlen( line )) > 1 )) {
780 if ( lcur + len + 1 > lmax ) {
781 lmax = LDAPMOD_MAXLINE
782 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
783 if (( buf = (char *)safe_realloc( buf, lmax )) == NULL ) {
784 perror( "safe_realloc" );
788 strcpy( buf + lcur, line );