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
31 static char *binddn = NULL;
32 static char *passwd = NULL;
33 static char *ldaphost = NULL;
34 static int ldapport = 0;
35 static int new, replace, not, verbose, contoper, force, valsfromfiles;
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"
56 static void usage LDAP_P(( const char *prog ));
57 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
58 static int process_ldif_rec LDAP_P(( char *rbuf ));
59 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
60 char *value, int vlen ));
61 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
62 static int dodelete LDAP_P(( char *dn ));
63 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
64 static void freepmods LDAP_P(( LDAPMod **pmods ));
65 static int fromfile LDAP_P(( char *path, struct berval *bv ));
66 static char *read_one_record LDAP_P(( FILE *fp ));
69 usage( const char *prog )
71 fprintf( stderr, "Add or modify entries from an LDAP server\n\n"
72 "usage: %s [-abcknrvF] [-d debug-level] [-P version] [-h ldaphost]\n"
73 " [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n"
74 " a - add values (default%s)\n"
75 " b - read values from files (for binary attributes)\n"
76 " c - continuous operation\n"
79 " f - read from file\n"
80 " F - force all changes records to be used\n"
82 " n - print adds, don't actually do them\n"
84 " r - replace values\n"
87 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
93 main( int argc, char **argv )
95 char *infile, *rbuf, *start, *p, *q;
97 int rc, i, use_ldif, authmethod, version, want_bindpw, debug;
99 if (( prog = strrchr( argv[ 0 ], *DIRSEP )) == NULL ) {
105 /* Print usage when no parameters */
109 new = ( strcmp( prog, "ldapadd" ) == 0 );
112 not = verbose = valsfromfiles = want_bindpw = debug = 0;
113 authmethod = LDAP_AUTH_SIMPLE;
116 while (( i = getopt( argc, argv, "WFabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
121 case 'b': /* read values from files (for binary attributes) */
124 case 'c': /* continuous operation */
127 case 'r': /* default is to replace rather than add values */
130 case 'k': /* kerberos bind */
132 authmethod = LDAP_AUTH_KRBV4;
134 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
136 return( EXIT_FAILURE );
139 case 'K': /* kerberos bind, part 1 only */
141 authmethod = LDAP_AUTH_KRBV41;
143 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
145 return( EXIT_FAILURE );
148 case 'F': /* force all changes records to be used */
151 case 'h': /* ldap host */
152 ldaphost = strdup( optarg );
154 case 'D': /* bind DN */
155 binddn = strdup( optarg );
157 case 'w': /* password */
158 passwd = strdup( optarg );
161 debug |= atoi( optarg );
163 case 'f': /* read from file */
164 infile = strdup( optarg );
167 ldapport = atoi( optarg );
169 case 'n': /* print adds, don't actually do them */
172 case 'v': /* verbose mode */
179 switch( atoi(optarg) )
182 version = LDAP_VERSION2;
185 version = LDAP_VERSION3;
188 fprintf( stderr, "protocol version should be 2 or 3\n" );
197 if ( argc != optind )
200 if ( infile != NULL ) {
201 if (( fp = fopen( infile, "r" )) == NULL ) {
203 return( EXIT_FAILURE );
210 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
211 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
213 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
214 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
220 (void) SIGNAL( SIGPIPE, SIG_IGN );
224 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
225 perror( "ldap_init" );
226 return( EXIT_FAILURE );
229 /* this seems prudent */
231 int deref = LDAP_DEREF_NEVER;
232 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
236 passwd = getpass("Enter LDAP Password: ");
239 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
241 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
244 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
245 ldap_perror( ld, "ldap_bind" );
246 return( EXIT_FAILURE );
252 while (( rc == 0 || contoper ) &&
253 ( rbuf = read_one_record( fp )) != NULL ) {
255 * we assume record is ldif/slapd.replog if the first line
256 * has a colon that appears to the left of any equal signs, OR
257 * if the first line consists entirely of digits (an entry id)
259 use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
260 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
261 (( q = strchr( rbuf, '=' )) == NULL || p < q );
265 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
266 for ( p = rbuf; p < q; ++p ) {
267 if ( !isdigit( (unsigned char) *p )) {
278 rc = process_ldif_rec( start );
280 rc = process_ldapmod_rec( start );
284 fprintf( stderr, "%s() = %d\n",
285 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
299 process_ldif_rec( char *rbuf )
301 char *line, *dn, *type, *value, *newrdn, *p;
302 int rc, linenum, vlen, modop, replicaport;
303 int expect_modop, expect_sep, expect_ct, expect_newrdn;
304 int expect_deleteoldrdn, deleteoldrdn;
305 int saw_replica, use_record, new_entry, delete_entry, got_all;
310 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
311 expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
318 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
320 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
326 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
327 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
328 prog, linenum, dn == NULL ? "" : dn );
329 rc = LDAP_PARAM_ERROR;
334 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
336 if (( p = strchr( value, ':' )) == NULL ) {
340 replicaport = atoi( p );
342 if ( strcasecmp( value, ldaphost ) == 0 &&
343 replicaport == ldapport ) {
346 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
347 if (( dn = strdup( value )) == NULL ) {
353 continue; /* skip all lines until we see "dn:" */
358 if ( !use_record && saw_replica ) {
359 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
365 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
366 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
369 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
371 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
373 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
374 got_all = delete_entry = 1;
377 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
378 prog, T_CHANGETYPESTR, value, linenum, dn );
379 rc = LDAP_PARAM_ERROR;
382 } else if ( new ) { /* missing changetype => add */
384 modop = LDAP_MOD_ADD;
386 expect_modop = 1; /* missing changetype => modify */
390 if ( expect_modop ) {
393 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
394 modop = LDAP_MOD_ADD;
396 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
397 modop = LDAP_MOD_REPLACE;
399 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
400 modop = LDAP_MOD_DELETE;
401 addmodifyop( &pmods, modop, value, NULL, 0 );
403 } else { /* no modify op: use default */
404 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
408 if ( expect_newrdn ) {
409 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
410 if (( newrdn = strdup( value )) == NULL ) {
414 expect_deleteoldrdn = 1;
417 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
418 prog, T_NEWRDNSTR, type, linenum, dn );
419 rc = LDAP_PARAM_ERROR;
421 } else if ( expect_deleteoldrdn ) {
422 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
423 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
426 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
427 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
428 rc = LDAP_PARAM_ERROR;
430 } else if ( got_all ) {
432 "%s: extra lines at end (line %d of entry %s)\n",
434 rc = LDAP_PARAM_ERROR;
436 addmodifyop( &pmods, modop, type, value, vlen );
441 if ( delete_entry ) {
443 } else if ( newrdn != NULL ) {
444 rc = domodrdn( dn, newrdn, deleteoldrdn );
446 rc = domodify( dn, pmods, new_entry );
449 if ( rc == LDAP_SUCCESS ) {
457 if ( newrdn != NULL ) {
460 if ( pmods != NULL ) {
469 process_ldapmod_rec( char *rbuf )
471 char *line, *dn, *p, *q, *attr, *value;
472 int rc, linenum, modop;
481 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
483 if (( p = strchr( rbuf, '\n' )) == NULL ) {
486 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
487 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
495 if ( dn == NULL ) { /* first line contains DN */
496 if (( dn = strdup( line )) == NULL ) {
501 if (( p = strchr( line, '=' )) == NULL ) {
503 p = line + strlen( line );
510 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
511 ; /* skip attribute leading white space */
514 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
515 *q = '\0'; /* remove attribute trailing white space */
518 if ( value != NULL ) {
519 while ( isspace( (unsigned char) *value )) {
520 ++value; /* skip value leading white space */
522 for ( q = value + strlen( value ) - 1; q > value &&
523 isspace( (unsigned char) *q ); --q ) {
524 *q = '\0'; /* remove value trailing white space */
526 if ( *value == '\0' ) {
532 if ( value == NULL && new ) {
533 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
534 prog, linenum, attr );
535 rc = LDAP_PARAM_ERROR;
539 modop = LDAP_MOD_DELETE;
543 modop = LDAP_MOD_ADD;
547 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
550 addmodifyop( &pmods, modop, attr, value,
551 ( value == NULL ) ? 0 : strlen( value ));
560 rc = LDAP_PARAM_ERROR;
561 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
566 if ( pmods != NULL ) {
578 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
585 modop |= LDAP_MOD_BVALUES;
588 if ( pmods != NULL ) {
589 for ( ; pmods[ i ] != NULL; ++i ) {
590 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
591 pmods[ i ]->mod_op == modop ) {
597 if ( pmods == NULL || pmods[ i ] == NULL ) {
598 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
599 sizeof( LDAPMod * ))) == NULL ) {
604 pmods[ i + 1 ] = NULL;
605 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
610 pmods[ i ]->mod_op = modop;
611 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
617 if ( value != NULL ) {
619 if ( pmods[ i ]->mod_bvalues != NULL ) {
620 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
624 if (( pmods[ i ]->mod_bvalues =
625 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
626 (j + 2) * sizeof( struct berval * ))) == NULL ) {
627 perror( "ber_realloc" );
630 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
631 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
633 perror( "ber_memalloc" );
636 pmods[ i ]->mod_bvalues[ j ] = bvp;
638 if ( valsfromfiles && *value == '/' ) { /* get value from file */
639 if ( fromfile( value, bvp ) < 0 ) {
644 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
648 SAFEMEMCPY( bvp->bv_val, value, vlen );
649 bvp->bv_val[ vlen ] = '\0';
656 domodify( char *dn, LDAPMod **pmods, int newentry )
658 int i, j, k, notascii, op;
661 if ( pmods == NULL ) {
662 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
664 return( LDAP_PARAM_ERROR );
668 for ( i = 0; pmods[ i ] != NULL; ++i ) {
669 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
670 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
671 "replace" : op == LDAP_MOD_ADD ?
672 "add" : "delete", pmods[ i ]->mod_type );
673 if ( pmods[ i ]->mod_bvalues != NULL ) {
674 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
675 bvp = pmods[ i ]->mod_bvalues[ j ];
677 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
678 if ( !isascii( bvp->bv_val[ k ] )) {
684 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
686 printf( "\t%s\n", bvp->bv_val );
694 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
696 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
701 i = ldap_add_s( ld, dn, pmods );
703 i = ldap_modify_s( ld, dn, pmods );
705 if ( i != LDAP_SUCCESS ) {
706 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
707 } else if ( verbose ) {
708 printf( "modify complete\n" );
725 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
727 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
728 ldap_perror( ld, "ldap_delete" );
729 } else if ( verbose ) {
730 printf( "delete complete" );
743 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
748 printf( "new RDN: %s (%skeep existing values)\n",
749 newrdn, deleteoldrdn ? "do not " : "" );
752 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
754 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
756 ldap_perror( ld, "ldap_modrdn" );
758 printf( "modrdn completed\n" );
772 freepmods( LDAPMod **pmods )
776 for ( i = 0; pmods[ i ] != NULL; ++i ) {
777 if ( pmods[ i ]->mod_bvalues != NULL ) {
778 ber_bvecfree( pmods[ i ]->mod_bvalues );
780 if ( pmods[ i ]->mod_type != NULL ) {
781 free( pmods[ i ]->mod_type );
790 fromfile( char *path, struct berval *bv )
796 if (( fp = fopen( path, "r" )) == NULL ) {
801 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
807 bv->bv_len = ftell( fp );
809 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
815 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
821 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
825 if ( (unsigned long) rlen != bv->bv_len ) {
831 return( bv->bv_len );
836 read_one_record( FILE *fp )
839 char *buf, line[ LDAPMOD_MAXLINE ];
845 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
846 (( len = strlen( line )) > 1 )) {
847 if ( lcur + len + 1 > lmax ) {
848 lmax = LDAPMOD_MAXLINE
849 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
850 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
855 strcpy( buf + lcur, line );