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 to %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) 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 ? value : "" )) == NULL ) {
408 exit( EXIT_FAILURE );
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"
419 "\t(LDAP host/port does not match replica: lines)\n",
425 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
426 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
429 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
431 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0
432 || strcasecmp( value, T_MODDNCTSTR ) == 0
433 || strcasecmp( value, T_RENAMECTSTR ) == 0)
436 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
437 got_all = delete_entry = 1;
440 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
441 prog, T_CHANGETYPESTR, value, linenum, dn );
442 rc = LDAP_PARAM_ERROR;
445 } else if ( new ) { /* missing changetype => add */
447 modop = LDAP_MOD_ADD;
449 expect_modop = 1; /* missing changetype => modify */
453 if ( expect_modop ) {
456 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
457 modop = LDAP_MOD_ADD;
459 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
460 modop = LDAP_MOD_REPLACE;
462 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
463 modop = LDAP_MOD_DELETE;
464 addmodifyop( &pmods, modop, value, NULL, 0 );
466 } else { /* no modify op: use default */
467 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
471 if ( expect_newrdn ) {
472 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
473 if (( newrdn = strdup( value )) == NULL ) {
475 exit( EXIT_FAILURE );
477 expect_deleteoldrdn = 1;
480 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
481 prog, T_NEWRDNSTR, type, linenum, dn );
482 rc = LDAP_PARAM_ERROR;
484 } else if ( expect_deleteoldrdn ) {
485 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
486 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
487 expect_deleteoldrdn = 0;
491 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
492 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
493 rc = LDAP_PARAM_ERROR;
495 } else if ( expect_newsup ) {
496 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
497 if (( newsup = strdup( value )) == NULL ) {
499 exit( EXIT_FAILURE );
503 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
504 prog, T_NEWSUPSTR, type, linenum, dn );
505 rc = LDAP_PARAM_ERROR;
507 } else if ( got_all ) {
509 "%s: extra lines at end (line %d of entry \"%s\")\n",
511 rc = LDAP_PARAM_ERROR;
513 addmodifyop( &pmods, modop, type, value, vlen );
521 if( version && linenum == 1 ) {
526 if ( delete_entry ) {
528 } else if ( newrdn != NULL ) {
529 rc = domodrdn( dn, newrdn, deleteoldrdn );
531 rc = domodify( dn, pmods, new_entry );
534 if ( rc == LDAP_SUCCESS ) {
542 if ( newrdn != NULL ) {
545 if ( pmods != NULL ) {
546 ldap_mods_free( pmods, 1 );
554 process_ldapmod_rec( char *rbuf )
556 char *line, *dn, *p, *q, *attr, *value;
557 int rc, linenum, modop;
566 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
568 if (( p = strchr( rbuf, '\n' )) == NULL ) {
571 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
572 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
580 if ( dn == NULL ) { /* first line contains DN */
581 if (( dn = strdup( line )) == NULL ) {
583 exit( EXIT_FAILURE );
586 if (( p = strchr( line, '=' )) == NULL ) {
588 p = line + strlen( line );
595 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
596 ; /* skip attribute leading white space */
599 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
600 *q = '\0'; /* remove attribute trailing white space */
603 if ( value != NULL ) {
604 while ( isspace( (unsigned char) *value )) {
605 ++value; /* skip value leading white space */
607 for ( q = value + strlen( value ) - 1; q > value &&
608 isspace( (unsigned char) *q ); --q ) {
609 *q = '\0'; /* remove value trailing white space */
611 if ( *value == '\0' ) {
617 if ( value == NULL && new ) {
618 fprintf( stderr, "%s: missing value on line %d (attr=\"%s\")\n",
619 prog, linenum, attr );
620 rc = LDAP_PARAM_ERROR;
624 modop = LDAP_MOD_DELETE;
628 modop = LDAP_MOD_ADD;
632 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
635 addmodifyop( &pmods, modop, attr, value,
636 ( value == NULL ) ? 0 : strlen( value ));
645 rc = LDAP_PARAM_ERROR;
646 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
651 if ( pmods != NULL ) {
652 ldap_mods_free( pmods, 1 );
663 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
670 modop |= LDAP_MOD_BVALUES;
673 if ( pmods != NULL ) {
674 for ( ; pmods[ i ] != NULL; ++i ) {
675 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
676 pmods[ i ]->mod_op == modop ) {
682 if ( pmods == NULL || pmods[ i ] == NULL ) {
683 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
684 sizeof( LDAPMod * ))) == NULL ) {
686 exit( EXIT_FAILURE );
689 pmods[ i + 1 ] = NULL;
690 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
693 exit( EXIT_FAILURE );
695 pmods[ i ]->mod_op = modop;
696 if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
698 exit( EXIT_FAILURE );
702 if ( value != NULL ) {
704 if ( pmods[ i ]->mod_bvalues != NULL ) {
705 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
709 if (( pmods[ i ]->mod_bvalues =
710 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
711 (j + 2) * sizeof( struct berval * ))) == NULL ) {
712 perror( "ber_realloc" );
713 exit( EXIT_FAILURE );
715 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
716 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
718 perror( "ber_memalloc" );
719 exit( EXIT_FAILURE );
721 pmods[ i ]->mod_bvalues[ j ] = bvp;
723 if ( valsfromfiles && *value == '/' ) { /* get value from file */
724 if ( fromfile( value, bvp ) < 0 ) {
725 exit( EXIT_FAILURE );
729 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
731 exit( EXIT_FAILURE );
733 SAFEMEMCPY( bvp->bv_val, value, vlen );
734 bvp->bv_val[ vlen ] = '\0';
741 domodify( char *dn, LDAPMod **pmods, int newentry )
743 int i, j, k, notascii, op;
746 if ( pmods == NULL ) {
747 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
749 return( LDAP_PARAM_ERROR );
753 for ( i = 0; pmods[ i ] != NULL; ++i ) {
754 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
755 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
756 "replace" : op == LDAP_MOD_ADD ?
757 "add" : "delete", pmods[ i ]->mod_type );
758 if ( pmods[ i ]->mod_bvalues != NULL ) {
759 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
760 bvp = pmods[ i ]->mod_bvalues[ j ];
762 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
763 if ( !isascii( bvp->bv_val[ k ] )) {
769 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
771 printf( "\t%s\n", bvp->bv_val );
779 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
781 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
786 i = ldap_add_s( ld, dn, pmods );
788 i = ldap_modify_s( ld, dn, pmods );
790 if ( i != LDAP_SUCCESS ) {
791 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
792 } else if ( verbose ) {
793 printf( "modify complete\n" );
810 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
812 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
813 ldap_perror( ld, "ldap_delete" );
814 } else if ( verbose ) {
815 printf( "delete complete" );
828 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
833 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
835 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
836 newrdn, deleteoldrdn ? "do not " : "" );
839 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
841 ldap_perror( ld, "ldap_modrdn" );
843 printf( "modrdn completed\n" );
856 fromfile( char *path, struct berval *bv )
862 if (( fp = fopen( path, "r" )) == NULL ) {
867 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
873 bv->bv_len = ftell( fp );
875 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
881 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
884 ber_memfree( bv->bv_val );
889 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
893 if ( (unsigned long) rlen != bv->bv_len ) {
895 ber_memfree( bv->bv_val );
900 return( bv->bv_len );
905 read_one_record( FILE *fp )
907 char *buf, line[ LDAPMOD_MAXLINE ];
913 while ( fgets( line, sizeof(line), fp ) != NULL ) {
914 int len = strlen( line );
924 if ( lcur + len + 1 > lmax ) {
925 lmax = LDAPMOD_MAXLINE
926 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
928 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
930 exit( EXIT_FAILURE );
934 strcpy( buf + lcur, line );