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_MODDNCTSTR "moddn"
52 #define T_RENAMECTSTR "rename"
53 #define T_MODOPADDSTR "add"
54 #define T_MODOPREPLACESTR "replace"
55 #define T_MODOPDELETESTR "delete"
56 #define T_MODSEPSTR "-"
57 #define T_NEWRDNSTR "newrdn"
58 #define T_DELETEOLDRDNSTR "deleteoldrdn"
59 #define T_NEWSUPSTR "newsuperior"
62 static void usage LDAP_P(( const char *prog ));
63 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
64 static int process_ldif_rec LDAP_P(( char *rbuf ));
65 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
66 char *value, int vlen ));
67 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
68 static int dodelete LDAP_P(( char *dn ));
69 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
70 static int fromfile LDAP_P(( char *path, struct berval *bv ));
71 static char *read_one_record LDAP_P(( FILE *fp ));
74 usage( const char *prog )
77 "Add or modify entries from an LDAP server\n\n"
78 "usage: %s [-abcknrvF] [-M[M]] [-d debug-level] [-P version] [-h ldaphost]\n"
79 " [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n"
80 " a - add values (default%s)\n"
81 " b - read values from files (for binary attributes)\n"
82 " c - continuous operation\n"
84 " M - enable Manage DSA IT control (-MM for critical)\n"
86 " f - read from file\n"
87 " F - force all changes records to be used\n"
89 " n - print adds, don't actually do them\n"
91 " r - replace values\n"
94 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
100 main( int argc, char **argv )
102 char *infile, *rbuf, *start, *p, *q;
104 int rc, i, use_ldif, authmethod, version, want_bindpw, debug, manageDSAit;
106 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
112 /* Print usage when no parameters */
116 new = ( strcmp( prog, "ldapadd" ) == 0 );
119 not = verbose = valsfromfiles = want_bindpw = debug = manageDSAit = 0;
120 authmethod = LDAP_AUTH_SIMPLE;
123 while (( i = getopt( argc, argv, "WFMabckKnrtvh:p:D:w:d:f:P:" )) != EOF ) {
128 case 'b': /* read values from files (for binary attributes) */
131 case 'c': /* continuous operation */
134 case 'r': /* default is to replace rather than add values */
137 case 'k': /* kerberos bind */
139 authmethod = LDAP_AUTH_KRBV4;
141 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
143 return( EXIT_FAILURE );
146 case 'K': /* kerberos bind, part 1 only */
148 authmethod = LDAP_AUTH_KRBV41;
150 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
152 return( EXIT_FAILURE );
155 case 'F': /* force all changes records to be used */
158 case 'h': /* ldap host */
159 ldaphost = strdup( optarg );
161 case 'D': /* bind DN */
162 binddn = strdup( optarg );
164 case 'w': /* password */
165 passwd = strdup( optarg );
169 for( p = optarg; *p == '\0'; p++ ) {
175 debug |= atoi( optarg );
177 case 'f': /* read from file */
178 infile = strdup( optarg );
181 ldapport = atoi( optarg );
183 case 'n': /* print adds, don't actually do them */
186 case 'v': /* verbose mode */
190 /* enable Manage DSA IT */
197 switch( atoi(optarg) )
200 version = LDAP_VERSION2;
203 version = LDAP_VERSION3;
206 fprintf( stderr, "protocol version should be 2 or 3\n" );
215 if ( argc != optind )
218 if ( infile != NULL ) {
219 if (( fp = fopen( infile, "r" )) == NULL ) {
221 return( EXIT_FAILURE );
228 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
229 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
231 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
232 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
238 (void) SIGNAL( SIGPIPE, SIG_IGN );
242 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
243 perror( "ldap_init" );
244 return( EXIT_FAILURE );
247 /* this seems prudent */
249 int deref = LDAP_DEREF_NEVER;
250 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
254 passwd = getpass("Enter LDAP Password: ");
257 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
259 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
262 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
263 ldap_perror( ld, "ldap_bind" );
264 return( EXIT_FAILURE );
273 LDAPControl *ctrls[2];
277 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
278 c.ldctl_value.bv_val = NULL;
279 c.ldctl_value.bv_len = 0;
280 c.ldctl_iscritical = manageDSAit > 1;
282 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
284 if( err != LDAP_OPT_SUCCESS ) {
285 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
286 if( c.ldctl_iscritical ) {
287 exit( EXIT_FAILURE );
292 while (( rc == 0 || contoper ) &&
293 ( rbuf = read_one_record( fp )) != NULL ) {
295 * we assume record is ldif/slapd.replog if the first line
296 * has a colon that appears to the left of any equal signs, OR
297 * if the first line consists entirely of digits (an entry id)
299 use_ldif = ( *rbuf == '#' ) ||
300 (( p = strchr( rbuf, ':' )) != NULL &&
301 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
302 (( q = strchr( rbuf, '=' )) == NULL || p < q ));
306 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
307 for ( p = rbuf; p < q; ++p ) {
308 if ( !isdigit( (unsigned char) *p )) {
319 rc = process_ldif_rec( start );
321 rc = process_ldapmod_rec( start );
325 fprintf( stderr, "%s() = %d\n",
326 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
340 process_ldif_rec( char *rbuf )
342 char *line, *dn, *type, *value, *newrdn, *newsup, *p;
343 int rc, linenum, vlen, modop, replicaport;
344 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
345 int expect_deleteoldrdn, deleteoldrdn;
346 int saw_replica, use_record, new_entry, delete_entry, got_all;
351 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
352 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
353 expect_sep = expect_ct = 0;
358 dn = newrdn = newsup = NULL;
360 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
362 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
368 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
369 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
370 prog, linenum, dn == NULL ? "" : dn );
371 rc = LDAP_PARAM_ERROR;
376 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
378 if (( p = strchr( value, ':' )) == NULL ) {
382 replicaport = atoi( p );
384 if ( strcasecmp( value, ldaphost ) == 0 &&
385 replicaport == ldapport ) {
388 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
389 if (( dn = strdup( value )) == NULL ) {
395 continue; /* skip all lines until we see "dn:" */
400 if ( !use_record && saw_replica ) {
401 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
407 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
408 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
411 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
413 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0
414 || strcasecmp( value, T_MODDNCTSTR ) == 0
415 || strcasecmp( value, T_RENAMECTSTR ) == 0)
418 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
419 got_all = delete_entry = 1;
422 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
423 prog, T_CHANGETYPESTR, value, linenum, dn );
424 rc = LDAP_PARAM_ERROR;
427 } else if ( new ) { /* missing changetype => add */
429 modop = LDAP_MOD_ADD;
431 expect_modop = 1; /* missing changetype => modify */
435 if ( expect_modop ) {
438 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
439 modop = LDAP_MOD_ADD;
441 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
442 modop = LDAP_MOD_REPLACE;
444 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
445 modop = LDAP_MOD_DELETE;
446 addmodifyop( &pmods, modop, value, NULL, 0 );
448 } else { /* no modify op: use default */
449 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
453 if ( expect_newrdn ) {
454 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
455 if (( newrdn = strdup( value )) == NULL ) {
459 expect_deleteoldrdn = 1;
462 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
463 prog, T_NEWRDNSTR, type, linenum, dn );
464 rc = LDAP_PARAM_ERROR;
466 } else if ( expect_deleteoldrdn ) {
467 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
468 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
469 expect_deleteoldrdn = 0;
473 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
474 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
475 rc = LDAP_PARAM_ERROR;
477 } else if ( expect_newsup ) {
478 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
479 if (( newsup = strdup( value )) == NULL ) {
485 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
486 prog, T_NEWSUPSTR, type, linenum, dn );
487 rc = LDAP_PARAM_ERROR;
489 } else if ( got_all ) {
491 "%s: extra lines at end (line %d of entry %s)\n",
493 rc = LDAP_PARAM_ERROR;
495 addmodifyop( &pmods, modop, type, value, vlen );
504 if ( delete_entry ) {
506 } else if ( newrdn != NULL ) {
507 rc = domodrdn( dn, newrdn, deleteoldrdn );
509 rc = domodify( dn, pmods, new_entry );
512 if ( rc == LDAP_SUCCESS ) {
520 if ( newrdn != NULL ) {
523 if ( pmods != NULL ) {
524 ldap_mods_free( pmods, 1 );
532 process_ldapmod_rec( char *rbuf )
534 char *line, *dn, *p, *q, *attr, *value;
535 int rc, linenum, modop;
544 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
546 if (( p = strchr( rbuf, '\n' )) == NULL ) {
549 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
550 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
558 if ( dn == NULL ) { /* first line contains DN */
559 if (( dn = strdup( line )) == NULL ) {
564 if (( p = strchr( line, '=' )) == NULL ) {
566 p = line + strlen( line );
573 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
574 ; /* skip attribute leading white space */
577 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
578 *q = '\0'; /* remove attribute trailing white space */
581 if ( value != NULL ) {
582 while ( isspace( (unsigned char) *value )) {
583 ++value; /* skip value leading white space */
585 for ( q = value + strlen( value ) - 1; q > value &&
586 isspace( (unsigned char) *q ); --q ) {
587 *q = '\0'; /* remove value trailing white space */
589 if ( *value == '\0' ) {
595 if ( value == NULL && new ) {
596 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
597 prog, linenum, attr );
598 rc = LDAP_PARAM_ERROR;
602 modop = LDAP_MOD_DELETE;
606 modop = LDAP_MOD_ADD;
610 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
613 addmodifyop( &pmods, modop, attr, value,
614 ( value == NULL ) ? 0 : strlen( value ));
623 rc = LDAP_PARAM_ERROR;
624 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
629 if ( pmods != NULL ) {
630 ldap_mods_free( pmods, 1 );
641 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
648 modop |= LDAP_MOD_BVALUES;
651 if ( pmods != NULL ) {
652 for ( ; pmods[ i ] != NULL; ++i ) {
653 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
654 pmods[ i ]->mod_op == modop ) {
660 if ( pmods == NULL || pmods[ i ] == NULL ) {
661 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
662 sizeof( LDAPMod * ))) == NULL ) {
667 pmods[ i + 1 ] = NULL;
668 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
673 pmods[ i ]->mod_op = modop;
674 if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
680 if ( value != NULL ) {
682 if ( pmods[ i ]->mod_bvalues != NULL ) {
683 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
687 if (( pmods[ i ]->mod_bvalues =
688 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
689 (j + 2) * sizeof( struct berval * ))) == NULL ) {
690 perror( "ber_realloc" );
693 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
694 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
696 perror( "ber_memalloc" );
699 pmods[ i ]->mod_bvalues[ j ] = bvp;
701 if ( valsfromfiles && *value == '/' ) { /* get value from file */
702 if ( fromfile( value, bvp ) < 0 ) {
707 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
711 SAFEMEMCPY( bvp->bv_val, value, vlen );
712 bvp->bv_val[ vlen ] = '\0';
719 domodify( char *dn, LDAPMod **pmods, int newentry )
721 int i, j, k, notascii, op;
724 if ( pmods == NULL ) {
725 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
727 return( LDAP_PARAM_ERROR );
731 for ( i = 0; pmods[ i ] != NULL; ++i ) {
732 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
733 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
734 "replace" : op == LDAP_MOD_ADD ?
735 "add" : "delete", pmods[ i ]->mod_type );
736 if ( pmods[ i ]->mod_bvalues != NULL ) {
737 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
738 bvp = pmods[ i ]->mod_bvalues[ j ];
740 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
741 if ( !isascii( bvp->bv_val[ k ] )) {
747 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
749 printf( "\t%s\n", bvp->bv_val );
757 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
759 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
764 i = ldap_add_s( ld, dn, pmods );
766 i = ldap_modify_s( ld, dn, pmods );
768 if ( i != LDAP_SUCCESS ) {
769 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
770 } else if ( verbose ) {
771 printf( "modify complete\n" );
788 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
790 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
791 ldap_perror( ld, "ldap_delete" );
792 } else if ( verbose ) {
793 printf( "delete complete" );
806 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
811 printf( "new RDN: %s (%skeep existing values)\n",
812 newrdn, deleteoldrdn ? "do not " : "" );
815 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
817 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
819 ldap_perror( ld, "ldap_modrdn" );
821 printf( "modrdn completed\n" );
834 fromfile( char *path, struct berval *bv )
840 if (( fp = fopen( path, "r" )) == NULL ) {
845 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
851 bv->bv_len = ftell( fp );
853 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
859 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
862 ber_memfree( bv->bv_val );
867 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
871 if ( (unsigned long) rlen != bv->bv_len ) {
873 ber_memfree( bv->bv_val );
878 return( bv->bv_len );
883 read_one_record( FILE *fp )
885 char *buf, line[ LDAPMOD_MAXLINE ];
891 while ( fgets( line, sizeof(line), fp ) != NULL ) {
892 int len = strlen( line );
902 if ( lcur + len + 1 > lmax ) {
903 lmax = LDAPMOD_MAXLINE
904 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
906 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
912 strcpy( buf + lcur, line );