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_VERSION_STR "version"
45 #define T_REPLICA_STR "replica"
47 #define T_CHANGETYPESTR "changetype"
48 #define T_ADDCTSTR "add"
49 #define T_MODIFYCTSTR "modify"
50 #define T_DELETECTSTR "delete"
51 #define T_MODRDNCTSTR "modrdn"
52 #define T_MODDNCTSTR "moddn"
53 #define T_RENAMECTSTR "rename"
54 #define T_MODOPADDSTR "add"
55 #define T_MODOPREPLACESTR "replace"
56 #define T_MODOPDELETESTR "delete"
57 #define T_MODSEPSTR "-"
58 #define T_NEWRDNSTR "newrdn"
59 #define T_DELETEOLDRDNSTR "deleteoldrdn"
60 #define T_NEWSUPSTR "newsuperior"
63 static void usage LDAP_P(( const char *prog ));
64 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
65 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
66 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
67 char *value, int vlen ));
68 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
69 static int dodelete LDAP_P(( char *dn ));
70 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
71 static int fromfile LDAP_P(( char *path, struct berval *bv ));
72 static char *read_one_record LDAP_P(( FILE *fp ));
75 usage( const char *prog )
78 "Add or modify entries from an LDAP server\n\n"
79 "usage: %s [-abcknrvF] [-M[M]] [-d debug-level] [-P version] [-h ldaphost]\n"
80 " [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n"
81 " a - add values (default%s)\n"
82 " b - read values from files (for binary attributes)\n"
83 " c - continuous operation\n"
85 " M - enable Manage DSA IT control (-MM for critical)\n"
87 " f - read from file\n"
88 " F - force all changes records to be used\n"
90 " n - print adds, don't actually do them\n"
92 " r - replace values\n"
95 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
101 main( int argc, char **argv )
103 char *infile, *rbuf, *start, *p, *q;
105 int rc, i, use_ldif, authmethod, version, want_bindpw, debug, manageDSAit;
108 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
114 /* Print usage when no parameters */
118 new = ( strcmp( prog, "ldapadd" ) == 0 );
121 not = verbose = valsfromfiles = want_bindpw = debug = manageDSAit = 0;
122 authmethod = LDAP_AUTH_SIMPLE;
125 while (( i = getopt( argc, argv, "WFMabckKnrtvh:p:D:w:d:f:P:" )) != EOF ) {
130 case 'b': /* read values from files (for binary attributes) */
133 case 'c': /* continuous operation */
136 case 'r': /* default is to replace rather than add values */
139 case 'k': /* kerberos bind */
141 authmethod = LDAP_AUTH_KRBV4;
143 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
145 return( EXIT_FAILURE );
148 case 'K': /* kerberos bind, part 1 only */
150 authmethod = LDAP_AUTH_KRBV41;
152 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
154 return( EXIT_FAILURE );
157 case 'F': /* force all changes records to be used */
160 case 'h': /* ldap host */
161 ldaphost = strdup( optarg );
163 case 'D': /* bind DN */
164 binddn = strdup( optarg );
166 case 'w': /* password */
167 passwd = strdup( optarg );
171 for( p = optarg; *p == '\0'; p++ ) {
177 debug |= atoi( optarg );
179 case 'f': /* read from file */
180 infile = strdup( optarg );
183 ldapport = atoi( optarg );
185 case 'n': /* print adds, don't actually do them */
188 case 'v': /* verbose mode */
192 /* enable Manage DSA IT */
199 switch( atoi(optarg) )
202 version = LDAP_VERSION2;
205 version = LDAP_VERSION3;
208 fprintf( stderr, "protocol version should be 2 or 3\n" );
217 if ( argc != optind )
220 if ( infile != NULL ) {
221 if (( fp = fopen( infile, "r" )) == NULL ) {
223 return( EXIT_FAILURE );
230 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
231 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
233 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
234 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
240 (void) SIGNAL( SIGPIPE, SIG_IGN );
244 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
245 perror( "ldap_init" );
246 return( EXIT_FAILURE );
249 /* this seems prudent */
251 int deref = LDAP_DEREF_NEVER;
252 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
256 passwd = getpass("Enter LDAP Password: ");
259 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
261 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
264 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
265 ldap_perror( ld, "ldap_bind" );
266 return( EXIT_FAILURE );
275 LDAPControl *ctrls[2];
279 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
280 c.ldctl_value.bv_val = NULL;
281 c.ldctl_value.bv_len = 0;
282 c.ldctl_iscritical = manageDSAit > 1;
284 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
286 if( err != LDAP_OPT_SUCCESS ) {
287 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
288 if( c.ldctl_iscritical ) {
289 exit( EXIT_FAILURE );
295 while (( rc == 0 || contoper ) &&
296 ( rbuf = read_one_record( fp )) != NULL ) {
299 * we assume record is ldif/slapd.replog if the first line
300 * has a colon that appears to the left of any equal signs, OR
301 * if the first line consists entirely of digits (an entry id)
303 use_ldif = ( *rbuf == '#' ) ||
304 (( p = strchr( rbuf, ':' )) != NULL &&
305 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
306 (( q = strchr( rbuf, '=' )) == NULL || p < q ));
310 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
311 for ( p = rbuf; p < q; ++p ) {
312 if ( !isdigit( (unsigned char) *p )) {
323 rc = process_ldif_rec( start, count );
325 rc = process_ldapmod_rec( start );
329 fprintf( stderr, "%s() = %d\n",
330 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
344 process_ldif_rec( char *rbuf, int count )
346 char *line, *dn, *type, *value, *newrdn, *newsup, *p;
347 int rc, linenum, modop, replicaport;
349 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
350 int expect_deleteoldrdn, deleteoldrdn;
351 int saw_replica, use_record, new_entry, delete_entry, got_all;
357 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
358 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
359 expect_sep = expect_ct = 0;
365 dn = newrdn = newsup = NULL;
367 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
370 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
376 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
377 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
378 prog, linenum, dn == NULL ? "" : dn );
379 rc = LDAP_PARAM_ERROR;
384 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
386 if (( p = strchr( value, ':' )) == NULL ) {
390 replicaport = atoi( p );
392 if ( strcasecmp( value, ldaphost ) == 0 &&
393 replicaport == ldapport ) {
396 } else if ( count == 1 && linenum == 1 &&
397 strcasecmp( type, T_VERSION_STR ) == 0 )
399 if( vlen == 0 || atoi(value) != 1 ) {
400 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
401 prog, value == NULL ? "(null)" : value, linenum );
405 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
406 if (( dn = strdup( value )) == NULL ) {
412 continue; /* skip all lines until we see "dn:" */
417 if ( !use_record && saw_replica ) {
418 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
424 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
425 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
428 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
430 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0
431 || strcasecmp( value, T_MODDNCTSTR ) == 0
432 || strcasecmp( value, T_RENAMECTSTR ) == 0)
435 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
436 got_all = delete_entry = 1;
439 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
440 prog, T_CHANGETYPESTR, value, linenum, dn );
441 rc = LDAP_PARAM_ERROR;
444 } else if ( new ) { /* missing changetype => add */
446 modop = LDAP_MOD_ADD;
448 expect_modop = 1; /* missing changetype => modify */
452 if ( expect_modop ) {
455 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
456 modop = LDAP_MOD_ADD;
458 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
459 modop = LDAP_MOD_REPLACE;
461 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
462 modop = LDAP_MOD_DELETE;
463 addmodifyop( &pmods, modop, value, NULL, 0 );
465 } else { /* no modify op: use default */
466 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
470 if ( expect_newrdn ) {
471 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
472 if (( newrdn = strdup( value )) == NULL ) {
476 expect_deleteoldrdn = 1;
479 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
480 prog, T_NEWRDNSTR, type, linenum, dn );
481 rc = LDAP_PARAM_ERROR;
483 } else if ( expect_deleteoldrdn ) {
484 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
485 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
486 expect_deleteoldrdn = 0;
490 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
491 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
492 rc = LDAP_PARAM_ERROR;
494 } else if ( expect_newsup ) {
495 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
496 if (( newsup = strdup( value )) == NULL ) {
502 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
503 prog, T_NEWSUPSTR, type, linenum, dn );
504 rc = LDAP_PARAM_ERROR;
506 } else if ( got_all ) {
508 "%s: extra lines at end (line %d of entry %s)\n",
510 rc = LDAP_PARAM_ERROR;
512 addmodifyop( &pmods, modop, type, value, vlen );
520 if( version && linenum == 1 ) {
525 if ( delete_entry ) {
527 } else if ( newrdn != NULL ) {
528 rc = domodrdn( dn, newrdn, deleteoldrdn );
530 rc = domodify( dn, pmods, new_entry );
533 if ( rc == LDAP_SUCCESS ) {
541 if ( newrdn != NULL ) {
544 if ( pmods != NULL ) {
545 ldap_mods_free( pmods, 1 );
553 process_ldapmod_rec( char *rbuf )
555 char *line, *dn, *p, *q, *attr, *value;
556 int rc, linenum, modop;
565 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
567 if (( p = strchr( rbuf, '\n' )) == NULL ) {
570 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
571 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
579 if ( dn == NULL ) { /* first line contains DN */
580 if (( dn = strdup( line )) == NULL ) {
585 if (( p = strchr( line, '=' )) == NULL ) {
587 p = line + strlen( line );
594 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
595 ; /* skip attribute leading white space */
598 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
599 *q = '\0'; /* remove attribute trailing white space */
602 if ( value != NULL ) {
603 while ( isspace( (unsigned char) *value )) {
604 ++value; /* skip value leading white space */
606 for ( q = value + strlen( value ) - 1; q > value &&
607 isspace( (unsigned char) *q ); --q ) {
608 *q = '\0'; /* remove value trailing white space */
610 if ( *value == '\0' ) {
616 if ( value == NULL && new ) {
617 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
618 prog, linenum, attr );
619 rc = LDAP_PARAM_ERROR;
623 modop = LDAP_MOD_DELETE;
627 modop = LDAP_MOD_ADD;
631 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
634 addmodifyop( &pmods, modop, attr, value,
635 ( value == NULL ) ? 0 : strlen( value ));
644 rc = LDAP_PARAM_ERROR;
645 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
650 if ( pmods != NULL ) {
651 ldap_mods_free( pmods, 1 );
662 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
669 modop |= LDAP_MOD_BVALUES;
672 if ( pmods != NULL ) {
673 for ( ; pmods[ i ] != NULL; ++i ) {
674 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
675 pmods[ i ]->mod_op == modop ) {
681 if ( pmods == NULL || pmods[ i ] == NULL ) {
682 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
683 sizeof( LDAPMod * ))) == NULL ) {
688 pmods[ i + 1 ] = NULL;
689 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
694 pmods[ i ]->mod_op = modop;
695 if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
701 if ( value != NULL ) {
703 if ( pmods[ i ]->mod_bvalues != NULL ) {
704 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
708 if (( pmods[ i ]->mod_bvalues =
709 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
710 (j + 2) * sizeof( struct berval * ))) == NULL ) {
711 perror( "ber_realloc" );
714 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
715 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
717 perror( "ber_memalloc" );
720 pmods[ i ]->mod_bvalues[ j ] = bvp;
722 if ( valsfromfiles && *value == '/' ) { /* get value from file */
723 if ( fromfile( value, bvp ) < 0 ) {
728 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
732 SAFEMEMCPY( bvp->bv_val, value, vlen );
733 bvp->bv_val[ vlen ] = '\0';
740 domodify( char *dn, LDAPMod **pmods, int newentry )
742 int i, j, k, notascii, op;
745 if ( pmods == NULL ) {
746 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
748 return( LDAP_PARAM_ERROR );
752 for ( i = 0; pmods[ i ] != NULL; ++i ) {
753 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
754 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
755 "replace" : op == LDAP_MOD_ADD ?
756 "add" : "delete", pmods[ i ]->mod_type );
757 if ( pmods[ i ]->mod_bvalues != NULL ) {
758 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
759 bvp = pmods[ i ]->mod_bvalues[ j ];
761 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
762 if ( !isascii( bvp->bv_val[ k ] )) {
768 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
770 printf( "\t%s\n", bvp->bv_val );
778 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
780 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
785 i = ldap_add_s( ld, dn, pmods );
787 i = ldap_modify_s( ld, dn, pmods );
789 if ( i != LDAP_SUCCESS ) {
790 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
791 } else if ( verbose ) {
792 printf( "modify complete\n" );
809 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
811 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
812 ldap_perror( ld, "ldap_delete" );
813 } else if ( verbose ) {
814 printf( "delete complete" );
827 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
832 printf( "new RDN: %s (%skeep existing values)\n",
833 newrdn, deleteoldrdn ? "do not " : "" );
836 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
838 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
840 ldap_perror( ld, "ldap_modrdn" );
842 printf( "modrdn completed\n" );
855 fromfile( char *path, struct berval *bv )
861 if (( fp = fopen( path, "r" )) == NULL ) {
866 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
872 bv->bv_len = ftell( fp );
874 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
880 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
883 ber_memfree( bv->bv_val );
888 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
892 if ( (unsigned long) rlen != bv->bv_len ) {
894 ber_memfree( bv->bv_val );
899 return( bv->bv_len );
904 read_one_record( FILE *fp )
906 char *buf, line[ LDAPMOD_MAXLINE ];
912 while ( fgets( line, sizeof(line), fp ) != NULL ) {
913 int len = strlen( line );
923 if ( lcur + len + 1 > lmax ) {
924 lmax = LDAPMOD_MAXLINE
925 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
927 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
933 strcpy( buf + lcur, line );