3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* ldapmodify.c - generic program to modify or add entries using LDAP */
12 #include <ac/stdlib.h>
15 #include <ac/signal.h>
16 #include <ac/string.h>
17 #include <ac/unistd.h>
21 #ifdef HAVE_SYS_FILE_H
32 #include "ldap_defaults.h"
35 static char *binddn = NULL;
36 static char *passwd = NULL;
37 static char *ldaphost = NULL;
38 static int ldapport = 0;
39 static int new, replace, not, verbose, contoper, force, valsfromfiles;
42 #define LDAPMOD_MAXLINE 4096
44 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
45 #define T_VERSION_STR "version"
46 #define T_REPLICA_STR "replica"
48 #define T_CHANGETYPESTR "changetype"
49 #define T_ADDCTSTR "add"
50 #define T_MODIFYCTSTR "modify"
51 #define T_DELETECTSTR "delete"
52 #define T_MODRDNCTSTR "modrdn"
53 #define T_MODDNCTSTR "moddn"
54 #define T_RENAMECTSTR "rename"
55 #define T_MODOPADDSTR "add"
56 #define T_MODOPREPLACESTR "replace"
57 #define T_MODOPDELETESTR "delete"
58 #define T_MODSEPSTR "-"
59 #define T_NEWRDNSTR "newrdn"
60 #define T_DELETEOLDRDNSTR "deleteoldrdn"
61 #define T_NEWSUPSTR "newsuperior"
64 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
65 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
66 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
67 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
68 char *value, int vlen ));
69 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
70 static int dodelete LDAP_P(( char *dn ));
71 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
72 static int fromfile LDAP_P(( char *path, struct berval *bv ));
73 static char *read_one_record LDAP_P(( FILE *fp ));
76 usage( const char *prog )
79 "Add or modify entries from an LDAP server\n\n"
80 "usage: %s [-abcknrvF] [-M[M]] [-d debug-level] [-P version] [-h ldaphost]\n"
81 " [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n"
82 " a - add values (default%s)\n"
83 " b - read values from files (for binary attributes)\n"
84 " c - continuous operation\n"
86 " M - enable Manage DSA IT control (-MM for critical)\n"
88 " f - read from file\n"
89 " F - force all changes records to be used\n"
91 " n - print adds, don't actually do them\n"
93 " r - replace values\n"
96 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
102 main( int argc, char **argv )
104 char *infile, *rbuf, *start, *p, *q;
106 int rc, i, use_ldif, authmethod, version, want_bindpw, debug, manageDSAit;
109 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
115 /* Print usage when no parameters */
119 new = ( strcmp( prog, "ldapadd" ) == 0 );
122 not = verbose = valsfromfiles = want_bindpw = debug = manageDSAit = 0;
123 authmethod = LDAP_AUTH_SIMPLE;
126 while (( i = getopt( argc, argv, "WFMabckKnrtvh:p:D:w:d:f:P:" )) != EOF ) {
131 case 'b': /* read values from files (for binary attributes) */
134 case 'c': /* continuous operation */
137 case 'r': /* default is to replace rather than add values */
140 case 'k': /* kerberos bind */
142 authmethod = LDAP_AUTH_KRBV4;
144 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
146 return( EXIT_FAILURE );
149 case 'K': /* kerberos bind, part 1 only */
151 authmethod = LDAP_AUTH_KRBV41;
153 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
155 return( EXIT_FAILURE );
158 case 'F': /* force all changes records to be used */
161 case 'h': /* ldap host */
162 ldaphost = strdup( optarg );
164 case 'D': /* bind DN */
165 binddn = strdup( optarg );
167 case 'w': /* password */
168 passwd = strdup( optarg );
172 for( p = optarg; *p == '\0'; p++ ) {
178 debug |= atoi( optarg );
180 case 'f': /* read from file */
181 infile = strdup( optarg );
184 ldapport = atoi( optarg );
186 case 'n': /* print adds, don't actually do them */
189 case 'v': /* verbose mode */
193 /* enable Manage DSA IT */
200 switch( atoi(optarg) )
203 version = LDAP_VERSION2;
206 version = LDAP_VERSION3;
209 fprintf( stderr, "protocol version should be 2 or 3\n" );
218 if ( argc != optind )
221 if ( infile != NULL ) {
222 if (( fp = fopen( infile, "r" )) == NULL ) {
224 return( EXIT_FAILURE );
231 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
232 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
234 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
235 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
241 (void) SIGNAL( SIGPIPE, SIG_IGN );
245 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
246 perror( "ldap_init" );
247 return( EXIT_FAILURE );
250 /* this seems prudent */
252 int deref = LDAP_DEREF_NEVER;
253 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
255 /* don't chase referrals */
256 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
259 passwd = getpass("Enter LDAP Password: ");
262 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
264 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION to %d\n", version );
267 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
268 ldap_perror( ld, "ldap_bind" );
269 return( EXIT_FAILURE );
278 LDAPControl *ctrls[2];
282 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
283 c.ldctl_value.bv_val = NULL;
284 c.ldctl_value.bv_len = 0;
285 c.ldctl_iscritical = manageDSAit > 1;
287 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
289 if( err != LDAP_OPT_SUCCESS ) {
290 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
291 if( c.ldctl_iscritical ) {
292 exit( EXIT_FAILURE );
298 while (( rc == 0 || contoper ) &&
299 ( rbuf = read_one_record( fp )) != NULL ) {
302 * we assume record is ldif/slapd.replog if the first line
303 * has a colon that appears to the left of any equal signs, OR
304 * if the first line consists entirely of digits (an entry id)
306 use_ldif = ( *rbuf == '#' ) ||
307 (( p = strchr( rbuf, ':' )) != NULL &&
308 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
309 (( q = strchr( rbuf, '=' )) == NULL || p < q ));
313 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
314 for ( p = rbuf; p < q; ++p ) {
315 if ( !isdigit( (unsigned char) *p )) {
326 rc = process_ldif_rec( start, count );
328 rc = process_ldapmod_rec( start );
332 fprintf( stderr, "%s() = %d\n",
333 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
347 process_ldif_rec( char *rbuf, int count )
349 char *line, *dn, *type, *value, *newrdn, *newsup, *p;
350 int rc, linenum, modop, replicaport;
352 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
353 int expect_deleteoldrdn, deleteoldrdn;
354 int saw_replica, use_record, new_entry, delete_entry, got_all;
360 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
361 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
362 expect_sep = expect_ct = 0;
368 dn = newrdn = newsup = NULL;
370 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
373 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
379 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
380 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
381 prog, linenum, dn == NULL ? "" : dn );
382 rc = LDAP_PARAM_ERROR;
387 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
389 if (( p = strchr( value, ':' )) == NULL ) {
393 replicaport = atoi( p );
395 if ( strcasecmp( value, ldaphost ) == 0 &&
396 replicaport == ldapport ) {
399 } else if ( count == 1 && linenum == 1 &&
400 strcasecmp( type, T_VERSION_STR ) == 0 )
402 if( vlen == 0 || atoi(value) != 1 ) {
403 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
404 prog, value == NULL ? "(null)" : value, linenum );
408 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
409 if (( dn = strdup( value ? value : "" )) == NULL ) {
411 exit( EXIT_FAILURE );
415 goto end_line; /* skip all lines until we see "dn:" */
420 if ( !use_record && saw_replica ) {
421 printf( "%s: skipping change record for entry: %s\n"
422 "\t(LDAP host/port does not match replica: lines)\n",
426 ber_memfree( value );
430 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
431 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
434 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
436 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0
437 || strcasecmp( value, T_MODDNCTSTR ) == 0
438 || strcasecmp( value, T_RENAMECTSTR ) == 0)
441 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
442 got_all = delete_entry = 1;
445 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
446 prog, T_CHANGETYPESTR, value, linenum, dn );
447 rc = LDAP_PARAM_ERROR;
450 } else if ( new ) { /* missing changetype => add */
452 modop = LDAP_MOD_ADD;
454 expect_modop = 1; /* missing changetype => modify */
458 if ( expect_modop ) {
461 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
462 modop = LDAP_MOD_ADD;
464 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
465 modop = LDAP_MOD_REPLACE;
467 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
468 modop = LDAP_MOD_DELETE;
469 addmodifyop( &pmods, modop, value, NULL, 0 );
471 } else { /* no modify op: use default */
472 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
476 if ( expect_newrdn ) {
477 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
478 if (( newrdn = strdup( value )) == NULL ) {
480 exit( EXIT_FAILURE );
482 expect_deleteoldrdn = 1;
485 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
486 prog, T_NEWRDNSTR, type, linenum, dn );
487 rc = LDAP_PARAM_ERROR;
489 } else if ( expect_deleteoldrdn ) {
490 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
491 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
492 expect_deleteoldrdn = 0;
496 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
497 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
498 rc = LDAP_PARAM_ERROR;
500 } else if ( expect_newsup ) {
501 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
502 if (( newsup = strdup( value )) == NULL ) {
504 exit( EXIT_FAILURE );
508 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
509 prog, T_NEWSUPSTR, type, linenum, dn );
510 rc = LDAP_PARAM_ERROR;
512 } else if ( got_all ) {
514 "%s: extra lines at end (line %d of entry \"%s\")\n",
516 rc = LDAP_PARAM_ERROR;
518 addmodifyop( &pmods, modop, type, value, vlen );
523 ber_memfree( value );
530 if( version && linenum == 1 ) {
535 if ( delete_entry ) {
537 } else if ( newrdn != NULL ) {
538 rc = domodrdn( dn, newrdn, deleteoldrdn );
540 rc = domodify( dn, pmods, new_entry );
543 if ( rc == LDAP_SUCCESS ) {
551 if ( newrdn != NULL ) {
554 if ( pmods != NULL ) {
555 ldap_mods_free( pmods, 1 );
563 process_ldapmod_rec( char *rbuf )
565 char *line, *dn, *p, *q, *attr, *value;
566 int rc, linenum, modop;
575 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
577 if (( p = strchr( rbuf, '\n' )) == NULL ) {
580 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
581 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
589 if ( dn == NULL ) { /* first line contains DN */
590 if (( dn = strdup( line )) == NULL ) {
592 exit( EXIT_FAILURE );
595 if (( p = strchr( line, '=' )) == NULL ) {
597 p = line + strlen( line );
604 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
605 ; /* skip attribute leading white space */
608 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
609 *q = '\0'; /* remove attribute trailing white space */
612 if ( value != NULL ) {
613 while ( isspace( (unsigned char) *value )) {
614 ++value; /* skip value leading white space */
616 for ( q = value + strlen( value ) - 1; q > value &&
617 isspace( (unsigned char) *q ); --q ) {
618 *q = '\0'; /* remove value trailing white space */
620 if ( *value == '\0' ) {
626 if ( value == NULL && new ) {
627 fprintf( stderr, "%s: missing value on line %d (attr=\"%s\")\n",
628 prog, linenum, attr );
629 rc = LDAP_PARAM_ERROR;
633 modop = LDAP_MOD_DELETE;
637 modop = LDAP_MOD_ADD;
641 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
644 addmodifyop( &pmods, modop, attr, value,
645 ( value == NULL ) ? 0 : strlen( value ));
654 rc = LDAP_PARAM_ERROR;
655 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
660 if ( pmods != NULL ) {
661 ldap_mods_free( pmods, 1 );
672 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
679 modop |= LDAP_MOD_BVALUES;
682 if ( pmods != NULL ) {
683 for ( ; pmods[ i ] != NULL; ++i ) {
684 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
685 pmods[ i ]->mod_op == modop ) {
691 if ( pmods == NULL || pmods[ i ] == NULL ) {
692 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
693 sizeof( LDAPMod * ))) == NULL ) {
695 exit( EXIT_FAILURE );
698 pmods[ i + 1 ] = NULL;
699 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
702 exit( EXIT_FAILURE );
704 pmods[ i ]->mod_op = modop;
705 if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
707 exit( EXIT_FAILURE );
711 if ( value != NULL ) {
713 if ( pmods[ i ]->mod_bvalues != NULL ) {
714 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
718 if (( pmods[ i ]->mod_bvalues =
719 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
720 (j + 2) * sizeof( struct berval * ))) == NULL ) {
721 perror( "ber_realloc" );
722 exit( EXIT_FAILURE );
724 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
725 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
727 perror( "ber_memalloc" );
728 exit( EXIT_FAILURE );
730 pmods[ i ]->mod_bvalues[ j ] = bvp;
732 if ( valsfromfiles && *value == '/' ) { /* get value from file */
733 if ( fromfile( value, bvp ) < 0 ) {
734 exit( EXIT_FAILURE );
738 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
740 exit( EXIT_FAILURE );
742 SAFEMEMCPY( bvp->bv_val, value, vlen );
743 bvp->bv_val[ vlen ] = '\0';
750 domodify( char *dn, LDAPMod **pmods, int newentry )
752 int i, j, k, notascii, op;
755 if ( pmods == NULL ) {
756 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
758 return( LDAP_PARAM_ERROR );
762 for ( i = 0; pmods[ i ] != NULL; ++i ) {
763 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
764 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
765 "replace" : op == LDAP_MOD_ADD ?
766 "add" : "delete", pmods[ i ]->mod_type );
767 if ( pmods[ i ]->mod_bvalues != NULL ) {
768 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
769 bvp = pmods[ i ]->mod_bvalues[ j ];
771 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
772 if ( !isascii( bvp->bv_val[ k ] )) {
778 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
780 printf( "\t%s\n", bvp->bv_val );
788 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
790 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
795 i = ldap_add_s( ld, dn, pmods );
797 i = ldap_modify_s( ld, dn, pmods );
799 if ( i != LDAP_SUCCESS ) {
800 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
801 } else if ( verbose ) {
802 printf( "modify complete\n" );
819 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
821 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
822 ldap_perror( ld, "ldap_delete" );
823 } else if ( verbose ) {
824 printf( "delete complete" );
837 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
842 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
844 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
845 newrdn, deleteoldrdn ? "do not " : "" );
848 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
850 ldap_perror( ld, "ldap_modrdn" );
852 printf( "modrdn completed\n" );
865 fromfile( char *path, struct berval *bv )
871 if (( fp = fopen( path, "r" )) == NULL ) {
876 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
882 bv->bv_len = ftell( fp );
884 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
890 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
893 ber_memfree( bv->bv_val );
898 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
902 if ( (unsigned long) rlen != bv->bv_len ) {
904 ber_memfree( bv->bv_val );
909 return( bv->bv_len );
914 read_one_record( FILE *fp )
916 char *buf, line[ LDAPMOD_MAXLINE ];
922 while ( fgets( line, sizeof(line), fp ) != NULL ) {
923 int len = strlen( line );
933 if ( lcur + len + 1 > lmax ) {
934 lmax = LDAPMOD_MAXLINE
935 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
937 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
939 exit( EXIT_FAILURE );
943 strcpy( buf + lcur, line );