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] [-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"
82 " f - read from file\n"
83 " F - force all changes records to be used\n"
85 " n - print adds, don't actually do them\n"
87 " r - replace values\n"
90 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
96 main( int argc, char **argv )
98 char *infile, *rbuf, *start, *p, *q;
100 int rc, i, use_ldif, authmethod, version, want_bindpw, debug;
102 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
108 /* Print usage when no parameters */
112 new = ( strcmp( prog, "ldapadd" ) == 0 );
115 not = verbose = valsfromfiles = want_bindpw = debug = 0;
116 authmethod = LDAP_AUTH_SIMPLE;
119 while (( i = getopt( argc, argv, "WFabckKnrtvh:p:D:w:d:f:P:" )) != EOF ) {
124 case 'b': /* read values from files (for binary attributes) */
127 case 'c': /* continuous operation */
130 case 'r': /* default is to replace rather than add values */
133 case 'k': /* kerberos bind */
135 authmethod = LDAP_AUTH_KRBV4;
137 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
139 return( EXIT_FAILURE );
142 case 'K': /* kerberos bind, part 1 only */
144 authmethod = LDAP_AUTH_KRBV41;
146 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
148 return( EXIT_FAILURE );
151 case 'F': /* force all changes records to be used */
154 case 'h': /* ldap host */
155 ldaphost = strdup( optarg );
157 case 'D': /* bind DN */
158 binddn = strdup( optarg );
160 case 'w': /* password */
161 passwd = strdup( optarg );
165 for( p = optarg; *p == NULL; p++ ) {
171 debug |= atoi( optarg );
173 case 'f': /* read from file */
174 infile = strdup( optarg );
177 ldapport = atoi( optarg );
179 case 'n': /* print adds, don't actually do them */
182 case 'v': /* verbose mode */
189 switch( atoi(optarg) )
192 version = LDAP_VERSION2;
195 version = LDAP_VERSION3;
198 fprintf( stderr, "protocol version should be 2 or 3\n" );
207 if ( argc != optind )
210 if ( infile != NULL ) {
211 if (( fp = fopen( infile, "r" )) == NULL ) {
213 return( EXIT_FAILURE );
220 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
221 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
223 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
224 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
230 (void) SIGNAL( SIGPIPE, SIG_IGN );
234 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
235 perror( "ldap_init" );
236 return( EXIT_FAILURE );
239 /* this seems prudent */
241 int deref = LDAP_DEREF_NEVER;
242 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
246 passwd = getpass("Enter LDAP Password: ");
249 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
251 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
254 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
255 ldap_perror( ld, "ldap_bind" );
256 return( EXIT_FAILURE );
262 while (( rc == 0 || contoper ) &&
263 ( rbuf = read_one_record( fp )) != NULL ) {
265 * we assume record is ldif/slapd.replog if the first line
266 * has a colon that appears to the left of any equal signs, OR
267 * if the first line consists entirely of digits (an entry id)
269 use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
270 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
271 (( q = strchr( rbuf, '=' )) == NULL || p < q );
275 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
276 for ( p = rbuf; p < q; ++p ) {
277 if ( !isdigit( (unsigned char) *p )) {
288 rc = process_ldif_rec( start );
290 rc = process_ldapmod_rec( start );
294 fprintf( stderr, "%s() = %d\n",
295 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
309 process_ldif_rec( char *rbuf )
311 char *line, *dn, *type, *value, *newrdn, *p;
312 int rc, linenum, vlen, modop, replicaport;
313 int expect_modop, expect_sep, expect_ct, expect_newrdn;
314 int expect_deleteoldrdn, deleteoldrdn;
315 int saw_replica, use_record, new_entry, delete_entry, got_all;
320 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
321 expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
328 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
330 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
336 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
337 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
338 prog, linenum, dn == NULL ? "" : dn );
339 rc = LDAP_PARAM_ERROR;
344 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
346 if (( p = strchr( value, ':' )) == NULL ) {
350 replicaport = atoi( p );
352 if ( strcasecmp( value, ldaphost ) == 0 &&
353 replicaport == ldapport ) {
356 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
357 if (( dn = strdup( value )) == NULL ) {
363 continue; /* skip all lines until we see "dn:" */
368 if ( !use_record && saw_replica ) {
369 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
375 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
376 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
379 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
381 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
383 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
384 got_all = delete_entry = 1;
387 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
388 prog, T_CHANGETYPESTR, value, linenum, dn );
389 rc = LDAP_PARAM_ERROR;
392 } else if ( new ) { /* missing changetype => add */
394 modop = LDAP_MOD_ADD;
396 expect_modop = 1; /* missing changetype => modify */
400 if ( expect_modop ) {
403 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
404 modop = LDAP_MOD_ADD;
406 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
407 modop = LDAP_MOD_REPLACE;
409 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
410 modop = LDAP_MOD_DELETE;
411 addmodifyop( &pmods, modop, value, NULL, 0 );
413 } else { /* no modify op: use default */
414 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
418 if ( expect_newrdn ) {
419 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
420 if (( newrdn = strdup( value )) == NULL ) {
424 expect_deleteoldrdn = 1;
427 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
428 prog, T_NEWRDNSTR, type, linenum, dn );
429 rc = LDAP_PARAM_ERROR;
431 } else if ( expect_deleteoldrdn ) {
432 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
433 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
436 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
437 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
438 rc = LDAP_PARAM_ERROR;
440 } else if ( got_all ) {
442 "%s: extra lines at end (line %d of entry %s)\n",
444 rc = LDAP_PARAM_ERROR;
446 addmodifyop( &pmods, modop, type, value, vlen );
451 if ( delete_entry ) {
453 } else if ( newrdn != NULL ) {
454 rc = domodrdn( dn, newrdn, deleteoldrdn );
456 rc = domodify( dn, pmods, new_entry );
459 if ( rc == LDAP_SUCCESS ) {
467 if ( newrdn != NULL ) {
470 if ( pmods != NULL ) {
471 ldap_mods_free( pmods, 1 );
479 process_ldapmod_rec( char *rbuf )
481 char *line, *dn, *p, *q, *attr, *value;
482 int rc, linenum, modop;
491 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
493 if (( p = strchr( rbuf, '\n' )) == NULL ) {
496 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
497 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
505 if ( dn == NULL ) { /* first line contains DN */
506 if (( dn = strdup( line )) == NULL ) {
511 if (( p = strchr( line, '=' )) == NULL ) {
513 p = line + strlen( line );
520 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
521 ; /* skip attribute leading white space */
524 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
525 *q = '\0'; /* remove attribute trailing white space */
528 if ( value != NULL ) {
529 while ( isspace( (unsigned char) *value )) {
530 ++value; /* skip value leading white space */
532 for ( q = value + strlen( value ) - 1; q > value &&
533 isspace( (unsigned char) *q ); --q ) {
534 *q = '\0'; /* remove value trailing white space */
536 if ( *value == '\0' ) {
542 if ( value == NULL && new ) {
543 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
544 prog, linenum, attr );
545 rc = LDAP_PARAM_ERROR;
549 modop = LDAP_MOD_DELETE;
553 modop = LDAP_MOD_ADD;
557 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
560 addmodifyop( &pmods, modop, attr, value,
561 ( value == NULL ) ? 0 : strlen( value ));
570 rc = LDAP_PARAM_ERROR;
571 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
576 if ( pmods != NULL ) {
577 ldap_mods_free( pmods, 1 );
588 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
595 modop |= LDAP_MOD_BVALUES;
598 if ( pmods != NULL ) {
599 for ( ; pmods[ i ] != NULL; ++i ) {
600 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
601 pmods[ i ]->mod_op == modop ) {
607 if ( pmods == NULL || pmods[ i ] == NULL ) {
608 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
609 sizeof( LDAPMod * ))) == NULL ) {
614 pmods[ i + 1 ] = NULL;
615 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
620 pmods[ i ]->mod_op = modop;
621 if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
627 if ( value != NULL ) {
629 if ( pmods[ i ]->mod_bvalues != NULL ) {
630 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
634 if (( pmods[ i ]->mod_bvalues =
635 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
636 (j + 2) * sizeof( struct berval * ))) == NULL ) {
637 perror( "ber_realloc" );
640 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
641 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
643 perror( "ber_memalloc" );
646 pmods[ i ]->mod_bvalues[ j ] = bvp;
648 if ( valsfromfiles && *value == '/' ) { /* get value from file */
649 if ( fromfile( value, bvp ) < 0 ) {
654 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
658 SAFEMEMCPY( bvp->bv_val, value, vlen );
659 bvp->bv_val[ vlen ] = '\0';
666 domodify( char *dn, LDAPMod **pmods, int newentry )
668 int i, j, k, notascii, op;
671 if ( pmods == NULL ) {
672 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
674 return( LDAP_PARAM_ERROR );
678 for ( i = 0; pmods[ i ] != NULL; ++i ) {
679 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
680 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
681 "replace" : op == LDAP_MOD_ADD ?
682 "add" : "delete", pmods[ i ]->mod_type );
683 if ( pmods[ i ]->mod_bvalues != NULL ) {
684 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
685 bvp = pmods[ i ]->mod_bvalues[ j ];
687 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
688 if ( !isascii( bvp->bv_val[ k ] )) {
694 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
696 printf( "\t%s\n", bvp->bv_val );
704 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
706 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
711 i = ldap_add_s( ld, dn, pmods );
713 i = ldap_modify_s( ld, dn, pmods );
715 if ( i != LDAP_SUCCESS ) {
716 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
717 } else if ( verbose ) {
718 printf( "modify complete\n" );
735 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
737 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
738 ldap_perror( ld, "ldap_delete" );
739 } else if ( verbose ) {
740 printf( "delete complete" );
753 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
758 printf( "new RDN: %s (%skeep existing values)\n",
759 newrdn, deleteoldrdn ? "do not " : "" );
762 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
764 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
766 ldap_perror( ld, "ldap_modrdn" );
768 printf( "modrdn completed\n" );
781 fromfile( char *path, struct berval *bv )
787 if (( fp = fopen( path, "r" )) == NULL ) {
792 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
798 bv->bv_len = ftell( fp );
800 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
806 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
809 ber_memfree( bv->bv_val );
814 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
818 if ( (unsigned long) rlen != bv->bv_len ) {
820 ber_memfree( bv->bv_val );
825 return( bv->bv_len );
830 read_one_record( FILE *fp )
833 char *buf, line[ LDAPMOD_MAXLINE ];
839 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
840 (( len = strlen( line )) > 1 )) {
841 if ( lcur + len + 1 > lmax ) {
842 lmax = LDAPMOD_MAXLINE
843 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
844 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
849 strcpy( buf + lcur, line );