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, *error_msg;
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);
257 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
258 if ( matched_msg != NULL ) {
259 if ( *matched_msg != '\0' )
260 fprintf( rejfp, ", matched DN: %s", matched_msg );
261 ldap_memfree( matched_msg );
265 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
266 if ( error_msg != NULL ) {
267 if ( *error_msg != '\0' )
268 fprintf( rejfp, ", additional info: %s", error_msg );
269 ldap_memfree( error_msg );
271 fprintf( rejfp, "\n%s\n", rejbuf );
282 if ( rejfp != NULL ) {
291 process_ldif_rec( char *rbuf, int count )
293 char *line, *dn, *type, *newrdn, *newsup, *p;
294 int rc, linenum, modop, replicaport;
295 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
296 int expect_deleteoldrdn, deleteoldrdn;
297 int saw_replica, use_record, new_entry, delete_entry, got_all;
301 LDAPControl **pctrls;
305 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
306 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
307 expect_sep = expect_ct = 0;
314 dn = newrdn = newsup = NULL;
316 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
319 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
325 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
326 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
327 prog, linenum, dn == NULL ? "" : dn );
328 rc = LDAP_PARAM_ERROR;
333 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
335 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
339 replicaport = atoi( p );
341 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
342 replicaport == ldapport ) {
345 } else if ( count == 1 && linenum == 1 &&
346 strcasecmp( type, T_VERSION_STR ) == 0 )
348 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
349 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
350 prog, val.bv_val, linenum );
354 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
355 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
357 exit( EXIT_FAILURE );
361 goto end_line; /* skip all lines until we see "dn:" */
366 /* Check for "control" tag after dn and before changetype. */
367 if (strcasecmp(type, T_CONTROL_STR) == 0) {
368 /* Parse and add it to the list of controls */
369 rc = parse_ldif_control( line, &pctrls );
371 fprintf( stderr, "%s: Error processing %s line, line %d: %s\n",
372 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
378 if ( !use_record && saw_replica ) {
379 printf( "%s: skipping change record for entry: %s\n"
380 "\t(LDAP host/port does not match replica: lines)\n",
384 ber_memfree( val.bv_val );
388 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
389 #ifdef LIBERAL_CHANGETYPE_MODOP
390 /* trim trailing spaces (and log warning ...) */
393 for ( icnt = val.bv_len; --icnt > 0; ) {
394 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
399 if ( ++icnt != val.bv_len ) {
400 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
401 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
402 val.bv_val[icnt] = '\0';
404 #endif /* LIBERAL_CHANGETYPE_MODOP */
406 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
409 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
411 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
412 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
413 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
416 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
417 got_all = delete_entry = 1;
420 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
421 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
422 rc = LDAP_PARAM_ERROR;
425 } else if ( ldapadd ) { /* missing changetype => add */
427 modop = LDAP_MOD_ADD;
429 expect_modop = 1; /* missing changetype => modify */
433 if ( expect_modop ) {
434 #ifdef LIBERAL_CHANGETYPE_MODOP
435 /* trim trailing spaces (and log warning ...) */
438 for ( icnt = val.bv_len; --icnt > 0; ) {
439 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
444 if ( ++icnt != val.bv_len ) {
445 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
446 prog, type, val.bv_val, linenum, dn );
447 val.bv_val[icnt] = '\0';
449 #endif /* LIBERAL_CHANGETYPE_MODOP */
453 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
454 modop = LDAP_MOD_ADD;
456 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
457 modop = LDAP_MOD_REPLACE;
458 addmodifyop( &pmods, modop, val.bv_val, NULL );
460 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
461 modop = LDAP_MOD_DELETE;
462 addmodifyop( &pmods, modop, val.bv_val, NULL );
464 } else { /* no modify op: use default */
465 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
469 if ( expect_newrdn ) {
470 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
471 if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
473 exit( EXIT_FAILURE );
475 expect_deleteoldrdn = 1;
478 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
479 prog, T_NEWRDNSTR, type, linenum, dn );
480 rc = LDAP_PARAM_ERROR;
482 } else if ( expect_deleteoldrdn ) {
483 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
484 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
485 expect_deleteoldrdn = 0;
489 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
490 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
491 rc = LDAP_PARAM_ERROR;
493 } else if ( expect_newsup ) {
494 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
495 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
497 exit( EXIT_FAILURE );
501 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
502 prog, T_NEWSUPSTR, type, linenum, dn );
503 rc = LDAP_PARAM_ERROR;
505 } else if ( got_all ) {
507 "%s: extra lines at end (line %d of entry \"%s\")\n",
509 rc = LDAP_PARAM_ERROR;
511 addmodifyop( &pmods, modop, type, &val );
516 ber_memfree( val.bv_val );
523 if( version && linenum == 1 ) {
527 /* If default controls are set (as with -M option) and controls are
528 specified in the LDIF file, we must add the default controls to
529 the list of controls sent with the ldap operation.
533 LDAPControl **defctrls = NULL; /* Default server controls */
534 LDAPControl **newctrls = NULL;
535 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
537 int npc=0; /* Number of LDIF controls */
538 int ndefc=0; /* Number of default controls */
539 while (pctrls[npc]) /* Count LDIF controls */
541 while (defctrls[ndefc]) /* Count default controls */
543 newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*));
544 if (newctrls == NULL)
549 for (i=npc; i<npc+ndefc; i++) {
550 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
551 if (pctrls[i] == NULL) {
556 pctrls[npc+ndefc] = NULL;
558 ldap_controls_free(defctrls); /* Must be freed by library */
565 if ( delete_entry ) {
566 rc = dodelete( dn, pctrls );
567 } else if ( newrdn != NULL ) {
568 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
570 rc = domodify( dn, pmods, pctrls, new_entry );
573 if ( rc == LDAP_SUCCESS ) {
581 if ( newrdn != NULL ) {
584 if ( pmods != NULL ) {
585 ldap_mods_free( pmods, 1 );
588 if (pctrls != NULL) {
589 ldap_controls_free( pctrls );
595 /* Parse an LDIF control line of the form
596 control: oid [true/false] [: value] or
597 control: oid [true/false] [:: base64-value] or
598 control: oid [true/false] [:< url]
599 The control is added to the list of controls in *ppctrls.
602 parse_ldif_control( char *line,
603 LDAPControl ***ppctrls )
606 int criticality = 0; /* Default is false if not present */
609 ber_len_t value_len = 0;
611 char *s, *oidStart, *pcolon;
612 LDAPControl *newctrl = NULL;
613 LDAPControl **pctrls = NULL;
618 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
619 pcolon = s; /* Save this position for later */
620 if (*s++ != ':') /* Make sure colon follows */
621 return ( LDAP_PARAM_ERROR );
622 while (*s && isspace(*s)) s++; /* Skip white space before OID */
624 /* OID should come next. Validate and extract it. */
626 return ( LDAP_PARAM_ERROR );
628 while (isdigit(*s) || *s == '.') s++; /* OID should be digits or . */
630 return ( LDAP_PARAM_ERROR ); /* OID was not present */
631 if (*s) { /* End of OID should be space or NULL */
633 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
634 *s++ = 0; /* Replace space with null to terminate */
638 oid = ber_strdup(oidStart);
640 return ( LDAP_NO_MEMORY );
642 /* Optional Criticality field is next. */
643 while (*s && isspace(*s)) s++; /* Skip white space before criticality */
644 if (strncasecmp(s, "true", 4) == 0) {
648 else if (strncasecmp(s, "false", 5) == 0) {
653 /* Optional value field is next */
654 while (*s && isspace(*s)) s++; /* Skip white space before value */
656 if (*s != ':') { /* If value is present, must start with : */
657 rc = LDAP_PARAM_ERROR;
661 /* Shift value down over OID and criticality so it's in the form
663 control:: base64-value
665 Then we can use ldif_parse_line to extract and decode the value
667 while ( (*pcolon++ = *s++) != 0) /* Shift value */
669 rc = ldif_parse_line(line, &type, &val, &value_len);
670 if (type) ber_memfree(type); /* Don't need this field*/
672 rc = LDAP_PARAM_ERROR;
677 /* Create a new LDAPControl structure. */
678 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
679 if ( newctrl == NULL ) {
683 newctrl->ldctl_oid = oid;
685 newctrl->ldctl_iscritical = criticality;
686 newctrl->ldctl_value.bv_len = value_len;
687 newctrl->ldctl_value.bv_val = val;
690 /* Add the new control to the passed-in list of controls. */
693 while ( pctrls[i] ) /* Count the # of controls passed in */
696 /* Allocate 1 more slot for the new control and 1 for the NULL. */
697 pctrls = (LDAPControl **)ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *)));
698 if (pctrls == NULL) {
709 if (newctrl->ldctl_oid)
710 ber_memfree(newctrl->ldctl_oid);
711 if (newctrl->ldctl_value.bv_val)
712 ber_memfree(newctrl->ldctl_value.bv_val);
713 ber_memfree(newctrl);
735 modop |= LDAP_MOD_BVALUES;
738 if ( pmods != NULL ) {
739 for ( ; pmods[ i ] != NULL; ++i ) {
740 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
741 pmods[ i ]->mod_op == modop )
748 if ( pmods == NULL || pmods[ i ] == NULL ) {
749 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
750 sizeof( LDAPMod * ))) == NULL )
753 exit( EXIT_FAILURE );
757 pmods[ i + 1 ] = NULL;
759 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
760 if ( pmods[ i ] == NULL ) {
762 exit( EXIT_FAILURE );
765 pmods[ i ]->mod_op = modop;
766 pmods[ i ]->mod_type = ber_strdup( attr );
767 if ( pmods[ i ]->mod_type == NULL ) {
769 exit( EXIT_FAILURE );
775 if ( pmods[ i ]->mod_bvalues != NULL ) {
776 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
781 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
782 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
783 if ( pmods[ i ]->mod_bvalues == NULL ) {
784 perror( "ber_realloc" );
785 exit( EXIT_FAILURE );
788 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
789 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
790 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
791 perror( "ber_bvdup" );
792 exit( EXIT_FAILURE );
802 LDAPControl **pctrls,
805 int i, j, k, notascii, op;
808 if ( pmods == NULL ) {
809 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
811 return( LDAP_PARAM_ERROR );
814 for ( i = 0; pmods[ i ] != NULL; ++i ) {
815 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
816 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
818 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
819 prog, pmods[i]->mod_type, dn );
820 return LDAP_PARAM_ERROR;
825 for ( i = 0; pmods[ i ] != NULL; ++i ) {
826 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
827 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
828 "replace" : op == LDAP_MOD_ADD ?
829 "add" : "delete", pmods[ i ]->mod_type );
830 if ( pmods[ i ]->mod_bvalues != NULL ) {
831 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
832 bvp = pmods[ i ]->mod_bvalues[ j ];
834 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
835 if ( !isascii( bvp->bv_val[ k ] )) {
841 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
843 printf( "\t%s\n", bvp->bv_val );
851 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
853 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
858 i = ldap_add_ext_s( ld, dn, pmods, pctrls, NULL );
860 i = ldap_modify_ext_s( ld, dn, pmods, pctrls, NULL );
862 if ( i != LDAP_SUCCESS ) {
863 /* print error message about failed update including DN */
864 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
865 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
866 } else if ( verbose ) {
867 printf( "modify complete\n" );
882 LDAPControl **pctrls )
886 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
888 if (( rc = ldap_delete_ext_s( ld, dn, pctrls, NULL )) != LDAP_SUCCESS ) {
889 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
890 ldap_perror( ld, "ldap_delete" );
891 } else if ( verbose ) {
892 printf( "delete complete" );
910 LDAPControl **pctrls )
915 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
917 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
918 newrdn, deleteoldrdn ? "do not " : "" );
921 if (( rc = ldap_rename_s( ld, dn, newrdn, newsup, deleteoldrdn, pctrls, NULL ))
923 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
924 ldap_perror( ld, "ldap_modrdn" );
926 printf( "modrdn completed\n" );
939 read_one_record( FILE *fp )
941 char *buf, line[ LDAPMOD_MAXLINE ];
947 while ( fgets( line, sizeof(line), fp ) != NULL ) {
948 int len = strlen( line );
950 if( len < 2 || ( len == 2 && *line == '\r' )) {
958 if ( lcur + len + 1 > lmax ) {
959 lmax = LDAPMOD_MAXLINE
960 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
962 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
964 exit( EXIT_FAILURE );
968 strcpy( buf + lcur, line );