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 */
11 #include <ac/stdlib.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
20 #ifdef HAVE_SYS_FILE_H
31 #include <ldap_defaults.h>
34 static char *binddn = NULL;
35 static char *passwd = NULL;
36 static char *ldaphost = NULL;
37 static int ldapport = 0;
38 static int new, replace, not, verbose, contoper, force, valsfromfiles;
41 #define LDAPMOD_MAXLINE 4096
43 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
44 #define T_REPLICA_STR "replica"
46 #define T_CHANGETYPESTR "changetype"
47 #define T_ADDCTSTR "add"
48 #define T_MODIFYCTSTR "modify"
49 #define T_DELETECTSTR "delete"
50 #define T_MODRDNCTSTR "modrdn"
51 #define T_MODOPADDSTR "add"
52 #define T_MODOPREPLACESTR "replace"
53 #define T_MODOPDELETESTR "delete"
54 #define T_MODSEPSTR "-"
55 #define T_NEWRDNSTR "newrdn"
56 #define T_DELETEOLDRDNSTR "deleteoldrdn"
59 static void usage LDAP_P(( const char *prog ));
60 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
61 static int process_ldif_rec LDAP_P(( char *rbuf ));
62 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
63 char *value, int vlen ));
64 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
65 static int dodelete LDAP_P(( char *dn ));
66 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
67 static int fromfile LDAP_P(( char *path, struct berval *bv ));
68 static char *read_one_record LDAP_P(( FILE *fp ));
71 usage( const char *prog )
74 "Add or modify entries from an LDAP server\n\n"
75 "usage: %s [-abcknrvF] [-M[M]] [-d debug-level] [-P version] [-h ldaphost]\n"
76 " [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n"
77 " a - add values (default%s)\n"
78 " b - read values from files (for binary attributes)\n"
79 " c - continuous operation\n"
81 " M - enable Manage DSA IT control (-MM for critical)\n"
83 " f - read from file\n"
84 " F - force all changes records to be used\n"
86 " n - print adds, don't actually do them\n"
88 " r - replace values\n"
91 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
97 main( int argc, char **argv )
99 char *infile, *rbuf, *start, *p, *q;
101 int rc, i, use_ldif, authmethod, version, want_bindpw, debug, manageDSAit;
103 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
109 /* Print usage when no parameters */
113 new = ( strcmp( prog, "ldapadd" ) == 0 );
116 not = verbose = valsfromfiles = want_bindpw = debug = manageDSAit = 0;
117 authmethod = LDAP_AUTH_SIMPLE;
120 while (( i = getopt( argc, argv, "WFMabckKnrtvh:p:D:w:d:f:P:" )) != EOF ) {
125 case 'b': /* read values from files (for binary attributes) */
128 case 'c': /* continuous operation */
131 case 'r': /* default is to replace rather than add values */
134 case 'k': /* kerberos bind */
136 authmethod = LDAP_AUTH_KRBV4;
138 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
140 return( EXIT_FAILURE );
143 case 'K': /* kerberos bind, part 1 only */
145 authmethod = LDAP_AUTH_KRBV41;
147 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
149 return( EXIT_FAILURE );
152 case 'F': /* force all changes records to be used */
155 case 'h': /* ldap host */
156 ldaphost = strdup( optarg );
158 case 'D': /* bind DN */
159 binddn = strdup( optarg );
161 case 'w': /* password */
162 passwd = strdup( optarg );
166 for( p = optarg; *p == '\0'; p++ ) {
172 debug |= atoi( optarg );
174 case 'f': /* read from file */
175 infile = strdup( optarg );
178 ldapport = atoi( optarg );
180 case 'n': /* print adds, don't actually do them */
183 case 'v': /* verbose mode */
187 /* enable Manage DSA IT */
194 switch( atoi(optarg) )
197 version = LDAP_VERSION2;
200 version = LDAP_VERSION3;
203 fprintf( stderr, "protocol version should be 2 or 3\n" );
212 if ( argc != optind )
215 if ( infile != NULL ) {
216 if (( fp = fopen( infile, "r" )) == NULL ) {
218 return( EXIT_FAILURE );
225 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
226 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
228 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
229 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
235 (void) SIGNAL( SIGPIPE, SIG_IGN );
239 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
240 perror( "ldap_init" );
241 return( EXIT_FAILURE );
244 /* this seems prudent */
246 int deref = LDAP_DEREF_NEVER;
247 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
251 passwd = getpass("Enter LDAP Password: ");
254 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
256 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
259 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
260 ldap_perror( ld, "ldap_bind" );
261 return( EXIT_FAILURE );
270 LDAPControl *ctrls[2];
274 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
275 c.ldctl_value.bv_val = NULL;
276 c.ldctl_value.bv_len = 0;
277 c.ldctl_iscritical = manageDSAit > 1;
279 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
281 if( err != LDAP_OPT_SUCCESS ) {
282 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
283 if( c.ldctl_iscritical ) {
284 exit( EXIT_FAILURE );
289 while (( rc == 0 || contoper ) &&
290 ( rbuf = read_one_record( fp )) != NULL ) {
292 * we assume record is ldif/slapd.replog if the first line
293 * has a colon that appears to the left of any equal signs, OR
294 * if the first line consists entirely of digits (an entry id)
296 use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
297 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
298 (( q = strchr( rbuf, '=' )) == NULL || p < q );
302 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
303 for ( p = rbuf; p < q; ++p ) {
304 if ( !isdigit( (unsigned char) *p )) {
315 rc = process_ldif_rec( start );
317 rc = process_ldapmod_rec( start );
321 fprintf( stderr, "%s() = %d\n",
322 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
336 process_ldif_rec( char *rbuf )
338 char *line, *dn, *type, *value, *newrdn, *p;
339 int rc, linenum, vlen, modop, replicaport;
340 int expect_modop, expect_sep, expect_ct, expect_newrdn;
341 int expect_deleteoldrdn, deleteoldrdn;
342 int saw_replica, use_record, new_entry, delete_entry, got_all;
347 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
348 expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
355 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
357 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
363 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
364 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
365 prog, linenum, dn == NULL ? "" : dn );
366 rc = LDAP_PARAM_ERROR;
371 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
373 if (( p = strchr( value, ':' )) == NULL ) {
377 replicaport = atoi( p );
379 if ( strcasecmp( value, ldaphost ) == 0 &&
380 replicaport == ldapport ) {
383 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
384 if (( dn = strdup( value )) == NULL ) {
390 continue; /* skip all lines until we see "dn:" */
395 if ( !use_record && saw_replica ) {
396 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
402 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
403 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
406 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
408 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
410 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
411 got_all = delete_entry = 1;
414 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
415 prog, T_CHANGETYPESTR, value, linenum, dn );
416 rc = LDAP_PARAM_ERROR;
419 } else if ( new ) { /* missing changetype => add */
421 modop = LDAP_MOD_ADD;
423 expect_modop = 1; /* missing changetype => modify */
427 if ( expect_modop ) {
430 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
431 modop = LDAP_MOD_ADD;
433 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
434 modop = LDAP_MOD_REPLACE;
436 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
437 modop = LDAP_MOD_DELETE;
438 addmodifyop( &pmods, modop, value, NULL, 0 );
440 } else { /* no modify op: use default */
441 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
445 if ( expect_newrdn ) {
446 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
447 if (( newrdn = strdup( value )) == NULL ) {
451 expect_deleteoldrdn = 1;
454 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
455 prog, T_NEWRDNSTR, type, linenum, dn );
456 rc = LDAP_PARAM_ERROR;
458 } else if ( expect_deleteoldrdn ) {
459 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
460 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
463 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
464 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
465 rc = LDAP_PARAM_ERROR;
467 } else if ( got_all ) {
469 "%s: extra lines at end (line %d of entry %s)\n",
471 rc = LDAP_PARAM_ERROR;
473 addmodifyop( &pmods, modop, type, value, vlen );
478 if ( delete_entry ) {
480 } else if ( newrdn != NULL ) {
481 rc = domodrdn( dn, newrdn, deleteoldrdn );
483 rc = domodify( dn, pmods, new_entry );
486 if ( rc == LDAP_SUCCESS ) {
494 if ( newrdn != NULL ) {
497 if ( pmods != NULL ) {
498 ldap_mods_free( pmods, 1 );
506 process_ldapmod_rec( char *rbuf )
508 char *line, *dn, *p, *q, *attr, *value;
509 int rc, linenum, modop;
518 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
520 if (( p = strchr( rbuf, '\n' )) == NULL ) {
523 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
524 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
532 if ( dn == NULL ) { /* first line contains DN */
533 if (( dn = strdup( line )) == NULL ) {
538 if (( p = strchr( line, '=' )) == NULL ) {
540 p = line + strlen( line );
547 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
548 ; /* skip attribute leading white space */
551 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
552 *q = '\0'; /* remove attribute trailing white space */
555 if ( value != NULL ) {
556 while ( isspace( (unsigned char) *value )) {
557 ++value; /* skip value leading white space */
559 for ( q = value + strlen( value ) - 1; q > value &&
560 isspace( (unsigned char) *q ); --q ) {
561 *q = '\0'; /* remove value trailing white space */
563 if ( *value == '\0' ) {
569 if ( value == NULL && new ) {
570 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
571 prog, linenum, attr );
572 rc = LDAP_PARAM_ERROR;
576 modop = LDAP_MOD_DELETE;
580 modop = LDAP_MOD_ADD;
584 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
587 addmodifyop( &pmods, modop, attr, value,
588 ( value == NULL ) ? 0 : strlen( value ));
597 rc = LDAP_PARAM_ERROR;
598 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
603 if ( pmods != NULL ) {
604 ldap_mods_free( pmods, 1 );
615 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
622 modop |= LDAP_MOD_BVALUES;
625 if ( pmods != NULL ) {
626 for ( ; pmods[ i ] != NULL; ++i ) {
627 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
628 pmods[ i ]->mod_op == modop ) {
634 if ( pmods == NULL || pmods[ i ] == NULL ) {
635 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
636 sizeof( LDAPMod * ))) == NULL ) {
641 pmods[ i + 1 ] = NULL;
642 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
647 pmods[ i ]->mod_op = modop;
648 if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
654 if ( value != NULL ) {
656 if ( pmods[ i ]->mod_bvalues != NULL ) {
657 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
661 if (( pmods[ i ]->mod_bvalues =
662 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
663 (j + 2) * sizeof( struct berval * ))) == NULL ) {
664 perror( "ber_realloc" );
667 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
668 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
670 perror( "ber_memalloc" );
673 pmods[ i ]->mod_bvalues[ j ] = bvp;
675 if ( valsfromfiles && *value == '/' ) { /* get value from file */
676 if ( fromfile( value, bvp ) < 0 ) {
681 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
685 SAFEMEMCPY( bvp->bv_val, value, vlen );
686 bvp->bv_val[ vlen ] = '\0';
693 domodify( char *dn, LDAPMod **pmods, int newentry )
695 int i, j, k, notascii, op;
698 if ( pmods == NULL ) {
699 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
701 return( LDAP_PARAM_ERROR );
705 for ( i = 0; pmods[ i ] != NULL; ++i ) {
706 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
707 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
708 "replace" : op == LDAP_MOD_ADD ?
709 "add" : "delete", pmods[ i ]->mod_type );
710 if ( pmods[ i ]->mod_bvalues != NULL ) {
711 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
712 bvp = pmods[ i ]->mod_bvalues[ j ];
714 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
715 if ( !isascii( bvp->bv_val[ k ] )) {
721 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
723 printf( "\t%s\n", bvp->bv_val );
731 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
733 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
738 i = ldap_add_s( ld, dn, pmods );
740 i = ldap_modify_s( ld, dn, pmods );
742 if ( i != LDAP_SUCCESS ) {
743 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
744 } else if ( verbose ) {
745 printf( "modify complete\n" );
762 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
764 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
765 ldap_perror( ld, "ldap_delete" );
766 } else if ( verbose ) {
767 printf( "delete complete" );
780 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
785 printf( "new RDN: %s (%skeep existing values)\n",
786 newrdn, deleteoldrdn ? "do not " : "" );
789 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
791 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
793 ldap_perror( ld, "ldap_modrdn" );
795 printf( "modrdn completed\n" );
808 fromfile( char *path, struct berval *bv )
814 if (( fp = fopen( path, "r" )) == NULL ) {
819 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
825 bv->bv_len = ftell( fp );
827 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
833 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
836 ber_memfree( bv->bv_val );
841 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
845 if ( (unsigned long) rlen != bv->bv_len ) {
847 ber_memfree( bv->bv_val );
852 return( bv->bv_len );
857 read_one_record( FILE *fp )
860 char *buf, line[ LDAPMOD_MAXLINE ];
866 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
867 (( len = strlen( line )) > 1 )) {
868 if ( lcur + len + 1 > lmax ) {
869 lmax = LDAPMOD_MAXLINE
870 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
871 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
876 strcpy( buf + lcur, line );