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>
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
33 #include "lutil_ldap.h"
35 #include "ldap_defaults.h"
39 static char *binddn = NULL;
40 static struct berval passwd = { 0, NULL };
41 static char *ldapuri = NULL;
42 static char *ldaphost = NULL;
43 static int ldapport = 0;
44 #ifdef HAVE_CYRUS_SASL
45 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
46 static char *sasl_realm = NULL;
47 static char *sasl_authc_id = NULL;
48 static char *sasl_authz_id = NULL;
49 static char *sasl_mech = NULL;
50 static char *sasl_secprops = NULL;
52 static int use_tls = 0;
53 static int ldapadd, not, verbose, contoper, force;
54 static LDAP *ld = NULL;
56 #define LDAPMOD_MAXLINE 4096
58 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
59 #define T_VERSION_STR "version"
60 #define T_REPLICA_STR "replica"
62 #define T_CHANGETYPESTR "changetype"
63 #define T_ADDCTSTR "add"
64 #define T_MODIFYCTSTR "modify"
65 #define T_DELETECTSTR "delete"
66 #define T_MODRDNCTSTR "modrdn"
67 #define T_MODDNCTSTR "moddn"
68 #define T_RENAMECTSTR "rename"
69 #define T_MODOPADDSTR "add"
70 #define T_MODOPREPLACESTR "replace"
71 #define T_MODOPDELETESTR "delete"
72 #define T_MODSEPSTR "-"
73 #define T_NEWRDNSTR "newrdn"
74 #define T_DELETEOLDRDNSTR "deleteoldrdn"
75 #define T_NEWSUPSTR "newsuperior"
78 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
79 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
80 static void addmodifyop LDAP_P((
81 LDAPMod ***pmodsp, int modop,
83 struct berval *value ));
84 static int domodify LDAP_P((
88 static int dodelete LDAP_P((
90 static int dorename LDAP_P((
95 static char *read_one_record LDAP_P(( FILE *fp ));
98 usage( const char *prog )
101 "Add or modify entries from an LDAP server\n\n"
102 "usage: %s [options]\n"
103 " The list of desired operations are read from stdin or from the file\n"
104 " specified by \"-f file\".\n"
105 "Add or modify options:\n"
106 " -a add values (default%s)\n"
107 " -c continuous operation mode (do not stop on errors)\n"
108 " -f file read operations from `file'\n"
109 " -F force all changes records to be used\n"
110 " -S file write skipped modifications to `file'\n"
113 " -d level set LDAP debugging level to `level'\n"
114 " -D binddn bind DN\n"
115 " -h host LDAP server\n"
116 " -H URI LDAP Uniform Resource Indentifier(s)\n"
117 " -I use SASL Interactive mode\n"
118 " -k use Kerberos authentication\n"
119 " -K like -k, but do only step 1 of the Kerberos bind\n"
120 " -M enable Manage DSA IT control (-MM to make critical)\n"
121 " -n show what would be done but don't actually update\n"
122 " -O props SASL security properties\n"
123 " -p port port on LDAP server\n"
124 " -P version procotol version (default: 3)\n"
125 " -Q use SASL Quiet mode\n"
126 " -R realm SASL realm\n"
127 " -U authcid SASL authentication identity\n"
128 " -v run in verbose mode (diagnostics to standard output)\n"
129 " -w passwd bind passwd (for simple authentication)\n"
130 " -W prompt for bind passwd\n"
131 " -x Simple authentication\n"
132 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
133 " -Y mech SASL mechanism\n"
134 " -Z Start TLS request (-ZZ to require successful response)\n"
135 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
137 exit( EXIT_FAILURE );
142 main( int argc, char **argv )
144 char *infile, *rejfile, *rbuf, *start, *rejbuf;
146 char *matched_msg = NULL, *error_msg = NULL;
147 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
150 prog = lutil_progname( "ldapmodify", argc, argv );
152 /* Print usage when no parameters */
153 if( argc < 2 ) usage( prog );
155 /* strncmp instead of strcmp since NT binaries carry .exe extension */
156 ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
160 not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
164 while (( i = getopt( argc, argv, "acrf:F"
165 "Cd:D:h:H:IkKMnO:p:P:QR:S:U:vw:WxX:Y:Z" )) != EOF )
172 case 'c': /* continuous operation */
175 case 'f': /* read from file */
176 if( infile != NULL ) {
177 fprintf( stderr, "%s: -f previously specified\n", prog );
180 infile = strdup( optarg );
182 case 'F': /* force all changes records to be used */
191 debug |= atoi( optarg );
193 case 'D': /* bind DN */
194 if( binddn != NULL ) {
195 fprintf( stderr, "%s: -D previously specified\n", prog );
198 binddn = strdup( optarg );
200 case 'h': /* ldap host */
201 if( ldapuri != NULL ) {
202 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
205 if( ldaphost != NULL ) {
206 fprintf( stderr, "%s: -h previously specified\n", prog );
209 ldaphost = strdup( optarg );
211 case 'H': /* ldap URI */
212 if( ldaphost != NULL ) {
213 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
217 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
220 if( ldapuri != NULL ) {
221 fprintf( stderr, "%s: -H previously specified\n", prog );
224 ldapuri = strdup( optarg );
227 #ifdef HAVE_CYRUS_SASL
228 if( version == LDAP_VERSION2 ) {
229 fprintf( stderr, "%s: -I incompatible with version %d\n",
233 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
234 fprintf( stderr, "%s: incompatible previous "
235 "authentication choice\n",
239 authmethod = LDAP_AUTH_SASL;
240 version = LDAP_VERSION3;
241 sasl_flags = LDAP_SASL_INTERACTIVE;
244 fprintf( stderr, "%s: was not compiled with SASL support\n",
246 return( EXIT_FAILURE );
248 case 'k': /* kerberos bind */
249 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
250 if( version > LDAP_VERSION2 ) {
251 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
256 if( authmethod != -1 ) {
257 fprintf( stderr, "%s: -k incompatible with previous "
258 "authentication choice\n", prog );
262 authmethod = LDAP_AUTH_KRBV4;
264 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
268 case 'K': /* kerberos bind, part one only */
269 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
270 if( version > LDAP_VERSION2 ) {
271 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
275 if( authmethod != -1 ) {
276 fprintf( stderr, "%s: incompatible with previous "
277 "authentication choice\n", prog );
281 authmethod = LDAP_AUTH_KRBV41;
283 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
284 return( EXIT_FAILURE );
288 /* enable Manage DSA IT */
289 if( version == LDAP_VERSION2 ) {
290 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
295 version = LDAP_VERSION3;
297 case 'n': /* print deletes, don't actually do them */
301 #ifdef HAVE_CYRUS_SASL
302 if( sasl_secprops != NULL ) {
303 fprintf( stderr, "%s: -O previously specified\n", prog );
306 if( version == LDAP_VERSION2 ) {
307 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
311 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
312 fprintf( stderr, "%s: incompatible previous "
313 "authentication choice\n", prog );
316 authmethod = LDAP_AUTH_SASL;
317 version = LDAP_VERSION3;
318 sasl_secprops = strdup( optarg );
320 fprintf( stderr, "%s: not compiled with SASL support\n",
322 return( EXIT_FAILURE );
327 fprintf( stderr, "%s: -p previously specified\n", prog );
330 ldapport = atoi( optarg );
333 switch( atoi(optarg) ) {
335 if( version == LDAP_VERSION3 ) {
336 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
340 version = LDAP_VERSION2;
343 if( version == LDAP_VERSION2 ) {
344 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
348 version = LDAP_VERSION3;
351 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
354 return( EXIT_FAILURE );
357 #ifdef HAVE_CYRUS_SASL
358 if( version == LDAP_VERSION2 ) {
359 fprintf( stderr, "%s: -Q incompatible with version %d\n",
363 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
364 fprintf( stderr, "%s: incompatible previous "
365 "authentication choice\n",
369 authmethod = LDAP_AUTH_SASL;
370 version = LDAP_VERSION3;
371 sasl_flags = LDAP_SASL_QUIET;
374 fprintf( stderr, "%s: not compiled with SASL support\n",
376 return( EXIT_FAILURE );
378 case 'r': /* replace (obsolete) */
382 #ifdef HAVE_CYRUS_SASL
383 if( sasl_realm != NULL ) {
384 fprintf( stderr, "%s: -R previously specified\n", prog );
387 if( version == LDAP_VERSION2 ) {
388 fprintf( stderr, "%s: -R incompatible with version %d\n",
392 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
393 fprintf( stderr, "%s: incompatible previous "
394 "authentication choice\n",
398 authmethod = LDAP_AUTH_SASL;
399 version = LDAP_VERSION3;
400 sasl_realm = strdup( optarg );
402 fprintf( stderr, "%s: not compiled with SASL support\n",
404 return( EXIT_FAILURE );
407 case 'S': /* skipped modifications to file */
408 if( rejfile != NULL ) {
409 fprintf( stderr, "%s: -S previously specified\n", prog );
412 rejfile = strdup( optarg );
415 #ifdef HAVE_CYRUS_SASL
416 if( sasl_authc_id != NULL ) {
417 fprintf( stderr, "%s: -U previously specified\n", prog );
420 if( version == LDAP_VERSION2 ) {
421 fprintf( stderr, "%s: -U incompatible with version %d\n",
425 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
426 fprintf( stderr, "%s: incompatible previous "
427 "authentication choice\n",
431 authmethod = LDAP_AUTH_SASL;
432 version = LDAP_VERSION3;
433 sasl_authc_id = strdup( optarg );
435 fprintf( stderr, "%s: not compiled with SASL support\n",
437 return( EXIT_FAILURE );
440 case 'v': /* verbose mode */
443 case 'w': /* password */
444 passwd.bv_val = strdup( optarg );
448 for( p = optarg; *p != '\0'; p++ ) {
452 passwd.bv_len = strlen( passwd.bv_val );
458 #ifdef HAVE_CYRUS_SASL
459 if( sasl_mech != NULL ) {
460 fprintf( stderr, "%s: -Y previously specified\n", prog );
463 if( version == LDAP_VERSION2 ) {
464 fprintf( stderr, "%s: -Y incompatible with version %d\n",
468 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
469 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
472 authmethod = LDAP_AUTH_SASL;
473 version = LDAP_VERSION3;
474 sasl_mech = strdup( optarg );
476 fprintf( stderr, "%s: not compiled with SASL support\n",
478 return( EXIT_FAILURE );
482 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
483 fprintf( stderr, "%s: incompatible with previous "
484 "authentication choice\n", prog );
487 authmethod = LDAP_AUTH_SIMPLE;
490 #ifdef HAVE_CYRUS_SASL
491 if( sasl_authz_id != NULL ) {
492 fprintf( stderr, "%s: -X previously specified\n", prog );
495 if( version == LDAP_VERSION2 ) {
496 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
500 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
501 fprintf( stderr, "%s: -X incompatible with "
502 "authentication choice\n", prog );
505 authmethod = LDAP_AUTH_SASL;
506 version = LDAP_VERSION3;
507 sasl_authz_id = strdup( optarg );
509 fprintf( stderr, "%s: not compiled with SASL support\n",
511 return( EXIT_FAILURE );
516 if( version == LDAP_VERSION2 ) {
517 fprintf( stderr, "%s: -Z incompatible with version %d\n",
521 version = LDAP_VERSION3;
524 fprintf( stderr, "%s: not compiled with TLS support\n",
526 return( EXIT_FAILURE );
530 fprintf( stderr, "%s: unrecognized option -%c\n",
537 version = LDAP_VERSION3;
539 if (authmethod == -1 && version > LDAP_VERSION2) {
540 #ifdef HAVE_CYRUS_SASL
541 authmethod = LDAP_AUTH_SASL;
543 authmethod = LDAP_AUTH_SIMPLE;
547 if ( argc != optind )
550 if ( rejfile != NULL ) {
551 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
553 return( EXIT_FAILURE );
559 if ( infile != NULL ) {
560 if (( fp = fopen( infile, "r" )) == NULL ) {
562 return( EXIT_FAILURE );
569 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
570 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
572 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
573 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
579 (void) SIGNAL( SIGPIPE, SIG_IGN );
583 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
585 fprintf( stderr, "ldap_init( %s, %d )\n",
586 ldaphost != NULL ? ldaphost : "<DEFAULT>",
590 ld = ldap_init( ldaphost, ldapport );
592 perror("ldapmodify: ldap_init");
598 fprintf( stderr, "ldap_initialize( %s )\n",
599 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
602 rc = ldap_initialize( &ld, ldapuri );
603 if( rc != LDAP_SUCCESS ) {
604 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
605 rc, ldap_err2string(rc) );
611 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
612 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
614 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
615 referrals ? "on" : "off" );
620 if (version == -1 ) {
621 version = LDAP_VERSION3;
624 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
625 != LDAP_OPT_SUCCESS )
627 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
632 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
633 ldap_perror( ld, "ldap_start_tls" );
635 return( EXIT_FAILURE );
640 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
641 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
644 if ( authmethod == LDAP_AUTH_SASL ) {
645 #ifdef HAVE_CYRUS_SASL
648 if( sasl_secprops != NULL ) {
649 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
650 (void *) sasl_secprops );
652 if( rc != LDAP_OPT_SUCCESS ) {
654 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
656 return( EXIT_FAILURE );
660 defaults = lutil_sasl_defaults( ld,
667 rc = ldap_sasl_interactive_bind_s( ld, binddn,
668 sasl_mech, NULL, NULL,
669 sasl_flags, lutil_sasl_interact, defaults );
671 if( rc != LDAP_SUCCESS ) {
672 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
673 return( EXIT_FAILURE );
676 fprintf( stderr, "%s: not compiled with SASL support\n",
678 return( EXIT_FAILURE );
682 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
684 ldap_perror( ld, "ldap_bind" );
685 return( EXIT_FAILURE );
697 LDAPControl *ctrls[2];
701 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
702 c.ldctl_value.bv_val = NULL;
703 c.ldctl_value.bv_len = 0;
704 c.ldctl_iscritical = manageDSAit > 1;
706 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
708 if( err != LDAP_OPT_SUCCESS ) {
709 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
710 c.ldctl_iscritical ? "critical " : "" );
711 if( c.ldctl_iscritical ) {
712 exit( EXIT_FAILURE );
718 while (( rc == 0 || contoper ) &&
719 ( rbuf = read_one_record( fp )) != NULL ) {
725 len = strlen( rbuf );
726 if (( rejbuf = (char *)malloc( len+1 )) == NULL ) {
728 exit( EXIT_FAILURE );
730 memcpy( rejbuf, rbuf, len+1 );
733 rc = process_ldif_rec( start, count );
736 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
738 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
739 if ( matched_msg != NULL && *matched_msg != '\0' ) {
740 fprintf( rejfp, ", matched DN: %s", matched_msg );
743 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
744 if ( error_msg != NULL && *error_msg != '\0' ) {
745 fprintf( rejfp, ", additional info: %s", error_msg );
747 fprintf( rejfp, "\n%s\n", rejbuf );
758 if ( rejfp != NULL ) {
767 process_ldif_rec( char *rbuf, int count )
769 char *line, *dn, *type, *newrdn, *newsup, *p;
770 int rc, linenum, modop, replicaport;
771 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
772 int expect_deleteoldrdn, deleteoldrdn;
773 int saw_replica, use_record, new_entry, delete_entry, got_all;
780 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
781 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
782 expect_sep = expect_ct = 0;
788 dn = newrdn = newsup = NULL;
790 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
793 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
799 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
800 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
801 prog, linenum, dn == NULL ? "" : dn );
802 rc = LDAP_PARAM_ERROR;
807 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
809 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
813 replicaport = atoi( p );
815 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
816 replicaport == ldapport ) {
819 } else if ( count == 1 && linenum == 1 &&
820 strcasecmp( type, T_VERSION_STR ) == 0 )
822 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
823 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
824 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
828 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
829 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
831 exit( EXIT_FAILURE );
835 goto end_line; /* skip all lines until we see "dn:" */
840 if ( !use_record && saw_replica ) {
841 printf( "%s: skipping change record for entry: %s\n"
842 "\t(LDAP host/port does not match replica: lines)\n",
846 ber_memfree( val.bv_val );
850 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
851 #ifdef LIBERAL_CHANGETYPE_MODOP
852 /* trim trailing spaces (and log warning ...) */
855 for ( icnt = val.bv_len; --icnt > 0; ) {
856 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
861 if ( ++icnt != val.bv_len ) {
862 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
863 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
864 val.bv_val[icnt] = '\0';
866 #endif /* LIBERAL_CHANGETYPE_MODOP */
868 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
871 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
873 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
874 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
875 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
878 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
879 got_all = delete_entry = 1;
882 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
883 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
884 rc = LDAP_PARAM_ERROR;
887 } else if ( ldapadd ) { /* missing changetype => add */
889 modop = LDAP_MOD_ADD;
891 expect_modop = 1; /* missing changetype => modify */
895 if ( expect_modop ) {
896 #ifdef LIBERAL_CHANGETYPE_MODOP
897 /* trim trailing spaces (and log warning ...) */
900 for ( icnt = val.bv_len; --icnt > 0; ) {
901 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
906 if ( ++icnt != val.bv_len ) {
907 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
908 prog, type, val.bv_val, linenum, dn );
909 val.bv_val[icnt] = '\0';
911 #endif /* LIBERAL_CHANGETYPE_MODOP */
915 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
916 modop = LDAP_MOD_ADD;
918 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
919 modop = LDAP_MOD_REPLACE;
920 addmodifyop( &pmods, modop, val.bv_val, NULL );
922 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
923 modop = LDAP_MOD_DELETE;
924 addmodifyop( &pmods, modop, val.bv_val, NULL );
926 } else { /* no modify op: use default */
927 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
931 if ( expect_newrdn ) {
932 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
933 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
935 exit( EXIT_FAILURE );
937 expect_deleteoldrdn = 1;
940 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
941 prog, T_NEWRDNSTR, type, linenum, dn );
942 rc = LDAP_PARAM_ERROR;
944 } else if ( expect_deleteoldrdn ) {
945 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
946 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
947 expect_deleteoldrdn = 0;
951 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
952 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
953 rc = LDAP_PARAM_ERROR;
955 } else if ( expect_newsup ) {
956 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
957 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
959 exit( EXIT_FAILURE );
963 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
964 prog, T_NEWSUPSTR, type, linenum, dn );
965 rc = LDAP_PARAM_ERROR;
967 } else if ( got_all ) {
969 "%s: extra lines at end (line %d of entry \"%s\")\n",
971 rc = LDAP_PARAM_ERROR;
973 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
978 ber_memfree( val.bv_val );
985 if( version && linenum == 1 ) {
990 if ( delete_entry ) {
992 } else if ( newrdn != NULL ) {
993 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
995 rc = domodify( dn, pmods, new_entry );
998 if ( rc == LDAP_SUCCESS ) {
1006 if ( newrdn != NULL ) {
1009 if ( pmods != NULL ) {
1010 ldap_mods_free( pmods, 1 );
1022 struct berval *val )
1028 modop |= LDAP_MOD_BVALUES;
1031 if ( pmods != NULL ) {
1032 for ( ; pmods[ i ] != NULL; ++i ) {
1033 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1034 pmods[ i ]->mod_op == modop )
1041 if ( pmods == NULL || pmods[ i ] == NULL ) {
1042 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1043 sizeof( LDAPMod * ))) == NULL )
1045 perror( "realloc" );
1046 exit( EXIT_FAILURE );
1050 pmods[ i + 1 ] = NULL;
1052 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1053 if ( pmods[ i ] == NULL ) {
1055 exit( EXIT_FAILURE );
1058 pmods[ i ]->mod_op = modop;
1059 pmods[ i ]->mod_type = ber_strdup( attr );
1060 if ( pmods[ i ]->mod_type == NULL ) {
1062 exit( EXIT_FAILURE );
1066 if ( val != NULL ) {
1068 if ( pmods[ i ]->mod_bvalues != NULL ) {
1069 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1074 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1075 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1076 if ( pmods[ i ]->mod_bvalues == NULL ) {
1077 perror( "ber_realloc" );
1078 exit( EXIT_FAILURE );
1081 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1082 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1083 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1084 perror( "ber_bvdup" );
1085 exit( EXIT_FAILURE );
1097 int i, j, k, notascii, op;
1100 if ( pmods == NULL ) {
1101 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1103 return( LDAP_PARAM_ERROR );
1106 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1107 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1108 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1110 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1111 prog, pmods[i]->mod_type, dn );
1112 return LDAP_PARAM_ERROR;
1117 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1118 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1119 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1120 "replace" : op == LDAP_MOD_ADD ?
1121 "add" : "delete", pmods[ i ]->mod_type );
1122 if ( pmods[ i ]->mod_bvalues != NULL ) {
1123 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1124 bvp = pmods[ i ]->mod_bvalues[ j ];
1126 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1127 if ( !isascii( bvp->bv_val[ k ] )) {
1133 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1135 printf( "\t%s\n", bvp->bv_val );
1143 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1145 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1150 i = ldap_add_s( ld, dn, pmods );
1152 i = ldap_modify_s( ld, dn, pmods );
1154 if ( i != LDAP_SUCCESS ) {
1155 /* print error message about failed update including DN */
1156 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1157 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1158 } else if ( verbose ) {
1159 printf( "modify complete\n" );
1177 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1179 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1180 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1181 ldap_perror( ld, "ldap_delete" );
1182 } else if ( verbose ) {
1183 printf( "delete complete" );
1205 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1207 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1208 newrdn, deleteoldrdn ? "do not " : "" );
1211 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1214 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1215 ldap_perror( ld, "ldap_modrdn" );
1217 printf( "modrdn completed\n" );
1230 read_one_record( FILE *fp )
1232 char *buf, line[ LDAPMOD_MAXLINE ];
1238 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1239 int len = strlen( line );
1241 if( len < 2 || ( len == 2 && *line == '\r' )) {
1249 if ( lcur + len + 1 > lmax ) {
1250 lmax = LDAPMOD_MAXLINE
1251 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1253 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1254 perror( "realloc" );
1255 exit( EXIT_FAILURE );
1259 strcpy( buf + lcur, line );