1 /* ldapmodify.c - generic program to modify or add entries using LDAP */
10 #include <sys/types.h>
15 #include <ac/unistd.h>
21 #include "ldapconfig.h"
24 static char *binddn = LDAPMODIFY_BINDDN;
25 static char *passwd = LDAPMODIFY_BIND_CRED;
26 static char *ldaphost = LDAPHOST;
27 static int ldapport = LDAP_PORT;
28 static int new, replace, not, verbose, contoper, force, valsfromfiles;
32 extern int ldap_debug, lber_debug;
33 #endif /* LDAP_DEBUG */
35 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
38 #define LDAPMOD_MAXLINE 4096
40 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
41 #define T_REPLICA_STR "replica"
43 #define T_CHANGETYPESTR "changetype"
44 #define T_ADDCTSTR "add"
45 #define T_MODIFYCTSTR "modify"
46 #define T_DELETECTSTR "delete"
47 #define T_MODRDNCTSTR "modrdn"
48 #define T_MODOPADDSTR "add"
49 #define T_MODOPREPLACESTR "replace"
50 #define T_MODOPDELETESTR "delete"
51 #define T_MODSEPSTR "-"
52 #define T_NEWRDNSTR "newrdn"
53 #define T_DELETEOLDRDNSTR "deleteoldrdn"
56 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
57 static int process_ldif_rec LDAP_P(( char *rbuf ));
58 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
59 char *value, int vlen ));
60 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
61 static int dodelete LDAP_P(( char *dn ));
62 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
63 static void freepmods LDAP_P(( LDAPMod **pmods ));
64 static int fromfile LDAP_P(( char *path, struct berval *bv ));
65 static char *read_one_record LDAP_P(( FILE *fp ));
72 char *infile, *rbuf, *start, *p, *q;
74 int rc, i, kerberos, use_ldif, authmethod;
75 char *usage = "usage: %s [-abcknrvF] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n";
80 if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
85 new = ( strcmp( prog, "ldapadd" ) == 0 );
88 kerberos = not = verbose = valsfromfiles = 0;
90 while (( i = getopt( argc, argv, "FabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
95 case 'b': /* read values from files (for binary attributes) */
98 case 'c': /* continuous operation */
101 case 'r': /* default is to replace rather than add values */
104 case 'k': /* kerberos bind */
107 case 'K': /* kerberos bind, part 1 only */
110 case 'F': /* force all changes records to be used */
113 case 'h': /* ldap host */
114 ldaphost = strdup( optarg );
116 case 'D': /* bind DN */
117 binddn = strdup( optarg );
119 case 'w': /* password */
120 passwd = strdup( optarg );
124 ldap_debug = lber_debug = atoi( optarg ); /* */
125 #else /* LDAP_DEBUG */
126 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
128 #endif /* LDAP_DEBUG */
130 case 'f': /* read from file */
131 infile = strdup( optarg );
134 ldapport = atoi( optarg );
136 case 'n': /* print adds, don't actually do them */
139 case 'v': /* verbose mode */
143 fprintf( stderr, usage, prog );
148 if ( argc - optind != 0 ) {
149 fprintf( stderr, usage, prog );
153 if ( infile != NULL ) {
154 if (( fp = fopen( infile, "r" )) == NULL ) {
164 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
165 perror( "ldap_open" );
169 ld->ld_deref = LDAP_DEREF_NEVER; /* this seems prudent */
172 authmethod = LDAP_AUTH_SIMPLE;
173 } else if ( kerberos == 1 ) {
174 authmethod = LDAP_AUTH_KRBV41;
176 authmethod = LDAP_AUTH_KRBV4;
178 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
179 ldap_perror( ld, "ldap_bind" );
186 while (( rc == 0 || contoper ) &&
187 ( rbuf = read_one_record( fp )) != NULL ) {
189 * we assume record is ldif/slapd.replog if the first line
190 * has a colon that appears to the left of any equal signs, OR
191 * if the first line consists entirely of digits (an entry id)
193 use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
194 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
195 (( q = strchr( rbuf, '=' )) == NULL || p < q );
199 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
200 for ( p = rbuf; p < q; ++p ) {
201 if ( !isdigit( *p )) {
212 rc = process_ldif_rec( start );
214 rc = process_ldapmod_rec( start );
229 process_ldif_rec( char *rbuf )
231 char *line, *dn, *type, *value, *newrdn, *p;
232 int rc, linenum, vlen, modop, replicaport;
233 int expect_modop, expect_sep, expect_ct, expect_newrdn;
234 int expect_deleteoldrdn, deleteoldrdn;
235 int saw_replica, use_record, new_entry, delete_entry, got_all;
240 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
241 expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
248 while ( rc == 0 && ( line = str_getline( &rbuf )) != NULL ) {
250 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
256 if ( str_parse_line( line, &type, &value, &vlen ) < 0 ) {
257 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
258 prog, linenum, dn == NULL ? "" : dn );
259 rc = LDAP_PARAM_ERROR;
264 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
266 if (( p = strchr( value, ':' )) == NULL ) {
267 replicaport = LDAP_PORT;
270 replicaport = atoi( p );
272 if ( strcasecmp( value, ldaphost ) == 0 &&
273 replicaport == ldapport ) {
276 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
277 if (( dn = strdup( value )) == NULL ) {
283 continue; /* skip all lines until we see "dn:" */
288 if ( !use_record && saw_replica ) {
289 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
295 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
296 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
299 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
301 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
303 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
304 got_all = delete_entry = 1;
307 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
308 prog, T_CHANGETYPESTR, value, linenum, dn );
309 rc = LDAP_PARAM_ERROR;
312 } else if ( new ) { /* missing changetype => add */
314 modop = LDAP_MOD_ADD;
316 expect_modop = 1; /* missing changetype => modify */
320 if ( expect_modop ) {
323 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
324 modop = LDAP_MOD_ADD;
326 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
327 modop = LDAP_MOD_REPLACE;
329 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
330 modop = LDAP_MOD_DELETE;
331 addmodifyop( &pmods, modop, value, NULL, 0 );
333 } else { /* no modify op: use default */
334 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
338 if ( expect_newrdn ) {
339 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
340 if (( newrdn = strdup( value )) == NULL ) {
344 expect_deleteoldrdn = 1;
347 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
348 prog, T_NEWRDNSTR, type, linenum, dn );
349 rc = LDAP_PARAM_ERROR;
351 } else if ( expect_deleteoldrdn ) {
352 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
353 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
356 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
357 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
358 rc = LDAP_PARAM_ERROR;
360 } else if ( got_all ) {
362 "%s: extra lines at end (line %d of entry %s)\n",
364 rc = LDAP_PARAM_ERROR;
366 addmodifyop( &pmods, modop, type, value, vlen );
371 if ( delete_entry ) {
373 } else if ( newrdn != NULL ) {
374 rc = domodrdn( dn, newrdn, deleteoldrdn );
376 rc = domodify( dn, pmods, new_entry );
379 if ( rc == LDAP_SUCCESS ) {
387 if ( newrdn != NULL ) {
390 if ( pmods != NULL ) {
399 process_ldapmod_rec( char *rbuf )
401 char *line, *dn, *p, *q, *attr, *value;
402 int rc, linenum, modop;
411 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
413 if (( p = strchr( rbuf, '\n' )) == NULL ) {
416 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
425 if ( dn == NULL ) { /* first line contains DN */
426 if (( dn = strdup( line )) == NULL ) {
431 if (( p = strchr( line, '=' )) == NULL ) {
433 p = line + strlen( line );
439 for ( attr = line; *attr != '\0' && isspace( *attr ); ++attr ) {
440 ; /* skip attribute leading white space */
443 for ( q = p - 1; q > attr && isspace( *q ); --q ) {
444 *q = '\0'; /* remove attribute trailing white space */
447 if ( value != NULL ) {
448 while ( isspace( *value )) {
449 ++value; /* skip value leading white space */
451 for ( q = value + strlen( value ) - 1; q > value &&
452 isspace( *q ); --q ) {
453 *q = '\0'; /* remove value trailing white space */
455 if ( *value == '\0' ) {
461 if ( value == NULL && new ) {
462 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
463 prog, linenum, attr );
464 rc = LDAP_PARAM_ERROR;
468 modop = LDAP_MOD_DELETE;
472 modop = LDAP_MOD_ADD;
476 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
479 addmodifyop( &pmods, modop, attr, value,
480 ( value == NULL ) ? 0 : strlen( value ));
489 rc = LDAP_PARAM_ERROR;
490 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
495 if ( pmods != NULL ) {
507 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
514 modop |= LDAP_MOD_BVALUES;
517 if ( pmods != NULL ) {
518 for ( ; pmods[ i ] != NULL; ++i ) {
519 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
520 pmods[ i ]->mod_op == modop ) {
526 if ( pmods == NULL || pmods[ i ] == NULL ) {
527 if (( pmods = (LDAPMod **)safe_realloc( pmods, (i + 2) *
528 sizeof( LDAPMod * ))) == NULL ) {
529 perror( "safe_realloc" );
533 pmods[ i + 1 ] = NULL;
534 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
539 pmods[ i ]->mod_op = modop;
540 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
546 if ( value != NULL ) {
548 if ( pmods[ i ]->mod_bvalues != NULL ) {
549 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
553 if (( pmods[ i ]->mod_bvalues =
554 (struct berval **)safe_realloc( pmods[ i ]->mod_bvalues,
555 (j + 2) * sizeof( struct berval * ))) == NULL ) {
556 perror( "safe_realloc" );
559 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
560 if (( bvp = (struct berval *)malloc( sizeof( struct berval )))
565 pmods[ i ]->mod_bvalues[ j ] = bvp;
567 if ( valsfromfiles && *value == '/' ) { /* get value from file */
568 if ( fromfile( value, bvp ) < 0 ) {
573 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
577 SAFEMEMCPY( bvp->bv_val, value, vlen );
578 bvp->bv_val[ vlen ] = '\0';
585 domodify( char *dn, LDAPMod **pmods, int newentry )
587 int i, j, k, notascii, op;
590 if ( pmods == NULL ) {
591 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
593 return( LDAP_PARAM_ERROR );
597 for ( i = 0; pmods[ i ] != NULL; ++i ) {
598 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
599 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
600 "replace" : op == LDAP_MOD_ADD ?
601 "add" : "delete", pmods[ i ]->mod_type );
602 if ( pmods[ i ]->mod_bvalues != NULL ) {
603 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
604 bvp = pmods[ i ]->mod_bvalues[ j ];
606 for ( k = 0; k < bvp->bv_len; ++k ) {
607 if ( !isascii( bvp->bv_val[ k ] )) {
613 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
615 printf( "\t%s\n", bvp->bv_val );
623 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
625 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
630 i = ldap_add_s( ld, dn, pmods );
632 i = ldap_modify_s( ld, dn, pmods );
634 if ( i != LDAP_SUCCESS ) {
635 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
636 } else if ( verbose ) {
637 printf( "modify complete\n" );
654 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
656 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
657 ldap_perror( ld, "ldap_delete" );
658 } else if ( verbose ) {
659 printf( "delete complete" );
672 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
677 printf( "new RDN: %s (%skeep existing values)\n",
678 newrdn, deleteoldrdn ? "do not " : "" );
681 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
683 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
685 ldap_perror( ld, "ldap_modrdn" );
687 printf( "modrdn completed\n" );
701 freepmods( LDAPMod **pmods )
705 for ( i = 0; pmods[ i ] != NULL; ++i ) {
706 if ( pmods[ i ]->mod_bvalues != NULL ) {
707 ber_bvecfree( pmods[ i ]->mod_bvalues );
709 if ( pmods[ i ]->mod_type != NULL ) {
710 free( pmods[ i ]->mod_type );
719 fromfile( char *path, struct berval *bv )
725 if (( fp = fopen( path, "r" )) == NULL ) {
730 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
736 bv->bv_len = ftell( fp );
738 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
744 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
750 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
754 if ( rlen != bv->bv_len ) {
760 return( bv->bv_len );
765 read_one_record( FILE *fp )
768 char *buf, line[ LDAPMOD_MAXLINE ];
774 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
775 (( len = strlen( line )) > 1 )) {
776 if ( lcur + len + 1 > lmax ) {
777 lmax = LDAPMOD_MAXLINE
778 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
779 if (( buf = (char *)safe_realloc( buf, lmax )) == NULL ) {
780 perror( "safe_realloc" );
784 strcpy( buf + lcur, line );