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 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 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
115 " [!]manageDSAit (alternate form, see -M)\n"
116 #ifdef LDAP_CONTROL_NOOP
119 " -f file read operations from `file'\n"
120 " -h host LDAP server\n"
121 " -H URI LDAP Uniform Resource Indentifier(s)\n"
122 " -I use SASL Interactive mode\n"
123 " -k use Kerberos authentication\n"
124 " -K like -k, but do only step 1 of the Kerberos bind\n"
125 " -M enable Manage DSA IT control (-MM to make critical)\n"
126 " -n show what would be done but don't actually update\n"
127 " -O props SASL security properties\n"
128 " -p port port on LDAP server\n"
129 " -P version procotol version (default: 3)\n"
130 " -Q use SASL Quiet mode\n"
131 " -R realm SASL realm\n"
132 " -U authcid SASL authentication identity\n"
133 " -v run in verbose mode (diagnostics to standard output)\n"
134 " -w passwd bind passwd (for simple authentication)\n"
135 " -W prompt for bind passwd\n"
136 " -x Simple authentication\n"
137 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
138 " -y file Read passwd from file\n"
139 " -Y mech SASL mechanism\n"
140 " -Z Start TLS request (-ZZ to require successful response)\n"
141 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
143 exit( EXIT_FAILURE );
148 main( int argc, char **argv )
150 char *infile, *rejfile, *rbuf, *start, *rejbuf = NULL;
152 char *matched_msg = NULL, *error_msg = NULL;
153 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
155 char *pw_file = NULL;
156 char *control, *cvalue;
158 #ifdef LDAP_CONTROL_NOOP
162 prog = lutil_progname( "ldapmodify", argc, argv );
164 /* Print usage when no parameters */
165 if( argc < 2 ) usage( prog );
167 /* strncmp instead of strcmp since NT binaries carry .exe extension */
168 ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
172 not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
176 while (( i = getopt( argc, argv, "acrf:E:F"
177 "Cd:D:e:h:H:IkKMnO:p:P:QR:S:U:vw:WxX:y:Y:Z" )) != EOF )
184 case 'c': /* continuous operation */
187 case 'E': /* modify controls */
188 if( version == LDAP_VERSION2 ) {
189 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
194 /* should be extended to support comma separated list of
195 * [!]key[=value] parameters, e.g. -E !foo,bar=567
200 if( optarg[0] == '!' ) {
205 control = strdup( optarg );
206 if ( (cvalue = strchr( control, '=' )) != NULL ) {
209 fprintf( stderr, "Invalid modify control name: %s\n", control );
212 case 'f': /* read from file */
213 if( infile != NULL ) {
214 fprintf( stderr, "%s: -f previously specified\n", prog );
217 infile = strdup( optarg );
219 case 'F': /* force all changes records to be used */
228 debug |= atoi( optarg );
230 case 'D': /* bind DN */
231 if( binddn != NULL ) {
232 fprintf( stderr, "%s: -D previously specified\n", prog );
235 binddn = strdup( optarg );
237 case 'e': /* general controls */
238 if( version == LDAP_VERSION2 ) {
239 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
244 /* should be extended to support comma separated list of
245 * [!]key[=value] parameters, e.g. -e !foo,bar=567
250 if( optarg[0] == '!' ) {
255 control = strdup( optarg );
256 if ( (cvalue = strchr( control, '=' )) != NULL ) {
260 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
261 if( cvalue != NULL ) {
262 fprintf( stderr, "manageDSAit: no control value expected" );
267 manageDSAit = 1 + crit;
271 #ifdef LDAP_CONTROL_NOOP
272 } else if ( strcasecmp( control, "noop" ) == 0 ) {
273 if( cvalue != NULL ) {
274 fprintf( stderr, "noop: no control value expected" );
285 fprintf( stderr, "Invalid general control name: %s\n", control );
289 case 'h': /* ldap host */
290 if( ldapuri != NULL ) {
291 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
294 if( ldaphost != NULL ) {
295 fprintf( stderr, "%s: -h previously specified\n", prog );
298 ldaphost = strdup( optarg );
300 case 'H': /* ldap URI */
301 if( ldaphost != NULL ) {
302 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
306 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
309 if( ldapuri != NULL ) {
310 fprintf( stderr, "%s: -H previously specified\n", prog );
313 ldapuri = strdup( optarg );
316 #ifdef HAVE_CYRUS_SASL
317 if( version == LDAP_VERSION2 ) {
318 fprintf( stderr, "%s: -I incompatible with version %d\n",
322 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
323 fprintf( stderr, "%s: incompatible previous "
324 "authentication choice\n",
328 authmethod = LDAP_AUTH_SASL;
329 version = LDAP_VERSION3;
330 sasl_flags = LDAP_SASL_INTERACTIVE;
333 fprintf( stderr, "%s: was not compiled with SASL support\n",
335 return( EXIT_FAILURE );
337 case 'k': /* kerberos bind */
338 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
339 if( version > LDAP_VERSION2 ) {
340 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
345 if( authmethod != -1 ) {
346 fprintf( stderr, "%s: -k incompatible with previous "
347 "authentication choice\n", prog );
351 authmethod = LDAP_AUTH_KRBV4;
353 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
357 case 'K': /* kerberos bind, part one only */
358 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
359 if( version > LDAP_VERSION2 ) {
360 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
364 if( authmethod != -1 ) {
365 fprintf( stderr, "%s: incompatible with previous "
366 "authentication choice\n", prog );
370 authmethod = LDAP_AUTH_KRBV41;
372 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
373 return( EXIT_FAILURE );
377 /* enable Manage DSA IT */
378 if( version == LDAP_VERSION2 ) {
379 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
384 version = LDAP_VERSION3;
386 case 'n': /* print deletes, don't actually do them */
390 #ifdef HAVE_CYRUS_SASL
391 if( sasl_secprops != NULL ) {
392 fprintf( stderr, "%s: -O previously specified\n", prog );
395 if( version == LDAP_VERSION2 ) {
396 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
400 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
401 fprintf( stderr, "%s: incompatible previous "
402 "authentication choice\n", prog );
405 authmethod = LDAP_AUTH_SASL;
406 version = LDAP_VERSION3;
407 sasl_secprops = strdup( optarg );
409 fprintf( stderr, "%s: not compiled with SASL support\n",
411 return( EXIT_FAILURE );
416 fprintf( stderr, "%s: -p previously specified\n", prog );
419 ldapport = atoi( optarg );
422 switch( atoi(optarg) ) {
424 if( version == LDAP_VERSION3 ) {
425 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
429 version = LDAP_VERSION2;
432 if( version == LDAP_VERSION2 ) {
433 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
437 version = LDAP_VERSION3;
440 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
443 return( EXIT_FAILURE );
446 #ifdef HAVE_CYRUS_SASL
447 if( version == LDAP_VERSION2 ) {
448 fprintf( stderr, "%s: -Q incompatible with version %d\n",
452 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
453 fprintf( stderr, "%s: incompatible previous "
454 "authentication choice\n",
458 authmethod = LDAP_AUTH_SASL;
459 version = LDAP_VERSION3;
460 sasl_flags = LDAP_SASL_QUIET;
463 fprintf( stderr, "%s: not compiled with SASL support\n",
465 return( EXIT_FAILURE );
467 case 'r': /* replace (obsolete) */
471 #ifdef HAVE_CYRUS_SASL
472 if( sasl_realm != NULL ) {
473 fprintf( stderr, "%s: -R previously specified\n", prog );
476 if( version == LDAP_VERSION2 ) {
477 fprintf( stderr, "%s: -R incompatible with version %d\n",
481 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
482 fprintf( stderr, "%s: incompatible previous "
483 "authentication choice\n",
487 authmethod = LDAP_AUTH_SASL;
488 version = LDAP_VERSION3;
489 sasl_realm = strdup( optarg );
491 fprintf( stderr, "%s: not compiled with SASL support\n",
493 return( EXIT_FAILURE );
496 case 'S': /* skipped modifications to file */
497 if( rejfile != NULL ) {
498 fprintf( stderr, "%s: -S previously specified\n", prog );
501 rejfile = strdup( optarg );
504 #ifdef HAVE_CYRUS_SASL
505 if( sasl_authc_id != NULL ) {
506 fprintf( stderr, "%s: -U previously specified\n", prog );
509 if( version == LDAP_VERSION2 ) {
510 fprintf( stderr, "%s: -U incompatible with version %d\n",
514 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
515 fprintf( stderr, "%s: incompatible previous "
516 "authentication choice\n",
520 authmethod = LDAP_AUTH_SASL;
521 version = LDAP_VERSION3;
522 sasl_authc_id = strdup( optarg );
524 fprintf( stderr, "%s: not compiled with SASL support\n",
526 return( EXIT_FAILURE );
529 case 'v': /* verbose mode */
532 case 'w': /* password */
533 passwd.bv_val = strdup( optarg );
537 for( p = optarg; *p != '\0'; p++ ) {
541 passwd.bv_len = strlen( passwd.bv_val );
550 #ifdef HAVE_CYRUS_SASL
551 if( sasl_mech != NULL ) {
552 fprintf( stderr, "%s: -Y previously specified\n", prog );
555 if( version == LDAP_VERSION2 ) {
556 fprintf( stderr, "%s: -Y incompatible with version %d\n",
560 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
561 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
564 authmethod = LDAP_AUTH_SASL;
565 version = LDAP_VERSION3;
566 sasl_mech = strdup( optarg );
568 fprintf( stderr, "%s: not compiled with SASL support\n",
570 return( EXIT_FAILURE );
574 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
575 fprintf( stderr, "%s: incompatible with previous "
576 "authentication choice\n", prog );
579 authmethod = LDAP_AUTH_SIMPLE;
582 #ifdef HAVE_CYRUS_SASL
583 if( sasl_authz_id != NULL ) {
584 fprintf( stderr, "%s: -X previously specified\n", prog );
587 if( version == LDAP_VERSION2 ) {
588 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
592 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
593 fprintf( stderr, "%s: -X incompatible with "
594 "authentication choice\n", prog );
597 authmethod = LDAP_AUTH_SASL;
598 version = LDAP_VERSION3;
599 sasl_authz_id = strdup( optarg );
601 fprintf( stderr, "%s: not compiled with SASL support\n",
603 return( EXIT_FAILURE );
608 if( version == LDAP_VERSION2 ) {
609 fprintf( stderr, "%s: -Z incompatible with version %d\n",
613 version = LDAP_VERSION3;
616 fprintf( stderr, "%s: not compiled with TLS support\n",
618 return( EXIT_FAILURE );
622 fprintf( stderr, "%s: unrecognized option -%c\n",
629 version = LDAP_VERSION3;
631 if (authmethod == -1 && version > LDAP_VERSION2) {
632 #ifdef HAVE_CYRUS_SASL
633 authmethod = LDAP_AUTH_SASL;
635 authmethod = LDAP_AUTH_SIMPLE;
639 if ( argc != optind )
642 if ( rejfile != NULL ) {
643 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
645 return( EXIT_FAILURE );
651 if ( infile != NULL ) {
652 if (( fp = fopen( infile, "r" )) == NULL ) {
654 return( EXIT_FAILURE );
661 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
662 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
664 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
665 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
671 (void) SIGNAL( SIGPIPE, SIG_IGN );
675 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
677 fprintf( stderr, "ldap_init( %s, %d )\n",
678 ldaphost != NULL ? ldaphost : "<DEFAULT>",
682 ld = ldap_init( ldaphost, ldapport );
684 perror("ldapmodify: ldap_init");
690 fprintf( stderr, "ldap_initialize( %s )\n",
691 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
694 rc = ldap_initialize( &ld, ldapuri );
695 if( rc != LDAP_SUCCESS ) {
696 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
697 rc, ldap_err2string(rc) );
703 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
704 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
706 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
707 referrals ? "on" : "off" );
712 if (version == -1 ) {
713 version = LDAP_VERSION3;
716 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
717 != LDAP_OPT_SUCCESS )
719 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
724 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
725 ldap_perror( ld, "ldap_start_tls" );
727 return( EXIT_FAILURE );
731 if ( pw_file || want_bindpw ) {
733 rc = lutil_get_filed_password( pw_file, &passwd );
734 if( rc ) return EXIT_FAILURE;
736 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
737 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
741 if ( authmethod == LDAP_AUTH_SASL ) {
742 #ifdef HAVE_CYRUS_SASL
745 if( sasl_secprops != NULL ) {
746 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
747 (void *) sasl_secprops );
749 if( rc != LDAP_OPT_SUCCESS ) {
751 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
753 return( EXIT_FAILURE );
757 defaults = lutil_sasl_defaults( ld,
764 rc = ldap_sasl_interactive_bind_s( ld, binddn,
765 sasl_mech, NULL, NULL,
766 sasl_flags, lutil_sasl_interact, defaults );
768 if( rc != LDAP_SUCCESS ) {
769 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
770 return( EXIT_FAILURE );
773 fprintf( stderr, "%s: not compiled with SASL support\n",
775 return( EXIT_FAILURE );
779 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
781 ldap_perror( ld, "ldap_bind" );
782 return( EXIT_FAILURE );
792 #ifdef LDAP_CONTROL_NOOP
798 LDAPControl *ctrls[3];
803 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
804 c1.ldctl_value.bv_val = NULL;
805 c1.ldctl_value.bv_len = 0;
806 c1.ldctl_iscritical = manageDSAit > 1;
809 #ifdef LDAP_CONTROL_NOOP
814 c2.ldctl_oid = LDAP_CONTROL_NOOP;
815 c2.ldctl_value.bv_val = NULL;
816 c2.ldctl_value.bv_len = 0;
817 c2.ldctl_iscritical = noop > 1;
821 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
823 if( err != LDAP_OPT_SUCCESS ) {
824 fprintf( stderr, "Could not set %scontrols\n",
825 (c1.ldctl_iscritical || c2.ldctl_iscritical)
826 ? "critical " : "" );
827 if ( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
834 while (( rc == 0 || contoper ) &&
835 ( rbuf = read_one_record( fp )) != NULL ) {
841 len = strlen( rbuf );
842 if (( rejbuf = (char *)malloc( len+1 )) == NULL ) {
844 exit( EXIT_FAILURE );
846 memcpy( rejbuf, rbuf, len+1 );
849 rc = process_ldif_rec( start, count );
852 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
854 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
855 if ( matched_msg != NULL && *matched_msg != '\0' ) {
856 fprintf( rejfp, ", matched DN: %s", matched_msg );
859 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
860 if ( error_msg != NULL && *error_msg != '\0' ) {
861 fprintf( rejfp, ", additional info: %s", error_msg );
863 fprintf( rejfp, "\n%s\n", rejbuf );
874 if ( rejfp != NULL ) {
883 process_ldif_rec( char *rbuf, int count )
885 char *line, *dn, *type, *newrdn, *newsup, *p;
886 int rc, linenum, modop, replicaport;
887 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
888 int expect_deleteoldrdn, deleteoldrdn;
889 int saw_replica, use_record, new_entry, delete_entry, got_all;
896 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
897 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
898 expect_sep = expect_ct = 0;
904 dn = newrdn = newsup = NULL;
906 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
909 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
915 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
916 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
917 prog, linenum, dn == NULL ? "" : dn );
918 rc = LDAP_PARAM_ERROR;
923 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
925 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
929 replicaport = atoi( p );
931 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
932 replicaport == ldapport ) {
935 } else if ( count == 1 && linenum == 1 &&
936 strcasecmp( type, T_VERSION_STR ) == 0 )
938 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
939 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
940 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
944 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
945 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
947 exit( EXIT_FAILURE );
951 goto end_line; /* skip all lines until we see "dn:" */
956 if ( !use_record && saw_replica ) {
957 printf( "%s: skipping change record for entry: %s\n"
958 "\t(LDAP host/port does not match replica: lines)\n",
962 ber_memfree( val.bv_val );
966 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
967 #ifdef LIBERAL_CHANGETYPE_MODOP
968 /* trim trailing spaces (and log warning ...) */
971 for ( icnt = val.bv_len; --icnt > 0; ) {
972 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
977 if ( ++icnt != val.bv_len ) {
978 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
979 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
980 val.bv_val[icnt] = '\0';
982 #endif /* LIBERAL_CHANGETYPE_MODOP */
984 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
987 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
989 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
990 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
991 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
994 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
995 got_all = delete_entry = 1;
998 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
999 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
1000 rc = LDAP_PARAM_ERROR;
1003 } else if ( ldapadd ) { /* missing changetype => add */
1005 modop = LDAP_MOD_ADD;
1007 expect_modop = 1; /* missing changetype => modify */
1011 if ( expect_modop ) {
1012 #ifdef LIBERAL_CHANGETYPE_MODOP
1013 /* trim trailing spaces (and log warning ...) */
1016 for ( icnt = val.bv_len; --icnt > 0; ) {
1017 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1022 if ( ++icnt != val.bv_len ) {
1023 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1024 prog, type, val.bv_val, linenum, dn );
1025 val.bv_val[icnt] = '\0';
1027 #endif /* LIBERAL_CHANGETYPE_MODOP */
1031 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
1032 modop = LDAP_MOD_ADD;
1034 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
1035 modop = LDAP_MOD_REPLACE;
1036 addmodifyop( &pmods, modop, val.bv_val, NULL );
1038 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
1039 modop = LDAP_MOD_DELETE;
1040 addmodifyop( &pmods, modop, val.bv_val, NULL );
1042 } else { /* no modify op: use default */
1043 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
1047 if ( expect_newrdn ) {
1048 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
1049 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
1051 exit( EXIT_FAILURE );
1053 expect_deleteoldrdn = 1;
1056 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1057 prog, T_NEWRDNSTR, type, linenum, dn );
1058 rc = LDAP_PARAM_ERROR;
1060 } else if ( expect_deleteoldrdn ) {
1061 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
1062 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
1063 expect_deleteoldrdn = 0;
1067 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1068 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
1069 rc = LDAP_PARAM_ERROR;
1071 } else if ( expect_newsup ) {
1072 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
1073 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
1075 exit( EXIT_FAILURE );
1079 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1080 prog, T_NEWSUPSTR, type, linenum, dn );
1081 rc = LDAP_PARAM_ERROR;
1083 } else if ( got_all ) {
1085 "%s: extra lines at end (line %d of entry \"%s\")\n",
1086 prog, linenum, dn );
1087 rc = LDAP_PARAM_ERROR;
1089 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
1093 ber_memfree( type );
1094 ber_memfree( val.bv_val );
1097 if( linenum == 0 ) {
1101 if( version && linenum == 1 ) {
1106 if ( delete_entry ) {
1107 rc = dodelete( dn );
1108 } else if ( newrdn != NULL ) {
1109 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
1111 rc = domodify( dn, pmods, new_entry );
1114 if ( rc == LDAP_SUCCESS ) {
1122 if ( newrdn != NULL ) {
1125 if ( pmods != NULL ) {
1126 ldap_mods_free( pmods, 1 );
1138 struct berval *val )
1144 modop |= LDAP_MOD_BVALUES;
1147 if ( pmods != NULL ) {
1148 for ( ; pmods[ i ] != NULL; ++i ) {
1149 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1150 pmods[ i ]->mod_op == modop )
1157 if ( pmods == NULL || pmods[ i ] == NULL ) {
1158 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1159 sizeof( LDAPMod * ))) == NULL )
1161 perror( "realloc" );
1162 exit( EXIT_FAILURE );
1166 pmods[ i + 1 ] = NULL;
1168 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1169 if ( pmods[ i ] == NULL ) {
1171 exit( EXIT_FAILURE );
1174 pmods[ i ]->mod_op = modop;
1175 pmods[ i ]->mod_type = ber_strdup( attr );
1176 if ( pmods[ i ]->mod_type == NULL ) {
1178 exit( EXIT_FAILURE );
1182 if ( val != NULL ) {
1184 if ( pmods[ i ]->mod_bvalues != NULL ) {
1185 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1190 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1191 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1192 if ( pmods[ i ]->mod_bvalues == NULL ) {
1193 perror( "ber_realloc" );
1194 exit( EXIT_FAILURE );
1197 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1198 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1199 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1200 perror( "ber_bvdup" );
1201 exit( EXIT_FAILURE );
1213 int i, j, k, notascii, op;
1216 if ( pmods == NULL ) {
1217 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1219 return( LDAP_PARAM_ERROR );
1222 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1223 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1224 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1226 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1227 prog, pmods[i]->mod_type, dn );
1228 return LDAP_PARAM_ERROR;
1233 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1234 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1235 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1236 "replace" : op == LDAP_MOD_ADD ?
1237 "add" : "delete", pmods[ i ]->mod_type );
1238 if ( pmods[ i ]->mod_bvalues != NULL ) {
1239 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1240 bvp = pmods[ i ]->mod_bvalues[ j ];
1242 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1243 if ( !isascii( bvp->bv_val[ k ] )) {
1249 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1251 printf( "\t%s\n", bvp->bv_val );
1259 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1261 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1266 i = ldap_add_s( ld, dn, pmods );
1268 i = ldap_modify_s( ld, dn, pmods );
1270 if ( i != LDAP_SUCCESS ) {
1271 /* print error message about failed update including DN */
1272 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1273 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1274 } else if ( verbose ) {
1275 printf( "modify complete\n" );
1293 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1295 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1296 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1297 ldap_perror( ld, "ldap_delete" );
1298 } else if ( verbose ) {
1299 printf( "delete complete" );
1321 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1323 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1324 newrdn, deleteoldrdn ? "do not " : "" );
1327 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1330 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1331 ldap_perror( ld, "ldap_modrdn" );
1333 printf( "modrdn completed\n" );
1346 read_one_record( FILE *fp )
1348 char *buf, line[ LDAPMOD_MAXLINE ];
1354 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1355 int len = strlen( line );
1357 if( len < 2 || ( len == 2 && *line == '\r' )) {
1365 if ( lcur + len + 1 > lmax ) {
1366 lmax = LDAPMOD_MAXLINE
1367 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1369 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1370 perror( "realloc" );
1371 exit( EXIT_FAILURE );
1375 strcpy( buf + lcur, line );