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, replace, 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 " -r replace values\n"
106 " -F force all changes records to be used\n"
109 " -d level set LDAP debugging level to `level'\n"
110 " -D binddn bind DN\n"
111 " -f file read operations from `file'\n"
112 " -h host LDAP server\n"
113 " -H URI LDAP Uniform Resource Indentifier(s)\n"
114 " -I use SASL Interactive mode\n"
115 " -k use Kerberos authentication\n"
116 " -K like -k, but do only step 1 of the Kerberos bind\n"
117 " -M enable Manage DSA IT control (-MM to make critical)\n"
118 " -n show what would be done but don't actually search\n"
119 " -O props SASL security properties\n"
120 " -p port port on LDAP server\n"
121 " -P version procotol version (default: 3)\n"
122 " -Q use SASL Quiet mode\n"
123 " -R realm SASL realm\n"
124 " -U authcid SASL authentication identity\n"
125 " -v run in verbose mode (diagnostics to standard output)\n"
126 " -w passwd bind passwd (for simple authentication)\n"
127 " -W prompt for bind passwd\n"
128 " -x Simple authentication\n"
129 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
130 " -Y mech SASL mechanism\n"
131 " -Z Start TLS request (-ZZ to require successful response)\n"
132 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
134 exit( EXIT_FAILURE );
139 main( int argc, char **argv )
141 char *infile, *rbuf, *start;
143 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
146 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
152 /* Print usage when no parameters */
153 if( argc < 2 ) usage( prog );
155 ldapadd = ( strcmp( prog, "ldapadd" ) == 0 );
158 not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
162 while (( i = getopt( argc, argv, "acrf:F"
163 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z" )) != EOF )
170 case 'c': /* continuous operation */
173 case 'f': /* read from file */
174 if( infile != NULL ) {
175 fprintf( stderr, "%s: -f previously specified\n", prog );
178 infile = strdup( optarg );
180 case 'F': /* force all changes records to be used */
183 case 'r': /* default is to replace rather than add values */
192 debug |= atoi( optarg );
194 case 'D': /* bind DN */
195 if( binddn != NULL ) {
196 fprintf( stderr, "%s: -D previously specified\n", prog );
199 binddn = strdup( optarg );
201 case 'h': /* ldap host */
202 if( ldapuri != NULL ) {
203 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
206 if( ldaphost != NULL ) {
207 fprintf( stderr, "%s: -h previously specified\n", prog );
210 ldaphost = strdup( optarg );
212 case 'H': /* ldap URI */
213 if( ldaphost != NULL ) {
214 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
218 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
221 if( ldapuri != NULL ) {
222 fprintf( stderr, "%s: -H previously specified\n", prog );
225 ldapuri = strdup( optarg );
228 #ifdef HAVE_CYRUS_SASL
229 if( version == LDAP_VERSION2 ) {
230 fprintf( stderr, "%s: -I incompatible with version %d\n",
234 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
235 fprintf( stderr, "%s: incompatible previous "
236 "authentication choice\n",
240 authmethod = LDAP_AUTH_SASL;
241 version = LDAP_VERSION3;
242 sasl_flags = LDAP_SASL_INTERACTIVE;
245 fprintf( stderr, "%s: was not compiled with SASL support\n",
247 return( EXIT_FAILURE );
249 case 'k': /* kerberos bind */
250 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
251 if( version > LDAP_VERSION2 ) {
252 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
257 if( authmethod != -1 ) {
258 fprintf( stderr, "%s: -k incompatible with previous "
259 "authentication choice\n", prog );
263 authmethod = LDAP_AUTH_KRBV4;
265 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
269 case 'K': /* kerberos bind, part one only */
270 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
271 if( version > LDAP_VERSION2 ) {
272 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
276 if( authmethod != -1 ) {
277 fprintf( stderr, "%s: incompatible with previous "
278 "authentication choice\n", prog );
282 authmethod = LDAP_AUTH_KRBV41;
284 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
285 return( EXIT_FAILURE );
289 /* enable Manage DSA IT */
290 if( version == LDAP_VERSION2 ) {
291 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
296 version = LDAP_VERSION3;
298 case 'n': /* print deletes, don't actually do them */
302 #ifdef HAVE_CYRUS_SASL
303 if( sasl_secprops != NULL ) {
304 fprintf( stderr, "%s: -O previously specified\n", prog );
307 if( version == LDAP_VERSION2 ) {
308 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
312 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
313 fprintf( stderr, "%s: incompatible previous "
314 "authentication choice\n", prog );
317 authmethod = LDAP_AUTH_SASL;
318 version = LDAP_VERSION3;
319 sasl_secprops = strdup( optarg );
321 fprintf( stderr, "%s: not compiled with SASL support\n",
323 return( EXIT_FAILURE );
328 fprintf( stderr, "%s: -p previously specified\n", prog );
331 ldapport = atoi( optarg );
334 switch( atoi(optarg) ) {
336 if( version == LDAP_VERSION3 ) {
337 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
341 version = LDAP_VERSION2;
344 if( version == LDAP_VERSION2 ) {
345 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
349 version = LDAP_VERSION3;
352 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
355 return( EXIT_FAILURE );
358 #ifdef HAVE_CYRUS_SASL
359 if( version == LDAP_VERSION2 ) {
360 fprintf( stderr, "%s: -Q incompatible with version %d\n",
364 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
365 fprintf( stderr, "%s: incompatible previous "
366 "authentication choice\n",
370 authmethod = LDAP_AUTH_SASL;
371 version = LDAP_VERSION3;
372 sasl_flags = LDAP_SASL_QUIET;
375 fprintf( stderr, "%s: not compiled with SASL support\n",
377 return( EXIT_FAILURE );
380 #ifdef HAVE_CYRUS_SASL
381 if( sasl_realm != NULL ) {
382 fprintf( stderr, "%s: -R previously specified\n", prog );
385 if( version == LDAP_VERSION2 ) {
386 fprintf( stderr, "%s: -R incompatible with version %d\n",
390 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
391 fprintf( stderr, "%s: incompatible previous "
392 "authentication choice\n",
396 authmethod = LDAP_AUTH_SASL;
397 version = LDAP_VERSION3;
398 sasl_realm = strdup( optarg );
400 fprintf( stderr, "%s: not compiled with SASL support\n",
402 return( EXIT_FAILURE );
406 #ifdef HAVE_CYRUS_SASL
407 if( sasl_authc_id != NULL ) {
408 fprintf( stderr, "%s: -U previously specified\n", prog );
411 if( version == LDAP_VERSION2 ) {
412 fprintf( stderr, "%s: -U incompatible with version %d\n",
416 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
417 fprintf( stderr, "%s: incompatible previous "
418 "authentication choice\n",
422 authmethod = LDAP_AUTH_SASL;
423 version = LDAP_VERSION3;
424 sasl_authc_id = strdup( optarg );
426 fprintf( stderr, "%s: not compiled with SASL support\n",
428 return( EXIT_FAILURE );
431 case 'v': /* verbose mode */
434 case 'w': /* password */
435 passwd.bv_val = strdup( optarg );
439 for( p = optarg; *p != '\0'; p++ ) {
443 passwd.bv_len = strlen( passwd.bv_val );
449 #ifdef HAVE_CYRUS_SASL
450 if( sasl_mech != NULL ) {
451 fprintf( stderr, "%s: -Y previously specified\n", prog );
454 if( version == LDAP_VERSION2 ) {
455 fprintf( stderr, "%s: -Y incompatible with version %d\n",
459 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
460 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
463 authmethod = LDAP_AUTH_SASL;
464 version = LDAP_VERSION3;
465 sasl_mech = strdup( optarg );
467 fprintf( stderr, "%s: not compiled with SASL support\n",
469 return( EXIT_FAILURE );
473 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
474 fprintf( stderr, "%s: incompatible with previous "
475 "authentication choice\n", prog );
478 authmethod = LDAP_AUTH_SIMPLE;
481 #ifdef HAVE_CYRUS_SASL
482 if( sasl_authz_id != NULL ) {
483 fprintf( stderr, "%s: -X previously specified\n", prog );
486 if( version == LDAP_VERSION2 ) {
487 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
491 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
492 fprintf( stderr, "%s: -X incompatible with "
493 "authentication choice\n", prog );
496 authmethod = LDAP_AUTH_SASL;
497 version = LDAP_VERSION3;
498 sasl_authz_id = strdup( optarg );
500 fprintf( stderr, "%s: not compiled with SASL support\n",
502 return( EXIT_FAILURE );
507 if( version == LDAP_VERSION2 ) {
508 fprintf( stderr, "%s: -Z incompatible with version %d\n",
512 version = LDAP_VERSION3;
515 fprintf( stderr, "%s: not compiled with TLS support\n",
517 return( EXIT_FAILURE );
521 fprintf( stderr, "%s: unrecognized option -%c\n",
528 version = LDAP_VERSION3;
530 if (authmethod == -1 && version > LDAP_VERSION2) {
531 #ifdef HAVE_CYRUS_SASL
532 authmethod = LDAP_AUTH_SASL;
534 authmethod = LDAP_AUTH_SIMPLE;
538 if ( argc != optind )
541 if ( infile != NULL ) {
542 if (( fp = fopen( infile, "r" )) == NULL ) {
544 return( EXIT_FAILURE );
551 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
552 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
554 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
555 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
561 (void) SIGNAL( SIGPIPE, SIG_IGN );
565 lutil_log_initialize( argc, argv );
569 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
571 fprintf( stderr, "ldap_init( %s, %d )\n",
572 ldaphost != NULL ? ldaphost : "<DEFAULT>",
576 ld = ldap_init( ldaphost, ldapport );
578 perror("ldapsearch: ldap_init");
584 fprintf( stderr, "ldap_initialize( %s )\n",
585 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
588 rc = ldap_initialize( &ld, ldapuri );
589 if( rc != LDAP_SUCCESS ) {
590 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
591 rc, ldap_err2string(rc) );
597 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
598 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
600 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
601 referrals ? "on" : "off" );
606 if (version == -1 ) {
607 version = LDAP_VERSION3;
610 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
611 != LDAP_OPT_SUCCESS )
613 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
618 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
619 ldap_perror( ld, "ldap_start_tls" );
621 return( EXIT_FAILURE );
626 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
627 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
630 if ( authmethod == LDAP_AUTH_SASL ) {
631 #ifdef HAVE_CYRUS_SASL
634 if( sasl_secprops != NULL ) {
635 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
636 (void *) sasl_secprops );
638 if( rc != LDAP_OPT_SUCCESS ) {
640 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
642 return( EXIT_FAILURE );
646 defaults = lutil_sasl_defaults( ld,
653 rc = ldap_sasl_interactive_bind_s( ld, binddn,
654 sasl_mech, NULL, NULL,
655 sasl_flags, lutil_sasl_interact, defaults );
657 if( rc != LDAP_SUCCESS ) {
658 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
659 return( EXIT_FAILURE );
662 fprintf( stderr, "%s: not compiled with SASL support\n",
664 return( EXIT_FAILURE );
668 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
670 ldap_perror( ld, "ldap_bind" );
671 return( EXIT_FAILURE );
682 LDAPControl *ctrls[2];
686 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
687 c.ldctl_value.bv_val = NULL;
688 c.ldctl_value.bv_len = 0;
689 c.ldctl_iscritical = manageDSAit > 1;
691 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
693 if( err != LDAP_OPT_SUCCESS ) {
694 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
695 c.ldctl_iscritical ? "critical " : "" );
696 if( c.ldctl_iscritical ) {
697 exit( EXIT_FAILURE );
703 while (( rc == 0 || contoper ) &&
704 ( rbuf = read_one_record( fp )) != NULL ) {
709 rc = process_ldif_rec( start, count );
712 fprintf( stderr, "ldif_record() = %d\n", rc );
725 process_ldif_rec( char *rbuf, int count )
727 char *line, *dn, *type, *newrdn, *newsup, *p;
728 int rc, linenum, modop, replicaport;
729 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
730 int expect_deleteoldrdn, deleteoldrdn;
731 int saw_replica, use_record, new_entry, delete_entry, got_all;
738 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
739 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
740 expect_sep = expect_ct = 0;
746 dn = newrdn = newsup = NULL;
748 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
751 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
757 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
758 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
759 prog, linenum, dn == NULL ? "" : dn );
760 rc = LDAP_PARAM_ERROR;
765 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
767 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
771 replicaport = atoi( p );
773 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
774 replicaport == ldapport ) {
777 } else if ( count == 1 && linenum == 1 &&
778 strcasecmp( type, T_VERSION_STR ) == 0 )
780 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
781 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
782 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
786 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
787 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
789 exit( EXIT_FAILURE );
793 goto end_line; /* skip all lines until we see "dn:" */
798 if ( !use_record && saw_replica ) {
799 printf( "%s: skipping change record for entry: %s\n"
800 "\t(LDAP host/port does not match replica: lines)\n",
804 ber_memfree( val.bv_val );
808 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
809 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
812 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
814 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
815 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
816 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
819 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
820 got_all = delete_entry = 1;
823 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
824 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
825 rc = LDAP_PARAM_ERROR;
828 } else if ( ldapadd ) { /* missing changetype => add */
830 modop = LDAP_MOD_ADD;
832 expect_modop = 1; /* missing changetype => modify */
836 if ( expect_modop ) {
839 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
840 modop = LDAP_MOD_ADD;
842 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
843 modop = LDAP_MOD_REPLACE;
844 addmodifyop( &pmods, modop, val.bv_val, NULL );
846 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
847 modop = LDAP_MOD_DELETE;
848 addmodifyop( &pmods, modop, val.bv_val, NULL );
850 } else { /* no modify op: use default */
851 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
855 if ( expect_newrdn ) {
856 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
857 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
859 exit( EXIT_FAILURE );
861 expect_deleteoldrdn = 1;
864 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
865 prog, T_NEWRDNSTR, type, linenum, dn );
866 rc = LDAP_PARAM_ERROR;
868 } else if ( expect_deleteoldrdn ) {
869 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
870 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
871 expect_deleteoldrdn = 0;
875 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
876 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
877 rc = LDAP_PARAM_ERROR;
879 } else if ( expect_newsup ) {
880 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
881 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
883 exit( EXIT_FAILURE );
887 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
888 prog, T_NEWSUPSTR, type, linenum, dn );
889 rc = LDAP_PARAM_ERROR;
891 } else if ( got_all ) {
893 "%s: extra lines at end (line %d of entry \"%s\")\n",
895 rc = LDAP_PARAM_ERROR;
897 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
902 ber_memfree( val.bv_val );
909 if( version && linenum == 1 ) {
914 if ( delete_entry ) {
916 } else if ( newrdn != NULL ) {
917 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
919 rc = domodify( dn, pmods, new_entry );
922 if ( rc == LDAP_SUCCESS ) {
930 if ( newrdn != NULL ) {
933 if ( pmods != NULL ) {
934 ldap_mods_free( pmods, 1 );
952 modop |= LDAP_MOD_BVALUES;
955 if ( pmods != NULL ) {
956 for ( ; pmods[ i ] != NULL; ++i ) {
957 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
958 pmods[ i ]->mod_op == modop )
965 if ( pmods == NULL || pmods[ i ] == NULL ) {
966 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
967 sizeof( LDAPMod * ))) == NULL )
970 exit( EXIT_FAILURE );
974 pmods[ i + 1 ] = NULL;
976 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
977 if ( pmods[ i ] == NULL ) {
979 exit( EXIT_FAILURE );
982 pmods[ i ]->mod_op = modop;
983 pmods[ i ]->mod_type = ber_strdup( attr );
984 if ( pmods[ i ]->mod_type == NULL ) {
986 exit( EXIT_FAILURE );
992 if ( pmods[ i ]->mod_bvalues != NULL ) {
993 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
998 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
999 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1000 if ( pmods[ i ]->mod_bvalues == NULL ) {
1001 perror( "ber_realloc" );
1002 exit( EXIT_FAILURE );
1005 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1006 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1007 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1008 perror( "ber_bvdup" );
1009 exit( EXIT_FAILURE );
1021 int i, j, k, notascii, op;
1024 if ( pmods == NULL ) {
1025 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1027 return( LDAP_PARAM_ERROR );
1030 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1031 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1032 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1034 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1035 prog, pmods[i]->mod_type, dn );
1036 return LDAP_PARAM_ERROR;
1041 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1042 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1043 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1044 "replace" : op == LDAP_MOD_ADD ?
1045 "add" : "delete", pmods[ i ]->mod_type );
1046 if ( pmods[ i ]->mod_bvalues != NULL ) {
1047 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1048 bvp = pmods[ i ]->mod_bvalues[ j ];
1050 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1051 if ( !isascii( bvp->bv_val[ k ] )) {
1057 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1059 printf( "\t%s\n", bvp->bv_val );
1067 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1069 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1074 i = ldap_add_s( ld, dn, pmods );
1076 i = ldap_modify_s( ld, dn, pmods );
1078 if ( i != LDAP_SUCCESS ) {
1079 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1080 } else if ( verbose ) {
1081 printf( "modify complete\n" );
1099 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1101 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1102 ldap_perror( ld, "ldap_delete" );
1103 } else if ( verbose ) {
1104 printf( "delete complete" );
1126 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1128 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1129 newrdn, deleteoldrdn ? "do not " : "" );
1132 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1134 ldap_perror( ld, "ldap_modrdn" );
1136 printf( "modrdn completed\n" );
1149 read_one_record( FILE *fp )
1151 char *buf, line[ LDAPMOD_MAXLINE ];
1157 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1158 int len = strlen( line );
1168 if ( lcur + len + 1 > lmax ) {
1169 lmax = LDAPMOD_MAXLINE
1170 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1172 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1173 perror( "realloc" );
1174 exit( EXIT_FAILURE );
1178 strcpy( buf + lcur, line );