1 /* ldapmodify.c - generic program to modify or add entries using LDAP */
10 #include <sys/types.h>
22 #include "ldapconfig.h"
25 static char *binddn = LDAPMODIFY_BINDDN;
26 static char *passwd = LDAPMODIFY_BIND_CRED;
27 static char *ldaphost = LDAPHOST;
28 static int ldapport = LDAP_PORT;
29 static int new, replace, not, verbose, contoper, force, valsfromfiles;
33 extern int ldap_debug, lber_debug;
34 #endif /* LDAP_DEBUG */
36 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
39 #define LDAPMOD_MAXLINE 4096
41 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
42 #define T_REPLICA_STR "replica"
44 #define T_CHANGETYPESTR "changetype"
45 #define T_ADDCTSTR "add"
46 #define T_MODIFYCTSTR "modify"
47 #define T_DELETECTSTR "delete"
48 #define T_MODRDNCTSTR "modrdn"
49 #define T_MODOPADDSTR "add"
50 #define T_MODOPREPLACESTR "replace"
51 #define T_MODOPDELETESTR "delete"
52 #define T_MODSEPSTR "-"
53 #define T_NEWRDNSTR "newrdn"
54 #define T_DELETEOLDRDNSTR "deleteoldrdn"
58 static int process_ldapmod_rec( char *rbuf );
59 static int process_ldif_rec( char *rbuf );
60 static void addmodifyop( LDAPMod ***pmodsp, int modop, char *attr,
61 char *value, int vlen );
62 static int domodify( char *dn, LDAPMod **pmods, int newentry );
63 static int dodelete( char *dn );
64 static int domodrdn( char *dn, char *newrdn, int deleteoldrdn );
65 static void freepmods( LDAPMod **pmods );
66 static int fromfile( char *path, struct berval *bv );
67 static char *read_one_record( FILE *fp );
68 #else /* NEEDPROTOS */
69 static int process_ldapmod_rec();
70 static int process_ldif_rec();
71 static void addmodifyop();
72 static int domodify();
73 static int dodelete();
74 static int domodrdn();
75 static void freepmods();
76 static int fromfile();
77 static char *read_one_record();
78 #endif /* NEEDPROTOS */
85 char *infile, *rbuf, *start, *p, *q;
87 int rc, i, kerberos, use_ldif, authmethod;
88 char *usage = "usage: %s [-abcknrvF] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n";
93 if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
98 new = ( strcmp( prog, "ldapadd" ) == 0 );
101 kerberos = not = verbose = valsfromfiles = 0;
103 while (( i = getopt( argc, argv, "FabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
108 case 'b': /* read values from files (for binary attributes) */
111 case 'c': /* continuous operation */
114 case 'r': /* default is to replace rather than add values */
117 case 'k': /* kerberos bind */
120 case 'K': /* kerberos bind, part 1 only */
123 case 'F': /* force all changes records to be used */
126 case 'h': /* ldap host */
127 ldaphost = strdup( optarg );
129 case 'D': /* bind DN */
130 binddn = strdup( optarg );
132 case 'w': /* password */
133 passwd = strdup( optarg );
137 ldap_debug = lber_debug = atoi( optarg ); /* */
138 #else /* LDAP_DEBUG */
139 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
141 #endif /* LDAP_DEBUG */
143 case 'f': /* read from file */
144 infile = strdup( optarg );
147 ldapport = atoi( optarg );
149 case 'n': /* print adds, don't actually do them */
152 case 'v': /* verbose mode */
156 fprintf( stderr, usage, prog );
161 if ( argc - optind != 0 ) {
162 fprintf( stderr, usage, prog );
166 if ( infile != NULL ) {
167 if (( fp = fopen( infile, "r" )) == NULL ) {
177 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
178 perror( "ldap_open" );
182 ld->ld_deref = LDAP_DEREF_NEVER; /* this seems prudent */
185 authmethod = LDAP_AUTH_SIMPLE;
186 } else if ( kerberos == 1 ) {
187 authmethod = LDAP_AUTH_KRBV41;
189 authmethod = LDAP_AUTH_KRBV4;
191 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
192 ldap_perror( ld, "ldap_bind" );
199 while (( rc == 0 || contoper ) &&
200 ( rbuf = read_one_record( fp )) != NULL ) {
202 * we assume record is ldif/slapd.replog if the first line
203 * has a colon that appears to the left of any equal signs, OR
204 * if the first line consists entirely of digits (an entry id)
206 use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
207 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
208 (( q = strchr( rbuf, '=' )) == NULL || p < q );
212 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
213 for ( p = rbuf; p < q; ++p ) {
214 if ( !isdigit( *p )) {
225 rc = process_ldif_rec( start );
227 rc = process_ldapmod_rec( start );
242 process_ldif_rec( char *rbuf )
244 char *line, *dn, *type, *value, *newrdn, *p;
245 int rc, linenum, vlen, modop, replicaport;
246 int expect_modop, expect_sep, expect_ct, expect_newrdn;
247 int expect_deleteoldrdn, deleteoldrdn;
248 int saw_replica, use_record, new_entry, delete_entry, got_all;
253 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
254 expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
261 while ( rc == 0 && ( line = str_getline( &rbuf )) != NULL ) {
263 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
269 if ( str_parse_line( line, &type, &value, &vlen ) < 0 ) {
270 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
271 prog, linenum, dn == NULL ? "" : dn );
272 rc = LDAP_PARAM_ERROR;
277 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
279 if (( p = strchr( value, ':' )) == NULL ) {
280 replicaport = LDAP_PORT;
283 replicaport = atoi( p );
285 if ( strcasecmp( value, ldaphost ) == 0 &&
286 replicaport == ldapport ) {
289 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
290 if (( dn = strdup( value )) == NULL ) {
296 continue; /* skip all lines until we see "dn:" */
301 if ( !use_record && saw_replica ) {
302 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
308 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
309 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
312 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
314 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
316 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
317 got_all = delete_entry = 1;
320 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
321 prog, T_CHANGETYPESTR, value, linenum, dn );
322 rc = LDAP_PARAM_ERROR;
325 } else if ( new ) { /* missing changetype => add */
327 modop = LDAP_MOD_ADD;
329 expect_modop = 1; /* missing changetype => modify */
333 if ( expect_modop ) {
336 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
337 modop = LDAP_MOD_ADD;
339 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
340 modop = LDAP_MOD_REPLACE;
342 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
343 modop = LDAP_MOD_DELETE;
344 addmodifyop( &pmods, modop, value, NULL, 0 );
346 } else { /* no modify op: use default */
347 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
351 if ( expect_newrdn ) {
352 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
353 if (( newrdn = strdup( value )) == NULL ) {
357 expect_deleteoldrdn = 1;
360 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
361 prog, T_NEWRDNSTR, type, linenum, dn );
362 rc = LDAP_PARAM_ERROR;
364 } else if ( expect_deleteoldrdn ) {
365 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
366 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
369 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
370 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
371 rc = LDAP_PARAM_ERROR;
373 } else if ( got_all ) {
375 "%s: extra lines at end (line %d of entry %s)\n",
377 rc = LDAP_PARAM_ERROR;
379 addmodifyop( &pmods, modop, type, value, vlen );
384 if ( delete_entry ) {
386 } else if ( newrdn != NULL ) {
387 rc = domodrdn( dn, newrdn, deleteoldrdn );
389 rc = domodify( dn, pmods, new_entry );
392 if ( rc == LDAP_SUCCESS ) {
400 if ( newrdn != NULL ) {
403 if ( pmods != NULL ) {
412 process_ldapmod_rec( char *rbuf )
414 char *line, *dn, *p, *q, *attr, *value;
415 int rc, linenum, modop;
424 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
426 if (( p = strchr( rbuf, '\n' )) == NULL ) {
429 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
438 if ( dn == NULL ) { /* first line contains DN */
439 if (( dn = strdup( line )) == NULL ) {
444 if (( p = strchr( line, '=' )) == NULL ) {
446 p = line + strlen( line );
452 for ( attr = line; *attr != '\0' && isspace( *attr ); ++attr ) {
453 ; /* skip attribute leading white space */
456 for ( q = p - 1; q > attr && isspace( *q ); --q ) {
457 *q = '\0'; /* remove attribute trailing white space */
460 if ( value != NULL ) {
461 while ( isspace( *value )) {
462 ++value; /* skip value leading white space */
464 for ( q = value + strlen( value ) - 1; q > value &&
465 isspace( *q ); --q ) {
466 *q = '\0'; /* remove value trailing white space */
468 if ( *value == '\0' ) {
474 if ( value == NULL && new ) {
475 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
476 prog, linenum, attr );
477 rc = LDAP_PARAM_ERROR;
481 modop = LDAP_MOD_DELETE;
485 modop = LDAP_MOD_ADD;
489 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
492 addmodifyop( &pmods, modop, attr, value,
493 ( value == NULL ) ? 0 : strlen( value ));
502 rc = LDAP_PARAM_ERROR;
503 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
508 if ( pmods != NULL ) {
520 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
527 modop |= LDAP_MOD_BVALUES;
530 if ( pmods != NULL ) {
531 for ( ; pmods[ i ] != NULL; ++i ) {
532 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
533 pmods[ i ]->mod_op == modop ) {
539 if ( pmods == NULL || pmods[ i ] == NULL ) {
540 if (( pmods = (LDAPMod **)safe_realloc( pmods, (i + 2) *
541 sizeof( LDAPMod * ))) == NULL ) {
542 perror( "safe_realloc" );
546 pmods[ i + 1 ] = NULL;
547 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
552 pmods[ i ]->mod_op = modop;
553 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
559 if ( value != NULL ) {
561 if ( pmods[ i ]->mod_bvalues != NULL ) {
562 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
566 if (( pmods[ i ]->mod_bvalues =
567 (struct berval **)safe_realloc( pmods[ i ]->mod_bvalues,
568 (j + 2) * sizeof( struct berval * ))) == NULL ) {
569 perror( "safe_realloc" );
572 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
573 if (( bvp = (struct berval *)malloc( sizeof( struct berval )))
578 pmods[ i ]->mod_bvalues[ j ] = bvp;
580 if ( valsfromfiles && *value == '/' ) { /* get value from file */
581 if ( fromfile( value, bvp ) < 0 ) {
586 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
590 SAFEMEMCPY( bvp->bv_val, value, vlen );
591 bvp->bv_val[ vlen ] = '\0';
598 domodify( char *dn, LDAPMod **pmods, int newentry )
600 int i, j, k, notascii, op;
603 if ( pmods == NULL ) {
604 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
606 return( LDAP_PARAM_ERROR );
610 for ( i = 0; pmods[ i ] != NULL; ++i ) {
611 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
612 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
613 "replace" : op == LDAP_MOD_ADD ?
614 "add" : "delete", pmods[ i ]->mod_type );
615 if ( pmods[ i ]->mod_bvalues != NULL ) {
616 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
617 bvp = pmods[ i ]->mod_bvalues[ j ];
619 for ( k = 0; k < bvp->bv_len; ++k ) {
620 if ( !isascii( bvp->bv_val[ k ] )) {
626 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
628 printf( "\t%s\n", bvp->bv_val );
636 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
638 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
643 i = ldap_add_s( ld, dn, pmods );
645 i = ldap_modify_s( ld, dn, pmods );
647 if ( i != LDAP_SUCCESS ) {
648 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
649 } else if ( verbose ) {
650 printf( "modify complete\n" );
667 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
669 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
670 ldap_perror( ld, "ldap_delete" );
671 } else if ( verbose ) {
672 printf( "delete complete" );
685 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
690 printf( "new RDN: %s (%skeep existing values)\n",
691 newrdn, deleteoldrdn ? "do not " : "" );
694 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
696 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
698 ldap_perror( ld, "ldap_modrdn" );
700 printf( "modrdn completed\n" );
714 freepmods( LDAPMod **pmods )
718 for ( i = 0; pmods[ i ] != NULL; ++i ) {
719 if ( pmods[ i ]->mod_bvalues != NULL ) {
720 ber_bvecfree( pmods[ i ]->mod_bvalues );
722 if ( pmods[ i ]->mod_type != NULL ) {
723 free( pmods[ i ]->mod_type );
732 fromfile( char *path, struct berval *bv )
738 if (( fp = fopen( path, "r" )) == NULL ) {
743 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
749 bv->bv_len = ftell( fp );
751 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
757 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
763 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
767 if ( rlen != bv->bv_len ) {
773 return( bv->bv_len );
778 read_one_record( FILE *fp )
781 char *buf, line[ LDAPMOD_MAXLINE ];
787 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
788 (( len = strlen( line )) > 1 )) {
789 if ( lcur + len + 1 > lmax ) {
790 lmax = LDAPMOD_MAXLINE
791 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
792 if (( buf = (char *)safe_realloc( buf, lmax )) == NULL ) {
793 perror( "safe_realloc" );
797 strcpy( buf + lcur, line );