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"
39 #define _OLV_APP "ldapmodify"
41 #include "ol_version.h"
44 static int ldapadd, force = 0;
45 static char *rejfile = NULL;
46 static LDAP *ld = NULL;
48 #define LDAPMOD_MAXLINE 4096
50 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
51 #define T_VERSION_STR "version"
52 #define T_REPLICA_STR "replica"
54 #define T_CONTROL_STR "control"
55 #define T_CHANGETYPESTR "changetype"
56 #define T_ADDCTSTR "add"
57 #define T_MODIFYCTSTR "modify"
58 #define T_DELETECTSTR "delete"
59 #define T_MODRDNCTSTR "modrdn"
60 #define T_MODDNCTSTR "moddn"
61 #define T_RENAMECTSTR "rename"
62 #define T_MODOPADDSTR "add"
63 #define T_MODOPREPLACESTR "replace"
64 #define T_MODOPDELETESTR "delete"
65 #define T_MODSEPSTR "-"
66 #define T_NEWRDNSTR "newrdn"
67 #define T_DELETEOLDRDNSTR "deleteoldrdn"
68 #define T_NEWSUPSTR "newsuperior"
71 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
72 static int parse_ldif_control LDAP_P(( char *line, LDAPControl ***pctrls ));
73 static void addmodifyop LDAP_P((
74 LDAPMod ***pmodsp, int modop,
76 struct berval *value ));
77 static int domodify LDAP_P((
82 static int dodelete LDAP_P((
84 LDAPControl **pctrls ));
85 static int dorename LDAP_P((
90 LDAPControl **pctrls ));
91 static char *read_one_record LDAP_P(( FILE *fp ));
97 "Add or modify entries from an LDAP server\n\n"
98 "usage: %s [options]\n"
99 " The list of desired operations are read from stdin or from the file\n"
100 " specified by \"-f file\".\n"
101 "Add or modify options:\n"
102 " -a add values (default%s)\n"
103 " -F force all changes records to be used\n"
104 " -S file write skipped modifications to `file'\n"
105 , prog, (ldapadd ? "" : " is to replace") );
107 exit( EXIT_FAILURE );
111 const char options[] = "aFrS:"
112 "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
115 handle_private_option( int i )
119 char *control, *cvalue;
121 case 'E': /* modify controls */
122 if( protocol == LDAP_VERSION2 ) {
123 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
125 exit( EXIT_FAILURE );
128 /* should be extended to support comma separated list of
129 * [!]key[=value] parameters, e.g. -E !foo,bar=567
134 if( optarg[0] == '!' ) {
139 control = ber_strdup( optarg );
140 if ( (cvalue = strchr( control, '=' )) != NULL ) {
143 fprintf( stderr, "Invalid modify control name: %s\n", control );
151 case 'F': /* force all changes records to be used */
155 case 'r': /* replace (obsolete) */
158 case 'S': /* skipped modifications to file */
159 if( rejfile != NULL ) {
160 fprintf( stderr, "%s: -S previously specified\n", prog );
161 exit( EXIT_FAILURE );
163 rejfile = ber_strdup( optarg );
174 main( int argc, char **argv )
176 char *rbuf, *start, *rejbuf = NULL;
178 char *matched_msg, *error_msg;
182 prog = lutil_progname( "ldapmodify", argc, argv );
184 /* strncmp instead of strcmp since NT binaries carry .exe extension */
185 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
187 /* Print usage when no parameters */
188 if( argc < 2 ) usage();
190 tool_args( argc, argv );
192 if ( argc != optind )
195 if ( rejfile != NULL ) {
196 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
198 return( EXIT_FAILURE );
204 if ( infile != NULL ) {
205 if (( fp = fopen( infile, "r" )) == NULL ) {
207 return( EXIT_FAILURE );
216 ld = tool_conn_setup( not, 0 );
219 if ( pw_file || want_bindpw ) {
221 rc = lutil_get_filed_password( pw_file, &passwd );
222 if( rc ) return EXIT_FAILURE;
224 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
225 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
234 if ( authzid || manageDSAit || noop )
235 tool_server_controls( ld, NULL, 0 );
239 while (( rc == 0 || contoper ) &&
240 ( rbuf = read_one_record( fp )) != NULL ) {
246 len = strlen( rbuf );
247 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
249 exit( EXIT_FAILURE );
251 memcpy( rejbuf, rbuf, len+1 );
254 rc = process_ldif_rec( start, count );
259 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
262 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
263 if ( matched_msg != NULL ) {
264 if ( *matched_msg != '\0' )
265 fprintf( rejfp, ", matched DN: %s", matched_msg );
266 ldap_memfree( matched_msg );
270 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
271 if ( error_msg != NULL ) {
272 if ( *error_msg != '\0' )
273 fprintf( rejfp, ", additional info: %s", error_msg );
274 ldap_memfree( error_msg );
276 fprintf( rejfp, "\n%s\n", rejbuf );
287 if ( rejfp != NULL ) {
296 process_ldif_rec( char *rbuf, int count )
298 char *line, *dn, *type, *newrdn, *newsup, *p;
299 int rc, linenum, modop, replicaport;
300 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
301 int expect_deleteoldrdn, deleteoldrdn;
302 int saw_replica, use_record, new_entry, delete_entry, got_all;
306 LDAPControl **pctrls;
310 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
311 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
312 expect_sep = expect_ct = 0;
319 dn = newrdn = newsup = NULL;
321 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
324 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
330 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
331 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
332 prog, linenum, dn == NULL ? "" : dn );
333 rc = LDAP_PARAM_ERROR;
338 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
340 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
344 replicaport = atoi( p );
346 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
347 replicaport == ldapport ) {
350 } else if ( count == 1 && linenum == 1 &&
351 strcasecmp( type, T_VERSION_STR ) == 0 )
353 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
354 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
355 prog, val.bv_val, linenum );
359 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
360 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
362 exit( EXIT_FAILURE );
366 goto end_line; /* skip all lines until we see "dn:" */
371 /* Check for "control" tag after dn and before changetype. */
372 if (strcasecmp(type, T_CONTROL_STR) == 0) {
373 /* Parse and add it to the list of controls */
374 rc = parse_ldif_control( line, &pctrls );
376 fprintf( stderr, "%s: Error processing %s line, line %d: %s\n",
377 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
383 if ( !use_record && saw_replica ) {
384 printf( "%s: skipping change record for entry: %s\n"
385 "\t(LDAP host/port does not match replica: lines)\n",
389 ber_memfree( val.bv_val );
393 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
394 #ifdef LIBERAL_CHANGETYPE_MODOP
395 /* trim trailing spaces (and log warning ...) */
398 for ( icnt = val.bv_len; --icnt > 0; ) {
399 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
404 if ( ++icnt != val.bv_len ) {
405 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
406 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
407 val.bv_val[icnt] = '\0';
409 #endif /* LIBERAL_CHANGETYPE_MODOP */
411 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
414 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
416 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
417 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
418 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
421 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
422 got_all = delete_entry = 1;
425 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
426 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
427 rc = LDAP_PARAM_ERROR;
430 } else if ( ldapadd ) { /* missing changetype => add */
432 modop = LDAP_MOD_ADD;
434 expect_modop = 1; /* missing changetype => modify */
438 if ( expect_modop ) {
439 #ifdef LIBERAL_CHANGETYPE_MODOP
440 /* trim trailing spaces (and log warning ...) */
443 for ( icnt = val.bv_len; --icnt > 0; ) {
444 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
449 if ( ++icnt != val.bv_len ) {
450 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
451 prog, type, val.bv_val, linenum, dn );
452 val.bv_val[icnt] = '\0';
454 #endif /* LIBERAL_CHANGETYPE_MODOP */
458 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
459 modop = LDAP_MOD_ADD;
461 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
462 modop = LDAP_MOD_REPLACE;
463 addmodifyop( &pmods, modop, val.bv_val, NULL );
465 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
466 modop = LDAP_MOD_DELETE;
467 addmodifyop( &pmods, modop, val.bv_val, NULL );
469 } else { /* no modify op: use default */
470 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
474 if ( expect_newrdn ) {
475 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
476 if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
478 exit( EXIT_FAILURE );
480 expect_deleteoldrdn = 1;
483 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
484 prog, T_NEWRDNSTR, type, linenum, dn );
485 rc = LDAP_PARAM_ERROR;
487 } else if ( expect_deleteoldrdn ) {
488 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
489 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
490 expect_deleteoldrdn = 0;
494 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
495 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
496 rc = LDAP_PARAM_ERROR;
498 } else if ( expect_newsup ) {
499 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
500 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
502 exit( EXIT_FAILURE );
506 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
507 prog, T_NEWSUPSTR, type, linenum, dn );
508 rc = LDAP_PARAM_ERROR;
510 } else if ( got_all ) {
512 "%s: extra lines at end (line %d of entry \"%s\")\n",
514 rc = LDAP_PARAM_ERROR;
516 addmodifyop( &pmods, modop, type, &val );
521 ber_memfree( val.bv_val );
528 if( version && linenum == 1 ) {
532 /* If default controls are set (as with -M option) and controls are
533 specified in the LDIF file, we must add the default controls to
534 the list of controls sent with the ldap operation.
538 LDAPControl **defctrls = NULL; /* Default server controls */
539 LDAPControl **newctrls = NULL;
540 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
542 int npc=0; /* Number of LDIF controls */
543 int ndefc=0; /* Number of default controls */
544 while (pctrls[npc]) /* Count LDIF controls */
546 while (defctrls[ndefc]) /* Count default controls */
548 newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*));
549 if (newctrls == NULL)
554 for (i=npc; i<npc+ndefc; i++) {
555 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
556 if (pctrls[i] == NULL) {
561 pctrls[npc+ndefc] = NULL;
563 ldap_controls_free(defctrls); /* Must be freed by library */
570 if ( delete_entry ) {
571 rc = dodelete( dn, pctrls );
572 } else if ( newrdn != NULL ) {
573 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
575 rc = domodify( dn, pmods, pctrls, new_entry );
578 if ( rc == LDAP_SUCCESS ) {
586 if ( newrdn != NULL ) {
589 if ( pmods != NULL ) {
590 ldap_mods_free( pmods, 1 );
593 if (pctrls != NULL) {
594 ldap_controls_free( pctrls );
600 /* Parse an LDIF control line of the form
601 control: oid [true/false] [: value] or
602 control: oid [true/false] [:: base64-value] or
603 control: oid [true/false] [:< url]
604 The control is added to the list of controls in *ppctrls.
607 parse_ldif_control( char *line,
608 LDAPControl ***ppctrls )
611 int criticality = 0; /* Default is false if not present */
614 ber_len_t value_len = 0;
616 char *s, *oidStart, *pcolon;
617 LDAPControl *newctrl = NULL;
618 LDAPControl **pctrls = NULL;
623 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
624 pcolon = s; /* Save this position for later */
625 if (*s++ != ':') /* Make sure colon follows */
626 return ( LDAP_PARAM_ERROR );
627 while (*s && isspace((unsigned char)*s))
628 s++; /* Skip white space before OID */
630 /* OID should come next. Validate and extract it. */
632 return ( LDAP_PARAM_ERROR );
634 while (isdigit((unsigned char)*s) || *s == '.')
635 s++; /* OID should be digits or . */
637 return ( LDAP_PARAM_ERROR ); /* OID was not present */
638 if (*s) { /* End of OID should be space or NULL */
639 if (!isspace((unsigned char)*s))
640 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
641 *s++ = 0; /* Replace space with null to terminate */
645 oid = ber_strdup(oidStart);
647 return ( LDAP_NO_MEMORY );
649 /* Optional Criticality field is next. */
650 while (*s && isspace((unsigned char)*s))
651 s++; /* Skip white space before criticality */
652 if (strncasecmp(s, "true", 4) == 0) {
656 else if (strncasecmp(s, "false", 5) == 0) {
661 /* Optional value field is next */
662 while (*s && isspace((unsigned char)*s))
663 s++; /* Skip white space before value */
665 if (*s != ':') { /* If value is present, must start with : */
666 rc = LDAP_PARAM_ERROR;
670 /* Shift value down over OID and criticality so it's in the form
672 control:: base64-value
674 Then we can use ldif_parse_line to extract and decode the value
676 while ( (*pcolon++ = *s++) != 0) /* Shift value */
678 rc = ldif_parse_line(line, &type, &val, &value_len);
679 if (type) ber_memfree(type); /* Don't need this field*/
681 rc = LDAP_PARAM_ERROR;
686 /* Create a new LDAPControl structure. */
687 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
688 if ( newctrl == NULL ) {
692 newctrl->ldctl_oid = oid;
694 newctrl->ldctl_iscritical = criticality;
695 newctrl->ldctl_value.bv_len = value_len;
696 newctrl->ldctl_value.bv_val = val;
699 /* Add the new control to the passed-in list of controls. */
702 while ( pctrls[i] ) /* Count the # of controls passed in */
705 /* Allocate 1 more slot for the new control and 1 for the NULL. */
706 pctrls = (LDAPControl **)ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *)));
707 if (pctrls == NULL) {
718 if (newctrl->ldctl_oid)
719 ber_memfree(newctrl->ldctl_oid);
720 if (newctrl->ldctl_value.bv_val)
721 ber_memfree(newctrl->ldctl_value.bv_val);
722 ber_memfree(newctrl);
744 modop |= LDAP_MOD_BVALUES;
747 if ( pmods != NULL ) {
748 for ( ; pmods[ i ] != NULL; ++i ) {
749 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
750 pmods[ i ]->mod_op == modop )
757 if ( pmods == NULL || pmods[ i ] == NULL ) {
758 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
759 sizeof( LDAPMod * ))) == NULL )
762 exit( EXIT_FAILURE );
766 pmods[ i + 1 ] = NULL;
768 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
769 if ( pmods[ i ] == NULL ) {
771 exit( EXIT_FAILURE );
774 pmods[ i ]->mod_op = modop;
775 pmods[ i ]->mod_type = ber_strdup( attr );
776 if ( pmods[ i ]->mod_type == NULL ) {
778 exit( EXIT_FAILURE );
784 if ( pmods[ i ]->mod_bvalues != NULL ) {
785 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
790 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
791 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
792 if ( pmods[ i ]->mod_bvalues == NULL ) {
793 perror( "ber_realloc" );
794 exit( EXIT_FAILURE );
797 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
798 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
799 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
800 perror( "ber_bvdup" );
801 exit( EXIT_FAILURE );
811 LDAPControl **pctrls,
814 int i, j, k, notascii, op;
817 if ( pmods == NULL ) {
818 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
820 return( LDAP_PARAM_ERROR );
823 for ( i = 0; pmods[ i ] != NULL; ++i ) {
824 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
825 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
827 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
828 prog, pmods[i]->mod_type, dn );
829 return LDAP_PARAM_ERROR;
834 for ( i = 0; pmods[ i ] != NULL; ++i ) {
835 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
836 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
837 "replace" : op == LDAP_MOD_ADD ?
838 "add" : "delete", pmods[ i ]->mod_type );
839 if ( pmods[ i ]->mod_bvalues != NULL ) {
840 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
841 bvp = pmods[ i ]->mod_bvalues[ j ];
843 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
844 if ( !isascii( bvp->bv_val[ k ] )) {
850 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
852 printf( "\t%s\n", bvp->bv_val );
860 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
862 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
867 i = ldap_add_ext_s( ld, dn, pmods, pctrls, NULL );
869 i = ldap_modify_ext_s( ld, dn, pmods, pctrls, NULL );
871 if ( i != LDAP_SUCCESS ) {
872 /* print error message about failed update including DN */
873 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
874 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
875 } else if ( verbose ) {
876 printf( "modify complete\n" );
891 LDAPControl **pctrls )
895 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
897 if (( rc = ldap_delete_ext_s( ld, dn, pctrls, NULL )) != LDAP_SUCCESS ) {
898 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
899 ldap_perror( ld, "ldap_delete" );
900 } else if ( verbose ) {
901 printf( "delete complete" );
919 LDAPControl **pctrls )
924 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
926 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
927 newrdn, deleteoldrdn ? "do not " : "" );
930 if (( rc = ldap_rename_s( ld, dn, newrdn, newsup, deleteoldrdn, pctrls, NULL ))
932 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
933 ldap_perror( ld, "ldap_modrdn" );
935 printf( "modrdn completed\n" );
948 read_one_record( FILE *fp )
950 char *buf, line[ LDAPMOD_MAXLINE ];
956 while ( fgets( line, sizeof(line), fp ) != NULL ) {
957 int len = strlen( line );
959 if( len < 2 || ( len == 2 && *line == '\r' )) {
967 if ( lcur + len + 1 > lmax ) {
968 lmax = LDAPMOD_MAXLINE
969 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
971 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
973 exit( EXIT_FAILURE );
977 strcpy( buf + lcur, line );