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"
57 static int process_ldapmod_rec( char *rbuf );
58 static int process_ldif_rec( char *rbuf );
59 static void addmodifyop( LDAPMod ***pmodsp, int modop, char *attr,
60 char *value, int vlen );
61 static int domodify( char *dn, LDAPMod **pmods, int newentry );
62 static int dodelete( char *dn );
63 static int domodrdn( char *dn, char *newrdn, int deleteoldrdn );
64 static void freepmods( LDAPMod **pmods );
65 static int fromfile( char *path, struct berval *bv );
66 static char *read_one_record( FILE *fp );
67 #else /* NEEDPROTOS */
68 static int process_ldapmod_rec();
69 static int process_ldif_rec();
70 static void addmodifyop();
71 static int domodify();
72 static int dodelete();
73 static int domodrdn();
74 static void freepmods();
75 static int fromfile();
76 static char *read_one_record();
77 #endif /* NEEDPROTOS */
84 char *infile, *rbuf, *start, *p, *q;
86 int rc, i, kerberos, use_ldif, authmethod;
87 char *usage = "usage: %s [-abcknrvF] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n";
92 if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
97 new = ( strcmp( prog, "ldapadd" ) == 0 );
100 kerberos = not = verbose = valsfromfiles = 0;
102 while (( i = getopt( argc, argv, "FabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
107 case 'b': /* read values from files (for binary attributes) */
110 case 'c': /* continuous operation */
113 case 'r': /* default is to replace rather than add values */
116 case 'k': /* kerberos bind */
119 case 'K': /* kerberos bind, part 1 only */
122 case 'F': /* force all changes records to be used */
125 case 'h': /* ldap host */
126 ldaphost = strdup( optarg );
128 case 'D': /* bind DN */
129 binddn = strdup( optarg );
131 case 'w': /* password */
132 passwd = strdup( optarg );
136 ldap_debug = lber_debug = atoi( optarg ); /* */
137 #else /* LDAP_DEBUG */
138 fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
140 #endif /* LDAP_DEBUG */
142 case 'f': /* read from file */
143 infile = strdup( optarg );
146 ldapport = atoi( optarg );
148 case 'n': /* print adds, don't actually do them */
151 case 'v': /* verbose mode */
155 fprintf( stderr, usage, prog );
160 if ( argc - optind != 0 ) {
161 fprintf( stderr, usage, prog );
165 if ( infile != NULL ) {
166 if (( fp = fopen( infile, "r" )) == NULL ) {
176 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
177 perror( "ldap_open" );
181 ld->ld_deref = LDAP_DEREF_NEVER; /* this seems prudent */
184 authmethod = LDAP_AUTH_SIMPLE;
185 } else if ( kerberos == 1 ) {
186 authmethod = LDAP_AUTH_KRBV41;
188 authmethod = LDAP_AUTH_KRBV4;
190 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
191 ldap_perror( ld, "ldap_bind" );
198 while (( rc == 0 || contoper ) &&
199 ( rbuf = read_one_record( fp )) != NULL ) {
201 * we assume record is ldif/slapd.replog if the first line
202 * has a colon that appears to the left of any equal signs, OR
203 * if the first line consists entirely of digits (an entry id)
205 use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
206 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
207 (( q = strchr( rbuf, '=' )) == NULL || p < q );
211 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
212 for ( p = rbuf; p < q; ++p ) {
213 if ( !isdigit( *p )) {
224 rc = process_ldif_rec( start );
226 rc = process_ldapmod_rec( start );
241 process_ldif_rec( char *rbuf )
243 char *line, *dn, *type, *value, *newrdn, *p;
244 int rc, linenum, vlen, modop, replicaport;
245 int expect_modop, expect_sep, expect_ct, expect_newrdn;
246 int expect_deleteoldrdn, deleteoldrdn;
247 int saw_replica, use_record, new_entry, delete_entry, got_all;
252 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
253 expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
260 while ( rc == 0 && ( line = str_getline( &rbuf )) != NULL ) {
262 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
268 if ( str_parse_line( line, &type, &value, &vlen ) < 0 ) {
269 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
270 prog, linenum, dn == NULL ? "" : dn );
271 rc = LDAP_PARAM_ERROR;
276 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
278 if (( p = strchr( value, ':' )) == NULL ) {
279 replicaport = LDAP_PORT;
282 replicaport = atoi( p );
284 if ( strcasecmp( value, ldaphost ) == 0 &&
285 replicaport == ldapport ) {
288 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
289 if (( dn = strdup( value )) == NULL ) {
295 continue; /* skip all lines until we see "dn:" */
300 if ( !use_record && saw_replica ) {
301 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
307 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
308 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
311 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
313 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
315 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
316 got_all = delete_entry = 1;
319 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
320 prog, T_CHANGETYPESTR, value, linenum, dn );
321 rc = LDAP_PARAM_ERROR;
324 } else if ( new ) { /* missing changetype => add */
326 modop = LDAP_MOD_ADD;
328 expect_modop = 1; /* missing changetype => modify */
332 if ( expect_modop ) {
335 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
336 modop = LDAP_MOD_ADD;
338 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
339 modop = LDAP_MOD_REPLACE;
341 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
342 modop = LDAP_MOD_DELETE;
343 addmodifyop( &pmods, modop, value, NULL, 0 );
345 } else { /* no modify op: use default */
346 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
350 if ( expect_newrdn ) {
351 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
352 if (( newrdn = strdup( value )) == NULL ) {
356 expect_deleteoldrdn = 1;
359 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
360 prog, T_NEWRDNSTR, type, linenum, dn );
361 rc = LDAP_PARAM_ERROR;
363 } else if ( expect_deleteoldrdn ) {
364 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
365 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
368 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
369 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
370 rc = LDAP_PARAM_ERROR;
372 } else if ( got_all ) {
374 "%s: extra lines at end (line %d of entry %s)\n",
376 rc = LDAP_PARAM_ERROR;
378 addmodifyop( &pmods, modop, type, value, vlen );
383 if ( delete_entry ) {
385 } else if ( newrdn != NULL ) {
386 rc = domodrdn( dn, newrdn, deleteoldrdn );
388 rc = domodify( dn, pmods, new_entry );
391 if ( rc == LDAP_SUCCESS ) {
399 if ( newrdn != NULL ) {
402 if ( pmods != NULL ) {
411 process_ldapmod_rec( char *rbuf )
413 char *line, *dn, *p, *q, *attr, *value;
414 int rc, linenum, modop;
423 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
425 if (( p = strchr( rbuf, '\n' )) == NULL ) {
428 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
437 if ( dn == NULL ) { /* first line contains DN */
438 if (( dn = strdup( line )) == NULL ) {
443 if (( p = strchr( line, '=' )) == NULL ) {
445 p = line + strlen( line );
451 for ( attr = line; *attr != '\0' && isspace( *attr ); ++attr ) {
452 ; /* skip attribute leading white space */
455 for ( q = p - 1; q > attr && isspace( *q ); --q ) {
456 *q = '\0'; /* remove attribute trailing white space */
459 if ( value != NULL ) {
460 while ( isspace( *value )) {
461 ++value; /* skip value leading white space */
463 for ( q = value + strlen( value ) - 1; q > value &&
464 isspace( *q ); --q ) {
465 *q = '\0'; /* remove value trailing white space */
467 if ( *value == '\0' ) {
473 if ( value == NULL && new ) {
474 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
475 prog, linenum, attr );
476 rc = LDAP_PARAM_ERROR;
480 modop = LDAP_MOD_DELETE;
484 modop = LDAP_MOD_ADD;
488 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
491 addmodifyop( &pmods, modop, attr, value,
492 ( value == NULL ) ? 0 : strlen( value ));
501 rc = LDAP_PARAM_ERROR;
502 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
507 if ( pmods != NULL ) {
519 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
526 modop |= LDAP_MOD_BVALUES;
529 if ( pmods != NULL ) {
530 for ( ; pmods[ i ] != NULL; ++i ) {
531 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
532 pmods[ i ]->mod_op == modop ) {
538 if ( pmods == NULL || pmods[ i ] == NULL ) {
539 if (( pmods = (LDAPMod **)safe_realloc( pmods, (i + 2) *
540 sizeof( LDAPMod * ))) == NULL ) {
541 perror( "safe_realloc" );
545 pmods[ i + 1 ] = NULL;
546 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
551 pmods[ i ]->mod_op = modop;
552 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
558 if ( value != NULL ) {
560 if ( pmods[ i ]->mod_bvalues != NULL ) {
561 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
565 if (( pmods[ i ]->mod_bvalues =
566 (struct berval **)safe_realloc( pmods[ i ]->mod_bvalues,
567 (j + 2) * sizeof( struct berval * ))) == NULL ) {
568 perror( "safe_realloc" );
571 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
572 if (( bvp = (struct berval *)malloc( sizeof( struct berval )))
577 pmods[ i ]->mod_bvalues[ j ] = bvp;
579 if ( valsfromfiles && *value == '/' ) { /* get value from file */
580 if ( fromfile( value, bvp ) < 0 ) {
585 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
589 SAFEMEMCPY( bvp->bv_val, value, vlen );
590 bvp->bv_val[ vlen ] = '\0';
597 domodify( char *dn, LDAPMod **pmods, int newentry )
599 int i, j, k, notascii, op;
602 if ( pmods == NULL ) {
603 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
605 return( LDAP_PARAM_ERROR );
609 for ( i = 0; pmods[ i ] != NULL; ++i ) {
610 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
611 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
612 "replace" : op == LDAP_MOD_ADD ?
613 "add" : "delete", pmods[ i ]->mod_type );
614 if ( pmods[ i ]->mod_bvalues != NULL ) {
615 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
616 bvp = pmods[ i ]->mod_bvalues[ j ];
618 for ( k = 0; k < bvp->bv_len; ++k ) {
619 if ( !isascii( bvp->bv_val[ k ] )) {
625 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
627 printf( "\t%s\n", bvp->bv_val );
635 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
637 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
642 i = ldap_add_s( ld, dn, pmods );
644 i = ldap_modify_s( ld, dn, pmods );
646 if ( i != LDAP_SUCCESS ) {
647 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
648 } else if ( verbose ) {
649 printf( "modify complete\n" );
666 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
668 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
669 ldap_perror( ld, "ldap_delete" );
670 } else if ( verbose ) {
671 printf( "delete complete" );
684 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
689 printf( "new RDN: %s (%skeep existing values)\n",
690 newrdn, deleteoldrdn ? "do not " : "" );
693 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
695 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
697 ldap_perror( ld, "ldap_modrdn" );
699 printf( "modrdn completed\n" );
713 freepmods( LDAPMod **pmods )
717 for ( i = 0; pmods[ i ] != NULL; ++i ) {
718 if ( pmods[ i ]->mod_bvalues != NULL ) {
719 ber_bvecfree( pmods[ i ]->mod_bvalues );
721 if ( pmods[ i ]->mod_type != NULL ) {
722 free( pmods[ i ]->mod_type );
731 fromfile( char *path, struct berval *bv )
737 if (( fp = fopen( path, "r" )) == NULL ) {
742 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
748 bv->bv_len = ftell( fp );
750 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
756 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
762 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
766 if ( rlen != bv->bv_len ) {
772 return( bv->bv_len );
777 read_one_record( FILE *fp )
780 char *buf, line[ LDAPMOD_MAXLINE ];
786 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
787 (( len = strlen( line )) > 1 )) {
788 if ( lcur + len + 1 > lmax ) {
789 lmax = LDAPMOD_MAXLINE
790 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
791 if (( buf = (char *)safe_realloc( buf, lmax )) == NULL ) {
792 perror( "safe_realloc" );
796 strcpy( buf + lcur, line );