1 /* ldapmodify.c - generic program to modify or add entries using LDAP */
18 #include "ldapconfig.h"
21 static char *binddn = LDAPMODIFY_BINDDN;
22 static char *passwd = LDAPMODIFY_BIND_CRED;
23 static char *ldaphost = LDAPHOST;
24 static int ldapport = LDAP_PORT;
25 static int new, replace, not, verbose, contoper, force, valsfromfiles;
29 extern int ldap_debug, lber_debug;
30 #endif /* LDAP_DEBUG */
32 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
35 #define LDAPMOD_MAXLINE 4096
37 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
38 #define T_REPLICA_STR "replica"
40 #define T_CHANGETYPESTR "changetype"
41 #define T_ADDCTSTR "add"
42 #define T_MODIFYCTSTR "modify"
43 #define T_DELETECTSTR "delete"
44 #define T_MODRDNCTSTR "modrdn"
45 #define T_MODOPADDSTR "add"
46 #define T_MODOPREPLACESTR "replace"
47 #define T_MODOPDELETESTR "delete"
48 #define T_MODSEPSTR "-"
49 #define T_NEWRDNSTR "newrdn"
50 #define T_DELETEOLDRDNSTR "deleteoldrdn"
54 static int process_ldapmod_rec( char *rbuf );
55 static int process_ldif_rec( char *rbuf );
56 static void addmodifyop( LDAPMod ***pmodsp, int modop, char *attr,
57 char *value, int vlen );
58 static int domodify( char *dn, LDAPMod **pmods, int newentry );
59 static int dodelete( char *dn );
60 static int domodrdn( char *dn, char *newrdn, int deleteoldrdn );
61 static void freepmods( LDAPMod **pmods );
62 static int fromfile( char *path, struct berval *bv );
63 static char *read_one_record( FILE *fp );
64 #else /* NEEDPROTOS */
65 static int process_ldapmod_rec();
66 static int process_ldif_rec();
67 static void addmodifyop();
68 static int domodify();
69 static int dodelete();
70 static int domodrdn();
71 static void freepmods();
72 static int fromfile();
73 static char *read_one_record();
74 #endif /* NEEDPROTOS */
81 char *infile, *rbuf, *start, *p, *q;
83 int rc, i, kerberos, use_ldif, authmethod;
84 char *usage = "usage: %s [-abcknrvF] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n";
89 if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
94 new = ( strcmp( prog, "ldapadd" ) == 0 );
97 kerberos = not = verbose = valsfromfiles = 0;
99 while (( i = getopt( argc, argv, "FabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
104 case 'b': /* read values from files (for binary attributes) */
107 case 'c': /* continuous operation */
110 case 'r': /* default is to replace rather than add values */
113 case 'k': /* kerberos bind */
116 case 'K': /* kerberos bind, part 1 only */
119 case 'F': /* force all changes records to be used */
122 case 'h': /* ldap host */
123 ldaphost = strdup( optarg );
125 case 'D': /* bind DN */
126 binddn = strdup( optarg );
128 case 'w': /* password */
129 passwd = strdup( optarg );
133 ldap_debug = lber_debug = atoi( optarg ); /* */
134 #else /* LDAP_DEBUG */
135 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
137 #endif /* LDAP_DEBUG */
139 case 'f': /* read from file */
140 infile = strdup( optarg );
143 ldapport = atoi( optarg );
145 case 'n': /* print adds, don't actually do them */
148 case 'v': /* verbose mode */
152 fprintf( stderr, usage, prog );
157 if ( argc - optind != 0 ) {
158 fprintf( stderr, usage, prog );
162 if ( infile != NULL ) {
163 if (( fp = fopen( infile, "r" )) == NULL ) {
173 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
174 perror( "ldap_open" );
178 ld->ld_deref = LDAP_DEREF_NEVER; /* this seems prudent */
181 authmethod = LDAP_AUTH_SIMPLE;
182 } else if ( kerberos == 1 ) {
183 authmethod = LDAP_AUTH_KRBV41;
185 authmethod = LDAP_AUTH_KRBV4;
187 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
188 ldap_perror( ld, "ldap_bind" );
195 while (( rc == 0 || contoper ) &&
196 ( rbuf = read_one_record( fp )) != NULL ) {
198 * we assume record is ldif/slapd.replog if the first line
199 * has a colon that appears to the left of any equal signs, OR
200 * if the first line consists entirely of digits (an entry id)
202 use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
203 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
204 (( q = strchr( rbuf, '=' )) == NULL || p < q );
208 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
209 for ( p = rbuf; p < q; ++p ) {
210 if ( !isdigit( *p )) {
221 rc = process_ldif_rec( start );
223 rc = process_ldapmod_rec( start );
238 process_ldif_rec( char *rbuf )
240 char *line, *dn, *type, *value, *newrdn, *p;
241 int rc, linenum, vlen, modop, replicaport;
242 int expect_modop, expect_sep, expect_ct, expect_newrdn;
243 int expect_deleteoldrdn, deleteoldrdn;
244 int saw_replica, use_record, new_entry, delete_entry, got_all;
249 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
250 expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
257 while ( rc == 0 && ( line = str_getline( &rbuf )) != NULL ) {
259 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
265 if ( str_parse_line( line, &type, &value, &vlen ) < 0 ) {
266 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
267 prog, linenum, dn == NULL ? "" : dn );
268 rc = LDAP_PARAM_ERROR;
273 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
275 if (( p = strchr( value, ':' )) == NULL ) {
276 replicaport = LDAP_PORT;
279 replicaport = atoi( p );
281 if ( strcasecmp( value, ldaphost ) == 0 &&
282 replicaport == ldapport ) {
285 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
286 if (( dn = strdup( value )) == NULL ) {
292 continue; /* skip all lines until we see "dn:" */
297 if ( !use_record && saw_replica ) {
298 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
304 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
305 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
308 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
310 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
312 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
313 got_all = delete_entry = 1;
316 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
317 prog, T_CHANGETYPESTR, value, linenum, dn );
318 rc = LDAP_PARAM_ERROR;
321 } else if ( new ) { /* missing changetype => add */
323 modop = LDAP_MOD_ADD;
325 expect_modop = 1; /* missing changetype => modify */
329 if ( expect_modop ) {
332 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
333 modop = LDAP_MOD_ADD;
335 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
336 modop = LDAP_MOD_REPLACE;
338 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
339 modop = LDAP_MOD_DELETE;
340 addmodifyop( &pmods, modop, value, NULL, 0 );
342 } else { /* no modify op: use default */
343 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
347 if ( expect_newrdn ) {
348 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
349 if (( newrdn = strdup( value )) == NULL ) {
353 expect_deleteoldrdn = 1;
356 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
357 prog, T_NEWRDNSTR, type, linenum, dn );
358 rc = LDAP_PARAM_ERROR;
360 } else if ( expect_deleteoldrdn ) {
361 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
362 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
365 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
366 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
367 rc = LDAP_PARAM_ERROR;
369 } else if ( got_all ) {
371 "%s: extra lines at end (line %d of entry %s)\n",
373 rc = LDAP_PARAM_ERROR;
375 addmodifyop( &pmods, modop, type, value, vlen );
380 if ( delete_entry ) {
382 } else if ( newrdn != NULL ) {
383 rc = domodrdn( dn, newrdn, deleteoldrdn );
385 rc = domodify( dn, pmods, new_entry );
388 if ( rc == LDAP_SUCCESS ) {
396 if ( newrdn != NULL ) {
399 if ( pmods != NULL ) {
408 process_ldapmod_rec( char *rbuf )
410 char *line, *dn, *p, *q, *attr, *value;
411 int rc, linenum, modop;
420 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
422 if (( p = strchr( rbuf, '\n' )) == NULL ) {
425 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
434 if ( dn == NULL ) { /* first line contains DN */
435 if (( dn = strdup( line )) == NULL ) {
440 if (( p = strchr( line, '=' )) == NULL ) {
442 p = line + strlen( line );
448 for ( attr = line; *attr != '\0' && isspace( *attr ); ++attr ) {
449 ; /* skip attribute leading white space */
452 for ( q = p - 1; q > attr && isspace( *q ); --q ) {
453 *q = '\0'; /* remove attribute trailing white space */
456 if ( value != NULL ) {
457 while ( isspace( *value )) {
458 ++value; /* skip value leading white space */
460 for ( q = value + strlen( value ) - 1; q > value &&
461 isspace( *q ); --q ) {
462 *q = '\0'; /* remove value trailing white space */
464 if ( *value == '\0' ) {
470 if ( value == NULL && new ) {
471 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
472 prog, linenum, attr );
473 rc = LDAP_PARAM_ERROR;
477 modop = LDAP_MOD_DELETE;
481 modop = LDAP_MOD_ADD;
485 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
488 addmodifyop( &pmods, modop, attr, value,
489 ( value == NULL ) ? 0 : strlen( value ));
498 rc = LDAP_PARAM_ERROR;
499 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
504 if ( pmods != NULL ) {
516 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
523 modop |= LDAP_MOD_BVALUES;
526 if ( pmods != NULL ) {
527 for ( ; pmods[ i ] != NULL; ++i ) {
528 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
529 pmods[ i ]->mod_op == modop ) {
535 if ( pmods == NULL || pmods[ i ] == NULL ) {
536 if (( pmods = (LDAPMod **)safe_realloc( pmods, (i + 2) *
537 sizeof( LDAPMod * ))) == NULL ) {
538 perror( "safe_realloc" );
542 pmods[ i + 1 ] = NULL;
543 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
548 pmods[ i ]->mod_op = modop;
549 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
555 if ( value != NULL ) {
557 if ( pmods[ i ]->mod_bvalues != NULL ) {
558 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
562 if (( pmods[ i ]->mod_bvalues =
563 (struct berval **)safe_realloc( pmods[ i ]->mod_bvalues,
564 (j + 2) * sizeof( struct berval * ))) == NULL ) {
565 perror( "safe_realloc" );
568 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
569 if (( bvp = (struct berval *)malloc( sizeof( struct berval )))
574 pmods[ i ]->mod_bvalues[ j ] = bvp;
576 if ( valsfromfiles && *value == '/' ) { /* get value from file */
577 if ( fromfile( value, bvp ) < 0 ) {
582 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
586 SAFEMEMCPY( bvp->bv_val, value, vlen );
587 bvp->bv_val[ vlen ] = '\0';
594 domodify( char *dn, LDAPMod **pmods, int newentry )
596 int i, j, k, notascii, op;
599 if ( pmods == NULL ) {
600 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
602 return( LDAP_PARAM_ERROR );
606 for ( i = 0; pmods[ i ] != NULL; ++i ) {
607 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
608 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
609 "replace" : op == LDAP_MOD_ADD ?
610 "add" : "delete", pmods[ i ]->mod_type );
611 if ( pmods[ i ]->mod_bvalues != NULL ) {
612 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
613 bvp = pmods[ i ]->mod_bvalues[ j ];
615 for ( k = 0; k < bvp->bv_len; ++k ) {
616 if ( !isascii( bvp->bv_val[ k ] )) {
622 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
624 printf( "\t%s\n", bvp->bv_val );
632 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
634 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
639 i = ldap_add_s( ld, dn, pmods );
641 i = ldap_modify_s( ld, dn, pmods );
643 if ( i != LDAP_SUCCESS ) {
644 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
645 } else if ( verbose ) {
646 printf( "modify complete\n" );
663 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
665 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
666 ldap_perror( ld, "ldap_delete" );
667 } else if ( verbose ) {
668 printf( "delete complete" );
681 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
686 printf( "new RDN: %s (%skeep existing values)\n",
687 newrdn, deleteoldrdn ? "do not " : "" );
690 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
692 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
694 ldap_perror( ld, "ldap_modrdn" );
696 printf( "modrdn completed\n" );
710 freepmods( LDAPMod **pmods )
714 for ( i = 0; pmods[ i ] != NULL; ++i ) {
715 if ( pmods[ i ]->mod_bvalues != NULL ) {
716 ber_bvecfree( pmods[ i ]->mod_bvalues );
718 if ( pmods[ i ]->mod_type != NULL ) {
719 free( pmods[ i ]->mod_type );
728 fromfile( char *path, struct berval *bv )
734 if (( fp = fopen( path, "r" )) == NULL ) {
739 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
745 bv->bv_len = ftell( fp );
747 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
753 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
759 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
763 if ( rlen != bv->bv_len ) {
769 return( bv->bv_len );
774 read_one_record( FILE *fp )
777 char *buf, line[ LDAPMOD_MAXLINE ];
783 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
784 (( len = strlen( line )) > 1 )) {
785 if ( lcur + len + 1 > lmax ) {
786 lmax = LDAPMOD_MAXLINE
787 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
788 if (( buf = (char *)safe_realloc( buf, lmax )) == NULL ) {
789 perror( "safe_realloc" );
793 strcpy( buf + lcur, line );