3 * Copyright 1998-2002 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:" "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z";
110 handle_private_option( int i )
114 char *control, *cvalue;
116 case 'E': /* modify controls */
117 if( version == LDAP_VERSION2 ) {
118 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
120 exit( EXIT_FAILURE );
123 /* should be extended to support comma separated list of
124 * [!]key[=value] parameters, e.g. -E !foo,bar=567
129 if( optarg[0] == '!' ) {
134 control = ber_strdup( optarg );
135 if ( (cvalue = strchr( control, '=' )) != NULL ) {
138 fprintf( stderr, "Invalid modify control name: %s\n", control );
146 case 'F': /* force all changes records to be used */
150 case 'r': /* replace (obsolete) */
153 case 'S': /* skipped modifications to file */
154 if( rejfile != NULL ) {
155 fprintf( stderr, "%s: -S previously specified\n", prog );
156 exit( EXIT_FAILURE );
158 rejfile = ber_strdup( optarg );
169 main( int argc, char **argv )
171 char *rbuf, *start, *rejbuf = NULL;
173 char *matched_msg = NULL, *error_msg = NULL;
177 prog = lutil_progname( "ldapmodify", argc, argv );
179 /* strncmp instead of strcmp since NT binaries carry .exe extension */
180 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
182 /* Print usage when no parameters */
183 if( argc < 2 ) usage();
185 tool_args( argc, argv );
187 if ( argc != optind )
190 if ( rejfile != NULL ) {
191 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
193 return( EXIT_FAILURE );
199 if ( infile != NULL ) {
200 if (( fp = fopen( infile, "r" )) == NULL ) {
202 return( EXIT_FAILURE );
211 ld = tool_conn_setup( not, 0 );
214 if ( pw_file || want_bindpw ) {
216 rc = lutil_get_filed_password( pw_file, &passwd );
217 if( rc ) return EXIT_FAILURE;
219 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
220 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
229 if ( authzid || manageDSAit || noop )
230 tool_server_controls( ld, NULL, 0 );
234 while (( rc == 0 || contoper ) &&
235 ( rbuf = read_one_record( fp )) != NULL ) {
241 len = strlen( rbuf );
242 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
244 exit( EXIT_FAILURE );
246 memcpy( rejbuf, rbuf, len+1 );
249 rc = process_ldif_rec( start, count );
254 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
256 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
257 if ( matched_msg != NULL && *matched_msg != '\0' ) {
258 fprintf( rejfp, ", matched DN: %s", matched_msg );
261 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
262 if ( error_msg != NULL && *error_msg != '\0' ) {
263 fprintf( rejfp, ", additional info: %s", error_msg );
265 fprintf( rejfp, "\n%s\n", rejbuf );
276 if ( rejfp != NULL ) {
285 process_ldif_rec( char *rbuf, int count )
287 char *line, *dn, *type, *newrdn, *newsup, *p;
288 int rc, linenum, modop, replicaport;
289 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
290 int expect_deleteoldrdn, deleteoldrdn;
291 int saw_replica, use_record, new_entry, delete_entry, got_all;
295 LDAPControl **pctrls;
299 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
300 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
301 expect_sep = expect_ct = 0;
308 dn = newrdn = newsup = NULL;
310 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
313 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
319 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
320 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
321 prog, linenum, dn == NULL ? "" : dn );
322 rc = LDAP_PARAM_ERROR;
327 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
329 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
333 replicaport = atoi( p );
335 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
336 replicaport == ldapport ) {
339 } else if ( count == 1 && linenum == 1 &&
340 strcasecmp( type, T_VERSION_STR ) == 0 )
342 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
343 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
344 prog, val.bv_val, linenum );
348 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
349 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
351 exit( EXIT_FAILURE );
355 goto end_line; /* skip all lines until we see "dn:" */
360 /* Check for "control" tag after dn and before changetype. */
361 if (strcasecmp(type, T_CONTROL_STR) == 0) {
362 /* Parse and add it to the list of controls */
363 rc = parse_ldif_control( line, &pctrls );
365 fprintf( stderr, "%s: Error processing %s line, line %d: %s\n",
366 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
372 if ( !use_record && saw_replica ) {
373 printf( "%s: skipping change record for entry: %s\n"
374 "\t(LDAP host/port does not match replica: lines)\n",
378 ber_memfree( val.bv_val );
382 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
383 #ifdef LIBERAL_CHANGETYPE_MODOP
384 /* trim trailing spaces (and log warning ...) */
387 for ( icnt = val.bv_len; --icnt > 0; ) {
388 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
393 if ( ++icnt != val.bv_len ) {
394 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
395 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
396 val.bv_val[icnt] = '\0';
398 #endif /* LIBERAL_CHANGETYPE_MODOP */
400 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
403 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
405 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
406 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
407 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
410 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
411 got_all = delete_entry = 1;
414 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
415 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
416 rc = LDAP_PARAM_ERROR;
419 } else if ( ldapadd ) { /* missing changetype => add */
421 modop = LDAP_MOD_ADD;
423 expect_modop = 1; /* missing changetype => modify */
427 if ( expect_modop ) {
428 #ifdef LIBERAL_CHANGETYPE_MODOP
429 /* trim trailing spaces (and log warning ...) */
432 for ( icnt = val.bv_len; --icnt > 0; ) {
433 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
438 if ( ++icnt != val.bv_len ) {
439 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
440 prog, type, val.bv_val, linenum, dn );
441 val.bv_val[icnt] = '\0';
443 #endif /* LIBERAL_CHANGETYPE_MODOP */
447 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
448 modop = LDAP_MOD_ADD;
450 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
451 modop = LDAP_MOD_REPLACE;
452 addmodifyop( &pmods, modop, val.bv_val, NULL );
454 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
455 modop = LDAP_MOD_DELETE;
456 addmodifyop( &pmods, modop, val.bv_val, NULL );
458 } else { /* no modify op: use default */
459 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
463 if ( expect_newrdn ) {
464 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
465 if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
467 exit( EXIT_FAILURE );
469 expect_deleteoldrdn = 1;
472 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
473 prog, T_NEWRDNSTR, type, linenum, dn );
474 rc = LDAP_PARAM_ERROR;
476 } else if ( expect_deleteoldrdn ) {
477 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
478 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
479 expect_deleteoldrdn = 0;
483 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
484 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
485 rc = LDAP_PARAM_ERROR;
487 } else if ( expect_newsup ) {
488 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
489 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
491 exit( EXIT_FAILURE );
495 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
496 prog, T_NEWSUPSTR, type, linenum, dn );
497 rc = LDAP_PARAM_ERROR;
499 } else if ( got_all ) {
501 "%s: extra lines at end (line %d of entry \"%s\")\n",
503 rc = LDAP_PARAM_ERROR;
505 addmodifyop( &pmods, modop, type, &val );
510 ber_memfree( val.bv_val );
517 if( version && linenum == 1 ) {
521 /* If default controls are set (as with -M option) and controls are
522 specified in the LDIF file, we must add the default controls to
523 the list of controls sent with the ldap operation.
527 LDAPControl **defctrls = NULL; /* Default server controls */
528 LDAPControl **newctrls = NULL;
529 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
531 int npc=0; /* Number of LDIF controls */
532 int ndefc=0; /* Number of default controls */
533 while (pctrls[npc]) /* Count LDIF controls */
535 while (defctrls[ndefc]) /* Count default controls */
537 newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*));
538 if (newctrls == NULL)
543 for (i=npc; i<npc+ndefc; i++) {
544 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
545 if (pctrls[i] == NULL) {
550 pctrls[npc+ndefc] = NULL;
551 ldap_controls_free(defctrls); /* Must be freed by library */
559 if ( delete_entry ) {
560 rc = dodelete( dn, pctrls );
561 } else if ( newrdn != NULL ) {
562 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
564 rc = domodify( dn, pmods, pctrls, new_entry );
567 if ( rc == LDAP_SUCCESS ) {
575 if ( newrdn != NULL ) {
578 if ( pmods != NULL ) {
579 ldap_mods_free( pmods, 1 );
582 if (pctrls != NULL) {
583 ldap_controls_free( pctrls );
589 /* Parse an LDIF control line of the form
590 control: oid [true/false] [: value] or
591 control: oid [true/false] [:: base64-value] or
592 control: oid [true/false] [:< url]
593 The control is added to the list of controls in *ppctrls.
596 parse_ldif_control( char *line,
597 LDAPControl ***ppctrls )
600 int criticality = 0; /* Default is false if not present */
603 ber_len_t value_len = 0;
605 char *s, *oidStart, *pcolon;
606 LDAPControl *newctrl = NULL;
607 LDAPControl **pctrls = NULL;
612 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
613 pcolon = s; /* Save this position for later */
614 if (*s++ != ':') /* Make sure colon follows */
615 return ( LDAP_PARAM_ERROR );
616 while (*s && isspace(*s)) s++; /* Skip white space before OID */
618 /* OID should come next. Validate and extract it. */
620 return ( LDAP_PARAM_ERROR );
622 while (isdigit(*s) || *s == '.') s++; /* OID should be digits or . */
624 return ( LDAP_PARAM_ERROR ); /* OID was not present */
625 if (*s) { /* End of OID should be space or NULL */
627 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
628 *s++ = 0; /* Replace space with null to terminate */
632 oid = ber_strdup(oidStart);
634 return ( LDAP_NO_MEMORY );
636 /* Optional Criticality field is next. */
637 while (*s && isspace(*s)) s++; /* Skip white space before criticality */
638 if (strncasecmp(s, "true", 4) == 0) {
642 else if (strncasecmp(s, "false", 5) == 0) {
647 /* Optional value field is next */
648 while (*s && isspace(*s)) s++; /* Skip white space before value */
650 if (*s != ':') { /* If value is present, must start with : */
651 rc = LDAP_PARAM_ERROR;
655 /* Shift value down over OID and criticality so it's in the form
657 control:: base64-value
659 Then we can use ldif_parse_line to extract and decode the value
661 while ( (*pcolon++ = *s++) != 0) /* Shift value */
663 rc = ldif_parse_line(line, &type, &val, &value_len);
664 if (type) ber_memfree(type); /* Don't need this field*/
666 rc = LDAP_PARAM_ERROR;
671 /* Create a new LDAPControl structure. */
672 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
673 if ( newctrl == NULL ) {
677 newctrl->ldctl_oid = oid;
679 newctrl->ldctl_iscritical = criticality;
680 newctrl->ldctl_value.bv_len = value_len;
681 newctrl->ldctl_value.bv_val = val;
684 /* Add the new control to the passed-in list of controls. */
687 while ( pctrls[i] ) /* Count the # of controls passed in */
690 /* Allocate 1 more slot for the new control and 1 for the NULL. */
691 pctrls = (LDAPControl **)ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *)));
692 if (pctrls == NULL) {
703 if (newctrl->ldctl_oid)
704 ber_memfree(newctrl->ldctl_oid);
705 if (newctrl->ldctl_value.bv_val)
706 ber_memfree(newctrl->ldctl_value.bv_val);
707 ber_memfree(newctrl);
729 modop |= LDAP_MOD_BVALUES;
732 if ( pmods != NULL ) {
733 for ( ; pmods[ i ] != NULL; ++i ) {
734 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
735 pmods[ i ]->mod_op == modop )
742 if ( pmods == NULL || pmods[ i ] == NULL ) {
743 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
744 sizeof( LDAPMod * ))) == NULL )
747 exit( EXIT_FAILURE );
751 pmods[ i + 1 ] = NULL;
753 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
754 if ( pmods[ i ] == NULL ) {
756 exit( EXIT_FAILURE );
759 pmods[ i ]->mod_op = modop;
760 pmods[ i ]->mod_type = ber_strdup( attr );
761 if ( pmods[ i ]->mod_type == NULL ) {
763 exit( EXIT_FAILURE );
769 if ( pmods[ i ]->mod_bvalues != NULL ) {
770 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
775 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
776 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
777 if ( pmods[ i ]->mod_bvalues == NULL ) {
778 perror( "ber_realloc" );
779 exit( EXIT_FAILURE );
782 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
783 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
784 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
785 perror( "ber_bvdup" );
786 exit( EXIT_FAILURE );
796 LDAPControl **pctrls,
799 int i, j, k, notascii, op;
802 if ( pmods == NULL ) {
803 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
805 return( LDAP_PARAM_ERROR );
808 for ( i = 0; pmods[ i ] != NULL; ++i ) {
809 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
810 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
812 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
813 prog, pmods[i]->mod_type, dn );
814 return LDAP_PARAM_ERROR;
819 for ( i = 0; pmods[ i ] != NULL; ++i ) {
820 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
821 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
822 "replace" : op == LDAP_MOD_ADD ?
823 "add" : "delete", pmods[ i ]->mod_type );
824 if ( pmods[ i ]->mod_bvalues != NULL ) {
825 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
826 bvp = pmods[ i ]->mod_bvalues[ j ];
828 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
829 if ( !isascii( bvp->bv_val[ k ] )) {
835 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
837 printf( "\t%s\n", bvp->bv_val );
845 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
847 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
852 i = ldap_add_ext_s( ld, dn, pmods, pctrls, NULL );
854 i = ldap_modify_ext_s( ld, dn, pmods, pctrls, NULL );
856 if ( i != LDAP_SUCCESS ) {
857 /* print error message about failed update including DN */
858 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
859 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
860 } else if ( verbose ) {
861 printf( "modify complete\n" );
876 LDAPControl **pctrls )
880 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
882 if (( rc = ldap_delete_ext_s( ld, dn, pctrls, NULL )) != LDAP_SUCCESS ) {
883 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
884 ldap_perror( ld, "ldap_delete" );
885 } else if ( verbose ) {
886 printf( "delete complete" );
904 LDAPControl **pctrls )
909 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
911 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
912 newrdn, deleteoldrdn ? "do not " : "" );
915 if (( rc = ldap_rename_s( ld, dn, newrdn, newsup, deleteoldrdn, pctrls, NULL ))
917 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
918 ldap_perror( ld, "ldap_modrdn" );
920 printf( "modrdn completed\n" );
933 read_one_record( FILE *fp )
935 char *buf, line[ LDAPMOD_MAXLINE ];
941 while ( fgets( line, sizeof(line), fp ) != NULL ) {
942 int len = strlen( line );
944 if( len < 2 || ( len == 2 && *line == '\r' )) {
952 if ( lcur + len + 1 > lmax ) {
953 lmax = LDAPMOD_MAXLINE
954 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
956 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
958 exit( EXIT_FAILURE );
962 strcpy( buf + lcur, line );