3 * Copyright 1998-2000 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>
15 #include <ac/signal.h>
16 #include <ac/string.h>
17 #include <ac/unistd.h>
19 #ifdef HAVE_SYS_STAT_H
23 #ifdef HAVE_SYS_FILE_H
32 #include "lutil_ldap.h"
34 #include "ldap_defaults.h"
37 static char *binddn = NULL;
38 static struct berval passwd = { 0, NULL };
39 static char *ldapuri = NULL;
40 static char *ldaphost = NULL;
41 static int ldapport = 0;
42 #ifdef HAVE_CYRUS_SASL
43 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
44 static char *sasl_realm = NULL;
45 static char *sasl_authc_id = NULL;
46 static char *sasl_authz_id = NULL;
47 static char *sasl_mech = NULL;
48 static char *sasl_secprops = NULL;
50 static int use_tls = 0;
51 static int ldapadd, not, verbose, contoper, force;
52 static LDAP *ld = NULL;
54 #define LDAPMOD_MAXLINE 4096
56 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
57 #define T_VERSION_STR "version"
58 #define T_REPLICA_STR "replica"
60 #define T_CHANGETYPESTR "changetype"
61 #define T_ADDCTSTR "add"
62 #define T_MODIFYCTSTR "modify"
63 #define T_DELETECTSTR "delete"
64 #define T_MODRDNCTSTR "modrdn"
65 #define T_MODDNCTSTR "moddn"
66 #define T_RENAMECTSTR "rename"
67 #define T_MODOPADDSTR "add"
68 #define T_MODOPREPLACESTR "replace"
69 #define T_MODOPDELETESTR "delete"
70 #define T_MODSEPSTR "-"
71 #define T_NEWRDNSTR "newrdn"
72 #define T_DELETEOLDRDNSTR "deleteoldrdn"
73 #define T_NEWSUPSTR "newsuperior"
76 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
77 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
78 static void addmodifyop LDAP_P((
79 LDAPMod ***pmodsp, int modop,
81 struct berval *value ));
82 static int domodify LDAP_P((
86 static int dodelete LDAP_P((
88 static int dorename LDAP_P((
93 static char *read_one_record LDAP_P(( FILE *fp ));
96 usage( const char *prog )
99 "Add or modify entries from an LDAP server\n\n"
100 "usage: %s [options]\n"
101 " The list of desired operations are read from stdin or from the file\n"
102 " specified by \"-f file\".\n"
103 "Add or modify options:\n"
104 " -a add values (default%s)\n"
105 " -F force all changes records to be used\n"
108 " -d level set LDAP debugging level to `level'\n"
109 " -D binddn bind DN\n"
110 " -f file read operations from `file'\n"
111 " -h host LDAP server\n"
112 " -H URI LDAP Uniform Resource Indentifier(s)\n"
113 " -I use SASL Interactive mode\n"
114 " -k use Kerberos authentication\n"
115 " -K like -k, but do only step 1 of the Kerberos bind\n"
116 " -M enable Manage DSA IT control (-MM to make critical)\n"
117 " -n show what would be done but don't actually search\n"
118 " -O props SASL security properties\n"
119 " -p port port on LDAP server\n"
120 " -P version procotol version (default: 3)\n"
121 " -Q use SASL Quiet mode\n"
122 " -R realm SASL realm\n"
123 " -U authcid SASL authentication identity\n"
124 " -v run in verbose mode (diagnostics to standard output)\n"
125 " -w passwd bind passwd (for simple authentication)\n"
126 " -W prompt for bind passwd\n"
127 " -x Simple authentication\n"
128 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
129 " -Y mech SASL mechanism\n"
130 " -Z Start TLS request (-ZZ to require successful response)\n"
131 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
133 exit( EXIT_FAILURE );
138 main( int argc, char **argv )
140 char *infile, *rbuf, *start;
142 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
145 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
151 /* Print usage when no parameters */
152 if( argc < 2 ) usage( prog );
154 ldapadd = ( strcmp( prog, "ldapadd" ) == 0 );
157 not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
161 while (( i = getopt( argc, argv, "acrf:F"
162 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z" )) != EOF )
169 case 'c': /* continuous operation */
172 case 'f': /* read from file */
173 if( infile != NULL ) {
174 fprintf( stderr, "%s: -f previously specified\n", prog );
177 infile = strdup( optarg );
179 case 'F': /* force all changes records to be used */
188 debug |= atoi( optarg );
190 case 'D': /* bind DN */
191 if( binddn != NULL ) {
192 fprintf( stderr, "%s: -D previously specified\n", prog );
195 binddn = strdup( optarg );
197 case 'h': /* ldap host */
198 if( ldapuri != NULL ) {
199 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
202 if( ldaphost != NULL ) {
203 fprintf( stderr, "%s: -h previously specified\n", prog );
206 ldaphost = strdup( optarg );
208 case 'H': /* ldap URI */
209 if( ldaphost != NULL ) {
210 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
214 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
217 if( ldapuri != NULL ) {
218 fprintf( stderr, "%s: -H previously specified\n", prog );
221 ldapuri = strdup( optarg );
224 #ifdef HAVE_CYRUS_SASL
225 if( version == LDAP_VERSION2 ) {
226 fprintf( stderr, "%s: -I incompatible with version %d\n",
230 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
231 fprintf( stderr, "%s: incompatible previous "
232 "authentication choice\n",
236 authmethod = LDAP_AUTH_SASL;
237 version = LDAP_VERSION3;
238 sasl_flags = LDAP_SASL_INTERACTIVE;
241 fprintf( stderr, "%s: was not compiled with SASL support\n",
243 return( EXIT_FAILURE );
245 case 'k': /* kerberos bind */
246 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
247 if( version > LDAP_VERSION2 ) {
248 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
253 if( authmethod != -1 ) {
254 fprintf( stderr, "%s: -k incompatible with previous "
255 "authentication choice\n", prog );
259 authmethod = LDAP_AUTH_KRBV4;
261 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
265 case 'K': /* kerberos bind, part one only */
266 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
267 if( version > LDAP_VERSION2 ) {
268 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
272 if( authmethod != -1 ) {
273 fprintf( stderr, "%s: incompatible with previous "
274 "authentication choice\n", prog );
278 authmethod = LDAP_AUTH_KRBV41;
280 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
281 return( EXIT_FAILURE );
285 /* enable Manage DSA IT */
286 if( version == LDAP_VERSION2 ) {
287 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
292 version = LDAP_VERSION3;
294 case 'n': /* print deletes, don't actually do them */
298 #ifdef HAVE_CYRUS_SASL
299 if( sasl_secprops != NULL ) {
300 fprintf( stderr, "%s: -O previously specified\n", prog );
303 if( version == LDAP_VERSION2 ) {
304 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
308 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
309 fprintf( stderr, "%s: incompatible previous "
310 "authentication choice\n", prog );
313 authmethod = LDAP_AUTH_SASL;
314 version = LDAP_VERSION3;
315 sasl_secprops = strdup( optarg );
317 fprintf( stderr, "%s: not compiled with SASL support\n",
319 return( EXIT_FAILURE );
324 fprintf( stderr, "%s: -p previously specified\n", prog );
327 ldapport = atoi( optarg );
330 switch( atoi(optarg) ) {
332 if( version == LDAP_VERSION3 ) {
333 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
337 version = LDAP_VERSION2;
340 if( version == LDAP_VERSION2 ) {
341 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
345 version = LDAP_VERSION3;
348 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
351 return( EXIT_FAILURE );
354 #ifdef HAVE_CYRUS_SASL
355 if( version == LDAP_VERSION2 ) {
356 fprintf( stderr, "%s: -Q incompatible with version %d\n",
360 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
361 fprintf( stderr, "%s: incompatible previous "
362 "authentication choice\n",
366 authmethod = LDAP_AUTH_SASL;
367 version = LDAP_VERSION3;
368 sasl_flags = LDAP_SASL_QUIET;
371 fprintf( stderr, "%s: not compiled with SASL support\n",
373 return( EXIT_FAILURE );
375 case 'r': /* replace (obsolete) */
379 #ifdef HAVE_CYRUS_SASL
380 if( sasl_realm != NULL ) {
381 fprintf( stderr, "%s: -R previously specified\n", prog );
384 if( version == LDAP_VERSION2 ) {
385 fprintf( stderr, "%s: -R incompatible with version %d\n",
389 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
390 fprintf( stderr, "%s: incompatible previous "
391 "authentication choice\n",
395 authmethod = LDAP_AUTH_SASL;
396 version = LDAP_VERSION3;
397 sasl_realm = strdup( optarg );
399 fprintf( stderr, "%s: not compiled with SASL support\n",
401 return( EXIT_FAILURE );
405 #ifdef HAVE_CYRUS_SASL
406 if( sasl_authc_id != NULL ) {
407 fprintf( stderr, "%s: -U previously specified\n", prog );
410 if( version == LDAP_VERSION2 ) {
411 fprintf( stderr, "%s: -U incompatible with version %d\n",
415 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
416 fprintf( stderr, "%s: incompatible previous "
417 "authentication choice\n",
421 authmethod = LDAP_AUTH_SASL;
422 version = LDAP_VERSION3;
423 sasl_authc_id = strdup( optarg );
425 fprintf( stderr, "%s: not compiled with SASL support\n",
427 return( EXIT_FAILURE );
430 case 'v': /* verbose mode */
433 case 'w': /* password */
434 passwd.bv_val = strdup( optarg );
438 for( p = optarg; *p != '\0'; p++ ) {
442 passwd.bv_len = strlen( passwd.bv_val );
448 #ifdef HAVE_CYRUS_SASL
449 if( sasl_mech != NULL ) {
450 fprintf( stderr, "%s: -Y previously specified\n", prog );
453 if( version == LDAP_VERSION2 ) {
454 fprintf( stderr, "%s: -Y incompatible with version %d\n",
458 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
459 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
462 authmethod = LDAP_AUTH_SASL;
463 version = LDAP_VERSION3;
464 sasl_mech = strdup( optarg );
466 fprintf( stderr, "%s: not compiled with SASL support\n",
468 return( EXIT_FAILURE );
472 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
473 fprintf( stderr, "%s: incompatible with previous "
474 "authentication choice\n", prog );
477 authmethod = LDAP_AUTH_SIMPLE;
480 #ifdef HAVE_CYRUS_SASL
481 if( sasl_authz_id != NULL ) {
482 fprintf( stderr, "%s: -X previously specified\n", prog );
485 if( version == LDAP_VERSION2 ) {
486 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
490 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
491 fprintf( stderr, "%s: -X incompatible with "
492 "authentication choice\n", prog );
495 authmethod = LDAP_AUTH_SASL;
496 version = LDAP_VERSION3;
497 sasl_authz_id = strdup( optarg );
499 fprintf( stderr, "%s: not compiled with SASL support\n",
501 return( EXIT_FAILURE );
506 if( version == LDAP_VERSION2 ) {
507 fprintf( stderr, "%s: -Z incompatible with version %d\n",
511 version = LDAP_VERSION3;
514 fprintf( stderr, "%s: not compiled with TLS support\n",
516 return( EXIT_FAILURE );
520 fprintf( stderr, "%s: unrecognized option -%c\n",
527 version = LDAP_VERSION3;
529 if (authmethod == -1 && version > LDAP_VERSION2) {
530 #ifdef HAVE_CYRUS_SASL
531 authmethod = LDAP_AUTH_SASL;
533 authmethod = LDAP_AUTH_SIMPLE;
537 if ( argc != optind )
540 if ( infile != NULL ) {
541 if (( fp = fopen( infile, "r" )) == NULL ) {
543 return( EXIT_FAILURE );
550 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
551 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
553 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
554 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
560 (void) SIGNAL( SIGPIPE, SIG_IGN );
564 lutil_log_initialize( argc, argv );
568 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
570 fprintf( stderr, "ldap_init( %s, %d )\n",
571 ldaphost != NULL ? ldaphost : "<DEFAULT>",
575 ld = ldap_init( ldaphost, ldapport );
577 perror("ldapsearch: ldap_init");
583 fprintf( stderr, "ldap_initialize( %s )\n",
584 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
587 rc = ldap_initialize( &ld, ldapuri );
588 if( rc != LDAP_SUCCESS ) {
589 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
590 rc, ldap_err2string(rc) );
596 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
597 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
599 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
600 referrals ? "on" : "off" );
605 if (version == -1 ) {
606 version = LDAP_VERSION3;
609 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
610 != LDAP_OPT_SUCCESS )
612 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
617 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
618 ldap_perror( ld, "ldap_start_tls" );
620 return( EXIT_FAILURE );
625 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
626 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
629 if ( authmethod == LDAP_AUTH_SASL ) {
630 #ifdef HAVE_CYRUS_SASL
633 if( sasl_secprops != NULL ) {
634 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
635 (void *) sasl_secprops );
637 if( rc != LDAP_OPT_SUCCESS ) {
639 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
641 return( EXIT_FAILURE );
645 defaults = lutil_sasl_defaults( ld,
652 rc = ldap_sasl_interactive_bind_s( ld, binddn,
653 sasl_mech, NULL, NULL,
654 sasl_flags, lutil_sasl_interact, defaults );
656 if( rc != LDAP_SUCCESS ) {
657 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
658 return( EXIT_FAILURE );
661 fprintf( stderr, "%s: not compiled with SASL support\n",
663 return( EXIT_FAILURE );
667 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
669 ldap_perror( ld, "ldap_bind" );
670 return( EXIT_FAILURE );
681 LDAPControl *ctrls[2];
685 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
686 c.ldctl_value.bv_val = NULL;
687 c.ldctl_value.bv_len = 0;
688 c.ldctl_iscritical = manageDSAit > 1;
690 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
692 if( err != LDAP_OPT_SUCCESS ) {
693 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
694 c.ldctl_iscritical ? "critical " : "" );
695 if( c.ldctl_iscritical ) {
696 exit( EXIT_FAILURE );
702 while (( rc == 0 || contoper ) &&
703 ( rbuf = read_one_record( fp )) != NULL ) {
708 rc = process_ldif_rec( start, count );
711 fprintf( stderr, "ldif_record() = %d\n", rc );
724 process_ldif_rec( char *rbuf, int count )
726 char *line, *dn, *type, *newrdn, *newsup, *p;
727 int rc, linenum, modop, replicaport;
728 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
729 int expect_deleteoldrdn, deleteoldrdn;
730 int saw_replica, use_record, new_entry, delete_entry, got_all;
737 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
738 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
739 expect_sep = expect_ct = 0;
745 dn = newrdn = newsup = NULL;
747 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
750 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
756 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
757 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
758 prog, linenum, dn == NULL ? "" : dn );
759 rc = LDAP_PARAM_ERROR;
764 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
766 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
770 replicaport = atoi( p );
772 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
773 replicaport == ldapport ) {
776 } else if ( count == 1 && linenum == 1 &&
777 strcasecmp( type, T_VERSION_STR ) == 0 )
779 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
780 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
781 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
785 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
786 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
788 exit( EXIT_FAILURE );
792 goto end_line; /* skip all lines until we see "dn:" */
797 if ( !use_record && saw_replica ) {
798 printf( "%s: skipping change record for entry: %s\n"
799 "\t(LDAP host/port does not match replica: lines)\n",
803 ber_memfree( val.bv_val );
807 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
808 #ifdef LIBERAL_CHANGETYPE_MODOP
809 /* trim trailing spaces (and log warning ...) */
812 for ( icnt = val.bv_len; --icnt > 0; ) {
813 if ( !isspace( val.bv_val[icnt] ) ) {
818 if ( ++icnt != val.bv_len ) {
819 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
820 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
821 val.bv_val[icnt] = '\0';
823 #endif /* LIBERAL_CHANGETYPE_MODOP */
825 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
828 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
830 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
831 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
832 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
835 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
836 got_all = delete_entry = 1;
839 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
840 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
841 rc = LDAP_PARAM_ERROR;
844 } else if ( ldapadd ) { /* missing changetype => add */
846 modop = LDAP_MOD_ADD;
848 expect_modop = 1; /* missing changetype => modify */
852 if ( expect_modop ) {
853 #ifdef LIBERAL_CHANGETYPE_MODOP
854 /* trim trailing spaces (and log warning ...) */
857 for ( icnt = val.bv_len; --icnt > 0; ) {
858 if ( !isspace( val.bv_val[icnt] ) ) {
863 if ( ++icnt != val.bv_len ) {
864 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
865 prog, type, val.bv_val, linenum, dn );
866 val.bv_val[icnt] = '\0';
868 #endif /* LIBERAL_CHANGETYPE_MODOP */
872 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
873 modop = LDAP_MOD_ADD;
875 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
876 modop = LDAP_MOD_REPLACE;
877 addmodifyop( &pmods, modop, val.bv_val, NULL );
879 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
880 modop = LDAP_MOD_DELETE;
881 addmodifyop( &pmods, modop, val.bv_val, NULL );
883 } else { /* no modify op: use default */
884 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
888 if ( expect_newrdn ) {
889 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
890 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
892 exit( EXIT_FAILURE );
894 expect_deleteoldrdn = 1;
897 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
898 prog, T_NEWRDNSTR, type, linenum, dn );
899 rc = LDAP_PARAM_ERROR;
901 } else if ( expect_deleteoldrdn ) {
902 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
903 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
904 expect_deleteoldrdn = 0;
908 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
909 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
910 rc = LDAP_PARAM_ERROR;
912 } else if ( expect_newsup ) {
913 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
914 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
916 exit( EXIT_FAILURE );
920 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
921 prog, T_NEWSUPSTR, type, linenum, dn );
922 rc = LDAP_PARAM_ERROR;
924 } else if ( got_all ) {
926 "%s: extra lines at end (line %d of entry \"%s\")\n",
928 rc = LDAP_PARAM_ERROR;
930 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
935 ber_memfree( val.bv_val );
942 if( version && linenum == 1 ) {
947 if ( delete_entry ) {
949 } else if ( newrdn != NULL ) {
950 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
952 rc = domodify( dn, pmods, new_entry );
955 if ( rc == LDAP_SUCCESS ) {
963 if ( newrdn != NULL ) {
966 if ( pmods != NULL ) {
967 ldap_mods_free( pmods, 1 );
985 modop |= LDAP_MOD_BVALUES;
988 if ( pmods != NULL ) {
989 for ( ; pmods[ i ] != NULL; ++i ) {
990 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
991 pmods[ i ]->mod_op == modop )
998 if ( pmods == NULL || pmods[ i ] == NULL ) {
999 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1000 sizeof( LDAPMod * ))) == NULL )
1002 perror( "realloc" );
1003 exit( EXIT_FAILURE );
1007 pmods[ i + 1 ] = NULL;
1009 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1010 if ( pmods[ i ] == NULL ) {
1012 exit( EXIT_FAILURE );
1015 pmods[ i ]->mod_op = modop;
1016 pmods[ i ]->mod_type = ber_strdup( attr );
1017 if ( pmods[ i ]->mod_type == NULL ) {
1019 exit( EXIT_FAILURE );
1023 if ( val != NULL ) {
1025 if ( pmods[ i ]->mod_bvalues != NULL ) {
1026 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1031 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1032 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1033 if ( pmods[ i ]->mod_bvalues == NULL ) {
1034 perror( "ber_realloc" );
1035 exit( EXIT_FAILURE );
1038 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1039 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1040 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1041 perror( "ber_bvdup" );
1042 exit( EXIT_FAILURE );
1054 int i, j, k, notascii, op;
1057 if ( pmods == NULL ) {
1058 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1060 return( LDAP_PARAM_ERROR );
1063 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1064 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1065 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1067 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1068 prog, pmods[i]->mod_type, dn );
1069 return LDAP_PARAM_ERROR;
1074 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1075 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1076 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1077 "replace" : op == LDAP_MOD_ADD ?
1078 "add" : "delete", pmods[ i ]->mod_type );
1079 if ( pmods[ i ]->mod_bvalues != NULL ) {
1080 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1081 bvp = pmods[ i ]->mod_bvalues[ j ];
1083 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1084 if ( !isascii( bvp->bv_val[ k ] )) {
1090 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1092 printf( "\t%s\n", bvp->bv_val );
1100 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1102 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1107 i = ldap_add_s( ld, dn, pmods );
1109 i = ldap_modify_s( ld, dn, pmods );
1111 if ( i != LDAP_SUCCESS ) {
1112 /* print error message about failed update including DN */
1113 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1114 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1115 } else if ( verbose ) {
1116 printf( "modify complete\n" );
1134 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1136 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1137 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1138 ldap_perror( ld, "ldap_delete" );
1139 } else if ( verbose ) {
1140 printf( "delete complete" );
1162 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1164 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1165 newrdn, deleteoldrdn ? "do not " : "" );
1168 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1171 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1172 ldap_perror( ld, "ldap_modrdn" );
1174 printf( "modrdn completed\n" );
1187 read_one_record( FILE *fp )
1189 char *buf, line[ LDAPMOD_MAXLINE ];
1195 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1196 int len = strlen( line );
1198 if( len < 2 || ( len == 3 && *line == '\r' )) {
1206 if ( lcur + len + 1 > lmax ) {
1207 lmax = LDAPMOD_MAXLINE
1208 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1210 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1211 perror( "realloc" );
1212 exit( EXIT_FAILURE );
1216 strcpy( buf + lcur, line );