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
32 #include "lutil_ldap.h"
34 #include "ldap_defaults.h"
38 static char *binddn = NULL;
39 static struct berval passwd = { 0, NULL };
40 static char *ldapuri = NULL;
41 static char *ldaphost = NULL;
42 static int ldapport = 0;
43 #ifdef HAVE_CYRUS_SASL
44 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
45 static char *sasl_realm = NULL;
46 static char *sasl_authc_id = NULL;
47 static char *sasl_authz_id = NULL;
48 static char *sasl_mech = NULL;
49 static char *sasl_secprops = NULL;
51 static int use_tls = 0;
52 static int ldapadd, not, verbose, contoper, force;
53 static LDAP *ld = NULL;
55 #define LDAPMOD_MAXLINE 4096
57 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
58 #define T_VERSION_STR "version"
59 #define T_REPLICA_STR "replica"
61 #define T_CHANGETYPESTR "changetype"
62 #define T_ADDCTSTR "add"
63 #define T_MODIFYCTSTR "modify"
64 #define T_DELETECTSTR "delete"
65 #define T_MODRDNCTSTR "modrdn"
66 #define T_MODDNCTSTR "moddn"
67 #define T_RENAMECTSTR "rename"
68 #define T_MODOPADDSTR "add"
69 #define T_MODOPREPLACESTR "replace"
70 #define T_MODOPDELETESTR "delete"
71 #define T_MODSEPSTR "-"
72 #define T_NEWRDNSTR "newrdn"
73 #define T_DELETEOLDRDNSTR "deleteoldrdn"
74 #define T_NEWSUPSTR "newsuperior"
77 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
78 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
79 static void addmodifyop LDAP_P((
80 LDAPMod ***pmodsp, int modop,
82 struct berval *value ));
83 static int domodify LDAP_P((
87 static int dodelete LDAP_P((
89 static int dorename LDAP_P((
94 static char *read_one_record LDAP_P(( FILE *fp ));
97 usage( const char *prog )
100 "Add or modify entries from an LDAP server\n\n"
101 "usage: %s [options]\n"
102 " The list of desired operations are read from stdin or from the file\n"
103 " specified by \"-f file\".\n"
104 "Add or modify options:\n"
105 " -a add values (default%s)\n"
106 " -c continuous operation mode (do not stop on errors)\n"
107 " -f file read operations from `file'\n"
108 " -F force all changes records to be used\n"
109 " -S file write skipped modifications to `file'\n"
112 " -d level set LDAP debugging level to `level'\n"
113 " -D binddn bind DN\n"
114 " -h host LDAP server\n"
115 " -H URI LDAP Uniform Resource Indentifier(s)\n"
116 " -I use SASL Interactive mode\n"
117 " -k use Kerberos authentication\n"
118 " -K like -k, but do only step 1 of the Kerberos bind\n"
119 " -M enable Manage DSA IT control (-MM to make critical)\n"
120 " -n show what would be done but don't actually update\n"
121 " -O props SASL security properties\n"
122 " -p port port on LDAP server\n"
123 " -P version procotol version (default: 3)\n"
124 " -Q use SASL Quiet mode\n"
125 " -R realm SASL realm\n"
126 " -U authcid SASL authentication identity\n"
127 " -v run in verbose mode (diagnostics to standard output)\n"
128 " -w passwd bind passwd (for simple authentication)\n"
129 " -W prompt for bind passwd\n"
130 " -x Simple authentication\n"
131 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
132 " -Y mech SASL mechanism\n"
133 " -Z Start TLS request (-ZZ to require successful response)\n"
134 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
136 exit( EXIT_FAILURE );
141 main( int argc, char **argv )
143 char *infile, *rejfile, *rbuf, *start, *rejbuf;
145 char *matched_msg = NULL, *error_msg = NULL;
146 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
149 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
155 /* Print usage when no parameters */
156 if( argc < 2 ) usage( prog );
158 /* strncmp instead of strcmp since NT binaries carry .exe extension */
159 ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
163 not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
167 while (( i = getopt( argc, argv, "acrf:F"
168 "Cd:D:h:H:IkKMnO:p:P:QR:S:U:vw:WxX:Y:Z" )) != EOF )
175 case 'c': /* continuous operation */
178 case 'f': /* read from file */
179 if( infile != NULL ) {
180 fprintf( stderr, "%s: -f previously specified\n", prog );
183 infile = strdup( optarg );
185 case 'F': /* force all changes records to be used */
194 debug |= atoi( optarg );
196 case 'D': /* bind DN */
197 if( binddn != NULL ) {
198 fprintf( stderr, "%s: -D previously specified\n", prog );
201 binddn = strdup( optarg );
203 case 'h': /* ldap host */
204 if( ldapuri != NULL ) {
205 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
208 if( ldaphost != NULL ) {
209 fprintf( stderr, "%s: -h previously specified\n", prog );
212 ldaphost = strdup( optarg );
214 case 'H': /* ldap URI */
215 if( ldaphost != NULL ) {
216 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
220 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
223 if( ldapuri != NULL ) {
224 fprintf( stderr, "%s: -H previously specified\n", prog );
227 ldapuri = strdup( optarg );
230 #ifdef HAVE_CYRUS_SASL
231 if( version == LDAP_VERSION2 ) {
232 fprintf( stderr, "%s: -I incompatible with version %d\n",
236 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
237 fprintf( stderr, "%s: incompatible previous "
238 "authentication choice\n",
242 authmethod = LDAP_AUTH_SASL;
243 version = LDAP_VERSION3;
244 sasl_flags = LDAP_SASL_INTERACTIVE;
247 fprintf( stderr, "%s: was not compiled with SASL support\n",
249 return( EXIT_FAILURE );
251 case 'k': /* kerberos bind */
252 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
253 if( version > LDAP_VERSION2 ) {
254 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
259 if( authmethod != -1 ) {
260 fprintf( stderr, "%s: -k incompatible with previous "
261 "authentication choice\n", prog );
265 authmethod = LDAP_AUTH_KRBV4;
267 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
271 case 'K': /* kerberos bind, part one only */
272 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
273 if( version > LDAP_VERSION2 ) {
274 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
278 if( authmethod != -1 ) {
279 fprintf( stderr, "%s: incompatible with previous "
280 "authentication choice\n", prog );
284 authmethod = LDAP_AUTH_KRBV41;
286 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
287 return( EXIT_FAILURE );
291 /* enable Manage DSA IT */
292 if( version == LDAP_VERSION2 ) {
293 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
298 version = LDAP_VERSION3;
300 case 'n': /* print deletes, don't actually do them */
304 #ifdef HAVE_CYRUS_SASL
305 if( sasl_secprops != NULL ) {
306 fprintf( stderr, "%s: -O previously specified\n", prog );
309 if( version == LDAP_VERSION2 ) {
310 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
314 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
315 fprintf( stderr, "%s: incompatible previous "
316 "authentication choice\n", prog );
319 authmethod = LDAP_AUTH_SASL;
320 version = LDAP_VERSION3;
321 sasl_secprops = strdup( optarg );
323 fprintf( stderr, "%s: not compiled with SASL support\n",
325 return( EXIT_FAILURE );
330 fprintf( stderr, "%s: -p previously specified\n", prog );
333 ldapport = atoi( optarg );
336 switch( atoi(optarg) ) {
338 if( version == LDAP_VERSION3 ) {
339 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
343 version = LDAP_VERSION2;
346 if( version == LDAP_VERSION2 ) {
347 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
351 version = LDAP_VERSION3;
354 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
357 return( EXIT_FAILURE );
360 #ifdef HAVE_CYRUS_SASL
361 if( version == LDAP_VERSION2 ) {
362 fprintf( stderr, "%s: -Q incompatible with version %d\n",
366 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
367 fprintf( stderr, "%s: incompatible previous "
368 "authentication choice\n",
372 authmethod = LDAP_AUTH_SASL;
373 version = LDAP_VERSION3;
374 sasl_flags = LDAP_SASL_QUIET;
377 fprintf( stderr, "%s: not compiled with SASL support\n",
379 return( EXIT_FAILURE );
381 case 'r': /* replace (obsolete) */
385 #ifdef HAVE_CYRUS_SASL
386 if( sasl_realm != NULL ) {
387 fprintf( stderr, "%s: -R previously specified\n", prog );
390 if( version == LDAP_VERSION2 ) {
391 fprintf( stderr, "%s: -R incompatible with version %d\n",
395 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
396 fprintf( stderr, "%s: incompatible previous "
397 "authentication choice\n",
401 authmethod = LDAP_AUTH_SASL;
402 version = LDAP_VERSION3;
403 sasl_realm = strdup( optarg );
405 fprintf( stderr, "%s: not compiled with SASL support\n",
407 return( EXIT_FAILURE );
410 case 'S': /* skipped modifications to file */
411 if( rejfile != NULL ) {
412 fprintf( stderr, "%s: -S previously specified\n", prog );
415 rejfile = strdup( optarg );
418 #ifdef HAVE_CYRUS_SASL
419 if( sasl_authc_id != NULL ) {
420 fprintf( stderr, "%s: -U previously specified\n", prog );
423 if( version == LDAP_VERSION2 ) {
424 fprintf( stderr, "%s: -U incompatible with version %d\n",
428 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
429 fprintf( stderr, "%s: incompatible previous "
430 "authentication choice\n",
434 authmethod = LDAP_AUTH_SASL;
435 version = LDAP_VERSION3;
436 sasl_authc_id = strdup( optarg );
438 fprintf( stderr, "%s: not compiled with SASL support\n",
440 return( EXIT_FAILURE );
443 case 'v': /* verbose mode */
446 case 'w': /* password */
447 passwd.bv_val = strdup( optarg );
451 for( p = optarg; *p != '\0'; p++ ) {
455 passwd.bv_len = strlen( passwd.bv_val );
461 #ifdef HAVE_CYRUS_SASL
462 if( sasl_mech != NULL ) {
463 fprintf( stderr, "%s: -Y previously specified\n", prog );
466 if( version == LDAP_VERSION2 ) {
467 fprintf( stderr, "%s: -Y incompatible with version %d\n",
471 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
472 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
475 authmethod = LDAP_AUTH_SASL;
476 version = LDAP_VERSION3;
477 sasl_mech = strdup( optarg );
479 fprintf( stderr, "%s: not compiled with SASL support\n",
481 return( EXIT_FAILURE );
485 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
486 fprintf( stderr, "%s: incompatible with previous "
487 "authentication choice\n", prog );
490 authmethod = LDAP_AUTH_SIMPLE;
493 #ifdef HAVE_CYRUS_SASL
494 if( sasl_authz_id != NULL ) {
495 fprintf( stderr, "%s: -X previously specified\n", prog );
498 if( version == LDAP_VERSION2 ) {
499 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
503 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
504 fprintf( stderr, "%s: -X incompatible with "
505 "authentication choice\n", prog );
508 authmethod = LDAP_AUTH_SASL;
509 version = LDAP_VERSION3;
510 sasl_authz_id = strdup( optarg );
512 fprintf( stderr, "%s: not compiled with SASL support\n",
514 return( EXIT_FAILURE );
519 if( version == LDAP_VERSION2 ) {
520 fprintf( stderr, "%s: -Z incompatible with version %d\n",
524 version = LDAP_VERSION3;
527 fprintf( stderr, "%s: not compiled with TLS support\n",
529 return( EXIT_FAILURE );
533 fprintf( stderr, "%s: unrecognized option -%c\n",
540 version = LDAP_VERSION3;
542 if (authmethod == -1 && version > LDAP_VERSION2) {
543 #ifdef HAVE_CYRUS_SASL
544 authmethod = LDAP_AUTH_SASL;
546 authmethod = LDAP_AUTH_SIMPLE;
550 if ( argc != optind )
553 if ( rejfile != NULL ) {
554 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
556 return( EXIT_FAILURE );
562 if ( infile != NULL ) {
563 if (( fp = fopen( infile, "r" )) == NULL ) {
565 return( EXIT_FAILURE );
572 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
573 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
575 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
576 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
582 (void) SIGNAL( SIGPIPE, SIG_IGN );
586 lutil_log_initialize( argc, argv );
590 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
592 fprintf( stderr, "ldap_init( %s, %d )\n",
593 ldaphost != NULL ? ldaphost : "<DEFAULT>",
597 ld = ldap_init( ldaphost, ldapport );
599 perror("ldapmodify: ldap_init");
605 fprintf( stderr, "ldap_initialize( %s )\n",
606 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
609 rc = ldap_initialize( &ld, ldapuri );
610 if( rc != LDAP_SUCCESS ) {
611 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
612 rc, ldap_err2string(rc) );
618 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
619 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
621 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
622 referrals ? "on" : "off" );
627 if (version == -1 ) {
628 version = LDAP_VERSION3;
631 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
632 != LDAP_OPT_SUCCESS )
634 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
639 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
640 ldap_perror( ld, "ldap_start_tls" );
642 return( EXIT_FAILURE );
647 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
648 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
651 if ( authmethod == LDAP_AUTH_SASL ) {
652 #ifdef HAVE_CYRUS_SASL
655 if( sasl_secprops != NULL ) {
656 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
657 (void *) sasl_secprops );
659 if( rc != LDAP_OPT_SUCCESS ) {
661 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
663 return( EXIT_FAILURE );
667 defaults = lutil_sasl_defaults( ld,
674 rc = ldap_sasl_interactive_bind_s( ld, binddn,
675 sasl_mech, NULL, NULL,
676 sasl_flags, lutil_sasl_interact, defaults );
678 if( rc != LDAP_SUCCESS ) {
679 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
680 return( EXIT_FAILURE );
683 fprintf( stderr, "%s: not compiled with SASL support\n",
685 return( EXIT_FAILURE );
689 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
691 ldap_perror( ld, "ldap_bind" );
692 return( EXIT_FAILURE );
704 LDAPControl *ctrls[2];
708 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
709 c.ldctl_value.bv_val = NULL;
710 c.ldctl_value.bv_len = 0;
711 c.ldctl_iscritical = manageDSAit > 1;
713 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
715 if( err != LDAP_OPT_SUCCESS ) {
716 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
717 c.ldctl_iscritical ? "critical " : "" );
718 if( c.ldctl_iscritical ) {
719 exit( EXIT_FAILURE );
725 while (( rc == 0 || contoper ) &&
726 ( rbuf = read_one_record( fp )) != NULL ) {
732 len = strlen( rbuf );
733 if (( rejbuf = (char *)malloc( len+1 )) == NULL ) {
735 exit( EXIT_FAILURE );
737 memcpy( rejbuf, rbuf, len+1 );
740 rc = process_ldif_rec( start, count );
743 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
745 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
746 if ( matched_msg != NULL && *matched_msg != '\0' ) {
747 fprintf( rejfp, ", matched DN: %s", matched_msg );
750 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
751 if ( error_msg != NULL && *error_msg != '\0' ) {
752 fprintf( rejfp, ", additional info: %s", error_msg );
754 fprintf( rejfp, "\n%s\n", rejbuf );
765 if ( rejfp != NULL ) {
774 process_ldif_rec( char *rbuf, int count )
776 char *line, *dn, *type, *newrdn, *newsup, *p;
777 int rc, linenum, modop, replicaport;
778 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
779 int expect_deleteoldrdn, deleteoldrdn;
780 int saw_replica, use_record, new_entry, delete_entry, got_all;
787 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
788 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
789 expect_sep = expect_ct = 0;
795 dn = newrdn = newsup = NULL;
797 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
800 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
806 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
807 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
808 prog, linenum, dn == NULL ? "" : dn );
809 rc = LDAP_PARAM_ERROR;
814 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
816 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
820 replicaport = atoi( p );
822 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
823 replicaport == ldapport ) {
826 } else if ( count == 1 && linenum == 1 &&
827 strcasecmp( type, T_VERSION_STR ) == 0 )
829 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
830 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
831 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
835 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
836 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
838 exit( EXIT_FAILURE );
842 goto end_line; /* skip all lines until we see "dn:" */
847 if ( !use_record && saw_replica ) {
848 printf( "%s: skipping change record for entry: %s\n"
849 "\t(LDAP host/port does not match replica: lines)\n",
853 ber_memfree( val.bv_val );
857 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
858 #ifdef LIBERAL_CHANGETYPE_MODOP
859 /* trim trailing spaces (and log warning ...) */
862 for ( icnt = val.bv_len; --icnt > 0; ) {
863 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
868 if ( ++icnt != val.bv_len ) {
869 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
870 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
871 val.bv_val[icnt] = '\0';
873 #endif /* LIBERAL_CHANGETYPE_MODOP */
875 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
878 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
880 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
881 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
882 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
885 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
886 got_all = delete_entry = 1;
889 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
890 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
891 rc = LDAP_PARAM_ERROR;
894 } else if ( ldapadd ) { /* missing changetype => add */
896 modop = LDAP_MOD_ADD;
898 expect_modop = 1; /* missing changetype => modify */
902 if ( expect_modop ) {
903 #ifdef LIBERAL_CHANGETYPE_MODOP
904 /* trim trailing spaces (and log warning ...) */
907 for ( icnt = val.bv_len; --icnt > 0; ) {
908 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
913 if ( ++icnt != val.bv_len ) {
914 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
915 prog, type, val.bv_val, linenum, dn );
916 val.bv_val[icnt] = '\0';
918 #endif /* LIBERAL_CHANGETYPE_MODOP */
922 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
923 modop = LDAP_MOD_ADD;
925 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
926 modop = LDAP_MOD_REPLACE;
927 addmodifyop( &pmods, modop, val.bv_val, NULL );
929 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
930 modop = LDAP_MOD_DELETE;
931 addmodifyop( &pmods, modop, val.bv_val, NULL );
933 } else { /* no modify op: use default */
934 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
938 if ( expect_newrdn ) {
939 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
940 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
942 exit( EXIT_FAILURE );
944 expect_deleteoldrdn = 1;
947 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
948 prog, T_NEWRDNSTR, type, linenum, dn );
949 rc = LDAP_PARAM_ERROR;
951 } else if ( expect_deleteoldrdn ) {
952 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
953 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
954 expect_deleteoldrdn = 0;
958 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
959 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
960 rc = LDAP_PARAM_ERROR;
962 } else if ( expect_newsup ) {
963 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
964 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
966 exit( EXIT_FAILURE );
970 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
971 prog, T_NEWSUPSTR, type, linenum, dn );
972 rc = LDAP_PARAM_ERROR;
974 } else if ( got_all ) {
976 "%s: extra lines at end (line %d of entry \"%s\")\n",
978 rc = LDAP_PARAM_ERROR;
980 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
985 ber_memfree( val.bv_val );
992 if( version && linenum == 1 ) {
997 if ( delete_entry ) {
999 } else if ( newrdn != NULL ) {
1000 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
1002 rc = domodify( dn, pmods, new_entry );
1005 if ( rc == LDAP_SUCCESS ) {
1013 if ( newrdn != NULL ) {
1016 if ( pmods != NULL ) {
1017 ldap_mods_free( pmods, 1 );
1029 struct berval *val )
1035 modop |= LDAP_MOD_BVALUES;
1038 if ( pmods != NULL ) {
1039 for ( ; pmods[ i ] != NULL; ++i ) {
1040 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1041 pmods[ i ]->mod_op == modop )
1048 if ( pmods == NULL || pmods[ i ] == NULL ) {
1049 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1050 sizeof( LDAPMod * ))) == NULL )
1052 perror( "realloc" );
1053 exit( EXIT_FAILURE );
1057 pmods[ i + 1 ] = NULL;
1059 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1060 if ( pmods[ i ] == NULL ) {
1062 exit( EXIT_FAILURE );
1065 pmods[ i ]->mod_op = modop;
1066 pmods[ i ]->mod_type = ber_strdup( attr );
1067 if ( pmods[ i ]->mod_type == NULL ) {
1069 exit( EXIT_FAILURE );
1073 if ( val != NULL ) {
1075 if ( pmods[ i ]->mod_bvalues != NULL ) {
1076 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1081 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1082 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1083 if ( pmods[ i ]->mod_bvalues == NULL ) {
1084 perror( "ber_realloc" );
1085 exit( EXIT_FAILURE );
1088 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1089 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1090 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1091 perror( "ber_bvdup" );
1092 exit( EXIT_FAILURE );
1104 int i, j, k, notascii, op;
1107 if ( pmods == NULL ) {
1108 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1110 return( LDAP_PARAM_ERROR );
1113 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1114 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1115 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1117 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1118 prog, pmods[i]->mod_type, dn );
1119 return LDAP_PARAM_ERROR;
1124 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1125 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1126 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1127 "replace" : op == LDAP_MOD_ADD ?
1128 "add" : "delete", pmods[ i ]->mod_type );
1129 if ( pmods[ i ]->mod_bvalues != NULL ) {
1130 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1131 bvp = pmods[ i ]->mod_bvalues[ j ];
1133 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1134 if ( !isascii( bvp->bv_val[ k ] )) {
1140 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1142 printf( "\t%s\n", bvp->bv_val );
1150 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1152 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1157 i = ldap_add_s( ld, dn, pmods );
1159 i = ldap_modify_s( ld, dn, pmods );
1161 if ( i != LDAP_SUCCESS ) {
1162 /* print error message about failed update including DN */
1163 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1164 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1165 } else if ( verbose ) {
1166 printf( "modify complete\n" );
1184 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1186 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1187 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1188 ldap_perror( ld, "ldap_delete" );
1189 } else if ( verbose ) {
1190 printf( "delete complete" );
1212 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1214 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1215 newrdn, deleteoldrdn ? "do not " : "" );
1218 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1221 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1222 ldap_perror( ld, "ldap_modrdn" );
1224 printf( "modrdn completed\n" );
1237 read_one_record( FILE *fp )
1239 char *buf, line[ LDAPMOD_MAXLINE ];
1245 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1246 int len = strlen( line );
1248 if( len < 2 || ( len == 3 && *line == '\r' )) {
1256 if ( lcur + len + 1 > lmax ) {
1257 lmax = LDAPMOD_MAXLINE
1258 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1260 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1261 perror( "realloc" );
1262 exit( EXIT_FAILURE );
1266 strcpy( buf + lcur, line );