3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* ldapmodify.c - generic program to modify or add entries using LDAP */
12 #include <ac/stdlib.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
17 #ifdef HAVE_SYS_STAT_H
21 #ifdef HAVE_SYS_FILE_H
31 #include "lutil_ldap.h"
33 #include "ldap_defaults.h"
40 static int ldapadd, force = 0;
41 static char *rejfile = NULL;
42 static LDAP *ld = NULL;
44 #define LDAPMOD_MAXLINE 4096
46 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
47 #define T_VERSION_STR "version"
48 #define T_REPLICA_STR "replica"
50 #define T_CONTROL_STR "control"
51 #define T_CHANGETYPESTR "changetype"
52 #define T_ADDCTSTR "add"
53 #define T_MODIFYCTSTR "modify"
54 #define T_DELETECTSTR "delete"
55 #define T_MODRDNCTSTR "modrdn"
56 #define T_MODDNCTSTR "moddn"
57 #define T_RENAMECTSTR "rename"
58 #define T_MODOPADDSTR "add"
59 #define T_MODOPREPLACESTR "replace"
60 #define T_MODOPDELETESTR "delete"
61 #define T_MODSEPSTR "-"
62 #define T_NEWRDNSTR "newrdn"
63 #define T_DELETEOLDRDNSTR "deleteoldrdn"
64 #define T_NEWSUPSTR "newsuperior"
67 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
68 static int parse_ldif_control LDAP_P(( char *line, LDAPControl ***pctrls ));
69 static void addmodifyop LDAP_P((
70 LDAPMod ***pmodsp, int modop,
72 struct berval *value ));
73 static int domodify LDAP_P((
78 static int dodelete LDAP_P((
80 LDAPControl **pctrls ));
81 static int dorename LDAP_P((
86 LDAPControl **pctrls ));
87 static char *read_one_record LDAP_P(( FILE *fp ));
93 "Add or modify entries from an LDAP server\n\n"
94 "usage: %s [options]\n"
95 " The list of desired operations are read from stdin or from the file\n"
96 " specified by \"-f file\".\n"
97 "Add or modify options:\n"
98 " -a add values (default%s)\n"
99 " -F force all changes records to be used\n"
100 " -S file write skipped modifications to `file'\n"
101 , prog, (ldapadd ? "" : " is to replace") );
103 exit( EXIT_FAILURE );
107 const char options[] = "aFrS:"
108 "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
111 handle_private_option( int i )
115 char *control, *cvalue;
117 case 'E': /* modify controls */
118 if( protocol == LDAP_VERSION2 ) {
119 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
121 exit( EXIT_FAILURE );
124 /* should be extended to support comma separated list of
125 * [!]key[=value] parameters, e.g. -E !foo,bar=567
130 if( optarg[0] == '!' ) {
135 control = ber_strdup( optarg );
136 if ( (cvalue = strchr( control, '=' )) != NULL ) {
139 fprintf( stderr, "Invalid modify control name: %s\n", control );
147 case 'F': /* force all changes records to be used */
151 case 'r': /* replace (obsolete) */
154 case 'S': /* skipped modifications to file */
155 if( rejfile != NULL ) {
156 fprintf( stderr, "%s: -S previously specified\n", prog );
157 exit( EXIT_FAILURE );
159 rejfile = ber_strdup( optarg );
170 main( int argc, char **argv )
172 char *rbuf, *start, *rejbuf = NULL;
174 char *matched_msg, *error_msg;
178 prog = lutil_progname( "ldapmodify", argc, argv );
180 /* strncmp instead of strcmp since NT binaries carry .exe extension */
181 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
183 /* Print usage when no parameters */
184 if( argc < 2 ) usage();
186 tool_args( argc, argv );
188 if ( argc != optind )
191 if ( rejfile != NULL ) {
192 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
194 return( EXIT_FAILURE );
200 if ( infile != NULL ) {
201 if (( fp = fopen( infile, "r" )) == NULL ) {
203 return( EXIT_FAILURE );
212 ld = tool_conn_setup( not, 0 );
215 if ( pw_file || want_bindpw ) {
217 rc = lutil_get_filed_password( pw_file, &passwd );
218 if( rc ) return EXIT_FAILURE;
220 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
221 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
230 if ( authzid || manageDSAit || noop )
231 tool_server_controls( ld, NULL, 0 );
235 while (( rc == 0 || contoper ) &&
236 ( rbuf = read_one_record( fp )) != NULL ) {
242 len = strlen( rbuf );
243 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
245 exit( EXIT_FAILURE );
247 memcpy( rejbuf, rbuf, len+1 );
250 rc = process_ldif_rec( start, count );
255 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
258 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
259 if ( matched_msg != NULL ) {
260 if ( *matched_msg != '\0' )
261 fprintf( rejfp, ", matched DN: %s", matched_msg );
262 ldap_memfree( matched_msg );
266 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
267 if ( error_msg != NULL ) {
268 if ( *error_msg != '\0' )
269 fprintf( rejfp, ", additional info: %s", error_msg );
270 ldap_memfree( error_msg );
272 fprintf( rejfp, "\n%s\n", rejbuf );
283 if ( rejfp != NULL ) {
292 process_ldif_rec( char *rbuf, int count )
294 char *line, *dn, *type, *newrdn, *newsup, *p;
295 int rc, linenum, modop, replicaport;
296 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
297 int expect_deleteoldrdn, deleteoldrdn;
298 int saw_replica, use_record, new_entry, delete_entry, got_all;
302 LDAPControl **pctrls;
306 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
307 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
308 expect_sep = expect_ct = 0;
315 dn = newrdn = newsup = NULL;
317 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
320 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
326 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
327 fprintf( stderr, "%s: invalid format (line %d) 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( val.bv_val, ':' )) == NULL ) {
340 replicaport = atoi( p );
342 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
343 replicaport == ldapport ) {
346 } else if ( count == 1 && linenum == 1 &&
347 strcasecmp( type, T_VERSION_STR ) == 0 )
349 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
350 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
351 prog, val.bv_val, linenum );
355 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
356 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
358 exit( EXIT_FAILURE );
362 goto end_line; /* skip all lines until we see "dn:" */
367 /* Check for "control" tag after dn and before changetype. */
368 if (strcasecmp(type, T_CONTROL_STR) == 0) {
369 /* Parse and add it to the list of controls */
370 rc = parse_ldif_control( line, &pctrls );
372 fprintf( stderr, "%s: Error processing %s line, line %d: %s\n",
373 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
379 if ( !use_record && saw_replica ) {
380 printf( "%s: skipping change record for entry: %s\n"
381 "\t(LDAP host/port does not match replica: lines)\n",
385 ber_memfree( val.bv_val );
389 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
390 #ifdef LIBERAL_CHANGETYPE_MODOP
391 /* trim trailing spaces (and log warning ...) */
394 for ( icnt = val.bv_len; --icnt > 0; ) {
395 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
400 if ( ++icnt != val.bv_len ) {
401 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
402 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
403 val.bv_val[icnt] = '\0';
405 #endif /* LIBERAL_CHANGETYPE_MODOP */
407 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
410 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
412 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
413 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
414 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
417 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
418 got_all = delete_entry = 1;
421 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
422 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
423 rc = LDAP_PARAM_ERROR;
426 } else if ( ldapadd ) { /* missing changetype => add */
428 modop = LDAP_MOD_ADD;
430 expect_modop = 1; /* missing changetype => modify */
434 if ( expect_modop ) {
435 #ifdef LIBERAL_CHANGETYPE_MODOP
436 /* trim trailing spaces (and log warning ...) */
439 for ( icnt = val.bv_len; --icnt > 0; ) {
440 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
445 if ( ++icnt != val.bv_len ) {
446 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
447 prog, type, val.bv_val, linenum, dn );
448 val.bv_val[icnt] = '\0';
450 #endif /* LIBERAL_CHANGETYPE_MODOP */
454 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
455 modop = LDAP_MOD_ADD;
457 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
458 modop = LDAP_MOD_REPLACE;
459 addmodifyop( &pmods, modop, val.bv_val, NULL );
461 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
462 modop = LDAP_MOD_DELETE;
463 addmodifyop( &pmods, modop, val.bv_val, NULL );
465 } else { /* no modify op: use default */
466 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
470 if ( expect_newrdn ) {
471 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
472 if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
474 exit( EXIT_FAILURE );
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 = ( *val.bv_val == '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 = ber_strdup( val.bv_val )) == NULL ) {
498 exit( EXIT_FAILURE );
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, &val );
517 ber_memfree( val.bv_val );
524 if( version && linenum == 1 ) {
528 /* If default controls are set (as with -M option) and controls are
529 specified in the LDIF file, we must add the default controls to
530 the list of controls sent with the ldap operation.
534 LDAPControl **defctrls = NULL; /* Default server controls */
535 LDAPControl **newctrls = NULL;
536 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
538 int npc=0; /* Number of LDIF controls */
539 int ndefc=0; /* Number of default controls */
540 while (pctrls[npc]) /* Count LDIF controls */
542 while (defctrls[ndefc]) /* Count default controls */
544 newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*));
545 if (newctrls == NULL)
550 for (i=npc; i<npc+ndefc; i++) {
551 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
552 if (pctrls[i] == NULL) {
557 pctrls[npc+ndefc] = NULL;
559 ldap_controls_free(defctrls); /* Must be freed by library */
566 if ( delete_entry ) {
567 rc = dodelete( dn, pctrls );
568 } else if ( newrdn != NULL ) {
569 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
571 rc = domodify( dn, pmods, pctrls, new_entry );
574 if ( rc == LDAP_SUCCESS ) {
582 if ( newrdn != NULL ) {
585 if ( pmods != NULL ) {
586 ldap_mods_free( pmods, 1 );
589 if (pctrls != NULL) {
590 ldap_controls_free( pctrls );
596 /* Parse an LDIF control line of the form
597 control: oid [true/false] [: value] or
598 control: oid [true/false] [:: base64-value] or
599 control: oid [true/false] [:< url]
600 The control is added to the list of controls in *ppctrls.
603 parse_ldif_control( char *line,
604 LDAPControl ***ppctrls )
607 int criticality = 0; /* Default is false if not present */
610 ber_len_t value_len = 0;
612 char *s, *oidStart, *pcolon;
613 LDAPControl *newctrl = NULL;
614 LDAPControl **pctrls = NULL;
619 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
620 pcolon = s; /* Save this position for later */
621 if (*s++ != ':') /* Make sure colon follows */
622 return ( LDAP_PARAM_ERROR );
623 while (*s && isspace((unsigned char)*s))
624 s++; /* Skip white space before OID */
626 /* OID should come next. Validate and extract it. */
628 return ( LDAP_PARAM_ERROR );
630 while (isdigit((unsigned char)*s) || *s == '.')
631 s++; /* OID should be digits or . */
633 return ( LDAP_PARAM_ERROR ); /* OID was not present */
634 if (*s) { /* End of OID should be space or NULL */
635 if (!isspace((unsigned char)*s))
636 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
637 *s++ = 0; /* Replace space with null to terminate */
641 oid = ber_strdup(oidStart);
643 return ( LDAP_NO_MEMORY );
645 /* Optional Criticality field is next. */
646 while (*s && isspace((unsigned char)*s))
647 s++; /* Skip white space before criticality */
648 if (strncasecmp(s, "true", 4) == 0) {
652 else if (strncasecmp(s, "false", 5) == 0) {
657 /* Optional value field is next */
658 while (*s && isspace((unsigned char)*s))
659 s++; /* Skip white space before value */
661 if (*s != ':') { /* If value is present, must start with : */
662 rc = LDAP_PARAM_ERROR;
666 /* Shift value down over OID and criticality so it's in the form
668 control:: base64-value
670 Then we can use ldif_parse_line to extract and decode the value
672 while ( (*pcolon++ = *s++) != 0) /* Shift value */
674 rc = ldif_parse_line(line, &type, &val, &value_len);
675 if (type) ber_memfree(type); /* Don't need this field*/
677 rc = LDAP_PARAM_ERROR;
682 /* Create a new LDAPControl structure. */
683 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
684 if ( newctrl == NULL ) {
688 newctrl->ldctl_oid = oid;
690 newctrl->ldctl_iscritical = criticality;
691 newctrl->ldctl_value.bv_len = value_len;
692 newctrl->ldctl_value.bv_val = val;
695 /* Add the new control to the passed-in list of controls. */
698 while ( pctrls[i] ) /* Count the # of controls passed in */
701 /* Allocate 1 more slot for the new control and 1 for the NULL. */
702 pctrls = (LDAPControl **)ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *)));
703 if (pctrls == NULL) {
714 if (newctrl->ldctl_oid)
715 ber_memfree(newctrl->ldctl_oid);
716 if (newctrl->ldctl_value.bv_val)
717 ber_memfree(newctrl->ldctl_value.bv_val);
718 ber_memfree(newctrl);
740 modop |= LDAP_MOD_BVALUES;
743 if ( pmods != NULL ) {
744 for ( ; pmods[ i ] != NULL; ++i ) {
745 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
746 pmods[ i ]->mod_op == modop )
753 if ( pmods == NULL || pmods[ i ] == NULL ) {
754 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
755 sizeof( LDAPMod * ))) == NULL )
758 exit( EXIT_FAILURE );
762 pmods[ i + 1 ] = NULL;
764 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
765 if ( pmods[ i ] == NULL ) {
767 exit( EXIT_FAILURE );
770 pmods[ i ]->mod_op = modop;
771 pmods[ i ]->mod_type = ber_strdup( attr );
772 if ( pmods[ i ]->mod_type == NULL ) {
774 exit( EXIT_FAILURE );
780 if ( pmods[ i ]->mod_bvalues != NULL ) {
781 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
786 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
787 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
788 if ( pmods[ i ]->mod_bvalues == NULL ) {
789 perror( "ber_realloc" );
790 exit( EXIT_FAILURE );
793 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
794 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
795 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
796 perror( "ber_bvdup" );
797 exit( EXIT_FAILURE );
807 LDAPControl **pctrls,
810 int i, j, k, notascii, op;
813 if ( pmods == NULL ) {
814 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
816 return( LDAP_PARAM_ERROR );
819 for ( i = 0; pmods[ i ] != NULL; ++i ) {
820 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
821 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
823 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
824 prog, pmods[i]->mod_type, dn );
825 return LDAP_PARAM_ERROR;
830 for ( i = 0; pmods[ i ] != NULL; ++i ) {
831 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
832 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
833 "replace" : op == LDAP_MOD_ADD ?
834 "add" : "delete", pmods[ i ]->mod_type );
835 if ( pmods[ i ]->mod_bvalues != NULL ) {
836 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
837 bvp = pmods[ i ]->mod_bvalues[ j ];
839 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
840 if ( !isascii( bvp->bv_val[ k ] )) {
846 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
848 printf( "\t%s\n", bvp->bv_val );
856 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
858 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
863 i = ldap_add_ext_s( ld, dn, pmods, pctrls, NULL );
865 i = ldap_modify_ext_s( ld, dn, pmods, pctrls, NULL );
867 if ( i != LDAP_SUCCESS ) {
868 /* print error message about failed update including DN */
869 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
870 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
871 } else if ( verbose ) {
872 printf( "modify complete\n" );
887 LDAPControl **pctrls )
891 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
893 if (( rc = ldap_delete_ext_s( ld, dn, pctrls, NULL )) != LDAP_SUCCESS ) {
894 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
895 ldap_perror( ld, "ldap_delete" );
896 } else if ( verbose ) {
897 printf( "delete complete" );
915 LDAPControl **pctrls )
920 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
922 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
923 newrdn, deleteoldrdn ? "do not " : "" );
926 if (( rc = ldap_rename_s( ld, dn, newrdn, newsup, deleteoldrdn, pctrls, NULL ))
928 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
929 ldap_perror( ld, "ldap_modrdn" );
931 printf( "modrdn completed\n" );
944 read_one_record( FILE *fp )
946 char *buf, line[ LDAPMOD_MAXLINE ];
952 while ( fgets( line, sizeof(line), fp ) != NULL ) {
953 int len = strlen( line );
955 if( len < 2 || ( len == 2 && *line == '\r' )) {
963 if ( lcur + len + 1 > lmax ) {
964 lmax = LDAPMOD_MAXLINE
965 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
967 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
969 exit( EXIT_FAILURE );
973 strcpy( buf + lcur, line );