2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 /* ldapmodify.c - generic program to modify or add entries using LDAP */
13 #include <ac/signal.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
19 #ifdef HAVE_SYS_FILE_H
30 #include <ldapconfig.h>
33 static char *binddn = NULL;
34 static char *passwd = NULL;
35 static char *ldaphost = NULL;
36 static int ldapport = 0;
37 static int new, replace, not, verbose, contoper, force, valsfromfiles;
40 #define LDAPMOD_MAXLINE 4096
42 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
43 #define T_REPLICA_STR "replica"
45 #define T_CHANGETYPESTR "changetype"
46 #define T_ADDCTSTR "add"
47 #define T_MODIFYCTSTR "modify"
48 #define T_DELETECTSTR "delete"
49 #define T_MODRDNCTSTR "modrdn"
50 #define T_MODOPADDSTR "add"
51 #define T_MODOPREPLACESTR "replace"
52 #define T_MODOPDELETESTR "delete"
53 #define T_MODSEPSTR "-"
54 #define T_NEWRDNSTR "newrdn"
55 #define T_DELETEOLDRDNSTR "deleteoldrdn"
58 static void usage LDAP_P(( const char *prog ));
59 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
60 static int process_ldif_rec LDAP_P(( char *rbuf ));
61 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
62 char *value, int vlen ));
63 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
64 static int dodelete LDAP_P(( char *dn ));
65 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
66 static int fromfile LDAP_P(( char *path, struct berval *bv ));
67 static char *read_one_record LDAP_P(( FILE *fp ));
70 usage( const char *prog )
73 "Add or modify entries from an LDAP server\n\n"
74 "usage: %s [-abcknrvF] [-d debug-level] [-P version] [-h ldaphost]\n"
75 " [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n"
76 " a - add values (default%s)\n"
77 " b - read values from files (for binary attributes)\n"
78 " c - continuous operation\n"
81 " f - read from file\n"
82 " F - force all changes records to be used\n"
84 " n - print adds, don't actually do them\n"
86 " r - replace values\n"
89 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
95 main( int argc, char **argv )
97 char *infile, *rbuf, *start, *p, *q;
99 int rc, i, use_ldif, authmethod, version, want_bindpw, debug;
101 if (( prog = strrchr( argv[ 0 ], *DIRSEP )) == NULL ) {
107 /* Print usage when no parameters */
111 new = ( strcmp( prog, "ldapadd" ) == 0 );
114 not = verbose = valsfromfiles = want_bindpw = debug = 0;
115 authmethod = LDAP_AUTH_SIMPLE;
118 while (( i = getopt( argc, argv, "WFabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
123 case 'b': /* read values from files (for binary attributes) */
126 case 'c': /* continuous operation */
129 case 'r': /* default is to replace rather than add values */
132 case 'k': /* kerberos bind */
134 authmethod = LDAP_AUTH_KRBV4;
136 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
138 return( EXIT_FAILURE );
141 case 'K': /* kerberos bind, part 1 only */
143 authmethod = LDAP_AUTH_KRBV41;
145 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
147 return( EXIT_FAILURE );
150 case 'F': /* force all changes records to be used */
153 case 'h': /* ldap host */
154 ldaphost = strdup( optarg );
156 case 'D': /* bind DN */
157 binddn = strdup( optarg );
159 case 'w': /* password */
160 passwd = strdup( optarg );
163 debug |= atoi( optarg );
165 case 'f': /* read from file */
166 infile = strdup( optarg );
169 ldapport = atoi( optarg );
171 case 'n': /* print adds, don't actually do them */
174 case 'v': /* verbose mode */
181 switch( atoi(optarg) )
184 version = LDAP_VERSION2;
187 version = LDAP_VERSION3;
190 fprintf( stderr, "protocol version should be 2 or 3\n" );
199 if ( argc != optind )
202 if ( infile != NULL ) {
203 if (( fp = fopen( infile, "r" )) == NULL ) {
205 return( EXIT_FAILURE );
212 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
213 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
215 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
216 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
222 (void) SIGNAL( SIGPIPE, SIG_IGN );
226 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
227 perror( "ldap_init" );
228 return( EXIT_FAILURE );
231 /* this seems prudent */
233 int deref = LDAP_DEREF_NEVER;
234 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
238 passwd = getpass("Enter LDAP Password: ");
241 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
243 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
246 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
247 ldap_perror( ld, "ldap_bind" );
248 return( EXIT_FAILURE );
254 while (( rc == 0 || contoper ) &&
255 ( rbuf = read_one_record( fp )) != NULL ) {
257 * we assume record is ldif/slapd.replog if the first line
258 * has a colon that appears to the left of any equal signs, OR
259 * if the first line consists entirely of digits (an entry id)
261 use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
262 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
263 (( q = strchr( rbuf, '=' )) == NULL || p < q );
267 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
268 for ( p = rbuf; p < q; ++p ) {
269 if ( !isdigit( (unsigned char) *p )) {
280 rc = process_ldif_rec( start );
282 rc = process_ldapmod_rec( start );
286 fprintf( stderr, "%s() = %d\n",
287 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
301 process_ldif_rec( char *rbuf )
303 char *line, *dn, *type, *value, *newrdn, *p;
304 int rc, linenum, vlen, modop, replicaport;
305 int expect_modop, expect_sep, expect_ct, expect_newrdn;
306 int expect_deleteoldrdn, deleteoldrdn;
307 int saw_replica, use_record, new_entry, delete_entry, got_all;
312 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
313 expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
320 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
322 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
328 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
329 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
330 prog, linenum, dn == NULL ? "" : dn );
331 rc = LDAP_PARAM_ERROR;
336 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
338 if (( p = strchr( value, ':' )) == NULL ) {
342 replicaport = atoi( p );
344 if ( strcasecmp( value, ldaphost ) == 0 &&
345 replicaport == ldapport ) {
348 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
349 if (( dn = strdup( value )) == NULL ) {
355 continue; /* skip all lines until we see "dn:" */
360 if ( !use_record && saw_replica ) {
361 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
367 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
368 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
371 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
373 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
375 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
376 got_all = delete_entry = 1;
379 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
380 prog, T_CHANGETYPESTR, value, linenum, dn );
381 rc = LDAP_PARAM_ERROR;
384 } else if ( new ) { /* missing changetype => add */
386 modop = LDAP_MOD_ADD;
388 expect_modop = 1; /* missing changetype => modify */
392 if ( expect_modop ) {
395 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
396 modop = LDAP_MOD_ADD;
398 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
399 modop = LDAP_MOD_REPLACE;
401 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
402 modop = LDAP_MOD_DELETE;
403 addmodifyop( &pmods, modop, value, NULL, 0 );
405 } else { /* no modify op: use default */
406 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
410 if ( expect_newrdn ) {
411 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
412 if (( newrdn = strdup( value )) == NULL ) {
416 expect_deleteoldrdn = 1;
419 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
420 prog, T_NEWRDNSTR, type, linenum, dn );
421 rc = LDAP_PARAM_ERROR;
423 } else if ( expect_deleteoldrdn ) {
424 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
425 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
428 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
429 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
430 rc = LDAP_PARAM_ERROR;
432 } else if ( got_all ) {
434 "%s: extra lines at end (line %d of entry %s)\n",
436 rc = LDAP_PARAM_ERROR;
438 addmodifyop( &pmods, modop, type, value, vlen );
443 if ( delete_entry ) {
445 } else if ( newrdn != NULL ) {
446 rc = domodrdn( dn, newrdn, deleteoldrdn );
448 rc = domodify( dn, pmods, new_entry );
451 if ( rc == LDAP_SUCCESS ) {
459 if ( newrdn != NULL ) {
462 if ( pmods != NULL ) {
463 ldap_mods_free( pmods, 1 );
471 process_ldapmod_rec( char *rbuf )
473 char *line, *dn, *p, *q, *attr, *value;
474 int rc, linenum, modop;
483 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
485 if (( p = strchr( rbuf, '\n' )) == NULL ) {
488 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
489 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
497 if ( dn == NULL ) { /* first line contains DN */
498 if (( dn = strdup( line )) == NULL ) {
503 if (( p = strchr( line, '=' )) == NULL ) {
505 p = line + strlen( line );
512 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
513 ; /* skip attribute leading white space */
516 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
517 *q = '\0'; /* remove attribute trailing white space */
520 if ( value != NULL ) {
521 while ( isspace( (unsigned char) *value )) {
522 ++value; /* skip value leading white space */
524 for ( q = value + strlen( value ) - 1; q > value &&
525 isspace( (unsigned char) *q ); --q ) {
526 *q = '\0'; /* remove value trailing white space */
528 if ( *value == '\0' ) {
534 if ( value == NULL && new ) {
535 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
536 prog, linenum, attr );
537 rc = LDAP_PARAM_ERROR;
541 modop = LDAP_MOD_DELETE;
545 modop = LDAP_MOD_ADD;
549 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
552 addmodifyop( &pmods, modop, attr, value,
553 ( value == NULL ) ? 0 : strlen( value ));
562 rc = LDAP_PARAM_ERROR;
563 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
568 if ( pmods != NULL ) {
569 ldap_mods_free( pmods, 1 );
580 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
587 modop |= LDAP_MOD_BVALUES;
590 if ( pmods != NULL ) {
591 for ( ; pmods[ i ] != NULL; ++i ) {
592 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
593 pmods[ i ]->mod_op == modop ) {
599 if ( pmods == NULL || pmods[ i ] == NULL ) {
600 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
601 sizeof( LDAPMod * ))) == NULL ) {
606 pmods[ i + 1 ] = NULL;
607 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
612 pmods[ i ]->mod_op = modop;
613 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
619 if ( value != NULL ) {
621 if ( pmods[ i ]->mod_bvalues != NULL ) {
622 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
626 if (( pmods[ i ]->mod_bvalues =
627 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
628 (j + 2) * sizeof( struct berval * ))) == NULL ) {
629 perror( "ber_realloc" );
632 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
633 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
635 perror( "ber_memalloc" );
638 pmods[ i ]->mod_bvalues[ j ] = bvp;
640 if ( valsfromfiles && *value == '/' ) { /* get value from file */
641 if ( fromfile( value, bvp ) < 0 ) {
646 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
650 SAFEMEMCPY( bvp->bv_val, value, vlen );
651 bvp->bv_val[ vlen ] = '\0';
658 domodify( char *dn, LDAPMod **pmods, int newentry )
660 int i, j, k, notascii, op;
663 if ( pmods == NULL ) {
664 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
666 return( LDAP_PARAM_ERROR );
670 for ( i = 0; pmods[ i ] != NULL; ++i ) {
671 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
672 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
673 "replace" : op == LDAP_MOD_ADD ?
674 "add" : "delete", pmods[ i ]->mod_type );
675 if ( pmods[ i ]->mod_bvalues != NULL ) {
676 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
677 bvp = pmods[ i ]->mod_bvalues[ j ];
679 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
680 if ( !isascii( bvp->bv_val[ k ] )) {
686 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
688 printf( "\t%s\n", bvp->bv_val );
696 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
698 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
703 i = ldap_add_s( ld, dn, pmods );
705 i = ldap_modify_s( ld, dn, pmods );
707 if ( i != LDAP_SUCCESS ) {
708 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
709 } else if ( verbose ) {
710 printf( "modify complete\n" );
727 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
729 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
730 ldap_perror( ld, "ldap_delete" );
731 } else if ( verbose ) {
732 printf( "delete complete" );
745 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
750 printf( "new RDN: %s (%skeep existing values)\n",
751 newrdn, deleteoldrdn ? "do not " : "" );
754 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
756 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
758 ldap_perror( ld, "ldap_modrdn" );
760 printf( "modrdn completed\n" );
773 fromfile( char *path, struct berval *bv )
779 if (( fp = fopen( path, "r" )) == NULL ) {
784 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
790 bv->bv_len = ftell( fp );
792 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
798 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
801 ber_memfree( bv->bv_val );
806 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
810 if ( (unsigned long) rlen != bv->bv_len ) {
812 ber_memfree( bv->bv_val );
817 return( bv->bv_len );
822 read_one_record( FILE *fp )
825 char *buf, line[ LDAPMOD_MAXLINE ];
831 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
832 (( len = strlen( line )) > 1 )) {
833 if ( lcur + len + 1 > lmax ) {
834 lmax = LDAPMOD_MAXLINE
835 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
836 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
841 strcpy( buf + lcur, line );