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 ], '/' )) == NULL &&
100 ( prog = strrchr( argv[ 0 ], '\\' )) == NULL ) { /*for Windows/DOS*/
106 /* Print usage when no parameters */
110 new = ( strcmp( prog, "ldapadd" ) == 0 );
113 not = verbose = valsfromfiles = want_bindpw = debug = 0;
114 authmethod = LDAP_AUTH_SIMPLE;
117 while (( i = getopt( argc, argv, "WFabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
122 case 'b': /* read values from files (for binary attributes) */
125 case 'c': /* continuous operation */
128 case 'r': /* default is to replace rather than add values */
131 case 'k': /* kerberos bind */
133 authmethod = LDAP_AUTH_KRBV4;
135 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
138 case 'K': /* kerberos bind, part 1 only */
140 authmethod = LDAP_AUTH_KRBV41;
142 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
145 case 'F': /* force all changes records to be used */
148 case 'h': /* ldap host */
149 ldaphost = strdup( optarg );
151 case 'D': /* bind DN */
152 binddn = strdup( optarg );
154 case 'w': /* password */
155 passwd = strdup( optarg );
158 debug |= atoi( optarg );
160 case 'f': /* read from file */
161 infile = strdup( optarg );
164 ldapport = atoi( optarg );
166 case 'n': /* print adds, don't actually do them */
169 case 'v': /* verbose mode */
179 version = LDAP_VERSION2;
182 version = LDAP_VERSION3;
191 if ( argc != optind )
194 if ( infile != NULL ) {
195 if (( fp = fopen( infile, "r" )) == NULL ) {
204 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
205 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
210 (void) SIGNAL( SIGPIPE, SIG_IGN );
214 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
215 perror( "ldap_init" );
219 /* this seems prudent */
221 int deref = LDAP_DEREF_NEVER;
222 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
226 passwd = getpass("Enter LDAP Password: ");
228 if( version != -1 ) {
229 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
232 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
233 ldap_perror( ld, "ldap_bind" );
240 while (( rc == 0 || contoper ) &&
241 ( rbuf = read_one_record( fp )) != NULL ) {
243 * we assume record is ldif/slapd.replog if the first line
244 * has a colon that appears to the left of any equal signs, OR
245 * if the first line consists entirely of digits (an entry id)
247 use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
248 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
249 (( q = strchr( rbuf, '=' )) == NULL || p < q );
253 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
254 for ( p = rbuf; p < q; ++p ) {
255 if ( !isdigit( (unsigned char) *p )) {
266 rc = process_ldif_rec( start );
268 rc = process_ldapmod_rec( start );
272 fprintf( stderr, "%s() = %d\n",
273 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
290 process_ldif_rec( char *rbuf )
292 char *line, *dn, *type, *value, *newrdn, *p;
293 int rc, linenum, vlen, modop, replicaport;
294 int expect_modop, expect_sep, expect_ct, expect_newrdn;
295 int expect_deleteoldrdn, deleteoldrdn;
296 int saw_replica, use_record, new_entry, delete_entry, got_all;
301 rc = got_all = saw_replica = delete_entry = expect_modop = 0;
302 expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
309 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
311 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
317 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
318 fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
319 prog, linenum, dn == NULL ? "" : dn );
320 rc = LDAP_PARAM_ERROR;
325 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
327 if (( p = strchr( value, ':' )) == NULL ) {
331 replicaport = atoi( p );
333 if ( strcasecmp( value, ldaphost ) == 0 &&
334 replicaport == ldapport ) {
337 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
338 if (( dn = strdup( value )) == NULL ) {
344 continue; /* skip all lines until we see "dn:" */
349 if ( !use_record && saw_replica ) {
350 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
356 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
357 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
360 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
362 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
364 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
365 got_all = delete_entry = 1;
368 "%s: unknown %s \"%s\" (line %d of entry: %s)\n",
369 prog, T_CHANGETYPESTR, value, linenum, dn );
370 rc = LDAP_PARAM_ERROR;
373 } else if ( new ) { /* missing changetype => add */
375 modop = LDAP_MOD_ADD;
377 expect_modop = 1; /* missing changetype => modify */
381 if ( expect_modop ) {
384 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
385 modop = LDAP_MOD_ADD;
387 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
388 modop = LDAP_MOD_REPLACE;
390 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
391 modop = LDAP_MOD_DELETE;
392 addmodifyop( &pmods, modop, value, NULL, 0 );
394 } else { /* no modify op: use default */
395 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
399 if ( expect_newrdn ) {
400 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
401 if (( newrdn = strdup( value )) == NULL ) {
405 expect_deleteoldrdn = 1;
408 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
409 prog, T_NEWRDNSTR, type, linenum, dn );
410 rc = LDAP_PARAM_ERROR;
412 } else if ( expect_deleteoldrdn ) {
413 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
414 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
417 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
418 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
419 rc = LDAP_PARAM_ERROR;
421 } else if ( got_all ) {
423 "%s: extra lines at end (line %d of entry %s)\n",
425 rc = LDAP_PARAM_ERROR;
427 addmodifyop( &pmods, modop, type, value, vlen );
432 if ( delete_entry ) {
434 } else if ( newrdn != NULL ) {
435 rc = domodrdn( dn, newrdn, deleteoldrdn );
437 rc = domodify( dn, pmods, new_entry );
440 if ( rc == LDAP_SUCCESS ) {
448 if ( newrdn != NULL ) {
451 if ( pmods != NULL ) {
460 process_ldapmod_rec( char *rbuf )
462 char *line, *dn, *p, *q, *attr, *value;
463 int rc, linenum, modop;
472 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
474 if (( p = strchr( rbuf, '\n' )) == NULL ) {
477 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
478 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
486 if ( dn == NULL ) { /* first line contains DN */
487 if (( dn = strdup( line )) == NULL ) {
492 if (( p = strchr( line, '=' )) == NULL ) {
494 p = line + strlen( line );
501 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
502 ; /* skip attribute leading white space */
505 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
506 *q = '\0'; /* remove attribute trailing white space */
509 if ( value != NULL ) {
510 while ( isspace( (unsigned char) *value )) {
511 ++value; /* skip value leading white space */
513 for ( q = value + strlen( value ) - 1; q > value &&
514 isspace( (unsigned char) *q ); --q ) {
515 *q = '\0'; /* remove value trailing white space */
517 if ( *value == '\0' ) {
523 if ( value == NULL && new ) {
524 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
525 prog, linenum, attr );
526 rc = LDAP_PARAM_ERROR;
530 modop = LDAP_MOD_DELETE;
534 modop = LDAP_MOD_ADD;
538 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
541 addmodifyop( &pmods, modop, attr, value,
542 ( value == NULL ) ? 0 : strlen( value ));
551 rc = LDAP_PARAM_ERROR;
552 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
557 if ( pmods != NULL ) {
569 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
576 modop |= LDAP_MOD_BVALUES;
579 if ( pmods != NULL ) {
580 for ( ; pmods[ i ] != NULL; ++i ) {
581 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
582 pmods[ i ]->mod_op == modop ) {
588 if ( pmods == NULL || pmods[ i ] == NULL ) {
589 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
590 sizeof( LDAPMod * ))) == NULL ) {
595 pmods[ i + 1 ] = NULL;
596 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
601 pmods[ i ]->mod_op = modop;
602 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
608 if ( value != NULL ) {
610 if ( pmods[ i ]->mod_bvalues != NULL ) {
611 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
615 if (( pmods[ i ]->mod_bvalues =
616 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
617 (j + 2) * sizeof( struct berval * ))) == NULL ) {
618 perror( "ber_realloc" );
621 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
622 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
624 perror( "ber_memalloc" );
627 pmods[ i ]->mod_bvalues[ j ] = bvp;
629 if ( valsfromfiles && *value == '/' ) { /* get value from file */
630 if ( fromfile( value, bvp ) < 0 ) {
635 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
639 SAFEMEMCPY( bvp->bv_val, value, vlen );
640 bvp->bv_val[ vlen ] = '\0';
647 domodify( char *dn, LDAPMod **pmods, int newentry )
649 int i, j, k, notascii, op;
652 if ( pmods == NULL ) {
653 fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
655 return( LDAP_PARAM_ERROR );
659 for ( i = 0; pmods[ i ] != NULL; ++i ) {
660 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
661 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
662 "replace" : op == LDAP_MOD_ADD ?
663 "add" : "delete", pmods[ i ]->mod_type );
664 if ( pmods[ i ]->mod_bvalues != NULL ) {
665 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
666 bvp = pmods[ i ]->mod_bvalues[ j ];
668 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
669 if ( !isascii( bvp->bv_val[ k ] )) {
675 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
677 printf( "\t%s\n", bvp->bv_val );
685 printf( "%sadding new entry %s\n", not ? "!" : "", dn );
687 printf( "%smodifying entry %s\n", not ? "!" : "", dn );
692 i = ldap_add_s( ld, dn, pmods );
694 i = ldap_modify_s( ld, dn, pmods );
696 if ( i != LDAP_SUCCESS ) {
697 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
698 } else if ( verbose ) {
699 printf( "modify complete\n" );
716 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
718 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
719 ldap_perror( ld, "ldap_delete" );
720 } else if ( verbose ) {
721 printf( "delete complete" );
734 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
739 printf( "new RDN: %s (%skeep existing values)\n",
740 newrdn, deleteoldrdn ? "do not " : "" );
743 printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
745 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
747 ldap_perror( ld, "ldap_modrdn" );
749 printf( "modrdn completed\n" );
763 freepmods( LDAPMod **pmods )
767 for ( i = 0; pmods[ i ] != NULL; ++i ) {
768 if ( pmods[ i ]->mod_bvalues != NULL ) {
769 ber_bvecfree( pmods[ i ]->mod_bvalues );
771 if ( pmods[ i ]->mod_type != NULL ) {
772 free( pmods[ i ]->mod_type );
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 *)malloc( bv->bv_len )) == NULL ) {
806 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
812 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
816 if ( (unsigned long) rlen != bv->bv_len ) {
822 return( bv->bv_len );
827 read_one_record( FILE *fp )
830 char *buf, line[ LDAPMOD_MAXLINE ];
836 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
837 (( len = strlen( line )) > 1 )) {
838 if ( lcur + len + 1 > lmax ) {
839 lmax = LDAPMOD_MAXLINE
840 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
841 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
846 strcpy( buf + lcur, line );